GeezerVison is showing github.com — child_process: support Uint8Array input to methods · nodejs/node@627ecee
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/child_process.md +6-6Lines changed: 6 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -580,8 +580,8 @@ added: v0.11.12 | 580 | 580 | * `args` {Array} List of string arguments | 581 | 581 | * `options` {Object} | 582 | 582 | * `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 | 585 | 585 | - supplying this value will override `stdio[0]` | 586 | 586 | * `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`) | 587 | 587 | - `stderr` by default will be output to the parent process' stderr unless | @@ -618,8 +618,8 @@ added: v0.11.12 | 618 | 618 | * `command` {String} The command to run | 619 | 619 | * `options` {Object} | 620 | 620 | * `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 | 623 | 623 | - supplying this value will override `stdio[0]` | 624 | 624 | * `stdio` {String | Array} Child's stdio configuration. (Default: `'pipe'`) | 625 | 625 | - `stderr` by default will be output to the parent process' stderr unless | @@ -666,8 +666,8 @@ added: v0.11.12 | 666 | 666 | * `args` {Array} List of string arguments | 667 | 667 | * `options` {Object} | 668 | 668 | * `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 | 671 | 671 | - supplying this value will override `stdio[0]` | 672 | 672 | * `stdio` {String | Array} Child's stdio configuration. | 673 | 673 | * `env` {Object} Environment key-value pairs | |
Collapse file Copy file name to clipboardExpand all lines: lib/child_process.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ const uv = process.binding('uv'); | 9 | 9 | const spawn_sync = process.binding('spawn_sync'); | 10 | 10 | const Buffer = require('buffer').Buffer; | 11 | 11 | const Pipe = process.binding('pipe_wrap').Pipe; | | 12 | +const { isUint8Array } = process.binding('util'); | 12 | 13 | const child_process = require('internal/child_process'); | 13 | 14 |
| 14 | 15 | const errnoException = util._errnoException; | @@ -503,13 +504,13 @@ function spawnSync(/*file, args, options*/) { | 503 | 504 | var input = options.stdio[i] && options.stdio[i].input; | 504 | 505 | if (input != null) { | 505 | 506 | var pipe = options.stdio[i] = util._extend({}, options.stdio[i]); | 506 | | - if (Buffer.isBuffer(input)) | | 507 | + if (isUint8Array(input)) | 507 | 508 | pipe.input = input; | 508 | 509 | else if (typeof input === 'string') | 509 | 510 | pipe.input = Buffer.from(input, options.encoding); | 510 | 511 | else | 511 | 512 | 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', | 513 | 514 | i, | 514 | 515 | typeof input)); | 515 | 516 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/child_process.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const StringDecoder = require('string_decoder').StringDecoder; | 4 | | -const Buffer = require('buffer').Buffer; | 5 | 4 | const EventEmitter = require('events'); | 6 | 5 | const net = require('net'); | 7 | 6 | const dgram = require('dgram'); | @@ -17,6 +16,7 @@ const TTY = process.binding('tty_wrap').TTY; | 17 | 16 | const TCP = process.binding('tcp_wrap').TCP; | 18 | 17 | const UDP = process.binding('udp_wrap').UDP; | 19 | 18 | const SocketList = require('internal/socket_list'); | | 19 | +const { isUint8Array } = process.binding('util'); | 20 | 20 |
| 21 | 21 | const errnoException = util._errnoException; | 22 | 22 | const SocketListSend = SocketList.SocketListSend; | @@ -847,10 +847,11 @@ function _validateStdio(stdio, sync) { | 847 | 847 | wrapType: getHandleWrapType(handle), | 848 | 848 | handle: handle | 849 | 849 | }); | 850 | | - } else if (stdio instanceof Buffer || typeof stdio === 'string') { | | 850 | + } else if (isUint8Array(stdio) || typeof stdio === 'string') { | 851 | 851 | if (!sync) { | 852 | 852 | 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: ' + | 854 | 855 | util.inspect(stdio)); | 855 | 856 | } | 856 | 857 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-spawnsync-input.js +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,7 @@ var options = { | 57 | 57 |
| 58 | 58 | assert.throws(function() { | 59 | 59 | spawnSync('cat', [], options); | 60 | | -}, /TypeError:.*should be Buffer or string not number/); | | 60 | +}, /TypeError:.*should be Buffer, Uint8Array or string not number/); | 61 | 61 |
| 62 | 62 |
| 63 | 63 | options = { | @@ -80,6 +80,16 @@ checkSpawnSyncRet(ret); | 80 | 80 | assert.deepStrictEqual(ret.stdout, options.input); | 81 | 81 | assert.deepStrictEqual(ret.stderr, Buffer.from('')); | 82 | 82 |
| | 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 | + | 83 | 93 | verifyBufOutput(spawnSync(process.execPath, args)); | 84 | 94 |
| 85 | 95 | ret = spawnSync(process.execPath, args, { encoding: 'utf8' }); | |
|