/// import { Browser } from './browser.js'; import { Target } from './target.js'; import { CdpHeaderEntry, CdpHeaders, CdpNetworkErrorReason, CdpRequest, CdpRequestPaused } from './types'; export declare type InterceptorHandler = (req: InterceptedRequest) => Promise; /** * Manages request interception. * * Most common use cases require request interception to span across all connected targets. * Additionally, request interception may be required by multiple unrelated logical parts * of the automation. * * To address these concerns, Automation Engine includes modular request interception APIs * with following features: * * - multiple interceptors can be stacked together; interception callback allows * passing request further along the interceptors chain (with or without modifications), * as well as handling the request exclusively (e.g. by fulfilling it with custom response, * or aborting it, or sending it with or without modifications bypassing next interceptors) * - interceptors can be added and removed dynamically via synchronous APIs to minimize race * conditions * - callbacks are executed asynchronously, allowing for custom I/O in handlers * * ### Details * * - Each opened target enables request interception from very beginning, sending `Fetch.enable`. * Note that only `Request` stage interception is currently supported * (which means we can't fire request onwards and later modify the response). * - Request interceptors are installed via `Browser#interceptRequests` * - Interceptors are processed in the same order as installed. * - Each interceptor's handler is executed for each request. * - Handler may produce an outcome which *immediately* results in one of * `fulfillResponse`, `failRequest` or `continueRequest` commands being sent and * subsequent interceptors *not* invoked. * - If handler does not return an outcome, the request is passed onwards to the next interceptor and so forth — * until the end of interceptors chain. * - At the end, if no interceptors produced an outcome, the request is resumed unmodified * (as per normal browser behaviour). * - Handler can return an outcome by invoking one of the `InterceptedRequest` methods and returning its results. */ export declare class Interceptor { browser: Browser; handler: InterceptorHandler; label: string; id: string; constructor(browser: Browser, handler: InterceptorHandler, label: string); /** * Removes this interceptor from the chain. * * Note: this does not terminate any active interception callbacks; rather, * the callback will no longer be called for new requests. */ stop(): void; } /** * An instance of intercepted request. * * Methods of this class create interception outcomes (return values of interception callback). * * Modifying instance properties and calling `pass` allows passing * modified version of request further along the chain of interceptors. */ export declare class InterceptedRequest { target: Target; requestId: string; frameId: string; resourceType: string; request: CdpRequest; responseErrorReason?: string; responseStatusCode?: number; responseHeaders?: CdpHeaderEntry[]; networkId?: string; protected modifications: InterceptorModifications; constructor(target: Target, payload: CdpRequestPaused); /** * Aborts the request with specified error reason. * If the request is used for navigation, Chrome will display one of its predefined error pages * based on provided reason. * * @param errorReason CDP error reason. */ fail(errorReason: CdpNetworkErrorReason): InterceptorOutcome; /** * Fulfils the request with custom response. * The request will not be sent and will not be propagated to next interceptors. * * @param response Reponse data to fulfil the request with. */ fulfill(response: InterceptorResponse): InterceptorOutcome; /** * Sends the request with optional modifications. * The request will bypass next interceptors in chain. * * In most cases `pass` should be used instead, to allow interceptors stacking. * Use `continue` only when processing the request by other interceptors is undesired. * * @param mods Optional request modifications. */ continue(mods?: InterceptorModifications): InterceptorOutcome; /** * Passes request further down the interception chain. * If there are no more interceptors, the request will be sent * with current modifications applied. * * This should be a default interceptor outcome for most cases, unless * the interceptor specifically wants to avoid any other interceptors * to alter request behavior (in such case `continue` should be used). * * @param mods Optional request modifications. */ pass(mods?: InterceptorModifications): InterceptorOutcome; getEffectiveHeaders(): CdpHeaders | undefined; getEffectiveHeaderEntries(): CdpHeaderEntry[] | undefined; protected assignModifications(mods: InterceptorModifications): void; } /** * Interceptor outcome, produced by methods on `InterceptedRequest`. * * @internal */ export interface InterceptorOutcome { method: string; params: any; } /** * Response data for fulfilling the request with custom response. */ export interface InterceptorResponse { responseCode: number; responseHeaders?: CdpHeaders; body?: string | Buffer; } /** * Represents request fields that can be modified. */ export interface InterceptorModifications { /** * If specified, request URL will be modified. */ url?: string; /** * If specified, request method will be modified. */ method?: string; /** * If specified, request post data will be modified. */ postData?: string; /** * If specified, extra headers are added to request. */ extraHeaders?: CdpHeaders; /** * If specified, request headers are replaced. * Note: this replaces headers completely, with any extra headers discarded. */ headers?: CdpHeaders; } //# sourceMappingURL=interceptor.d.ts.map