Skip to content

Commit

Permalink
fix(waitForFunction): process isFunction auto-detection (#5312)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Feb 4, 2021
1 parent 1798677 commit c2b8718
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
24 changes: 18 additions & 6 deletions src/server/frames.ts
Expand Up @@ -1019,13 +1019,25 @@ export class Frame extends EventEmitter {
async _waitForFunctionExpression<R>(expression: string, isFunction: boolean | undefined, arg: any, options: types.WaitForFunctionOptions = {}): Promise<js.SmartHandle<R>> {
if (typeof options.pollingInterval === 'number')
assert(options.pollingInterval > 0, 'Cannot poll with non-positive interval: ' + options.pollingInterval);
const predicateBody = isFunction ? 'return (' + expression + ')(arg)' : 'return (' + expression + ')';
const task: dom.SchedulableTask<R> = injectedScript => injectedScript.evaluateHandle((injectedScript, { predicateBody, polling, arg }) => {
const innerPredicate = new Function('arg', predicateBody) as (arg: any) => R;
expression = js.normalizeEvaluationExpression(expression, isFunction);
const task: dom.SchedulableTask<R> = injectedScript => injectedScript.evaluateHandle((injectedScript, { expression, isFunction, polling, arg }) => {
const predicate = (arg: any): R => {
let result = self.eval(expression);
if (isFunction === true) {
result = result(arg);
} else if (isFunction === false) {
result = result;
} else {
// auto detect.
if (typeof result === 'function')
result = result(arg);
}
return result;
};
if (typeof polling !== 'number')
return injectedScript.pollRaf((progress, continuePolling) => innerPredicate(arg) || continuePolling);
return injectedScript.pollInterval(polling, (progress, continuePolling) => innerPredicate(arg) || continuePolling);
}, { predicateBody, polling: options.pollingInterval, arg });
return injectedScript.pollRaf((progress, continuePolling) => predicate(arg) || continuePolling);
return injectedScript.pollInterval(polling, (progress, continuePolling) => predicate(arg) || continuePolling);
}, { expression, isFunction, polling: options.pollingInterval, arg });
return runAbortableTask(
progress => this._scheduleRerunnableHandleTask(progress, 'main', task),
this._page._timeoutSettings.timeout(options));
Expand Down
3 changes: 0 additions & 3 deletions src/server/injected/utilityScript.ts
Expand Up @@ -21,9 +21,6 @@ export default class UtilityScript {
const args = argsAndHandles.slice(0, argCount);
const handles = argsAndHandles.slice(argCount);
const parameters = args.map(a => parseEvaluationResultValue(a, handles));
expression = expression.trim();
if (/^(async)?\s*function(\s|\()/.test(expression))
expression = '(' + expression + ')';
let result = global.eval(expression);
if (isFunction === true) {
result = result(...parameters);
Expand Down
48 changes: 28 additions & 20 deletions src/server/javascript.ts
Expand Up @@ -175,26 +175,7 @@ export async function evaluate(context: ExecutionContext, returnByValue: boolean

export async function evaluateExpression(context: ExecutionContext, returnByValue: boolean, expression: string, isFunction: boolean | undefined, ...args: any[]): Promise<any> {
const utilityScript = await context.utilityScript();

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!');
}
}
}

expression = normalizeEvaluationExpression(expression, isFunction);
const handles: (Promise<JSHandle>)[] = [];
const toDispose: Promise<JSHandle>[] = [];
const pushHandle = (handle: Promise<JSHandle>): number => {
Expand Down Expand Up @@ -245,3 +226,30 @@ export function parseUnserializableValue(unserializableValue: string): any {
if (unserializableValue === '-0')
return -0;
}

export function normalizeEvaluationExpression(expression: string, isFunction: boolean | undefined): string {
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;
}

0 comments on commit c2b8718

Please sign in to comment.