serverside client chunking impl (CURSED)

This commit is contained in:
Spencer Pogorzelski 2023-08-14 18:30:33 -07:00
parent a839d0fe51
commit b73a561844
5 changed files with 51 additions and 20 deletions

View file

@ -7,7 +7,6 @@ import {
ReadyStateCallback,
WebSocketImpl,
} from "bare-client-custom";
import { ReadableStream, TransformStream } from "node:stream/web";
import { MAX_CHUNK_SIZE } from "protocol";
import { Connection } from "./Connection";
@ -23,6 +22,17 @@ function createBodyStream(
): ReadableStream<ArrayBuffer | Uint8Array> | null {
if (body === null) return null;
if (typeof body === "string") {
body = new TextEncoder().encode(body);
}
if (ArrayBuffer.isView(body)) {
body = body.buffer.slice(
body.byteOffset,
body.byteOffset + body.byteLength
);
}
const transformer = () =>
new TransformStream({
transform: async (
@ -82,7 +92,6 @@ function createBodyStream(
}
if (body instanceof Blob) {
// @ts-expect-error
return body.stream().pipeThrough(transformer());
}