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 LimitedBytesTransformStream}. */
|
|
|
|
export interface LimitedBytesTransformStreamOptions {
|
|
|
|
/**
|
|
|
|
* If true, a {@linkcode RangeError} is thrown when queueing the current chunk
|
|
|
|
* would exceed the specified size limit.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
|
|
|
error?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-12-04 06:12:52 +00:00
|
|
|
/**
|
2024-05-28 01:27:40 +00:00
|
|
|
* A {@linkcode 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 {@linkcode 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
|
|
|
|
* ```ts
|
|
|
|
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-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 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
|
|
|
|
* ```ts
|
|
|
|
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-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 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
|
|
|
|
* ```ts
|
|
|
|
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-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";
|
2022-11-29 13:55:38 +00:00
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* 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());
|
2022-11-29 13:55:38 +00:00
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(transformed),
|
|
|
|
* // The second chunk was not read because it would exceed the specified size
|
|
|
|
* ["1234"],
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
2024-07-18 23:05:16 +00:00
|
|
|
* @example Throw error when the total byte length of the chunks exceeds the
|
|
|
|
* specified size
|
|
|
|
*
|
|
|
|
* To do so, set `options.error` to `true`.
|
|
|
|
*
|
2022-11-29 13:55:38 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-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 TextEncoderStream()).pipeThrough(
|
|
|
|
* new LimitedBytesTransformStream(5, { error: true }),
|
|
|
|
* ).pipeThrough(new TextDecoderStream());
|
2023-12-04 06:12:52 +00:00
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* await assertRejects(async () => {
|
|
|
|
* await Array.fromAsync(transformed);
|
|
|
|
* }, RangeError);
|
2022-11-29 13:55:38 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class LimitedBytesTransformStream
|
|
|
|
extends TransformStream<Uint8Array, Uint8Array> {
|
|
|
|
#read = 0;
|
2023-12-04 06:12:52 +00:00
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new instance.
|
|
|
|
*
|
|
|
|
* @param size A size limit in bytes.
|
|
|
|
* @param options Options for the stream.
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
size: number,
|
|
|
|
options: LimitedBytesTransformStreamOptions = { error: false },
|
|
|
|
) {
|
2022-11-29 13:55:38 +00:00
|
|
|
super({
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
if ((this.#read + chunk.byteLength) > size) {
|
|
|
|
if (options.error) {
|
|
|
|
throw new RangeError(`Exceeded byte size limit of '${size}'`);
|
|
|
|
} else {
|
|
|
|
controller.terminate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.#read += chunk.byteLength;
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|