wpstream

package
v1.12.2-0...-eb60e09 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wpstream is a hand-rolled Engine.IO v4 + Socket.IO v4 + socket.io-stream client. It ports the socket.io transport used by src/bin/vip-wp.js for the wpcliStrategy=websocket WP-CLI strategy.

engineio.go is the bottom layer: the Engine.IO v4 transport. It does the HTTP long-poll handshake, upgrades to WebSocket, answers server pings, and exposes a packet-level duplex to the Socket.IO codec above it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a Socket.IO v4 namespace client over a transport.

func NewClient

func NewClient(eng transport, nsp string) *Client

NewClient creates a Socket.IO namespace client over the given transport.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect sends the namespace CONNECT packet and waits for the server CONNECT ack, then starts the read loop in a background goroutine.

func (*Client) Emit

func (c *Client) Emit(ctx context.Context, event string, args []any, ack func([]any)) error

Emit sends an event to the server. If ack is non-nil the packet carries an ack id and ack will be called when the server replies.

func (*Client) On

func (c *Client) On(event string, h func(args []any))

On registers a handler for the named event.

func (*Client) OnRaw

func (c *Client) OnRaw(event string, h func(args []any, ackID *int))

OnRaw registers a handler for the named event that also receives the ack id. Raw handlers fire before plain On handlers. The iostream layer uses this to send ACK replies for $stream-write events.

type DialOptions

type DialOptions struct {
	BaseURL string      // e.g. https://api.wpvip.com (no trailing /socket.io/)
	Header  http.Header // extraHeaders carried on BOTH transports (Bearer token)
	Client  *http.Client
}

DialOptions configure the Engine.IO connection.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is a connected Engine.IO transport (after the websocket upgrade). A background read loop dispatches incoming frames: pings are answered immediately, non-ping packets are queued on recvCh for Read callers.

func Dial

func Dial(ctx context.Context, opts DialOptions) (*Engine, error)

Dial performs the polling handshake then upgrades to WebSocket.

func (*Engine) Close

func (e *Engine) Close() error

func (*Engine) Read

func (e *Engine) Read(ctx context.Context) (Packet, error)

Read returns the next non-heartbeat packet. Blocks until a packet arrives, the context is cancelled, or the connection is closed.

func (*Engine) SID

func (e *Engine) SID() string

func (*Engine) WriteBinary

func (e *Engine) WriteBinary(ctx context.Context, data []byte) error

WriteBinary sends a raw binary attachment frame (EIO4: no type prefix).

func (*Engine) WriteMessage

func (e *Engine) WriteMessage(ctx context.Context, payload []byte) error

WriteMessage sends a Socket.IO message packet ("4" + payload).

type IOStream

type IOStream struct {
	// contains filtered or unexported fields
}

IOStream is a duplex stream over the socket.io-stream subprotocol. It implements io.ReadWriteCloser.

Flow control: reading triggers a $stream-read credit to the remote sender; the sender waits for that credit before flushing one chunk via $stream-write. Write blocks until a credit arrives (from the remote reader calling Read) and until the remote acknowledges receipt (ack from $stream-write handler).

Teardown: abort(err) closes the `closed` channel, unblocking all blocked Read/Write/deliver calls. deliverEOF() is a normal end-of-stream; it uses a separate sync.Once-guarded readEOF channel so buffered data can still drain.

func (*IOStream) Close

func (s *IOStream) Close() error

Close implements io.Closer. Sends $stream-end exactly once and marks the stream closed. Idempotent.

func (*IOStream) Read

func (s *IOStream) Read(p []byte) (int, error)

Read implements io.Reader. On the first call (or after consuming a previous chunk) it sends a $stream-read credit to the remote, then blocks until a chunk, EOF, or error arrives. Also unblocks when the stream is aborted (I3).

func (*IOStream) Write

func (s *IOStream) Write(p []byte) (int, error)

Write implements io.Writer. Blocks until a read-credit arrives from the remote (i.e., the remote called Read, which sent $stream-read), then sends the chunk and blocks until the remote ACKs receipt. Both waits are escapable via the closed channel (I3). Emit errors are propagated (M1).

type Options

type Options struct {
	APIHost    string
	Token      string
	GUID       string
	InputToken string
	Columns    int
	Rows       int
	IsTTY      bool // CR→LF stdin normalization when true (vip-wp.js:51)

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
}

Options configure a single Run.

type Packet

type Packet struct {
	Type   byte
	Data   []byte
	Binary bool
}

Packet is one Engine.IO packet. For text packets Type is the type char and Data is the payload after it. For raw binary frames (socket.io attachments) Binary is true, Type is eioMessage by convention, and Data holds the bytes.

type Result

type Result struct {
	ExitCode int
}

Result carries the terminal outcome (the caller maps ExitCode to os.Exit).

func Run

func Run(ctx context.Context, opts Options) (Result, error)

Run connects and executes one WP-CLI command over socket.io. It implements the reconnect/offset loop: on disconnect (before an exit event arrives) it re-dials with exponential backoff and resumes from offset.

C1/C2 fix: the loop is entirely self-contained; every Engine is explicitly closed before the next attempt or before returning. No goroutine is launched that outlives its engine.

type StreamSocket

type StreamSocket struct {
	// contains filtered or unexported fields
}

StreamSocket wraps a Client to provide the socket.io-stream subprotocol. Port of socket.io-stream/lib/socket.js.

func NewStreamSocket

func NewStreamSocket(ctx context.Context, cli *Client) *StreamSocket

func (*StreamSocket) CreateStream

func (ss *StreamSocket) CreateStream() *IOStream

func (*StreamSocket) Emit

func (ss *StreamSocket) Emit(ctx context.Context, event string, args []any, ack func([]any)) error

func (*StreamSocket) On

func (ss *StreamSocket) On(event string, h func(args []any, ackID *int))

Jump to

Keyboard shortcuts

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