mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 23:56:58 +00:00
stream: validate writable defaultEncoding
PR-URL: https://github.com/nodejs/node/pull/46322 Fixes: https://github.com/nodejs/node/issues/46301 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
a5fd53f8ef
commit
9e7093f416
@ -122,7 +122,14 @@ function WritableState(options, stream, isDuplex) {
|
|||||||
// Crypto is kind of old and crusty. Historically, its default string
|
// Crypto is kind of old and crusty. Historically, its default string
|
||||||
// encoding is 'binary' so we have to make this configurable.
|
// encoding is 'binary' so we have to make this configurable.
|
||||||
// Everything else in the universe uses 'utf8', though.
|
// Everything else in the universe uses 'utf8', though.
|
||||||
this.defaultEncoding = (options && options.defaultEncoding) || 'utf8';
|
const defaultEncoding = options?.defaultEncoding;
|
||||||
|
if (defaultEncoding == null) {
|
||||||
|
this.defaultEncoding = 'utf8';
|
||||||
|
} else if (Buffer.isEncoding(defaultEncoding)) {
|
||||||
|
this.defaultEncoding = defaultEncoding;
|
||||||
|
} else {
|
||||||
|
throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
|
||||||
|
}
|
||||||
|
|
||||||
// Not an actual buffer we keep track of, but a measurement
|
// Not an actual buffer we keep track of, but a measurement
|
||||||
// of how much we're waiting to get pushed to some underlying
|
// of how much we're waiting to get pushed to some underlying
|
||||||
|
@ -56,3 +56,50 @@ class MyWritable extends stream.Writable {
|
|||||||
m.write('some-text', 'utf8');
|
m.write('some-text', 'utf8');
|
||||||
m.end();
|
m.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
assert.throws(() => {
|
||||||
|
const m = new MyWritable(null, {
|
||||||
|
defaultEncoding: 'my invalid encoding',
|
||||||
|
});
|
||||||
|
m.end();
|
||||||
|
}, {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const w = new MyWritable(function(isBuffer, type, enc) {
|
||||||
|
assert(!isBuffer);
|
||||||
|
assert.strictEqual(type, 'string');
|
||||||
|
assert.strictEqual(enc, 'utf8');
|
||||||
|
}, {
|
||||||
|
defaultEncoding: null,
|
||||||
|
decodeStrings: false
|
||||||
|
});
|
||||||
|
w.write('asd');
|
||||||
|
w.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const m = new MyWritable(function(isBuffer, type, enc) {
|
||||||
|
assert.strictEqual(type, 'object');
|
||||||
|
assert.strictEqual(enc, 'utf8');
|
||||||
|
}, { defaultEncoding: 'hex',
|
||||||
|
objectMode: true });
|
||||||
|
m.write({ foo: 'bar' }, 'utf8');
|
||||||
|
m.end();
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user