Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
process: on error in stdout/stderr, reopen as null
On an error in stdout/stderr stream, redirect the error to a new
`'stdio-error'` event and set the stdout/stderr to a null stream.

Additional refactoring the stdio.js while in the code.
  • Loading branch information
jasnell committed Apr 13, 2016
commit 7916d607cb97e008245294bff0f6aa183b2f0a8e
79 changes: 51 additions & 28 deletions lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,63 @@ exports.setup = setupStdio;
function setupStdio() {
var stdin, stdout, stderr;

function cannotCloseStderr(er) {
throw new Error('process.stderr cannot be closed.');
}
function cannotCloseStdout(er) {
throw new Error('process.stdout cannot be closed.');
}

Object.defineProperty(process, 'stdout', {
configurable: true,
configurable: false,
enumerable: true,
get: () => {
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);
};
stdout.once('error', (err) => {
stdout = createNullStream(1);
stdout.destroy = stdout.destroySoon = cannotCloseStdout;
process.nextTick(() => process.emit('stdio-error', err, 1));
});
stdout.destroy = stdout.destroySoon = cannotCloseStdout;
if (stdout.isTTY) {
process.on('SIGWINCH', function() {
stdout._refreshSize();
});
process.on('SIGWINCH', () => stdout._refreshSize());
}
return stdout;
return stdout;
}
});

Object.defineProperty(process, 'stderr', {
configurable: true,
configurable: false,
enumerable: true,
get: () => {
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);
};
stderr.once('error', (err) => {
stderr = createNullStream(2);
stderr.destroy = stderr.destroySoon = cannotCloseStderr;
process.nextTick(() => process.emit('stdio-error', err, 2));
});
stderr.destroy = stderr.destroySoon = cannotCloseStderr;
if (stderr.isTTY) {
process.on('SIGWINCH', function() {
stderr._refreshSize();
});
process.on('SIGWINCH', () => stderr._refreshSize());
}
return stderr;
}
});

Object.defineProperty(process, 'stdin', {
configurable: true,
configurable: false,
enumerable: true,
get: () => {
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 @@ -63,13 +70,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 @@ -111,7 +118,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 @@ -131,26 +138,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 All @@ -171,3 +178,19 @@ function createWritableStdioStream(fd) {

return stream;
}

function createNullStream(fd) {
// Used to approximate /dev/null
const Writable = require('stream').Writable;
const util = require('util');
function NullStream(fd) {
Writable.call(this, {});
this.fd = fd;
this._type = 'null';
}
util.inherits(NullStream, Writable);
NullStream.prototype._write = function(cb) {
setImmediate(cb);
};
return new NullStream(fd);
}
11 changes: 1 addition & 10 deletions test/parallel/test-tty-stdout-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@
require('../common');
const assert = require('assert');

const shouldThrow = function() {
process.stdout.end();
};

const validateError = function(e) {
return e instanceof Error &&
e.message === 'process.stdout cannot be closed.';
};

assert.throws(shouldThrow, validateError);
assert.throws(() => process.stdout.end(), /process.stdout cannot be closed./);