Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: internal/process/stdio.js cleanup / modernization #6766

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,41 @@ exports.setup = setupStdio;
function setupStdio() {
var stdin, stdout, stderr;

process.__defineGetter__('stdout', function() {
function getStdout() {
if (stdout) return stdout;
stdout = createWritableStdioStream(1);
stdout.destroy = stdout.destroySoon = function(er) {
er = er || new Error('process.stdout cannot be closed.');
stdout.emit('error', er);
};
if (stdout.isTTY) {
process.on('SIGWINCH', function() {
stdout._refreshSize();
});
process.on('SIGWINCH', () => stdout._refreshSize());
}
return stdout;
});
}

process.__defineGetter__('stderr', function() {
function getStderr() {
if (stderr) return stderr;
stderr = createWritableStdioStream(2);
stderr.destroy = stderr.destroySoon = function(er) {
er = er || new Error('process.stderr cannot be closed.');
stderr.emit('error', er);
};
if (stderr.isTTY) {
process.on('SIGWINCH', function() {
stderr._refreshSize();
});
process.on('SIGWINCH', () => stderr._refreshSize());
}
return stderr;
});
}

process.__defineGetter__('stdin', function() {
function getStdin() {
if (stdin) return stdin;

var tty_wrap = process.binding('tty_wrap');
var fd = 0;
const tty_wrap = process.binding('tty_wrap');
const fd = 0;

switch (tty_wrap.guessHandleType(fd)) {
case 'TTY':
var tty = require('tty');
const tty = require('tty');
stdin = new tty.ReadStream(fd, {
highWaterMark: 0,
readable: true,
Expand All @@ -52,13 +48,13 @@ function setupStdio() {
break;

case 'FILE':
var fs = require('fs');
const fs = require('fs');
stdin = new fs.ReadStream(null, { fd: fd, autoClose: false });
break;

case 'PIPE':
case 'TCP':
var net = require('net');
const net = require('net');

// It could be that process has been started with an IPC channel
// sitting on fd=0, in such case the pipe for this fd is already
Expand Down Expand Up @@ -100,7 +96,7 @@ function setupStdio() {

// if the user calls stdin.pause(), then we need to stop reading
// immediately, so that the process can close down.
stdin.on('pause', function() {
stdin.on('pause', () => {
if (!stdin._handle)
return;
stdin._readableState.reading = false;
Expand All @@ -109,6 +105,24 @@ function setupStdio() {
});

return stdin;
}

Object.defineProperty(process, 'stdout', {
configurable: true,
enumerable: true,
get: getStdout
});

Object.defineProperty(process, 'stderr', {
configurable: true,
enumerable: true,
get: getStderr
});

Object.defineProperty(process, 'stdin', {
configurable: true,
enumerable: true,
get: getStdin
});

process.openStdin = function() {
Expand All @@ -119,26 +133,26 @@ function setupStdio() {

function createWritableStdioStream(fd) {
var stream;
var tty_wrap = process.binding('tty_wrap');
const tty_wrap = process.binding('tty_wrap');

// Note stream._type is used for test-module-load-list.js

switch (tty_wrap.guessHandleType(fd)) {
case 'TTY':
var tty = require('tty');
const tty = require('tty');
stream = new tty.WriteStream(fd);
stream._type = 'tty';
break;

case 'FILE':
var fs = require('fs');
const fs = require('fs');
stream = new fs.SyncWriteStream(fd, { autoClose: false });
stream._type = 'fs';
break;

case 'PIPE':
case 'TCP':
var net = require('net');
const net = require('net');
stream = new net.Socket({
fd: fd,
readable: false,
Expand Down