Skip to content

Commit e413b2b

Browse files
fix(async): validate poolLimit in pooledMap (#7016)
1 parent 4221795 commit e413b2b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

async/pool.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@ const ERROR_WHILE_MAPPING_MESSAGE =
3232
* @typeParam T the input type.
3333
* @typeParam R the output type.
3434
* @param poolLimit The maximum count of items being processed concurrently.
35+
* Must be a positive integer.
3536
* @param array The input array for mapping.
3637
* @param iteratorFn The function to call for every item of the array.
3738
* @returns The async iterator with the transformed values.
39+
* @throws {RangeError} If `poolLimit` is not a positive integer.
3840
*/
3941
export function pooledMap<T, R>(
4042
poolLimit: number,
4143
array: Iterable<T> | AsyncIterable<T>,
4244
iteratorFn: (data: T) => Promise<R>,
4345
): AsyncIterableIterator<R> {
44-
// Create the async iterable that is returned from this function.
46+
if (!Number.isInteger(poolLimit) || poolLimit < 1) {
47+
throw new RangeError("'poolLimit' must be a positive integer");
48+
}
49+
4550
const res = new TransformStream<Promise<R>, R>({
4651
async transform(
4752
p: Promise<R>,

async/pool_test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@ import {
77
assertLess,
88
assertRejects,
99
assertStringIncludes,
10+
assertThrows,
1011
} from "@std/assert";
1112
import { FakeTime } from "@std/testing/time";
1213

14+
Deno.test("pooledMap() throws for invalid poolLimit", () => {
15+
const noop = (i: number) => Promise.resolve(i);
16+
for (const poolLimit of [0, -1, 1.5, NaN, Infinity]) {
17+
assertThrows(
18+
() => pooledMap(poolLimit, [1], noop),
19+
RangeError,
20+
"'poolLimit' must be a positive integer",
21+
);
22+
}
23+
});
24+
1325
Deno.test("pooledMap()", async () => {
1426
using time = new FakeTime();
1527

0 commit comments

Comments
 (0)