feat(rpc): switch Env to use an array, split ignoreDefaultArgs (#2984)

This commit is contained in:
Dmitry Gozman 2020-07-17 09:32:27 -07:00 committed by GitHub
parent df8b27069e
commit 5cf3e4f0f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 13 deletions

View File

@ -37,6 +37,24 @@ export type BrowserContextOptions = {
colorScheme?: types.ColorScheme, colorScheme?: types.ColorScheme,
acceptDownloads?: boolean, acceptDownloads?: boolean,
}; };
type LaunchOptionsBase = {
executablePath?: string,
args?: string[],
ignoreAllDefaultArgs?: boolean,
ignoreDefaultArgs?: string[],
handleSIGINT?: boolean,
handleSIGTERM?: boolean,
handleSIGHUP?: boolean,
timeout?: number,
env?: types.EnvArray,
headless?: boolean,
devtools?: boolean,
proxy?: types.ProxySettings,
downloadsPath?: string,
};
export type LaunchOptions = LaunchOptionsBase & { slowMo?: number };
export type LaunchServerOptions = LaunchOptionsBase & { port?: number };
export type LaunchPersistentContextOptions = { userDataDir: string } & LaunchOptions & BrowserContextOptions;
export interface Channel extends EventEmitter { export interface Channel extends EventEmitter {
} }
@ -60,11 +78,10 @@ export interface SelectorsChannel extends Channel {
export type SelectorsInitializer = {}; export type SelectorsInitializer = {};
export type LaunchPersistentContextOptions = { userDataDir: string } & types.LaunchOptions & BrowserContextOptions;
export interface BrowserTypeChannel extends Channel { export interface BrowserTypeChannel extends Channel {
connect(params: types.ConnectOptions): Promise<{ browser: BrowserChannel }>; connect(params: types.ConnectOptions): Promise<{ browser: BrowserChannel }>;
launch(params: types.LaunchOptions): Promise<{ browser: BrowserChannel }>; launch(params: LaunchOptions): Promise<{ browser: BrowserChannel }>;
launchServer(params: types.LaunchServerOptions): Promise<{ server: BrowserServerChannel }>; launchServer(params: LaunchServerOptions): Promise<{ server: BrowserServerChannel }>;
launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }>; launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }>;
} }
export type BrowserTypeInitializer = { export type BrowserTypeInitializer = {

View File

@ -15,13 +15,13 @@
*/ */
import * as types from '../../types'; import * as types from '../../types';
import { BrowserTypeChannel, BrowserTypeInitializer, LaunchPersistentContextOptions } from '../channels'; import { BrowserTypeChannel, BrowserTypeInitializer, LaunchPersistentContextOptions, LaunchOptions, LaunchServerOptions } from '../channels';
import { Browser } from './browser'; import { Browser } from './browser';
import { BrowserContext } from './browserContext'; import { BrowserContext } from './browserContext';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
import { BrowserServer } from './browserServer'; import { BrowserServer } from './browserServer';
import { LoggerSink } from '../../loggerSink'; import { LoggerSink } from '../../loggerSink';
import { headersObjectToArray } from '../serializers'; import { headersObjectToArray, envObjectToArray } from '../serializers';
export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeInitializer> { export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeInitializer> {
@ -45,7 +45,13 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
const logger = options.logger; const logger = options.logger;
options = { ...options, logger: undefined }; options = { ...options, logger: undefined };
return this._wrapApiCall('browserType.launch', async () => { return this._wrapApiCall('browserType.launch', async () => {
const browser = Browser.from((await this._channel.launch(options)).browser); const launchOptions: LaunchOptions = {
...options,
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
env: options.env ? envObjectToArray(options.env) : undefined,
};
const browser = Browser.from((await this._channel.launch(launchOptions)).browser);
browser._logger = logger; browser._logger = logger;
return browser; return browser;
}, logger); }, logger);
@ -55,7 +61,13 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
const logger = options.logger; const logger = options.logger;
options = { ...options, logger: undefined }; options = { ...options, logger: undefined };
return this._wrapApiCall('browserType.launchServer', async () => { return this._wrapApiCall('browserType.launchServer', async () => {
return BrowserServer.from((await this._channel.launchServer(options)).server); const launchServerOptions: LaunchServerOptions = {
...options,
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
env: options.env ? envObjectToArray(options.env) : undefined,
};
return BrowserServer.from((await this._channel.launchServer(launchServerOptions)).server);
}, logger); }, logger);
} }
@ -65,6 +77,9 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
return this._wrapApiCall('browserType.launchPersistentContext', async () => { return this._wrapApiCall('browserType.launchPersistentContext', async () => {
const persistentOptions: LaunchPersistentContextOptions = { const persistentOptions: LaunchPersistentContextOptions = {
...options, ...options,
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
env: options.env ? envObjectToArray(options.env) : undefined,
extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,
userDataDir, userDataDir,
}; };

View File

@ -126,3 +126,19 @@ export function headersArrayToObject(headers: types.HeadersArray): types.Headers
result[name] = value; result[name] = value;
return result; return result;
} }
export function envObjectToArray(env: types.Env): types.EnvArray {
const result: types.EnvArray = [];
for (const name in env) {
if (!Object.is(env[name], undefined))
result.push({ name, value: String(env[name]) });
}
return result;
}
export function envArrayToObject(env: types.EnvArray): types.Env {
const result: types.Env = {};
for (const { name, value } of env)
result[name] = value;
return result;
}

View File

@ -18,12 +18,12 @@ import { BrowserBase } from '../../browser';
import { BrowserTypeBase, BrowserType } from '../../server/browserType'; import { BrowserTypeBase, BrowserType } from '../../server/browserType';
import * as types from '../../types'; import * as types from '../../types';
import { BrowserDispatcher } from './browserDispatcher'; import { BrowserDispatcher } from './browserDispatcher';
import { BrowserChannel, BrowserTypeChannel, BrowserContextChannel, BrowserTypeInitializer, BrowserServerChannel, LaunchPersistentContextOptions } from '../channels'; import { BrowserChannel, BrowserTypeChannel, BrowserContextChannel, BrowserTypeInitializer, BrowserServerChannel, LaunchPersistentContextOptions, LaunchOptions, LaunchServerOptions } from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher'; import { Dispatcher, DispatcherScope } from './dispatcher';
import { BrowserContextBase } from '../../browserContext'; import { BrowserContextBase } from '../../browserContext';
import { BrowserContextDispatcher } from './browserContextDispatcher'; import { BrowserContextDispatcher } from './browserContextDispatcher';
import { BrowserServerDispatcher } from './browserServerDispatcher'; import { BrowserServerDispatcher } from './browserServerDispatcher';
import { headersArrayToObject } from '../serializers'; import { headersArrayToObject, envArrayToObject } from '../serializers';
export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeInitializer> implements BrowserTypeChannel { export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeInitializer> implements BrowserTypeChannel {
constructor(scope: DispatcherScope, browserType: BrowserTypeBase) { constructor(scope: DispatcherScope, browserType: BrowserTypeBase) {
@ -33,22 +33,34 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeIn
}, true, browserType.name()); }, true, browserType.name());
} }
async launch(params: types.LaunchOptions): Promise<{ browser: BrowserChannel }> { async launch(params: LaunchOptions): Promise<{ browser: BrowserChannel }> {
const browser = await this._object.launch(params); const options = {
...params,
ignoreDefaultArgs: params.ignoreAllDefaultArgs ? true : params.ignoreDefaultArgs,
env: params.env ? envArrayToObject(params.env) : undefined,
};
const browser = await this._object.launch(options);
return { browser: new BrowserDispatcher(this._scope, browser as BrowserBase) }; return { browser: new BrowserDispatcher(this._scope, browser as BrowserBase) };
} }
async launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }> { async launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }> {
const options = { const options = {
...params, ...params,
ignoreDefaultArgs: params.ignoreAllDefaultArgs ? true : params.ignoreDefaultArgs,
env: params.env ? envArrayToObject(params.env) : undefined,
extraHTTPHeaders: params.extraHTTPHeaders ? headersArrayToObject(params.extraHTTPHeaders) : undefined, extraHTTPHeaders: params.extraHTTPHeaders ? headersArrayToObject(params.extraHTTPHeaders) : undefined,
}; };
const browserContext = await this._object.launchPersistentContext(params.userDataDir, options); const browserContext = await this._object.launchPersistentContext(params.userDataDir, options);
return { context: new BrowserContextDispatcher(this._scope, browserContext as BrowserContextBase) }; return { context: new BrowserContextDispatcher(this._scope, browserContext as BrowserContextBase) };
} }
async launchServer(params: types.LaunchServerOptions): Promise<{ server: BrowserServerChannel }> { async launchServer(params: LaunchServerOptions): Promise<{ server: BrowserServerChannel }> {
return { server: new BrowserServerDispatcher(this._scope, await this._object.launchServer(params)) }; const options = {
...params,
ignoreDefaultArgs: params.ignoreAllDefaultArgs ? true : params.ignoreDefaultArgs,
env: params.env ? envArrayToObject(params.env) : undefined,
};
return { server: new BrowserServerDispatcher(this._scope, await this._object.launchServer(options)) };
} }
async connect(params: types.ConnectOptions): Promise<{ browser: BrowserChannel }> { async connect(params: types.ConnectOptions): Promise<{ browser: BrowserChannel }> {

View File

@ -280,6 +280,7 @@ export type BrowserContextOptions = {
}; };
export type Env = {[key: string]: string | number | boolean | undefined}; export type Env = {[key: string]: string | number | boolean | undefined};
export type EnvArray = { name: string, value: string }[];
export type LaunchOptionsBase = { export type LaunchOptionsBase = {
executablePath?: string, executablePath?: string,