forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf_reader.ts
182 lines (156 loc) · 4.63 KB
/
buf_reader.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import { assert } from "./deps.ts";
import { stripEol } from "./util.ts";
export interface ReadLineResult {
bytes: Uint8Array;
eol: boolean;
}
const DEFAULT_BUF_SIZE = 4096;
const MIN_BUF_SIZE = 16;
const MAX_CONSECUTIVE_EMPTY_READS = 100;
const CR = "\r".charCodeAt(0);
const LF = "\n".charCodeAt(0);
export class BufferFullError extends Error {
name = "BufferFullError";
constructor(public partial: Uint8Array) {
super("Buffer full");
}
}
/** BufReader implements buffering for a Reader object. */
export class BufReader {
#buffer!: Uint8Array;
#reader!: Deno.Reader;
#posRead = 0;
#posWrite = 0;
#eof = false;
// Reads a new chunk into the buffer.
#fill = async (): Promise<void> => {
// Slide existing data to beginning.
if (this.#posRead > 0) {
this.#buffer.copyWithin(0, this.#posRead, this.#posWrite);
this.#posWrite -= this.#posRead;
this.#posRead = 0;
}
if (this.#posWrite >= this.#buffer.byteLength) {
throw Error("bufio: tried to fill full buffer");
}
// Read new data: try a limited number of times.
for (let i = MAX_CONSECUTIVE_EMPTY_READS; i > 0; i--) {
const rr = await this.#reader.read(this.#buffer.subarray(this.#posWrite));
if (rr === null) {
this.#eof = true;
return;
}
assert(rr >= 0, "negative read");
this.#posWrite += rr;
if (rr > 0) {
return;
}
}
throw new Error(
`No progress after ${MAX_CONSECUTIVE_EMPTY_READS} read() calls`,
);
};
#reset = (buffer: Uint8Array, reader: Deno.Reader): void => {
this.#buffer = buffer;
this.#reader = reader;
this.#eof = false;
};
constructor(rd: Deno.Reader, size: number = DEFAULT_BUF_SIZE) {
if (size < MIN_BUF_SIZE) {
size = MIN_BUF_SIZE;
}
this.#reset(new Uint8Array(size), rd);
}
buffered(): number {
return this.#posWrite - this.#posRead;
}
async readLine(
strip = true,
): Promise<{ bytes: Uint8Array; eol: boolean } | null> {
let line: Uint8Array | null;
try {
line = await this.readSlice(LF);
} catch (err) {
let { partial } = err;
assert(
partial instanceof Uint8Array,
"Caught error from `readSlice()` without `partial` property",
);
// Don't throw if `readSlice()` failed with `BufferFullError`, instead we
// just return whatever is available and set the `more` flag.
if (!(err instanceof BufferFullError)) {
throw err;
}
// Handle the case where "\r\n" straddles the buffer.
if (
!this.#eof &&
partial.byteLength > 0 &&
partial[partial.byteLength - 1] === CR
) {
// Put the '\r' back on buf and drop it from line.
// Let the next call to ReadLine check for "\r\n".
assert(
this.#posRead > 0,
"Tried to rewind past start of buffer",
);
this.#posRead--;
partial = partial.subarray(0, partial.byteLength - 1);
}
return { bytes: partial, eol: this.#eof };
}
if (line === null) {
return null;
}
if (line.byteLength === 0) {
return { bytes: line, eol: true };
}
if (strip) {
line = stripEol(line);
}
return { bytes: line, eol: true };
}
async readSlice(delim: number): Promise<Uint8Array | null> {
let s = 0; // search start index
let slice: Uint8Array | undefined;
while (true) {
// Search buffer.
let i = this.#buffer.subarray(this.#posRead + s, this.#posWrite).indexOf(
delim,
);
if (i >= 0) {
i += s;
slice = this.#buffer.subarray(this.#posRead, this.#posRead + i + 1);
this.#posRead += i + 1;
break;
}
// EOF?
if (this.#eof) {
if (this.#posRead === this.#posWrite) {
return null;
}
slice = this.#buffer.subarray(this.#posRead, this.#posWrite);
this.#posRead = this.#posWrite;
break;
}
// Buffer full?
if (this.buffered() >= this.#buffer.byteLength) {
this.#posRead = this.#posWrite;
// #4521 The internal buffer should not be reused across reads because it causes corruption of data.
const oldbuf = this.#buffer;
const newbuf = this.#buffer.slice(0);
this.#buffer = newbuf;
throw new BufferFullError(oldbuf);
}
s = this.#posWrite - this.#posRead; // do not rescan area we scanned before
// Buffer is not full.
try {
await this.#fill();
} catch (err) {
err.partial = slice;
throw err;
}
}
return slice;
}
}