@@ -26,13 +26,15 @@ type CDPCommandSchema struct {
2626}
2727
2828type 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
3840var 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
335321func 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
507502func (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
517512func (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
527522func (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 )
0 commit comments