{"record":{"id":"23420328f4b16dd4","repo":"denoland/deno","slug":"the-view-s-length-must-be-greater-than-0-when-call","errorCode":null,"errorMessage":"The view's length must be greater than 0 when calling respondWithNewView() on a readable stream","messagePattern":"The view's length must be greater than 0 when calling respondWithNewView\\(\\) on a readable stream","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":2810,"sourceCode":"  } else {\n    buffer = DataViewPrototypeGetBuffer(/** @type {DataView} */ (view));\n    byteLength = DataViewPrototypeGetByteLength(/** @type {DataView} */ (view));\n    byteOffset = DataViewPrototypeGetByteOffset(/** @type {DataView} */ (view));\n  }\n\n  assert(!isDetachedBuffer(buffer));\n  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(","sourceCodeStart":2792,"sourceCodeEnd":2828,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L2792-L2828","documentation":"respondWithNewView(view) was called with a zero-length view while the stream is still 'readable'. A pending BYOB read needs at least one byte, so an empty region cannot fulfill it. Zero-length views are only meaningful as the acknowledgment on a closed stream.","triggerScenarios":"Passing new Uint8Array(0) (or an empty subarray) to respondWithNewView on an open stream — usually an EOF branch that should close instead, or view arithmetic that underflows to length 0.","commonSituations":"EOF handling written as 'respond with empty' instead of close(); subarray(end, start) mistakes producing empty views; reusing one template view for both data and acknowledgment paths.","solutions":["On EOF call controller.close() — do not respond with an empty view.","If no bytes are ready yet, return a pending promise from pull() without responding.","Verify view.byteLength > 0 before calling respondWithNewView on a readable stream."],"exampleFix":"// before\nif (n === 0) byob.respondWithNewView(new Uint8Array(0)); // readable -> TypeError\n\n// after\nif (n === 0) { controller.close(); return; }\nbyob.respondWithNewView(view);","handlingStrategy":"validation","validationCode":"if (view.byteLength === 0) { controller.close(); return; } // empty means EOF, not a response\nbyob.respondWithNewView(view);","typeGuard":null,"tryCatchPattern":"try {\n  byob.respondWithNewView(view);\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('greater than 0')) {\n    controller.close(); // nothing to deliver\n    return;\n  }\n  throw e;\n}","preventionTips":["While the stream is readable, an empty response region is always an error.","Keep acknowledgment paths (respond(0) on closed) separate from data paths."],"tags":["readable-stream","byob-reader","respondwithnewview","eof"],"backgroundTag":"byob-request-respond-misuse","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T04:17:50.494Z"}