2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
* Modifications 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-04 17:57:35 -08:00
|
|
|
import { helper } from '../helper';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as js from '../javascript';
|
|
|
|
import { FFSession } from './ffConnection';
|
|
|
|
import { Protocol } from './protocol';
|
|
|
|
|
|
|
|
export class FFExecutionContext implements js.ExecutionContextDelegate {
|
|
|
|
_session: FFSession;
|
|
|
|
_executionContextId: string;
|
|
|
|
|
|
|
|
constructor(session: FFSession, executionContextId: string) {
|
|
|
|
this._session = session;
|
|
|
|
this._executionContextId = executionContextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluate(context: js.ExecutionContext, returnByValue: boolean, pageFunction: Function | string, ...args: any[]): Promise<any> {
|
|
|
|
if (helper.isString(pageFunction)) {
|
|
|
|
const payload = await this._session.send('Runtime.evaluate', {
|
|
|
|
expression: pageFunction.trim(),
|
2020-01-16 15:24:37 -08:00
|
|
|
returnByValue,
|
2019-12-19 16:53:24 -08:00
|
|
|
executionContextId: this._executionContextId,
|
|
|
|
}).catch(rewriteError);
|
|
|
|
checkException(payload.exceptionDetails);
|
2020-01-16 15:24:37 -08:00
|
|
|
if (returnByValue)
|
|
|
|
return deserializeValue(payload.result!);
|
2020-05-15 15:21:49 -07:00
|
|
|
return context.createHandle(payload.result);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
if (typeof pageFunction !== 'function')
|
|
|
|
throw new Error(`Expected to get |string| or |function| as the first argument, but got "${pageFunction}" instead.`);
|
|
|
|
|
2020-03-19 13:07:33 -07:00
|
|
|
const { functionText, values, handles, dispose } = await js.prepareFunctionCall<Protocol.Runtime.CallFunctionArgument>(pageFunction, context, args, (value: any) => {
|
2020-03-18 10:41:46 -07:00
|
|
|
if (Object.is(value, -0))
|
|
|
|
return { handle: { unserializableValue: '-0' } };
|
|
|
|
if (Object.is(value, Infinity))
|
|
|
|
return { handle: { unserializableValue: 'Infinity' } };
|
|
|
|
if (Object.is(value, -Infinity))
|
|
|
|
return { handle: { unserializableValue: '-Infinity' } };
|
|
|
|
if (Object.is(value, NaN))
|
|
|
|
return { handle: { unserializableValue: 'NaN' } };
|
|
|
|
if (value && (value instanceof js.JSHandle))
|
|
|
|
return { handle: this._toCallArgument(value._remoteObject) };
|
|
|
|
return { value };
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
2020-03-02 13:51:32 -08:00
|
|
|
|
2020-03-19 13:07:33 -07:00
|
|
|
try {
|
|
|
|
const payload = await this._session.send('Runtime.callFunction', {
|
|
|
|
functionDeclaration: functionText,
|
|
|
|
args: [
|
|
|
|
...values.map(value => ({ value })),
|
|
|
|
...handles,
|
|
|
|
],
|
|
|
|
returnByValue,
|
|
|
|
executionContextId: this._executionContextId
|
|
|
|
}).catch(rewriteError);
|
|
|
|
checkException(payload.exceptionDetails);
|
|
|
|
if (returnByValue)
|
|
|
|
return deserializeValue(payload.result!);
|
2020-05-15 15:21:49 -07:00
|
|
|
return context.createHandle(payload.result);
|
2020-03-19 13:07:33 -07:00
|
|
|
} finally {
|
|
|
|
dispose();
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-16 15:24:37 -08:00
|
|
|
function rewriteError(error: Error): (Protocol.Runtime.evaluateReturnValue | Protocol.Runtime.callFunctionReturnValue) {
|
|
|
|
if (error.message.includes('cyclic object value') || error.message.includes('Object is not serializable'))
|
|
|
|
return {result: {type: 'undefined', value: undefined}};
|
2019-12-19 16:53:24 -08:00
|
|
|
if (error.message.includes('Failed to find execution context with id') || error.message.includes('Execution context was destroyed!'))
|
|
|
|
throw new Error('Execution context was destroyed, most likely because of a navigation.');
|
2020-03-02 13:51:32 -08:00
|
|
|
if (error instanceof TypeError && error.message.startsWith('Converting circular structure to JSON'))
|
|
|
|
error.message += ' Are you passing a nested JSHandle?';
|
2019-12-19 16:53:24 -08:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProperties(handle: js.JSHandle): Promise<Map<string, js.JSHandle>> {
|
2020-01-13 15:36:22 -08:00
|
|
|
const objectId = handle._remoteObject.objectId;
|
|
|
|
if (!objectId)
|
|
|
|
return new Map();
|
2019-12-19 16:53:24 -08:00
|
|
|
const response = await this._session.send('Runtime.getObjectProperties', {
|
|
|
|
executionContextId: this._executionContextId,
|
2020-01-13 15:36:22 -08:00
|
|
|
objectId,
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
|
|
|
const result = new Map();
|
|
|
|
for (const property of response.properties)
|
2020-05-15 15:21:49 -07:00
|
|
|
result.set(property.name, handle._context.createHandle(property.value));
|
2019-12-19 16:53:24 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async releaseHandle(handle: js.JSHandle): Promise<void> {
|
|
|
|
if (!handle._remoteObject.objectId)
|
|
|
|
return;
|
|
|
|
await this._session.send('Runtime.disposeObject', {
|
|
|
|
executionContextId: this._executionContextId,
|
|
|
|
objectId: handle._remoteObject.objectId,
|
2020-03-04 17:57:35 -08:00
|
|
|
}).catch(error => {});
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async handleJSONValue<T>(handle: js.JSHandle<T>): Promise<T> {
|
|
|
|
const payload = handle._remoteObject;
|
|
|
|
if (!payload.objectId)
|
|
|
|
return deserializeValue(payload);
|
|
|
|
const simpleValue = await this._session.send('Runtime.callFunction', {
|
|
|
|
executionContextId: this._executionContextId,
|
|
|
|
returnByValue: true,
|
2020-01-13 22:08:35 -08:00
|
|
|
functionDeclaration: ((e: any) => e).toString(),
|
2019-12-19 16:53:24 -08:00
|
|
|
args: [this._toCallArgument(payload)],
|
|
|
|
});
|
2020-01-13 22:08:35 -08:00
|
|
|
return deserializeValue(simpleValue.result!);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleToString(handle: js.JSHandle, includeType: boolean): string {
|
|
|
|
const payload = handle._remoteObject;
|
|
|
|
if (payload.objectId)
|
|
|
|
return 'JSHandle@' + (payload.subtype || payload.type);
|
|
|
|
return (includeType ? 'JSHandle:' : '') + deserializeValue(payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _toCallArgument(payload: any): any {
|
|
|
|
return { value: payload.value, unserializableValue: payload.unserializableValue, objectId: payload.objectId };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 18:46:53 -07:00
|
|
|
function checkException(exceptionDetails?: Protocol.Runtime.ExceptionDetails) {
|
|
|
|
if (!exceptionDetails)
|
|
|
|
return;
|
|
|
|
if (exceptionDetails.value)
|
|
|
|
throw new Error('Evaluation failed: ' + JSON.stringify(exceptionDetails.value));
|
|
|
|
else
|
|
|
|
throw new Error('Evaluation failed: ' + exceptionDetails.text + '\n' + exceptionDetails.stack);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:32:31 -08:00
|
|
|
export function deserializeValue({unserializableValue, value}: Protocol.Runtime.RemoteObject) {
|
2019-12-19 16:53:24 -08:00
|
|
|
if (unserializableValue === 'Infinity')
|
|
|
|
return Infinity;
|
|
|
|
if (unserializableValue === '-Infinity')
|
|
|
|
return -Infinity;
|
|
|
|
if (unserializableValue === '-0')
|
|
|
|
return -0;
|
|
|
|
if (unserializableValue === 'NaN')
|
|
|
|
return NaN;
|
|
|
|
return value;
|
|
|
|
}
|