sessiond

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearCurrent

func ClearCurrent(sessionID string) error

ClearCurrent removes the active-session marker. Called by stop.

func PathsFor

func PathsFor(sessionID string) (sock, meta string, err error)

PathsFor returns the socket + metadata paths for a session id.

func ReadCurrent

func ReadCurrent() string

ReadCurrent returns the recorded active session id, or "" if none.

func Resolve

func Resolve(explicit string) (string, bool)

Resolve picks a session id in priority order:

  1. the explicit flag value (if non-empty)
  2. the SCRAPFLY_SESSION env var
  3. the ~/.scrapfly/sessions/.current file

Returns ("", false) when nothing is available.

func Serve

func Serve(ctx context.Context, sessionID, wsURL string, onReady func(sock 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.

func SetCurrent

func SetCurrent(sessionID string) error

SetCurrent records sessionID as the active session for later invocations.

func SocketDir

func SocketDir() (string, error)

SocketDir returns (and creates) the per-user directory that holds session sockets + metadata. Files live at <dir>/<session>.sock and <session>.json.

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.

func LoadMeta

func LoadMeta(sessionID string) (*Meta, error)

LoadMeta reads the persisted metadata for a session, or nil if absent.

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.

type Response

type Response struct {
	OK    bool            `json:"ok"`
	Data  json.RawMessage `json:"data,omitempty"`
	Error string          `json:"error,omitempty"`
}

Response is the envelope written back per request.

func Send

func Send(sessionID string, req Request) (*Response, error)

Send opens the session's socket, writes one request, reads one response. Returned err covers transport + decoded Response.Error.

Jump to

Keyboard shortcuts

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