-
-
Notifications
You must be signed in to change notification settings - Fork 35k
Expand file tree
/
Copy pathstreams.mjs
More file actions
245 lines (219 loc) · 6.86 KB
/
Copy pathstreams.mjs
File metadata and controls
245 lines (219 loc) · 6.86 KB
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
/**
* This is a pared down version of
* https://github.com/pyodide/pyodide/blob/main/src/js/streams.ts
*
* It replaces the standard streams devices that Emscripten provides with our
* own better ones. It fixes the following deficiencies:
*
* 1. The emscripten std streams always have isatty set to true. These set
* isatty to match the value for the stdin/stdout/stderr that node sees.
* 2. The emscripten std streams don't support the ttygetwinsize ioctl. If
* isatty() returns true, then these do, and it returns the actual window
* size as the OS reports it to Node.
* 3. The emscripten std streams introduce an extra layer of buffering which has
* to be flushed with fsync().
* 4. The emscripten std streams are slow and complex because they go through a
* character-based handler layer. This is particularly awkward because both
* sides of this character based layer deal with buffers and so we need
* complex adaptors, buffering, etc on both sides. Removing this
* character-based middle layer makes everything better.
* https://github.com/emscripten-core/emscripten/blob/1aa7fb531f11e11e7ae49b75a24e1a8fe6fa4a7d/src/lib/libtty.js?plain=1#L104-L114
*
* Ideally some version of this should go upstream to Emscripten since it is not
* in any way specific to Python. But I (Hood) haven't gotten around to it yet.
*/
import * as tty from "node:tty";
import * as fs from "node:fs";
let FS;
const DEVOPS = {};
const DEVS = {};
function isErrnoError(e) {
return e && typeof e === "object" && "errno" in e;
}
const waitBuffer = new Int32Array(
new WebAssembly.Memory({ shared: true, initial: 1, maximum: 1 }).buffer,
);
function syncSleep(timeout) {
try {
Atomics.wait(waitBuffer, 0, 0, timeout);
return true;
} catch (_) {
return false;
}
}
/**
* Calls the callback and handle node EAGAIN errors.
*/
function handleEAGAIN(cb) {
while (true) {
try {
return cb();
} catch (e) {
if (e && e.code === "EAGAIN") {
// Presumably this means we're in node and tried to read from/write to
// an O_NONBLOCK file descriptor. Synchronously sleep for 10ms then try
// again. In case for some reason we fail to sleep, propagate the error
// (it will turn into an EOFError).
if (syncSleep(10)) {
continue;
}
}
throw e;
}
}
}
function readWriteHelper(stream, cb, method) {
let nbytes;
try {
nbytes = handleEAGAIN(cb);
} catch (e) {
if (e && e.code && Module.ERRNO_CODES[e.code]) {
throw new FS.ErrnoError(Module.ERRNO_CODES[e.code]);
}
if (isErrnoError(e)) {
// the handler set an errno, propagate it
throw e;
}
console.error("Error thrown in read:");
console.error(e);
throw new FS.ErrnoError(Module.ERRNO_CODES.EIO);
}
if (nbytes === undefined) {
// Prevent an infinite loop caused by incorrect code that doesn't return a
// value.
// Maybe we should set nbytes = buffer.length here instead?
console.warn(
`${method} returned undefined; a correct implementation must return a number`,
);
throw new FS.ErrnoError(Module.ERRNO_CODES.EIO);
}
if (nbytes !== 0) {
stream.node.timestamp = Date.now();
}
return nbytes;
}
function asUint8Array(arg) {
if (ArrayBuffer.isView(arg)) {
return new Uint8Array(arg.buffer, arg.byteOffset, arg.byteLength);
} else {
return new Uint8Array(arg);
}
}
const prepareBuffer = (buffer, offset, length) =>
asUint8Array(buffer).subarray(offset, offset + length);
const TTY_OPS = {
ioctl_tiocgwinsz(tty) {
return tty.devops.ioctl_tiocgwinsz?.() ?? [24, 80];
},
};
const stream_ops = {
open: function (stream) {
const devops = DEVOPS[stream.node.rdev];
if (!devops) {
throw new FS.ErrnoError(Module.ERRNO_CODES.ENODEV);
}
stream.devops = devops;
stream.tty = stream.devops.isatty ? { ops: TTY_OPS, devops } : undefined;
stream.seekable = false;
},
close: function (stream) {
// flush any pending line data
stream.stream_ops.fsync(stream);
},
fsync: function (stream) {
const ops = stream.devops;
if (ops.fsync) {
ops.fsync();
}
},
read: function (stream, buffer, offset, length, pos /* ignored */) {
buffer = prepareBuffer(buffer, offset, length);
return readWriteHelper(stream, () => stream.devops.read(buffer), "read");
},
write: function (stream, buffer, offset, length, pos /* ignored */) {
buffer = prepareBuffer(buffer, offset, length);
return readWriteHelper(stream, () => stream.devops.write(buffer), "write");
},
};
function nodeFsync(fd) {
try {
fs.fsyncSync(fd);
} catch (e) {
if (e?.code === "EINVAL") {
return;
}
// On Mac, calling fsync when not isatty returns ENOTSUP
// On Windows, stdin/stdout/stderr may be closed, returning EBADF or EPERM
if (
e?.code === "ENOTSUP" || e?.code === "EBADF" || e?.code === "EPERM"
) {
return;
}
throw e;
}
}
class NodeReader {
constructor(nodeStream) {
this.nodeStream = nodeStream;
this.isatty = tty.isatty(nodeStream.fd);
}
read(buffer) {
try {
return fs.readSync(this.nodeStream.fd, buffer);
} catch (e) {
// Platform differences: on Windows, reading EOF throws an exception,
// but on other OSes, reading EOF returns 0. Uniformize behavior by
// catching the EOF exception and returning 0.
if (e.toString().includes("EOF")) {
return 0;
}
throw e;
}
}
fsync() {
nodeFsync(this.nodeStream.fd);
}
ioctl_tiocgwinsz() {
return [this.nodeStream.rows ?? 24, this.nodeStream.columns ?? 80];
}
}
class NodeWriter {
constructor(nodeStream) {
this.nodeStream = nodeStream;
this.isatty = tty.isatty(nodeStream.fd);
}
write(buffer) {
return fs.writeSync(this.nodeStream.fd, buffer);
}
fsync() {
nodeFsync(this.nodeStream.fd);
}
ioctl_tiocgwinsz() {
return [this.nodeStream.rows ?? 24, this.nodeStream.columns ?? 80];
}
}
export function initializeStreams(fsarg) {
FS = fsarg;
const major = FS.createDevice.major++;
DEVS.stdin = FS.makedev(major, 0);
DEVS.stdout = FS.makedev(major, 1);
DEVS.stderr = FS.makedev(major, 2);
FS.registerDevice(DEVS.stdin, stream_ops);
FS.registerDevice(DEVS.stdout, stream_ops);
FS.registerDevice(DEVS.stderr, stream_ops);
FS.unlink("/dev/stdin");
FS.unlink("/dev/stdout");
FS.unlink("/dev/stderr");
FS.mkdev("/dev/stdin", DEVS.stdin);
FS.mkdev("/dev/stdout", DEVS.stdout);
FS.mkdev("/dev/stderr", DEVS.stderr);
DEVOPS[DEVS.stdin] = new NodeReader(process.stdin);
DEVOPS[DEVS.stdout] = new NodeWriter(process.stdout);
DEVOPS[DEVS.stderr] = new NodeWriter(process.stderr);
FS.closeStream(0 /* stdin */);
FS.closeStream(1 /* stdout */);
FS.closeStream(2 /* stderr */);
FS.open("/dev/stdin", 0 /* O_RDONLY */);
FS.open("/dev/stdout", 1 /* O_WRONLY */);
FS.open("/dev/stderr", 1 /* O_WRONLY */);
}