{"record":{"id":"acc179588bf6c2b4","repo":"denoland/deno","slug":"byteswritten-out-of-range","errorCode":null,"errorMessage":"\"bytesWritten\" out of range","messagePattern":"\"bytesWritten\" out of range","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":2609,"sourceCode":"  if (state === \"closed\") {\n    if (bytesWritten !== 0) {\n      throw new TypeError(\n        `\"bytesWritten\" must be 0 when calling respond() on a closed stream: received ${bytesWritten}`,\n      );\n    }\n  } else {\n    assert(state === \"readable\");\n    if (bytesWritten === 0) {\n      throw new TypeError(\n        '\"bytesWritten\" must be greater than 0 when calling respond() on a readable stream',\n      );\n    }\n    if (\n      (firstDescriptor.bytesFilled + bytesWritten) >\n        // deno-lint-ignore deno-internal/prefer-primordials\n        firstDescriptor.byteLength\n    ) {\n      throw new RangeError('\"bytesWritten\" out of range');\n    }\n  }\n  firstDescriptor.buffer = ArrayBufferPrototypeTransferToFixedLength(\n    // deno-lint-ignore deno-internal/prefer-primordials\n    firstDescriptor.buffer,\n  );\n  readableByteStreamControllerRespondInternal(controller, bytesWritten);\n}\n\n/**\n * @param {ReadableByteStreamController} controller\n * @param {number} bytesWritten\n * @param {PullIntoDescriptor} pullIntoDescriptor\n * @returns {void}\n */\nfunction readableByteStreamControllerRespondInReadableState(\n  controller,\n  bytesWritten,","sourceCodeStart":2591,"sourceCodeEnd":2627,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L2591-L2627","documentation":"RangeError from ReadableStreamBYOBRequest.respond(bytesWritten): firstDescriptor.bytesFilled + bytesWritten exceeds firstDescriptor.byteLength, meaning you reported more bytes than fit in the capacity handed out in byobRequest.view. The descriptor tracks how much of the consumer's view is already filled; over-reporting would claim memory beyond the view.","triggerScenarios":"Passing bytesWritten greater than byobRequest.view.byteLength; summing bytes across several partial reads into one respond(); confusing units (bits vs bytes, total file offset vs chunk length); ignoring that an earlier partial fill already consumed part of the view.","commonSituations":"Read loops that accumulate totals and respond once; porting Node 'bytesWritten' semantics; responding with a payload length plus header size that exceeds the view.","solutions":["Respond with this read call's count only: byob.respond(bytesReadThisCall).","Clamp before responding: bytesWritten = Math.min(bytesWritten, byob.view.byteLength).","When coalescing partial writes, keep a local fill counter and respond with only what fits; leave the rest for the next pull()."],"exampleFix":"// before\nlet total = 0;\nfor (const part of parts) { total += part.length; copyInto(byob.view, part); }\nbyob.respond(total); // exceeds view capacity -> RangeError\n\n// after\nlet filled = 0;\nfor (const part of parts) {\n  if (filled + part.length > byob.view.byteLength) break;\n  copyInto(byob.view.subarray(filled), part);\n  filled += part.length;\n}\nbyob.respond(filled);","handlingStrategy":"validation","validationCode":"const cap = byob.view.byteLength;        // capacity handed out\nconst n = Math.min(bytesWritten, cap); // never over-report\nif (n > 0) byob.respond(n);\nelse controller.close();","typeGuard":null,"tryCatchPattern":"try {\n  byob.respond(n);\n} catch (e) {\n  if (e instanceof RangeError && e.message.includes('out of range')) {\n    byob.respond(byob.view.byteLength); // retry with the valid maximum\n    return;\n  }\n  throw e;\n}","preventionTips":["respond() takes this operation's byte count, never an accumulator.","Keep a local fill counter when coalescing partial writes.","Assert n <= byob.view.byteLength in development builds."],"tags":["readable-stream","byob-reader","respond","rangeerror","buffer-overflow"],"backgroundTag":"byob-request-respond-misuse","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}