Skip to content

Commit 05a306c

Browse files
authored
cherry-pick(#41964): Revert "feat(routeFromHar): add interceptAPIRequests option (#41294)" (#41965)
1 parent 2934858 commit 05a306c

18 files changed

Lines changed: 18 additions & 750 deletions

File tree

‎docs/src/api/class-browsercontext.md‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,12 +1310,6 @@ When set to `minimal`, only record information necessary for routing from HAR. T
13101310

13111311
Optional setting to control resource content management. If `attach` is specified, resources are persisted as separate files or entries in the ZIP archive. If `embed` is specified, content is stored inline the HAR file.
13121312

1313-
### option: BrowserContext.routeFromHAR.interceptAPIRequests
1314-
* since: v1.62
1315-
- `interceptAPIRequests` <[boolean]>
1316-
1317-
If set to `true`, requests made via [APIRequestContext] (such as [`property: BrowserContext.request`] or [`property: Page.request`]) are also served from the HAR file. By default these requests are sent to the network, matching the behavior prior to v1.62. Defaults to `false` for backward compatibility.
1318-
13191313

13201314
## async method: BrowserContext.routeWebSocket
13211315
* since: v1.48

‎packages/isomorphic/protocolMetainfo.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ export const methodMetainfo = new Map<string, MethodMetainfo>([
8989
['BrowserContext.setGeolocation', { title: 'Set geolocation', group: 'configuration', }],
9090
['BrowserContext.setHTTPCredentials', { title: 'Set HTTP credentials', group: 'configuration', }],
9191
['BrowserContext.setNetworkInterceptionPatterns', { title: 'Route requests', group: 'route', }],
92-
['BrowserContext.routeAPIRequestsFromHar', { internal: true, }],
93-
['BrowserContext.unrouteAPIRequestsFromHar', { internal: true, }],
9492
['BrowserContext.setWebSocketInterceptionPatterns', { title: 'Route WebSockets', group: 'route', }],
9593
['BrowserContext.setOffline', { title: 'Set offline mode', }],
9694
['BrowserContext.storageState', { title: 'Get storage state', group: 'configuration', }],

‎packages/playwright-client/types/types.d.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10258,15 +10258,6 @@ export interface BrowserContext {
1025810258
* @param options
1025910259
*/
1026010260
routeFromHAR(har: string, options?: {
10261-
/**
10262-
* If set to `true`, requests made via [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext)
10263-
* (such as [browserContext.request](https://playwright.dev/docs/api/class-browsercontext#browser-context-request) or
10264-
* [page.request](https://playwright.dev/docs/api/class-page#page-request)) are also served from the HAR file. By
10265-
* default these requests are sent to the network, matching the behavior prior to v1.62. Defaults to `false` for
10266-
* backward compatibility.
10267-
*/
10268-
interceptAPIRequests?: boolean;
10269-
1027010261
/**
1027110262
* - If set to 'abort' any request not found in the HAR file will be aborted.
1027210263
* - If set to 'fallback' falls through to the next route handler in the handler chain.

‎packages/playwright-core/src/client/browserContext.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
398398
await this._updateWebSocketInterceptionPatterns({ title: 'Route WebSockets' });
399399
}
400400

401-
async routeFromHAR(har: string, options: { url?: string | RegExp, notFound?: 'abort' | 'fallback', update?: boolean, updateContent?: 'attach' | 'embed', updateMode?: 'minimal' | 'full', interceptAPIRequests?: boolean } = {}): Promise<void> {
401+
async routeFromHAR(har: string, options: { url?: string | RegExp, notFound?: 'abort' | 'fallback', update?: boolean, updateContent?: 'attach' | 'embed', updateMode?: 'minimal' | 'full' } = {}): Promise<void> {
402402
const localUtils = this._connection.localUtils();
403403
if (!localUtils)
404404
throw new Error('Route from har is not supported in thin clients');
@@ -409,8 +409,6 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
409409
const harRouter = await HarRouter.create(localUtils, har, options.notFound || 'abort', { urlMatch: options.url });
410410
this._harRouters.push(harRouter);
411411
await harRouter.addContextRoute(this);
412-
if (options.interceptAPIRequests)
413-
await harRouter.addAPIRequestRoute(this);
414412
}
415413

416414
private _disposeHarRouters() {

‎packages/playwright-core/src/client/channels.d.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,6 @@ export interface BrowserContextChannel extends BrowserContextEventTarget, Channe
12731273
setGeolocation(params: BrowserContextSetGeolocationParams, options: TimeoutOptions): Promise<BrowserContextSetGeolocationResult>;
12741274
setHTTPCredentials(params: BrowserContextSetHTTPCredentialsParams, options: TimeoutOptions): Promise<BrowserContextSetHTTPCredentialsResult>;
12751275
setNetworkInterceptionPatterns(params: BrowserContextSetNetworkInterceptionPatternsParams, options: TimeoutOptions): Promise<BrowserContextSetNetworkInterceptionPatternsResult>;
1276-
routeAPIRequestsFromHar(params: BrowserContextRouteAPIRequestsFromHarParams, options: TimeoutOptions): Promise<BrowserContextRouteAPIRequestsFromHarResult>;
1277-
unrouteAPIRequestsFromHar(params: BrowserContextUnrouteAPIRequestsFromHarParams, options: TimeoutOptions): Promise<BrowserContextUnrouteAPIRequestsFromHarResult>;
12781276
setWebSocketInterceptionPatterns(params: BrowserContextSetWebSocketInterceptionPatternsParams, options: TimeoutOptions): Promise<BrowserContextSetWebSocketInterceptionPatternsResult>;
12791277
setOffline(params: BrowserContextSetOfflineParams, options: TimeoutOptions): Promise<BrowserContextSetOfflineResult>;
12801278
storageState(params: BrowserContextStorageStateParams, options: TimeoutOptions): Promise<BrowserContextStorageStateResult>;
@@ -1509,28 +1507,6 @@ export type BrowserContextSetNetworkInterceptionPatternsOptions = {
15091507

15101508
};
15111509
export type BrowserContextSetNetworkInterceptionPatternsResult = void;
1512-
export type BrowserContextRouteAPIRequestsFromHarParams = {
1513-
harId: string,
1514-
urlGlob?: string,
1515-
urlRegexSource?: string,
1516-
urlRegexFlags?: string,
1517-
notFound: 'abort' | 'fallback',
1518-
};
1519-
export type BrowserContextRouteAPIRequestsFromHarOptions = {
1520-
urlGlob?: string,
1521-
urlRegexSource?: string,
1522-
urlRegexFlags?: string,
1523-
};
1524-
export type BrowserContextRouteAPIRequestsFromHarResult = {
1525-
registrationId: string,
1526-
};
1527-
export type BrowserContextUnrouteAPIRequestsFromHarParams = {
1528-
registrationId: string,
1529-
};
1530-
export type BrowserContextUnrouteAPIRequestsFromHarOptions = {
1531-
1532-
};
1533-
export type BrowserContextUnrouteAPIRequestsFromHarResult = void;
15341510
export type BrowserContextSetWebSocketInterceptionPatternsParams = {
15351511
patterns: {
15361512
glob?: string,

‎packages/playwright-core/src/client/harRouter.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616

1717
import { debugLogger } from '@utils/debugLogger';
18-
import { isRegExp, isString } from '@isomorphic/rtti';
1918

20-
import { kNoTimeout } from './timeoutSettings';
2119
import type { BrowserContext } from './browserContext';
2220
import type { LocalUtils } from './localUtils';
2321
import type { Route } from './network';
@@ -31,7 +29,6 @@ export class HarRouter {
3129
private _harId: string;
3230
private _notFoundAction: HarNotFoundAction;
3331
private _options: { urlMatch?: URLMatch; baseURL?: string; };
34-
private _apiRequestRegistrations: { context: BrowserContext, registrationId: string }[] = [];
3532

3633
static async create(localUtils: LocalUtils, file: string, notFoundAction: HarNotFoundAction, options: { urlMatch?: URLMatch }): Promise<HarRouter> {
3734
const { harId, error } = await localUtils.harOpen({ file });
@@ -120,26 +117,11 @@ export class HarRouter {
120117
await page.route(this._options.urlMatch || '**/*', route => this._handle(route));
121118
}
122119

123-
async addAPIRequestRoute(context: BrowserContext) {
124-
const urlMatch = this._options.urlMatch;
125-
const { registrationId } = await context._channel.routeAPIRequestsFromHar({
126-
harId: this._harId,
127-
urlGlob: isString(urlMatch) ? urlMatch : undefined,
128-
urlRegexSource: isRegExp(urlMatch) ? urlMatch.source : undefined,
129-
urlRegexFlags: isRegExp(urlMatch) ? urlMatch.flags : undefined,
130-
notFound: this._notFoundAction,
131-
}, kNoTimeout);
132-
this._apiRequestRegistrations.push({ context, registrationId });
133-
}
134-
135120
async [Symbol.asyncDispose]() {
136121
await this.dispose();
137122
}
138123

139124
dispose() {
140-
for (const { context, registrationId } of this._apiRequestRegistrations)
141-
context._channel.unrouteAPIRequestsFromHar({ registrationId }, kNoTimeout).catch(() => {});
142-
this._apiRequestRegistrations = [];
143125
this._localUtils.harClose({ harId: this._harId }).catch(() => {});
144126
}
145127
}

‎packages/playwright-core/src/server/browserContext.ts‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ import type { Browser, BrowserOptions } from './browser';
3939
import type { ConsoleMessage } from './console';
4040
import type { Download } from './download';
4141
import type * as frames from './frames';
42-
import type { HarBackend } from './harBackend';
4342
import type { PageError } from './page';
4443
import type { Progress } from './progress';
4544
import type { ClientCertificatesProxy } from './socksClientCertificatesInterceptor';
4645
import type { SerializedStorage } from '@injected/storageScript';
4746
import type * as types from './types';
48-
import type { URLMatch } from '@isomorphic/urlMatch';
4947
import type * as channels from './channels';
5048

5149
const BrowserContextEvent = {
@@ -122,7 +120,6 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
122120
private _playwrightBindingExposed?: Promise<void>;
123121
readonly dialogManager: DialogManager;
124122
private _consoleApiExposed = false;
125-
private _harForAPIRequests: HarForAPIRequestsRegistration[] = [];
126123

127124
constructor(browser: Browser, options: types.BrowserContextOptions, browserContextId: string | undefined) {
128125
super(browser, 'browser-context');
@@ -750,37 +747,8 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
750747
async notifyRoutesInFlightAboutRemovedHandler(handler: network.RouteHandler): Promise<void> {
751748
await Promise.all([...this._routesInFlight].map(route => route.removeHandler(handler)));
752749
}
753-
754-
routeAPIRequestsFromHar(options: { harBackend: HarBackend, urlMatch: URLMatch | undefined, notFound: 'abort' | 'fallback', baseURL: string | undefined }): { dispose: () => void } {
755-
const registration: HarForAPIRequestsRegistration = {
756-
harBackend: options.harBackend,
757-
urlMatch: options.urlMatch,
758-
notFound: options.notFound,
759-
baseURL: options.baseURL,
760-
};
761-
// Give priority to the newest registration, mirroring BrowserContext.route/Page.route.
762-
this._harForAPIRequests.unshift(registration);
763-
return {
764-
dispose: () => {
765-
const index = this._harForAPIRequests.indexOf(registration);
766-
if (index !== -1)
767-
this._harForAPIRequests.splice(index, 1);
768-
},
769-
};
770-
}
771-
772-
harForAPIRequests(): readonly HarForAPIRequestsRegistration[] {
773-
return this._harForAPIRequests;
774-
}
775750
}
776751

777-
export type HarForAPIRequestsRegistration = {
778-
harBackend: HarBackend;
779-
urlMatch: URLMatch | undefined;
780-
notFound: 'abort' | 'fallback';
781-
baseURL: string | undefined;
782-
};
783-
784752
export function validateBrowserContextOptions(options: types.BrowserContextOptions, browserOptions: BrowserOptions) {
785753
if (options.noDefaultViewport && options.deviceScaleFactor !== undefined)
786754
throw new Error(`"deviceScaleFactor" option is not supported with null "viewport"`);

‎packages/playwright-core/src/server/channels.d.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,6 @@ export interface BrowserContextChannel extends BrowserContextEventTarget, Channe
12741274
setGeolocation(params: BrowserContextSetGeolocationParams, progress: Progress): Promise<BrowserContextSetGeolocationResult>;
12751275
setHTTPCredentials(params: BrowserContextSetHTTPCredentialsParams, progress: Progress): Promise<BrowserContextSetHTTPCredentialsResult>;
12761276
setNetworkInterceptionPatterns(params: BrowserContextSetNetworkInterceptionPatternsParams, progress: Progress): Promise<BrowserContextSetNetworkInterceptionPatternsResult>;
1277-
routeAPIRequestsFromHar(params: BrowserContextRouteAPIRequestsFromHarParams, progress: Progress): Promise<BrowserContextRouteAPIRequestsFromHarResult>;
1278-
unrouteAPIRequestsFromHar(params: BrowserContextUnrouteAPIRequestsFromHarParams, progress: Progress): Promise<BrowserContextUnrouteAPIRequestsFromHarResult>;
12791277
setWebSocketInterceptionPatterns(params: BrowserContextSetWebSocketInterceptionPatternsParams, progress: Progress): Promise<BrowserContextSetWebSocketInterceptionPatternsResult>;
12801278
setOffline(params: BrowserContextSetOfflineParams, progress: Progress): Promise<BrowserContextSetOfflineResult>;
12811279
storageState(params: BrowserContextStorageStateParams, progress: Progress): Promise<BrowserContextStorageStateResult>;
@@ -1510,28 +1508,6 @@ export type BrowserContextSetNetworkInterceptionPatternsOptions = {
15101508

15111509
};
15121510
export type BrowserContextSetNetworkInterceptionPatternsResult = void;
1513-
export type BrowserContextRouteAPIRequestsFromHarParams = {
1514-
harId: string,
1515-
urlGlob?: string,
1516-
urlRegexSource?: string,
1517-
urlRegexFlags?: string,
1518-
notFound: 'abort' | 'fallback',
1519-
};
1520-
export type BrowserContextRouteAPIRequestsFromHarOptions = {
1521-
urlGlob?: string,
1522-
urlRegexSource?: string,
1523-
urlRegexFlags?: string,
1524-
};
1525-
export type BrowserContextRouteAPIRequestsFromHarResult = {
1526-
registrationId: string,
1527-
};
1528-
export type BrowserContextUnrouteAPIRequestsFromHarParams = {
1529-
registrationId: string,
1530-
};
1531-
export type BrowserContextUnrouteAPIRequestsFromHarOptions = {
1532-
1533-
};
1534-
export type BrowserContextUnrouteAPIRequestsFromHarResult = void;
15351511
export type BrowserContextSetWebSocketInterceptionPatternsParams = {
15361512
patterns: {
15371513
glob?: string,

‎packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts‎

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ import type { Request, Response, RouteHandler } from '../network';
4646
import type { InitScript, Page, PageError } from '../page';
4747
import type { Disposable } from '../disposable';
4848
import type { DispatcherScope } from './dispatcher';
49-
import type { LocalUtilsDispatcher } from './localUtilsDispatcher';
5049
import type * as channels from '../channels';
5150
import type { Progress } from '../progress';
5251
import type { URLMatch } from '@isomorphic/urlMatch';
5352

54-
type HarForAPIRequestsDisposable = Disposable & { registrationId: string };
55-
5653
export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextChannel, DispatcherScope> implements channels.BrowserContextChannel {
5754
_type_BrowserContext = true;
5855
private _context: BrowserContext;
@@ -338,38 +335,6 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
338335
this._routeWebSocketInitScript = await WebSocketRouteDispatcher.install(progress, this.connection, this._context);
339336
}
340337

341-
async routeAPIRequestsFromHar(params: channels.BrowserContextRouteAPIRequestsFromHarParams, progress: Progress): Promise<channels.BrowserContextRouteAPIRequestsFromHarResult> {
342-
// Reuse the HarBackend that was already opened via localUtils.harOpen for the page-side
343-
// route, rather than opening a second backend for the same HAR file. The backend is owned
344-
// by LocalUtils and closed via harClose, so this registration must not dispose it.
345-
const harBackend = this.connection.getDispatcher<LocalUtilsDispatcher>('LocalUtils')?.harBackendForId(params.harId);
346-
if (!harBackend)
347-
throw new Error('Internal error: har was not opened');
348-
const urlMatch: URLMatch | undefined =
349-
params.urlRegexSource !== undefined && params.urlRegexFlags !== undefined ? new RegExp(params.urlRegexSource, params.urlRegexFlags) :
350-
params.urlGlob !== undefined ? params.urlGlob : undefined;
351-
const registrationId = createGuid();
352-
const registration = this._context.routeAPIRequestsFromHar({
353-
harBackend,
354-
urlMatch,
355-
notFound: params.notFound,
356-
baseURL: this._context._options.baseURL,
357-
});
358-
this._disposables.push({
359-
registrationId,
360-
dispose: async () => registration.dispose(),
361-
} as HarForAPIRequestsDisposable);
362-
return { registrationId };
363-
}
364-
365-
async unrouteAPIRequestsFromHar(params: channels.BrowserContextUnrouteAPIRequestsFromHarParams, progress: Progress): Promise<void> {
366-
const index = this._disposables.findIndex(d => (d as HarForAPIRequestsDisposable).registrationId === params.registrationId);
367-
if (index === -1)
368-
return;
369-
const [disposable] = this._disposables.splice(index, 1);
370-
await progress.race(disposable.dispose());
371-
}
372-
373338
async storageState(params: channels.BrowserContextStorageStateParams, progress: Progress): Promise<channels.BrowserContextStorageStateResult> {
374339
return await this._context.storageState(progress, params.indexedDB, params.credentials);
375340
}

‎packages/playwright-core/src/server/dispatchers/dispatcher.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,6 @@ export class DispatcherConnection {
266266
return this._dispatcherByObject.get(object) as DispatcherType | undefined;
267267
}
268268

269-
getDispatcher<DispatcherType>(type: string): DispatcherType | undefined {
270-
for (const dispatcher of this._dispatcherByGuid.values()) {
271-
if (dispatcher._type === type)
272-
return dispatcher as DispatcherType;
273-
}
274-
return undefined;
275-
}
276-
277269
registerDispatcher(dispatcher: DispatcherScope) {
278270
assert(!this._dispatcherByGuid.has(dispatcher._guid));
279271
this._dispatcherByGuid.set(dispatcher._guid, dispatcher);

0 commit comments

Comments
 (0)