Skip to content

Commit ff12b8e

Browse files
sjungwon03aduh95
authored andcommitted
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: #64754 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent c816918 commit ff12b8e

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎lib/internal/streams/iter/consumers.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const {
2828

2929
const{
3030
codes: {
31-
ERR_INVALID_ARG_TYPE,
3231
ERR_INVALID_ARG_VALUE,
3332
ERR_OUT_OF_RANGE,
3433
},
@@ -39,6 +38,7 @@ const {
3938
validateFunction,
4039
validateInteger,
4140
validateObject,
41+
validateString,
4242
}=require('internal/validators');
4343

4444
const{
@@ -196,10 +196,7 @@ function validateBaseConsumerOptions(options) {
196196
validateInteger(options.limit,'options.limit',0);
197197
}
198198
if(options.encoding!==undefined){
199-
if(typeofoptions.encoding!=='string'){
200-
thrownewERR_INVALID_ARG_TYPE('options.encoding','string',
201-
options.encoding);
202-
}
199+
validateString(options.encoding,'options.encoding');
203200
try{
204201
newTextDecoder(options.encoding);
205202
}catch{

‎test/parallel/test-stream-iter-consumers-text.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ function testTextSyncUnsupportedEncodingThrowsRangeError() {
143143
);
144144
}
145145

146+
asyncfunctiontestTextNonStringEncodingThrowsTypeError(){
147+
awaitassert.rejects(
148+
()=>text(from('hello'),{encoding: 1}),
149+
{code: 'ERR_INVALID_ARG_TYPE'},
150+
);
151+
}
152+
153+
functiontestTextSyncNonStringEncodingThrowsTypeError(){
154+
assert.throws(
155+
()=>textSync(fromSync('hello'),{encoding: 1}),
156+
{code: 'ERR_INVALID_ARG_TYPE'},
157+
);
158+
}
159+
146160
Promise.all([
147161
testTextSyncBasic(),
148162
testTextAsync(),
@@ -159,4 +173,6 @@ Promise.all([
159173
testTextSyncBOMStripped(),
160174
testTextUnsupportedEncodingThrowsRangeError(),
161175
testTextSyncUnsupportedEncodingThrowsRangeError(),
176+
testTextNonStringEncodingThrowsTypeError(),
177+
testTextSyncNonStringEncodingThrowsTypeError(),
162178
]).then(common.mustCall());

0 commit comments

Comments
 (0)