-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy path12_io.js
268 lines (217 loc) · 4.62 KB
/
12_io.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2018-2025 the Deno authors. MIT license.
// Interfaces 100% copied from Go.
// Documentation liberally lifted from them too.
// Thank you! We love Go! <3
import { core, primordials } from "ext:core/mod.js";
import { op_set_raw } from "ext:core/ops";
const {
Uint8Array,
ArrayPrototypePush,
Symbol,
TypedArrayPrototypeSubarray,
TypedArrayPrototypeSet,
TypedArrayPrototypeGetByteLength,
} = primordials;
import {
readableStreamForRid,
writableStreamForRid,
} from "ext:deno_web/06_streams.js";
// Seek whence values.
// https://golang.org/pkg/io/#pkg-constants
const SeekMode = {
0: "Start",
1: "Current",
2: "End",
Start: 0,
Current: 1,
End: 2,
};
function readSync(rid, buffer) {
if (buffer.length === 0) return 0;
const nread = core.readSync(rid, buffer);
return nread === 0 ? null : nread;
}
async function read(rid, buffer) {
if (buffer.length === 0) return 0;
const nread = await core.read(rid, buffer);
return nread === 0 ? null : nread;
}
function writeSync(rid, data) {
return core.writeSync(rid, data);
}
function write(rid, data) {
return core.write(rid, data);
}
const READ_PER_ITER = 64 * 1024; // 64kb
async function readAll(r) {
const buffers = [];
while (true) {
const buf = new Uint8Array(READ_PER_ITER);
const read = await r.read(buf);
if (typeof read == "number") {
ArrayPrototypePush(buffers, TypedArrayPrototypeSubarray(buf, 0, read));
} else {
break;
}
}
return concatBuffers(buffers);
}
function readAllSync(r) {
const buffers = [];
while (true) {
const buf = new Uint8Array(READ_PER_ITER);
const read = r.readSync(buf);
if (typeof read == "number") {
ArrayPrototypePush(buffers, TypedArrayPrototypeSubarray(buf, 0, read));
} else {
break;
}
}
return concatBuffers(buffers);
}
function concatBuffers(buffers) {
let totalLen = 0;
for (let i = 0; i < buffers.length; ++i) {
totalLen += TypedArrayPrototypeGetByteLength(buffers[i]);
}
const contents = new Uint8Array(totalLen);
let n = 0;
for (let i = 0; i < buffers.length; ++i) {
const buf = buffers[i];
TypedArrayPrototypeSet(contents, buf, n);
n += TypedArrayPrototypeGetByteLength(buf);
}
return contents;
}
const STDIN_RID = 0;
const STDOUT_RID = 1;
const STDERR_RID = 2;
const REF = Symbol("REF");
const UNREF = Symbol("UNREF");
class Stdin {
#rid = STDIN_RID;
#ref = true;
#readable;
#opPromise;
constructor() {
}
get rid() {
return this.#rid;
}
async read(p) {
if (p.length === 0) return 0;
this.#opPromise = core.read(this.#rid, p);
if (!this.#ref) {
core.unrefOpPromise(this.#opPromise);
}
const nread = await this.#opPromise;
return nread === 0 ? null : nread;
}
readSync(p) {
return readSync(this.#rid, p);
}
close() {
core.tryClose(this.#rid);
}
get readable() {
if (this.#readable === undefined) {
this.#readable = readableStreamForRid(this.#rid);
}
return this.#readable;
}
setRaw(mode, options = { __proto__: null }) {
const cbreak = !!(options.cbreak ?? false);
op_set_raw(this.#rid, mode, cbreak);
}
isTerminal() {
return core.isTerminal(this.#rid);
}
[REF]() {
this.#ref = true;
if (this.#opPromise) {
core.refOpPromise(this.#opPromise);
}
}
[UNREF]() {
this.#ref = false;
if (this.#opPromise) {
core.unrefOpPromise(this.#opPromise);
}
}
}
class Stdout {
#rid = STDOUT_RID;
#writable;
constructor() {
}
get rid() {
return this.#rid;
}
write(p) {
return write(this.#rid, p);
}
writeSync(p) {
return writeSync(this.#rid, p);
}
close() {
core.close(this.#rid);
}
get writable() {
if (this.#writable === undefined) {
this.#writable = writableStreamForRid(this.#rid);
}
return this.#writable;
}
isTerminal() {
return core.isTerminal(this.#rid);
}
}
class Stderr {
#rid = STDERR_RID;
#writable;
constructor() {
}
get rid() {
return this.#rid;
}
write(p) {
return write(this.#rid, p);
}
writeSync(p) {
return writeSync(this.#rid, p);
}
close() {
core.close(this.#rid);
}
get writable() {
if (this.#writable === undefined) {
this.#writable = writableStreamForRid(this.#rid);
}
return this.#writable;
}
isTerminal() {
return core.isTerminal(this.#rid);
}
}
const stdin = new Stdin();
const stdout = new Stdout();
const stderr = new Stderr();
export {
read,
readAll,
readAllSync,
readSync,
REF,
SeekMode,
Stderr,
stderr,
STDERR_RID,
stdin,
STDIN_RID,
Stdout,
stdout,
STDOUT_RID,
UNREF,
write,
writeSync,
};