javascript.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.JavaScriptErrorInEvaluate = exports.JSHandle = exports.ExecutionContext = void 0;
  6. exports.evaluate = evaluate;
  7. exports.evaluateExpression = evaluateExpression;
  8. exports.isJavaScriptErrorInEvaluate = isJavaScriptErrorInEvaluate;
  9. exports.normalizeEvaluationExpression = normalizeEvaluationExpression;
  10. exports.parseUnserializableValue = parseUnserializableValue;
  11. exports.sparseArrayToString = sparseArrayToString;
  12. var utilityScriptSource = _interopRequireWildcard(require("../generated/utilityScriptSource"));
  13. var _utilityScriptSerializers = require("./isomorphic/utilityScriptSerializers");
  14. var _instrumentation = require("./instrumentation");
  15. var _manualPromise = require("../utils/manualPromise");
  16. 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); }
  17. 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; }
  18. /**
  19. * 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. class ExecutionContext extends _instrumentation.SdkObject {
  34. constructor(parent, delegate, worldNameForTest) {
  35. super(parent, 'execution-context');
  36. this._delegate = void 0;
  37. this._utilityScriptPromise = void 0;
  38. this._contextDestroyedScope = new _manualPromise.LongStandingScope();
  39. this.worldNameForTest = void 0;
  40. this.worldNameForTest = worldNameForTest;
  41. this._delegate = delegate;
  42. }
  43. contextDestroyed(reason) {
  44. this._contextDestroyedScope.close(new Error(reason));
  45. }
  46. async _raceAgainstContextDestroyed(promise) {
  47. return this._contextDestroyedScope.race(promise);
  48. }
  49. rawEvaluateJSON(expression) {
  50. return this._raceAgainstContextDestroyed(this._delegate.rawEvaluateJSON(expression));
  51. }
  52. rawEvaluateHandle(expression) {
  53. return this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(expression));
  54. }
  55. rawCallFunctionNoReply(func, ...args) {
  56. this._delegate.rawCallFunctionNoReply(func, ...args);
  57. }
  58. evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds) {
  59. return this._raceAgainstContextDestroyed(this._delegate.evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds));
  60. }
  61. getProperties(context, objectId) {
  62. return this._raceAgainstContextDestroyed(this._delegate.getProperties(context, objectId));
  63. }
  64. createHandle(remoteObject) {
  65. return this._delegate.createHandle(this, remoteObject);
  66. }
  67. releaseHandle(objectId) {
  68. return this._delegate.releaseHandle(objectId);
  69. }
  70. adoptIfNeeded(handle) {
  71. return null;
  72. }
  73. utilityScript() {
  74. if (!this._utilityScriptPromise) {
  75. const source = `
  76. (() => {
  77. const module = {};
  78. ${utilityScriptSource.source}
  79. return new (module.exports.UtilityScript())();
  80. })();`;
  81. this._utilityScriptPromise = this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', 'UtilityScript', objectId)));
  82. }
  83. return this._utilityScriptPromise;
  84. }
  85. async objectCount(objectId) {
  86. return this._delegate.objectCount(objectId);
  87. }
  88. async doSlowMo() {
  89. // overridden in FrameExecutionContext
  90. }
  91. }
  92. exports.ExecutionContext = ExecutionContext;
  93. class JSHandle extends _instrumentation.SdkObject {
  94. constructor(context, type, preview, objectId, value) {
  95. super(context, 'handle');
  96. this.__jshandle = true;
  97. this._context = void 0;
  98. this._disposed = false;
  99. this._objectId = void 0;
  100. this._value = void 0;
  101. this._objectType = void 0;
  102. this._preview = void 0;
  103. this._previewCallback = void 0;
  104. this._context = context;
  105. this._objectId = objectId;
  106. this._value = value;
  107. this._objectType = type;
  108. this._preview = this._objectId ? preview || `JSHandle@${this._objectType}` : String(value);
  109. if (this._objectId && globalThis.leakedJSHandles) globalThis.leakedJSHandles.set(this, new Error('Leaked JSHandle'));
  110. }
  111. callFunctionNoReply(func, arg) {
  112. this._context.rawCallFunctionNoReply(func, this, arg);
  113. }
  114. async evaluate(pageFunction, arg) {
  115. return evaluate(this._context, true /* returnByValue */, pageFunction, this, arg);
  116. }
  117. async evaluateHandle(pageFunction, arg) {
  118. return evaluate(this._context, false /* returnByValue */, pageFunction, this, arg);
  119. }
  120. async evaluateExpression(expression, options, arg) {
  121. const value = await evaluateExpression(this._context, expression, {
  122. ...options,
  123. returnByValue: true
  124. }, this, arg);
  125. await this._context.doSlowMo();
  126. return value;
  127. }
  128. async evaluateExpressionHandle(expression, options, arg) {
  129. const value = await evaluateExpression(this._context, expression, {
  130. ...options,
  131. returnByValue: false
  132. }, this, arg);
  133. await this._context.doSlowMo();
  134. return value;
  135. }
  136. async getProperty(propertyName) {
  137. const objectHandle = await this.evaluateHandle((object, propertyName) => {
  138. const result = {
  139. __proto__: null
  140. };
  141. result[propertyName] = object[propertyName];
  142. return result;
  143. }, propertyName);
  144. const properties = await objectHandle.getProperties();
  145. const result = properties.get(propertyName);
  146. objectHandle.dispose();
  147. return result;
  148. }
  149. async getProperties() {
  150. if (!this._objectId) return new Map();
  151. return this._context.getProperties(this._context, this._objectId);
  152. }
  153. rawValue() {
  154. return this._value;
  155. }
  156. async jsonValue() {
  157. if (!this._objectId) return this._value;
  158. const utilityScript = await this._context.utilityScript();
  159. const script = `(utilityScript, ...args) => utilityScript.jsonValue(...args)`;
  160. return this._context.evaluateWithArguments(script, true, utilityScript, [true], [this._objectId]);
  161. }
  162. asElement() {
  163. return null;
  164. }
  165. dispose() {
  166. if (this._disposed) return;
  167. this._disposed = true;
  168. if (this._objectId) {
  169. this._context.releaseHandle(this._objectId).catch(e => {});
  170. if (globalThis.leakedJSHandles) globalThis.leakedJSHandles.delete(this);
  171. }
  172. }
  173. toString() {
  174. return this._preview;
  175. }
  176. _setPreviewCallback(callback) {
  177. this._previewCallback = callback;
  178. }
  179. preview() {
  180. return this._preview;
  181. }
  182. worldNameForTest() {
  183. return this._context.worldNameForTest;
  184. }
  185. _setPreview(preview) {
  186. this._preview = preview;
  187. if (this._previewCallback) this._previewCallback(preview);
  188. }
  189. async objectCount() {
  190. if (!this._objectId) throw new Error('Can only count objects for a handle that points to the constructor prototype');
  191. return this._context.objectCount(this._objectId);
  192. }
  193. }
  194. exports.JSHandle = JSHandle;
  195. async function evaluate(context, returnByValue, pageFunction, ...args) {
  196. return evaluateExpression(context, String(pageFunction), {
  197. returnByValue,
  198. isFunction: typeof pageFunction === 'function'
  199. }, ...args);
  200. }
  201. async function evaluateExpression(context, expression, options, ...args) {
  202. const utilityScript = await context.utilityScript();
  203. expression = normalizeEvaluationExpression(expression, options.isFunction);
  204. const handles = [];
  205. const toDispose = [];
  206. const pushHandle = handle => {
  207. handles.push(handle);
  208. return handles.length - 1;
  209. };
  210. args = args.map(arg => (0, _utilityScriptSerializers.serializeAsCallArgument)(arg, handle => {
  211. if (handle instanceof JSHandle) {
  212. if (!handle._objectId) return {
  213. fallThrough: handle._value
  214. };
  215. if (handle._disposed) throw new JavaScriptErrorInEvaluate('JSHandle is disposed!');
  216. const adopted = context.adoptIfNeeded(handle);
  217. if (adopted === null) return {
  218. h: pushHandle(Promise.resolve(handle))
  219. };
  220. toDispose.push(adopted);
  221. return {
  222. h: pushHandle(adopted)
  223. };
  224. }
  225. return {
  226. fallThrough: handle
  227. };
  228. }));
  229. const utilityScriptObjectIds = [];
  230. for (const handle of await Promise.all(handles)) {
  231. if (handle._context !== context) throw new JavaScriptErrorInEvaluate('JSHandles can be evaluated only in the context they were created!');
  232. utilityScriptObjectIds.push(handle._objectId);
  233. }
  234. // See UtilityScript for arguments.
  235. const utilityScriptValues = [options.isFunction, options.returnByValue, options.exposeUtilityScript, expression, args.length, ...args];
  236. const script = `(utilityScript, ...args) => utilityScript.evaluate(...args)`;
  237. try {
  238. return await context.evaluateWithArguments(script, options.returnByValue || false, utilityScript, utilityScriptValues, utilityScriptObjectIds);
  239. } finally {
  240. toDispose.map(handlePromise => handlePromise.then(handle => handle.dispose()));
  241. }
  242. }
  243. function parseUnserializableValue(unserializableValue) {
  244. if (unserializableValue === 'NaN') return NaN;
  245. if (unserializableValue === 'Infinity') return Infinity;
  246. if (unserializableValue === '-Infinity') return -Infinity;
  247. if (unserializableValue === '-0') return -0;
  248. }
  249. function normalizeEvaluationExpression(expression, isFunction) {
  250. expression = expression.trim();
  251. if (isFunction) {
  252. try {
  253. new Function('(' + expression + ')');
  254. } catch (e1) {
  255. // This means we might have a function shorthand. Try another
  256. // time prefixing 'function '.
  257. if (expression.startsWith('async ')) expression = 'async function ' + expression.substring('async '.length);else expression = 'function ' + expression;
  258. try {
  259. new Function('(' + expression + ')');
  260. } catch (e2) {
  261. // We tried hard to serialize, but there's a weird beast here.
  262. throw new Error('Passed function is not well-serializable!');
  263. }
  264. }
  265. }
  266. if (/^(async)?\s*function(\s|\()/.test(expression)) expression = '(' + expression + ')';
  267. return expression;
  268. }
  269. // Error inside the expression evaluation as opposed to a protocol error.
  270. class JavaScriptErrorInEvaluate extends Error {}
  271. exports.JavaScriptErrorInEvaluate = JavaScriptErrorInEvaluate;
  272. function isJavaScriptErrorInEvaluate(error) {
  273. return error instanceof JavaScriptErrorInEvaluate;
  274. }
  275. function sparseArrayToString(entries) {
  276. const arrayEntries = [];
  277. for (const {
  278. name,
  279. value
  280. } of entries) {
  281. const index = +name;
  282. if (isNaN(index) || index < 0) continue;
  283. arrayEntries.push({
  284. index,
  285. value
  286. });
  287. }
  288. arrayEntries.sort((a, b) => a.index - b.index);
  289. let lastIndex = -1;
  290. const tokens = [];
  291. for (const {
  292. index,
  293. value
  294. } of arrayEntries) {
  295. const emptyItems = index - lastIndex - 1;
  296. if (emptyItems === 1) tokens.push(`empty`);else if (emptyItems > 1) tokens.push(`empty x ${emptyItems}`);
  297. tokens.push(String(value));
  298. lastIndex = index;
  299. }
  300. return '[' + tokens.join(', ') + ']';
  301. }