denoland/deno · error · TypeError

ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

Error message

The property 'options.encoding' is invalid. Received ${inspected}

What it means

The options bag of the web-to-Node readable adapter (Readable.fromWeb) accepts an encoding used to decode byte chunks into strings; it must be a name Buffer.isEncoding() recognizes. Any other value throws ERR_INVALID_ARG_VALUE for options.encoding before reading starts.

Source

Thrown at ext/node/polyfills/internal/webstreams/adapters.js:81

) {
  if (!isReadableStream(readableStream)) {
    throw new ERR_INVALID_ARG_TYPE(
      "readableStream",
      "ReadableStream",
      readableStream,
    );
  }

  validateObject(options, "options");
  const {
    highWaterMark,
    encoding,
    objectMode = false,
    signal,
  } = options;

  if (encoding !== undefined && !Buffer.isEncoding(encoding)) {
    throw new ERR_INVALID_ARG_VALUE(encoding, "options.encoding");
  }
  validateBoolean(objectMode, "options.objectMode");

  const reader = readableStream.getReader();
  let closed = false;

  const readable = new (lazyStream().Readable)({
    objectMode,
    highWaterMark,
    encoding,
    signal,

    read() {
      reader.read().then(
        (chunk) => {
          if (chunk.done) {
            readable.push(null);
          } else {

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Use canonical names: 'utf8', 'utf16le', 'latin1', 'ascii', 'base64', 'hex'
  2. Map charsets to Node names (iso-8859-1 → latin1, unicode → utf8) before passing
  3. Leave encoding undefined when raw Buffer chunks are wanted

Example fix

// before
const r = Readable.fromWeb(rs, { encoding: 'utf-16' }); // throws

// after
const r = Readable.fromWeb(rs, { encoding: 'utf16le' });
Defensive patterns

Strategy: validation

Validate before calling

if (encoding !== undefined && !Buffer.isEncoding(encoding)) {
  throw new Error(`Invalid encoding: ${encoding}`);
}
const r = Readable.fromWeb(rs, { encoding });

Type guard

const isValidEncoding = (e) => e === undefined || (typeof e === 'string' && Buffer.isEncoding(e));

Prevention

When it happens

Trigger: Readable.fromWeb(rs, {encoding: 'utf-16'}) (canonical is 'utf16le'); {encoding: 'TEXT'}; an encoding taken from an HTTP charset header — many IANA names are not Node Buffer encodings.

Common situations: charset=iso-8859-1 vs the Node name 'latin1'; user-configurable output encoding; defaults copied from ICU-based libraries; 'utf-8' works but variant spellings do not.

Related errors


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