Buffer creation functionality providing multiple ways to initialize Buffer instances with different data sources and allocation strategies.
Creates a new Buffer instance using various input types.
/**
* Creates a Buffer from a size (allocates uninitialized memory)
* @param size - Number of bytes to allocate
*/
function Buffer(size: number): Buffer;
/**
* Creates a Buffer from a string with optional encoding
* @param string - String to convert to buffer
* @param encoding - Character encoding (default: 'utf8')
*/
function Buffer(string: string, encoding?: string): Buffer;
/**
* Creates a Buffer from an array of bytes
* @param array - Array of numbers (0-255)
*/
function Buffer(array: number[]): Buffer;
/**
* Creates a copy of an existing Buffer
* @param buffer - Buffer to copy
*/
function Buffer(buffer: Buffer): Buffer;
/**
* Creates a Buffer from an ArrayBuffer
* @param arrayBuffer - ArrayBuffer to wrap
*/
function Buffer(arrayBuffer: ArrayBuffer): Buffer;Usage Examples:
// From size (uninitialized - may contain sensitive data)
const buf1 = Buffer(10); // 10 bytes of uninitialized memory
// From string
const buf2 = Buffer('hello', 'utf8');
const buf3 = Buffer('48656c6c6f', 'hex');
// From array
const buf4 = Buffer([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
// Copy existing buffer
const buf5 = Buffer(buf2);Creates a zero-filled Buffer of specified size.
/**
* Allocates a new zero-filled Buffer of specified size
* @param size - Number of bytes to allocate
* @param fill - Value to fill buffer with (default: 0)
* @param encoding - Encoding for string fill value
* @returns New zero-filled Buffer
*/
Buffer.alloc(size: number, fill?: string | Buffer | number, encoding?: string): Buffer;Usage Examples:
// Zero-filled buffer
const buf1 = Buffer.alloc(10); // <Buffer 00 00 00 00 00 00 00 00 00 00>
// Pre-filled buffer
const buf2 = Buffer.alloc(10, 1); // <Buffer 01 01 01 01 01 01 01 01 01 01>
const buf3 = Buffer.alloc(10, 'a'); // <Buffer 61 61 61 61 61 61 61 61 61 61>
const buf4 = Buffer.alloc(10, 'hello', 'utf8'); // Repeats 'hello'Creates an uninitialized Buffer of specified size (faster but potentially unsafe).
/**
* Allocates uninitialized Buffer (may contain sensitive data)
* @param size - Number of bytes to allocate
* @returns New uninitialized Buffer
*/
Buffer.allocUnsafe(size: number): Buffer;Usage Examples:
// Fast allocation (uninitialized memory)
const buf = Buffer.allocUnsafe(10);
// Must initialize before use to avoid sensitive data leaks
buf.fill(0);Creates an uninitialized Buffer outside the pre-allocated pool.
/**
* Allocates uninitialized Buffer outside pool (slower allocation)
* @param size - Number of bytes to allocate
* @returns New uninitialized Buffer
*/
Buffer.allocUnsafeSlow(size: number): Buffer;Universal buffer creation method supporting multiple input types.
/**
* Creates Buffer from string
* @param string - String to convert
* @param encoding - Character encoding (default: 'utf8')
*/
Buffer.from(string: string, encoding?: string): Buffer;
/**
* Creates Buffer from array of bytes
* @param array - Array of numbers (0-255)
*/
Buffer.from(array: number[]): Buffer;
/**
* Creates Buffer from another Buffer (copy)
* @param buffer - Buffer to copy
*/
Buffer.from(buffer: Buffer): Buffer;
/**
* Creates Buffer from ArrayBuffer
* @param arrayBuffer - ArrayBuffer to wrap
* @param byteOffset - Starting offset in ArrayBuffer
* @param length - Number of bytes to include
*/
Buffer.from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer;
/**
* Creates Buffer from Uint8Array
* @param uint8Array - Uint8Array to copy
*/
Buffer.from(uint8Array: Uint8Array): Buffer;Usage Examples:
// From string
const buf1 = Buffer.from('hello world', 'utf8');
const buf2 = Buffer.from('hello world'); // utf8 is default
// From hex string
const buf3 = Buffer.from('48656c6c6f', 'hex');
// From array
const buf4 = Buffer.from([72, 101, 108, 108, 111]);
// From ArrayBuffer
const ab = new ArrayBuffer(10);
const buf5 = Buffer.from(ab);
const buf6 = Buffer.from(ab, 2, 4); // offset 2, length 4
// Copy existing buffer
const buf7 = Buffer.from(buf1);Legacy constructor for backward compatibility.
/**
* Legacy constructor, equivalent to Buffer.alloc()
* @param length - Buffer length
* @returns New Buffer instance
*/
function SlowBuffer(length: number): Buffer;Buffer creation methods may throw the following errors:
kMaxLength)try {
const buf = Buffer.alloc(-1); // Throws RangeError
} catch (error) {
console.error('Invalid buffer size:', error.message);
}// Maximum buffer length (2^31 - 1)
const kMaxLength: number = 0x7fffffff;
// Pool size for buffer allocation (not used in browser)
Buffer.poolSize: number = 8192;
// Typed array support indicator
Buffer.TYPED_ARRAY_SUPPORT: boolean;