{"record":{"id":"4874f68a9ff9f4a5","repo":"denoland/deno","slug":"cannot-enqueue-value-with-size-chunk-size-is-inva","errorCode":null,"errorMessage":"Cannot enqueue value with size: chunk size is invalid","messagePattern":"Cannot enqueue value with size: chunk size is invalid","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":708,"sourceCode":"  }\n  return node.value;\n}\n/**\n * @template T\n * @param {{ [_queue]: Array<ValueWithSize<T | _close>>, [_queueTotalSize]: number }} container\n * @param {T} value\n * @param {number} size\n * @returns {void}\n */\nfunction enqueueValueWithSize(container, value, size) {\n  assert(container[_queue] && typeof container[_queueTotalSize] === \"number\");\n  if (isNonNegativeNumber(size) === false) {\n    throw new RangeError(\n      \"Cannot enqueue value with size: chunk size must be a positive number\",\n    );\n  }\n  if (size === Infinity) {\n    throw new RangeError(\n      \"Cannot enqueue value with size: chunk size is invalid\",\n    );\n  }\n  container[_queue].enqueueWithSize(value, size);\n  container[_queueTotalSize] += size;\n}\n\n/**\n * @param {QueuingStrategy} strategy\n * @param {number} defaultHWM\n */\nfunction extractHighWaterMark(strategy, defaultHWM) {\n  if (strategy.highWaterMark === undefined) {\n    return defaultHWM;\n  }\n  const highWaterMark = strategy.highWaterMark;\n  if (NumberIsNaN(highWaterMark) || highWaterMark < 0) {\n    throw new RangeError(","sourceCodeStart":690,"sourceCodeEnd":726,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L690-L726","documentation":"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'.","triggerScenarios":"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.","commonSituations":"Developers trying to bypass backpressure by returning Infinity from size; ratio-based size math with a zero denominator; sizes derived from unbounded inputs.","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."],"exampleFix":"// before\nnew WritableStream(sink, { size: () => Infinity }); // RangeError: chunk size is invalid\n\n// after\nnew WritableStream(sink, { size: () => Number.MAX_SAFE_INTEGER });","handlingStrategy":"validation","validationCode":"const size = (c) => {\n  const n = compute(c);\n  return Number.isFinite(n) ? Math.max(0, n) : Number.MAX_SAFE_INTEGER;\n};\nnew WritableStream(sink, { size });","typeGuard":"const isValidChunkSize = (n) =>\n  typeof n === 'number' && !Number.isNaN(n) && n >= 0 && n !== Infinity;","tryCatchPattern":"try {\n  await writer.write(chunk);\n} catch (e) {\n  if (e instanceof RangeError && e.message.includes('chunk size is invalid')) {\n    await writer.write(chunk); // after clamping size()\n  } else throw e;\n}","preventionTips":["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"],"tags":["streams","queuing-strategy","size","rangeerror"],"backgroundTag":"stream-queuing-strategy-invalid","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}