deno.land / std@0.224.0 / io / limited_reader.ts

limited_reader.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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** * A `LimitedReader` reads from `reader` but limits the amount of data returned to just `limit` bytes. * Each call to `read` updates `limit` to reflect the new amount remaining. * `read` returns `null` when `limit` <= `0` or * when the underlying `reader` returns `null`. */import type { Reader } from "./types.ts";
/** * @deprecated This will be removed in 1.0.0. Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead. */export class LimitedReader implements Reader { constructor(public reader: Reader, public limit: number) {}
async read(p: Uint8Array): Promise<number | null> { if (this.limit <= 0) { return null; }
if (p.length > this.limit) { p = p.subarray(0, this.limit); } const n = await this.reader.read(p); if (n === null) { return null; }
this.limit -= n; return n; }}
std

Version Info

Tagged at
6 months ago