child_process: support Uint8Array input to methods · nodejs/node@627ecee

4 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -580,8 +580,8 @@ added: v0.11.12

580580

* `args` {Array} List of string arguments

581581

* `options` {Object}

582582

* `cwd` {String} Current working directory of the child process

583-

* `input` {String|Buffer} The value which will be passed as stdin to the

584-

spawned process

583+

* `input` {String|Buffer|Uint8Array} The value which will be passed as stdin

584+

to the spawned process

585585

- supplying this value will override `stdio[0]`

586586

* `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`)

587587

- `stderr` by default will be output to the parent process' stderr unless

@@ -618,8 +618,8 @@ added: v0.11.12

618618

* `command` {String} The command to run

619619

* `options` {Object}

620620

* `cwd` {String} Current working directory of the child process

621-

* `input` {String|Buffer} The value which will be passed as stdin to the

622-

spawned process

621+

* `input` {String|Buffer|Uint8Array} The value which will be passed as stdin

622+

to the spawned process

623623

- supplying this value will override `stdio[0]`

624624

* `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`)

625625

- `stderr` by default will be output to the parent process' stderr unless

@@ -666,8 +666,8 @@ added: v0.11.12

666666

* `args` {Array} List of string arguments

667667

* `options` {Object}

668668

* `cwd` {String} Current working directory of the child process

669-

* `input` {String|Buffer} The value which will be passed as stdin to the

670-

spawned process

669+

* `input` {String|Buffer|Uint8Array} The value which will be passed as stdin

670+

to the spawned process

671671

- supplying this value will override `stdio[0]`

672672

* `stdio` {String | Array} Child's stdio configuration.

673673

* `env` {Object} Environment key-value pairs

Original file line numberDiff line numberDiff line change

@@ -9,6 +9,7 @@ const uv = process.binding('uv');

99

const spawn_sync = process.binding('spawn_sync');

1010

const Buffer = require('buffer').Buffer;

1111

const Pipe = process.binding('pipe_wrap').Pipe;

12+

const { isUint8Array } = process.binding('util');

1213

const child_process = require('internal/child_process');

1314
1415

const errnoException = util._errnoException;

@@ -503,13 +504,13 @@ function spawnSync(/*file, args, options*/) {

503504

var input = options.stdio[i] && options.stdio[i].input;

504505

if (input != null) {

505506

var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);

506-

if (Buffer.isBuffer(input))

507+

if (isUint8Array(input))

507508

pipe.input = input;

508509

else if (typeof input === 'string')

509510

pipe.input = Buffer.from(input, options.encoding);

510511

else

511512

throw new TypeError(util.format(

512-

'stdio[%d] should be Buffer or string not %s',

513+

'stdio[%d] should be Buffer, Uint8Array or string not %s',

513514

i,

514515

typeof input));

515516

}

Original file line numberDiff line numberDiff line change

@@ -1,7 +1,6 @@

11

'use strict';

22
33

const StringDecoder = require('string_decoder').StringDecoder;

4-

const Buffer = require('buffer').Buffer;

54

const EventEmitter = require('events');

65

const net = require('net');

76

const dgram = require('dgram');

@@ -17,6 +16,7 @@ const TTY = process.binding('tty_wrap').TTY;

1716

const TCP = process.binding('tcp_wrap').TCP;

1817

const UDP = process.binding('udp_wrap').UDP;

1918

const SocketList = require('internal/socket_list');

19+

const { isUint8Array } = process.binding('util');

2020
2121

const errnoException = util._errnoException;

2222

const SocketListSend = SocketList.SocketListSend;

@@ -847,10 +847,11 @@ function _validateStdio(stdio, sync) {

847847

wrapType: getHandleWrapType(handle),

848848

handle: handle

849849

});

850-

} else if (stdio instanceof Buffer || typeof stdio === 'string') {

850+

} else if (isUint8Array(stdio) || typeof stdio === 'string') {

851851

if (!sync) {

852852

cleanup();

853-

throw new TypeError('Asynchronous forks do not support Buffer input: ' +

853+

throw new TypeError('Asynchronous forks do not support ' +

854+

'Buffer, Uint8Array or string input: ' +

854855

util.inspect(stdio));

855856

}

856857

} else {

Original file line numberDiff line numberDiff line change

@@ -57,7 +57,7 @@ var options = {

5757
5858

assert.throws(function() {

5959

spawnSync('cat', [], options);

60-

}, /TypeError:.*should be Buffer or string not number/);

60+

}, /TypeError:.*should be Buffer, Uint8Array or string not number/);

6161
6262
6363

options = {

@@ -80,6 +80,16 @@ checkSpawnSyncRet(ret);

8080

assert.deepStrictEqual(ret.stdout, options.input);

8181

assert.deepStrictEqual(ret.stderr, Buffer.from(''));

8282
83+

options = {

84+

input: Uint8Array.from(Buffer.from('hello world'))

85+

};

86+
87+

ret = spawnSync('cat', [], options);

88+
89+

checkSpawnSyncRet(ret);

90+

assert.deepStrictEqual(ret.stdout, options.input);

91+

assert.deepStrictEqual(ret.stderr, Buffer.from(''));

92+
8393

verifyBufOutput(spawnSync(process.execPath, args));

8494
8595

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