denoland/deno · error · TypeError

"autoAllocateChunkSize" must be greater than 0

Error message

"autoAllocateChunkSize" must be greater than 0

What it means

The ReadableStream constructor rejected underlyingSource.autoAllocateChunkSize === 0 for a type: 'bytes' stream. autoAllocateChunkSize sets the size of the views the controller auto-allocates for byobRequest when the source is pulled without BYOB reads; zero would allocate nothing, so it must be a positive integer or absent. The option converts as unsigned long long, so negatives fail earlier during argument conversion — only exactly 0 reaches this check.

Source

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

  let startAlgorithm = _defaultStartAlgorithm;
  /** @type {() => Promise<void>} */
  let pullAlgorithm = _defaultPullAlgorithm;
  /** @type {(reason: any) => Promise<void>} */
  let cancelAlgorithm = _defaultCancelAlgorithm;
  controller[_underlyingSource] = underlyingSource;
  controller[_underlyingSourceDict] = underlyingSourceDict;
  if (underlyingSourceDict.start !== undefined) {
    startAlgorithm = underlyingSourceStartByte;
  }
  if (underlyingSourceDict.pull !== undefined) {
    pullAlgorithm = underlyingSourcePullByte;
  }
  if (underlyingSourceDict.cancel !== undefined) {
    cancelAlgorithm = underlyingSourceCancelByte;
  }
  const autoAllocateChunkSize = underlyingSourceDict["autoAllocateChunkSize"];
  if (autoAllocateChunkSize === 0) {
    throw new TypeError('"autoAllocateChunkSize" must be greater than 0');
  }
  setUpReadableByteStreamController(
    stream,
    controller,
    startAlgorithm,
    pullAlgorithm,
    cancelAlgorithm,
    highWaterMark,
    autoAllocateChunkSize,
  );
}

/**
 * @template R
 * @param {ReadableStream<R>} stream
 * @param {ReadableStreamDefaultController<R>} controller
 * @param {(controller: ReadableStreamDefaultController<R>) => void | Promise<void>} startAlgorithm
 * @param {(controller: ReadableStreamDefaultController<R>) => Promise<void> | undefined} pullAlgorithm

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Omit autoAllocateChunkSize entirely (it is optional) or set a positive size such as 64 * 1024.
  2. Validate config before constructing: use the key only when the value is a positive integer.
  3. If you meant 'no auto-allocation', leave the key out and rely on BYOB readers only.

Example fix

// before
new ReadableStream({ type: 'bytes', autoAllocateChunkSize: 0 }); // TypeError

// after
new ReadableStream({ type: 'bytes', autoAllocateChunkSize: 64 * 1024 });
// or omit the key entirely
Defensive patterns

Strategy: validation

Validate before calling

const chunkSize = Number(cfg.autoAllocateChunkSize);
const source = {
  type: 'bytes',
  ...(Number.isInteger(chunkSize) && chunkSize > 0
    ? { autoAllocateChunkSize: chunkSize }
    : {}),
};
const stream = new ReadableStream(source);

Try / catch

try {
  stream = new ReadableStream(source);
} catch (e) {
  if (e instanceof TypeError && e.message.includes('autoAllocateChunkSize')) {
    delete source.autoAllocateChunkSize;
    stream = new ReadableStream(source); // retry without the option
  } else throw e;
}

Prevention

When it happens

Trigger: new ReadableStream({ type: 'bytes', autoAllocateChunkSize: 0 }); passing a computed chunk size (config value, env var, CLI flag) that defaults or coerces to 0; passing 0 intending 'automatic/default'.

Common situations: Config-driven stream tuning where a missing key coerces to 0; object literals copied with placeholder zeros; refactors that move the size into a variable initialized to 0.

Related errors


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