2024-08-12 12:41:32 -04:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-14 11:38:45 -05:00
|
|
|
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
|
|
|
|
|
2024-06-20 03:26:34 -04:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
|
|
const {
|
|
|
|
Uint8ArrayPrototype,
|
|
|
|
Error,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
TypedArrayPrototypeSlice,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
ObjectValues,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
} = primordials;
|
2023-06-27 02:18:22 -04:00
|
|
|
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
clearLine,
|
|
|
|
clearScreenDown,
|
|
|
|
cursorTo,
|
|
|
|
moveCursor,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/readline/callbacks.mjs";
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Duplex, Readable, Writable } from "node:stream";
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as io from "ext:deno_io/12_io.js";
|
2023-10-30 11:53:08 -04:00
|
|
|
import { guessHandleType } from "ext:deno_node/internal_binding/util.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
// https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41
|
2024-03-22 16:49:07 -04:00
|
|
|
export function createWritableStdioStream(writer, name, warmup = false) {
|
2023-02-14 11:38:45 -05:00
|
|
|
const stream = new Writable({
|
2023-06-02 01:36:51 -04:00
|
|
|
emitClose: false,
|
2023-02-14 11:38:45 -05:00
|
|
|
write(buf, enc, cb) {
|
|
|
|
if (!writer) {
|
|
|
|
this.destroy(
|
|
|
|
new Error(`Deno.${name} is not available in this environment`),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-20 03:26:34 -04:00
|
|
|
writer.writeSync(
|
|
|
|
ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, buf)
|
|
|
|
? buf
|
|
|
|
: Buffer.from(buf, enc),
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
cb();
|
|
|
|
},
|
|
|
|
destroy(err, cb) {
|
|
|
|
cb(err);
|
|
|
|
this._undestroy();
|
|
|
|
if (!this._writableState.emitClose) {
|
|
|
|
nextTick(() => this.emit("close"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2024-01-24 14:09:49 -05:00
|
|
|
let fd = -1;
|
|
|
|
|
2024-06-20 03:26:34 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2024-01-24 14:09:49 -05:00
|
|
|
if (writer instanceof io.Stdout) {
|
|
|
|
fd = io.STDOUT_RID;
|
2024-06-20 03:26:34 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2024-01-24 14:09:49 -05:00
|
|
|
} else if (writer instanceof io.Stderr) {
|
|
|
|
fd = io.STDERR_RID;
|
|
|
|
}
|
|
|
|
stream.fd = fd;
|
2023-02-14 11:38:45 -05:00
|
|
|
stream.destroySoon = stream.destroy;
|
|
|
|
stream._isStdio = true;
|
|
|
|
stream.once("close", () => writer?.close());
|
2024-10-11 13:14:10 -04:00
|
|
|
|
|
|
|
// We cannot call `writer?.isTerminal()` eagerly here
|
|
|
|
let getIsTTY = () => writer?.isTerminal();
|
|
|
|
|
2024-06-20 03:26:34 -04:00
|
|
|
ObjectDefineProperties(stream, {
|
2023-02-14 11:38:45 -05:00
|
|
|
columns: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-14 11:38:45 -05:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: () =>
|
2024-01-23 18:01:56 -05:00
|
|
|
writer?.isTerminal() ? Deno.consoleSize?.().columns : undefined,
|
2023-02-14 11:38:45 -05:00
|
|
|
},
|
|
|
|
rows: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-14 11:38:45 -05:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2024-01-23 18:01:56 -05:00
|
|
|
get: () => writer?.isTerminal() ? Deno.consoleSize?.().rows : undefined,
|
2023-02-14 11:38:45 -05:00
|
|
|
},
|
|
|
|
isTTY: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-14 11:38:45 -05:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2024-10-11 13:14:10 -04:00
|
|
|
// Allow users to overwrite it
|
|
|
|
get: () => getIsTTY(),
|
|
|
|
set: (value) => {
|
|
|
|
getIsTTY = () => value;
|
|
|
|
},
|
2023-02-14 11:38:45 -05:00
|
|
|
},
|
|
|
|
getWindowSize: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-14 11:38:45 -05:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: () =>
|
2024-06-20 03:26:34 -04:00
|
|
|
writer?.isTerminal() ? ObjectValues(Deno.consoleSize?.()) : undefined,
|
2023-02-14 11:38:45 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-03-22 16:49:07 -04:00
|
|
|
// If we're warming up, create a stdout/stderr stream that assumes a terminal (the most likely case).
|
|
|
|
// If we're wrong at boot time, we'll recreate it.
|
|
|
|
if (warmup || writer?.isTerminal()) {
|
2023-02-14 11:38:45 -05:00
|
|
|
// These belong on tty.WriteStream(), but the TTY streams currently have
|
|
|
|
// following problems:
|
|
|
|
// 1. Using them here introduces a circular dependency.
|
|
|
|
// 2. Creating a net.Socket() from a fd is not currently supported.
|
|
|
|
stream.cursorTo = function (x, y, callback) {
|
|
|
|
return cursorTo(this, x, y, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
stream.moveCursor = function (dx, dy, callback) {
|
|
|
|
return moveCursor(this, dx, dy, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
stream.clearLine = function (dir, callback) {
|
|
|
|
return clearLine(this, dir, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
stream.clearScreenDown = function (callback) {
|
|
|
|
return clearScreenDown(this, callback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _guessStdinType(fd) {
|
|
|
|
if (typeof fd !== "number" || fd < 0) return "UNKNOWN";
|
2023-10-30 11:53:08 -04:00
|
|
|
return guessHandleType(fd);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const _read = function (size) {
|
|
|
|
const p = Buffer.alloc(size || 16 * 1024);
|
2024-06-20 03:26:34 -04:00
|
|
|
PromisePrototypeThen(io.stdin?.read(p), (length) => {
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
this.push(length === null ? null : TypedArrayPrototypeSlice(p, 0, length));
|
|
|
|
}, (error) => {
|
|
|
|
this.destroy(error);
|
|
|
|
});
|
2023-02-14 11:38:45 -05:00
|
|
|
};
|
|
|
|
|
2023-10-31 07:54:43 -04:00
|
|
|
let readStream;
|
|
|
|
export function setReadStream(s) {
|
|
|
|
readStream = s;
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
/** https://nodejs.org/api/process.html#process_process_stdin */
|
|
|
|
// https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L189
|
2023-02-16 08:30:14 -05:00
|
|
|
/** Create process.stdin */
|
2024-03-22 16:49:07 -04:00
|
|
|
export const initStdin = (warmup = false) => {
|
2024-01-24 14:09:49 -05:00
|
|
|
const fd = io.stdin ? io.STDIN_RID : undefined;
|
2023-02-16 08:30:14 -05:00
|
|
|
let stdin;
|
2024-03-22 16:49:07 -04:00
|
|
|
// Warmup assumes a TTY for all stdio
|
|
|
|
const stdinType = warmup ? "TTY" : _guessStdinType(fd);
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
switch (stdinType) {
|
|
|
|
case "FILE": {
|
|
|
|
// Since `fs.ReadStream` cannot be imported before process initialization,
|
|
|
|
// use `Readable` instead.
|
|
|
|
// https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L200
|
|
|
|
// https://github.com/nodejs/node/blob/v18.12.1/lib/internal/fs/streams.js#L148
|
2023-02-16 08:30:14 -05:00
|
|
|
stdin = new Readable({
|
2023-02-14 11:38:45 -05:00
|
|
|
highWaterMark: 64 * 1024,
|
|
|
|
autoDestroy: false,
|
|
|
|
read: _read,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2023-10-30 11:53:08 -04:00
|
|
|
case "TTY": {
|
2024-03-22 16:49:07 -04:00
|
|
|
// If it's a TTY, we know that the stdin we created during warmup is the correct one and
|
|
|
|
// just return null to re-use it.
|
|
|
|
if (!warmup) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-31 07:54:43 -04:00
|
|
|
stdin = new readStream(fd);
|
2023-10-30 11:53:08 -04:00
|
|
|
break;
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
case "PIPE":
|
|
|
|
case "TCP": {
|
|
|
|
// For PIPE and TCP, `new Duplex()` should be replaced `new net.Socket()` if possible.
|
|
|
|
// There are two problems that need to be resolved.
|
|
|
|
// 1. Using them here introduces a circular dependency.
|
|
|
|
// 2. Creating a net.Socket() from a fd is not currently supported.
|
|
|
|
// https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L206
|
|
|
|
// https://github.com/nodejs/node/blob/v18.12.1/lib/net.js#L329
|
2023-02-16 08:30:14 -05:00
|
|
|
stdin = new Duplex({
|
2023-02-14 11:38:45 -05:00
|
|
|
readable: stdinType === "TTY" ? undefined : true,
|
|
|
|
writable: stdinType === "TTY" ? undefined : false,
|
|
|
|
readableHighWaterMark: stdinType === "TTY" ? 0 : undefined,
|
|
|
|
allowHalfOpen: false,
|
|
|
|
emitClose: false,
|
|
|
|
autoDestroy: true,
|
|
|
|
decodeStrings: false,
|
|
|
|
read: _read,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (stdinType !== "TTY") {
|
|
|
|
// Make sure the stdin can't be `.end()`-ed
|
2023-02-16 08:30:14 -05:00
|
|
|
stdin._writableState.ended = true;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Provide a dummy contentless input for e.g. non-console
|
|
|
|
// Windows applications.
|
2023-02-16 08:30:14 -05:00
|
|
|
stdin = new Readable({ read() {} });
|
2024-06-20 03:26:34 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2023-02-16 08:30:14 -05:00
|
|
|
stdin.push(null);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 22:37:37 -05:00
|
|
|
stdin.on("close", () => io.stdin?.close());
|
2024-01-24 14:09:49 -05:00
|
|
|
stdin.fd = io.stdin ? io.STDIN_RID : -1;
|
2024-06-20 03:26:34 -04:00
|
|
|
ObjectDefineProperty(stdin, "isTTY", {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-16 08:30:14 -05:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
2024-01-23 18:01:56 -05:00
|
|
|
return io.stdin.isTerminal();
|
2023-02-16 08:30:14 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
stdin._isRawMode = false;
|
|
|
|
stdin.setRawMode = (enable) => {
|
2023-03-04 22:37:37 -05:00
|
|
|
io.stdin?.setRaw?.(enable);
|
2023-02-16 08:30:14 -05:00
|
|
|
stdin._isRawMode = enable;
|
|
|
|
return stdin;
|
|
|
|
};
|
2024-06-20 03:26:34 -04:00
|
|
|
ObjectDefineProperty(stdin, "isRaw", {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-16 08:30:14 -05:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
|
|
|
return stdin._isRawMode;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
return stdin;
|
|
|
|
};
|