55package transport
66
77import (
8+ "encoding/json"
89 "fmt"
910 "net"
1011 "net/url"
@@ -126,15 +127,33 @@ func (e *UpstreamTransport) Close() error {
126127 return nil
127128}
128129
129- func (e * UpstreamTransport ) Send (command string , params map [string ]any , sessionID string , timeout ... time.Duration ) (map [string ]any , error ) {
130+ func (e * UpstreamTransport ) Send (command any , params map [string ]any , sessionID string , timeout ... time.Duration ) (map [string ]any , error ) {
131+ method , ok := command .(string )
132+ if ! ok {
133+ message , ok := command .(types.CdpCommandMessage )
134+ if ! ok {
135+ return nil , fmt .Errorf ("command must be a CDP method name or CdpCommandMessage" )
136+ }
137+ payload := map [string ]any {
138+ "id" : message .ID ,
139+ "method" : message .Method ,
140+ }
141+ if message .Params != nil {
142+ payload ["params" ] = message .Params
143+ }
144+ if message .SessionID != "" {
145+ payload ["sessionId" ] = message .SessionID
146+ }
147+ return map [string ]any {}, e .writeCommand (payload )
148+ }
130149 e .pendingMu .Lock ()
131150 e .nextID ++
132151 id := e .nextID
133152 done := make (chan map [string ]any , 1 )
134153 e .pending [id ] = done
135154 e .pendingMu .Unlock ()
136155
137- message := map [string ]any {"id" : id , "method" : command , "params" : params }
156+ message := map [string ]any {"id" : id , "method" : method , "params" : params }
138157 if sessionID != "" {
139158 message ["sessionId" ] = sessionID
140159 }
@@ -151,7 +170,7 @@ func (e *UpstreamTransport) Send(command string, params map[string]any, sessionI
151170 if effectiveTimeout <= 0 {
152171 response := <- done
153172 if errObj , ok := response ["error" ].(map [string ]any ); ok {
154- return nil , fmt .Errorf ("%s failed: %v" , command , errObj ["message" ])
173+ return nil , fmt .Errorf ("%s failed: %v" , method , errObj ["message" ])
155174 }
156175 if result , ok := response ["result" ].(map [string ]any ); ok {
157176 return result , nil
@@ -163,10 +182,10 @@ func (e *UpstreamTransport) Send(command string, params map[string]any, sessionI
163182 e .pendingMu .Lock ()
164183 delete (e .pending , id )
165184 e .pendingMu .Unlock ()
166- return nil , fmt .Errorf ("%s timed out after %s" , command , effectiveTimeout )
185+ return nil , fmt .Errorf ("%s timed out after %s" , method , effectiveTimeout )
167186 case response := <- done :
168187 if errObj , ok := response ["error" ].(map [string ]any ); ok {
169- return nil , fmt .Errorf ("%s failed: %v" , command , errObj ["message" ])
188+ return nil , fmt .Errorf ("%s failed: %v" , method , errObj ["message" ])
170189 }
171190 if result , ok := response ["result" ].(map [string ]any ); ok {
172191 return result , nil
@@ -332,6 +351,50 @@ func (e *UpstreamTransport) EmitRecv(message map[string]any) {
332351 e .emitRecv (message )
333352}
334353
354+ func (e * UpstreamTransport ) parseAndEmitRecv (data []byte ) error {
355+ var message map [string ]any
356+ if err := json .Unmarshal (data , & message ); err != nil {
357+ return fmt .Errorf ("invalid CDP message: %w" , err )
358+ }
359+ if _ , ok := commandID (message ["id" ]); ok {
360+ if errObj , hasError := message ["error" ]; hasError && errObj != nil {
361+ errorMap , ok := errObj .(map [string ]any )
362+ if ! ok {
363+ return fmt .Errorf ("invalid CDP response error" )
364+ }
365+ if message , ok := errorMap ["message" ].(string ); ! ok || message == "" {
366+ return fmt .Errorf ("invalid CDP response error message" )
367+ }
368+ }
369+ if sessionID , ok := message ["sessionId" ]; ok && sessionID != nil {
370+ if _ , ok := sessionID .(string ); ! ok {
371+ return fmt .Errorf ("invalid CDP response sessionId" )
372+ }
373+ }
374+ e .emitRecv (message )
375+ return nil
376+ }
377+ if _ , hasID := message ["id" ]; hasID {
378+ return fmt .Errorf ("invalid CDP response id" )
379+ }
380+ method , _ := message ["method" ].(string )
381+ if method == "" {
382+ return fmt .Errorf ("invalid CDP event method" )
383+ }
384+ if params , ok := message ["params" ]; ok && params != nil {
385+ if _ , ok := params .(map [string ]any ); ! ok {
386+ return fmt .Errorf ("invalid CDP event params" )
387+ }
388+ }
389+ if sessionID , ok := message ["sessionId" ]; ok && sessionID != nil {
390+ if _ , ok := sessionID .(string ); ! ok {
391+ return fmt .Errorf ("invalid CDP event sessionId" )
392+ }
393+ }
394+ e .emitRecv (message )
395+ return nil
396+ }
397+
335398func (e * UpstreamTransport ) emitUpstreamEvent (method string , payload map [string ]any , targetID string , sessionID string ) {
336399 e .listenerMu .Lock ()
337400 listeners := append ([]upstreamEventListener (nil ), e .eventListeners [method ]... )
0 commit comments