LimitedBytesTransformStream

import { LimitedBytesTransformStream } from "jsr:@ende/streams";

class

class LimitedBytesTransformStream extends TransformStream<Uint8Array, Uint8Array>

A TransformStream that will only read & enqueue chunks until the total amount of enqueued data exceeds size. The last chunk that would exceed the limit will NOT be enqueued, in which case a RangeError is thrown when options.error is set to true, otherwise the stream is just terminated.

Example: size is equal to the total byte length of the chunks

import { LimitedBytesTransformStream } from "@ende/streams/limited-bytes-transform-stream";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
  new LimitedBytesTransformStream(8),
).pipeThrough(new TextDecoderStream());

assertEquals(
  await Array.fromAsync(transformed),
  ["1234", "5678"],
);

Example: size is less than the total byte length of the chunks, and at the

boundary of the chunks

import { LimitedBytesTransformStream } from "@ende/streams/limited-bytes-transform-stream";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
  // `4` is the boundary of the chunks
  new LimitedBytesTransformStream(4),
).pipeThrough(new TextDecoderStream());

assertEquals(
  await Array.fromAsync(transformed),
  // The first chunk was read, but the second chunk was not
  ["1234"],
);

Example: size is less than the total byte length of the chunks, and not at

the boundary of the chunks

import { LimitedBytesTransformStream } from "@ende/streams/limited-bytes-transform-stream";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
  // `5` is not the boundary of the chunks
  new LimitedBytesTransformStream(5),
).pipeThrough(new TextDecoderStream());

assertEquals(
  await Array.fromAsync(transformed),
  // The second chunk was not read because it would exceed the specified size
  ["1234"],
);

Example: Throw error when the total byte length of the chunks exceeds the

specified size

To do so, set options.error to true.

import { LimitedBytesTransformStream } from "@ende/streams/limited-bytes-transform-stream";
import { assertRejects } from "@ende/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
  new LimitedBytesTransformStream(5, { error: true }),
).pipeThrough(new TextDecoderStream());

await assertRejects(async () => {
  await Array.fromAsync(transformed);
}, RangeError);

Constructors

constructor

new LimitedBytesTransformStream(size: number, options: LimitedBytesTransformStreamOptions = [UNSUPPORTED])

Constructs a new instance.

size: number

A size limit in bytes.

options: LimitedBytesTransformStreamOptions = [UNSUPPORTED]

Options for the stream.

Source