Skip to content

Commit

Permalink
process: internal/process/stdio.js cleanup / modernization
Browse files Browse the repository at this point in the history
Avoid using deprecated getter syntax plus other
miscellaneous updates.

PR-URL: #6766
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
jasnell committed May 17, 2016
1 parent 42ede93 commit f856234
Showing 1 changed file with 35 additions and 21 deletions.
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

0 comments on commit f856234

Please sign in to comment.