feat(rpc): introduce JSON type in the protocol for arbitrary blobs (#3367)

This commit is contained in:
Dmitry Gozman 2020-08-10 11:20:32 -07:00 committed by GitHub
parent 6f3f608d5b
commit ef76f5b922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 30 additions and 37 deletions

View File

@ -180,7 +180,7 @@ export type BrowserTypeLaunchParams = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
slowMo?: number,
};
@ -206,7 +206,7 @@ export type BrowserTypeLaunchOptions = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
slowMo?: number,
};
@ -235,7 +235,7 @@ export type BrowserTypeLaunchServerParams = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
port?: number,
};
@ -261,7 +261,7 @@ export type BrowserTypeLaunchServerOptions = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
port?: number,
};
@ -2228,17 +2228,17 @@ export interface CDPSessionChannel extends Channel {
}
export type CDPSessionEventEvent = {
method: string,
params?: SerializedValue,
params?: any,
};
export type CDPSessionSendParams = {
method: string,
params?: SerializedValue,
params?: any,
};
export type CDPSessionSendOptions = {
params?: SerializedValue,
params?: any,
};
export type CDPSessionSendResult = {
result: SerializedValue,
result: any,
};
export type CDPSessionDetachParams = {};
export type CDPSessionDetachOptions = {};

View File

@ -20,7 +20,6 @@ import { BrowserContext } from './browserContext';
import { ChannelOwner } from './channelOwner';
import { BrowserServer } from './browserServer';
import { headersObjectToArray, envObjectToArray } from '../../converters';
import { serializeArgument } from './jsHandle';
import { assert } from '../../helper';
import { LaunchOptions, LaunchServerOptions, ConnectOptions, LaunchPersistentContextOptions } from './types';
@ -53,7 +52,6 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
env: options.env ? envObjectToArray(options.env) : undefined,
firefoxUserPrefs: options.firefoxUserPrefs ? serializeArgument(options.firefoxUserPrefs).value : undefined,
};
const browser = Browser.from((await this._channel.launch(launchOptions)).browser);
browser._logger = logger;
@ -70,7 +68,6 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
env: options.env ? envObjectToArray(options.env) : undefined,
firefoxUserPrefs: options.firefoxUserPrefs ? serializeArgument(options.firefoxUserPrefs).value : undefined,
};
return BrowserServer.from((await this._channel.launchServer(launchServerOptions)).server);
}, logger);

View File

