Class LimitedTransformStream<T>

A TransformStream that will only read & enqueue size amount of chunks.

If options.error is set, then instead of terminating the stream, a RangeError will be thrown when the total number of enqueued chunks is about to exceed the specified size.

T The type the chunks in the stream.

import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
import { assertEquals } from "@std/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(
new LimitedTransformStream(2),
);

// All chunks were read
assertEquals(
await Array.fromAsync(transformed),
["1234", "5678"],
);
import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
import { assertEquals } from "@std/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(
new LimitedTransformStream(1),
);

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

about to exceed the specified limit

Do this by setting options.error to true.

import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
import { assertRejects } from "@std/assert";

const stream = ReadableStream.from(["1234", "5678"]);
const transformed = stream.pipeThrough(
new LimitedTransformStream(1, { error: true }),
);

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

Type Parameters

  • T
Hierarchy
  • TransformStream<T, T>
    • LimitedTransformStream

Constructors

Properties

Constructors

Properties

readable: ReadableStream<T>
writable: WritableStream<T>