-
Notifications
You must be signed in to change notification settings - Fork 5.6k
chore(bidi): improve request interception #39750
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ export class BidiBrowser extends Browser { | |
| readonly _contexts = new Map<string, BidiBrowserContext>(); | ||
| readonly _bidiPages = new Map<bidi.BrowsingContext.BrowsingContext, BidiPage>(); | ||
| private readonly _eventListeners: RegisteredListener[]; | ||
| private _cacheBehavior: bidi.Network.SetCacheBehaviorParameters['cacheBehavior'] = 'default'; | ||
|
|
||
| static async connect(parent: SdkObject, transport: ConnectionTransport, options: BrowserOptions): Promise<BidiBrowser> { | ||
| const browser = new BidiBrowser(parent, transport, options); | ||
|
|
@@ -74,6 +75,8 @@ export class BidiBrowser extends Browser { | |
| ], | ||
| }); | ||
|
|
||
| await browser._browserSession.send('network.addIntercept', { phases: [bidi.Network.InterceptPhase.AuthRequired] }); | ||
|
|
||
| await browser._browserSession.send('network.addDataCollector', { | ||
| dataTypes: [bidi.Network.DataType.Response], | ||
| maxEncodedDataSize: 20_000_000, // same default as in CDP: https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/inspector/inspector_network_agent.cc;l=134;drc=4128411589187a396829a827f59a655bed876aa7 | ||
|
|
@@ -132,6 +135,14 @@ export class BidiBrowser extends Browser { | |
| return !this._connection.isClosed(); | ||
| } | ||
|
|
||
| async updateCacheBehavior() { | ||
| const cacheBehavior = [...this._contexts.values()].some(context => context.requestInterceptors.length > 0) ? 'bypass' : 'default'; | ||
| if (this._cacheBehavior !== cacheBehavior) { | ||
| await this._browserSession.send('network.setCacheBehavior', { cacheBehavior }); | ||
| this._cacheBehavior = cacheBehavior; | ||
| } | ||
| } | ||
|
|
||
| private _onBrowsingContextCreated(event: bidi.BrowsingContext.Info) { | ||
| if (event.parent) { | ||
| const parentFrameId = event.parent; | ||
|
|
@@ -415,18 +426,18 @@ export class BidiBrowserContext extends BrowserContext { | |
| } | ||
|
|
||
| async doUpdateRequestInterception(): Promise<void> { | ||
| let interceptPromise = Promise.resolve<any>(undefined); | ||
| if (this.requestInterceptors.length > 0 && !this._interceptId) { | ||
| const { intercept } = await this._browser._browserSession.send('network.addIntercept', { | ||
| interceptPromise = this._browser._browserSession.send('network.addIntercept', { | ||
| phases: [bidi.Network.InterceptPhase.BeforeRequestSent], | ||
| urlPatterns: [{ type: 'pattern' }], | ||
| }); | ||
| this._interceptId = intercept; | ||
| }).then(({ intercept }) => this._interceptId = intercept); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's unify the way |
||
| } | ||
| if (this.requestInterceptors.length === 0 && this._interceptId) { | ||
| const intercept = this._interceptId; | ||
| this._interceptId = undefined; | ||
| await this._browser._browserSession.send('network.removeIntercept', { intercept }); | ||
| interceptPromise = this._browser._browserSession.send('network.removeIntercept', { intercept }); | ||
| } | ||
| await Promise.all([this._browser.updateCacheBehavior(), interceptPromise]); | ||
| } | ||
|
|
||
| override async doUpdateDefaultViewport() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it doesn't affect performance of non-auth requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, it doesn't.
Furthermore, we were previously adding a
BeforeRequestSentinterception (impacting performance of all requests) even if only theAuthRequiredinterception was needed, so this PR will improve performance in some cases.