2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/** Options for {@linkcode LimitedTransformStream} */
|
|
|
|
export interface LimitedTransformStreamOptions {
|
|
|
|
/**
|
|
|
|
* If true, a {@linkcode RangeError} is thrown when the total number of
|
|
|
|
* enqueued chunks is about to exceed the specified limit.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
|
|
|
error?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-12-04 06:12:52 +00:00
|
|
|
/**
|
|
|
|
* A {@linkcode TransformStream} that will only read & enqueue `size` amount of
|
|
|
|
* chunks.
|
2022-11-29 13:55:38 +00:00
|
|
|
*
|
2023-12-04 06:12:52 +00:00
|
|
|
* If `options.error` is set, then instead of terminating the stream,
|
2024-05-28 01:27:40 +00:00
|
|
|
* a {@linkcode RangeError} will be thrown when the total number of enqueued
|
|
|
|
* chunks is about to exceed the specified size.
|
|
|
|
*
|
|
|
|
* @typeparam T The type the chunks in the stream.
|
|
|
|
*
|
|
|
|
* @example `size` is equal to the total number of chunks
|
|
|
|
* ```ts
|
|
|
|
* import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* const stream = ReadableStream.from(["1234", "5678"]);
|
|
|
|
* const transformed = stream.pipeThrough(
|
|
|
|
* new LimitedTransformStream(2),
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* // All chunks were read
|
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(transformed),
|
|
|
|
* ["1234", "5678"],
|
|
|
|
* );
|
|
|
|
* ```
|
2022-11-29 13:55:38 +00:00
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* @example `size` is less than the total number of chunks
|
2022-11-29 13:55:38 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* 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"],
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
2024-07-18 23:05:16 +00:00
|
|
|
* @example Throw a {@linkcode RangeError} when the total number of chunks is
|
|
|
|
* about to exceed the specified limit
|
|
|
|
*
|
|
|
|
* Do this by setting `options.error` to `true`.
|
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* ```ts
|
|
|
|
* import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertRejects } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* const stream = ReadableStream.from(["1234", "5678"]);
|
|
|
|
* const transformed = stream.pipeThrough(
|
|
|
|
* new LimitedTransformStream(1, { error: true }),
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* await assertRejects(async () => {
|
|
|
|
* await Array.fromAsync(transformed);
|
|
|
|
* }, RangeError);
|
2022-11-29 13:55:38 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class LimitedTransformStream<T> extends TransformStream<T, T> {
|
|
|
|
#read = 0;
|
2023-12-04 06:12:52 +00:00
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new instance.
|
|
|
|
*
|
|
|
|
* @param size The maximum number of chunks to read.
|
|
|
|
* @param options Options for the stream.
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
size: number,
|
|
|
|
options: LimitedTransformStreamOptions = { error: false },
|
|
|
|
) {
|
2022-11-29 13:55:38 +00:00
|
|
|
super({
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
if ((this.#read + 1) > size) {
|
|
|
|
if (options.error) {
|
|
|
|
throw new RangeError(`Exceeded chunk limit of '${size}'`);
|
|
|
|
} else {
|
|
|
|
controller.terminate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.#read++;
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|