Skip to content

Commit

Permalink
feat: better support for network.provideResponse (#2065)
Browse files Browse the repository at this point in the history
TODO: Update comment is the code
  • Loading branch information
Lightning00Blade authored Mar 22, 2024
1 parent 405b995 commit 99f81fe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 44 deletions.
35 changes: 31 additions & 4 deletions src/bidiMapper/domains/network/NetworkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class NetworkProcessor {
): Promise<EmptyResult> {
const {
statusCode,
reasonPhrase,
reasonPhrase: responsePhrase,
headers,
body,
request: networkId,
Expand All @@ -218,11 +218,38 @@ export class NetworkProcessor {
Network.InterceptPhase.AuthRequired,
]);

// We need to pass through if the request is already in
// AuthRequired phase
if (request.interceptPhase === Network.InterceptPhase.AuthRequired) {
// We need to use `ProvideCredentials`
// As `Default` may cancel the request
await request.continueWithAuth({
response: 'ProvideCredentials',
});
return {};
}

// If we con't modify the response
// Just continue the request
if (!body && !headers) {
await request.continueRequest();
return {};
}

const responseCode = statusCode ?? request.statusCode ?? 200;

let parsedBody: string | undefined;
if (body?.type === 'string') {
parsedBody = btoa(body.value);
} else if (body?.type === 'base64') {
parsedBody = body.value;
}

await request.provideResponse({
responseCode: statusCode ?? request.statusCode,
responsePhrase: reasonPhrase,
responseCode,
responsePhrase,
responseHeaders,
body: body?.value, // TODO: Differ base64 / string
body: parsedBody,
});

return {};
Expand Down
7 changes: 3 additions & 4 deletions src/bidiMapper/domains/network/NetworkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,11 @@ export class NetworkRequest {
}

/** Returns the HTTP status code associated with this request if any. */
get statusCode(): number {
get statusCode(): number | undefined {
return (
this.#response.paused?.responseStatusCode ??
this.#response.extraInfo?.statusCode ??
this.#response.info?.status ??
-1 // TODO: Throw an exception or use some other status code?
this.#response.info?.status
);
}

Expand Down Expand Up @@ -544,7 +543,7 @@ export class NetworkRequest {
return {
url: this.url,
protocol: this.#response.info?.protocol ?? '',
status: this.statusCode,
status: this.statusCode ?? -1, // TODO: Throw an exception or use some other status code?
statusText:
this.#response.info?.statusText ||
this.#response.paused?.responseStatusText ||
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 99f81fe

Please sign in to comment.