2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2019-11-27 18:59:29 +00:00
|
|
|
NumberIsInteger,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const net = require('net');
|
2018-08-23 14:20:12 +00:00
|
|
|
const { TTY, isTTY } = internalBinding('tty_wrap');
|
2024-04-23 17:05:38 +00:00
|
|
|
const {
|
|
|
|
ErrnoException,
|
|
|
|
codes: {
|
|
|
|
ERR_INVALID_FD,
|
|
|
|
ERR_TTY_INIT_FAILED,
|
|
|
|
},
|
|
|
|
} = require('internal/errors');
|
2019-01-19 14:37:38 +00:00
|
|
|
const {
|
|
|
|
getColorDepth,
|
2023-02-12 18:26:21 +00:00
|
|
|
hasColors,
|
2019-01-19 14:37:38 +00:00
|
|
|
} = require('internal/tty');
|
2013-07-16 21:28:38 +00:00
|
|
|
|
2018-05-06 21:09:01 +00:00
|
|
|
// Lazy loaded for startup performance.
|
|
|
|
let readline;
|
|
|
|
|
2017-11-12 03:58:26 +00:00
|
|
|
function isatty(fd) {
|
2021-03-19 09:48:02 +00:00
|
|
|
return NumberIsInteger(fd) && fd >= 0 && fd <= 2147483647 &&
|
|
|
|
isTTY(fd);
|
2017-11-12 03:58:26 +00:00
|
|
|
}
|
2011-09-20 22:57:23 +00:00
|
|
|
|
2012-12-13 06:06:35 +00:00
|
|
|
function ReadStream(fd, options) {
|
|
|
|
if (!(this instanceof ReadStream))
|
|
|
|
return new ReadStream(fd, options);
|
2017-04-11 22:29:39 +00:00
|
|
|
if (fd >> 0 !== fd || fd < 0)
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_FD(fd);
|
2012-12-13 06:06:35 +00:00
|
|
|
|
2017-10-28 00:20:54 +00:00
|
|
|
const ctx = {};
|
2020-07-15 19:44:10 +00:00
|
|
|
const tty = new TTY(fd, ctx);
|
2017-10-28 00:20:54 +00:00
|
|
|
if (ctx.code !== undefined) {
|
2018-03-20 16:46:30 +00:00
|
|
|
throw new ERR_TTY_INIT_FAILED(ctx);
|
2017-10-28 00:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 02:15:57 +00:00
|
|
|
net.Socket.call(this, {
|
2021-07-03 20:10:21 +00:00
|
|
|
readableHighWaterMark: 0,
|
2018-12-18 02:15:57 +00:00
|
|
|
handle: tty,
|
2020-07-15 19:44:10 +00:00
|
|
|
manualStart: true,
|
2023-02-12 18:26:21 +00:00
|
|
|
...options,
|
2018-12-18 02:15:57 +00:00
|
|
|
});
|
2011-09-20 22:57:23 +00:00
|
|
|
|
2012-03-26 22:21:25 +00:00
|
|
|
this.isRaw = false;
|
2012-12-13 06:06:35 +00:00
|
|
|
this.isTTY = true;
|
2011-09-20 22:57:23 +00:00
|
|
|
}
|
2019-03-20 04:37:55 +00:00
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectSetPrototypeOf(ReadStream.prototype, net.Socket.prototype);
|
|
|
|
ObjectSetPrototypeOf(ReadStream, net.Socket);
|
2011-12-05 23:36:45 +00:00
|
|
|
|
2012-03-26 22:21:25 +00:00
|
|
|
ReadStream.prototype.setRawMode = function(flag) {
|
|
|
|
flag = !!flag;
|
2022-08-24 17:12:30 +00:00
|
|
|
const err = this._handle?.setRawMode(flag);
|
2018-09-16 19:47:17 +00:00
|
|
|
if (err) {
|
2024-04-23 17:05:38 +00:00
|
|
|
this.emit('error', new ErrnoException(err, 'setRawMode'));
|
2018-09-19 18:09:25 +00:00
|
|
|
return this;
|
2018-09-16 19:47:17 +00:00
|
|
|
}
|
2012-03-26 22:21:25 +00:00
|
|
|
this.isRaw = flag;
|
2018-09-19 18:09:25 +00:00
|
|
|
return this;
|
2012-03-26 22:21:25 +00:00
|
|
|
};
|
2011-09-20 22:57:23 +00:00
|
|
|
|
|
|
|
function WriteStream(fd) {
|
2017-04-11 22:29:39 +00:00
|
|
|
if (!(this instanceof WriteStream))
|
|
|
|
return new WriteStream(fd);
|
|
|
|
if (fd >> 0 !== fd || fd < 0)
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_FD(fd);
|
2017-04-11 22:29:39 +00:00
|
|
|
|
2017-10-28 00:20:54 +00:00
|
|
|
const ctx = {};
|
2020-07-15 19:44:10 +00:00
|
|
|
const tty = new TTY(fd, ctx);
|
2017-10-28 00:20:54 +00:00
|
|
|
if (ctx.code !== undefined) {
|
2018-03-20 16:46:30 +00:00
|
|
|
throw new ERR_TTY_INIT_FAILED(ctx);
|
2017-10-28 00:20:54 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 22:57:23 +00:00
|
|
|
net.Socket.call(this, {
|
2021-07-03 20:10:21 +00:00
|
|
|
readableHighWaterMark: 0,
|
2017-10-28 00:20:54 +00:00
|
|
|
handle: tty,
|
2023-02-12 18:26:21 +00:00
|
|
|
manualStart: true,
|
2011-09-20 22:57:23 +00:00
|
|
|
});
|
|
|
|
|
2016-07-14 10:24:10 +00:00
|
|
|
// Prevents interleaved or dropped stdout/stderr output for terminals.
|
2016-05-20 14:20:08 +00:00
|
|
|
// As noted in the following reference, local TTYs tend to be quite fast and
|
2017-06-17 13:11:45 +00:00
|
|
|
// this behavior has become expected due historical functionality on OS X,
|
2016-05-20 14:20:08 +00:00
|
|
|
// even though it was originally intended to change in v1.0.2 (Libuv 1.2.1).
|
|
|
|
// Ref: https://github.com/nodejs/node/pull/1771#issuecomment-119351671
|
2016-12-21 20:39:54 +00:00
|
|
|
this._handle.setBlocking(true);
|
2016-05-20 14:20:08 +00:00
|
|
|
|
2024-08-11 06:32:13 +00:00
|
|
|
const winSize = [0, 0];
|
2018-01-17 18:38:51 +00:00
|
|
|
const err = this._handle.getWindowSize(winSize);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (!err) {
|
2012-04-30 23:18:37 +00:00
|
|
|
this.columns = winSize[0];
|
|
|
|
this.rows = winSize[1];
|
|
|
|
}
|
2011-09-20 22:57:23 +00:00
|
|
|
}
|
2019-03-20 04:37:55 +00:00
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectSetPrototypeOf(WriteStream.prototype, net.Socket.prototype);
|
|
|
|
ObjectSetPrototypeOf(WriteStream, net.Socket);
|
2011-09-20 22:57:23 +00:00
|
|
|
|
|
|
|
WriteStream.prototype.isTTY = true;
|
|
|
|
|
2018-04-01 09:27:01 +00:00
|
|
|
WriteStream.prototype.getColorDepth = getColorDepth;
|
2011-09-20 22:57:23 +00:00
|
|
|
|
2019-01-19 14:37:38 +00:00
|
|
|
WriteStream.prototype.hasColors = hasColors;
|
|
|
|
|
2012-03-26 22:21:25 +00:00
|
|
|
WriteStream.prototype._refreshSize = function() {
|
2018-01-17 18:38:51 +00:00
|
|
|
const oldCols = this.columns;
|
|
|
|
const oldRows = this.rows;
|
2024-08-11 06:32:13 +00:00
|
|
|
const winSize = [0, 0];
|
2018-01-17 18:38:51 +00:00
|
|
|
const err = this._handle.getWindowSize(winSize);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
2024-04-23 17:05:38 +00:00
|
|
|
this.emit('error', new ErrnoException(err, 'getWindowSize'));
|
2012-05-01 22:51:29 +00:00
|
|
|
return;
|
2012-04-30 23:18:37 +00:00
|
|
|
}
|
2020-12-31 11:40:50 +00:00
|
|
|
const { 0: newCols, 1: newRows } = winSize;
|
2012-03-26 22:21:25 +00:00
|
|
|
if (oldCols !== newCols || oldRows !== newRows) {
|
|
|
|
this.columns = newCols;
|
|
|
|
this.rows = newRows;
|
|
|
|
this.emit('resize');
|
2011-09-20 22:57:23 +00:00
|
|
|
}
|
2012-03-28 23:27:57 +00:00
|
|
|
};
|
2011-09-20 22:57:23 +00:00
|
|
|
|
2018-01-17 18:38:51 +00:00
|
|
|
// Backwards-compat
|
2019-07-16 15:46:59 +00:00
|
|
|
WriteStream.prototype.cursorTo = function(x, y, callback) {
|
2018-05-06 21:09:01 +00:00
|
|
|
if (readline === undefined) readline = require('readline');
|
2019-07-16 15:46:59 +00:00
|
|
|
return readline.cursorTo(this, x, y, callback);
|
2012-03-26 22:21:25 +00:00
|
|
|
};
|
2019-07-16 15:46:59 +00:00
|
|
|
WriteStream.prototype.moveCursor = function(dx, dy, callback) {
|
2018-05-06 21:09:01 +00:00
|
|
|
if (readline === undefined) readline = require('readline');
|
2019-07-16 15:46:59 +00:00
|
|
|
return readline.moveCursor(this, dx, dy, callback);
|
2011-09-20 22:57:23 +00:00
|
|
|
};
|
2019-07-16 15:46:59 +00:00
|
|
|
WriteStream.prototype.clearLine = function(dir, callback) {
|
2018-05-06 21:09:01 +00:00
|
|
|
if (readline === undefined) readline = require('readline');
|
2019-07-16 15:46:59 +00:00
|
|
|
return readline.clearLine(this, dir, callback);
|
2011-09-20 22:57:23 +00:00
|
|
|
};
|
2019-07-16 15:46:59 +00:00
|
|
|
WriteStream.prototype.clearScreenDown = function(callback) {
|
2018-05-06 21:09:01 +00:00
|
|
|
if (readline === undefined) readline = require('readline');
|
2019-07-16 15:46:59 +00:00
|
|
|
return readline.clearScreenDown(this, callback);
|
2012-03-20 22:14:40 +00:00
|
|
|
};
|
2011-09-23 02:55:15 +00:00
|
|
|
WriteStream.prototype.getWindowSize = function() {
|
2012-03-26 22:21:25 +00:00
|
|
|
return [this.columns, this.rows];
|
2011-09-23 02:55:15 +00:00
|
|
|
};
|
2017-11-12 03:58:26 +00:00
|
|
|
|
|
|
|
module.exports = { isatty, ReadStream, WriteStream };
|