@ts-pf/message
Opt-in JSON text frames and MessageSession for message-oriented transports. Not an HTTP codec. Bindings live in @ts-pf/message-server and @ts-pf/message-client.
Agent skill: skills/ts-pf-message/. Sync with npx skills experimental_sync -y.
createPortDuplex / createWsDuplex / WebSocketLike adapt MessagePort and a WebSocket-like socket to Duplex. Stdio is a subpath:
import { createPortDuplex, createWsDuplex, type WebSocketLike } from '@ts-pf/message'import { createStdioDuplex } from '@ts-pf/message/stdio'WebSocketLike is the WHATWG subset the adapter needs; its close(code?, reason?) matches both a browser WebSocket and an npm ws socket. createWsDuplex itself closes with 1000, or 1002 on a binary / non-string message; application close codes (4400–4499) come from WsHandler’s bind handle in @ts-pf/message-server.
MessageSession runs the handshake. On the server role, onHello(meta) accepts the hello; if it throws, the session sends hello-error with toWireError(thrown) from @ts-pf/protocol — a PFError goes out as its { code, message, data? } (so a server can refuse with, for example, UNAUTHORIZED), anything else as the fixed INTERNAL / Internal server error — and closes. The client role rejects ready with that error (errorFromEnvelope, no status).
createPushQueue() is the primitive a transport adapter uses to turn frames into an AsyncIterable: push(value) from a frame callback, end() to complete, fail(error) to reject the next pull. Both closes are one-shot. @ts-pf/message-server (streamed input) and @ts-pf/message-client (streamed output) use it; so can a custom Duplex binding. A custom Duplex does not need it directly: DuplexHandler (@ts-pf/message-server) and DuplexLink (@ts-pf/message-client) own the queues and multiplexing.
import { createPushQueue } from '@ts-pf/message'
const items = createPushQueue<number>()items.push(1)items.push(2)items.end() // or items.fail(error)for await (const item of items) console.log(item) // 1, 2Backpressure
Section titled “Backpressure”Nothing changes on the wire; the pipe reports pressure and the sender slows down. Duplex.send may return a promise: the pipe is above its write high-water mark and the sender must not pull the next item of that stream until it resolves (it must settle when the duplex closes; a rejection closes the session). createStdioDuplex returns one when output.write() is refused and resolves it on 'drain'; createWsDuplex(socket, { highWaterMark }) (default 1 MiB) returns one while socket.bufferedAmount is over the mark (polled every 10 ms) or while frames queue during CONNECTING; createPortDuplex never does — a MessagePort has no buffer signal, so a port cannot report backpressure. A Duplex may also implement pause() / resume() so a receiver can stop reading while a per-call queue is over its mark (pausing is connection-wide): stdio pauses the Readable, ws forwards the socket’s own pause / resume when it has them (npm ws does; browsers do not), a port cannot. createMemoryDuplex({ highWaterMark }) simulates a slow peer for tests; without the option it stays synchronous.
BackpressureOptions is the one option object @ts-pf/message-server (HandlerOptions.backpressure) and @ts-pf/message-client (LinkOptions.backpressure) take; protection is on by default:
import type { BackpressureOptions, StallInfo } from '@ts-pf/message'
const backpressure: BackpressureOptions = { highWaterMark: 1024 * 1024, // bytes buffered before a sender pauses; also the per-call receive-queue bound (frame text length). Infinity disables drainTimeout: 30_000, // ms a paused sender waits before the call is aborted; 0 waits forever onStall: (info: StallInfo) => console.warn(info.path.join('.'), info.action, info.waitedMs), // default warns on 'aborted' only; false silences}onStall sees action: 'paused' once per call (normal flow control on a busy link, not logged by default) and action: 'aborted' when the drain timeout fires (logged by default); a throw from it is swallowed and never changes the call. A bad value is a TypeError naming the option from the binding’s constructor. The validated form and the per-call drain wait (resolveBackpressure, DrainWaiter) are exported for the two bindings and marked @internal; a custom Duplex needs neither. createPushQueue({ highWaterMark, onPause, onResume }) (PushQueueOptions) bounds the queue: push(value, size) counts size (default 1) against the mark, onPause fires once above it, onResume once the consumer drains it to half or on end / fail; nothing is dropped.
Wire spec: packages/protocol/PROTOCOL.md (Message transports).
MessageSession,Duplex,encodeFrame,decodeFramecreateMemoryDuplex,createPortDuplex,createWsDuplex,WebSocketLikecreatePushQueue,PushQueue,PushQueueOptionsBackpressureOptions,StallInfo(resolveBackpressure/DrainWaiterare exported@internalfor the bindings)errorFromEnvelope(localFailureis in@ts-pf/protocol)createStdioDuplexfrom./stdioonly