From ef76f5b922655da7b7eada0ac2ebfa9c5305e41f Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 10 Aug 2020 11:20:32 -0700 Subject: [PATCH] feat(rpc): introduce JSON type in the protocol for arbitrary blobs (#3367) --- src/rpc/channels.ts | 16 ++++++++-------- src/rpc/client/browserType.ts | 3 --- src/rpc/client/cdpSession.ts | 9 +++------ src/rpc/protocol.yml | 10 +++++----- src/rpc/server/browserTypeDispatcher.ts | 3 --- src/rpc/server/cdpSessionDispatcher.ts | 11 ++++------- src/rpc/validator.ts | 8 ++++---- src/rpc/validatorPrimitives.ts | 3 +++ utils/generate_channels.js | 4 +++- 9 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/rpc/channels.ts b/src/rpc/channels.ts index a872fc22ea0ab..232a813cd69c1 100644 --- a/src/rpc/channels.ts +++ b/src/rpc/channels.ts @@ -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 = {}; diff --git a/src/rpc/client/browserType.ts b/src/rpc/client/browserType.ts index 185816331eb09..c040c55907c75 100644 --- a/src/rpc/client/browserType.ts +++ b/src/rpc/client/browserType.ts @@ -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 { static from(cdpSession: CDPSessionChannel): CDPSession { @@ -34,8 +33,7 @@ export class CDPSession extends ChannelOwner { - 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 { 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]; }); } diff --git a/src/rpc/protocol.yml b/src/rpc/protocol.yml index bada84e12e8d3..9a862f3881586 100644 --- a/src/rpc/protocol.yml +++ b/src/rpc/protocol.yml @@ -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? diff --git a/src/rpc/server/browserTypeDispatcher.ts b/src/rpc/server/browserTypeDispatcher.ts index 87ee56c7f9de5..affdc082e1811 100644 --- a/src/rpc/server/browserTypeDispatcher.ts +++ b/src/rpc/server/browserTypeDispatcher.ts @@ -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 implements BrowserTypeChannel { constructor(scope: DispatcherScope, browserType: BrowserTypeBase) { @@ -39,7 +38,6 @@ export class BrowserTypeDispatcher extends Dispatcher 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 { diff --git a/src/rpc/validator.ts b/src/rpc/validator.ts index 9f46fa68aac3a..5a2a0d63c67c4 100644 --- a/src/rpc/validator.ts +++ b/src/rpc/validator.ts @@ -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({ diff --git a/src/rpc/validatorPrimitives.ts b/src/rpc/validatorPrimitives.ts index 818d29cbc7362..215cb6e976950 100644 --- a/src/rpc/validatorPrimitives.ts +++ b/src/rpc/validatorPrimitives.ts @@ -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)) diff --git a/utils/generate_channels.js b/utils/generate_channels.js index d70dacad95a00..e9556df489e0e 100755 --- a/utils/generate_channels.js +++ b/utils/generate_channels.js @@ -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 };