@tanstack/router-core
Version:
Modern and scalable routing for React applications
127 lines (126 loc) • 3.31 kB
JavaScript
let seroval = require("seroval");
//#region src/ssr/serializer/rawStreamCodec.ts
function toBase64(bytes) {
const chunks = [];
for (let i = 0; i < bytes.length; i += 32768) chunks.push(String.fromCharCode.apply(null, bytes.subarray(i, i + 32768)));
return btoa(chunks.join(""));
}
function fromBase64(value) {
const binary = atob(value);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
return bytes;
}
const textDecoder = /* @__PURE__ */ new TextDecoder("utf-8", {
fatal: true,
ignoreBOM: true
});
/** `'t' + utf8` for a valid UTF-8 chunk, otherwise `'b' + base64`. */
function encodeText(value) {
try {
return "t" + textDecoder.decode(value);
} catch {
return "b" + toBase64(value);
}
}
const textEncoder = /* @__PURE__ */ new TextEncoder();
function decodeText(value) {
const data = value.slice(1);
return value[0] === "t" ? textEncoder.encode(data) : fromBase64(data);
}
/**
* Pump a byte stream into a Seroval stream, one encoded chunk per read.
* Returns the Seroval stream and a `stop` function that cancels the reader
* without signalling the Seroval stream; the abort signal and read failures
* stop the pump and throw through the Seroval stream.
*/
function pumpEncodedStream(readable, encode, signal) {
signal?.throwIfAborted();
const stream = (0, seroval.createStream)();
const reader = readable.getReader();
let active = true;
const release = () => {
active = false;
signal?.removeEventListener("abort", abort);
reader.releaseLock();
};
const stop = (reason) => {
if (!active) return false;
reader.cancel(reason).catch(() => {});
release();
return true;
};
const abort = () => {
if (stop(signal.reason)) stream.throw(signal.reason);
};
signal?.addEventListener("abort", abort);
(async () => {
try {
while (active) {
const { done, value } = await reader.read();
if (!active) return;
if (done) {
release();
stream.return(void 0);
return;
}
stream.next(encode(value));
}
} catch (error) {
if (stop(error)) stream.throw(error);
}
})();
return [stream, stop];
}
/** Rebuild a byte stream from encoded Seroval stream chunks. */
function fromEncodedStream(source, decode) {
let unsubscribe;
let done = false;
return new ReadableStream({
start(controller) {
const dispose = source.on({
next(value) {
if (done) return;
try {
controller.enqueue(decode(value));
} catch (error) {
done = true;
const stop = unsubscribe;
unsubscribe = void 0;
stop?.();
controller.error(error);
}
},
throw(error) {
if (!done) {
done = true;
unsubscribe = void 0;
controller.error(error);
}
},
return() {
if (!done) {
done = true;
unsubscribe = void 0;
controller.close();
}
}
});
if (done) dispose();
else unsubscribe = dispose;
},
cancel() {
const dispose = unsubscribe;
unsubscribe = void 0;
dispose?.();
}
});
}
//#endregion
exports.decodeText = decodeText;
exports.encodeText = encodeText;
exports.fromBase64 = fromBase64;
exports.fromEncodedStream = fromEncodedStream;
exports.pumpEncodedStream = pumpEncodedStream;
exports.toBase64 = toBase64;
//# sourceMappingURL=rawStreamCodec.cjs.map