import { Buffer } from "./deps.ts";
export class TransformChunkSizes extends TransformStream<Uint8Array, Uint8Array> { constructor(outChunkSize: number) { const buffer = new Buffer(); buffer.grow(outChunkSize);
super({ start() {}, async transform(chunk, controller) { buffer.write(chunk);
while (buffer.length >= outChunkSize) { const outChunk = new Uint8Array(outChunkSize); const readFromBuffer = await buffer.read(outChunk); if (readFromBuffer !== outChunkSize) { throw new Error( `Unexpectedly read ${readFromBuffer} bytes from transform buffer when trying to read ${outChunkSize} bytes.`, ); } controller.enqueue(outChunk); } }, flush(controller) { if (buffer.length) { controller.enqueue(buffer.bytes()); } }, }); }}