denoland/deno · error · TypeError

The view's length must be greater than 0 when calling respon

Error message

The view's length must be greater than 0 when calling respondWithNewView() on a readable stream

What it means

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.

Source

Thrown at ext/web/06_streams.js:2810

  } else {
    buffer = DataViewPrototypeGetBuffer(/** @type {DataView} */ (view));
    byteLength = DataViewPrototypeGetByteLength(/** @type {DataView} */ (view));
    byteOffset = DataViewPrototypeGetByteOffset(/** @type {DataView} */ (view));
  }

  assert(!isDetachedBuffer(buffer));
  const firstDescriptor = controller[_pendingPullIntos].peek();
  const state = controller[_stream][_state];
  if (state === "closed") {
    if (byteLength !== 0) {
      throw new TypeError(
        `The view's length must be 0 when calling respondWithNewView() on a closed stream: received ${byteLength}`,
      );
    }
  } else {
    assert(state === "readable");
    if (byteLength === 0) {
      throw new TypeError(
        "The view's length must be greater than 0 when calling respondWithNewView() on a readable stream",
      );
    }
  }
  // deno-lint-ignore deno-internal/prefer-primordials
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== byteOffset) {
    throw new RangeError(
      "The region specified by view does not match byobRequest",
    );
  }
  if (firstDescriptor.bufferByteLength !== getArrayBufferByteLength(buffer)) {
    throw new RangeError(
      "The buffer of view has different capacity than byobRequest",
    );
  }
  // deno-lint-ignore deno-internal/prefer-primordials
  if (firstDescriptor.bytesFilled + byteLength > firstDescriptor.byteLength) {
    throw new RangeError(

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. On EOF call controller.close() — do not respond with an empty view.
  2. If no bytes are ready yet, return a pending promise from pull() without responding.
  3. Verify view.byteLength > 0 before calling respondWithNewView on a readable stream.

Example fix

// before
if (n === 0) byob.respondWithNewView(new Uint8Array(0)); // readable -> TypeError

// after
if (n === 0) { controller.close(); return; }
byob.respondWithNewView(view);
Defensive patterns

Strategy: validation

Validate before calling

if (view.byteLength === 0) { controller.close(); return; } // empty means EOF, not a response
byob.respondWithNewView(view);

Try / catch

try {
  byob.respondWithNewView(view);
} catch (e) {
  if (e instanceof TypeError && e.message.includes('greater than 0')) {
    controller.close(); // nothing to deliver
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/23420328f4b16dd4. Report an issue: GitHub.