{"modulePath":"github.com/kernel/kernel-images/server","version":"v0.0.0-20261125192504-955d9b34e97d","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package wsproxy\n\n## Functions\n\n```go\nfunc Proxy(w http.ResponseWriter, r *http.Request, upstreamURL string, opts ProxyOptions)\n```\nProxy accepts a client WebSocket upgrade, dials the upstream URL, and pumps messages bidirectionally until either side closes. ProxyOptions fields are optional and use defaults when omitted.\n\n```go\nfunc Pump(ctx context.Context, client, upstream Conn, onClose func(cause PumpExitCause), logger *slog.Logger, transform MessageTransform, gate Gate, observe Observer)\n```\nPump bidirectionally copies messages between client and upstream until either side errors or ctx is cancelled, then calls onClose with the cause. If transform is non-nil it is called for every message; the returned bytes are forwarded to the other side. If gate is non-nil it is called for every client-to-upstream message after transform, and a message it refuses is answered to the client instead of being forwarded. If observe is non-nil it is called for every message that was forwarded successfully, so a message whose write failed, or that the gate refused, is never observed.\n\n## Types\n\n```go\ntype Conn interface {\n\tRead(ctx context.Context) (websocket.MessageType, []byte, error)\n\tWrite(ctx context.Context, typ websocket.MessageType, p []byte) error\n\tClose(statusCode websocket.StatusCode, reason string) error\n}\n```\nConn abstracts a WebSocket connection for testing and flexibility.\n\n```go\ntype Gate func(mt websocket.MessageType, msg []byte) (reply []byte, refuse bool)\n```\nGate inspects a client-to-upstream message before it is forwarded. refuse reports whether to hold the message back; reply, when non-empty, is written to the client in its place so the caller still gets an answer to the request it made. The two are separate so that a gate which cannot build a reply still refuses the message rather than letting it through.\n\nUnlike an Observer, a Gate runs ahead of the forward, so whatever it does is latency on the message it is looking at rather than on the next one. Keep it to a decision.\n\n```go\ntype MessageTransform func(direction string, mt websocket.MessageType, msg []byte) []byte\n```\nMessageTransform is called for every message flowing through the proxy. direction is \"-\u003e\" for client-to-upstream and \"\\\u003c-\" for upstream-to-client. It returns the (possibly modified) message bytes to forward.\n\n```go\ntype Observer func(direction string, mt websocket.MessageType, msg []byte, ts int64)\n```\nObserver is called after a message has been successfully written to the other side, with ts set to the time that write completed (Unix microseconds). It runs on the pump goroutine, so anything it does delays the next message: hand work to a worker rather than doing it here. msg is not retained by the pump after the call, so an observer may take ownership.\n\n```go\ntype ProxyOptions struct {\n\tAcceptOptions *websocket.AcceptOptions\n\tDialOptions   *websocket.DialOptions\n\tLogger        *slog.Logger\n\tTransform     MessageTransform\n\tObserve       Observer\n\t// Registry, when set, tracks the accepted client connection so it is\n\t// closed with a Going Away frame on server shutdown.\n\tRegistry *wsdrain.Registry\n}\n```\nProxyOptions configures the proxy accept/dial behavior and optional message transformation. Zero values are valid and use sensible defaults.\n\n```go\ntype PumpExitCause string\n```\nPumpExitCause names which side caused Pump to return. Callers use this to distinguish a clean client close from an upstream failure or context cancellation when deciding telemetry attribution and reconnect policy.\n\n```go\nconst (\n\t// PumpExitClient indicates the client-side read or upstream-side write\n\t// returned an error first (typically: client closed the WS).\n\tPumpExitClient PumpExitCause = \"client\"\n\t// PumpExitUpstream indicates the upstream-side read or client-side write\n\t// returned an error first (typically: upstream died or restarted).\n\tPumpExitUpstream PumpExitCause = \"upstream\"\n\t// PumpExitContext indicates the pump's context was cancelled before\n\t// either side errored (typically: server shutdown).\n\tPumpExitContext PumpExitCause = \"context\"\n)\n```\n\n","path":"github.com/kernel/kernel-images/server/lib/wsproxy","name":"wsproxy","synopsis":"","isRedistributable":true}
