child_process: refactor internal/child_process.js

* Prefer === to == where possible
* Remove condition that will always be false
* Prefer for-loop statements to forEach where possible for perfomance reasons

PR-URL: https://github.com/nodejs/node/pull/11366
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
Arseniy Maximov 2017-02-14 06:39:24 +03:00 committed by Michaël Zasso
parent ca37ec084a
commit 8fc362ea98

View File

@ -230,12 +230,16 @@ util.inherits(ChildProcess, EventEmitter);
function flushStdio(subprocess) { function flushStdio(subprocess) {
if (subprocess.stdio == null) return; const stdio = subprocess.stdio;
subprocess.stdio.forEach(function(stream, fd, stdio) {
if (stdio == null) return;
for (var i = 0; i < stdio.length; i++) {
const stream = stdio[i];
if (!stream || !stream.readable || stream._readableState.readableListening) if (!stream || !stream.readable || stream._readableState.readableListening)
return; continue;
stream.resume(); stream.resume();
}); }
} }
@ -268,6 +272,7 @@ ChildProcess.prototype.spawn = function(options) {
const self = this; const self = this;
var ipc; var ipc;
var ipcFd; var ipcFd;
var i;
// If no `stdio` option was given - use default // If no `stdio` option was given - use default
var stdio = options.stdio || 'pipe'; var stdio = options.stdio || 'pipe';
@ -302,11 +307,12 @@ ChildProcess.prototype.spawn = function(options) {
if (err !== uv.UV_ENOENT) return err; if (err !== uv.UV_ENOENT) return err;
} else if (err) { } else if (err) {
// Close all opened fds on error // Close all opened fds on error
stdio.forEach(function(stdio) { for (i = 0; i < stdio.length; i++) {
if (stdio.type === 'pipe') { const stream = stdio[i];
stdio.handle.close(); if (stream.type === 'pipe') {
stream.handle.close();
}
} }
});
this._handle.close(); this._handle.close();
this._handle = null; this._handle = null;
@ -315,27 +321,29 @@ ChildProcess.prototype.spawn = function(options) {
this.pid = this._handle.pid; this.pid = this._handle.pid;
stdio.forEach(function(stdio, i) { for (i = 0; i < stdio.length; i++) {
if (stdio.type === 'ignore') return; const stream = stdio[i];
if (stream.type === 'ignore') continue;
if (stdio.ipc) { if (stream.ipc) {
self._closesNeeded++; self._closesNeeded++;
return; continue;
} }
if (stdio.handle) { if (stream.handle) {
// when i === 0 - we're dealing with stdin // when i === 0 - we're dealing with stdin
// (which is the only one writable pipe) // (which is the only one writable pipe)
stdio.socket = createSocket(self.pid !== 0 ? stdio.handle : null, i > 0); stream.socket = createSocket(self.pid !== 0 ?
stream.handle : null, i > 0);
if (i > 0 && self.pid !== 0) { if (i > 0 && self.pid !== 0) {
self._closesNeeded++; self._closesNeeded++;
stdio.socket.on('close', function() { stream.socket.on('close', function() {
maybeClose(self); maybeClose(self);
}); });
} }
} }
}); }
this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ? this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ?
stdio[0].socket : null; stdio[0].socket : null;
@ -797,11 +805,11 @@ function _validateStdio(stdio, sync) {
} }
// Defaults // Defaults
if (stdio === null || stdio === undefined) { if (stdio == null) {
stdio = i < 3 ? 'pipe' : 'ignore'; stdio = i < 3 ? 'pipe' : 'ignore';
} }
if (stdio === null || stdio === 'ignore') { if (stdio === 'ignore') {
acc.push({type: 'ignore'}); acc.push({type: 'ignore'});
} else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) { } else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) {
var a = { var a = {
@ -888,7 +896,7 @@ function getSocketList(type, slave, key) {
function maybeClose(subprocess) { function maybeClose(subprocess) {
subprocess._closesGot++; subprocess._closesGot++;
if (subprocess._closesGot == subprocess._closesNeeded) { if (subprocess._closesGot === subprocess._closesNeeded) {
subprocess.emit('close', subprocess.exitCode, subprocess.signalCode); subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
} }
} }