Documentation
¶
Overview ¶
Package daemon runs a background process that holds one CDP connection and serves Browser calls over a Unix socket. Keeping a single long-lived connection means Chrome's "Allow debugging?" prompt fires once per session (not once per command) and each command is a fast socket round-trip.
Protocol: one JSON request/response per connection (NDJSON), where a request names a Browser method and carries its positional args as JSON.
Index ¶
- func Remote(c *Client) chrome.Browser
- func RunDaemon(sockPath string, opts chrome.Options, idle time.Duration) error
- func Serve(ln net.Listener, b chrome.Browser, idle time.Duration)
- func SocketPath(key string) string
- func Status(sock, endpoint string) (map[string]any, error)
- type Client
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunDaemon ¶
RunDaemon connects Chrome and serves sockPath until idle or stopped. Used by the hidden `__daemon` invocation.
func Serve ¶
Serve accepts connections on ln and dispatches each to b until an idle period of `idle` elapses or a stop request arrives.
func SocketPath ¶
SocketPath returns the daemon socket path for an endpoint key, under the XDG runtime/state dir. Liveness is discovered by connecting (no PID file).
func Status ¶ added in v0.2.0
Status reports whether the daemon for this endpoint is running and, when it is, what its connection to Chrome actually did.
The StatusInfo error is PROPAGATED, not swallowed. A daemon that answers its socket and then fails the status call is precisely the interesting case — it is alive and its CDP connection is not — and discarding the error left `running: true` standing as if nothing had gone wrong, which is one of the three unverified claims that let `doctor` say ready.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to a running daemon over its Unix socket (one connection per call).
func Ensure ¶
func Ensure(ctx context.Context, sockPath, exePath string, env []string, consentTimeout time.Duration) (*Client, error)
Ensure connects to a running daemon, or spawns one (detached) and waits for it to come up. env carries the connection options (CHROME_CDP_*) for the daemon.
consentTimeout is how long the spawned daemon is allowed to spend waiting out Chrome's consent prompt. Ensure has to know it too: the daemon's wait is invisible from here, and a client that gave up at ten seconds while its daemon was still holding the connection would report a failure that had not happened. It arrives normalised (see chrome.ClampConsentTimeout).
ctx bounds the whole thing, including the wait for the spawn lock. Without it, --timeout stopped applying the moment a command needed a connection: the lock's holder may be sitting on an unanswered prompt for two minutes, and every caller behind it inherited that wait with no way to say otherwise.
func TryConnect ¶
TryConnect returns a Client if a daemon is already listening on sockPath.
func (*Client) StatusInfo ¶
StatusInfo returns the daemon's status payload: {connected, target_count}.
type Request ¶
type Request struct {
Method string `json:"method"`
Args []json.RawMessage `json:"args,omitempty"`
TimeoutMs int64 `json:"timeout_ms,omitempty"`
}
Request is a Browser method call. Args are the positional arguments (after ctx) as JSON. TimeoutMs carries the client's remaining deadline so the daemon bounds the action instead of running (and blocking the client) forever.
type Response ¶
type Response struct {
Result json.RawMessage `json:"result,omitempty"`
Error string `json:"error,omitempty"`
Done bool `json:"done,omitempty"`
}
Response is the method result (or an error string).
A unary call answers with exactly one Response. A STREAMING call (see streamDispatch) answers with one Response per emitted value followed by a terminator carrying Done, so the client knows the stream ended rather than guessing from a closed socket.