stream: use validateString for consumer encoding

Replace the manual `options.encoding` type check in stream iterator
consumers with the shared validator. Add coverage for non-string values
in the async and sync text consumers.

Signed-off-by: sjungwon03 <sjungwon03@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64754
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
Jungwon Sohn authored and Antoine du Hamel committed 2026-08-05 11:15:04 +02:00
1 parent c816918e14
commit ff12b8e22e
2 files changed
+18 -5

No files matched your search

+2 -5
View File
@@ -28,7 +28,6 @@ const {
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
},
@@ -39,6 +38,7 @@ const {
validateFunction,
validateInteger,
validateObject,
validateString,
} = require('internal/validators');
const {
@@ -196,10 +196,7 @@ function validateBaseConsumerOptions(options) {
validateInteger(options.limit, 'options.limit', 0);
}
if (options.encoding !== undefined) {
if (typeof options.encoding !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.encoding', 'string',
options.encoding);
}
validateString(options.encoding, 'options.encoding');
try {
new TextDecoder(options.encoding);
} catch {
@@ -143,6 +143,20 @@ function testTextSyncUnsupportedEncodingThrowsRangeError() {
);
}
async function testTextNonStringEncodingThrowsTypeError() {
await assert.rejects(
() => text(from('hello'), { encoding: 1 }),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
}
function testTextSyncNonStringEncodingThrowsTypeError() {
assert.throws(
() => textSync(fromSync('hello'), { encoding: 1 }),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
}
Promise.all([
testTextSyncBasic(),
testTextAsync(),
@@ -159,4 +173,6 @@ Promise.all([
testTextSyncBOMStripped(),
testTextUnsupportedEncodingThrowsRangeError(),
testTextSyncUnsupportedEncodingThrowsRangeError(),
testTextNonStringEncodingThrowsTypeError(),
testTextSyncNonStringEncodingThrowsTypeError(),
]).then(common.mustCall());