Buffer management utilities for automatic buffer growth during encoding operations.

This module provides utilities for managing buffer allocation and growth when encoding data of unknown or variable size. The primary function autoGrowBuffer automatically resizes buffers as needed during encoding operations, preventing buffer overflow errors while maintaining efficient memory usage.

Key Features

  • Automatic Growth: Buffers grow automatically when encoding operations require more space than initially allocated
  • Intelligent Resizing: Uses configurable growth factors and respects maximum size limits to prevent excessive memory usage
  • Error Handling: Provides clear error messages when growth limits are reached or invalid configurations are provided
  • Type Safety: Full TypeScript support with generic return types
  • Performance: Efficient exponential growth strategy minimizes resize operations

Usage Patterns

The autoGrowBuffer function is designed to work with any encoding function that may require variable buffer sizes. Common use cases include:

  • Encoding data structures with variable-length fields
  • Processing streams of unknown size
  • Building binary formats with dynamic content
  • Handling user-provided data of unpredictable size

Configuration

Buffer growth behavior can be customized through the AutogrowOptions interface:

  • initialSize: Starting buffer size (default: 4KB)
  • maxByteLength: Maximum allowed buffer size (default: 400MB)
  • growthFactor: Multiplier for each resize operation (default: 2x)

Error Handling

The module provides clear error messages for common failure scenarios:

  • Invalid initial size or growth factor configuration
  • Buffer growth reaching maximum size limits
  • Non-RangeError exceptions are propagated unchanged

Robustness Features

The implementation includes safeguards against infinite loops and edge cases:

  • Growth Factor Validation: Rejects growth factors ≤ 1 to prevent infinite loops where buffers never actually grow
  • Minimum Growth Guarantee: Ensures buffers always grow by at least 1 byte, preventing infinite loops when truncation results in no size increase
  • Integer Truncation Safety: Uses Math.trunc() to ensure valid buffer sizes while guaranteeing forward progress

Functions

f
autoGrowBuffer<T>(
tryEncodeFn: (buffer: Uint8Array) => T,
autogrowOptions?: AutogrowOptions
): T

Automatically grows a buffer until the encoding function succeeds.

Interfaces

I
AutogrowOptions

Configuration options for automatic buffer growth.

  • growthFactor: number

    Growth factor multiplier for buffer resizing. Must be greater than 1. Each resize multiplies the current size by this factor. Defaults to 2 (doubling).

  • initialSize: number

    Initial buffer size in bytes. Must be greater than 0 and less than or equal to maxByteLength. Defaults to 4096 bytes (4KB).

  • maxByteLength: number

    Maximum buffer size in bytes. The buffer will not grow beyond this limit. When reached, a RangeError will be thrown. Defaults to 400MB.