Skip to main content

Overview

The Buffer class provides a way to work with binary data. It extends Uint8Array and is compatible with Node.js Buffer API.

Constructor

Buffer.from()

Create a Buffer from various sources.
static from(value: string | Uint8Array | number[] | ArrayBuffer, encoding?: string): Buffer

Examples

const buf = Buffer.from('hello world');
console.log(buf); // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>

Buffer.alloc()

Create a zero-filled Buffer.
static alloc(size: number, fill?: number): Buffer
const buf = Buffer.alloc(10);
console.log(buf); // <Buffer 00 00 00 00 00 00 00 00 00 00>

const filled = Buffer.alloc(5, 0xFF);
console.log(filled); // <Buffer ff ff ff ff ff>

Buffer.allocUnsafe()

Create an uninitialized Buffer (faster but may contain old data).
static allocUnsafe(size: number): Buffer
const buf = Buffer.allocUnsafe(10);
// Contains random data, must be filled before use

Static Methods

Buffer.concat()

Concatenate multiple buffers.
static concat(list: (Uint8Array | Buffer)[], totalLength?: number): Buffer
const buf1 = Buffer.from('Hello ');
const buf2 = Buffer.from('World');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toString()); // 'Hello World'

Buffer.isBuffer()

Check if an object is a Buffer.
static isBuffer(obj: unknown): obj is Buffer
Buffer.isBuffer(Buffer.from('test')); // true
Buffer.isBuffer('test'); // false

Buffer.byteLength()

Get the byte length of a string.
static byteLength(str: string, encoding?: string): number
Buffer.byteLength('hello'); // 5
Buffer.byteLength('SGVsbG8=', 'base64'); // 5
Buffer.byteLength('48656c6c6f', 'hex'); // 5

Instance Methods

toString()

Convert buffer to string.
toString(encoding?: string, start?: number, end?: number): string
const buf = Buffer.from('hello');
buf.toString(); // 'hello'

write()

Write string to buffer.
write(str: string, offset?: number, length?: number, encoding?: string): number
const buf = Buffer.alloc(10);
const written = buf.write('hello', 0, 'utf8');
console.log(written); // 5
console.log(buf.toString()); // 'hello\x00\x00\x00\x00\x00'

toJSON()

Serialize buffer to JSON.
toJSON(): { type: 'Buffer'; data: number[] }
const buf = Buffer.from('test');
JSON.stringify(buf);
// '{"type":"Buffer","data":[116,101,115,116]}'

copy()

Copy buffer data to another buffer.
copy(target: Buffer | Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): number
const src = Buffer.from('hello');
const dest = Buffer.alloc(10);
src.copy(dest, 2);
console.log(dest.toString()); // '\x00\x00hello\x00\x00\x00'

equals()

Compare buffers for equality.
equals(other: Uint8Array): boolean
const buf1 = Buffer.from('hello');
const buf2 = Buffer.from('hello');
const buf3 = Buffer.from('world');

buf1.equals(buf2); // true
buf1.equals(buf3); // false

compare()

Compare buffers (returns -1, 0, or 1).
compare(other: Uint8Array): number
const buf1 = Buffer.from('abc');
const buf2 = Buffer.from('def');

buf1.compare(buf2); // -1 (buf1 < buf2)
buf2.compare(buf1); // 1 (buf2 > buf1)
buf1.compare(buf1); // 0 (equal)

slice() / subarray()

Extract a section of the buffer.
slice(start?: number, end?: number): Buffer
subarray(start?: number, end?: number): Buffer
const buf = Buffer.from('hello world');
const slice = buf.slice(0, 5);
console.log(slice.toString()); // 'hello'

Integer Methods

Reading Integers

buf.readUInt8(offset);
buf.readUInt16BE(offset);
buf.readUInt32BE(offset);
buf.readInt8(offset);
buf.readInt16BE(offset);
buf.readInt32BE(offset);

Example

const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);

buf.readUInt16BE(0); // 0x1234 (4660)
buf.readUInt16LE(0); // 0x3412 (13330)
buf.readUInt32BE(0); // 0x12345678

Writing Integers

buf.writeUInt8(value, offset);
buf.writeUInt16BE(value, offset);
buf.writeUInt32BE(value, offset);
buf.writeInt8(value, offset);
buf.writeInt16BE(value, offset);
buf.writeInt32BE(value, offset);

Example

const buf = Buffer.alloc(4);
buf.writeUInt32BE(0x12345678, 0);
console.log(buf); // <Buffer 12 34 56 78>

buf.writeUInt32LE(0x12345678, 0);
console.log(buf); // <Buffer 78 56 34 12>

Search Methods

indexOf()

Find first occurrence.
indexOf(value: number | Uint8Array | string, byteOffset?: number, encoding?: string): number
const buf = Buffer.from('hello world');
buf.indexOf('world'); // 6
buf.indexOf(0x6f); // 4 (first 'o')
buf.indexOf('x'); // -1 (not found)

lastIndexOf()

Find last occurrence.
lastIndexOf(value: number | Uint8Array | string, byteOffset?: number, encoding?: string): number
const buf = Buffer.from('hello world');
buf.lastIndexOf('o'); // 7

includes()

Check if buffer contains a value.
includes(value: number | Uint8Array | string, byteOffset?: number, encoding?: string): boolean
const buf = Buffer.from('hello world');
buf.includes('world'); // true
buf.includes('xyz'); // false

Complete Example

import { Buffer } from '@core/node-compat/buffer';

// Create buffer from string
const buf = Buffer.from('Hello, World!');

// Convert to different encodings
console.log(buf.toString('base64')); // 'SGVsbG8sIFdvcmxkIQ=='
console.log(buf.toString('hex')); // '48656c6c6f2c20576f726c6421'

// Search and manipulate
const worldIndex = buf.indexOf('World');
console.log(worldIndex); // 7

const worldBuf = buf.slice(worldIndex, worldIndex + 5);
console.log(worldBuf.toString()); // 'World'

// Write binary data
const binBuf = Buffer.alloc(4);
binBuf.writeUInt32BE(0xDEADBEEF, 0);
console.log(binBuf.toString('hex')); // 'deadbeef'

Build docs developers (and LLMs) love