Skip to content

Commit 4ee863d

Browse files
cjihrigMyles Borins
authored and
Myles Borins
committed
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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 426aa0a commit 4ee863d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/child_process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ function spawnSync(/*file, args, options*/) {
428428

429429
var result = spawn_sync.spawn(options);
430430

431-
if (result.output && options.encoding) {
431+
if (result.output && options.encoding && options.encoding !== 'buffer') {
432432
for (i = 0; i < result.output.length; i++) {
433433
if (!result.output[i])
434434
continue;

test/common.js

+11
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ exports.spawnPwd = function(options) {
237237
}
238238
};
239239

240+
241+
exports.spawnSyncPwd = function(options) {
242+
const spawnSync = require('child_process').spawnSync;
243+
244+
if (exports.isWindows) {
245+
return spawnSync('cmd.exe', ['/c', 'cd'], options);
246+
} else {
247+
return spawnSync('pwd', [], options);
248+
}
249+
};
250+
240251
exports.platformTimeout = function(ms) {
241252
if (process.config.target_defaults.default_configuration === 'Debug')
242253
ms = 2 * ms;

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

+14
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ assert.deepEqual(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)