@ -17,7 +17,6 @@
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
import { ChannelOwner } from './channelOwner';
import { Protocol } from '../../chromium/protocol';
import { parseResult, serializeArgument } from './jsHandle';
export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitializer> {
static from(cdpSession: CDPSessionChannel): CDPSession {
@ -34,8 +33,7 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
super(parent, type, guid, initializer);
this._channel.on('event', ({ method, params }) => {
const cdpParams = params ? parseResult(params) : undefined;
this.emit(method, cdpParams);
this.emit(method, params);
});
this.on = super.on;
@ -50,9 +48,8 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
params?: Protocol.CommandParameters[T]
): Promise<Protocol.CommandReturnValues[T]> {
return this._wrapApiCall('cdpSession.send', async () => {
const protocolParams = params ? serializeArgument(params).value : undefined;
const result = await this._channel.send({ method, params: protocolParams });
return parseResult(result.result) as Protocol.CommandReturnValues[T];
const result = await this._channel.send({ method, params });
return result.result as Protocol.CommandReturnValues[T];
});
}

View File

@ -215,7 +215,7 @@ BrowserType:
username: string?
password: string?
downloadsPath: string?
firefoxUserPrefs: SerializedValue?
firefoxUserPrefs: json?
chromiumSandbox: boolean?
slowMo: number?
returns:
@ -252,7 +252,7 @@ BrowserType:
username: string?
password: string?
downloadsPath: string?
firefoxUserPrefs: SerializedValue?
firefoxUserPrefs: json?
chromiumSandbox: boolean?
port: number?
returns:
@ -1849,9 +1849,9 @@ CDPSession:
send:
parameters:
method: string
params: SerializedValue?
params: json?
returns:
result: SerializedValue
result: json
detach:
@ -1860,7 +1860,7 @@ CDPSession:
event:
parameters:
method: string
params: SerializedValue?
params: json?

View File

@ -24,7 +24,6 @@ import { BrowserContextBase } from '../../browserContext';
import { BrowserContextDispatcher } from './browserContextDispatcher';
import { BrowserServerDispatcher } from './browserServerDispatcher';
import { headersArrayToObject, envArrayToObject } from '../../converters';
import { parseValue } from './jsHandleDispatcher';
export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeInitializer> implements BrowserTypeChannel {
constructor(scope: DispatcherScope, browserType: BrowserTypeBase) {
@ -39,7 +38,6 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeIn
...params,
ignoreDefaultArgs: params.ignoreAllDefaultArgs ? true : params.ignoreDefaultArgs,
env: params.env ? envArrayToObject(params.env) : undefined,
firefoxUserPrefs: params.firefoxUserPrefs ? parseValue(params.firefoxUserPrefs) : undefined,
};
const browser = await this._object.launch(options);
return { browser: new BrowserDispatcher(this._scope, browser as BrowserBase) };
@ -61,7 +59,6 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeIn
const options = {
...params,
ignoreDefaultArgs: params.ignoreAllDefaultArgs ? true : params.ignoreDefaultArgs,
firefoxUserPrefs: params.firefoxUserPrefs ? parseValue(params.firefoxUserPrefs) : undefined,
env: params.env ? envArrayToObject(params.env) : undefined,
};
return { server: new BrowserServerDispatcher(this._scope, await this._object.launchServer(options)) };

View File

@ -15,23 +15,20 @@
*/
import { CRSession, CRSessionEvents } from '../../chromium/crConnection';
import { CDPSessionChannel, CDPSessionInitializer, SerializedValue } from '../channels';
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { serializeResult, parseValue } from './jsHandleDispatcher';
export class CDPSessionDispatcher extends Dispatcher<CRSession, CDPSessionInitializer> implements CDPSessionChannel {
constructor(scope: DispatcherScope, crSession: CRSession) {
super(scope, crSession, 'CDPSession', {}, true);
crSession._eventListener = (method, cdpParams) => {
const params = cdpParams ? serializeResult(cdpParams) : undefined;
crSession._eventListener = (method, params) => {
this._dispatchEvent('event', { method, params });
};
crSession.on(CRSessionEvents.Disconnected, () => this._dispose());
}
async send(params: { method: string, params?: SerializedValue }): Promise<{ result: SerializedValue }> {
const cdpParams = params.params ? parseValue(params.params) : undefined;
return { result: serializeResult(await this._object.send(params.method as any, cdpParams)) };
async send(params: { method: string, params?: any }): Promise<{ result: any }> {
return { result: await this._object.send(params.method as any, params.params) };
}
async detach(): Promise<void> {

View File

@ -16,7 +16,7 @@
// This file is generated by generate_channels.js, do not edit manually.
import { Validator, ValidationError, tOptional, tObject, tBoolean, tNumber, tString, tEnum, tArray, tBinary } from './validatorPrimitives';
import { Validator, ValidationError, tOptional, tObject, tBoolean, tNumber, tString, tAny, tEnum, tArray, tBinary } from './validatorPrimitives';
export { Validator, ValidationError } from './validatorPrimitives';
type Scheme = { [key: string]: Validator };
@ -127,7 +127,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
password: tOptional(tString),
})),
downloadsPath: tOptional(tString),
firefoxUserPrefs: tOptional(tType('SerializedValue')),
firefoxUserPrefs: tOptional(tAny),
chromiumSandbox: tOptional(tBoolean),
slowMo: tOptional(tNumber),
});
@ -153,7 +153,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
password: tOptional(tString),
})),
downloadsPath: tOptional(tString),
firefoxUserPrefs: tOptional(tType('SerializedValue')),
firefoxUserPrefs: tOptional(tAny),
chromiumSandbox: tOptional(tBoolean),
port: tOptional(tNumber),
});
@ -837,7 +837,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
});
scheme.CDPSessionSendParams = tObject({
method: tString,
params: tOptional(tType('SerializedValue')),
params: tOptional(tAny),
});
scheme.CDPSessionDetachParams = tOptional(tObject({}));
scheme.ElectronLaunchParams = tObject({

View File

@ -52,6 +52,9 @@ export const tUndefined: Validator = (arg: any, path: string) => {
return arg;
throw new ValidationError(`${path}: expected undefined, got ${typeof arg}`);
};
export const tAny: Validator = (arg: any, path: string) => {
return arg;
};
export const tOptional = (v: Validator): Validator => {
return (arg: any, path: string) => {
if (Object.is(arg, undefined))

View File

@ -36,6 +36,8 @@ function inlineType(type, indent, wrapEnums = false) {
type = type.substring(0, type.length - 1);
if (type === 'binary')
return { ts: 'Binary', scheme: 'tBinary', optional };
if (type === 'json')
return { ts: 'any', scheme: 'tAny', optional };
if (['string', 'boolean', 'number', 'undefined'].includes(type))
return { ts: type, scheme: `t${titleCase(type)}`, optional };
if (channels.has(type))
@ -137,7 +139,7 @@ const validator_ts = [
// This file is generated by ${path.basename(__filename)}, do not edit manually.
import { Validator, ValidationError, tOptional, tObject, tBoolean, tNumber, tString, tEnum, tArray, tBinary } from './validatorPrimitives';
import { Validator, ValidationError, tOptional, tObject, tBoolean, tNumber, tString, tAny, tEnum, tArray, tBinary } from './validatorPrimitives';
export { Validator, ValidationError } from './validatorPrimitives';
type Scheme = { [key: string]: Validator };