crProtocolHelper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.exceptionToError = exceptionToError;
  6. exports.getExceptionMessage = getExceptionMessage;
  7. exports.readProtocolStream = readProtocolStream;
  8. exports.releaseObject = releaseObject;
  9. exports.saveProtocolStream = saveProtocolStream;
  10. exports.toButtonsMask = toButtonsMask;
  11. exports.toConsoleMessageLocation = toConsoleMessageLocation;
  12. exports.toModifiersMask = toModifiersMask;
  13. var _fs = _interopRequireDefault(require("fs"));
  14. var _fileUtils = require("../../utils/fileUtils");
  15. var _stackTrace = require("../../utils/stackTrace");
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. /**
  18. * Copyright 2017 Google Inc. All rights reserved.
  19. * Modifications copyright (c) Microsoft Corporation.
  20. *
  21. * Licensed under the Apache License, Version 2.0 (the "License");
  22. * you may not use this file except in compliance with the License.
  23. * You may obtain a copy of the License at
  24. *
  25. * http://www.apache.org/licenses/LICENSE-2.0
  26. *
  27. * Unless required by applicable law or agreed to in writing, software
  28. * distributed under the License is distributed on an "AS IS" BASIS,
  29. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. * See the License for the specific language governing permissions and
  31. * limitations under the License.
  32. */
  33. function getExceptionMessage(exceptionDetails) {
  34. if (exceptionDetails.exception) return exceptionDetails.exception.description || String(exceptionDetails.exception.value);
  35. let message = exceptionDetails.text;
  36. if (exceptionDetails.stackTrace) {
  37. for (const callframe of exceptionDetails.stackTrace.callFrames) {
  38. const location = callframe.url + ':' + callframe.lineNumber + ':' + callframe.columnNumber;
  39. const functionName = callframe.functionName || '<anonymous>';
  40. message += `\n at ${functionName} (${location})`;
  41. }
  42. }
  43. return message;
  44. }
  45. async function releaseObject(client, objectId) {
  46. await client.send('Runtime.releaseObject', {
  47. objectId
  48. }).catch(error => {});
  49. }
  50. async function saveProtocolStream(client, handle, path) {
  51. let eof = false;
  52. await (0, _fileUtils.mkdirIfNeeded)(path);
  53. const fd = await _fs.default.promises.open(path, 'w');
  54. while (!eof) {
  55. const response = await client.send('IO.read', {
  56. handle
  57. });
  58. eof = response.eof;
  59. const buf = Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
  60. await fd.write(buf);
  61. }
  62. await fd.close();
  63. await client.send('IO.close', {
  64. handle
  65. });
  66. }
  67. async function readProtocolStream(client, handle) {
  68. let eof = false;
  69. const chunks = [];
  70. while (!eof) {
  71. const response = await client.send('IO.read', {
  72. handle
  73. });
  74. eof = response.eof;
  75. const buf = Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
  76. chunks.push(buf);
  77. }
  78. await client.send('IO.close', {
  79. handle
  80. });
  81. return Buffer.concat(chunks);
  82. }
  83. function toConsoleMessageLocation(stackTrace) {
  84. return stackTrace && stackTrace.callFrames.length ? {
  85. url: stackTrace.callFrames[0].url,
  86. lineNumber: stackTrace.callFrames[0].lineNumber,
  87. columnNumber: stackTrace.callFrames[0].columnNumber
  88. } : {
  89. url: '',
  90. lineNumber: 0,
  91. columnNumber: 0
  92. };
  93. }
  94. function exceptionToError(exceptionDetails) {
  95. const messageWithStack = getExceptionMessage(exceptionDetails);
  96. const lines = messageWithStack.split('\n');
  97. const firstStackTraceLine = lines.findIndex(line => line.startsWith(' at'));
  98. let messageWithName = '';
  99. let stack = '';
  100. if (firstStackTraceLine === -1) {
  101. messageWithName = messageWithStack;
  102. } else {
  103. messageWithName = lines.slice(0, firstStackTraceLine).join('\n');
  104. stack = messageWithStack;
  105. }
  106. const {
  107. name,
  108. message
  109. } = (0, _stackTrace.splitErrorMessage)(messageWithName);
  110. const err = new Error(message);
  111. err.stack = stack;
  112. err.name = name;
  113. return err;
  114. }
  115. function toModifiersMask(modifiers) {
  116. let mask = 0;
  117. if (modifiers.has('Alt')) mask |= 1;
  118. if (modifiers.has('Control')) mask |= 2;
  119. if (modifiers.has('Meta')) mask |= 4;
  120. if (modifiers.has('Shift')) mask |= 8;
  121. return mask;
  122. }
  123. function toButtonsMask(buttons) {
  124. let mask = 0;
  125. if (buttons.has('left')) mask |= 1;
  126. if (buttons.has('right')) mask |= 2;
  127. if (buttons.has('middle')) mask |= 4;
  128. return mask;
  129. }