Skip to content

5.5 is buffer or serialized

wiki[bot] edited this page Aug 23, 2026 · 1 revision

Is Buffer or Serialized

Universal buffer detection for Node.js and browser environments. Detects Buffer, ArrayBuffer, TypedArray, DataView, and serialized buffer formats.

import {
  isBufferOrSerialized,
  isActualArrayBuffer,
  isActualSharedArrayBuffer,
  isNodeBuffer,
  isSerializedBuffer,
  getObjectClass,
  type BufferLike,
  type SerializedBuffer
} from "@triplef/helpers/is-buffer-or-serialized";

Functions

isBufferOrSerialized(obj)

Main function that checks if a value is any type of buffer or serialized buffer.

function isBufferOrSerialized(obj: unknown): obj is BufferLike;

Supported Types:

Type Example Description
Node.js Buffer Buffer.from("test") Node.js binary buffer
ArrayBuffer new ArrayBuffer(8) Fixed-length raw binary buffer
SharedArrayBuffer new SharedArrayBuffer(8) Shared memory buffer
TypedArray new Uint8Array([1, 2, 3]) ArrayBuffer view
DataView new DataView(buffer) Multi-byte data access
Serialized { type: "Buffer", data: [...] } JSON-serialized buffer

Examples:

// Node.js Buffer (Node.js only)
isBufferOrSerialized(Buffer.from("test")); // true

// ArrayBuffer
isBufferOrSerialized(new ArrayBuffer(8)); // true

// TypedArray
isBufferOrSerialized(new Uint8Array([1, 2, 3])); // true
isBufferOrSerialized(new Int16Array([1, 2, 3])); // true

// DataView
const buffer = new ArrayBuffer(8);
isBufferOrSerialized(new DataView(buffer)); // true

// Serialized buffer (from JSON.stringify)
isBufferOrSerialized({ type: "Buffer", data: [116, 101, 115, 116] }); // true

// Non-buffers
isBufferOrSerialized("string"); // false
isBufferOrSerialized(42); // false
isBufferOrSerialized({ foo: "bar" }); // false

isActualArrayBuffer(obj)

Checks if an object is a real ArrayBuffer, with cross-realm support.

function isActualArrayBuffer(obj: object): obj is ArrayBuffer;

Uses instanceof for same-realm, and Object.prototype.toString + method verification for cross-realm (iframes, workers).

isActualSharedArrayBuffer(obj)

Checks if an object is a real SharedArrayBuffer.

function isActualSharedArrayBuffer(obj: object): obj is SharedArrayBuffer;

isNodeBuffer(obj)

Checks if an object is a Node.js Buffer.

function isNodeBuffer(obj: object): obj is Buffer;

Returns false in non-Node.js environments.

isSerializedBuffer(obj)

Checks if an object matches the serialized buffer format.

function isSerializedBuffer(obj: object): obj is SerializedBuffer;

Validates that the object has type: "Buffer" and data: number[].

getObjectClass(obj)

Gets the internal class of an object using Object.prototype.toString.

function getObjectClass(obj: object): string;
getObjectClass(new ArrayBuffer(8)); // "[object ArrayBuffer]"
getObjectClass(new Uint8Array(8)); // "[object Uint8Array]"

Types

BufferLike

Union type of all supported buffer types.

type BufferLike =
  | ArrayBuffer
  | SharedArrayBuffer
  | ReturnType<typeof ArrayBuffer.isView>
  | SerializedBuffer;

SerializedBuffer

Type for serialized buffer objects.

type SerializedBuffer = {
  type: "Buffer";
  data: number[];
};

Cross-Realm Safety

All buffer checks work correctly across:

  • Iframes (different execution contexts)
  • Web Workers
  • Node.js vm contexts

The function uses instanceof first for performance, then falls back to Object.prototype.toString + method verification for cross-realm scenarios.

Spoof Protection

Functions validate that objects are genuine buffer types, not objects with spoofed Symbol.toStringTag:

// This will return false - not a real ArrayBuffer
const fake = { [Symbol.toStringTag]: "ArrayBuffer" };
isBufferOrSerialized(fake); // false

Clone this wiki locally