mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: prefer use of DeferPromise (#9969)
This commit is contained in:
parent
c8f6adf9f3
commit
0b1e20cf5b
@ -192,26 +192,6 @@ export class HTTPRequest extends BaseHTTPRequest {
|
|||||||
return this._redirectChain.slice();
|
return this._redirectChain.slice();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Access information about the request's failure.
|
|
||||||
*
|
|
||||||
* @remarks
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* Example of logging all failed requests:
|
|
||||||
*
|
|
||||||
* ```ts
|
|
||||||
* page.on('requestfailed', request => {
|
|
||||||
* console.log(request.url() + ' ' + request.failure().errorText);
|
|
||||||
* });
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @returns `null` unless the request failed. If the request fails this can
|
|
||||||
* return an object with `errorText` containing a human-readable error
|
|
||||||
* message, e.g. `net::ERR_FAILED`. It is not guaranteed that there will be
|
|
||||||
* failure text if the request fails.
|
|
||||||
*/
|
|
||||||
override failure(): {errorText: string} | null {
|
override failure(): {errorText: string} | null {
|
||||||
if (!this._failureText) {
|
if (!this._failureText) {
|
||||||
return null;
|
return null;
|
||||||
@ -221,35 +201,6 @@ export class HTTPRequest extends BaseHTTPRequest {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Continues request with optional request overrides.
|
|
||||||
*
|
|
||||||
* @remarks
|
|
||||||
*
|
|
||||||
* To use this, request
|
|
||||||
* interception should be enabled with {@link Page.setRequestInterception}.
|
|
||||||
*
|
|
||||||
* Exception is immediately thrown if the request interception is not enabled.
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* ```ts
|
|
||||||
* await page.setRequestInterception(true);
|
|
||||||
* page.on('request', request => {
|
|
||||||
* // Override headers
|
|
||||||
* const headers = Object.assign({}, request.headers(), {
|
|
||||||
* foo: 'bar', // set "foo" header
|
|
||||||
* origin: undefined, // remove "origin" header
|
|
||||||
* });
|
|
||||||
* request.continue({headers});
|
|
||||||
* });
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @param overrides - optional overrides to apply to the request.
|
|
||||||
* @param priority - If provided, intercept is resolved using
|
|
||||||
* cooperative handling rules. Otherwise, intercept is resolved
|
|
||||||
* immediately.
|
|
||||||
*/
|
|
||||||
override async continue(
|
override async continue(
|
||||||
overrides: ContinueRequestOverrides = {},
|
overrides: ContinueRequestOverrides = {},
|
||||||
priority?: number
|
priority?: number
|
||||||
|
@ -19,6 +19,7 @@ import {
|
|||||||
HTTPResponse as BaseHTTPResponse,
|
HTTPResponse as BaseHTTPResponse,
|
||||||
RemoteAddress,
|
RemoteAddress,
|
||||||
} from '../api/HTTPResponse.js';
|
} from '../api/HTTPResponse.js';
|
||||||
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {ProtocolError} from './Errors.js';
|
import {ProtocolError} from './Errors.js';
|
||||||
@ -33,8 +34,7 @@ export class HTTPResponse extends BaseHTTPResponse {
|
|||||||
#client: CDPSession;
|
#client: CDPSession;
|
||||||
#request: HTTPRequest;
|
#request: HTTPRequest;
|
||||||
#contentPromise: Promise<Buffer> | null = null;
|
#contentPromise: Promise<Buffer> | null = null;
|
||||||
#bodyLoadedPromise: Promise<Error | void>;
|
#bodyLoadedPromise = createDeferredPromise<Error | void>();
|
||||||
#bodyLoadedPromiseFulfill: (err: Error | void) => void = () => {};
|
|
||||||
#remoteAddress: RemoteAddress;
|
#remoteAddress: RemoteAddress;
|
||||||
#status: number;
|
#status: number;
|
||||||
#statusText: string;
|
#statusText: string;
|
||||||
@ -55,10 +55,6 @@ export class HTTPResponse extends BaseHTTPResponse {
|
|||||||
this.#client = client;
|
this.#client = client;
|
||||||
this.#request = request;
|
this.#request = request;
|
||||||
|
|
||||||
this.#bodyLoadedPromise = new Promise(fulfill => {
|
|
||||||
this.#bodyLoadedPromiseFulfill = fulfill;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.#remoteAddress = {
|
this.#remoteAddress = {
|
||||||
ip: responsePayload.remoteIPAddress,
|
ip: responsePayload.remoteIPAddress,
|
||||||
port: responsePayload.remotePort,
|
port: responsePayload.remotePort,
|
||||||
@ -105,9 +101,9 @@ export class HTTPResponse extends BaseHTTPResponse {
|
|||||||
|
|
||||||
override _resolveBody(err: Error | null): void {
|
override _resolveBody(err: Error | null): void {
|
||||||
if (err) {
|
if (err) {
|
||||||
return this.#bodyLoadedPromiseFulfill(err);
|
return this.#bodyLoadedPromise.resolve(err);
|
||||||
}
|
}
|
||||||
return this.#bodyLoadedPromiseFulfill();
|
return this.#bodyLoadedPromise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
override remoteAddress(): RemoteAddress {
|
override remoteAddress(): RemoteAddress {
|
||||||
|
@ -74,27 +74,10 @@ export class LifecycleWatcher {
|
|||||||
#eventListeners: PuppeteerEventListener[];
|
#eventListeners: PuppeteerEventListener[];
|
||||||
#initialLoaderId: string;
|
#initialLoaderId: string;
|
||||||
|
|
||||||
#sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
#sameDocumentNavigationPromise = createDeferredPromise<Error | undefined>();
|
||||||
#sameDocumentNavigationPromise = new Promise<Error | undefined>(fulfill => {
|
#lifecyclePromise = createDeferredPromise<void>();
|
||||||
this.#sameDocumentNavigationCompleteCallback = fulfill;
|
#newDocumentNavigationPromise = createDeferredPromise<Error | undefined>();
|
||||||
});
|
#terminationPromise = createDeferredPromise<Error | undefined>();
|
||||||
|
|
||||||
#lifecycleCallback: () => void = noop;
|
|
||||||
#lifecyclePromise: Promise<void> = new Promise(fulfill => {
|
|
||||||
this.#lifecycleCallback = fulfill;
|
|
||||||
});
|
|
||||||
|
|
||||||
#newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
|
||||||
#newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
|
|
||||||
fulfill => {
|
|
||||||
this.#newDocumentNavigationCompleteCallback = fulfill;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
#terminationCallback: (x?: Error) => void = noop;
|
|
||||||
#terminationPromise: Promise<Error | undefined> = new Promise(fulfill => {
|
|
||||||
this.#terminationCallback = fulfill;
|
|
||||||
});
|
|
||||||
|
|
||||||
#timeoutPromise: Promise<TimeoutError | undefined>;
|
#timeoutPromise: Promise<TimeoutError | undefined>;
|
||||||
|
|
||||||
@ -211,8 +194,7 @@ export class LifecycleWatcher {
|
|||||||
|
|
||||||
#onFrameDetached(frame: Frame): void {
|
#onFrameDetached(frame: Frame): void {
|
||||||
if (this.#frame === frame) {
|
if (this.#frame === frame) {
|
||||||
this.#terminationCallback.call(
|
this.#terminationPromise.resolve(
|
||||||
null,
|
|
||||||
new Error('Navigating frame was detached')
|
new Error('Navigating frame was detached')
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@ -227,7 +209,7 @@ export class LifecycleWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#terminate(error: Error): void {
|
#terminate(error: Error): void {
|
||||||
this.#terminationCallback.call(null, error);
|
this.#terminationPromise.resolve(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
||||||
@ -286,12 +268,12 @@ export class LifecycleWatcher {
|
|||||||
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#lifecycleCallback();
|
this.#lifecyclePromise.resolve();
|
||||||
if (this.#hasSameDocumentNavigation) {
|
if (this.#hasSameDocumentNavigation) {
|
||||||
this.#sameDocumentNavigationCompleteCallback();
|
this.#sameDocumentNavigationPromise.resolve(undefined);
|
||||||
}
|
}
|
||||||
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
|
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
|
||||||
this.#newDocumentNavigationCompleteCallback();
|
this.#newDocumentNavigationPromise.resolve(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkLifecycle(
|
function checkLifecycle(
|
||||||
|
Loading…
Reference in New Issue
Block a user