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: validate writable defaultEncoding #46322

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ function WritableState(options, stream, isDuplex) {
// Crypto is kind of old and crusty. Historically, its default string
// encoding is 'binary' so we have to make this configurable.
// Everything else in the universe uses 'utf8', though.
this.defaultEncoding = (options && options.defaultEncoding) || 'utf8';
const defaultEncoding = options?.defaultEncoding;

if (defaultEncoding && !Buffer.isEncoding(defaultEncoding)) {
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
}

this.defaultEncoding = defaultEncoding || 'utf8';
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved

// Not an actual buffer we keep track of, but a measurement
// of how much we're waiting to get pushed to some underlying
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-stream-writable-decoded-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,27 @@ class MyWritable extends stream.Writable {
m.write('some-text', 'utf8');
m.end();
}

{
assert.throws(() => {
const m = new MyWritable(null, {
defaultEncoding: 'my invalid encoding',
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
});
m.end();
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
}, {
code: 'ERR_UNKNOWN_ENCODING',
});
}

{
const w = new MyWritable(function(isBuffer, type, enc) {
assert(!isBuffer);
assert.strictEqual(type, 'string');
assert.strictEqual(enc, 'hex');
}, {
defaultEncoding: 'hex',
decodeStrings: false
});
w.write('asd');
w.end();
}