Skip to content

Commit 3fada3c

Browse files
committed
Tighten translated runtime parity
1 parent eb2d218 commit 3fada3c

19 files changed

Lines changed: 228 additions & 190 deletions

go/modcdp/client/CDPTypes.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,12 @@ func (types *CDPTypes) ToJSON() map[string]any {
409409
}
410410
types.mu.RLock()
411411
state := map[string]any{
412-
"custom_commands": len(types.CustomCommands),
413-
"custom_events": len(types.CustomEvents),
414-
"custom_middlewares": len(types.CustomMiddlewares),
415-
"command_schemas": len(types.commandSchemas),
416-
"event_schemas": len(types.eventSchemas),
412+
"custom_commands": len(types.CustomCommands),
413+
"custom_events": len(types.CustomEvents),
414+
"custom_middlewares": len(types.CustomMiddlewares),
415+
"command_params_schemas": len(types.commandSchemas),
416+
"command_result_schemas": len(types.commandSchemas),
417+
"event_schemas": len(types.eventSchemas),
417418
}
418419
types.mu.RUnlock()
419420
return modtypes.ModCDPToJSON(types, modtypes.ModCDPJSONConfig{

go/modcdp/client/ModCDPClient.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,13 +816,42 @@ func (c *ModCDPClient) serverConfigureParams(customCommands []map[string]any, cu
816816
}
817817
params := map[string]any{}
818818
if c.Config.ServerConfig != nil {
819-
if c.Config.ServerConfig.Upstream.UpstreamWSCDPURL != "" {
820-
hasUpstreamConfig = true
821-
upstream["upstream_ws_cdp_url"] = c.Config.ServerConfig.Upstream.UpstreamWSCDPURL
819+
serverUpstream := c.Config.ServerConfig.Upstream
820+
if serverUpstream.UpstreamMode != "" {
821+
upstream["upstream_mode"] = serverUpstream.UpstreamMode
822+
}
823+
if serverUpstream.UpstreamWSCDPURL != "" {
824+
upstream["upstream_ws_cdp_url"] = serverUpstream.UpstreamWSCDPURL
825+
}
826+
if serverUpstream.UpstreamNatsURL != "" {
827+
upstream["upstream_nats_url"] = serverUpstream.UpstreamNatsURL
828+
}
829+
if serverUpstream.UpstreamNatsSubjectPrefix != "" {
830+
upstream["upstream_nats_subject_prefix"] = serverUpstream.UpstreamNatsSubjectPrefix
831+
}
832+
if serverUpstream.UpstreamNatsRole != "" {
833+
upstream["upstream_nats_role"] = serverUpstream.UpstreamNatsRole
834+
}
835+
if serverUpstream.UpstreamNatsWaitTimeoutMS != 0 {
836+
upstream["upstream_nats_wait_timeout_ms"] = serverUpstream.UpstreamNatsWaitTimeoutMS
837+
}
838+
if serverUpstream.UpstreamReverseWSBind != "" {
839+
upstream["upstream_reversews_bind"] = serverUpstream.UpstreamReverseWSBind
840+
}
841+
if serverUpstream.UpstreamReverseWSWaitTimeoutMS != 0 {
842+
upstream["upstream_reversews_wait_timeout_ms"] = serverUpstream.UpstreamReverseWSWaitTimeoutMS
843+
}
844+
if serverUpstream.UpstreamNativeMessagingHostName != "" {
845+
upstream["upstream_nativemessaging_host_name"] = serverUpstream.UpstreamNativeMessagingHostName
846+
}
847+
if serverUpstream.UpstreamWSConnectErrorSettleTimeoutMS != 0 {
848+
upstream["upstream_ws_connect_error_settle_timeout_ms"] = serverUpstream.UpstreamWSConnectErrorSettleTimeoutMS
849+
}
850+
if serverUpstream.UpstreamCDPSendTimeoutMS != 0 {
851+
upstream["upstream_cdp_send_timeout_ms"] = serverUpstream.UpstreamCDPSendTimeoutMS
822852
}
823-
if c.Config.ServerConfig.Upstream.UpstreamWSConnectErrorSettleTimeoutMS != 0 {
853+
if len(upstream) > 0 {
824854
hasUpstreamConfig = true
825-
upstream["upstream_ws_connect_error_settle_timeout_ms"] = c.Config.ServerConfig.Upstream.UpstreamWSConnectErrorSettleTimeoutMS
826855
}
827856
if c.Config.ServerConfig.Router.RouterRoutes != nil {
828857
router["router_routes"] = c.Config.ServerConfig.Router.RouterRoutes
@@ -1067,7 +1096,7 @@ func (c *ModCDPClient) sendCommand(method string, params map[string]any, cdpSess
10671096
var result any
10681097
if command.Target == "direct_cdp" {
10691098
step := command.Steps[0]
1070-
result, err = c.Upstream.Send(step.Method, step.Params, step.SessionID)
1099+
result, err = c.Router.Send(step.Method, step.Params, step.SessionID)
10711100
} else if command.Target == "service_worker" {
10721101
if c.Injector == nil || c.Injector.SessionID == "" {
10731102
return nil, fmt.Errorf("service_worker commands require an injected ModCDP extension target")

go/modcdp/injector/BBExtensionInjector.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ func (i *BBExtensionInjector) ConfigForLauncher() LauncherConfig {
7070
}
7171

7272
func (i *BBExtensionInjector) Inject() (*ExtensionInjectionResult, error) {
73-
extensionID := i.Config.InjectorServiceWorkerExtensionID
74-
i.Config.InjectorServiceWorkerExtensionID = ""
75-
defer func() { i.Config.InjectorServiceWorkerExtensionID = extensionID }()
76-
7773
discovered, err := i.waitForReadyServiceWorker(i.Config.InjectorServiceWorkerReadyTimeoutMS, i.Config.InjectorTrustServiceWorkerTarget)
7874
if err != nil || discovered == nil {
7975
return discovered, err

go/modcdp/injector/CDPExtensionInjector.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,16 @@ func (i *CDPExtensionInjector) Inject() (*ExtensionInjectionResult, error) {
4343
loadResult, err := i.sendWithTimeout("Extensions.loadUnpacked", map[string]any{"path": i.UnpackedExtensionPath}, "", i.Config.InjectorCDPSendTimeoutMS)
4444
if err != nil {
4545
if strings.Contains(err.Error(), "Method not available") || strings.Contains(err.Error(), "Method not found") || strings.Contains(err.Error(), "wasn't found") {
46-
i.LastError = err
4746
return nil, nil
4847
}
4948
return nil, fmt.Errorf("Extensions.loadUnpacked failed for %s: %w", i.UnpackedExtensionPath, err)
5049
}
5150
extensionID, _ := loadResult["id"].(string)
52-
if extensionID == "" {
53-
extensionID, _ = loadResult["extensionId"].(string)
54-
}
5551
if extensionID == "" {
5652
return nil, fmt.Errorf("Extensions.loadUnpacked returned no extension id")
5753
}
5854
i.ExtensionID = extensionID
5955
i.ServiceWorkerExtensionID = extensionID
60-
i.Config.InjectorCDPExtensionID = extensionID
61-
i.Config.InjectorServiceWorkerExtensionID = extensionID
6256
swURLPrefix := "chrome-extension://" + extensionID + "/"
6357
deadline := time.Now().Add(time.Duration(i.Config.InjectorServiceWorkerReadyTimeoutMS) * time.Millisecond)
6458
for time.Now().Before(deadline) {

go/modcdp/injector/ExtensionInjector.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ type ExtensionInjector struct {
4444
URL string
4545
SessionID string
4646
UnusableTargetIDs map[string]bool
47-
LastError error
4847
ExtraArgs []string
4948
}
5049

go/modcdp/injector/NodeExtensionFiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func PrepareUnpackedExtension(extensionPath string) (*PreparedExtension, error)
5353
_ = os.RemoveAll(dir)
5454
return nil, err
5555
}
56-
return &PreparedExtension{UnpackedExtensionPath: dir, CleanupPath: dir}, nil
56+
return &PreparedExtension{UnpackedExtensionPath: extensionRoot(dir), CleanupPath: dir}, nil
5757
}
5858
reader, err := zip.OpenReader(extensionPath)
5959
if err != nil {

go/modcdp/launcher/BrowserLauncher.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io"
1111
"net"
1212
"net/http"
13-
"net/url"
1413
"os"
1514
"strings"
1615
"time"
@@ -49,20 +48,8 @@ func WebsocketURLFor(endpoint string) (string, error) {
4948
return "", fmt.Errorf("GET /json/version: %w", err)
5049
}
5150
defer resp.Body.Close()
52-
if resp.StatusCode == http.StatusNotFound {
53-
parsed, parseErr := url.Parse(httpEndpoint)
54-
if parseErr != nil {
55-
return "", parseErr
56-
}
57-
if parsed.Scheme == "https" {
58-
parsed.Scheme = "wss"
59-
} else {
60-
parsed.Scheme = "ws"
61-
}
62-
parsed.Path = "/devtools/browser"
63-
parsed.RawQuery = ""
64-
parsed.Fragment = ""
65-
return parsed.String(), nil
51+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
52+
return "", fmt.Errorf("GET %s/json/version -> %d", httpEndpoint, resp.StatusCode)
6653
}
6754
body, _ := io.ReadAll(resp.Body)
6855
var version map[string]any
@@ -71,7 +58,7 @@ func WebsocketURLFor(endpoint string) (string, error) {
7158
}
7259
wsURL, _ := version["webSocketDebuggerUrl"].(string)
7360
if wsURL == "" {
74-
return "", fmt.Errorf("HTTP discovery for %s returned no webSocketDebuggerUrl", endpoint)
61+
return "", fmt.Errorf("cdp_url HTTP discovery returned no webSocketDebuggerUrl")
7562
}
7663
return wsURL, nil
7764
}

go/modcdp/launcher/LocalBrowserLauncher.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (l *LocalBrowserLauncher) Launch(config LauncherConfig) (*LaunchedBrowser,
6767
ownsProfileDir = true
6868
}
6969
cleanupProfileDir := ownsProfileDir
70-
if config.LauncherLocalCleanupUserDataDir != nil {
70+
if !ownsProfileDir && config.LauncherLocalCleanupUserDataDir != nil {
7171
cleanupProfileDir = *config.LauncherLocalCleanupUserDataDir
7272
}
7373
args := []string{
@@ -101,7 +101,7 @@ func (l *LocalBrowserLauncher) Launch(config LauncherConfig) (*LaunchedBrowser,
101101
if headless {
102102
args = append(args, "--headless=new")
103103
}
104-
sandbox := runtime.GOOS != "linux"
104+
sandbox := !headless
105105
if config.LauncherLocalSandbox != nil {
106106
sandbox = *config.LauncherLocalSandbox
107107
}
@@ -215,9 +215,10 @@ func (l *LocalBrowserLauncher) Launch(config LauncherConfig) (*LaunchedBrowser,
215215
return nil, err
216216
}
217217
loopbackCDPURL := ""
218+
loopbackCDPPort := port
218219
if useLoopbackCDP {
219220
if port == 0 {
220-
loopbackCDPURL, _, err = waitForBrowserSelectedCdpWebSocketURL(profileDir, time.Duration(chromeReadyTimeoutMS)*time.Millisecond, time.Duration(chromeReadyPollIntervalMS)*time.Millisecond)
221+
loopbackCDPURL, loopbackCDPPort, err = waitForBrowserSelectedCdpWebSocketURL(profileDir, time.Duration(chromeReadyTimeoutMS)*time.Millisecond, time.Duration(chromeReadyPollIntervalMS)*time.Millisecond)
221222
} else {
222223
loopbackCDPURL, err = waitForCdpWebSocketURL(fmt.Sprintf("http://127.0.0.1:%d", port), time.Duration(chromeReadyTimeoutMS)*time.Millisecond, time.Duration(chromeReadyPollIntervalMS)*time.Millisecond)
223224
}
@@ -228,7 +229,7 @@ func (l *LocalBrowserLauncher) Launch(config LauncherConfig) (*LaunchedBrowser,
228229
}
229230
launched := &LaunchedBrowser{
230231
LoopbackCDPURL: loopbackCDPURL,
231-
CDPListenPort: port,
232+
CDPListenPort: loopbackCDPPort,
232233
Close: close,
233234
ProfileDir: profileDir,
234235
PipeRead: pipeRead,

go/modcdp/launcher/LocalBrowserLauncher_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func TestLaunchesARealBrowserOverAChosenCDPPortAndExplicitProfileDir(t *testing.
6464
if chrome.ProfileDir != profileDir {
6565
t.Fatalf("ProfileDir = %q, want %q", chrome.ProfileDir, profileDir)
6666
}
67+
if chrome.CDPListenPort != port {
68+
t.Fatalf("CDPListenPort = %d, want %d", chrome.CDPListenPort, port)
69+
}
6770
transportConfig := launcher.ConfigForUpstream()
6871
if transportConfig["upstream_ws_cdp_url"] != chrome.CDPURL {
6972
t.Fatalf("transport cdp_url = %v, want %s", transportConfig["upstream_ws_cdp_url"], chrome.CDPURL)
@@ -171,6 +174,9 @@ func TestLaunchesAPipeBrowserWithAnAuxiliaryLoopbackCDPEndpointOnlyWhenRequested
171174
if !strings.HasPrefix(chrome.LoopbackCDPURL, "ws://127.0.0.1:") {
172175
t.Fatalf("LoopbackCDPURL = %q", chrome.LoopbackCDPURL)
173176
}
177+
if chrome.CDPListenPort <= 0 {
178+
t.Fatalf("CDPListenPort = %d", chrome.CDPListenPort)
179+
}
174180
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
175181
defer cancel()
176182
conn, _, _, err := ws.Dial(ctx, chrome.LoopbackCDPURL)

0 commit comments

Comments
 (0)