-
Notifications
You must be signed in to change notification settings - Fork 6k
feat(routeFromHar): add interceptAPIRequests option #41294
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
base: main
Are you sure you want to change the base?
Changes from all commits
9586308
e0fc0be
c2281e0
ab2893c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
|
|
||
| import { debugLogger } from '@utils/debugLogger'; | ||
| import { isRegExp, isString } from '@isomorphic/rtti'; | ||
|
|
||
| import type { BrowserContext } from './browserContext'; | ||
| import type { LocalUtils } from './localUtils'; | ||
|
|
@@ -29,6 +30,7 @@ export class HarRouter { | |
| private _harId: string; | ||
| private _notFoundAction: HarNotFoundAction; | ||
| private _options: { urlMatch?: URLMatch; baseURL?: string; }; | ||
| private _apiRequestRegistrations: { context: BrowserContext, registrationId: string }[] = []; | ||
|
|
||
| static async create(localUtils: LocalUtils, file: string, notFoundAction: HarNotFoundAction, options: { urlMatch?: URLMatch }): Promise<HarRouter> { | ||
| const { harId, error } = await localUtils.harOpen({ file }); | ||
|
|
@@ -117,11 +119,26 @@ export class HarRouter { | |
| await page.route(this._options.urlMatch || '**/*', route => this._handle(route)); | ||
| } | ||
|
|
||
| async addAPIRequestRoute(context: BrowserContext) { | ||
| const urlMatch = this._options.urlMatch; | ||
| const { registrationId } = await context._channel.routeAPIRequestsFromHar({ | ||
| harId: this._harId, | ||
| urlGlob: isString(urlMatch) ? urlMatch : undefined, | ||
| urlRegexSource: isRegExp(urlMatch) ? urlMatch.source : undefined, | ||
| urlRegexFlags: isRegExp(urlMatch) ? urlMatch.flags : undefined, | ||
| notFound: this._notFoundAction, | ||
| }); | ||
| this._apiRequestRegistrations.push({ context, registrationId }); | ||
| } | ||
|
|
||
| async [Symbol.asyncDispose]() { | ||
| await this.dispose(); | ||
| } | ||
|
|
||
| dispose() { | ||
| this._localUtils.harClose({ harId: this._harId }).catch(() => {}); | ||
|
Contributor
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: this should probably be the last thing instead of the first |
||
| for (const { context, registrationId } of this._apiRequestRegistrations) | ||
| context._channel.unrouteAPIRequestsFromHar({ registrationId }).catch(() => {}); | ||
| this._apiRequestRegistrations = []; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,10 +46,13 @@ import type { Request, Response, RouteHandler } from '../network'; | |
| import type { InitScript, Page, PageError } from '../page'; | ||
| import type { Disposable } from '../disposable'; | ||
| import type { DispatcherScope } from './dispatcher'; | ||
| import type { LocalUtilsDispatcher } from './localUtilsDispatcher'; | ||
| import type * as channels from '../channels'; | ||
| import type { Progress } from '../progress'; | ||
| import type { URLMatch } from '@isomorphic/urlMatch'; | ||
|
|
||
| type HarForAPIRequestsDisposable = Disposable & { registrationId: string }; | ||
|
|
||
| export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextChannel, DispatcherScope> implements channels.BrowserContextChannel { | ||
| _type_BrowserContext = true; | ||
| private _context: BrowserContext; | ||
|
|
@@ -335,6 +338,39 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel | |
| this._routeWebSocketInitScript = await WebSocketRouteDispatcher.install(progress, this.connection, this._context); | ||
| } | ||
|
|
||
| async routeAPIRequestsFromHar(params: channels.BrowserContextRouteAPIRequestsFromHarParams, progress: Progress): Promise<channels.BrowserContextRouteAPIRequestsFromHarResult> { | ||
| // Reuse the HarBackend that was already opened via localUtils.harOpen for the page-side | ||
| // route, rather than opening a second backend for the same HAR file. The backend is owned | ||
| // by LocalUtils and closed via harClose, so this registration must not dispose it. | ||
| const localUtils = [...this.connection._dispatcherByGuid.values()].find(d => d._type === 'LocalUtils') as LocalUtilsDispatcher | undefined; | ||
|
Contributor
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: is there no better way of finding this than by looking at the |
||
| const harBackend = localUtils?.harBackendForId(params.harId); | ||
| if (!harBackend) | ||
| throw new Error('Internal error: har was not opened'); | ||
| const urlMatch: URLMatch | undefined = | ||
| params.urlRegexSource !== undefined && params.urlRegexFlags !== undefined ? new RegExp(params.urlRegexSource, params.urlRegexFlags) : | ||
| params.urlGlob !== undefined ? params.urlGlob : undefined; | ||
| const registrationId = createGuid(); | ||
| const registration = this._context.routeAPIRequestsFromHar({ | ||
| harBackend, | ||
| urlMatch, | ||
| notFound: params.notFound, | ||
| baseURL: this._context._options.baseURL, | ||
| }); | ||
| this._disposables.push({ | ||
| registrationId, | ||
| dispose: async () => registration.dispose(), | ||
| } as HarForAPIRequestsDisposable); | ||
| return { registrationId }; | ||
| } | ||
|
|
||
| async unrouteAPIRequestsFromHar(params: channels.BrowserContextUnrouteAPIRequestsFromHarParams, progress: Progress): Promise<void> { | ||
| const index = this._disposables.findIndex(d => (d as HarForAPIRequestsDisposable).registrationId === params.registrationId); | ||
| if (index === -1) | ||
| return; | ||
| const [disposable] = this._disposables.splice(index, 1); | ||
| await progress.race(disposable.dispose()); | ||
| } | ||
|
|
||
| async storageState(params: channels.BrowserContextStorageStateParams, progress: Progress): Promise<channels.BrowserContextStorageStateResult> { | ||
| return await this._context.storageState(progress, params.indexedDB, params.credentials); | ||
| } | ||
|
|
||
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.
is there a way to use a
RouteHandlerlikeaddContextRoute/addPageRoutesince it seems like_handlemight already be able to do a lot of what you're adding in the backend (i.e. loading and looking up in a.har)?Uh oh!
There was an error while loading. Please reload this page.
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.
RouteHandler/_handlefulfills a browserRoute, which only exists for traffic that reaches the browser/CDP.APIRequestContextissues requests directly over Nodehttp/https(_sendRequest) and never creates aRoute, so_handlecan't intercept it — this why #11502 added har recording/tracing ability toAPIRequestContextseparately.