From b890569afc18fcbeecc7e4edc9d8722541d3a9c5 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 16 Jul 2020 13:36:22 -0700 Subject: [PATCH] feat(rpc): move leftover extraHTTPHeaders to HeadersArray (#2980) --- src/rpc/channels.ts | 24 ++++++++++++++++++++++-- src/rpc/client/browser.ts | 9 +++++++-- src/rpc/client/browserType.ts | 10 ++++++++-- src/rpc/server/browserDispatcher.ts | 12 ++++++++---- src/rpc/server/browserTypeDispatcher.ts | 11 ++++++++--- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/rpc/channels.ts b/src/rpc/channels.ts index f8d62f39df..cdf9cbfd6e 100644 --- a/src/rpc/channels.ts +++ b/src/rpc/channels.ts @@ -18,6 +18,25 @@ import { EventEmitter } from 'events'; import * as types from '../types'; export type Binary = string; +export type BrowserContextOptions = { + viewport?: types.Size | null, + ignoreHTTPSErrors?: boolean, + javaScriptEnabled?: boolean, + bypassCSP?: boolean, + userAgent?: string, + locale?: string, + timezoneId?: string, + geolocation?: types.Geolocation, + permissions?: string[], + extraHTTPHeaders?: types.HeadersArray, + offline?: boolean, + httpCredentials?: types.Credentials, + deviceScaleFactor?: number, + isMobile?: boolean, + hasTouch?: boolean, + colorScheme?: types.ColorScheme, + acceptDownloads?: boolean, +}; export interface Channel extends EventEmitter { } @@ -41,11 +60,12 @@ 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 }>; - launchPersistentContext(params: { userDataDir: string } & types.LaunchOptions & types.BrowserContextOptions): Promise<{ context: BrowserContextChannel }>; + launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }>; } export type BrowserTypeInitializer = { executablePath: string, @@ -69,7 +89,7 @@ export interface BrowserChannel extends Channel { on(event: 'close', callback: () => void): this; close(): Promise; - newContext(params: types.BrowserContextOptions): Promise<{ context: BrowserContextChannel }>; + newContext(params: BrowserContextOptions): Promise<{ context: BrowserContextChannel }>; crNewBrowserCDPSession(): Promise<{ session: CDPSessionChannel }>; crStartTracing(params: { page?: PageChannel, path?: string, screenshots?: boolean, categories?: string[] }): Promise; diff --git a/src/rpc/client/browser.ts b/src/rpc/client/browser.ts index 5dedf0634d..0c41df7f95 100644 --- a/src/rpc/client/browser.ts +++ b/src/rpc/client/browser.ts @@ -15,13 +15,14 @@ */ import * as types from '../../types'; -import { BrowserChannel, BrowserInitializer } from '../channels'; +import { BrowserChannel, BrowserInitializer, BrowserContextOptions } from '../channels'; import { BrowserContext } from './browserContext'; import { Page } from './page'; import { ChannelOwner } from './channelOwner'; import { Events } from '../../events'; import { LoggerSink } from '../../loggerSink'; import { BrowserType } from './browserType'; +import { headersObjectToArray } from '../serializers'; export class Browser extends ChannelOwner { readonly _contexts = new Set(); @@ -53,7 +54,11 @@ export class Browser extends ChannelOwner { async newContext(options: types.BrowserContextOptions & { logger?: LoggerSink } = {}): Promise { const logger = options.logger; options = { ...options, logger: undefined }; - const context = BrowserContext.from((await this._channel.newContext(options)).context); + const contextOptions: BrowserContextOptions = { + ...options, + extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, + }; + const context = BrowserContext.from((await this._channel.newContext(contextOptions)).context); this._contexts.add(context); context._logger = logger || this._logger; return context; diff --git a/src/rpc/client/browserType.ts b/src/rpc/client/browserType.ts index b4fe0db220..0f403b199e 100644 --- a/src/rpc/client/browserType.ts +++ b/src/rpc/client/browserType.ts @@ -15,12 +15,13 @@ */ import * as types from '../../types'; -import { BrowserTypeChannel, BrowserTypeInitializer } from '../channels'; +import { BrowserTypeChannel, BrowserTypeInitializer, LaunchPersistentContextOptions } 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'; export class BrowserType extends ChannelOwner { @@ -62,7 +63,12 @@ export class BrowserType extends ChannelOwner { - const result = await this._channel.launchPersistentContext({ userDataDir, ...options }); + const persistentOptions: LaunchPersistentContextOptions = { + ...options, + extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, + userDataDir, + }; + const result = await this._channel.launchPersistentContext(persistentOptions); const context = BrowserContext.from(result.context); context._logger = logger; return context; diff --git a/src/rpc/server/browserDispatcher.ts b/src/rpc/server/browserDispatcher.ts index 6729c67793..f8cfa1b3a0 100644 --- a/src/rpc/server/browserDispatcher.ts +++ b/src/rpc/server/browserDispatcher.ts @@ -17,13 +17,13 @@ import { Browser, BrowserBase } from '../../browser'; import { BrowserContextBase } from '../../browserContext'; import { Events } from '../../events'; -import * as types from '../../types'; -import { BrowserChannel, BrowserContextChannel, BrowserInitializer, CDPSessionChannel, Binary } from '../channels'; +import { BrowserChannel, BrowserContextChannel, BrowserInitializer, CDPSessionChannel, Binary, BrowserContextOptions } from '../channels'; import { BrowserContextDispatcher } from './browserContextDispatcher'; import { CDPSessionDispatcher } from './cdpSessionDispatcher'; import { Dispatcher, DispatcherScope } from './dispatcher'; import { CRBrowser } from '../../chromium/crBrowser'; import { PageDispatcher } from './pageDispatcher'; +import { headersArrayToObject } from '../serializers'; export class BrowserDispatcher extends Dispatcher implements BrowserChannel { constructor(scope: DispatcherScope, browser: BrowserBase) { @@ -34,8 +34,12 @@ export class BrowserDispatcher extends Dispatcher i }); } - async newContext(params: types.BrowserContextOptions): Promise<{ context: BrowserContextChannel }> { - return { context: new BrowserContextDispatcher(this._scope, await this._object.newContext(params) as BrowserContextBase) }; + async newContext(params: BrowserContextOptions): Promise<{ context: BrowserContextChannel }> { + const options = { + ...params, + extraHTTPHeaders: params.extraHTTPHeaders ? headersArrayToObject(params.extraHTTPHeaders) : undefined, + }; + return { context: new BrowserContextDispatcher(this._scope, await this._object.newContext(options) as BrowserContextBase) }; } async close(): Promise { diff --git a/src/rpc/server/browserTypeDispatcher.ts b/src/rpc/server/browserTypeDispatcher.ts index b9baca8e20..78839448c1 100644 --- a/src/rpc/server/browserTypeDispatcher.ts +++ b/src/rpc/server/browserTypeDispatcher.ts @@ -18,11 +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 } from '../channels'; +import { BrowserChannel, BrowserTypeChannel, BrowserContextChannel, BrowserTypeInitializer, BrowserServerChannel, LaunchPersistentContextOptions } from '../channels'; import { Dispatcher, DispatcherScope } from './dispatcher'; import { BrowserContextBase } from '../../browserContext'; import { BrowserContextDispatcher } from './browserContextDispatcher'; import { BrowserServerDispatcher } from './browserServerDispatcher'; +import { headersArrayToObject } from '../serializers'; export class BrowserTypeDispatcher extends Dispatcher implements BrowserTypeChannel { constructor(scope: DispatcherScope, browserType: BrowserTypeBase) { @@ -37,8 +38,12 @@ export class BrowserTypeDispatcher extends Dispatcher { - const browserContext = await this._object.launchPersistentContext(params.userDataDir, params); + async launchPersistentContext(params: LaunchPersistentContextOptions): Promise<{ context: BrowserContextChannel }> { + const options = { + ...params, + 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) }; }