Skip to content

Commit

Permalink
feat(rpc): introduce JSON type in the protocol for arbitrary blobs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Aug 10, 2020
1 parent 6f3f608 commit ef76f5b
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 37 deletions.
16 changes: 8 additions & 8 deletions src/rpc/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export type BrowserTypeLaunchParams = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
slowMo?: number,
};
Expand All @@ -206,7 +206,7 @@ export type BrowserTypeLaunchOptions = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
slowMo?: number,
};
Expand Down Expand Up @@ -235,7 +235,7 @@ export type BrowserTypeLaunchServerParams = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
port?: number,
};
Expand All @@ -261,7 +261,7 @@ export type BrowserTypeLaunchServerOptions = {
password?: string,
},
downloadsPath?: string,
firefoxUserPrefs?: SerializedValue,
firefoxUserPrefs?: any,
chromiumSandbox?: boolean,
port?: number,
};
Expand Down Expand Up @@ -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 = {};
Expand Down
3 changes: 0 additions & 3 deletions src/rpc/client/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/rpc/client/cdpSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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];
});
}

Expand Down
10 changes: 5 additions & 5 deletions src/rpc/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ BrowserType:
username: string?
password: string?
downloadsPath: string?
firefoxUserPrefs: SerializedValue?
firefoxUserPrefs: json?
chromiumSandbox: boolean?
slowMo: number?
returns:
Expand Down Expand Up @@ -252,7 +252,7 @@ BrowserType:
username: string?
password: string?
downloadsPath: string?
firefoxUserPrefs: SerializedValue?
firefoxUserPrefs: json?
chromiumSandbox: boolean?
port: number?
returns:
Expand Down Expand Up @@ -1849,9 +1849,9 @@ CDPSession:
send:
parameters:
method: string
params: SerializedValue?
params: json?
returns:
result: SerializedValue
result: json

detach:

Expand All @@ -1860,7 +1860,7 @@ CDPSession:
event:
parameters:
method: string
params: SerializedValue?
params: json?



Expand Down
3 changes: 0 additions & 3 deletions src/rpc/server/browserTypeDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) };
Expand All @@ -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)) };
Expand Down
11 changes: 4 additions & 7 deletions src/rpc/server/cdpSessionDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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),
});
Expand All @@ -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),
});
Expand Down Expand Up @@ -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({
Expand Down
3 changes: 3 additions & 0 deletions src/rpc/validatorPrimitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion utils/generate_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 };
Expand Down

0 comments on commit ef76f5b

Please sign in to comment.