Skip to content

Commit af924e2

Browse files
committed
fix: formatBytes NaN/Infinity guard + worker-pool constructor validation
- formatBytes returns "0B" for NaN, Infinity, and negative inputs - R2Partitioner throws QueryModeError if partitionCount < 1 - WorkerPool throws QueryModeError if maxFanOut < 1
1 parent 8a6c7fb commit af924e2

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/format.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface LocalTimingInfo {
2020

2121
/** Format bytes into a human-readable string. */
2222
export function formatBytes(n: number): string {
23+
if (!Number.isFinite(n) || n < 0) return "0B";
2324
if (n === 0) return "0B";
2425
if (n < 1024) return `${n}B`;
2526
if (n < 1024 * 1024) return `${(n / 1024).toFixed(n < 10240 ? 1 : 0)}KB`;

src/worker-pool.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Row } from "./types.js";
22
import { encodeColumnarRun, decodeColumnarRun } from "./r2-spill.js";
3+
import { QueryModeError } from "./errors.js";
34

45
/** A partition of data stored in R2 */
56
export interface R2Partition {
@@ -21,6 +22,7 @@ export class R2Partitioner {
2122
private partitionBytesWritten: number[];
2223

2324
constructor(bucket: R2Bucket, prefix: string, partitionCount: number) {
25+
if (partitionCount < 1) throw new QueryModeError("QUERY_FAILED", "R2Partitioner: partitionCount must be >= 1");
2426
this.bucket = bucket;
2527
this.prefix = prefix;
2628
this.partitionCount = partitionCount;
@@ -115,7 +117,9 @@ export class WorkerPool {
115117
}) {
116118
this.bucket = opts.bucket;
117119
this.doNamespace = opts.doNamespace;
118-
this.maxFanOut = opts.maxFanOut ?? 50;
120+
const fanOut = opts.maxFanOut ?? 50;
121+
if (fanOut < 1) throw new QueryModeError("QUERY_FAILED", "WorkerPool: maxFanOut must be >= 1");
122+
this.maxFanOut = fanOut;
119123
}
120124

121125
/**

0 commit comments

Comments
 (0)