or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

buffer-creation.mdbuffer-manipulation.mddata-read-write.mdindex.mdstring-encoding.md
tile.json

buffer-creation.mddocs/

Buffer Creation

Buffer creation functionality providing multiple ways to initialize Buffer instances with different data sources and allocation strategies.

Capabilities

Buffer Constructor

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);

Buffer.alloc

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'

Buffer.allocUnsafe

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);

Buffer.allocUnsafeSlow

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;

Buffer.from

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);

SlowBuffer (Legacy)

Legacy constructor for backward compatibility.

/**
 * Legacy constructor, equivalent to Buffer.alloc()
 * @param length - Buffer length
 * @returns New Buffer instance
 */
function SlowBuffer(length: number): Buffer;

Error Handling

Buffer creation methods may throw the following errors:

  • RangeError: When size exceeds maximum buffer length (kMaxLength)
  • TypeError: When arguments are of incorrect type
  • RangeError: When ArrayBuffer offset/length parameters are invalid
try {
  const buf = Buffer.alloc(-1); // Throws RangeError
} catch (error) {
  console.error('Invalid buffer size:', error.message);
}

Constants

// 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;