Reference
rouzer/ndjson
Functions
$type
Create a compile-time marker for newline-delimited JSON response items.
export function $type<T>(): ResponsePluginMarker<AsyncIterable<T>, NdjsonSource<T>, typeof codecId>;
Remarks
The returned marker is handled by clientPlugin in clients and
routerPlugin in routers. Generated client action functions resolve to an
AsyncIterable<T>, while route handlers may return either an Iterable<T>
or an AsyncIterable<T>. Rouzer does not validate streamed items at runtime.
decodeNdjson
Decode a newline-delimited JSON byte stream.
export function decodeNdjson<T = unknown>(stream: ReadableStream<Uint8Array>): AsyncIterable<T>;
Remarks
UTF-8 chunks may split JSON lines. Both \n and \r\n line endings
are accepted, and a final line does not need a trailing newline. Malformed
lines throw a SyntaxError that includes the 1-based line number.
encodeNdjson
Encode an iterable of values as a newline-delimited JSON byte stream.
export function encodeNdjson(source: NdjsonSource, options?: NdjsonEncodeOptions): ReadableStream<Uint8Array>;
Remarks
Each yielded value is serialized with JSON.stringify and followed
by \n. Values that cannot be represented as a JSON text, such as
undefined, cause the stream to error when read. When options.signal
aborts, the source iterator's return() method is called and the stream is
closed.
ndjsonResponse
Create a Response whose body is encoded as newline-delimited JSON.
export function ndjsonResponse<T>(source: NdjsonSource<T>, { signal, ...init }?: ResponseInit & NdjsonEncodeOptions): Response;
Remarks
The response defaults to
content-type: application/x-ndjson; charset=utf-8 unless the caller supplies
a content type in init.headers.
Constants
clientPlugin
Client plugin that decodes successful NDJSON responses.
export const clientPlugin: ClientResponsePlugin;
Remarks
Register this plugin with createClient({ plugins }) when the route
tree contains response: ndjson.$type<T>() markers.
codecId
const codecId = "rouzer/ndjson";
routerPlugin
Router plugin that encodes handler results as NDJSON responses.
export const routerPlugin: RouterResponsePlugin;
Remarks
Register this plugin with createRouter({ plugins }) when the route
tree contains response: ndjson.$type<T>() markers. Handler or generator
errors are not encoded as NDJSON items; model application-level errors in the
item type when clients should receive them as data.
Types
NdjsonEncodeOptions
Options for Rouzer's NDJSON response encoder.
export type NdjsonEncodeOptions = {
signal?: AbortSignal;
};
Properties
signalSignal whose abort cancels the source iterator and closes the stream.
š NdjsonEncodeOptions on GitHub
NdjsonSource
Values accepted by Rouzer's NDJSON response encoder.
export type NdjsonSource<T = unknown> = Iterable<T> | AsyncIterable<T>;