From 4913e0f9d3feaf4e6080d2283a4a94f483f5bb97 Mon Sep 17 00:00:00 2001 From: sjungwon03 Date: Sun, 26 Jul 2026 17:54:17 +0900 Subject: [PATCH] stream: use validateString for consumer encoding Signed-off-by: sjungwon03 --- lib/internal/streams/iter/consumers.js | 7 ++----- test/parallel/test-stream-iter-consumers-text.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/internal/streams/iter/consumers.js b/lib/internal/streams/iter/consumers.js index 3894b3fa24a8c4..e3bd7856dcd4d5 100644 --- a/lib/internal/streams/iter/consumers.js +++ b/lib/internal/streams/iter/consumers.js @@ -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 { diff --git a/test/parallel/test-stream-iter-consumers-text.js b/test/parallel/test-stream-iter-consumers-text.js index 16fca64142a442..a8fb7a74367fcd 100644 --- a/test/parallel/test-stream-iter-consumers-text.js +++ b/test/parallel/test-stream-iter-consumers-text.js @@ -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());