crExecutionContext.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CRExecutionContext = void 0;
  6. var _crProtocolHelper = require("./crProtocolHelper");
  7. var js = _interopRequireWildcard(require("../javascript"));
  8. var _stackTrace = require("../../utils/stackTrace");
  9. var _utilityScriptSerializers = require("../isomorphic/utilityScriptSerializers");
  10. var _protocolError = require("../protocolError");
  11. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  12. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  13. /**
  14. * Copyright 2017 Google Inc. All rights reserved.
  15. * Modifications copyright (c) Microsoft Corporation.
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License");
  18. * you may not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS,
  25. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. */
  29. class CRExecutionContext {
  30. constructor(client, contextPayload) {
  31. this._client = void 0;
  32. this._contextId = void 0;
  33. this._client = client;
  34. this._contextId = contextPayload.id;
  35. }
  36. async rawEvaluateJSON(expression) {
  37. const {
  38. exceptionDetails,
  39. result: remoteObject
  40. } = await this._client.send('Runtime.evaluate', {
  41. expression,
  42. contextId: this._contextId,
  43. returnByValue: true
  44. }).catch(rewriteError);
  45. if (exceptionDetails) throw new js.JavaScriptErrorInEvaluate((0, _crProtocolHelper.getExceptionMessage)(exceptionDetails));
  46. return remoteObject.value;
  47. }
  48. async rawEvaluateHandle(expression) {
  49. const {
  50. exceptionDetails,
  51. result: remoteObject
  52. } = await this._client.send('Runtime.evaluate', {
  53. expression,
  54. contextId: this._contextId
  55. }).catch(rewriteError);
  56. if (exceptionDetails) throw new js.JavaScriptErrorInEvaluate((0, _crProtocolHelper.getExceptionMessage)(exceptionDetails));
  57. return remoteObject.objectId;
  58. }
  59. rawCallFunctionNoReply(func, ...args) {
  60. this._client.send('Runtime.callFunctionOn', {
  61. functionDeclaration: func.toString(),
  62. arguments: args.map(a => a instanceof js.JSHandle ? {
  63. objectId: a._objectId
  64. } : {
  65. value: a
  66. }),
  67. returnByValue: true,
  68. executionContextId: this._contextId,
  69. userGesture: true
  70. }).catch(() => {});
  71. }
  72. async evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds) {
  73. const {
  74. exceptionDetails,
  75. result: remoteObject
  76. } = await this._client.send('Runtime.callFunctionOn', {
  77. functionDeclaration: expression,
  78. objectId: utilityScript._objectId,
  79. arguments: [{
  80. objectId: utilityScript._objectId
  81. }, ...values.map(value => ({
  82. value
  83. })), ...objectIds.map(objectId => ({
  84. objectId
  85. }))],
  86. returnByValue,
  87. awaitPromise: true,
  88. userGesture: true
  89. }).catch(rewriteError);
  90. if (exceptionDetails) throw new js.JavaScriptErrorInEvaluate((0, _crProtocolHelper.getExceptionMessage)(exceptionDetails));
  91. return returnByValue ? (0, _utilityScriptSerializers.parseEvaluationResultValue)(remoteObject.value) : utilityScript._context.createHandle(remoteObject);
  92. }
  93. async getProperties(context, objectId) {
  94. const response = await this._client.send('Runtime.getProperties', {
  95. objectId,
  96. ownProperties: true
  97. });
  98. const result = new Map();
  99. for (const property of response.result) {
  100. if (!property.enumerable || !property.value) continue;
  101. result.set(property.name, context.createHandle(property.value));
  102. }
  103. return result;
  104. }
  105. createHandle(context, remoteObject) {
  106. return new js.JSHandle(context, remoteObject.subtype || remoteObject.type, renderPreview(remoteObject), remoteObject.objectId, potentiallyUnserializableValue(remoteObject));
  107. }
  108. async releaseHandle(objectId) {
  109. await (0, _crProtocolHelper.releaseObject)(this._client, objectId);
  110. }
  111. async objectCount(objectId) {
  112. const result = await this._client.send('Runtime.queryObjects', {
  113. prototypeObjectId: objectId
  114. });
  115. const match = result.objects.description.match(/Array\((\d+)\)/);
  116. return +match[1];
  117. }
  118. }
  119. exports.CRExecutionContext = CRExecutionContext;
  120. function rewriteError(error) {
  121. if (error.message.includes('Object reference chain is too long')) return {
  122. result: {
  123. type: 'undefined'
  124. }
  125. };
  126. if (error.message.includes('Object couldn\'t be returned by value')) return {
  127. result: {
  128. type: 'undefined'
  129. }
  130. };
  131. if (error instanceof TypeError && error.message.startsWith('Converting circular structure to JSON')) (0, _stackTrace.rewriteErrorMessage)(error, error.message + ' Are you passing a nested JSHandle?');
  132. if (!js.isJavaScriptErrorInEvaluate(error) && !(0, _protocolError.isSessionClosedError)(error)) throw new Error('Execution context was destroyed, most likely because of a navigation.');
  133. throw error;
  134. }
  135. function potentiallyUnserializableValue(remoteObject) {
  136. const value = remoteObject.value;
  137. const unserializableValue = remoteObject.unserializableValue;
  138. return unserializableValue ? js.parseUnserializableValue(unserializableValue) : value;
  139. }
  140. function renderPreview(object) {
  141. if (object.type === 'undefined') return 'undefined';
  142. if ('value' in object) return String(object.value);
  143. if (object.unserializableValue) return String(object.unserializableValue);
  144. if (object.description === 'Object' && object.preview) {
  145. const tokens = [];
  146. for (const {
  147. name,
  148. value
  149. } of object.preview.properties) tokens.push(`${name}: ${value}`);
  150. return `{${tokens.join(', ')}}`;
  151. }
  152. if (object.subtype === 'array' && object.preview) return js.sparseArrayToString(object.preview.properties);
  153. return object.description;
  154. }