Skip to content

Commit 163140a

Browse files
committed
stream: use validateObject for zlib/iter params
The kValidateObjectAllowArray flag matches the replaced check: arrays keep passing and the thrown error is unchanged. Signed-off-by: greenhead <shren0812@gmail.com>
1 parent 31cde9f commit 163140a

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

lib/internal/streams/iter/transform.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const { isArrayBufferView, isAnyArrayBuffer } = require('internal/util/types');
4040
const { kValidatedTransform } = require('internal/streams/iter/types');
4141
const {
4242
checkRangesOrGetDefault,
43+
kValidateObjectAllowArray,
4344
validateFiniteNumber,
4445
validateObject,
4546
} = require('internal/validators');
@@ -106,9 +107,7 @@ function validateDictionary(dictionary) {
106107

107108
function validateParams(params, maxParam, errClass) {
108109
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);
112111
const keys = ObjectKeys(params);
113112
for (let i = 0; i < keys.length; i++) {
114113
const origKey = keys[i];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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());

0 commit comments

Comments
 (0)