UNPKG

@tanstack/router-core

Version:

Modern and scalable routing for React applications

44 lines (43 loc) 1.65 kB
import { RawStream } from "./RawStream.js"; import { decodeText, encodeText, fromBase64, fromEncodedStream, pumpEncodedStream, toBase64 } from "./rawStreamCodec.js"; import { createPlugin } from "seroval"; //#region src/ssr/serializer/RawStreamJSONPlugin.ts /** * Serializes a RawStream into JSON requests and static-cache responses. * The optional signal stops the source pump when the request is aborted. */ /* @__NO_SIDE_EFFECTS__ */ function createRawStreamJSONPlugin(signal) { return /* @__PURE__ */ createPlugin({ tag: "tss/RawStream", test: (value) => value instanceof RawStream, parse: { async: async (value, ctx) => { const text = await ctx.parse(value.hint === "text"); const [stream] = pumpEncodedStream(value.stream, value.hint === "text" ? encodeText : toBase64, signal); return { text, stream: await ctx.parse(stream) }; } }, serialize: void 0, deserialize: void 0 }); } const RawStreamJSONPlugin = /* @__PURE__ */ createRawStreamJSONPlugin(); /** * Deserializes the JSON shape above back into a `ReadableStream<Uint8Array>`. * `test` never matches, so this plugin is inert during serialization and can * share a plugin list with `RawStreamJSONPlugin`. */ const RawStreamJSONDeserializePlugin = /* @__PURE__ */ createPlugin({ tag: "tss/RawStream", test: () => false, parse: {}, serialize: void 0, deserialize(node, ctx) { return fromEncodedStream(ctx.deserialize(node.stream), ctx.deserialize(node.text) ? decodeText : fromBase64); } }); //#endregion export { RawStreamJSONDeserializePlugin, RawStreamJSONPlugin, createRawStreamJSONPlugin }; //# sourceMappingURL=RawStreamJSONPlugin.js.map