-
Notifications
You must be signed in to change notification settings - Fork 577
/
streams.mjs
121 lines (115 loc) · 3.22 KB
/
streams.mjs
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
import * as DenoUnstable from "../../_deno_unstable.ts";
import { Buffer } from "../buffer.ts";
import { Readable, Writable } from "../stream.ts";
import { stdio } from "./stdio.mjs";
// https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41
function createWritableStdioStream(writer, name) {
const stream = new Writable({
write(buf, enc, cb) {
if (!writer) {
this.destroy(
new Error(`Deno.${name} is not available in this environment`),
);
return;
}
writer.writeSync(buf instanceof Uint8Array ? buf : Buffer.from(buf, enc));
cb();
},
destroy(err, cb) {
cb(err);
this._undestroy();
if (!this._writableState.emitClose) {
nextTick(() => this.emit("close"));
}
},
});
stream.fd = writer?.rid ?? -1;
stream.destroySoon = stream.destroy;
stream._isStdio = true;
stream.once("close", () => writer?.close());
Object.defineProperties(stream, {
columns: {
enumerable: true,
configurable: true,
get: () =>
Deno.isatty?.(writer?.rid)
? Deno.consoleSize?.(writer?.rid).columns
: undefined,
},
rows: {
enumerable: true,
configurable: true,
get: () =>
Deno.isatty?.(writer?.rid)
? Deno.consoleSize?.(writer?.rid).rows
: undefined,
},
isTTY: {
enumerable: true,
configurable: true,
get: () => Deno.isatty?.(writer?.rid),
},
getWindowSize: {
enumerable: true,
configurable: true,
value: () =>
Deno.isatty?.(writer?.rid)
? Object.values(Deno.consoleSize?.(writer?.rid))
: undefined,
},
});
return stream;
}
/** https://nodejs.org/api/process.html#process_process_stderr */
export const stderr = stdio.stderr = createWritableStdioStream(
Deno.stderr,
"stderr",
);
/** https://nodejs.org/api/process.html#process_process_stdout */
export const stdout = stdio.stdout = createWritableStdioStream(
Deno.stdout,
"stdout",
);
/** https://nodejs.org/api/process.html#process_process_stdin */
export const stdin = stdio.stdin = new Readable({
highWaterMark: 0,
emitClose: false,
read(size) {
const p = Buffer.alloc(size || 16 * 1024);
if (!Deno.stdin) {
this.destroy(
new Error("Deno.stdin is not available in this environment"),
);
return;
}
Deno.stdin.read(p).then((length) => {
this.push(length === null ? null : p.slice(0, length));
}, (error) => {
this.destroy(error);
});
},
});
stdin.on("close", () => Deno.stdin?.close());
stdin.fd = Deno.stdin?.rid ?? -1;
Object.defineProperty(stdin, "isTTY", {
enumerable: true,
configurable: true,
get() {
return Deno.isatty?.(Deno.stdin.rid);
},
});
stdin._isRawMode = false;
stdin.setRawMode = (enable) => {
DenoUnstable.setRaw?.(Deno.stdin?.rid, enable);
stdin._isRawMode = enable;
return stdin;
};
Object.defineProperty(stdin, "isRaw", {
enumerable: true,
configurable: true,
get() {
return stdin._isRawMode;
},
});