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 4bc7025

Browse filesBrowse 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 <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 563efb7 commit 4bc7025
Copy full SHA for 4bc7025

File tree

Expand file treeCollapse file tree

2 files changed

+18
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-7
lines changed
Open diff view settings
Collapse file

‎lib/_stream_writable.js‎

Copy file name to clipboardExpand all lines: lib/_stream_writable.js
+2Lines changed: 2 additions & 0 deletions
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
}
Collapse file

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

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