Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/exec stdio #48553

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ changes:
* `options` {Object}
* `cwd` {string|URL} Current working directory of the child process.
**Default:** `process.cwd()`.
* `input` {string|Buffer|TypedArray|DataView} 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. `stderr` by default will
be output to the parent process' stderr unless `stdio` is specified.
**Default:** `'pipe'`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `encoding` {string} **Default:** `'utf8'`
* `shell` {string} Shell to execute the command with. See
Expand Down Expand Up @@ -302,6 +308,12 @@ changes:
* `args` {string\[]} List of string arguments.
* `options` {Object}
* `cwd` {string|URL} Current working directory of the child process.
* `input` {string|Buffer|TypedArray|DataView} 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. `stderr` by default will
be output to the parent process' stderr unless `stdio` is specified.
**Default:** `'pipe'`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `encoding` {string} **Default:** `'utf8'`
* `timeout` {number} **Default:** `0`
Expand Down Expand Up @@ -563,6 +575,9 @@ changes:
* `args` {string\[]} List of string arguments.
* `options` {Object}
* `cwd` {string|URL} Current working directory of the child process.
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
as stdin to the spawned process. Supplying this value will override
`stdio[0]`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `argv0` {string} Explicitly set the value of `argv[0]` sent to the child
process. This will be set to `command` if not specified.
Expand Down
31 changes: 28 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ function normalizeExecArgs(command, options, callback) {
* @param {string} command
* @param {{
* cmd?: string;
* input?: string | Buffer | TypedArray | DataView;
* stdio?: Array | string;
* env?: Record<string, string>;
* encoding?: string;
* shell?: string;
Expand All @@ -229,9 +231,7 @@ function normalizeExecArgs(command, options, callback) {
*/
function exec(command, options, callback) {
const opts = normalizeExecArgs(command, options, callback);
return module.exports.execFile(opts.file,
opts.options,
opts.callback);
return execFile(opts.file, opts.options, opts.callback);
}

const customPromiseExecFunction = (orig) => {
Expand Down Expand Up @@ -304,6 +304,8 @@ function normalizeExecFileArgs(file, args, options, callback) {
* @param {string[]} [args]
* @param {{
* cwd?: string;
* input?: string | Buffer | TypedArray | DataView;
* stdio?: Array | string;
* env?: Record<string, string>;
* encoding?: string;
* timeout?: number;
Expand Down Expand Up @@ -347,6 +349,8 @@ function execFile(file, args, options, callback) {

const child = spawn(file, args, {
cwd: options.cwd,
input: options.input,
stdio: options.stdio,
env: options.env,
gid: options.gid,
shell: options.shell,
Expand Down Expand Up @@ -730,6 +734,7 @@ function abortChildProcess(child, killSignal, reason) {
* @param {string[]} [args]
* @param {{
* cwd?: string;
* input?: string | Buffer | TypedArray | DataView;
* env?: Record<string, string>;
* argv0?: string;
* stdio?: Array | string;
Expand All @@ -754,6 +759,22 @@ function spawn(file, args, options) {
const child = new ChildProcess();

debug('spawn', options);
let input = options.input;
if (options.input) {
if (isArrayBufferView(input)) {
input = Buffer.from(input.buffer, input.byteOffset, input.byteLength);
} else if (typeof input === 'string') {
input = Buffer.from(input, options.encoding);
} else {
throw new ERR_INVALID_ARG_TYPE('options.input',
['Buffer',
'TypedArray',
'DataView',
'string'],
input);
}
}

child.spawn(options);

if (options.timeout > 0) {
Expand Down Expand Up @@ -791,6 +812,10 @@ function spawn(file, args, options) {
}
}

if (options.input) {
child.stdin.end(input);
}

return child;
}

Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-child-process-exec-stdio-ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
process.stdout.write('this should be ignored');
process.stderr.write('this should not be ignored');

} else {
const cmd = `"${process.execPath}" "${__filename}" child`;
cp.exec(cmd,
{ stdio: ['pipe', 'ignore', 'pipe'] },
common.mustCall((err, stdout, stderr) => {
assert.strictEqual(stderr, 'this should not be ignored');
assert.strictEqual(stdout, '');
}));
}
46 changes: 46 additions & 0 deletions test/parallel/test-child-process-exec-stdio-inherit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Joyent, Inc. and other Node contributors.
mscdex marked this conversation as resolved.
Show resolved Hide resolved
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const assert = require('assert');
const exec = require('child_process').exec;

if (process.argv[2] === 'parent')
parent();
else
grandparent();

function grandparent() {
const cmd = `"${process.execPath}" "${__filename}" parent`;
const input = 'asdfasdf';
const child = exec(cmd, (err, stdout, stderr) => {
assert.strictEqual(stdout.trim(), input.trim());
});
child.stderr.pipe(process.stderr);

child.stdin.end(input);
}

function parent() {
// Should not immediately exit.
exec('cat', { stdio: 'inherit' });
}
18 changes: 18 additions & 0 deletions test/parallel/test-child-process-execfile-stdio-ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
process.stdout.write('this should be ignored');
process.stderr.write('this should not be ignored');

} else {
cp.execFile(process.execPath,
[__filename, 'child'],
{ stdio: ['pipe', 'ignore', 'pipe'] },
common.mustCall((err, stdout, stderr) => {
assert.strictEqual(stderr, 'this should not be ignored');
assert.strictEqual(stdout, '');
}));
}
45 changes: 45 additions & 0 deletions test/parallel/test-child-process-execfile-stdio-inherit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;

if (process.argv[2] === 'parent')
parent();
else
grandparent();

function grandparent() {
const input = 'asdfasdf';
const child = execFile(process.execPath, [__filename, 'parent'], (err, stdout, stderr) => {
assert.strictEqual(stdout.trim(), input.trim());
});
child.stderr.pipe(process.stderr);

child.stdin.end(input);
}

function parent() {
// Should not immediately exit.
execFile('cat', [], { stdio: 'inherit' });
}
85 changes: 85 additions & 0 deletions test/parallel/test-child-process-spawn-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');

const assert = require('assert');

const spawn = require('child_process').spawn;

const badOptions = {
input: 1234
};

assert.throws(
() => spawn('cat', [], badOptions),
{ code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });

const stringOptions = {
input: 'hello world'
};

const catString = spawn('cat', [], stringOptions);

catString.stdout.on('data', common.mustCall((data) => {
assert.strictEqual(data.toString('utf8'), stringOptions.input);
}));

catString.on('close', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));

const bufferOptions = {
input: Buffer.from('hello world')
};

const catBuffer = spawn('cat', [], bufferOptions);

catBuffer.stdout.on('data', common.mustCall((data) => {
assert.deepStrictEqual(data, bufferOptions.input);
}));

catBuffer.on('close', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));

// common.getArrayBufferViews expects a buffer
// with length an multiple of 8
const msgBuf = Buffer.from('hello world'.repeat(8));
for (const arrayBufferView of common.getArrayBufferViews(msgBuf)) {
const options = {
input: arrayBufferView
};

const catArrayBufferView = spawn('cat', [], options);

catArrayBufferView.stdout.on('data', common.mustCall((data) => {
assert.deepStrictEqual(data, msgBuf);
}));

catArrayBufferView.on('close', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));
}