Skip to content

Commit

Permalink
Revert "Revert "Revert "feat: cooperative_request"""
Browse files Browse the repository at this point in the history
This reverts commit 8b7af59.
  • Loading branch information
benallfree committed Feb 12, 2021
1 parent 8b7af59 commit f82f72c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 64 deletions.
13 changes: 3 additions & 10 deletions src/common/HTTPRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export class HTTPRequest {
interceptionId: string,
allowInterception: boolean,
event: Protocol.Network.RequestWillBeSentEvent,
redirectChain: HTTPRequest[]
redirectChain: HTTPRequest[],
allowCooperativeInterceptionMode = false
) {
this._client = client;
this._requestId = event.requestId;
Expand All @@ -146,7 +147,7 @@ export class HTTPRequest {
this._postData = event.request.postData;
this._frame = frame;
this._redirectChain = redirectChain;
this._allowCooperativeInterceptionMode = false;
this._allowCooperativeInterceptionMode = allowCooperativeInterceptionMode;
this._continueRequestOverrides = {};
this._shouldContinue = true; // Continue by default
this._shouldRespond = false;
Expand All @@ -164,14 +165,6 @@ export class HTTPRequest {
return this._url;
}

hasCooperativeInterceptHandlers(): boolean {
return this._pendingCooperativeInterceptionHandlers.length > 0;
}

setAllowCooperativeRequestInterceptionMode(isEnabled: boolean): boolean {
return (this._allowCooperativeInterceptionMode = isEnabled);
}

private _assertCooperativeInterceptionMode(): void {
assert(
this._allowCooperativeInterceptionMode,
Expand Down
19 changes: 11 additions & 8 deletions src/common/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export interface InternalNetworkConditions extends NetworkConditions {
*/
export const NetworkManagerEmittedEvents = {
Request: Symbol('NetworkManager.Request'),
CooperativeRequest: Symbol('NetworkManager.CooperativeRequest'),
Response: Symbol('NetworkManager.Response'),
RequestFailed: Symbol('NetworkManager.RequestFailed'),
RequestFinished: Symbol('NetworkManager.RequestFinished'),
Expand All @@ -76,6 +75,7 @@ export class NetworkManager extends EventEmitter {
_credentials?: Credentials = null;
_attemptedAuthentications = new Set<string>();
_userRequestInterceptionEnabled = false;
_userCooperativeRequestInterceptionModeEnabled = false;
_protocolRequestInterceptionEnabled = false;
_userCacheDisabled = false;
_requestIdToInterceptionId = new Map<string, string>();
Expand Down Expand Up @@ -190,8 +190,13 @@ export class NetworkManager extends EventEmitter {
await this._updateProtocolCacheDisabled();
}

async setRequestInterception(value: boolean): Promise<void> {
async setRequestInterception(
value: boolean,
useCooperativeInterceptsMode = false
): Promise<void> {
this._userRequestInterceptionEnabled = value;
this._userCooperativeRequestInterceptionModeEnabled =
value && useCooperativeInterceptsMode;
await this._updateProtocolRequestInterception();
}

Expand Down Expand Up @@ -313,18 +318,16 @@ export class NetworkManager extends EventEmitter {
interceptionId,
this._userRequestInterceptionEnabled,
event,
redirectChain
redirectChain,
this._userCooperativeRequestInterceptionModeEnabled
);
this._requestIdToRequest.set(event.requestId, request);
request.setAllowCooperativeRequestInterceptionMode(true);
this.emit(NetworkManagerEmittedEvents.CooperativeRequest, request);
if (request.hasCooperativeInterceptHandlers()) {
this.emit(NetworkManagerEmittedEvents.Request, request);
if (this._userCooperativeRequestInterceptionModeEnabled) {
request.finalizeCooperativeInterceptions().catch((error) => {
debugError(error);
});
}
request.setAllowCooperativeRequestInterceptionMode(false);
this.emit(NetworkManagerEmittedEvents.Request, request);
}

_onRequestServedFromCache(
Expand Down
77 changes: 31 additions & 46 deletions src/common/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import {
} from './EvalTypes.js';
import { PDFOptions, paperFormats } from './PDFOptions.js';
import { isNode } from '../environment.js';
import { EventType, Handler } from '../../vendor/mitt/src/index.js';

/**
* @public
Expand Down Expand Up @@ -288,26 +287,6 @@ export const enum PageEmittedEvents {
* and mutating requests.
*/
Request = 'request',
/**
* Emitted when a page issues a request and contains a {@link HTTPRequest}.
*
* @remarks
* The object is readonly. See {@Page.setRequestInterception} for intercepting
* and mutating requests.
*
* Cooperative intercept mode allows multiple handlers to call continue(),
* respond(), or abort() multiple times in any combination.
* Page will wait for all async handlers to resovle before resolving
* the request. If any handler called abort(), the request will be aborted.
* If no handler called abort() but any handler called respond(),
* the request will be responded. Otherwise, the request will be continued.
* No handler needs to call continue() since this is the default.
*
* Listening for this event will cause any listener of the `Request` event
* to throw an incompatibility exception.
*
*/
CooperativeRequest = 'cooperative_request',
/**
* Emitted when a request fails, for example by timing out.
*
Expand Down Expand Up @@ -434,6 +413,7 @@ export class Page extends EventEmitter {
private _viewport: Viewport | null;
private _screenshotTaskQueue: ScreenshotTaskQueue;
private _workers = new Map<string, WebWorker>();
private _useCooperativeInterceptMode = false;
// TODO: improve this typedef - it's a function that takes a file chooser or
// something?
private _fileChooserInterceptors = new Set<Function>();
Expand Down Expand Up @@ -504,9 +484,6 @@ export class Page extends EventEmitter {
networkManager.on(NetworkManagerEmittedEvents.Request, (event) =>
this.emit(PageEmittedEvents.Request, event)
);
networkManager.on(NetworkManagerEmittedEvents.CooperativeRequest, (event) =>
this.emit(PageEmittedEvents.CooperativeRequest, event)
);
networkManager.on(NetworkManagerEmittedEvents.Response, (event) =>
this.emit(PageEmittedEvents.Response, event)
);
Expand Down Expand Up @@ -712,15 +689,28 @@ export class Page extends EventEmitter {

/**
* @param value - Whether to enable request interception.
* @param useCooperativeInterceptMode - Whether to enable cooperative request interception.
*
* @remarks
* Activating request interception enables {@link HTTPRequest.abort},
* {@link HTTPRequest.continue} and {@link HTTPRequest.respond} methods. This
* provides the capability to modify network requests that are made by a page.
*
* Once request interception is enabled, every request will stall unless it's
* Legacy intercept mode (useCooperativeInterceptMode===false): one handler should
* call one of continue(), respond(), or abort(), one time. If any of these
* methods are called multiple times or in combination, an error will be
* thrown. Once request interception is enabled, every request will stall unless it's
* continued, responded or aborted.
*
* Cooperative intercept mode (useCooperativeInterceptMode===true):
* Multiple handlers can call continue(), respond(),
* or abort() multiple times in any combination.
* Page will wait for all async handlers to resovle before resolving
* the request. If any handler called abort(), the request will be aborted.
* If no handler called abort() but any handler called respond(),
* the request will be responded. Otherwise, the request will be continued.
* No handler needs to call continue() since this is the default.
*
* **NOTE** Enabling request interception disables page caching.
*
* @example
Expand All @@ -743,8 +733,14 @@ export class Page extends EventEmitter {
* })();
* ```
*/
async setRequestInterception(value: boolean): Promise<void> {
return this._frameManager.networkManager().setRequestInterception(value);
async setRequestInterception(
value: boolean,
useCooperativeInterceptMode = false
): Promise<void> {
this._useCooperativeInterceptMode = value && useCooperativeInterceptMode;
return this._frameManager
.networkManager()
.setRequestInterception(value, this._useCooperativeInterceptMode);
}

/**
Expand Down Expand Up @@ -2017,25 +2013,14 @@ export class Page extends EventEmitter {
return this.mainFrame().waitForFunction(pageFunction, options, ...args);
}

on(event: EventType, handler: Handler<any>): EventEmitter {
if (event === PageEmittedEvents.Request) {
return super.on(event, (req: HTTPRequest) => {
if (req.hasCooperativeInterceptHandlers()) {
throw new Error(
`Cooperative intercept mode is already enabled. Lecacy Request handlers are not supported.`
);
}
handler(req);
});
}
if (event === PageEmittedEvents.CooperativeRequest) {
return super.on(event, (req: HTTPRequest) => {
req.enqueuePendingCooperativeInterceptionHandler(
Promise.resolve(handler(req))
);
});
}
return super.on(event, handler);
on(event, handler) {
if (event !== 'request' || !this._useCooperativeInterceptMode)
return super.on(event, handler);
return super.on(event, (req: HTTPRequest) => {
req.enqueuePendingCooperativeInterceptionHandler(
Promise.resolve(handler(req))
);
});
}
}

Expand Down

0 comments on commit f82f72c

Please sign in to comment.