GeezerVison is showing github.com — stream: use validateObject for zlib/iter params · nodejs/node@74d53bd
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/transform.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,7 @@ const { isArrayBufferView, isAnyArrayBuffer } = require('internal/util/types'); | 40 | 40 | const { kValidatedTransform } = require('internal/streams/iter/types'); | 41 | 41 | const { | 42 | 42 | checkRangesOrGetDefault, | | 43 | + kValidateObjectAllowArray, | 43 | 44 | validateFiniteNumber, | 44 | 45 | validateObject, | 45 | 46 | } = require('internal/validators'); | @@ -106,9 +107,7 @@ function validateDictionary(dictionary) { | 106 | 107 |
| 107 | 108 | function validateParams(params, maxParam, errClass) { | 108 | 109 | if (params === undefined) return; | 109 | | - if (typeof params !== 'object' || params === null) { | 110 | | - throw new ERR_INVALID_ARG_TYPE('options.params', 'Object', params); | 111 | | - } | | 110 | + validateObject(params, 'options.params', kValidateObjectAllowArray); | 112 | 111 | const keys = ObjectKeys(params); | 113 | 112 | for (let i = 0; i < keys.length; i++) { | 114 | 113 | const origKey = keys[i]; | |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --experimental-stream-iter | | 2 | +'use strict'; | | 3 | + | | 4 | +const common = require('../common'); | | 5 | +const assert = require('assert'); | | 6 | +const { from, pull, bytes } = require('stream/iter'); | | 7 | +const { compressBrotli, compressZstd } = require('zlib/iter'); | | 8 | + | | 9 | +// Type validation of options.params in zlib/iter transforms: plain | | 10 | +// objects and arrays pass the check, any other value rejects with | | 11 | +// ERR_INVALID_ARG_TYPE. Arrays have always passed the typeof-based | | 12 | +// check, so this behavior must be preserved by any refactor. | | 13 | + | | 14 | +const consume = (transform) => bytes(pull(from('test'), transform)); | | 15 | + | | 16 | +(async () => { | | 17 | + for (const compress of [compressBrotli, compressZstd]) { | | 18 | + for (const params of [42, 'bad', true, Symbol(), () => {}, null]) { | | 19 | + await assert.rejects( | | 20 | + consume(compress({ params })), | | 21 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 22 | + ); | | 23 | + } | | 24 | + | | 25 | + // An empty array has no own keys, so it passes both the type check | | 26 | + // and the per-key validation and compression succeeds. | | 27 | + const out = await consume(compress({ params: [] })); | | 28 | + assert.ok(out.byteLength > 0); | | 29 | + } | | 30 | +})().then(common.mustCall()); |
|