UNPKG

@tanstack/router-core

Version:

Modern and scalable routing for React applications

1 lines 2.1 kB
{"version":3,"file":"RawStream.cjs","names":[],"sources":["../../../../src/ssr/serializer/RawStream.ts"],"sourcesContent":["import type { PluginInfo, SerovalNode } from 'seroval'\n\nexport type RawStreamHint = 'binary' | 'text'\n\nexport interface RawStreamOptions {\n /**\n * Encoding hint for JSON and SSR serialization.\n * - 'binary' (default): Always use base64 encoding\n * - 'text': Try UTF-8 first, fallback to base64 for invalid UTF-8 chunks\n */\n hint?: RawStreamHint\n}\n\n/**\n * Marker class for ReadableStream<Uint8Array> that should be serialized\n * with base64/text encoding (JSON and SSR) or binary framing\n * (server-function responses).\n *\n * Wrap your binary streams with this to get efficient serialization:\n * ```ts\n * // For binary data (files, images, etc.)\n * return { data: new RawStream(file.stream()) }\n *\n * // For text-heavy data (RSC payloads, etc.)\n * return { data: new RawStream(rscStream, { hint: 'text' }) }\n * ```\n *\n * RawStreams returned from one server function share one ordered response.\n * Arbitrary or sequential consumption can require potentially unbounded client\n * buffering for unread data. Cancelling one RawStream discards it locally;\n * abort the whole server-function call to cancel the response and server work.\n * Consume streams concurrently, cancel unused streams promptly, or use separate\n * calls when independent backpressure is required.\n */\nexport class RawStream {\n public readonly hint: RawStreamHint\n\n constructor(\n public readonly stream: ReadableStream<Uint8Array>,\n options?: RawStreamOptions,\n ) {\n this.hint = options?.hint ?? 'binary'\n }\n}\n\nexport type OnRawStreamCallback = (\n streamId: number,\n stream: ReadableStream<Uint8Array>,\n) => void\n\nexport interface RawStreamJSONNode extends PluginInfo {\n text: SerovalNode\n stream: SerovalNode\n}\n\nexport interface RawStreamRPCNode extends PluginInfo {\n streamId: SerovalNode\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,YAAb,MAAuB;CAGrB,YACE,QACA,SACA;EAFgB,KAAA,SAAA;EAGhB,KAAK,OAAO,SAAS,QAAQ;CAC/B;AACF"}