Skip to content

Commit fc0d246

Browse files
szuendDevtools-frontend LUCI CQ
authored andcommitted
[protocol] Add more detailed CDPMessage types
This CL adds some basic message types for commands, responses, events and errors. The official source for these is https://chromium.googlesource.com/deps/inspector_protocol/. This CL was created mostly by using existing types and reverse- engineering the C++ code. [email protected] Bug: 453469270 Change-Id: I5749ca31090f0069419fe455cbd976801008a2fe Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7079116 Reviewed-by: Alex Rudenko <[email protected]> Commit-Queue: Simon Zünd <[email protected]>
1 parent 4ae3441 commit fc0d246

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

front_end/core/protocol_client/CDPConnection.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,55 @@
44

55
import type {ProtocolMapping} from '../../generated/protocol-mapping.js';
66

7+
export type CommandParams<T extends keyof ProtocolMapping.Commands> = ProtocolMapping.Commands[T]['paramsType'][0];
8+
export type CommandResult<T extends keyof ProtocolMapping.Commands> = ProtocolMapping.Commands[T]['returnType'];
9+
export type EventParams<T extends keyof ProtocolMapping.Events> = ProtocolMapping.Events[T];
10+
11+
export interface CDPBaseMessage {
12+
sessionId?: string;
13+
}
14+
15+
export interface CDPCommandRequest<T extends keyof ProtocolMapping.Commands> extends CDPBaseMessage {
16+
id: number;
17+
method: T;
18+
params: CommandParams<T>;
19+
}
20+
21+
export interface CDPCommandResponse<T extends keyof ProtocolMapping.Commands> extends CDPBaseMessage {
22+
id: number;
23+
result: CommandResult<T>;
24+
}
25+
26+
export interface CDPEvent<T extends keyof ProtocolMapping.Events> extends CDPBaseMessage {
27+
method: T;
28+
params: EventParams<T>;
29+
}
30+
31+
/**
32+
* Keep this in sync with https://source.chromium.org/chromium/chromium/src/+/main:third_party/inspector_protocol/crdtp/dispatch.h.
33+
*/
34+
export const enum CDPErrorStatus {
35+
PARSE_ERROR = -32700,
36+
INVALID_REQUEST = -32600,
37+
METHOD_NOT_FOUND = -32601,
38+
INVALID_PARAMS = -32602,
39+
INTERNAL_ERROR = -32603,
40+
SERVER_ERROR = -32000,
41+
SESSION_NOT_FOUND = SERVER_ERROR - 1,
42+
}
43+
44+
export interface CDPError extends CDPBaseMessage {
45+
id?: number;
46+
error: {
47+
code: CDPErrorStatus,
48+
message: string,
49+
data?: string,
50+
};
51+
}
52+
53+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
54+
export type CDPMessage = CDPCommandRequest<any>|CDPCommandResponse<any>|CDPEvent<any>|CDPError;
55+
756
/**
857
* Allows the sending and receiving of CDP commands and the notification of CDP events to observers.
958
*
@@ -12,9 +61,8 @@ import type {ProtocolMapping} from '../../generated/protocol-mapping.js';
1261
* would conflict with any other shared traffic.
1362
*/
1463
export interface CDPConnection {
15-
send<T extends keyof ProtocolMapping.Commands>(
16-
method: T, params: ProtocolMapping.Commands[T]['paramsType'][0],
17-
sessionId: string|undefined): Promise<ProtocolMapping.Commands[T]['returnType']|{getError(): string}>;
64+
send<T extends keyof ProtocolMapping.Commands>(method: T, params: CommandParams<T>, sessionId: string|undefined):
65+
Promise<CommandResult<T>|{getError(): string}>;
1866

1967
observe(observer: CDPConnectionObserver): void;
2068
unobserve(observer: CDPConnectionObserver): void;
@@ -34,6 +82,6 @@ export interface DebuggableCDPConnection extends CDPConnection {
3482
}
3583

3684
export interface RawMessageObserver {
37-
onMessageReceived(message: unknown): void;
38-
onMessageSent(message: unknown): void;
85+
onMessageReceived(message: CDPMessage): void;
86+
onMessageSent(message: CDPMessage): void;
3987
}

front_end/core/protocol_client/protocol_client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import * as CDPConnection from './CDPConnection.js';
56
import * as ConnectionTransport from './ConnectionTransport.js';
67
import * as InspectorBackend from './InspectorBackend.js';
78
import * as NodeURL from './NodeURL.js';
89

910
export {
11+
CDPConnection,
1012
ConnectionTransport,
1113
InspectorBackend,
1214
NodeURL,

front_end/testing/MockConnection.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {cleanTestDOM} from './DOMHooks.js';
1212
import {deinitializeGlobalVars, initializeGlobalVars} from './EnvironmentHelpers.js';
1313
import {setMockResourceTree} from './ResourceTreeHelpers.js';
1414

15-
export type ProtocolCommand = keyof ProtocolMapping.Commands;
16-
export type ProtocolCommandParams<C extends ProtocolCommand> = ProtocolMapping.Commands[C]['paramsType'];
17-
export type ProtocolResponse<C extends ProtocolCommand> = ProtocolMapping.Commands[C]['returnType'];
18-
export type ProtocolCommandHandler<C extends ProtocolCommand> = (...params: ProtocolCommandParams<C>) =>
19-
Omit<ProtocolResponse<C>, 'getError'>|{getError(): string}|
20-
PromiseLike<Omit<ProtocolResponse<C>, 'getError'>|{getError(): string}>;
15+
type ProtocolCommand = keyof ProtocolMapping.Commands;
16+
type CommandParams<C extends keyof ProtocolMapping.Commands> = ProtocolClient.CDPConnection.CommandParams<C>;
17+
type CommandResult<C extends keyof ProtocolMapping.Commands> = ProtocolClient.CDPConnection.CommandResult<C>;
18+
19+
export type ProtocolCommandHandler<C extends keyof ProtocolMapping.Commands> = (param: CommandParams<C>) =>
20+
Omit<CommandResult<C>, 'getError'>|{getError(): string}|
21+
PromiseLike<Omit<CommandResult<C>, 'getError'>|{getError(): string}>;
2122
export type MessageCallback = (result: string|Object) => void;
2223
interface Message {
2324
id: number;

0 commit comments

Comments
 (0)