FixedChunkStream

import { FixedChunkStream } from "jsr:@ende/streams/unstable-fixed-chunk-stream";

class ยท experimental

class FixedChunkStream extends TransformStream<Uint8Array, Uint8Array>

A transform stream that resize Uint8Array chunks into perfectly size chunks with the exception of the last chunk.

Example: Usage

import { FixedChunkStream } from "@ende/streams/unstable-fixed-chunk-stream";
import { assertEquals } from "@ende/assert/equals";

const readable = ReadableStream.from(function* () {
  let count = 0
  for (let i = 0; i < 100; ++i) {
    const array = new Uint8Array(Math.floor(Math.random() * 1000));
    count += array.length;
    yield array;
  }
  yield new Uint8Array(512 - count % 512)
}())
  .pipeThrough(new FixedChunkStream(512))
  .pipeTo(new WritableStream({
    write(chunk, _controller) {
      assertEquals(chunk.length, 512)
    }
  }))

Constructors

constructor

new FixedChunkStream(size: number)

Constructs a new instance.

size: number

The size of the chunks to be resized to.

Source