Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: write should throw on unknown encoding #33075

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/_stream_writable.js
Expand Up @@ -264,6 +264,8 @@ Writable.prototype.write = function(chunk, encoding, cb) {
} else {
if (!encoding)
encoding = state.defaultEncoding;
else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding))
throw new ERR_UNKNOWN_ENCODING(encoding);
if (typeof cb !== 'function')
cb = nop;
}
Expand Down
23 changes: 16 additions & 7 deletions test/parallel/test-stream-writable-write-error.js
Expand Up @@ -4,17 +4,17 @@ const assert = require('assert');

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

function expectError(w, arg, code, sync) {
function expectError(w, args, code, sync) {
if (sync) {
if (code) {
assert.throws(() => w.write(arg), { code });
assert.throws(() => w.write(...args), { code });
} else {
w.write(arg);
w.write(...args);
}
} else {
let errorCalled = false;
let ticked = false;
w.write(arg, common.mustCall((err) => {
w.write(...args, common.mustCall((err) => {
assert.strictEqual(ticked, true);
assert.strictEqual(errorCalled, false);
assert.strictEqual(err.code, code);
Expand All @@ -34,7 +34,7 @@ function test(autoDestroy) {
_write() {}
});
w.end();
expectError(w, 'asd', 'ERR_STREAM_WRITE_AFTER_END');
expectError(w, ['asd'], 'ERR_STREAM_WRITE_AFTER_END');
}

{
Expand All @@ -50,15 +50,24 @@ function test(autoDestroy) {
autoDestroy,
_write() {}
});
expectError(w, null, 'ERR_STREAM_NULL_VALUES', true);
expectError(w, [null], 'ERR_STREAM_NULL_VALUES', true);
}

{
const w = new Writable({
autoDestroy,
_write() {}
});
expectError(w, {}, 'ERR_INVALID_ARG_TYPE', true);
expectError(w, [{}], 'ERR_INVALID_ARG_TYPE', true);
}

{
const w = new Writable({
decodeStrings: false,
autoDestroy,
_write() {}
});
expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true);
}
}

Expand Down