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