|
1 | 1 | import { CDPEventEmitter } from "../emitter"; |
| 2 | +import { CDPConnection } from "../connection"; |
| 3 | +import type { |
| 4 | + CDPFrameId, |
| 5 | + CDPNetworkErrorReason, |
| 6 | + CDPNetworkRequest, |
| 7 | + CDPNetworkResourceType, |
| 8 | + CDPSessionId, |
| 9 | +} from "../types"; |
| 10 | + |
| 11 | +/** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#type-RequestId */ |
| 12 | +type FetchRequestId = string; |
| 13 | + |
| 14 | +/** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#type-RequestStage */ |
| 15 | +type FetchRequestStage = "Request" | "Response"; |
| 16 | + |
| 17 | +/** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#type-HeaderEntry */ |
| 18 | +interface HeaderEntry { |
| 19 | + name: string; |
| 20 | + value: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#type-AuthChallenge */ |
| 24 | +interface AuthChallenge { |
| 25 | + /** Source of the authentication challenge. */ |
| 26 | + source?: "Server" | "Proxy"; |
| 27 | + /** Origin that issued the authentication challenge. */ |
| 28 | + origin: string; |
| 29 | + /** The authentication scheme used, such as basic or digest. */ |
| 30 | + scheme: string; |
| 31 | + /** The realm of the challenge. May be empty. */ |
| 32 | + realm: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#type-AuthChallengeResponse */ |
| 36 | +interface AuthChallengeResponse { |
| 37 | + /** |
| 38 | + * The decision on what to do in response to the authorization challenge. |
| 39 | + * Default means deferring to the default behavior of the net stack, which will likely either |
| 40 | + * the Cancel authentication or display a popup dialog box. |
| 41 | + */ |
| 42 | + response: "Default" | "CancelAuth" | "ProvideCredentials"; |
| 43 | + /** The username to provide, possibly empty. Should only be set if response is ProvideCredentials. */ |
| 44 | + username?: string; |
| 45 | + /** The password to provide, possibly empty. Should only be set if response is ProvideCredentials. */ |
| 46 | + password?: string; |
| 47 | +} |
| 48 | + |
| 49 | +/** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#type-RequestPattern */ |
| 50 | +interface RequestPattern { |
| 51 | + /** Wildcards ('*' -> zero or more, '?' -> exactly one) are allowed. Escape character is backslash. Omitting is equivalent to "*". */ |
| 52 | + urlPattern?: string; |
| 53 | + /** If set, only requests for matching resource types will be intercepted. */ |
| 54 | + resourceType?: CDPNetworkResourceType; |
| 55 | + /** Stage at which to begin intercepting requests. Default is Request. */ |
| 56 | + requestStage?: FetchRequestStage; |
| 57 | +} |
| 58 | + |
| 59 | +interface EnableRequest { |
| 60 | + /** If specified, only requests matching any of these patterns will produce fetchRequested event and will be paused until clients response. If not set, all requests will be affected. */ |
| 61 | + patterns?: RequestPattern[]; |
| 62 | + /** If true, authRequired events will be issued and requests will be paused expecting a call to continueWithAuth. */ |
| 63 | + handleAuthRequests?: boolean; |
| 64 | +} |
| 65 | + |
| 66 | +interface FailRequestRequest { |
| 67 | + /** An id the client received in requestPaused event. */ |
| 68 | + requestId: FetchRequestId; |
| 69 | + /** Causes the request to fail with the given reason. */ |
| 70 | + reason: CDPNetworkErrorReason; |
| 71 | +} |
| 72 | + |
| 73 | +interface FulfillRequestRequest { |
| 74 | + /** An id the client received in requestPaused event. */ |
| 75 | + requestId: FetchRequestId; |
| 76 | + /** An HTTP response code. */ |
| 77 | + responseCode: number; |
| 78 | + /** Response headers. */ |
| 79 | + responseHeaders?: HeaderEntry[]; |
| 80 | + /** Alternative way of specifying response headers as a \0-separated series of name: value pairs. Prefer the above method unless you need to represent some non-UTF8 values that can't be transmitted over the protocol as text. (Encoded as a base64 string when passed over JSON) */ |
| 81 | + binaryResponseHeaders?: string; |
| 82 | + /** A response body. If absent, original response body will be used if the request is intercepted at the response stage and empty body will be used if the request is intercepted at the request stage. (Encoded as a base64 string when passed over JSON) */ |
| 83 | + body?: string; |
| 84 | + /** A textual representation of responseCode. If absent, a standard phrase matching responseCode is used. */ |
| 85 | + responsePhrase?: string; |
| 86 | +} |
| 87 | + |
| 88 | +interface ContinueRequestRequest { |
| 89 | + /** An id the client received in requestPaused event. */ |
| 90 | + requestId: FetchRequestId; |
| 91 | + /** If set, the request url will be modified in a way that's not observable by page. */ |
| 92 | + url?: string; |
| 93 | + /** If set, the request method will be overridden. */ |
| 94 | + method?: string; |
| 95 | + /** If set, overrides the post data in the request. (Encoded as a base64 string when passed over JSON) */ |
| 96 | + postData?: string; |
| 97 | + /** If set, overrides the request headers. Note that the overrides do not extend to subsequent redirect hops, if a redirect happens. */ |
| 98 | + headers?: HeaderEntry[]; |
| 99 | +} |
| 100 | + |
| 101 | +interface ContinueWithAuthRequest { |
| 102 | + /** An id the client received in authRequired event. */ |
| 103 | + requestId: FetchRequestId; |
| 104 | + /** Response to with an authChallenge. */ |
| 105 | + authChallengeResponse: AuthChallengeResponse; |
| 106 | +} |
| 107 | + |
| 108 | +interface GetResponseBodyResponse { |
| 109 | + /** Response body. */ |
| 110 | + body: string; |
| 111 | + /** True, if content was sent as base64. */ |
| 112 | + base64Encoded: boolean; |
| 113 | +} |
| 114 | + |
| 115 | +interface TakeResponseBodyAsStreamResponse { |
| 116 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/IO/#type-StreamHandle */ |
| 117 | + stream: string; |
| 118 | +} |
2 | 119 |
|
3 | 120 | export interface FetchEvents { |
4 | | - authRequired: Record<string, unknown>; |
5 | | - requestPaused: Record<string, unknown>; |
| 121 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#event-requestPaused */ |
| 122 | + requestPaused: { |
| 123 | + /** Each request the page makes will have a unique id. */ |
| 124 | + requestId: FetchRequestId; |
| 125 | + /** The details of the request. */ |
| 126 | + request: CDPNetworkRequest; |
| 127 | + /** The id of the frame that initiated the request. */ |
| 128 | + frameId: CDPFrameId; |
| 129 | + /** How the requested resource will be used. */ |
| 130 | + resourceType: CDPNetworkResourceType; |
| 131 | + /** Response error if intercepted at response stage. */ |
| 132 | + responseErrorReason?: CDPNetworkErrorReason; |
| 133 | + /** Response code if intercepted at response stage. */ |
| 134 | + responseStatusCode?: number; |
| 135 | + /** Response status text if intercepted at response stage. */ |
| 136 | + responseStatusText?: string; |
| 137 | + /** Response headers if intercepted at the response stage. */ |
| 138 | + responseHeaders?: HeaderEntry[]; |
| 139 | + /** If the intercepted request had a corresponding Network.requestWillBeSent event fired for it, then this networkId will be the same as the requestId present in the requestWillBeSent event. */ |
| 140 | + networkId?: FetchRequestId; |
| 141 | + }; |
| 142 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#event-authRequired */ |
| 143 | + authRequired: { |
| 144 | + /** Each request the page makes will have a unique id. */ |
| 145 | + requestId: FetchRequestId; |
| 146 | + /** The details of the request. */ |
| 147 | + request: CDPNetworkRequest; |
| 148 | + /** The id of the frame that initiated the request. */ |
| 149 | + frameId: CDPFrameId; |
| 150 | + /** How the requested resource will be used. */ |
| 151 | + resourceType: CDPNetworkResourceType; |
| 152 | + /** Details of the Authorization Challenge encountered. If this is set, client should respond with continueRequest that contains AuthChallengeResponse. */ |
| 153 | + authChallenge: AuthChallenge; |
| 154 | + }; |
6 | 155 | } |
7 | 156 |
|
8 | 157 | /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/ */ |
9 | 158 | export class CDFetch extends CDPEventEmitter<FetchEvents> { |
10 | | - public constructor() { |
| 159 | + private readonly _connection: CDPConnection; |
| 160 | + |
| 161 | + public constructor(connection: CDPConnection) { |
11 | 162 | super(); |
| 163 | + |
| 164 | + this._connection = connection; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @param sessionId result of "Target.attachToTarget" |
| 169 | + * @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-enable |
| 170 | + */ |
| 171 | + async enable(sessionId: CDPSessionId, params?: EnableRequest): Promise<void> { |
| 172 | + return this._connection.request("Fetch.enable", { sessionId, params }); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @param sessionId result of "Target.attachToTarget" |
| 177 | + * @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-disable |
| 178 | + */ |
| 179 | + async disable(sessionId: CDPSessionId): Promise<void> { |
| 180 | + return this._connection.request("Fetch.disable", { sessionId }); |
| 181 | + } |
| 182 | + |
| 183 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-failRequest */ |
| 184 | + async failRequest(sessionId: CDPSessionId, params: FailRequestRequest): Promise<void> { |
| 185 | + return this._connection.request("Fetch.failRequest", { sessionId, params }); |
| 186 | + } |
| 187 | + |
| 188 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-fulfillRequest */ |
| 189 | + async fulfillRequest(sessionId: CDPSessionId, params: FulfillRequestRequest): Promise<void> { |
| 190 | + return this._connection.request("Fetch.fulfillRequest", { sessionId, params }); |
| 191 | + } |
| 192 | + |
| 193 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-continueRequest */ |
| 194 | + async continueRequest(sessionId: CDPSessionId, params: ContinueRequestRequest): Promise<void> { |
| 195 | + return this._connection.request("Fetch.continueRequest", { sessionId, params }); |
| 196 | + } |
| 197 | + |
| 198 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-continueWithAuth */ |
| 199 | + async continueWithAuth(sessionId: CDPSessionId, params: ContinueWithAuthRequest): Promise<void> { |
| 200 | + return this._connection.request("Fetch.continueWithAuth", { sessionId, params }); |
| 201 | + } |
| 202 | + |
| 203 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-getResponseBody */ |
| 204 | + async getResponseBody(sessionId: CDPSessionId, requestId: FetchRequestId): Promise<GetResponseBodyResponse> { |
| 205 | + return this._connection.request("Fetch.getResponseBody", { sessionId, params: { requestId } }); |
| 206 | + } |
| 207 | + |
| 208 | + /** @link https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/#method-takeResponseBodyAsStream */ |
| 209 | + async takeResponseBodyAsStream( |
| 210 | + sessionId: CDPSessionId, |
| 211 | + requestId: FetchRequestId, |
| 212 | + ): Promise<TakeResponseBodyAsStreamResponse> { |
| 213 | + return this._connection.request("Fetch.takeResponseBodyAsStream", { sessionId, params: { requestId } }); |
12 | 214 | } |
13 | 215 | } |
0 commit comments