Skip to content

Commit 2c47817

Browse files
committed
stream: write should throw on unknown encoding
Validate encoding passed to write(chunk, encoding, cb) and throw if it is invalid.
1 parent ab7d9db commit 2c47817

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/_stream_writable.js

+2
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ Writable.prototype.write = function(chunk, encoding, cb) {
264264
} else {
265265
if (!encoding)
266266
encoding = state.defaultEncoding;
267+
else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding))
268+
throw new ERR_UNKNOWN_ENCODING(encoding);
267269
if (typeof cb !== 'function')
268270
cb = nop;
269271
}

test/parallel/test-stream-writable-write-error.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ const assert = require('assert');
44

55
const { Writable } = require('stream');
66

7-
function expectError(w, arg, code, sync) {
7+
function expectError(w, args, code, sync) {
88
if (sync) {
99
if (code) {
10-
assert.throws(() => w.write(arg), { code });
10+
assert.throws(() => w.write(...args), { code });
1111
} else {
12-
w.write(arg);
12+
w.write(...args);
1313
}
1414
} else {
1515
let errorCalled = false;
1616
let ticked = false;
17-
w.write(arg, common.mustCall((err) => {
17+
w.write(...args, common.mustCall((err) => {
1818
assert.strictEqual(ticked, true);
1919
assert.strictEqual(errorCalled, false);
2020
assert.strictEqual(err.code, code);
@@ -34,7 +34,7 @@ function test(autoDestroy) {
3434
_write() {}
3535
});
3636
w.end();
37-
expectError(w, 'asd', 'ERR_STREAM_WRITE_AFTER_END');
37+
expectError(w, ['asd'], 'ERR_STREAM_WRITE_AFTER_END');
3838
}
3939

4040
{
@@ -50,15 +50,23 @@ function test(autoDestroy) {
5050
autoDestroy,
5151
_write() {}
5252
});
53-
expectError(w, null, 'ERR_STREAM_NULL_VALUES', true);
53+
expectError(w, [null], 'ERR_STREAM_NULL_VALUES', true);
5454
}
5555

5656
{
5757
const w = new Writable({
5858
autoDestroy,
5959
_write() {}
6060
});
61-
expectError(w, {}, 'ERR_INVALID_ARG_TYPE', true);
61+
expectError(w, [{}], 'ERR_INVALID_ARG_TYPE', true);
62+
}
63+
64+
{
65+
const w = new Writable({
66+
autoDestroy,
67+
_write() {}
68+
});
69+
expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true);
6270
}
6371
}
6472

0 commit comments

Comments
 (0)