{"record":{"id":"035959058ceee8b0","repo":"denoland/deno","slug":"cannot-enqueue-value-with-size-chunk-size-must-be","errorCode":null,"errorMessage":"Cannot enqueue value with size: chunk size must be a positive number","messagePattern":"Cannot enqueue value with size: chunk size must be a positive number","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":703,"sourceCode":"  assert(container[_queue].size);\n  const node = container[_queue].dequeueNode();\n  container[_queueTotalSize] -= node.size;\n  if (container[_queueTotalSize] < 0) {\n    container[_queueTotalSize] = 0;\n  }\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) {","sourceCodeStart":685,"sourceCodeEnd":721,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L685-L721","documentation":"enqueueValueWithSize implements the streams-spec enqueue step: whatever the strategy's size() returns must be a number that is not NaN and not negative (isNonNegativeNumber, ext/web/06_streams.js:702-706), otherwise RangeError 'Cannot enqueue value with size: chunk size must be a positive number'. It runs for every controller.enqueue()/writer.write() on a stream with a custom size function. Despite the wording, 0 is allowed — only NaN and negative values throw here.","triggerScenarios":"new ReadableStream(src, { size: (c) => c.byteLength }) with string chunks (c.byteLength is undefined -> not a non-negative number); size: () => -1 from an arithmetic bug; size returning NaN from missing fields.","commonSituations":"Size functions written for byte chunks reused on object/string streams; size computed from ratios where operands can be missing; refactors that change chunk shape without updating the strategy.","solutions":["Make size() total: return c.byteLength when it is a number, otherwise 1 — `(typeof c.byteLength === 'number' ? c.byteLength : 1)`.","Clamp custom sizes: Math.max(0, computed).","Unit-test the size function against every chunk shape the stream actually carries.","Remember 0 is legal; NaN and negatives are not."],"exampleFix":"// before\nnew ReadableStream(src, { size: (c) => c.byteLength }); // string chunk -> undefined -> throw\n\n// after\nnew ReadableStream(src, {\n  size: (c) => (typeof c.byteLength === 'number' ? c.byteLength : 1),\n});","handlingStrategy":"validation","validationCode":"const size = (c) => {\n  const n = typeof c.byteLength === 'number' ? c.byteLength : 1;\n  return Number.isFinite(n) && n >= 0 ? n : 1;\n};\nnew WritableStream(sink, { highWaterMark: 4, size });","typeGuard":"const isValidChunkSize = (n) =>\n  typeof n === 'number' && !Number.isNaN(n) && n >= 0 && n !== Infinity;","tryCatchPattern":"try {\n  controller.enqueue(chunk);\n} catch (e) {\n  if (e instanceof RangeError && e.message.includes('chunk size')) {\n    fixSizeFunction();\n  } else throw e;\n}","preventionTips":["Fall back to 1 in size() when byteLength is missing","Clamp custom sizes with Math.max(0, n)","Unit-test size() against every chunk shape the stream carries"],"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"}