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

The inspector network bridge (inspector_network_bridge.js) base64-encodes params.data before emitting network-domain protocol events (the emitWithData path that forwards data to the DevTools front-end). The data must be a string, a non-DataView typed array (Buffer/Uint8Array), or an ArrayBuffer; anything else (plain object, number, boolean, null, DataView) reaches a plain TypeError with no err.code.

Source

Thrown at ext/node/polyfills/inspector_network_bridge.js:54

      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",
  );
}

function emit(eventName, params) {
  op_inspector_emit_protocol_event(eventName, JSONStringify(params ?? {}));
}

function emitWithData(eventName, params) {
  if (params && params.data !== undefined) {
    const encoded = encodeNetworkData(params.data);
    if (encoded !== params.data) {
      params = ObjectAssign({ __proto__: null }, params, { data: encoded });
    }
  }
  emit(eventName, params);
}

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Pass bodies as string or Uint8Array; stringify anything else with JSON.stringify().
  2. Convert DataView via new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength).
  3. When catching, match on the TypeError message text — there is no code property.

Example fix

// before
emitWithData('Network.dataReceived', { requestId, data: { length: 1024 } });
// TypeError: Expected data to be a string, Buffer, Uint8Array, or ArrayBuffer

// after
emitWithData('Network.dataReceived', { requestId, data: JSON.stringify({ length: 1024 }) });
Defensive patterns

Strategy: type-guard

Validate before calling

function encodeNetworkData(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 JSON.stringify(data);
}
emitWithData('Network.dataReceived', { requestId, data: encodeNetworkData(body) });

Type guard

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

Prevention

When it happens

Trigger: Broadcasting a network event whose params.data is a plain object or number — e.g. emitWithData('Network.dataReceived', { requestId, data: parsedJson }) — or a DataView holding the body bytes.

Common situations: Translating CDP payloads between JSON and binary representations; passing decoded objects where the raw body (string or bytes) is expected by the front-end.

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/e35497c7a6606fb1. Report an issue: GitHub.