Skip to content

Commit

Permalink
feat: better support for network.provideResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Mar 22, 2024
1 parent bb7ccfe commit 9d8e90f
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 @@ -206,7 +206,7 @@ export class NetworkProcessor {
): Promise<EmptyResult> {
const {
statusCode,
reasonPhrase,
reasonPhrase: responsePhrase,
headers,
body,
request: networkId,
Expand All @@ -227,11 +227,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 9d8e90f

Please sign in to comment.