denoland/deno · error · TypeError

Expected data to be a string, Buffer, Uint8Array, or ArrayBu

Error message

Expected data to be a string, Buffer, Uint8Array, or ArrayBuffer

What it means

An internal helper in the node:inspector polyfill base64-encodes payload data (via op_base64_encode_from_buffer) before it is sent over the inspector protocol. It accepts strings, non-DataView typed arrays (Buffer/Uint8Array), and ArrayBuffer; every other value (number, plain object, DataView, null) falls through to a plain TypeError with no err.code property.

Source

Thrown at ext/node/polyfills/inspector.js:95

      TypedArrayPrototypeGetByteLength(buf),
    );
  }
  if (TypedArrayPrototypeGetSymbolToStringTag(data) === "Uint8Array") {
    return op_base64_encode_from_buffer(
      data,
      0,
      TypedArrayPrototypeGetByteLength(data),
    );
  }
  if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
    const view = new Uint8Array(data);
    return op_base64_encode_from_buffer(
      view,
      0,
      TypedArrayPrototypeGetByteLength(view),
    );
  }
  throw new TypeError(
    "Expected data to be a string, Buffer, Uint8Array, or ArrayBuffer",
  );
}

class Session extends EventEmitter {
  #connection = null;
  #nextId = 1;
  #messageCallbacks = new SafeMap();
  #pendingMessages = [];
  #drainScheduled = false;
  #isDraining = false;

  connect() {
    if (this.#connection) {
      throw new ERR_INSPECTOR_ALREADY_CONNECTED("The inspector session");
    }
    this.#connection = op_inspector_connect(
      false,

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Serialize non-binary values yourself: JSON.stringify(value) or String(value) before passing them as data.
  2. Convert binary to Uint8Array/Buffer; for a DataView use new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength).
  3. Match on the TypeError message text when catching — there is no error code to test.

Example fix

// before - data is a plain object
emitWithPayload({ requestId: '1', data: { bytes: 4 } }); // internal encoder throws TypeError

// after
emitWithPayload({ requestId: '1', data: JSON.stringify({ bytes: 4 }) });
Defensive patterns

Strategy: type-guard

Validate before calling

function encodeInspectorData(data) {
  if (typeof data === 'string') return data;
  if (ArrayBuffer.isView(data) && !(data instanceof DataView)) return data;
  if (data instanceof ArrayBuffer) return new Uint8Array(data);
  return Buffer.from(JSON.stringify(data));
}

Type guard

function isInspectorEncodable(v) {
  return typeof v === 'string' || v instanceof ArrayBuffer ||
    (ArrayBuffer.isView(v) && !(v instanceof DataView));
}

Prevention

When it happens

Trigger: Handing data of an unsupported type to inspector paths that serialize message/network payloads — e.g. a data field that is a plain object, number, boolean, null, or a DataView instead of string/Buffer/Uint8Array/ArrayBuffer.

Common situations: Porting Node inspector tooling where a wider set of coercible inputs happened to work; passing decoded JSON objects where the raw body (string or bytes) is expected.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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