Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4ee863d

Browse filesBrowse files
cjihrigMyles Borins
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 426aa0a commit 4ee863d
Copy full SHA for 4ee863d

File tree

Expand file treeCollapse file tree

3 files changed

+26
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-1
lines changed
Open diff view settings
Collapse file

‎lib/child_process.js‎

Copy file name to clipboardExpand all lines: lib/child_process.js
+1-1Lines changed: 1 addition & 1 deletion
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;
Collapse file

‎test/common.js‎

Copy file name to clipboardExpand all lines: test/common.js
+11Lines changed: 11 additions & 0 deletions
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;
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-spawnsync.js
+14Lines changed: 14 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.