Skip to content

Commit

Permalink
child_process: support Uint8Array input to methods
Browse files Browse the repository at this point in the history
Support `Uint8Array` input to all the synchronous methods.

PR-URL: #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>
  • Loading branch information
addaleax authored and jasnell committed Jan 10, 2017
1 parent db14687 commit 627ecee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
12 changes: 6 additions & 6 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ added: v0.11.12
* `args` {Array} List of string arguments
* `options` {Object}
* `cwd` {String} Current working directory of the child process
* `input` {String|Buffer} The value which will be passed as stdin to the
spawned process
* `input` {String|Buffer|Uint8Array} The value which will be passed as stdin
to the spawned process
- supplying this value will override `stdio[0]`
* `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`)
- `stderr` by default will be output to the parent process' stderr unless
Expand Down Expand Up @@ -618,8 +618,8 @@ added: v0.11.12
* `command` {String} The command to run
* `options` {Object}
* `cwd` {String} Current working directory of the child process
* `input` {String|Buffer} The value which will be passed as stdin to the
spawned process
* `input` {String|Buffer|Uint8Array} The value which will be passed as stdin
to the spawned process
- supplying this value will override `stdio[0]`
* `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`)
- `stderr` by default will be output to the parent process' stderr unless
Expand Down Expand Up @@ -666,8 +666,8 @@ added: v0.11.12
* `args` {Array} List of string arguments
* `options` {Object}
* `cwd` {String} Current working directory of the child process
* `input` {String|Buffer} The value which will be passed as stdin to the
spawned process
* `input` {String|Buffer|Uint8Array} The value which will be passed as stdin
to the spawned process
- supplying this value will override `stdio[0]`
* `stdio` {String | Array} Child's stdio configuration.
* `env` {Object} Environment key-value pairs
Expand Down
5 changes: 3 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const uv = process.binding('uv');
const spawn_sync = process.binding('spawn_sync');
const Buffer = require('buffer').Buffer;
const Pipe = process.binding('pipe_wrap').Pipe;
const { isUint8Array } = process.binding('util');
const child_process = require('internal/child_process');

const errnoException = util._errnoException;
Expand Down Expand Up @@ -503,13 +504,13 @@ function spawnSync(/*file, args, options*/) {
var input = options.stdio[i] && options.stdio[i].input;
if (input != null) {
var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
if (Buffer.isBuffer(input))
if (isUint8Array(input))
pipe.input = input;
else if (typeof input === 'string')
pipe.input = Buffer.from(input, options.encoding);
else
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,
typeof input));
}
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const StringDecoder = require('string_decoder').StringDecoder;
const Buffer = require('buffer').Buffer;
const EventEmitter = require('events');
const net = require('net');
const dgram = require('dgram');
Expand All @@ -17,6 +16,7 @@ const TTY = process.binding('tty_wrap').TTY;
const TCP = process.binding('tcp_wrap').TCP;
const UDP = process.binding('udp_wrap').UDP;
const SocketList = require('internal/socket_list');
const { isUint8Array } = process.binding('util');

const errnoException = util._errnoException;
const SocketListSend = SocketList.SocketListSend;
Expand Down Expand Up @@ -847,10 +847,11 @@ function _validateStdio(stdio, sync) {
wrapType: getHandleWrapType(handle),
handle: handle
});
} else if (stdio instanceof Buffer || typeof stdio === 'string') {
} else if (isUint8Array(stdio) || typeof stdio === 'string') {
if (!sync) {
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));
}
} else {
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-child-process-spawnsync-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var options = {

assert.throws(function() {
spawnSync('cat', [], options);
}, /TypeError:.*should be Buffer or string not number/);
}, /TypeError:.*should be Buffer, Uint8Array or string not number/);


options = {
Expand All @@ -80,6 +80,16 @@ checkSpawnSyncRet(ret);
assert.deepStrictEqual(ret.stdout, options.input);
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));

ret = spawnSync(process.execPath, args, { encoding: 'utf8' });
Expand Down

0 comments on commit 627ecee

Please sign in to comment.