shell: rework code to use shell.spawn
spawn gives out streams and we have more control over the stdout/stderr buffers. otherwise, we have to provide a max buffer capture size to exec
This commit is contained in:
+41
-24
@@ -7,32 +7,62 @@ const assert = require('assert'),
|
||||
once = require('./once.js'),
|
||||
util = require('util');
|
||||
|
||||
exports = module.exports = shell;
|
||||
|
||||
function shell(tag) {
|
||||
assert.strictEqual(typeof tag, 'string');
|
||||
|
||||
return {
|
||||
exec: exec.bind(null, tag),
|
||||
spawn: spawn.bind(null, tag),
|
||||
sudo: sudo.bind(null, tag),
|
||||
promises: { sudo: util.promisify(sudo.bind(null, tag)) }
|
||||
};
|
||||
}
|
||||
|
||||
const SUDO = '/usr/bin/sudo';
|
||||
|
||||
// default encoding utf8, no shell, handles input, separate args, wait for process to finish
|
||||
async function execArgs(tag, file, args, options) {
|
||||
// default no shell, handles input, separate args, wait for process to finish
|
||||
function spawn(tag, file, args, options) {
|
||||
assert.strictEqual(typeof tag, 'string');
|
||||
assert.strictEqual(typeof file, 'string');
|
||||
assert(Array.isArray(args));
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
debug(`${tag} execArgs: ${file} ${JSON.stringify(args)}`);
|
||||
debug(`${tag}: ${file} ${JSON.stringify(args)}`);
|
||||
|
||||
const execOptions = Object.assign({ encoding: 'utf8', shell: false }, options);
|
||||
const spawnOptions = Object.assign({ shell: false }, options); // note: no encoding!
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const cp = child_process.execFile(file, args, execOptions, function (error, stdout, stderr) {
|
||||
if (!error) return resolve(stdout);
|
||||
const cp = child_process.spawn(file, args, spawnOptions);
|
||||
const stdoutBuffers = [], stderrBuffers = [];
|
||||
|
||||
const e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
|
||||
cp.stdout.on('data', (data) => stdoutBuffers.push(data));
|
||||
cp.stderr.on('data', (data) => stderrBuffers.push(data));
|
||||
|
||||
cp.on('close', function (code, signal) { // always called. after 'exit' or 'error'
|
||||
const stdoutBuffer = Buffer.concat(stdoutBuffers);
|
||||
const stdout = options.encoding ? stdoutBuffer.toString(options.encoding) : stdoutBuffer;
|
||||
if (code === 0) return resolve(stdout);
|
||||
|
||||
const stderrBuffer = Buffer.concat(stderrBuffers);
|
||||
const stderr = options.encoding ? stderrBuffer.toString(options.encoding) : stderrBuffer;
|
||||
|
||||
const e = new BoxError(BoxError.SHELL_ERROR, `${file} exited with code ${code} signal ${signal}`);
|
||||
e.stdout = stdout; // when promisified, this is the way to get stdout
|
||||
e.stderr = stderr; // when promisified, this is the way to get stderr
|
||||
e.code = error.code;
|
||||
e.signal = error.signal;
|
||||
debug(`${tag}: ${file} with args ${args.join(' ')} errored`, error);
|
||||
e.code = code;
|
||||
e.signal = signal;
|
||||
|
||||
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, e);
|
||||
|
||||
reject(e);
|
||||
});
|
||||
|
||||
cp.on('error', function (error) { // when the command itself could not be started
|
||||
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, error);
|
||||
});
|
||||
|
||||
// https://github.com/nodejs/node/issues/25231
|
||||
if (options.input) {
|
||||
cp.stdin.write(options.input);
|
||||
@@ -50,7 +80,7 @@ async function exec(tag, cmd, options) {
|
||||
if (!options.shell) {
|
||||
cmd = cmd.replace(/\s+/g, ' '); // collapse spaces when not using shell. note: no more complexity like parsing quotes here!
|
||||
const [file, ...args] = cmd.split(' ');
|
||||
return await execArgs(tag, file, args, options);
|
||||
return await spawn(tag, file, args, options);
|
||||
}
|
||||
|
||||
debug(`${tag} exec: ${cmd}`);
|
||||
@@ -145,16 +175,3 @@ function sudo(tag, args, options, callback) {
|
||||
|
||||
return cp;
|
||||
}
|
||||
|
||||
function shell(tag) {
|
||||
assert.strictEqual(typeof tag, 'string');
|
||||
|
||||
return {
|
||||
exec: exec.bind(null, tag),
|
||||
execArgs: execArgs.bind(null, tag),
|
||||
sudo: sudo.bind(null, tag),
|
||||
promises: { sudo: util.promisify(sudo.bind(null, tag)) }
|
||||
};
|
||||
}
|
||||
|
||||
exports = module.exports = shell;
|
||||
|
||||
Reference in New Issue
Block a user