{"record":{"id":"49dbeabf9cdf122a","repo":"denoland/deno","slug":"the-buffer-of-view-has-different-capacity-than-byo","errorCode":null,"errorMessage":"The buffer of view has different capacity than byobRequest","messagePattern":"The buffer of view has different capacity than byobRequest","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":2822,"sourceCode":"        `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\n/**\n * @param {ReadableByteStreamController} controller\n * @returns {PullIntoDescriptor}\n */\nfunction readableByteStreamControllerShiftPendingPullInto(controller) {","sourceCodeStart":2804,"sourceCodeEnd":2840,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L2804-L2840","documentation":"RangeError from respondWithNewView(view): the view's backing ArrayBuffer has a different byteLength than the pending pull-into's bufferByteLength (the capacity when the read started). The replacement view must live over the same-sized buffer — normally the exact buffer handed out — so the transfer back to the consumer stays consistent.","triggerScenarios":"Wrapping output in a newly allocated ArrayBuffer instead of using the request's buffer; detaching and recreating the consumer buffer between read(view) and respond; re-wrapping a resized buffer after transfer.","commonSituations":"Decode steps that allocate a new buffer per chunk; Node-style 'allocate a result and return it' patterns applied to BYOB responses; growable buffers resized in between.","solutions":["Write into byobRequest.view and call respond(n) — allocation-free and always consistent.","If you must respondWithNewView, use a view over the request's original buffer with the same capacity.","Never allocate a fresh buffer as the response region."],"exampleFix":"// before\nconst decoded = decodeIntoNewBuffer(byob.view);  // allocates a new buffer\nbyob.respondWithNewView(new Uint8Array(decoded)); // capacity differs -> RangeError\n\n// after\nconst n = decodeInto(byob.view); // writes into the request's buffer\nbyob.respond(n);","handlingStrategy":"validation","validationCode":"if (view.buffer !== byob.view.buffer) {\n  view = byob.view; // response region must live in the handed-out buffer\n}\nbyob.respondWithNewView(view);","typeGuard":null,"tryCatchPattern":"try {\n  byob.respondWithNewView(view);\n} catch (e) {\n  if (e instanceof RangeError && e.message.includes('different capacity')) {\n    byob.respond(byob.view.byteLength);\n    return;\n  }\n  throw e;\n}","preventionTips":["Allocate nothing for the response region — write into the request's own buffer.","Prefer respond(bytesWritten) whenever the write stayed inside the request's view."],"tags":["readable-stream","byob-reader","respondwithnewview","rangeerror","arraybuffer"],"backgroundTag":"byob-request-respond-misuse","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T04:17:50.494Z"}