deno.land / std@0.224.0 / streams / limited_bytes_transform_stream.ts

limited_bytes_transform_stream.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** * A {@linkcode TransformStream} that will only read & enqueue `size` amount of * bytes. This operation is chunk based and not BYOB based, and as such will * read more than needed. * * If `options.error` is set, then instead of terminating the stream, * an error will be thrown. * * @example * ```ts * import { LimitedBytesTransformStream } from "https://deno.land/std@$STD_VERSION/streams/limited_bytes_transform_stream.ts"; * * const res = await fetch("https://example.com"); * const parts = res.body! * .pipeThrough(new LimitedBytesTransformStream(512 * 1024)); * ``` */export class LimitedBytesTransformStream extends TransformStream<Uint8Array, Uint8Array> { #read = 0;
/** Constructs a new instance. */ constructor(size: number, options: { error?: boolean } = {}) { 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); } }, }); }}
std

Version Info

Tagged at
6 months ago