Skip to content

Commit db4e856

Browse files
authored
feat(rpc): use SerializedValue for CDPSession (#3076)
This is our way to define a schema for arbitrary values.
1 parent 1553f19 commit db4e856

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/rpc/channels.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,21 +1571,15 @@ export interface CDPSessionChannel extends Channel {
15711571
}
15721572
export type CDPSessionEventEvent = {
15731573
method: string,
1574-
params?: {
1575-
1576-
},
1574+
params?: SerializedValue,
15771575
};
15781576
export type CDPSessionDisconnectedEvent = {};
15791577
export type CDPSessionSendParams = {
15801578
method: string,
1581-
params?: {
1582-
1583-
},
1579+
params?: SerializedValue,
15841580
};
15851581
export type CDPSessionSendResult = {
1586-
result: {
1587-
1588-
},
1582+
result: SerializedValue,
15891583
};
15901584
export type CDPSessionDetachParams = {};
15911585
export type CDPSessionDetachResult = void;

src/rpc/client/cdpSession.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
1818
import { ChannelOwner } from './channelOwner';
1919
import { Protocol } from '../../chromium/protocol';
20+
import { parseResult, serializeArgument } from './jsHandle';
2021

2122
export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitializer> {
2223
static from(cdpSession: CDPSessionChannel): CDPSession {
@@ -32,7 +33,10 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
3233
constructor(parent: ChannelOwner, type: string, guid: string, initializer: CDPSessionInitializer) {
3334
super(parent, type, guid, initializer, true);
3435

35-
this._channel.on('event', ({ method, params }) => this.emit(method, params));
36+
this._channel.on('event', ({ method, params }) => {
37+
const cdpParams = params ? parseResult(params) : undefined;
38+
this.emit(method, cdpParams);
39+
});
3640
this._channel.on('disconnected', () => this._dispose());
3741

3842
this.on = super.on;
@@ -47,8 +51,9 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
4751
params?: Protocol.CommandParameters[T]
4852
): Promise<Protocol.CommandReturnValues[T]> {
4953
return this._wrapApiCall('cdpSession.send', async () => {
50-
const result = await this._channel.send({ method, params });
51-
return result.result as Protocol.CommandReturnValues[T];
54+
const protocolParams = params ? serializeArgument(params).value : undefined;
55+
const result = await this._channel.send({ method, params: protocolParams });
56+
return parseResult(result.result) as Protocol.CommandReturnValues[T];
5257
});
5358
}
5459

src/rpc/protocol.pdl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,16 +1407,16 @@ interface CDPSession
14071407
event event
14081408
parameters
14091409
method: string
1410-
params?: object
1410+
params?: SerializedValue
14111411

14121412
event disconnected
14131413

14141414
command send
14151415
parameters
14161416
method: string
1417-
params?: object
1417+
params?: SerializedValue
14181418
returns
1419-
result: object
1419+
result: SerializedValue
14201420

14211421
command detach
14221422

src/rpc/server/cdpSessionDispatcher.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@
1515
*/
1616

1717
import { CRSession, CRSessionEvents } from '../../chromium/crConnection';
18-
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
18+
import { CDPSessionChannel, CDPSessionInitializer, SerializedValue } from '../channels';
1919
import { Dispatcher, DispatcherScope } from './dispatcher';
20+
import { serializeResult, parseArgument } from './jsHandleDispatcher';
2021

2122
export class CDPSessionDispatcher extends Dispatcher<CRSession, CDPSessionInitializer> implements CDPSessionChannel {
2223
constructor(scope: DispatcherScope, crSession: CRSession) {
2324
super(scope, crSession, 'cdpSession', {}, true);
24-
crSession._eventListener = (method, params) => this._dispatchEvent('event', { method, params });
25+
crSession._eventListener = (method, cdpParams) => {
26+
const params = cdpParams ? serializeResult(cdpParams) : undefined;
27+
this._dispatchEvent('event', { method, params });
28+
};
2529
crSession.on(CRSessionEvents.Disconnected, () => {
2630
this._dispatchEvent('disconnected');
2731
this._dispose();
2832
});
2933
}
3034

31-
async send(params: { method: string, params?: Object }): Promise<{ result: Object }> {
32-
return { result: await this._object.send(params.method as any, params.params) };
35+
async send(params: { method: string, params?: SerializedValue }): Promise<{ result: SerializedValue }> {
36+
const cdpParams = params.params ? parseArgument({ value: params.params, handles: [] }) : undefined;
37+
return { result: serializeResult(await this._object.send(params.method as any, cdpParams)) };
3338
}
3439

3540
async detach(): Promise<void> {

0 commit comments

Comments
 (0)