feat(rpc): move leftover extraHTTPHeaders to HeadersArray (#2980)

This commit is contained in:
Dmitry Gozman 2020-07-16 13:36:22 -07:00 committed by GitHub
parent 439e048a4c
commit b890569afc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 13 deletions

View file

@ -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<void>;
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<void>;

View file

@ -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<BrowserChannel, BrowserInitializer> {
readonly _contexts = new Set<BrowserContext>();
@ -53,7 +54,11 @@ export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
async newContext(options: types.BrowserContextOptions & { logger?: LoggerSink } = {}): Promise<BrowserContext> {
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;

View file

@ -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<BrowserTypeChannel, BrowserTypeInitializer> {
@ -62,7 +63,12 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
const logger = options.logger;
options = { ...options, logger: undefined };
return this._wrapApiCall('browserType.launchPersistentContext', async () => {
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;

View file

@ -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<Browser, BrowserInitializer> implements BrowserChannel {
constructor(scope: DispatcherScope, browser: BrowserBase) {
@ -34,8 +34,12 @@ export class BrowserDispatcher extends Dispatcher<Browser, BrowserInitializer> 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<void> {

View file

@ -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<BrowserType, BrowserTypeInitializer> implements BrowserTypeChannel {
constructor(scope: DispatcherScope, browserType: BrowserTypeBase) {
@ -37,8 +38,12 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeIn
return { browser: new BrowserDispatcher(this._scope, browser as BrowserBase) };
}
async launchPersistentContext(params: { userDataDir: string } & types.LaunchOptions & types.BrowserContextOptions): Promise<{ context: BrowserContextChannel }> {
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) };
}