Skip to content

Commit 5dcd625

Browse files
committed
Align Python and Go supported ModCDP surface
1 parent 9a77e34 commit 5dcd625

41 files changed

Lines changed: 578 additions & 1394 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

go/examples/demo/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// --live Use the running Google Chrome enabled via chrome://inspect.
55
// --direct *.* -> direct_cdp on the client.
66
// --loopback *.* -> service_worker on client; *.* -> loopback_cdp on server. Default.
7-
// --debugger *.* -> service_worker on client; *.* -> chromedebugger on server.
87
// --upstream ws. Defaults to ws.
98

109
package main
@@ -91,8 +90,6 @@ func serverRoutesFor(mode, upstreamMode string) map[string]string {
9190
serverRoute := "auto"
9291
if mode == "loopback" {
9392
serverRoute = "loopback_cdp"
94-
} else if mode == "debugger" {
95-
serverRoute = "chromedebugger"
9693
}
9794
routes := map[string]string{
9895
"Mod.*": "service_worker",
@@ -170,9 +167,7 @@ func parseArgs(argv []string) (string, string, bool, error) {
170167
}
171168
live := flags["live"]
172169
mode := "loopback"
173-
if flags["debugger"] {
174-
mode = "debugger"
175-
} else if flags["direct"] {
170+
if flags["direct"] {
176171
mode = "direct"
177172
} else if flags["loopback"] {
178173
mode = "loopback"

go/modcdp/client/CDPTypes.go

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ type CDPCommandSchema struct {
2626
}
2727

2828
type CDPTypes struct {
29-
CustomCommands map[string]CustomCommand
30-
CustomEvents map[string]CustomEvent
31-
CustomMiddlewares []CustomMiddleware
32-
commandSchemas map[string]CDPCommandSchema
33-
nativeCommands map[string]bool
34-
eventSchemas map[string]map[string]any
35-
mu sync.RWMutex
29+
CustomCommands map[string]CustomCommand
30+
CustomEvents map[string]CustomEvent
31+
CustomMiddlewares []CustomMiddleware
32+
commandSchemas map[string]CDPCommandSchema
33+
commandParamsSchemas map[string]map[string]any
34+
commandResultSchemas map[string]map[string]any
35+
nativeCommands map[string]bool
36+
eventSchemas map[string]map[string]any
37+
mu sync.RWMutex
3638
}
3739

3840
var jsonSchemaObject = map[string]any{"type": "object"}
@@ -109,15 +111,8 @@ var modConfigureParamsSchema = map[string]any{
109111
"upstream": map[string]any{
110112
"type": "object",
111113
"properties": map[string]any{
112-
"upstream_mode": map[string]any{"enum": []any{"ws", "pipe", "nativemessaging", "reversews", "nats", "chromedebugger"}},
113-
"upstream_ws_cdp_url": map[string]any{"type": "string"},
114-
"upstream_nats_url": map[string]any{"type": "string"},
115-
"upstream_nats_subject_prefix": map[string]any{"type": "string"},
116-
"upstream_nats_role": map[string]any{"enum": []any{"client", "browser"}},
117-
"upstream_nats_wait_timeout_ms": map[string]any{"type": "number"},
118-
"upstream_reversews_bind": map[string]any{"type": "string"},
119-
"upstream_reversews_wait_timeout_ms": map[string]any{"type": "number"},
120-
"upstream_nativemessaging_host_name": map[string]any{"type": "string"},
114+
"upstream_mode": map[string]any{"enum": []any{"ws"}},
115+
"upstream_ws_cdp_url": map[string]any{"type": "string"},
121116
"upstream_ws_connect_error_settle_timeout_ms": map[string]any{"type": "number"},
122117
"upstream_cdp_send_timeout_ms": map[string]any{"type": "number"},
123118
},
@@ -142,18 +137,9 @@ var modConfigureParamsSchema = map[string]any{
142137
},
143138
"additionalProperties": false,
144139
},
145-
"downstream": map[string]any{
146-
"type": "object",
147-
"properties": map[string]any{
148-
"downstream_client_timeout_ms": map[string]any{"type": "number"},
149-
"downstream_close_browser_on_disconnect": map[string]any{"type": "boolean"},
150-
},
151-
"additionalProperties": false,
152-
},
153-
"server_browser_token": map[string]any{"type": "string"},
154-
"custom_commands": map[string]any{"type": "array", "items": modCommandRegistrationSchema},
155-
"custom_events": map[string]any{"type": "array", "items": modEventRegistrationSchema},
156-
"custom_middlewares": map[string]any{"type": "array", "items": modMiddlewareRegistrationSchema},
140+
"custom_commands": map[string]any{"type": "array", "items": modCommandRegistrationSchema},
141+
"custom_events": map[string]any{"type": "array", "items": modEventRegistrationSchema},
142+
"custom_middlewares": map[string]any{"type": "array", "items": modMiddlewareRegistrationSchema},
157143
},
158144
"additionalProperties": false,
159145
}
@@ -334,17 +320,25 @@ var defaultBuiltinEvents = []CustomEvent{
334320

335321
func NewCDPTypes(customCommands []CustomCommand, customEvents []CustomEvent, customMiddlewares []CustomMiddleware) *CDPTypes {
336322
types := &CDPTypes{
337-
CustomCommands: map[string]CustomCommand{},
338-
CustomEvents: map[string]CustomEvent{},
339-
CustomMiddlewares: []CustomMiddleware{},
340-
commandSchemas: map[string]CDPCommandSchema{},
341-
nativeCommands: map[string]bool{},
342-
eventSchemas: map[string]map[string]any{},
323+
CustomCommands: map[string]CustomCommand{},
324+
CustomEvents: map[string]CustomEvent{},
325+
CustomMiddlewares: []CustomMiddleware{},
326+
commandSchemas: map[string]CDPCommandSchema{},
327+
commandParamsSchemas: map[string]map[string]any{},
328+
commandResultSchemas: map[string]map[string]any{},
329+
nativeCommands: map[string]bool{},
330+
eventSchemas: map[string]map[string]any{},
343331
}
344332
types.hydrateNativeProtocolSchemas()
345333
types.mu.Lock()
346-
for method := range types.commandSchemas {
334+
for method, schema := range types.commandSchemas {
347335
types.nativeCommands[method] = true
336+
if schema.Params != nil {
337+
types.commandParamsSchemas[method] = schema.Params
338+
}
339+
if schema.Result != nil {
340+
types.commandResultSchemas[method] = schema.Result
341+
}
348342
}
349343
types.mu.Unlock()
350344
for _, command := range defaultBuiltinCommands {
@@ -412,8 +406,8 @@ func (types *CDPTypes) ToJSON() map[string]any {
412406
"custom_commands": len(types.CustomCommands),
413407
"custom_events": len(types.CustomEvents),
414408
"custom_middlewares": len(types.CustomMiddlewares),
415-
"command_params_schemas": len(types.commandSchemas),
416-
"command_result_schemas": len(types.commandSchemas),
409+
"command_params_schemas": len(types.commandParamsSchemas),
410+
"command_result_schemas": len(types.commandResultSchemas),
417411
"event_schemas": len(types.eventSchemas),
418412
}
419413
types.mu.RUnlock()
@@ -497,31 +491,32 @@ func (types *CDPTypes) NativeCommandSchema(method string) map[string]any {
497491
if !types.nativeCommands[method] {
498492
return nil
499493
}
500-
schema, ok := types.commandSchemas[method]
501-
if !ok {
494+
params := types.commandParamsSchemas[method]
495+
result := types.commandResultSchemas[method]
496+
if params == nil && result == nil {
502497
return nil
503498
}
504-
return map[string]any{"params": schema.Params, "result": schema.Result}
499+
return map[string]any{"params": params, "result": result}
505500
}
506501

507502
func (types *CDPTypes) CommandParamsSchema(method string) (map[string]any, bool) {
508503
types.mu.RLock()
509504
defer types.mu.RUnlock()
510-
schema, ok := types.commandSchemas[method]
511-
if !ok || schema.Params == nil {
505+
schema, ok := types.commandParamsSchemas[method]
506+
if !ok || schema == nil {
512507
return nil, false
513508
}
514-
return schema.Params, true
509+
return schema, true
515510
}
516511

517512
func (types *CDPTypes) CommandResultSchema(method string) (map[string]any, bool) {
518513
types.mu.RLock()
519514
defer types.mu.RUnlock()
520-
schema, ok := types.commandSchemas[method]
521-
if !ok || schema.Result == nil {
515+
schema, ok := types.commandResultSchemas[method]
516+
if !ok || schema == nil {
522517
return nil, false
523518
}
524-
return schema.Result, true
519+
return schema, true
525520
}
526521

527522
func (types *CDPTypes) EventPayloadSchema(event string) (map[string]any, bool) {
@@ -570,18 +565,16 @@ func (types *CDPTypes) AddCustomCommand(command CustomCommand) (string, bool, er
570565
}
571566
types.mu.Lock()
572567
defer types.mu.Unlock()
573-
existing := types.commandSchemas[name]
574568
if command.ParamsSchema != nil {
575569
if schema := cloneSchema(command.ParamsSchema); schema != nil {
576-
existing.Params = schema
570+
types.commandParamsSchemas[name] = schema
577571
}
578572
}
579573
if command.ResultSchema != nil {
580574
if schema := cloneSchema(command.ResultSchema); schema != nil {
581-
existing.Result = schema
575+
types.commandResultSchemas[name] = schema
582576
}
583577
}
584-
types.commandSchemas[name] = existing
585578
command.Name = name
586579
if command.ParamsSchema != nil {
587580
command.ParamsSchema = cloneSchema(command.ParamsSchema)

go/modcdp/client/CDPTypes_payload_schema_normalization_test.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ func TestCDPTypesSerializesBuiltinModCommandSchemasThroughTheSameWirePath(t *tes
6464

6565
parsedConfig, err := types.ParseCommandParams("Mod.configure", map[string]any{
6666
"client_config": map[string]any{"client_hydrate_aliases": false},
67-
"downstream": map[string]any{
68-
"downstream_client_timeout_ms": 1234,
69-
"downstream_close_browser_on_disconnect": true,
70-
},
67+
"upstream": map[string]any{"upstream_mode": "ws", "upstream_ws_cdp_url": "ws://127.0.0.1:9222/devtools/browser/test"},
7168
})
7269
if err != nil {
7370
t.Fatal(err)
@@ -76,19 +73,19 @@ func TestCDPTypesSerializesBuiltinModCommandSchemasThroughTheSameWirePath(t *tes
7673
if !ok || clientConfig["client_hydrate_aliases"] != false {
7774
t.Fatalf("client_config = %#v", parsedConfig["client_config"])
7875
}
79-
downstream, ok := parsedConfig["downstream"].(map[string]any)
76+
upstream, ok := parsedConfig["upstream"].(map[string]any)
8077
if !ok {
81-
t.Fatalf("downstream = %#v", parsedConfig["downstream"])
78+
t.Fatalf("upstream = %#v", parsedConfig["upstream"])
8279
}
83-
if downstream["downstream_client_timeout_ms"] != 1234 || downstream["downstream_close_browser_on_disconnect"] != true {
84-
t.Fatalf("downstream = %#v", downstream)
80+
if upstream["upstream_mode"] != "ws" || upstream["upstream_ws_cdp_url"] != "ws://127.0.0.1:9222/devtools/browser/test" {
81+
t.Fatalf("upstream = %#v", upstream)
8582
}
8683
_, err = types.ParseCommandParams("Mod.configure", map[string]any{
87-
"downstream": map[string]any{
88-
"closeBrowser": "not allowed over the wire",
84+
"upstream": map[string]any{
85+
"upstream_mode": "nats",
8986
},
9087
})
91-
if err == nil || !strings.Contains(err.Error(), "closeBrowser") {
92-
t.Fatalf("expected closeBrowser to be rejected, got %v", err)
88+
if err == nil || !strings.Contains(err.Error(), "enum") {
89+
t.Fatalf("expected unsupported upstream mode to be rejected, got %v", err)
9390
}
9491
}

0 commit comments

Comments
 (0)