Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/examples/demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sync"
"time"

modcdp "github.com/pirate/ModCDP/go/modcdp"
modcdp "github.com/browserbase/modcdp/go/modcdp"
"golang.org/x/term"
)

Expand Down
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/pirate/ModCDP/go
module github.com/browserbase/modcdp/go

go 1.25.0

Expand Down
22 changes: 11 additions & 11 deletions go/modcdp/client/ModCDPClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ import (
"time"

abxjsonschema "github.com/ArchiveBox/abxbus/abxbus-go/jsonschema"
"github.com/pirate/ModCDP/go/modcdp/injector"
"github.com/pirate/ModCDP/go/modcdp/launcher"
"github.com/pirate/ModCDP/go/modcdp/router"
"github.com/pirate/ModCDP/go/modcdp/translate"
transportpkg "github.com/pirate/ModCDP/go/modcdp/transport"
"github.com/pirate/ModCDP/go/modcdp/types"
"github.com/browserbase/modcdp/go/modcdp/injector"
"github.com/browserbase/modcdp/go/modcdp/launcher"
"github.com/browserbase/modcdp/go/modcdp/router"
"github.com/browserbase/modcdp/go/modcdp/translate"
transportpkg "github.com/browserbase/modcdp/go/modcdp/transport"
"github.com/browserbase/modcdp/go/modcdp/types"
)

var (
extIDFromURL = regexp.MustCompile(`^chrome-extension://([a-z]+)/`)
)

const modcdpReadyExpression = `Boolean(globalThis.ModCDP?.__ModCDPServerVersion === 1 && globalThis.ModCDP?.handleCommand && globalThis.ModCDP?.addCustomEvent)`
const modcdpReadyExpression = `Boolean(globalThis.ModCDP?.__ModCDPServerVersion >= 1 && globalThis.ModCDP?.handleCommand && globalThis.ModCDP?.addCustomEvent)`

const DefaultCDPSendTimeoutMS = 10_000
const DefaultEventWaitTimeoutMS = 10_000
Expand Down Expand Up @@ -1385,14 +1385,14 @@ func handlerPointer(handler Handler) uintptr {
}

