deno.land / std@0.167.0 / io / readers.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Based on https://github.com/golang/go/blob/0452f9460f50f0f0aba18df43dc2b31906fb66cc/src/io/io.go// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
import { Buffer } from "./buffer.ts";
/** * Reader utility for strings. * * @example * ```ts * import { StringReader } from "https://deno.land/std@$STD_VERSION/io/mod.ts"; * * const data = new Uint8Array(6); * const r = new StringReader("abcdef"); * const res0 = await r.read(data); * const res1 = await r.read(new Uint8Array(6)); * * // Number of bytes read * console.log(res0); // 6 * console.log(res1); // null, no byte left to read. EOL * * // text * * console.log(new TextDecoder().decode(data)); // abcdef * ``` * * **Output:** * * ```text * 6 * null * abcdef * ``` */export class StringReader extends Buffer { constructor(s: string) { super(new TextEncoder().encode(s).buffer); }}
/** Reader utility for combining multiple readers */export class MultiReader implements Deno.Reader { readonly #readers: Deno.Reader[]; #currentIndex = 0;
constructor(readers: Deno.Reader[]) { this.#readers = [...readers]; }
async read(p: Uint8Array): Promise<number | null> { const r = this.#readers[this.#currentIndex]; if (!r) return null; const result = await r.read(p); if (result === null) { this.#currentIndex++; return 0; } return result; }}
/** * 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`. */export class LimitedReader implements Deno.Reader { constructor(public reader: Deno.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
a year ago