extract-base-iterator
Version:
Base iterator for extract iterators like tar-iterator and zip-iterator
96 lines (95 loc) • 4.35 kB
TypeScript
/**
* Buffer Compatibility Layer for Node.js 0.8+
*
* Provides buffer utilities that work across all Node.js versions
* WITHOUT modifying global Buffer object.
*
* Version history:
* - Node 0.8-4.4: Only has `new Buffer()`, no `Buffer.alloc/from`
* - Node 4.5+: Has `Buffer.alloc/from`, deprecates `new Buffer()`
* - Node 10+: Warns or errors on `new Buffer()`
*
* Solution: Feature detection with graceful fallback in both directions.
*/
export declare const MAX_SAFE_BUFFER_LENGTH: number;
/**
* Check if a buffer size can be safely allocated on this Node version
* Uses conservative limit to work across all versions
*/
export declare function canAllocateBufferSize(size: number): boolean;
/**
* Allocate a zero-filled buffer (safe) - handles very large allocations
* - Uses Buffer.alloc() on Node 4.5+
* - Falls back to new Buffer() + fill on Node 0.8-4.4
* - For sizes > MAX_SAFE_BUFFER_LENGTH, allocates in chunks and copies
*/
export declare function allocBuffer(size: number): Buffer;
/**
* Allocate a buffer without initialization (unsafe but faster)
* - Uses Buffer.allocUnsafe() on Node 4.5+
* - Falls back to new Buffer() on Node 0.8-4.4
* - For sizes > MAX_SAFE_BUFFER_LENGTH, allocates in chunks without zeroing
*
* WARNING: Buffer contents are uninitialized and may contain sensitive data.
* Only use when you will immediately overwrite all bytes.
*/
export declare function allocBufferUnsafe(size: number): Buffer;
/**
* Create a buffer from string, array, or existing buffer
* - Uses Buffer.from() on Node 4.5+
* - Falls back to new Buffer() on Node 0.8-4.4
* - Handles Uint8Array conversion for Node 0.8 (crypto output compatibility)
*/
export declare function bufferFrom(data: string | number[] | Buffer | Uint8Array, encoding?: BufferEncoding): Buffer;
/**
* Compare two buffers or buffer regions
* - Uses Buffer.compare() on Node 5.10+ (with offset support)
* - Falls back to manual comparison on Node 0.8-5.9
*/
export declare function bufferCompare(source: Buffer, target: Buffer, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number;
/**
* Check if buffer region equals byte array
* Useful for magic number detection without Buffer.from()
*/
export declare function bufferEquals(buf: Buffer, offset: number, expected: number[]): boolean;
/**
* Copy buffer region to new buffer
* Works on all Node versions
*/
export declare function bufferSliceCopy(buf: Buffer, start: number, end: number): Buffer;
/**
* Read 64-bit unsigned integer (little-endian)
* Uses two 32-bit reads since BigInt not available until Node 10.4
*
* WARNING: Only accurate for values < Number.MAX_SAFE_INTEGER (2^53 - 1)
* This covers files up to ~9 PB which is practical for all real use cases.
*/
export declare function readUInt64LE(buf: Buffer, offset: number): number;
/**
* Write 64-bit unsigned integer (little-endian)
* Same precision limitation as readUInt64LE
*/
export declare function writeUInt64LE(buf: Buffer, value: number, offset: number): void;
/**
* Concatenate buffers - compatible with Node 0.8+
* Handles crypto output which may not be proper Buffer instances in old Node.
* Also handles very large concatenations that would exceed buffer limits.
*
* NOTE: This function is primarily needed for AES decryption compatibility
* in Node 0.8 where crypto output may not be proper Buffer instances.
* Libraries not using crypto can use native Buffer.concat() directly.
*/
export declare function bufferConcat(list: (Buffer | Uint8Array)[], totalLength?: number): Buffer;
/**
* Node 0.8 compatible isNaN (Number.isNaN didn't exist until ES2015)
* Uses self-comparison: NaN is the only value not equal to itself
*/
export declare function isNaN(value: number): boolean;
export declare function stringStartsWith(str: string, search: string, position?: number): boolean;
export declare function inflateRaw(input: Buffer): Buffer;
export declare function createInflateRawStream(): NodeJS.ReadWriteStream;
export declare function objectAssign<T, U>(target: T, source: U): T & U;
export declare const Readable: typeof import('stream').Readable;
export declare const Writable: typeof import('stream').Writable;
export declare const Transform: typeof import('stream').Transform;
export declare const PassThrough: typeof import('stream').PassThrough;