func (c *ModCDPClient) Close() {
if c.transport != nil {
_ = c.transport.Close()
c.transport = nil
}
if c.launchedBrowser != nil {
c.launchedBrowser.Close()
c.launchedBrowser = nil
}
if c.transport != nil {
_ = c.transport.Close()
c.transport = nil
}
for _, injector := range c.extensionInjectors {
_ = injector.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion go/modcdp/injector/BBBrowserExtensionInjector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package injector_test

import (
"fmt"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
"os"
"path/filepath"
"regexp"
Expand Down
2 changes: 1 addition & 1 deletion go/modcdp/injector/BorrowedExtensionInjector.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const __name = (fn) => fn;
%s
const ModCDP = installModCDPServer(globalThis);
return {
ok: Boolean(ModCDP?.__ModCDPServerVersion === 1 && ModCDP?.handleCommand && ModCDP?.addCustomEvent),
ok: Boolean(ModCDP?.__ModCDPServerVersion >= 1 && ModCDP?.handleCommand && ModCDP?.addCustomEvent),
extension_id: globalThis.chrome?.runtime?.id ?? null,
has_tabs: Boolean(globalThis.chrome?.tabs?.query),
has_debugger: Boolean(globalThis.chrome?.debugger?.sendCommand && globalThis.chrome?.debugger?.getTargets),
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/injector/BorrowedExtensionInjector_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package injector_test

import (
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/injector"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/injector"
"path/filepath"
"testing"
)
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/injector/DiscoveredExtensionInjector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package injector_test

import (
"encoding/json"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/injector"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/injector"
"os"
"path/filepath"
"testing"
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/injector/ExtensionInjector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/pirate/ModCDP/go/modcdp/types"
"github.com/browserbase/modcdp/go/modcdp/types"
)

const DefaultModCDPExtensionID = "mdedooklbnfejodmnhmkdpkaedafkehf"
Expand All @@ -21,7 +21,7 @@ const DefaultTargetSessionPollIntervalMS = 20
var DefaultModCDPServiceWorkerURLSuffixes = []string{"/modcdp/service_worker.js"}
var extIDFromURL = regexp.MustCompile(`^chrome-extension://([a-z]+)/`)

const modcdpReadyExpression = `Boolean(globalThis.ModCDP?.__ModCDPServerVersion === 1 && globalThis.ModCDP?.handleCommand && globalThis.ModCDP?.addCustomEvent)`
const modcdpReadyExpression = `Boolean(globalThis.ModCDP?.__ModCDPServerVersion >= 1 && globalThis.ModCDP?.handleCommand && globalThis.ModCDP?.addCustomEvent)`

type SendCDP = types.SendCDP
type SessionIDForTarget = types.SessionIDForTarget
Expand Down
6 changes: 3 additions & 3 deletions go/modcdp/injector/ExtensionInjector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/injector"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/injector"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestExtensionInjectorKeepsModCDPServiceWorkerAliveThroughOffscreenKeepalive
t.Fatal(err)
}
versionResult, _ := version["result"].(map[string]any)
if versionResult["value"] != float64(1) {
if versionResult["value"] != float64(2) {
t.Fatalf("ModCDP server version = %#v", versionResult["value"])
}
}
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/injector/ExtensionsLoadUnpackedInjector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/injector"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/injector"
"path/filepath"
"strings"
"testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package injector_test

import (
"archive/zip"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/injector"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/injector"
"os"
"path/filepath"
"runtime"
Expand Down
Binary file modified go/modcdp/injector/extension.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion go/modcdp/launcher/BrowserLauncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/pirate/ModCDP/go/modcdp/types"
"github.com/browserbase/modcdp/go/modcdp/types"
)

type LaunchOptions = types.LaunchOptions
Expand Down
9 changes: 5 additions & 4 deletions go/modcdp/modcdp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package modcdp

import (
"github.com/pirate/ModCDP/go/modcdp/client"
"github.com/pirate/ModCDP/go/modcdp/injector"
"github.com/pirate/ModCDP/go/modcdp/launcher"
"github.com/pirate/ModCDP/go/modcdp/transport"
"github.com/browserbase/modcdp/go/modcdp/client"
"github.com/browserbase/modcdp/go/modcdp/injector"
"github.com/browserbase/modcdp/go/modcdp/launcher"
"github.com/browserbase/modcdp/go/modcdp/transport"
)

type ModCDPClient = client.ModCDPClient
Expand All @@ -17,6 +17,7 @@ type ServerConfig = client.ServerConfig
type CustomCommand = client.CustomCommand
type CustomEvent = client.CustomEvent
type CustomMiddleware = client.CustomMiddleware
type CDPEvent = client.CDPEvent
type LaunchOptions = launcher.LaunchOptions
type LaunchedBrowser = launcher.LaunchedBrowser
type BrowserLauncher = launcher.BrowserLauncher
Expand Down
2 changes: 1 addition & 1 deletion go/modcdp/router/AutoSessionRouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"testing"
"time"

"github.com/browserbase/modcdp/go/modcdp/launcher"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/pirate/ModCDP/go/modcdp/launcher"
)

func TestAutoSessionRouterRejectsPendingExecutionContextWaitersWhenSessionDetaches(t *testing.T) {
Expand Down
20 changes: 16 additions & 4 deletions go/modcdp/translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func wrapCustomCommand(method string, params map[string]any, sessionID string) m
m, _ := json.Marshal(method)
p, _ := json.Marshal(params)
sid, _ := json.Marshal(sessionID)
return callFunctionParams(fmt.Sprintf(`async function() { return await globalThis.ModCDP.handleCommand(%s, %s, %s); }`, string(m), string(p), string(sid)))
return callFunctionParams(fmt.Sprintf(`async function() { return JSON.stringify(await globalThis.ModCDP.handleCommand(%s, %s, %s)); }`, string(m), string(p), string(sid)))
}

func wrapServiceWorkerCommand(method string, params map[string]any, sessionID string, targetSessionID string) []rawStep {
Expand All @@ -165,6 +165,7 @@ func wrapServiceWorkerCommand(method string, params map[string]any, sessionID st
}
}
runtimeParams := map[string]any{}
unwrap := "runtime"
switch method {
case "Mod.evaluate":
runtimeParams = wrapModCDPEvaluate(params, targetSessionID)
Expand All @@ -178,8 +179,9 @@ func wrapServiceWorkerCommand(method string, params map[string]any, sessionID st
cdpSessionID = targetSessionID
}
runtimeParams = wrapCustomCommand(method, params, cdpSessionID)
unwrap = "runtime_json"
}
return []rawStep{{Method: "Runtime.callFunctionOn", Params: runtimeParams, Unwrap: "runtime"}}
return []rawStep{{Method: "Runtime.callFunctionOn", Params: runtimeParams, Unwrap: unwrap}}
}

func WrapCommandIfNeeded(method string, params map[string]any, routes map[string]string, sessionID string, targetSessionID ...string) (rawCommand, error) {
Expand All @@ -198,7 +200,7 @@ func WrapCommandIfNeeded(method string, params map[string]any, routes map[string
}

func UnwrapResponseIfNeeded(result map[string]any, unwrap string) (any, error) {
if unwrap != "runtime" {
if unwrap != "runtime" && unwrap != "runtime_json" {
return result, nil
}
if ex, ok := result["exceptionDetails"].(map[string]any); ok {
Expand All @@ -219,7 +221,17 @@ func UnwrapResponseIfNeeded(result map[string]any, unwrap string) (any, error) {
return nil, fmt.Errorf("%s", msg)
}
inner, _ := result["result"].(map[string]any)
return inner["value"], nil
value := inner["value"]
if unwrap == "runtime_json" {
if raw, ok := value.(string); ok {
var decoded any
if err := json.Unmarshal([]byte(raw), &decoded); err != nil {
return nil, err
}
return decoded, nil
}
}
return value, nil
}

func UnwrapEventIfNeeded(method string, params map[string]any, sessionID string, ourSessionID string) (string, any, bool) {
Expand Down
8 changes: 8 additions & 0 deletions go/modcdp/translate/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ func TestTranslateRoutesWrapsAndUnwrapsModCDPProtocolMessagesDeterministically(t
t.Fatalf("unwrap = %q", wrapped.Steps[0].Unwrap)
}

configured, err := wrapCommandIfNeeded("Mod.configure", map[string]any{"server": map[string]any{"server_routes": map[string]any{"*.*": "loopback_cdp"}}}, DefaultClientRoutes(), "session-1")
if err != nil {
t.Fatal(err)
}
if configured.Steps[0].Unwrap != "runtime_json" {
t.Fatalf("configure unwrap = %q", configured.Steps[0].Unwrap)
}

unwrapped, err := unwrapResponseIfNeeded(map[string]any{"result": map[string]any{"type": "object", "value": map[string]any{"ok": true}}}, "runtime")
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/transport/NativeMessagingUpstreamTransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package transport_test
import (
"encoding/json"
"fmt"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/transport"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/transport"
"net"
"os"
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion go/modcdp/transport/NatsUpstreamTransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
. "github.com/pirate/ModCDP/go/modcdp/transport"
. "github.com/browserbase/modcdp/go/modcdp/transport"
"os"
"os/exec"
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion go/modcdp/transport/PipeUpstreamTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"sync"

"github.com/pirate/ModCDP/go/modcdp/launcher"
"github.com/browserbase/modcdp/go/modcdp/launcher"
)

type PipeUpstreamTransport struct {
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/transport/PipeUpstreamTransport_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package transport_test

import (
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/transport"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/transport"
"os"
"regexp"
"runtime"
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/transport/ReverseWebSocketUpstreamTransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/transport"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/transport"
"os"
"runtime"
"strings"
Expand Down
6 changes: 3 additions & 3 deletions go/modcdp/transport/UpstreamTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"net"
"sync"

"github.com/pirate/ModCDP/go/modcdp/injector"
"github.com/pirate/ModCDP/go/modcdp/launcher"
"github.com/pirate/ModCDP/go/modcdp/types"
"github.com/browserbase/modcdp/go/modcdp/injector"
"github.com/browserbase/modcdp/go/modcdp/launcher"
"github.com/browserbase/modcdp/go/modcdp/types"
)

type ExtensionInjectorConfig = types.ExtensionInjectorConfig
Expand Down
2 changes: 1 addition & 1 deletion go/modcdp/transport/UpstreamTransport_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package transport_test

import (
. "github.com/pirate/ModCDP/go/modcdp/transport"
. "github.com/browserbase/modcdp/go/modcdp/transport"
"strings"
"testing"
)
Expand Down
4 changes: 2 additions & 2 deletions go/modcdp/transport/WebSocketUpstreamTransport_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package transport_test

import (
modcdp "github.com/pirate/ModCDP/go/modcdp/client"
. "github.com/pirate/ModCDP/go/modcdp/transport"
modcdp "github.com/browserbase/modcdp/go/modcdp/client"
. "github.com/browserbase/modcdp/go/modcdp/transport"
"net/url"
"strings"
"testing"
Expand Down
Loading
Loading