deno.land / std@0.167.0 / streams / limited_transform_stream.ts

limited_transform_stream.ts
نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** A TransformStream that will only read & enqueue `size` amount of chunks. * * if options.error is set, then instead of terminating the stream, * an error will be thrown. * * ```ts * import { LimitedTransformStream } from "https://deno.land/std@$STD_VERSION/streams/limited_transform_stream.ts"; * const res = await fetch("https://example.com"); * const parts = res.body!.pipeThrough(new LimitedTransformStream(50)); * ``` */export class LimitedTransformStream<T> extends TransformStream<T, T> { #read = 0; constructor(size: number, options: { error?: boolean } = {}) { 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); } }, }); }}
std

Version Info

Tagged at
a year ago