mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: extract protocol utils to separate files (#11706)
This commit is contained in:
parent
6a28525fc9
commit
76da4e6479
@ -28,7 +28,6 @@ import type {
|
|||||||
NodeFor,
|
NodeFor,
|
||||||
} from '../common/types.js';
|
} from '../common/types.js';
|
||||||
import {
|
import {
|
||||||
getPageContent,
|
|
||||||
importFSPromises,
|
importFSPromises,
|
||||||
withSourcePuppeteerURLIfNone,
|
withSourcePuppeteerURLIfNone,
|
||||||
} from '../common/util.js';
|
} from '../common/util.js';
|
||||||
@ -776,7 +775,21 @@ export abstract class Frame extends EventEmitter<FrameEvents> {
|
|||||||
*/
|
*/
|
||||||
@throwIfDetached
|
@throwIfDetached
|
||||||
async content(): Promise<string> {
|
async content(): Promise<string> {
|
||||||
return await this.evaluate(getPageContent);
|
return await this.evaluate(() => {
|
||||||
|
let content = '';
|
||||||
|
for (const node of document.childNodes) {
|
||||||
|
switch (node) {
|
||||||
|
case document.documentElement:
|
||||||
|
content += document.documentElement.outerHTML;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
content += new XMLSerializer().serializeToString(node);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,11 +15,9 @@ import type {EvaluateFunc, HandleFor} from '../common/types.js';
|
|||||||
import {
|
import {
|
||||||
PuppeteerURL,
|
PuppeteerURL,
|
||||||
SOURCE_URL_REGEX,
|
SOURCE_URL_REGEX,
|
||||||
createEvaluationError,
|
|
||||||
getSourcePuppeteerURLIfAvailable,
|
getSourcePuppeteerURLIfAvailable,
|
||||||
getSourceUrlComment,
|
getSourceUrlComment,
|
||||||
isString,
|
isString,
|
||||||
valueFromRemoteObject,
|
|
||||||
} from '../common/util.js';
|
} from '../common/util.js';
|
||||||
import type PuppeteerUtil from '../injected/injected.js';
|
import type PuppeteerUtil from '../injected/injected.js';
|
||||||
import {AsyncIterableUtil} from '../util/AsyncIterableUtil.js';
|
import {AsyncIterableUtil} from '../util/AsyncIterableUtil.js';
|
||||||
@ -30,6 +28,7 @@ import {Binding} from './Binding.js';
|
|||||||
import {CdpElementHandle} from './ElementHandle.js';
|
import {CdpElementHandle} from './ElementHandle.js';
|
||||||
import type {IsolatedWorld} from './IsolatedWorld.js';
|
import type {IsolatedWorld} from './IsolatedWorld.js';
|
||||||
import {CdpJSHandle} from './JSHandle.js';
|
import {CdpJSHandle} from './JSHandle.js';
|
||||||
|
import {createEvaluationError, valueFromRemoteObject} from './utils.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
|
@ -11,11 +11,7 @@ import type {JSHandle} from '../api/JSHandle.js';
|
|||||||
import {Realm} from '../api/Realm.js';
|
import {Realm} from '../api/Realm.js';
|
||||||
import type {TimeoutSettings} from '../common/TimeoutSettings.js';
|
import type {TimeoutSettings} from '../common/TimeoutSettings.js';
|
||||||
import type {BindingPayload, EvaluateFunc, HandleFor} from '../common/types.js';
|
import type {BindingPayload, EvaluateFunc, HandleFor} from '../common/types.js';
|
||||||
import {
|
import {debugError, withSourcePuppeteerURLIfNone} from '../common/util.js';
|
||||||
addPageBinding,
|
|
||||||
debugError,
|
|
||||||
withSourcePuppeteerURLIfNone,
|
|
||||||
} from '../common/util.js';
|
|
||||||
import {Deferred} from '../util/Deferred.js';
|
import {Deferred} from '../util/Deferred.js';
|
||||||
import {disposeSymbol} from '../util/disposable.js';
|
import {disposeSymbol} from '../util/disposable.js';
|
||||||
import {Mutex} from '../util/Mutex.js';
|
import {Mutex} from '../util/Mutex.js';
|
||||||
@ -24,6 +20,7 @@ import type {Binding} from './Binding.js';
|
|||||||
import {ExecutionContext, createCdpHandle} from './ExecutionContext.js';
|
import {ExecutionContext, createCdpHandle} from './ExecutionContext.js';
|
||||||
import type {CdpFrame} from './Frame.js';
|
import type {CdpFrame} from './Frame.js';
|
||||||
import type {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
|
import type {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
|
||||||
|
import {addPageBinding} from './utils.js';
|
||||||
import type {CdpWebWorker} from './WebWorker.js';
|
import type {CdpWebWorker} from './WebWorker.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,10 +8,11 @@ import type {Protocol} from 'devtools-protocol';
|
|||||||
|
|
||||||
import type {CDPSession} from '../api/CDPSession.js';
|
import type {CDPSession} from '../api/CDPSession.js';
|
||||||
import {JSHandle} from '../api/JSHandle.js';
|
import {JSHandle} from '../api/JSHandle.js';
|
||||||
import {debugError, valueFromRemoteObject} from '../common/util.js';
|
import {debugError} from '../common/util.js';
|
||||||
|
|
||||||
import type {CdpElementHandle} from './ElementHandle.js';
|
import type {CdpElementHandle} from './ElementHandle.js';
|
||||||
import type {IsolatedWorld} from './IsolatedWorld.js';
|
import type {IsolatedWorld} from './IsolatedWorld.js';
|
||||||
|
import {valueFromRemoteObject} from './utils.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
|
@ -38,17 +38,14 @@ import {NetworkManagerEvent} from '../common/NetworkManagerEvents.js';
|
|||||||
import type {PDFOptions} from '../common/PDFOptions.js';
|
import type {PDFOptions} from '../common/PDFOptions.js';
|
||||||
import type {BindingPayload, HandleFor} from '../common/types.js';
|
import type {BindingPayload, HandleFor} from '../common/types.js';
|
||||||
import {
|
import {
|
||||||
createClientError,
|
|
||||||
debugError,
|
debugError,
|
||||||
evaluationString,
|
evaluationString,
|
||||||
getReadableAsBuffer,
|
getReadableAsBuffer,
|
||||||
getReadableFromProtocolStream,
|
getReadableFromProtocolStream,
|
||||||
NETWORK_IDLE_TIME,
|
NETWORK_IDLE_TIME,
|
||||||
pageBindingInitString,
|
|
||||||
parsePDFOptions,
|
parsePDFOptions,
|
||||||
timeout,
|
timeout,
|
||||||
validateDialogType,
|
validateDialogType,
|
||||||
valueFromRemoteObject,
|
|
||||||
waitForHTTP,
|
waitForHTTP,
|
||||||
} from '../common/util.js';
|
} from '../common/util.js';
|
||||||
import type {Viewport} from '../common/Viewport.js';
|
import type {Viewport} from '../common/Viewport.js';
|
||||||
@ -78,6 +75,11 @@ import type {CdpTarget} from './Target.js';
|
|||||||
import type {TargetManager} from './TargetManager.js';
|
import type {TargetManager} from './TargetManager.js';
|
||||||
import {TargetManagerEvent} from './TargetManager.js';
|
import {TargetManagerEvent} from './TargetManager.js';
|
||||||
import {Tracing} from './Tracing.js';
|
import {Tracing} from './Tracing.js';
|
||||||
|
import {
|
||||||
|
createClientError,
|
||||||
|
pageBindingInitString,
|
||||||
|
valueFromRemoteObject,
|
||||||
|
} from './utils.js';
|
||||||
import {CdpWebWorker} from './WebWorker.js';
|
import {CdpWebWorker} from './WebWorker.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,6 +9,7 @@ export * from './AriaQueryHandler.js';
|
|||||||
export * from './Binding.js';
|
export * from './Binding.js';
|
||||||
export * from './Browser.js';
|
export * from './Browser.js';
|
||||||
export * from './BrowserConnector.js';
|
export * from './BrowserConnector.js';
|
||||||
|
export * from './cdp.js';
|
||||||
export * from './CDPSession.js';
|
export * from './CDPSession.js';
|
||||||
export * from './ChromeTargetManager.js';
|
export * from './ChromeTargetManager.js';
|
||||||
export * from './Connection.js';
|
export * from './Connection.js';
|
||||||
@ -37,5 +38,5 @@ export * from './PredefinedNetworkConditions.js';
|
|||||||
export * from './Target.js';
|
export * from './Target.js';
|
||||||
export * from './TargetManager.js';
|
export * from './TargetManager.js';
|
||||||
export * from './Tracing.js';
|
export * from './Tracing.js';
|
||||||
|
export * from './utils.js';
|
||||||
export * from './WebWorker.js';
|
export * from './WebWorker.js';
|
||||||
export * from './cdp.js';
|
|
||||||
|
232
packages/puppeteer-core/src/cdp/utils.ts
Normal file
232
packages/puppeteer-core/src/cdp/utils.ts
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2017 Google Inc.
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type {Protocol} from 'devtools-protocol';
|
||||||
|
|
||||||
|
import {PuppeteerURL, evaluationString} from '../common/util.js';
|
||||||
|
import {assert} from '../util/assert.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function createEvaluationError(
|
||||||
|
details: Protocol.Runtime.ExceptionDetails
|
||||||
|
): unknown {
|
||||||
|
let name: string;
|
||||||
|
let message: string;
|
||||||
|
if (!details.exception) {
|
||||||
|
name = 'Error';
|
||||||
|
message = details.text;
|
||||||
|
} else if (
|
||||||
|
(details.exception.type !== 'object' ||
|
||||||
|
details.exception.subtype !== 'error') &&
|
||||||
|
!details.exception.objectId
|
||||||
|
) {
|
||||||
|
return valueFromRemoteObject(details.exception);
|
||||||
|
} else {
|
||||||
|
const detail = getErrorDetails(details);
|
||||||
|
name = detail.name;
|
||||||
|
message = detail.message;
|
||||||
|
}
|
||||||
|
const messageHeight = message.split('\n').length;
|
||||||
|
const error = new Error(message);
|
||||||
|
error.name = name;
|
||||||
|
const stackLines = error.stack!.split('\n');
|
||||||
|
const messageLines = stackLines.splice(0, messageHeight);
|
||||||
|
|
||||||
|
// The first line is this function which we ignore.
|
||||||
|
stackLines.shift();
|
||||||
|
if (details.stackTrace && stackLines.length < Error.stackTraceLimit) {
|
||||||
|
for (const frame of details.stackTrace.callFrames.reverse()) {
|
||||||
|
if (
|
||||||
|
PuppeteerURL.isPuppeteerURL(frame.url) &&
|
||||||
|
frame.url !== PuppeteerURL.INTERNAL_URL
|
||||||
|
) {
|
||||||
|
const url = PuppeteerURL.parse(frame.url);
|
||||||
|
stackLines.unshift(
|
||||||
|
` at ${frame.functionName || url.functionName} (${
|
||||||
|
url.functionName
|
||||||
|
} at ${url.siteString}, <anonymous>:${frame.lineNumber}:${
|
||||||
|
frame.columnNumber
|
||||||
|
})`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
stackLines.push(
|
||||||
|
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
|
||||||
|
frame.lineNumber
|
||||||
|
}:${frame.columnNumber})`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (stackLines.length >= Error.stackTraceLimit) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
error.stack = [...messageLines, ...stackLines].join('\n');
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getErrorDetails = (details: Protocol.Runtime.ExceptionDetails) => {
|
||||||
|
let name = '';
|
||||||
|
let message: string;
|
||||||
|
const lines = details.exception?.description?.split('\n at ') ?? [];
|
||||||
|
const size = Math.min(
|
||||||
|
details.stackTrace?.callFrames.length ?? 0,
|
||||||
|
lines.length - 1
|
||||||
|
);
|
||||||
|
lines.splice(-size, size);
|
||||||
|
if (details.exception?.className) {
|
||||||
|
name = details.exception.className;
|
||||||
|
}
|
||||||
|
message = lines.join('\n');
|
||||||
|
if (name && message.startsWith(`${name}: `)) {
|
||||||
|
message = message.slice(name.length + 2);
|
||||||
|
}
|
||||||
|
return {message, name};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function createClientError(
|
||||||
|
details: Protocol.Runtime.ExceptionDetails
|
||||||
|
): Error {
|
||||||
|
let name: string;
|
||||||
|
let message: string;
|
||||||
|
if (!details.exception) {
|
||||||
|
name = 'Error';
|
||||||
|
message = details.text;
|
||||||
|
} else if (
|
||||||
|
(details.exception.type !== 'object' ||
|
||||||
|
details.exception.subtype !== 'error') &&
|
||||||
|
!details.exception.objectId
|
||||||
|
) {
|
||||||
|
return valueFromRemoteObject(details.exception);
|
||||||
|
} else {
|
||||||
|
const detail = getErrorDetails(details);
|
||||||
|
name = detail.name;
|
||||||
|
message = detail.message;
|
||||||
|
}
|
||||||
|
const error = new Error(message);
|
||||||
|
error.name = name;
|
||||||
|
|
||||||
|
const messageHeight = error.message.split('\n').length;
|
||||||
|
const messageLines = error.stack!.split('\n').splice(0, messageHeight);
|
||||||
|
|
||||||
|
const stackLines = [];
|
||||||
|
if (details.stackTrace) {
|
||||||
|
for (const frame of details.stackTrace.callFrames) {
|
||||||
|
// Note we need to add `1` because the values are 0-indexed.
|
||||||
|
stackLines.push(
|
||||||
|
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
|
||||||
|
frame.lineNumber + 1
|
||||||
|
}:${frame.columnNumber + 1})`
|
||||||
|
);
|
||||||
|
if (stackLines.length >= Error.stackTraceLimit) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
error.stack = [...messageLines, ...stackLines].join('\n');
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function valueFromRemoteObject(
|
||||||
|
remoteObject: Protocol.Runtime.RemoteObject
|
||||||
|
): any {
|
||||||
|
assert(!remoteObject.objectId, 'Cannot extract value when objectId is given');
|
||||||
|
if (remoteObject.unserializableValue) {
|
||||||
|
if (remoteObject.type === 'bigint') {
|
||||||
|
return BigInt(remoteObject.unserializableValue.replace('n', ''));
|
||||||
|
}
|
||||||
|
switch (remoteObject.unserializableValue) {
|
||||||
|
case '-0':
|
||||||
|
return -0;
|
||||||
|
case 'NaN':
|
||||||
|
return NaN;
|
||||||
|
case 'Infinity':
|
||||||
|
return Infinity;
|
||||||
|
case '-Infinity':
|
||||||
|
return -Infinity;
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
'Unsupported unserializable value: ' +
|
||||||
|
remoteObject.unserializableValue
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return remoteObject.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function addPageBinding(type: string, name: string): void {
|
||||||
|
// This is the CDP binding.
|
||||||
|
// @ts-expect-error: In a different context.
|
||||||
|
const callCdp = globalThis[name];
|
||||||
|
|
||||||
|
// Depending on the frame loading state either Runtime.evaluate or
|
||||||
|
// Page.addScriptToEvaluateOnNewDocument might succeed. Let's check that we
|
||||||
|
// don't re-wrap Puppeteer's binding.
|
||||||
|
if (callCdp[Symbol.toStringTag] === 'PuppeteerBinding') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We replace the CDP binding with a Puppeteer binding.
|
||||||
|
Object.assign(globalThis, {
|
||||||
|
[name](...args: unknown[]): Promise<unknown> {
|
||||||
|
// This is the Puppeteer binding.
|
||||||
|
// @ts-expect-error: In a different context.
|
||||||
|
const callPuppeteer = globalThis[name];
|
||||||
|
callPuppeteer.args ??= new Map();
|
||||||
|
callPuppeteer.callbacks ??= new Map();
|
||||||
|
|
||||||
|
const seq = (callPuppeteer.lastSeq ?? 0) + 1;
|
||||||
|
callPuppeteer.lastSeq = seq;
|
||||||
|
callPuppeteer.args.set(seq, args);
|
||||||
|
|
||||||
|
callCdp(
|
||||||
|
JSON.stringify({
|
||||||
|
type,
|
||||||
|
name,
|
||||||
|
seq,
|
||||||
|
args,
|
||||||
|
isTrivial: !args.some(value => {
|
||||||
|
return value instanceof Node;
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
callPuppeteer.callbacks.set(seq, {
|
||||||
|
resolve(value: unknown) {
|
||||||
|
callPuppeteer.args.delete(seq);
|
||||||
|
resolve(value);
|
||||||
|
},
|
||||||
|
reject(value?: unknown) {
|
||||||
|
callPuppeteer.args.delete(seq);
|
||||||
|
reject(value);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// @ts-expect-error: In a different context.
|
||||||
|
globalThis[name][Symbol.toStringTag] = 'PuppeteerBinding';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function pageBindingInitString(type: string, name: string): string {
|
||||||
|
return evaluationString(addPageBinding, type, name);
|
||||||
|
}
|
@ -7,8 +7,6 @@
|
|||||||
import type FS from 'fs/promises';
|
import type FS from 'fs/promises';
|
||||||
import type {Readable} from 'stream';
|
import type {Readable} from 'stream';
|
||||||
|
|
||||||
import type {Protocol} from 'devtools-protocol';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
filterAsync,
|
filterAsync,
|
||||||
firstValueFrom,
|
firstValueFrom,
|
||||||
@ -46,133 +44,6 @@ export const debugError = debug('puppeteer:error');
|
|||||||
*/
|
*/
|
||||||
export const DEFAULT_VIEWPORT = Object.freeze({width: 800, height: 600});
|
export const DEFAULT_VIEWPORT = Object.freeze({width: 800, height: 600});
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function createEvaluationError(
|
|
||||||
details: Protocol.Runtime.ExceptionDetails
|
|
||||||
): unknown {
|
|
||||||
let name: string;
|
|
||||||
let message: string;
|
|
||||||
if (!details.exception) {
|
|
||||||
name = 'Error';
|
|
||||||
message = details.text;
|
|
||||||
} else if (
|
|
||||||
(details.exception.type !== 'object' ||
|
|
||||||
details.exception.subtype !== 'error') &&
|
|
||||||
!details.exception.objectId
|
|
||||||
) {
|
|
||||||
return valueFromRemoteObject(details.exception);
|
|
||||||
} else {
|
|
||||||
const detail = getErrorDetails(details);
|
|
||||||
name = detail.name;
|
|
||||||
message = detail.message;
|
|
||||||
}
|
|
||||||
const messageHeight = message.split('\n').length;
|
|
||||||
const error = new Error(message);
|
|
||||||
error.name = name;
|
|
||||||
const stackLines = error.stack!.split('\n');
|
|
||||||
const messageLines = stackLines.splice(0, messageHeight);
|
|
||||||
|
|
||||||
// The first line is this function which we ignore.
|
|
||||||
stackLines.shift();
|
|
||||||
if (details.stackTrace && stackLines.length < Error.stackTraceLimit) {
|
|
||||||
for (const frame of details.stackTrace.callFrames.reverse()) {
|
|
||||||
if (
|
|
||||||
PuppeteerURL.isPuppeteerURL(frame.url) &&
|
|
||||||
frame.url !== PuppeteerURL.INTERNAL_URL
|
|
||||||
) {
|
|
||||||
const url = PuppeteerURL.parse(frame.url);
|
|
||||||
stackLines.unshift(
|
|
||||||
` at ${frame.functionName || url.functionName} (${
|
|
||||||
url.functionName
|
|
||||||
} at ${url.siteString}, <anonymous>:${frame.lineNumber}:${
|
|
||||||
frame.columnNumber
|
|
||||||
})`
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
stackLines.push(
|
|
||||||
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
|
|
||||||
frame.lineNumber
|
|
||||||
}:${frame.columnNumber})`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (stackLines.length >= Error.stackTraceLimit) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
error.stack = [...messageLines, ...stackLines].join('\n');
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function createClientError(
|
|
||||||
details: Protocol.Runtime.ExceptionDetails
|
|
||||||
): Error {
|
|
||||||
let name: string;
|
|
||||||
let message: string;
|
|
||||||
if (!details.exception) {
|
|
||||||
name = 'Error';
|
|
||||||
message = details.text;
|
|
||||||
} else if (
|
|
||||||
(details.exception.type !== 'object' ||
|
|
||||||
details.exception.subtype !== 'error') &&
|
|
||||||
!details.exception.objectId
|
|
||||||
) {
|
|
||||||
return valueFromRemoteObject(details.exception);
|
|
||||||
} else {
|
|
||||||
const detail = getErrorDetails(details);
|
|
||||||
name = detail.name;
|
|
||||||
message = detail.message;
|
|
||||||
}
|
|
||||||
const error = new Error(message);
|
|
||||||
error.name = name;
|
|
||||||
|
|
||||||
const messageHeight = error.message.split('\n').length;
|
|
||||||
const messageLines = error.stack!.split('\n').splice(0, messageHeight);
|
|
||||||
|
|
||||||
const stackLines = [];
|
|
||||||
if (details.stackTrace) {
|
|
||||||
for (const frame of details.stackTrace.callFrames) {
|
|
||||||
// Note we need to add `1` because the values are 0-indexed.
|
|
||||||
stackLines.push(
|
|
||||||
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
|
|
||||||
frame.lineNumber + 1
|
|
||||||
}:${frame.columnNumber + 1})`
|
|
||||||
);
|
|
||||||
if (stackLines.length >= Error.stackTraceLimit) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
error.stack = [...messageLines, ...stackLines].join('\n');
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getErrorDetails = (details: Protocol.Runtime.ExceptionDetails) => {
|
|
||||||
let name = '';
|
|
||||||
let message: string;
|
|
||||||
const lines = details.exception?.description?.split('\n at ') ?? [];
|
|
||||||
const size = Math.min(
|
|
||||||
details.stackTrace?.callFrames.length ?? 0,
|
|
||||||
lines.length - 1
|
|
||||||
);
|
|
||||||
lines.splice(-size, size);
|
|
||||||
if (details.exception?.className) {
|
|
||||||
name = details.exception.className;
|
|
||||||
}
|
|
||||||
message = lines.join('\n');
|
|
||||||
if (name && message.startsWith(`${name}: `)) {
|
|
||||||
message = message.slice(name.length + 2);
|
|
||||||
}
|
|
||||||
return {message, name};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@ -265,36 +136,6 @@ export const getSourcePuppeteerURLIfAvailable = <
|
|||||||
return undefined;
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function valueFromRemoteObject(
|
|
||||||
remoteObject: Protocol.Runtime.RemoteObject
|
|
||||||
): any {
|
|
||||||
assert(!remoteObject.objectId, 'Cannot extract value when objectId is given');
|
|
||||||
if (remoteObject.unserializableValue) {
|
|
||||||
if (remoteObject.type === 'bigint') {
|
|
||||||
return BigInt(remoteObject.unserializableValue.replace('n', ''));
|
|
||||||
}
|
|
||||||
switch (remoteObject.unserializableValue) {
|
|
||||||
case '-0':
|
|
||||||
return -0;
|
|
||||||
case 'NaN':
|
|
||||||
return NaN;
|
|
||||||
case 'Infinity':
|
|
||||||
return Infinity;
|
|
||||||
case '-Infinity':
|
|
||||||
return -Infinity;
|
|
||||||
default:
|
|
||||||
throw new Error(
|
|
||||||
'Unsupported unserializable value: ' +
|
|
||||||
remoteObject.unserializableValue
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return remoteObject.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@ -352,71 +193,6 @@ export function evaluationString(
|
|||||||
return `(${fun})(${args.map(serializeArgument).join(',')})`;
|
return `(${fun})(${args.map(serializeArgument).join(',')})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function addPageBinding(type: string, name: string): void {
|
|
||||||
// This is the CDP binding.
|
|
||||||
// @ts-expect-error: In a different context.
|
|
||||||
const callCdp = globalThis[name];
|
|
||||||
|
|
||||||
// Depending on the frame loading state either Runtime.evaluate or
|
|
||||||
// Page.addScriptToEvaluateOnNewDocument might succeed. Let's check that we
|
|
||||||
// don't re-wrap Puppeteer's binding.
|
|
||||||
if (callCdp[Symbol.toStringTag] === 'PuppeteerBinding') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We replace the CDP binding with a Puppeteer binding.
|
|
||||||
Object.assign(globalThis, {
|
|
||||||
[name](...args: unknown[]): Promise<unknown> {
|
|
||||||
// This is the Puppeteer binding.
|
|
||||||
// @ts-expect-error: In a different context.
|
|
||||||
const callPuppeteer = globalThis[name];
|
|
||||||
callPuppeteer.args ??= new Map();
|
|
||||||
callPuppeteer.callbacks ??= new Map();
|
|
||||||
|
|
||||||
const seq = (callPuppeteer.lastSeq ?? 0) + 1;
|
|
||||||
callPuppeteer.lastSeq = seq;
|
|
||||||
callPuppeteer.args.set(seq, args);
|
|
||||||
|
|
||||||
callCdp(
|
|
||||||
JSON.stringify({
|
|
||||||
type,
|
|
||||||
name,
|
|
||||||
seq,
|
|
||||||
args,
|
|
||||||
isTrivial: !args.some(value => {
|
|
||||||
return value instanceof Node;
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
callPuppeteer.callbacks.set(seq, {
|
|
||||||
resolve(value: unknown) {
|
|
||||||
callPuppeteer.args.delete(seq);
|
|
||||||
resolve(value);
|
|
||||||
},
|
|
||||||
reject(value?: unknown) {
|
|
||||||
callPuppeteer.args.delete(seq);
|
|
||||||
reject(value);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
// @ts-expect-error: In a different context.
|
|
||||||
globalThis[name][Symbol.toStringTag] = 'PuppeteerBinding';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function pageBindingInitString(type: string, name: string): string {
|
|
||||||
return evaluationString(addPageBinding, type, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@ -512,25 +288,6 @@ export async function getReadableFromProtocolStream(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function getPageContent(): string {
|
|
||||||
let content = '';
|
|
||||||
for (const node of document.childNodes) {
|
|
||||||
switch (node) {
|
|
||||||
case document.documentElement:
|
|
||||||
content += document.documentElement.outerHTML;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
content += new XMLSerializer().serializeToString(node);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user