Skip to content

Commit 4bc7025

Browse files
committed
stream: write should throw on unknown encoding
Validate encoding passed to write(chunk, encoding, cb) and throw if it is invalid. PR-URL: #33075 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 563efb7 commit 4bc7025

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Diff for: lib/_stream_writable.js

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ Writable.prototype.write = function(chunk, encoding, cb) {
268268
} else {
269269
if (!encoding)
270270
encoding = state.defaultEncoding;
271+
else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding))
272+
throw new ERR_UNKNOWN_ENCODING(encoding);
271273
if (typeof cb !== 'function')
272274
cb = nop;
273275
}

Diff for: test/parallel/test-stream-writable-write-error.js

+16-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,24 @@ 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+
decodeStrings: false,
67+
autoDestroy,
68+
_write() {}
69+
});
70+
expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true);
6271
}
6372
}
6473

0 commit comments

Comments
 (0)