blob: 8d2cee6e2d613210f430aac3b4b3897e7540c059 (
plain)
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
|
// Ported to Deno from
// 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.
type Reader = Deno.Reader;
/** OneByteReader returns a Reader that implements
* each non-empty Read by reading one byte from r.
*/
export class OneByteReader implements Reader {
constructor(readonly r: Reader) {}
async read(p: Uint8Array): Promise<number | Deno.EOF> {
if (p.byteLength === 0) {
return 0;
}
if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array");
}
return this.r.read(p.subarray(0, 1));
}
}
/** HalfReader returns a Reader that implements Read
* by reading half as many requested bytes from r.
*/
export class HalfReader implements Reader {
constructor(readonly r: Reader) {}
async read(p: Uint8Array): Promise<number | Deno.EOF> {
if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array");
}
const half = Math.floor((p.byteLength + 1) / 2);
return this.r.read(p.subarray(0, half));
}
}
export class ErrTimeout extends Error {
constructor() {
super("timeout");
this.name = "ErrTimeout";
}
}
/** TimeoutReader returns ErrTimeout on the second read
* with no data. Subsequent calls to read succeed.
*/
export class TimeoutReader implements Reader {
count = 0;
constructor(readonly r: Reader) {}
async read(p: Uint8Array): Promise<number | Deno.EOF> {
this.count++;
if (this.count === 2) {
throw new ErrTimeout();
}
return this.r.read(p);
}
}
|