refactor: extract toggling of Network Interception (#12266)

This commit is contained in:
Nikolay Vitkov 2024-04-12 14:29:06 +02:00 committed by GitHub
parent df986ef2dd
commit 402b4a4812
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 28 deletions

View File

@ -511,18 +511,13 @@ export class BidiPage extends Page {
return [...this.#workers];
}
#interception?: string;
#userInterception?: string;
override async setRequestInterception(enable: boolean): Promise<void> {
if (enable && !this.#interception) {
this.#interception = await this.#frame.browsingContext.addIntercept({
phases: [Bidi.Network.InterceptPhase.BeforeRequestSent],
});
} else if (!enable && this.#interception) {
await this.#frame.browsingContext.userContext.browser.removeIntercept(
this.#interception
);
this.#interception = undefined;
}
this.#userInterception = await this.#toggleInterception(
[Bidi.Network.InterceptPhase.BeforeRequestSent],
this.#userInterception,
enable
);
}
/**
@ -531,19 +526,33 @@ export class BidiPage extends Page {
_credentials: Credentials | null = null;
#authInterception?: string;
override async authenticate(credentials: Credentials | null): Promise<void> {
if (credentials && !this.#authInterception) {
this.#authInterception = await this.#frame.browsingContext.addIntercept({
phases: [Bidi.Network.InterceptPhase.AuthRequired],
});
} else if (!credentials && this.#authInterception) {
await this.#frame.browsingContext.userContext.browser.removeIntercept(
this.#authInterception
);
this.#authInterception = undefined;
}
this.#authInterception = await this.#toggleInterception(
[Bidi.Network.InterceptPhase.AuthRequired],
this.#authInterception,
Boolean(credentials)
);
this._credentials = credentials;
}
async #toggleInterception(
phases: [Bidi.Network.InterceptPhase, ...Bidi.Network.InterceptPhase[]],
interception: string | undefined,
expected: boolean
): Promise<string | undefined> {
if (expected && !interception) {
return await this.#frame.browsingContext.addIntercept({
phases,
});
} else if (!expected && interception) {
await this.#frame.browsingContext.userContext.browser.removeIntercept(
interception
);
return;
}
return interception;
}
override setDragInterception(): never {
throw new UnsupportedOperation();
}

View File

@ -140,18 +140,16 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
);
}
async setExtraHTTPHeaders(
extraHTTPHeaders: Record<string, string>
): Promise<void> {
this.#extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders)) {
const value = extraHTTPHeaders[key];
async setExtraHTTPHeaders(headers: Record<string, string>): Promise<void> {
const extraHTTPHeaders: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
assert(
isString(value),
`Expected value of header "${key}" to be String, but "${typeof value}" is found.`
);
this.#extraHTTPHeaders[key.toLowerCase()] = value;
extraHTTPHeaders[key.toLowerCase()] = value;
}
this.#extraHTTPHeaders = extraHTTPHeaders;
await this.#applyToAllClients(this.#applyExtraHTTPHeaders.bind(this));
}