{"record":{"id":"c7ab8d8f80c7656a","repo":"denoland/deno","slug":"the-region-specified-by-view-does-not-match-byobre","errorCode":null,"errorMessage":"The region specified by view does not match byobRequest","messagePattern":"The region specified by view does not match byobRequest","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":2817,"sourceCode":"  const firstDescriptor = controller[_pendingPullIntos].peek();\n  const state = controller[_stream][_state];\n  if (state === \"closed\") {\n    if (byteLength !== 0) {\n      throw new TypeError(\n        `The view's length must be 0 when calling respondWithNewView() on a closed stream: received ${byteLength}`,\n      );\n    }\n  } else {\n    assert(state === \"readable\");\n    if (byteLength === 0) {\n      throw new TypeError(\n        \"The view's length must be greater than 0 when calling respondWithNewView() on a readable stream\",\n      );\n    }\n  }\n  // deno-lint-ignore deno-internal/prefer-primordials\n  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== byteOffset) {\n    throw new RangeError(\n      \"The region specified by view does not match byobRequest\",\n    );\n  }\n  if (firstDescriptor.bufferByteLength !== getArrayBufferByteLength(buffer)) {\n    throw new RangeError(\n      \"The buffer of view has different capacity than byobRequest\",\n    );\n  }\n  // deno-lint-ignore deno-internal/prefer-primordials\n  if (firstDescriptor.bytesFilled + byteLength > firstDescriptor.byteLength) {\n    throw new RangeError(\n      \"The region specified by view is larger than byobRequest\",\n    );\n  }\n  firstDescriptor.buffer = ArrayBufferPrototypeTransferToFixedLength(buffer);\n  readableByteStreamControllerRespondInternal(controller, byteLength);\n}\n","sourceCodeStart":2799,"sourceCodeEnd":2835,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L2799-L2835","documentation":"RangeError from respondWithNewView(view): the view's byteOffset does not equal firstDescriptor.byteOffset + firstDescriptor.bytesFilled. The BYOB protocol requires the replacement view to start exactly where the request left off inside the consumer's buffer; any other offset is rejected before the buffer is transferred back.","triggerScenarios":"Passing a view at offset 0 when a partial fill already advanced the fill point; passing a view over a different buffer; constructing a fresh Uint8Array for the response instead of reusing the request's own view; slicing with the wrong start index.","commonSituations":"Decode/encode steps that wrap output in a newly allocated view; responding with view.subarray(k) for the wrong k; copying request templates from non-BYOB example code.","solutions":["Default to byob.respond(n) — offset bookkeeping is then handled for you.","When you must supply a new view, derive it from the request's own view so the offset matches the fill point.","Verify view.byteOffset matches byob.view.byteOffset (plus any bytes already filled) before responding."],"exampleFix":"// before\nconst out = new Uint8Array(n); // offset 0, fresh buffer\nbyob.respondWithNewView(out);   // fill point is not 0 -> RangeError\n\n// after\nconst v = byob.view;         // the request's own view, correct offset\nwriteInto(v);                // fill starting at the current fill point\nbyob.respond(v.byteLength);","handlingStrategy":"validation","validationCode":"// the new view must start where the request left off\nif (view.byteOffset !== byob.view.byteOffset) {\n  view = byob.view; // fall back to the handed-out view\n}\nbyob.respondWithNewView(view);","typeGuard":null,"tryCatchPattern":"try {\n  byob.respondWithNewView(view);\n} catch (e) {\n  if (e instanceof RangeError && e.message.includes('does not match byobRequest')) {\n    byob.respond(byob.view.byteLength); // respond with the original view instead\n    return;\n  }\n  throw e;\n}","preventionTips":["respondWithNewView expects a region of the same buffer at the fill point.","When in doubt, write into byob.view and use respond(n) — offsets are handled for you.","Never respondWithNewView a freshly allocated view."],"tags":["readable-stream","byob-reader","respondwithnewview","rangeerror","byte-offset"],"backgroundTag":"byob-request-respond-misuse","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T04:17:50.494Z"}