Documentation
¶
Overview ¶
Package sessiond runs a persistent Scrapfly Browser session in the foreground and serves actions to local CLI invocations over a Unix domain socket. The daemon holds:
- the CDP WebSocket connection to the Scrapfly Browser,
- the attached cdp.Session (with enabled domains + RefTable),
so that multiple `scrapfly browser <action>` calls can share refs and cookies without re-attaching a fresh tab each time.
Wire format on the socket is newline-delimited JSON. One request per connection; one response per request; then close.
Index ¶
- func ClearCurrent(sessionID string) error
- func PathsFor(sessionID string) (sock, meta string, err error)
- func ReadCurrent() string
- func Resolve(explicit string) (string, bool)
- func Serve(ctx context.Context, sessionID, wsURL string, dialBudget time.Duration, ...) error
- func SetCurrent(sessionID string) error
- func SocketDir() (string, error)
- type Meta
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearCurrent ¶
ClearCurrent removes the active-session marker. Called by stop.
func ReadCurrent ¶
func ReadCurrent() string
ReadCurrent returns the recorded active session id, or "" if none.
func Resolve ¶
Resolve picks a session id in priority order:
- the explicit flag value (if non-empty)
- the SCRAPFLY_SESSION env var
- the ~/.scrapfly/sessions/.current file
Returns ("", false) when nothing is available.
func Serve ¶
func Serve(ctx context.Context, sessionID, wsURL string, dialBudget time.Duration, onReady func(sock, runID string)) error
Serve runs the daemon until ctx is cancelled or a SIGINT/SIGTERM is received. It attaches to wsURL, writes <sessionID>.json + .sock under ~/.scrapfly/sessions/, serves requests, and cleans up on exit.
The daemon is foreground: the caller is responsible for backgrounding (`&`, systemd, tmux). `onReady` is invoked once the socket is listening, with the Scrapfly run id from the upgrade response (empty when the peer did not advertise one).
dialBudget bounds the CDP dial. The upgrade response only lands once Scrapfly has allocated a browser, and a cold pool (no warm agent for the requested os/proxy tuple) routinely spends more than a minute there, so the budget is the caller's --timeout rather than a private constant: an operator hitting a cold pool can raise it. Zero or negative bounds the dial by ctx alone.
func SetCurrent ¶
SetCurrent records sessionID as the active session for later invocations.
Types ¶
type Meta ¶
type Meta struct {
SessionID string `json:"session_id"`
WSURL string `json:"ws_url"`
PID int `json:"pid"`
StartedAt time.Time `json:"started_at"`
}
Meta is persisted alongside the socket to record daemon info for status queries.
type Request ¶
type Request struct {
Action string `json:"action"`
URL string `json:"url,omitempty"`
Ref string `json:"ref,omitempty"`
Text string `json:"text,omitempty"`
Direction string `json:"direction,omitempty"`
Amount float64 `json:"amount,omitempty"`
FullPage bool `json:"fullpage,omitempty"`
JS string `json:"js,omitempty"`
// Antibot / Scrapfly extras.
Selector string `json:"selector,omitempty"` // CSS selector (default) or XPath when SelectorType is "xpath"
SelectorType string `json:"selector_type,omitempty"` // "css" | "xpath" (default "css")
TimeoutMs int `json:"timeout_ms,omitempty"`
Visible bool `json:"visible,omitempty"`
RenderIframes *bool `json:"render_iframes,omitempty"` // default true on content
Clear bool `json:"clear,omitempty"`
WPM float64 `json:"wpm,omitempty"`
TargetSelector string `json:"target_selector,omitempty"`
Distance float64 `json:"distance,omitempty"`
// Raw lets callers send arbitrary action JSON for future actions
// without adding a field here. Unused today.
Raw json.RawMessage `json:"raw,omitempty"`
}
Request is one action sent from a CLI process to the daemon.