Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract toggling of Network Interception #12266

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 27 additions & 18 deletions packages/puppeteer-core/src/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

/**
Expand All @@ -531,17 +526,31 @@ 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],
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 (!credentials && this.#authInterception) {
} else if (!expected && interception) {
await this.#frame.browsingContext.userContext.browser.removeIntercept(
this.#authInterception
interception
);
this.#authInterception = undefined;
return;
}
this._credentials = credentials;
return interception;
}

override setDragInterception(): never {
Expand Down
12 changes: 5 additions & 7 deletions packages/puppeteer-core/src/cdp/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops this was suppose to be separate, tell me if you want me to extract.
But we should should change the state only after assertion is completed.

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));
}
Expand Down