denoland/deno · error · RangeError
Cannot enqueue value with size: chunk size is invalid
Error message
Cannot enqueue value with size: chunk size is invalid
What it means
The second guard in enqueueValueWithSize (ext/web/06_streams.js:707-711): if the strategy's size() returns exactly Infinity, RangeError 'Cannot enqueue value with size: chunk size is invalid' is thrown, because an infinite chunk size would poison the queue's queueTotalSize arithmetic. This is required streams-spec behavior. Typical cause is a custom size function that divides by zero or deliberately returns Infinity trying to 'disable backpressure'.
Source
Thrown at ext/web/06_streams.js:708
}
return node.value;
}
/**
* @template T
* @param {{ [_queue]: Array<ValueWithSize<T | _close>>, [_queueTotalSize]: number }} container
* @param {T} value
* @param {number} size
* @returns {void}
*/
function enqueueValueWithSize(container, value, size) {
assert(container[_queue] && typeof container[_queueTotalSize] === "number");
if (isNonNegativeNumber(size) === false) {
throw new RangeError(
"Cannot enqueue value with size: chunk size must be a positive number",
);
}
if (size === Infinity) {
throw new RangeError(
"Cannot enqueue value with size: chunk size is invalid",
);
}
container[_queue].enqueueWithSize(value, size);
container[_queueTotalSize] += size;
}
/**
* @param {QueuingStrategy} strategy
* @param {number} defaultHWM
*/
function extractHighWaterMark(strategy, defaultHWM) {
if (strategy.highWaterMark === undefined) {
return defaultHWM;
}
const highWaterMark = strategy.highWaterMark;
if (NumberIsNaN(highWaterMark) || highWaterMark < 0) {
throw new RangeError(View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Clamp the size to a large finite value: Math.min(size, Number.MAX_SAFE_INTEGER).
- If the goal is 'no backpressure', omit size and pass a large finite highWaterMark instead.
- Guard division: size: (c) => count === 0 ? 1 : c.byteLength / count.
- Test size() against zero-denominator and empty-chunk cases.
Example fix
// before
new WritableStream(sink, { size: () => Infinity }); // RangeError: chunk size is invalid
// after
new WritableStream(sink, { size: () => Number.MAX_SAFE_INTEGER }); Defensive patterns
Strategy: validation
Validate before calling
const size = (c) => {
const n = compute(c);
return Number.isFinite(n) ? Math.max(0, n) : Number.MAX_SAFE_INTEGER;
};
new WritableStream(sink, { size }); Type guard
const isValidChunkSize = (n) => typeof n === 'number' && !Number.isNaN(n) && n >= 0 && n !== Infinity;
Try / catch
try {
await writer.write(chunk);
} catch (e) {
if (e instanceof RangeError && e.message.includes('chunk size is invalid')) {
await writer.write(chunk); // after clamping size()
} else throw e;
} Prevention
- Never return Infinity from size(); clamp to MAX_SAFE_INTEGER
- Use a large finite highWaterMark instead of infinite sizes for no backpressure
- Guard size math against zero denominators
When it happens
Trigger: new WritableStream(sink, { size: () => Infinity }) followed by a write; size: (c) => c.byteLength / count with count === 0; any size computation that overflows or intentionally yields Infinity.
Common situations: Developers trying to bypass backpressure by returning Infinity from size; ratio-based size math with a zero denominator; sizes derived from unbounded inputs.
Related errors
- Cannot enqueue value with size: chunk size must be a positiv
- Expected highWaterMark to be a positive number or Infinity,
- Response body is already used
- Value must be a positive bigint: received ${value}
- Value must fit in a 64-bit unsigned integer
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/4874f68a9ff9f4a5.
Report an issue: GitHub.