From 5cf3e4f0f2259063a7a0eaf2774454531f67d360 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Fri, 17 Jul 2020 09:32:27 -0700 Subject: [PATCH] feat(rpc): switch Env to use an array, split ignoreDefaultArgs (#2984) --- src/rpc/channels.ts | 23 ++++++++++++++++++++--- src/rpc/client/browserType.ts | 23 +++++++++++++++++++---- src/rpc/serializers.ts | 16 ++++++++++++++++ src/rpc/server/browserTypeDispatcher.ts | 24 ++++++++++++++++++------ src/types.ts | 1 + 5 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/rpc/channels.ts b/src/rpc/channels.ts index cdf9cbfd6e..ede604e07a 100644 --- a/src/rpc/channels.ts +++ b/src/rpc/channels.ts @@ -37,6 +37,24 @@ export type BrowserContextOptions = { colorScheme?: types.ColorScheme, 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 { } @@ -60,11 +78,10 @@ export interface SelectorsChannel extends Channel { export type SelectorsInitializer = {}; -export type LaunchPersistentContextOptions = { userDataDir: string } & types.LaunchOptions & BrowserContextOptions; export interface BrowserTypeChannel extends Channel { connect(params: types.ConnectOptions): Promise<{ browser: BrowserChannel }>; - launch(params: types.LaunchOptions): Promise<{ browser: BrowserChannel }>; - launchServer(params: types.LaunchServerOptions): Promise<{ server: BrowserServerChannel }>; + launch(params: LaunchOptions): Promise<{ browser: BrowserChannel }>; + launchServer(params: LaunchServerOptions): Promise<{ server: BrowserServerChannel }>; launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }>; } export type BrowserTypeInitializer = { diff --git a/src/rpc/client/browserType.ts b/src/rpc/client/browserType.ts index 0f403b199e..d43a36a976 100644 --- a/src/rpc/client/browserType.ts +++ b/src/rpc/client/browserType.ts @@ -15,13 +15,13 @@ */ import * as types from '../../types'; -import { BrowserTypeChannel, BrowserTypeInitializer, LaunchPersistentContextOptions } from '../channels'; +import { BrowserTypeChannel, BrowserTypeInitializer, LaunchPersistentContextOptions, LaunchOptions, LaunchServerOptions } from '../channels'; import { Browser } from './browser'; import { BrowserContext } from './browserContext'; import { ChannelOwner } from './channelOwner'; import { BrowserServer } from './browserServer'; import { LoggerSink } from '../../loggerSink'; -import { headersObjectToArray } from '../serializers'; +import { headersObjectToArray, envObjectToArray } from '../serializers'; export class BrowserType extends ChannelOwner { @@ -45,7 +45,13 @@ export class BrowserType extends ChannelOwner { - 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; return browser; }, logger); @@ -55,7 +61,13 @@ export class BrowserType extends ChannelOwner { - 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); } @@ -65,6 +77,9 @@ export class BrowserType extends ChannelOwner { const persistentOptions: LaunchPersistentContextOptions = { ...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, userDataDir, }; diff --git a/src/rpc/serializers.ts b/src/rpc/serializers.ts index eb74fa1bbf..df153f651a 100644 --- a/src/rpc/serializers.ts +++ b/src/rpc/serializers.ts @@ -126,3 +126,19 @@ export function headersArrayToObject(headers: types.HeadersArray): types.Headers result[name] = value; 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; +} diff --git a/src/rpc/server/browserTypeDispatcher.ts b/src/rpc/server/browserTypeDispatcher.ts index 78839448c1..ba781ea0fa 100644 --- a/src/rpc/server/browserTypeDispatcher.ts +++ b/src/rpc/server/browserTypeDispatcher.ts @@ -18,12 +18,12 @@ import { BrowserBase } from '../../browser'; import { BrowserTypeBase, BrowserType } from '../../server/browserType'; import * as types from '../../types'; 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 { BrowserContextBase } from '../../browserContext'; import { BrowserContextDispatcher } from './browserContextDispatcher'; import { BrowserServerDispatcher } from './browserServerDispatcher'; -import { headersArrayToObject } from '../serializers'; +import { headersArrayToObject, envArrayToObject } from '../serializers'; export class BrowserTypeDispatcher extends Dispatcher implements BrowserTypeChannel { constructor(scope: DispatcherScope, browserType: BrowserTypeBase) { @@ -33,22 +33,34 @@ export class BrowserTypeDispatcher extends Dispatcher { - const browser = await this._object.launch(params); + async launch(params: LaunchOptions): Promise<{ browser: BrowserChannel }> { + 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) }; } async launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }> { const options = { ...params, + ignoreDefaultArgs: params.ignoreAllDefaultArgs ? true : params.ignoreDefaultArgs, + env: params.env ? envArrayToObject(params.env) : undefined, extraHTTPHeaders: params.extraHTTPHeaders ? headersArrayToObject(params.extraHTTPHeaders) : undefined, }; const browserContext = await this._object.launchPersistentContext(params.userDataDir, options); return { context: new BrowserContextDispatcher(this._scope, browserContext as BrowserContextBase) }; } - async launchServer(params: types.LaunchServerOptions): Promise<{ server: BrowserServerChannel }> { - return { server: new BrowserServerDispatcher(this._scope, await this._object.launchServer(params)) }; + async launchServer(params: LaunchServerOptions): Promise<{ server: BrowserServerChannel }> { + 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 }> { diff --git a/src/types.ts b/src/types.ts index e53dcfe121..7086250c1e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -280,6 +280,7 @@ export type BrowserContextOptions = { }; export type Env = {[key: string]: string | number | boolean | undefined}; +export type EnvArray = { name: string, value: string }[]; export type LaunchOptionsBase = { executablePath?: string,