GeezerVison is showing github.com — stream: use validateString for consumer encoding · nodejs/node@1e182d1
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/consumers.js +2-5Lines changed: 2 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,6 @@ const { | 28 | 28 |
| 29 | 29 | const { | 30 | 30 | codes: { | 31 | | - ERR_INVALID_ARG_TYPE, | 32 | 31 | ERR_INVALID_ARG_VALUE, | 33 | 32 | ERR_OUT_OF_RANGE, | 34 | 33 | }, | @@ -39,6 +38,7 @@ const { | 39 | 38 | validateFunction, | 40 | 39 | validateInteger, | 41 | 40 | validateObject, | | 41 | + validateString, | 42 | 42 | } = require('internal/validators'); | 43 | 43 |
| 44 | 44 | const { | @@ -196,10 +196,7 @@ function validateBaseConsumerOptions(options) { | 196 | 196 | validateInteger(options.limit, 'options.limit', 0); | 197 | 197 | } | 198 | 198 | if (options.encoding !== undefined) { | 199 | | - if (typeof options.encoding !== 'string') { | 200 | | - throw new ERR_INVALID_ARG_TYPE('options.encoding', 'string', | 201 | | - options.encoding); | 202 | | - } | | 199 | + validateString(options.encoding, 'options.encoding'); | 203 | 200 | try { | 204 | 201 | new TextDecoder(options.encoding); | 205 | 202 | } catch { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-consumers-text.js +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -143,6 +143,20 @@ function testTextSyncUnsupportedEncodingThrowsRangeError() { | 143 | 143 | ); | 144 | 144 | } | 145 | 145 |
| | 146 | +async function testTextNonStringEncodingThrowsTypeError() { | | 147 | + await assert.rejects( | | 148 | + () => text(from('hello'), { encoding: 1 }), | | 149 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 150 | + ); | | 151 | +} | | 152 | + | | 153 | +function testTextSyncNonStringEncodingThrowsTypeError() { | | 154 | + assert.throws( | | 155 | + () => textSync(fromSync('hello'), { encoding: 1 }), | | 156 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 157 | + ); | | 158 | +} | | 159 | + | 146 | 160 | Promise.all([ | 147 | 161 | testTextSyncBasic(), | 148 | 162 | testTextAsync(), | @@ -159,4 +173,6 @@ Promise.all([ | 159 | 173 | testTextSyncBOMStripped(), | 160 | 174 | testTextUnsupportedEncodingThrowsRangeError(), | 161 | 175 | testTextSyncUnsupportedEncodingThrowsRangeError(), | | 176 | + testTextNonStringEncodingThrowsTypeError(), | | 177 | + testTextSyncNonStringEncodingThrowsTypeError(), | 162 | 178 | ]).then(common.mustCall()); |
|