chore: make LocalUtils easily available on the client (#14717)

Instead of plumbing it around, expose it through Connection.
This commit is contained in:
Dmitry Gozman 2022-06-08 13:22:05 -07:00 committed by GitHub
parent 21d570f62e
commit 176ab7e48b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 21 deletions

View File

@ -24,7 +24,6 @@ import { isSafeCloseError, kBrowserClosedError } from '../common/errors';
import type * as api from '../../types/types'; import type * as api from '../../types/types';
import { CDPSession } from './cdpSession'; import { CDPSession } from './cdpSession';
import type { BrowserType } from './browserType'; import type { BrowserType } from './browserType';
import type { LocalUtils } from './localUtils';
export class Browser extends ChannelOwner<channels.BrowserChannel> implements api.Browser { export class Browser extends ChannelOwner<channels.BrowserChannel> implements api.Browser {
readonly _contexts = new Set<BrowserContext>(); readonly _contexts = new Set<BrowserContext>();
@ -33,7 +32,6 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
_shouldCloseConnectionOnClose = false; _shouldCloseConnectionOnClose = false;
private _browserType!: BrowserType; private _browserType!: BrowserType;
readonly _name: string; readonly _name: string;
_localUtils!: LocalUtils;
static from(browser: channels.BrowserChannel): Browser { static from(browser: channels.BrowserChannel): Browser {
return (browser as any)._object; return (browser as any)._object;
@ -68,7 +66,6 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
this._contexts.add(context); this._contexts.add(context);
context._logger = options.logger || this._logger; context._logger = options.logger || this._logger;
context._setBrowserType(this._browserType); context._setBrowserType(this._browserType);
context.tracing._localUtils = this._localUtils;
await this._browserType._onDidCreateContext?.(context); await this._browserType._onDidCreateContext?.(context);
return context; return context;
} }

View File

@ -80,7 +80,6 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
const browser = Browser.from((await this._channel.launch(launchOptions)).browser); const browser = Browser.from((await this._channel.launch(launchOptions)).browser);
browser._logger = logger; browser._logger = logger;
browser._setBrowserType(this); browser._setBrowserType(this);
browser._localUtils = this._playwright._utils;
return browser; return browser;
} }
@ -109,7 +108,6 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
context._options = contextParams; context._options = contextParams;
context._logger = logger; context._logger = logger;
context._setBrowserType(this); context._setBrowserType(this);
context.tracing._localUtils = this._playwright._utils;
await this._onDidCreateContext?.(context); await this._onDidCreateContext?.(context);
return context; return context;
} }
@ -134,7 +132,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
connectParams.socksProxyRedirectPortForTest = (params as any).__testHookRedirectPortForwarding; connectParams.socksProxyRedirectPortForTest = (params as any).__testHookRedirectPortForwarding;
const { pipe } = await this._channel.connect(connectParams); const { pipe } = await this._channel.connect(connectParams);
const closePipe = () => pipe.close().catch(() => {}); const closePipe = () => pipe.close().catch(() => {});
const connection = new Connection(); const connection = new Connection(this._connection.localUtils());
connection.markAsRemote(); connection.markAsRemote();
connection.on('close', closePipe); connection.on('close', closePipe);
@ -176,7 +174,6 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
browser._logger = logger; browser._logger = logger;
browser._shouldCloseConnectionOnClose = true; browser._shouldCloseConnectionOnClose = true;
browser._setBrowserType(this); browser._setBrowserType(this);
browser._localUtils = this._playwright._utils;
browser.on(Events.Browser.Disconnected, closePipe); browser.on(Events.Browser.Disconnected, closePipe);
return browser; return browser;
}, deadline ? deadline - monotonicTime() : 0); }, deadline ? deadline - monotonicTime() : 0);
@ -214,7 +211,6 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
browser._contexts.add(BrowserContext.from(result.defaultContext)); browser._contexts.add(BrowserContext.from(result.defaultContext));
browser._logger = params.logger; browser._logger = params.logger;
browser._setBrowserType(this); browser._setBrowserType(this);
browser._localUtils = this._playwright._utils;
return browser; return browser;
} }
} }

View File

