@ts-pf/stream
Opt-in codecs for root AsyncIterable input and output. JSON calls stay JSON. StreamCodec frames streams as application/jsonl (one existing RPC envelope per line); SseCodec frames stream output as text/event-stream.
Agent skill: skills/ts-pf-stream/. Sync with npx skills experimental_sync -y.
import { procedure } from '@ts-pf/contract'import { z } from 'zod'import { stream, StreamCodec } from '@ts-pf/stream'
import { FetchHandler } from '@ts-pf/server-http'import { FetchLink } from '@ts-pf/client-http'
const codec = new StreamCodec()new FetchHandler(app, { codec })new FetchLink({ url: '/rpc', codec })
procedure.output(stream(z.object({ token: z.string() })))An input stream(item) takes items of the item schema’s input type from the sender; the handler iterates parsed items.
Use the same codec on the handler and the link. A stream() procedure on the default JsonCodec (or MultipartCodec) answers 400 BAD_REQUEST Stream procedure requires a streaming codec before the handler runs.
Put cleanup on the handler’s signal, not in a generator’s finally: a cancel calls the stream’s return(), and an async generator that was never pulled completes on return() without running its body, so its finally never runs, and a generator waiting on an await between yields does not see return() until its next yield. Over HTTP the handler’s signal is request.signal, which fires on disconnect when your host aborts it. A host that only cancels the response body calls return() without aborting signal, so if your host does that, return a hand-written iterator whose return() does the same cleanup; guard it so it runs once.
On the client, leaving the for await early (break, or a throw in the loop) cancels the response body, which ends the request. A loop that is waiting for the next item can’t break, so to stop it from outside, or to drop a stream you never read, abort the call’s signal. With an in-process fetch that calls FetchHandler, the abort only reaches the handler’s signal, so the handler has to react to it. If a FetchInterceptor clones the response, read or cancel the clone too: the server’s stream stays open until both copies are done.
Server-sent events
Section titled “Server-sent events”SseCodec frames root AsyncIterable output as text/event-stream. JSON calls stay JSON, and input streams stay application/jsonl. Contracts use the same stream():
import { procedure } from '@ts-pf/contract'import { z } from 'zod'import { SseCodec, stream } from '@ts-pf/stream'
import { FetchHandler } from '@ts-pf/server-http'import { FetchLink } from '@ts-pf/client-http'
const codec = new SseCodec() // optional { inner, keepAliveMs } — default 15_000; 0 disables pingsnew FetchHandler(app, { codec })new FetchLink({ url: '/rpc', codec })
procedure.output(stream(z.object({ token: z.string() })))An event: error after the stream started rejects the iterator with a PFError whose status is undefined (the response was already 200); switch on code. SSE_CONTENT_TYPE is the response content type. SseCodec was @ts-pf/sse before 1.0.
StreamCodec,stream()SseCodec,SSE_CONTENT_TYPE
Wire spec: packages/protocol/PROTOCOL.md (Message streams, SSE).