child_process: support Uint8Array input to methods

Support `Uint8Array` input to all the synchronous methods.

PR-URL: https://github.com/nodejs/node/pull/10653
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michal Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
Anna Henningsen 2017-01-06 02:12:26 +01:00 committed by James M Snell
parent db14687dec
commit 627ecee9ed
4 changed files with 24 additions and 12 deletions

View File

@ -580,8 +580,8 @@ added: v0.11.12
* `args` {Array} List of string arguments * `args` {Array} List of string arguments
* `options` {Object} * `options` {Object}
* `cwd` {String} Current working directory of the child process * `cwd` {String} Current working directory of the child process
* `input` {String|Buffer} The value which will be passed as stdin to the * `input` {String|Buffer|Uint8Array} The value which will be passed as stdin
spawned process to the spawned process
- supplying this value will override `stdio[0]` - supplying this value will override `stdio[0]`
* `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`) * `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`)
- `stderr` by default will be output to the parent process' stderr unless - `stderr` by default will be output to the parent process' stderr unless
@ -618,8 +618,8 @@ added: v0.11.12
* `command` {String} The command to run * `command` {String} The command to run
* `options` {Object} * `options` {Object}
* `cwd` {String} Current working directory of the child process * `cwd` {String} Current working directory of the child process
* `input` {String|Buffer} The value which will be passed as stdin to the * `input` {String|Buffer|Uint8Array} The value which will be passed as stdin
spawned process to the spawned process
- supplying this value will override `stdio[0]` - supplying this value will override `stdio[0]`
* `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`) * `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`)
- `stderr` by default will be output to the parent process' stderr unless - `stderr` by default will be output to the parent process' stderr unless
@ -666,8 +666,8 @@ added: v0.11.12
* `args` {Array} List of string arguments * `args` {Array} List of string arguments
* `options` {Object} * `options` {Object}
* `cwd` {String} Current working directory of the child process * `cwd` {String} Current working directory of the child process
* `input` {String|Buffer} The value which will be passed as stdin to the * `input` {String|Buffer|Uint8Array} The value which will be passed as stdin
spawned process to the spawned process
- supplying this value will override `stdio[0]` - supplying this value will override `stdio[0]`
* `stdio` {String | Array} Child's stdio configuration. * `stdio` {String | Array} Child's stdio configuration.
* `env` {Object} Environment key-value pairs * `env` {Object} Environment key-value pairs

View File

@ -9,6 +9,7 @@ const uv = process.binding('uv');
const spawn_sync = process.binding('spawn_sync'); const spawn_sync = process.binding('spawn_sync');
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
const Pipe = process.binding('pipe_wrap').Pipe; const Pipe = process.binding('pipe_wrap').Pipe;
const { isUint8Array } = process.binding('util');
const child_process = require('internal/child_process'); const child_process = require('internal/child_process');
const errnoException = util._errnoException; const errnoException = util._errnoException;
@ -503,13 +504,13 @@ function spawnSync(/*file, args, options*/) {
var input = options.stdio[i] && options.stdio[i].input; var input = options.stdio[i] && options.stdio[i].input;
if (input != null) { if (input != null) {
var pipe = options.stdio[i] = util._extend({}, options.stdio[i]); var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
if (Buffer.isBuffer(input)) if (isUint8Array(input))
pipe.input = input; pipe.input = input;
else if (typeof input === 'string') else if (typeof input === 'string')
pipe.input = Buffer.from(input, options.encoding); pipe.input = Buffer.from(input, options.encoding);
else else
throw new TypeError(util.format( throw new TypeError(util.format(
'stdio[%d] should be Buffer or string not %s', 'stdio[%d] should be Buffer, Uint8Array or string not %s',
i, i,
typeof input)); typeof input));
} }

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const StringDecoder = require('string_decoder').StringDecoder; const StringDecoder = require('string_decoder').StringDecoder;
const Buffer = require('buffer').Buffer;
const EventEmitter = require('events'); const EventEmitter = require('events');
const net = require('net'); const net = require('net');
const dgram = require('dgram'); const dgram = require('dgram');
@ -17,6 +16,7 @@ const TTY = process.binding('tty_wrap').TTY;
const TCP = process.binding('tcp_wrap').TCP; const TCP = process.binding('tcp_wrap').TCP;
const UDP = process.binding('udp_wrap').UDP; const UDP = process.binding('udp_wrap').UDP;
const SocketList = require('internal/socket_list'); const SocketList = require('internal/socket_list');
const { isUint8Array } = process.binding('util');
const errnoException = util._errnoException; const errnoException = util._errnoException;
const SocketListSend = SocketList.SocketListSend; const SocketListSend = SocketList.SocketListSend;
@ -847,10 +847,11 @@ function _validateStdio(stdio, sync) {
wrapType: getHandleWrapType(handle), wrapType: getHandleWrapType(handle),
handle: handle handle: handle
}); });
} else if (stdio instanceof Buffer || typeof stdio === 'string') { } else if (isUint8Array(stdio) || typeof stdio === 'string') {
if (!sync) { if (!sync) {
cleanup(); cleanup();
throw new TypeError('Asynchronous forks do not support Buffer input: ' + throw new TypeError('Asynchronous forks do not support ' +
'Buffer, Uint8Array or string input: ' +
util.inspect(stdio)); util.inspect(stdio));
} }
} else { } else {

View File

@ -57,7 +57,7 @@ var options = {
assert.throws(function() { assert.throws(function() {
spawnSync('cat', [], options); spawnSync('cat', [], options);
}, /TypeError:.*should be Buffer or string not number/); }, /TypeError:.*should be Buffer, Uint8Array or string not number/);
options = { options = {
@ -80,6 +80,16 @@ checkSpawnSyncRet(ret);
assert.deepStrictEqual(ret.stdout, options.input); assert.deepStrictEqual(ret.stdout, options.input);
assert.deepStrictEqual(ret.stderr, Buffer.from('')); assert.deepStrictEqual(ret.stderr, Buffer.from(''));
options = {
input: Uint8Array.from(Buffer.from('hello world'))
};
ret = spawnSync('cat', [], options);
checkSpawnSyncRet(ret);
assert.deepStrictEqual(ret.stdout, options.input);
assert.deepStrictEqual(ret.stderr, Buffer.from(''));
verifyBufOutput(spawnSync(process.execPath, args)); verifyBufOutput(spawnSync(process.execPath, args));
ret = spawnSync(process.execPath, args, { encoding: 'utf8' }); ret = spawnSync(process.execPath, args, { encoding: 'utf8' });