@ -67,12 +67,14 @@ export class Connection extends EventEmitter {
private _rootObject: Root; private _rootObject: Root;
private _closedErrorMessage: string | undefined; private _closedErrorMessage: string | undefined;
private _isRemote = false; private _isRemote = false;
private _localUtils?: LocalUtils;
// Some connections allow resolving in-process dispatchers. // Some connections allow resolving in-process dispatchers.
toImpl: ((client: ChannelOwner) => any) | undefined; toImpl: ((client: ChannelOwner) => any) | undefined;
constructor() { constructor(localUtils?: LocalUtils) {
super(); super();
this._rootObject = new Root(this); this._rootObject = new Root(this);
this._localUtils = localUtils;
} }
markAsRemote() { markAsRemote() {
@ -83,6 +85,10 @@ export class Connection extends EventEmitter {
return this._isRemote; return this._isRemote;
} }
localUtils(): LocalUtils {
return this._localUtils!;
}
async initializePlaywright(): Promise<Playwright> { async initializePlaywright(): Promise<Playwright> {
return await this._rootObject.initialize(); return await this._rootObject.initialize();
} }
@ -238,6 +244,8 @@ export class Connection extends EventEmitter {
break; break;
case 'LocalUtils': case 'LocalUtils':
result = new LocalUtils(parent, type, guid, initializer); result = new LocalUtils(parent, type, guid, initializer);
if (!this._localUtils)
this._localUtils = result as LocalUtils;
break; break;
case 'Page': case 'Page':
result = new Page(parent, type, guid, initializer); result = new Page(parent, type, guid, initializer);

View File

@ -73,7 +73,6 @@ export class APIRequest implements api.APIRequest {
extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,
storageState, storageState,
})).request); })).request);
context._tracing._localUtils = this._playwright._utils;
this._contexts.add(context); this._contexts.add(context);
context._request = this; context._request = this;
await this._onDidCreateContext?.(context); await this._onDidCreateContext?.(context);

View File

@ -18,10 +18,6 @@ import type * as channels from '../protocol/channels';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
export class LocalUtils extends ChannelOwner<channels.LocalUtilsChannel> { export class LocalUtils extends ChannelOwner<channels.LocalUtilsChannel> {
static from(channel: channels.LocalUtilsChannel): LocalUtils {
return (channel as any)._object;
}
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.LocalUtilsInitializer) { constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.LocalUtilsInitializer) {
super(parent, type, guid, initializer); super(parent, type, guid, initializer);
} }

View File

@ -22,7 +22,6 @@ import { BrowserType } from './browserType';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
import { Electron } from './electron'; import { Electron } from './electron';
import { APIRequest } from './fetch'; import { APIRequest } from './fetch';
import { LocalUtils } from './localUtils';
import { Selectors, SelectorsOwner } from './selectors'; import { Selectors, SelectorsOwner } from './selectors';
import type { Size } from './types'; import type { Size } from './types';
@ -46,7 +45,6 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
selectors: Selectors; selectors: Selectors;
readonly request: APIRequest; readonly request: APIRequest;
readonly errors: { TimeoutError: typeof TimeoutError }; readonly errors: { TimeoutError: typeof TimeoutError };
_utils: LocalUtils;
private _socksProxyHandler: socks.SocksProxyHandler | undefined; private _socksProxyHandler: socks.SocksProxyHandler | undefined;
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) { constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) {
@ -65,7 +63,6 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
this.devices[name] = descriptor; this.devices[name] = descriptor;
this.selectors = new Selectors(); this.selectors = new Selectors();
this.errors = { TimeoutError }; this.errors = { TimeoutError };
this._utils = LocalUtils.from(initializer.utils);
const selectorsOwner = SelectorsOwner.from(initializer.selectors); const selectorsOwner = SelectorsOwner.from(initializer.selectors);
this.selectors._addChannel(selectorsOwner); this.selectors._addChannel(selectorsOwner);

View File

@ -18,11 +18,8 @@ import type * as api from '../../types/types';
import type * as channels from '../protocol/channels'; import type * as channels from '../protocol/channels';
import { Artifact } from './artifact'; import { Artifact } from './artifact';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
import type { LocalUtils } from './localUtils';
export class Tracing extends ChannelOwner<channels.TracingChannel> implements api.Tracing { export class Tracing extends ChannelOwner<channels.TracingChannel> implements api.Tracing {
_localUtils!: LocalUtils;
static from(channel: channels.TracingChannel): Tracing { static from(channel: channels.TracingChannel): Tracing {
return (channel as any)._object; return (channel as any)._object;
} }
@ -81,6 +78,6 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
// Add local sources to the remote trace if necessary. // Add local sources to the remote trace if necessary.
if (result.sourceEntries?.length) if (result.sourceEntries?.length)
await this._localUtils.zip(filePath, result.sourceEntries); await this._connection.localUtils().zip(filePath, result.sourceEntries);
} }
} }