daemon

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 18 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Remote

func Remote(c *Client) chrome.Browser

Remote returns a chrome.Browser backed by the given daemon Client.

func RunDaemon

func RunDaemon(sockPath string, opts chrome.Options, idle time.Duration) error

RunDaemon connects Chrome and serves sockPath until idle or stopped. Used by the hidden `__daemon` invocation.

func Serve

func Serve(ln net.Listener, b chrome.Browser, idle time.Duration)

Serve accepts connections on ln and dispatches each to b until an idle period of `idle` elapses or a stop request arrives.

func SocketPath

func SocketPath(key string) string

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

func Status(sock, endpoint string) (map[string]any, error)

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

func TryConnect(sockPath string) *Client

TryConnect returns a Client if a daemon is already listening on sockPath.

func (*Client) Status

func (c *Client) Status() error

Status pings the daemon; nil error means it's alive.

func (*Client) StatusInfo

func (c *Client) StatusInfo() (map[string]any, error)

StatusInfo returns the daemon's status payload: {connected, target_count}.

func (*Client) Stop

func (c *Client) Stop() error

Stop asks the daemon to shut down.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL