{"version":3,"file":"rawStreamCodec.cjs","names":[],"sources":["../../../../src/ssr/serializer/rawStreamCodec.ts"],"sourcesContent":["import { createStream } from 'seroval'\n\n// Web-only primitives shared by the JSON and SSR RawStream plugins. This\n// module is part of the client bundle: it must not reference `Buffer`, and\n// the encode and decode halves must stay independently tree-shakeable.\n\nexport function toBase64(bytes: Uint8Array) {\n  const chunks: Array<string> = []\n  for (let i = 0; i < bytes.length; i += 0x8000) {\n    chunks.push(\n      String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000) as any),\n    )\n  }\n  return btoa(chunks.join(''))\n}\n\nexport function fromBase64(value: string) {\n  const binary = atob(value)\n  const bytes = new Uint8Array(binary.length)\n  for (let i = 0; i < binary.length; i++) {\n    bytes[i] = binary.charCodeAt(i)\n  }\n  return bytes\n}\n\nconst textDecoder = /* @__PURE__ */ new TextDecoder('utf-8', {\n  fatal: true,\n  ignoreBOM: true,\n})\n\n/** `'t' + utf8` for a valid UTF-8 chunk, otherwise `'b' + base64`. */\nexport function encodeText(value: Uint8Array) {\n  try {\n    return 't' + textDecoder.decode(value)\n  } catch {\n    return 'b' + toBase64(value)\n  }\n}\n\nconst textEncoder = /* @__PURE__ */ new TextEncoder()\n\nexport function decodeText(value: string) {\n  const data = value.slice(1)\n  return value[0] === 't' ? textEncoder.encode(data) : fromBase64(data)\n}\n\n/**\n * Pump a byte stream into a Seroval stream, one encoded chunk per read.\n * Returns the Seroval stream and a `stop` function that cancels the reader\n * without signalling the Seroval stream; the abort signal and read failures\n * stop the pump and throw through the Seroval stream.\n */\nexport function pumpEncodedStream(\n  readable: ReadableStream<Uint8Array>,\n  encode: (value: Uint8Array) => string,\n  signal?: AbortSignal,\n) {\n  signal?.throwIfAborted()\n\n  const stream = createStream<string | undefined>()\n  const reader = readable.getReader()\n  let active = true\n\n  const release = () => {\n    active = false\n    signal?.removeEventListener('abort', abort)\n    reader.releaseLock()\n  }\n  const stop = (reason?: unknown) => {\n    if (!active) {\n      return false\n    }\n    void reader.cancel(reason).catch(() => {})\n    release()\n    return true\n  }\n  const abort = () => {\n    if (stop(signal!.reason)) {\n      stream.throw(signal!.reason)\n    }\n  }\n\n  signal?.addEventListener('abort', abort)\n  ;(async () => {\n    try {\n      while (active) {\n        const { done, value } = await reader.read()\n        if (!active) {\n          return\n        }\n        if (done) {\n          release()\n          stream.return(undefined)\n          return\n        }\n        stream.next(encode(value))\n      }\n    } catch (error) {\n      if (stop(error)) {\n        stream.throw(error)\n      }\n    }\n  })()\n\n  return [stream, stop] as const\n}\n\n/** Rebuild a byte stream from encoded Seroval stream chunks. */\nexport function fromEncodedStream(\n  source: ReturnType<typeof createStream<string | undefined>>,\n  decode: (value: string) => Uint8Array,\n) {\n  // Dropping the disposer after a terminal event lets Seroval's buffer be\n  // collected while application code still holds the finished stream.\n  let unsubscribe: (() => void) | undefined\n  let done = false\n\n  return new ReadableStream<Uint8Array>({\n    start(controller) {\n      const dispose = source.on({\n        next(value) {\n          if (done) {\n            return\n          }\n          try {\n            controller.enqueue(decode(value!))\n          } catch (error) {\n            // A malformed chunk fails this stream and abandons the source.\n            done = true\n            const stop = unsubscribe\n            unsubscribe = undefined\n            stop?.()\n            controller.error(error)\n          }\n        },\n        throw(error) {\n          if (!done) {\n            done = true\n            unsubscribe = undefined\n            controller.error(error)\n          }\n        },\n        return() {\n          if (!done) {\n            done = true\n            unsubscribe = undefined\n            controller.close()\n          }\n        },\n      })\n      if (done) {\n        dispose()\n      } else {\n        unsubscribe = dispose\n      }\n    },\n    cancel() {\n      const dispose = unsubscribe\n      unsubscribe = undefined\n      dispose?.()\n    },\n  })\n}\n"],"mappings":";;AAMA,SAAgB,SAAS,OAAmB;CAC1C,MAAM,SAAwB,CAAC;CAC/B,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,OACrC,OAAO,KACL,OAAO,aAAa,MAAM,MAAM,MAAM,SAAS,GAAG,IAAI,KAAM,CAAQ,CACtE;CAEF,OAAO,KAAK,OAAO,KAAK,EA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