ffExecutionContext.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FFExecutionContext = void 0;
  6. var js = _interopRequireWildcard(require("../javascript"));
  7. var _stackTrace = require("../../utils/stackTrace");
  8. var _utilityScriptSerializers = require("../isomorphic/utilityScriptSerializers");
  9. var _protocolError = require("../protocolError");
  10. 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); }
  11. 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; }
  12. /**
  13. * Copyright 2019 Google Inc. All rights reserved.
  14. * Modifications copyright (c) Microsoft Corporation.
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. class FFExecutionContext {
  29. constructor(session, executionContextId) {
  30. this._session = void 0;
  31. this._executionContextId = void 0;
  32. this._session = session;
  33. this._executionContextId = executionContextId;
  34. }
  35. async rawEvaluateJSON(expression) {
  36. const payload = await this._session.send('Runtime.evaluate', {
  37. expression,
  38. returnByValue: true,
  39. executionContextId: this._executionContextId
  40. }).catch(rewriteError);
  41. checkException(payload.exceptionDetails);
  42. return payload.result.value;
  43. }
  44. async rawEvaluateHandle(expression) {
  45. const payload = await this._session.send('Runtime.evaluate', {
  46. expression,
  47. returnByValue: false,
  48. executionContextId: this._executionContextId
  49. }).catch(rewriteError);
  50. checkException(payload.exceptionDetails);
  51. return payload.result.objectId;
  52. }
  53. rawCallFunctionNoReply(func, ...args) {
  54. this._session.send('Runtime.callFunction', {
  55. functionDeclaration: func.toString(),
  56. args: args.map(a => a instanceof js.JSHandle ? {
  57. objectId: a._objectId
  58. } : {
  59. value: a
  60. }),
  61. returnByValue: true,
  62. executionContextId: this._executionContextId
  63. }).catch(() => {});
  64. }
  65. async evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds) {
  66. const payload = await this._session.send('Runtime.callFunction', {
  67. functionDeclaration: expression,
  68. args: [{
  69. objectId: utilityScript._objectId,
  70. value: undefined
  71. }, ...values.map(value => ({
  72. value
  73. })), ...objectIds.map(objectId => ({
  74. objectId,
  75. value: undefined
  76. }))],
  77. returnByValue,
  78. executionContextId: this._executionContextId
  79. }).catch(rewriteError);
  80. checkException(payload.exceptionDetails);
  81. if (returnByValue) return (0, _utilityScriptSerializers.parseEvaluationResultValue)(payload.result.value);
  82. return utilityScript._context.createHandle(payload.result);
  83. }
  84. async getProperties(context, objectId) {
  85. const response = await this._session.send('Runtime.getObjectProperties', {
  86. executionContextId: this._executionContextId,
  87. objectId
  88. });
  89. const result = new Map();
  90. for (const property of response.properties) result.set(property.name, context.createHandle(property.value));
  91. return result;
  92. }
  93. createHandle(context, remoteObject) {
  94. return new js.JSHandle(context, remoteObject.subtype || remoteObject.type || '', renderPreview(remoteObject), remoteObject.objectId, potentiallyUnserializableValue(remoteObject));
  95. }
  96. async releaseHandle(objectId) {
  97. await this._session.send('Runtime.disposeObject', {
  98. executionContextId: this._executionContextId,
  99. objectId
  100. });
  101. }
  102. objectCount(objectId) {
  103. throw new Error('Method not implemented in Firefox.');
  104. }
  105. }
  106. exports.FFExecutionContext = FFExecutionContext;
  107. function checkException(exceptionDetails) {
  108. if (!exceptionDetails) return;
  109. if (exceptionDetails.value) throw new js.JavaScriptErrorInEvaluate(JSON.stringify(exceptionDetails.value));else throw new js.JavaScriptErrorInEvaluate(exceptionDetails.text + (exceptionDetails.stack ? '\n' + exceptionDetails.stack : ''));
  110. }
  111. function rewriteError(error) {
  112. if (error.message.includes('cyclic object value') || error.message.includes('Object is not serializable')) return {
  113. result: {
  114. type: 'undefined',
  115. value: undefined
  116. }
  117. };
  118. if (error instanceof TypeError && error.message.startsWith('Converting circular structure to JSON')) (0, _stackTrace.rewriteErrorMessage)(error, error.message + ' Are you passing a nested JSHandle?');
  119. if (!js.isJavaScriptErrorInEvaluate(error) && !(0, _protocolError.isSessionClosedError)(error)) throw new Error('Execution context was destroyed, most likely because of a navigation.');
  120. throw error;
  121. }
  122. function potentiallyUnserializableValue(remoteObject) {
  123. const value = remoteObject.value;
  124. const unserializableValue = remoteObject.unserializableValue;
  125. return unserializableValue ? js.parseUnserializableValue(unserializableValue) : value;
  126. }
  127. function renderPreview(object) {
  128. if (object.type === 'undefined') return 'undefined';
  129. if (object.unserializableValue) return String(object.unserializableValue);
  130. if (object.type === 'symbol') return 'Symbol()';
  131. if (object.subtype === 'regexp') return 'RegExp';
  132. if (object.subtype === 'weakmap') return 'WeakMap';
  133. if (object.subtype === 'weakset') return 'WeakSet';
  134. if (object.subtype) return object.subtype[0].toUpperCase() + object.subtype.slice(1);
  135. if ('value' in object) return String(object.value);
  136. }