Skip to content

Commit b28468e

Browse files
cjihrigrvagg
authored andcommitted
child_process: allow buffer encoding in spawnSync
When the 'buffer' encoding is passed to spawnSync(), an exception is thrown in Buffer's toString() method because 'buffer' is not a valid encoding there. This commit special cases the 'buffer' encoding. Fixes: #6930 PR-URL: #6939 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent db3d2a7 commit b28468e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ function spawnSync(/*file, args, options*/) {
438438

439439
var result = spawn_sync.spawn(options);
440440

441-
if (result.output && options.encoding) {
441+
if (result.output && options.encoding && options.encoding !== 'buffer') {
442442
for (i = 0; i < result.output.length; i++) {
443443
if (!result.output[i])
444444
continue;

test/common.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ exports.spawnPwd = function(options) {
241241
}
242242
};
243243

244+
245+
exports.spawnSyncPwd = function(options) {
246+
const spawnSync = require('child_process').spawnSync;
247+
248+
if (exports.isWindows) {
249+
return spawnSync('cmd.exe', ['/c', 'cd'], options);
250+
} else {
251+
return spawnSync('pwd', [], options);
252+
}
253+
};
254+
244255
exports.platformTimeout = function(ms) {
245256
if (process.config.target_defaults.default_configuration === 'Debug')
246257
ms = 2 * ms;

test/parallel/test-child-process-spawnsync.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ assert.deepStrictEqual(ret_err.spawnargs, ['bar']);
3333

3434
assert.strictEqual(response.stdout.toString().trim(), cwd);
3535
})();
36+
37+
{
38+
// Test the encoding option
39+
const noEncoding = common.spawnSyncPwd();
40+
const bufferEncoding = common.spawnSyncPwd({encoding: 'buffer'});
41+
const utf8Encoding = common.spawnSyncPwd({encoding: 'utf8'});
42+
43+
assert.deepStrictEqual(noEncoding.output, bufferEncoding.output);
44+
assert.deepStrictEqual([
45+
null,
46+
noEncoding.stdout.toString(),
47+
noEncoding.stderr.toString()
48+
], utf8Encoding.output);
49+
}

0 commit comments

Comments
 (0)