/** * node-liblzma - Node.js bindings for liblzma * Copyright (C) Olivier Orabona * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ import { Transform, type TransformCallback, type TransformOptions } from 'node:stream'; import { LZMABufferError, LZMADataError, LZMAError, LZMAFormatError, LZMAMemoryError, LZMAMemoryLimitError, LZMAOptionsError, LZMAProgrammingError } from './errors.js'; import type { CheckType, CompressionCallback, FilterType, LZMAActionType, LZMAOptions, LZMAStatusType, ModeType, NativeLZMA, PresetType, ProgressInfo, XZFileIndex } from './types.js'; export { LZMAError, LZMAMemoryError, LZMAMemoryLimitError, LZMAFormatError, LZMAOptionsError, LZMADataError, LZMABufferError, LZMAProgrammingError, }; export { LZMAPool, type PoolMetrics } from './pool.js'; /** * Integrity check types for XZ streams. * Use CRC64 for best balance of speed and error detection. * @example * ```ts * const compressor = createXz({ check: check.CRC64 }); * ``` */ export declare const check: { readonly NONE: any; readonly CRC32: any; readonly CRC64: any; readonly SHA256: any; }; /** * Compression preset flags. * Can be combined with preset level using bitwise OR. * @example * ```ts * const compressor = createXz({ preset: 6 | preset.EXTREME }); * ``` */ export declare const preset: { /** Default compression level (6) */ readonly DEFAULT: any; /** Extreme mode flag - slower but better compression */ readonly EXTREME: any; }; /** * Decoder flags for controlling decompression behavior. * @example * ```ts * const decompressor = createUnxz({ flushFlag: flag.CONCATENATED }); * ``` */ export declare const flag: { /** Tell decoder if input has no integrity check */ readonly TELL_NO_CHECK: any; /** Tell decoder if integrity check is unsupported */ readonly TELL_UNSUPPORTED_CHECK: any; /** Tell decoder about any integrity check type */ readonly TELL_ANY_CHECK: any; /** Allow concatenated XZ streams */ readonly CONCATENATED: any; }; /** * Compression filters for preprocessing data before LZMA2. * BCJ filters improve compression for executable code. * @example * ```ts * // Compress x86 executable with BCJ filter * const compressor = createXz({ filters: [filter.X86, filter.LZMA2] }); * ``` */ export declare const filter: { /** LZMA2 compression filter (required, must be last) */ readonly LZMA2: any; /** BCJ filter for x86 executables */ readonly X86: any; /** BCJ filter for PowerPC executables */ readonly POWERPC: any; /** BCJ filter for IA-64 executables */ readonly IA64: any; /** BCJ filter for ARM executables */ readonly ARM: any; /** BCJ filter for ARM-Thumb executables */ readonly ARMTHUMB: any; /** BCJ filter for SPARC executables */ readonly SPARC: any; }; /** * Compression mode selection. * FAST uses less memory, NORMAL provides better compression. */ export declare const mode: { /** Fast compression mode - less memory, faster */ readonly FAST: any; /** Normal compression mode - better ratio */ readonly NORMAL: any; }; /** * LZMA stream action constants. * Control how the encoder/decoder processes input. */ export declare const LZMAAction: { /** Normal processing - continue encoding/decoding */ readonly RUN: any; /** Flush pending output synchronously */ readonly SYNC_FLUSH: any; /** Flush and reset encoder state */ readonly FULL_FLUSH: any; /** Finish the stream - no more input */ readonly FINISH: any; }; /** * LZMA operation status/return codes. * Used to indicate the result of encoding/decoding operations. */ export declare const LZMAStatus: { /** Operation completed successfully */ readonly OK: any; /** End of stream was reached */ readonly STREAM_END: any; /** Input stream has no integrity check */ readonly NO_CHECK: any; /** Cannot verify the integrity check type */ readonly UNSUPPORTED_CHECK: any; /** Integrity check type is now available */ readonly GET_CHECK: any; /** Cannot allocate memory */ readonly MEM_ERROR: any; /** Memory usage limit was reached */ readonly MEMLIMIT_ERROR: any; /** File format not recognized */ readonly FORMAT_ERROR: any; /** Invalid or unsupported options */ readonly OPTIONS_ERROR: any; /** Data is corrupt */ readonly DATA_ERROR: any; /** No progress is possible (input/output buffers full) */ readonly BUF_ERROR: any; /** Programming error in caller */ readonly PROG_ERROR: any; }; /** Extended filter constants including aliases and FILTERS_MAX limit. Superset of {@link filter}. */ export declare const LZMAFilter: { readonly X86_ALT: any; readonly POWERPC_ALT: any; readonly IA64_ALT: any; readonly ARM_ALT: any; readonly ARMTHUMB_ALT: any; readonly FILTERS_MAX: any; /** LZMA2 compression filter (required, must be last) */ readonly LZMA2: any; /** BCJ filter for x86 executables */ readonly X86: any; /** BCJ filter for PowerPC executables */ readonly POWERPC: any; /** BCJ filter for IA-64 executables */ readonly IA64: any; /** BCJ filter for ARM executables */ readonly ARM: any; /** BCJ filter for ARM-Thumb executables */ readonly ARMTHUMB: any; /** BCJ filter for SPARC executables */ readonly SPARC: any; }; /** Action: continue encoding/decoding */ export declare const LZMA_RUN: any; /** Action: flush pending output synchronously */ export declare const LZMA_SYNC_FLUSH: any; /** Action: flush and reset encoder state */ export declare const LZMA_FULL_FLUSH: any; /** Action: finish the stream — no more input */ export declare const LZMA_FINISH: any; /** liblzma return code: operation completed successfully */ export declare const LZMA_OK: any; /** liblzma return code: end of stream was reached */ export declare const LZMA_STREAM_END: any; /** liblzma return code: input stream has no integrity check */ export declare const LZMA_NO_CHECK: any; /** liblzma return code: cannot verify the integrity check type */ export declare const LZMA_UNSUPPORTED_CHECK: any; /** liblzma return code: integrity check type is now available */ export declare const LZMA_GET_CHECK: any; /** liblzma return code: cannot allocate memory */ export declare const LZMA_MEM_ERROR: any; /** liblzma return code: memory usage limit was reached */ export declare const LZMA_MEMLIMIT_ERROR: any; /** liblzma return code: file format not recognized */ export declare const LZMA_FORMAT_ERROR: any; /** liblzma return code: invalid or unsupported options */ export declare const LZMA_OPTIONS_ERROR: any; /** liblzma return code: data is corrupt */ export declare const LZMA_DATA_ERROR: any; /** liblzma return code: no progress is possible */ export declare const LZMA_BUF_ERROR: any; /** liblzma return code: programming error in caller */ export declare const LZMA_PROG_ERROR: any; /** BCJ filter for x86 architecture */ export declare const LZMA_FILTER_X86: any; /** BCJ filter for PowerPC architecture */ export declare const LZMA_FILTER_POWERPC: any; /** BCJ filter for IA-64 architecture */ export declare const LZMA_FILTER_IA64: any; /** BCJ filter for ARM architecture */ export declare const LZMA_FILTER_ARM: any; /** BCJ filter for ARM-Thumb architecture */ export declare const LZMA_FILTER_ARMTHUMB: any; /** Maximum number of filters in a chain */ export declare const LZMA_FILTERS_MAX: any; export type { LZMAOptions, CompressionCallback, LZMAActionType, LZMAStatusType, CheckType, PresetType, FilterType, ModeType, ProgressInfo, }; /** * Abstract base class for XZ compression/decompression streams. * Extends Node.js Transform stream with LZMA2 encoding/decoding. * * @example * ```ts * // Use Xz or Unxz classes instead of XzStream directly * const compressor = new Xz({ preset: 6 }); * readStream.pipe(compressor).pipe(writeStream); * ``` * * Emits `progress` event after each chunk with `{bytesRead, bytesWritten}` info. */ /** * Internal resolved options for XzStream instances. * * All fields are required (defaults applied in constructor) EXCEPT memlimit, * which is genuinely optional: validated by validateMemlimit() at the XzStream * constructor for decode streams; passed through to both the native N-API decoder * and the WASM Buffer API. xzAsync is compression-only and ignores this field. */ interface ResolvedLZMAOptions { check: number; preset: number; filters: number[]; mode: number; threads: number; chunkSize: number; flushFlag: number; /** Optional memlimit. Validated at the public XzStream constructor; passed through to the decoder backend (native or WASM). */ memlimit?: number | bigint; } export declare abstract class XzStream extends Transform { protected _opts: ResolvedLZMAOptions; protected _chunkSize: number; protected _flushFlag: number; protected lzma: NativeLZMA; protected _closed: boolean; protected _hadError: boolean; protected _offset: number; protected _buffer: Buffer; /** Total bytes read from input */ protected _bytesRead: number; /** Total bytes written to output */ protected _bytesWritten: number; constructor(streamMode: number, opts?: LZMAOptions, options?: TransformOptions); private _createLZMAError; /** Get total bytes read from input so far */ get bytesRead(): number; /** Get total bytes written to output so far */ get bytesWritten(): number; /** * Emit a progress event with current bytesRead and bytesWritten */ protected _emitProgress(): void; private _reallocateBuffer; flush(callback?: () => void): void; flush(kind: number, callback?: () => void): void; close(callback?: () => void): void; _transform(chunk: Buffer | null, _encoding: string, callback: TransformCallback): void; _flush(callback: TransformCallback): void; _processChunk(chunk: Buffer | null, flushFlag: number, cb?: TransformCallback): Buffer | undefined; } /** * XZ compression stream. * Compresses data using LZMA2 algorithm. * * @example * ```ts * const compressor = new Xz({ preset: 6 }); * input.pipe(compressor).pipe(output); * ``` */ export declare class Xz extends XzStream { constructor(lzmaOptions?: LZMAOptions, options?: TransformOptions); } /** * XZ decompression stream. * Decompresses data compressed with XZ/LZMA2. * * @example * ```ts * const decompressor = new Unxz(); * compressedInput.pipe(decompressor).pipe(output); * ``` */ export declare class Unxz extends XzStream { constructor(lzmaOptions?: LZMAOptions, options?: TransformOptions); } /** * Create a new XZ compression stream. * @param lzmaOptions - LZMA compression options * @param options - Node.js Transform stream options * @returns New Xz compression stream * * @example * ```ts * const compressor = createXz({ preset: 9 }); * ``` */ export declare function createXz(lzmaOptions?: LZMAOptions, options?: TransformOptions): Xz; /** * Create a new XZ decompression stream. * @param lzmaOptions - LZMA decompression options * @param options - Node.js Transform stream options * @returns New Unxz decompression stream * * @example * ```ts * const decompressor = createUnxz(); * ``` */ export declare function createUnxz(lzmaOptions?: LZMAOptions, options?: TransformOptions): Unxz; /** * Check if liblzma was built with threading support. * @returns true if multi-threaded compression is available */ export declare function hasThreads(): boolean; /** * Check if a buffer contains XZ compressed data by examining magic bytes. * @param buffer - Buffer to check (needs at least 6 bytes) * @returns true if the buffer starts with XZ magic bytes * @example * ```ts * const compressed = await xzAsync(Buffer.from('Hello')); * console.log(isXZ(compressed)); // true * console.log(isXZ(Buffer.from('Hello'))); // false * ``` */ export declare function isXZ(buffer: Buffer): boolean; /** * Get the runtime version of liblzma as a string. * @returns Version string like "5.4.1" * @example * ```ts * console.log(versionString()); // "5.4.1" * ``` */ export declare function versionString(): string; /** * Get the runtime version of liblzma as a number. * Format: MAJOR * 10000000 + MINOR * 10000 + PATCH * 10 * @returns Version number (e.g., 50040010 for 5.4.1) * @example * ```ts * console.log(versionNumber()); // 50040010 * ``` */ export declare function versionNumber(): number; /** * Get the memory usage estimate for encoding with a given preset. * @param presetLevel - Compression preset (0-9, optionally OR'd with preset.EXTREME) * @returns Memory usage in bytes, or 0 if preset is invalid * @example * ```ts * console.log(easyEncoderMemusage(6)); // ~104857600 (100 MB) * console.log(easyEncoderMemusage(9 | preset.EXTREME)); // ~712507392 (680 MB) * ``` */ export declare function easyEncoderMemusage(presetLevel: number): number; /** * Get the memory usage estimate for decoding. * @returns Memory usage in bytes for decoder initialization * @example * ```ts * console.log(easyDecoderMemusage()); // ~18874368 (18 MB) * ``` */ export declare function easyDecoderMemusage(): number; export type { XZFileIndex } from './types.js'; /** * Parse the index from a complete XZ file buffer to get metadata. * This allows you to know the uncompressed size before decompressing. * @param buffer - Complete XZ file buffer * @returns Object with file metadata * @throws Error if buffer is not a valid XZ stream * @example * ```ts * const compressed = await xzAsync(largeBuffer); * const info = parseFileIndex(compressed); * console.log(`Uncompressed: ${info.uncompressedSize}, Compressed: ${info.compressedSize}`); * console.log(`Ratio: ${(info.compressedSize / info.uncompressedSize * 100).toFixed(1)}%`); * ``` */ export declare function parseFileIndex(buffer: Buffer): XZFileIndex; /** * Human-readable error messages for LZMA status codes. */ export declare enum LZMAErrorMessage { SUCCESS = "Operation completed successfully", STREAM_END = "End of stream was reached", NO_CHECK = "Input stream has no integrity check", UNSUPPORTED_CHECK = "Cannot calculate the integrity check", GET_CHECK = "Integrity check type is not available", MEM_ERROR = "Cannot allocate memory", MEMLIMIT_ERROR = "Memory usage limit was reached", FORMAT_ERROR = "File format not recognized", OPTIONS_ERROR = "Invalid or unsupported options", DATA_ERROR = "Data is corrupt", BUF_ERROR = "No progress is possible", PROG_ERROR = "Programming error" } /** * Decompress a buffer asynchronously using callback. * @param buffer - Compressed data to decompress * @param callback - Callback with error or decompressed data * * @example * ```ts * unxz(compressedBuffer, (err, result) => { * if (err) throw err; * console.log(result.toString()); * }); * ``` */ export declare function unxz(buffer: Buffer | string, callback: CompressionCallback): void; export declare function unxz(buffer: Buffer | string, opts: LZMAOptions, callback: CompressionCallback): void; /** * Decompress a buffer synchronously. * @param buffer - Compressed data to decompress * @param opts - LZMA decompression options * @returns Decompressed data buffer * * @example * ```ts * const decompressed = unxzSync(compressedBuffer); * ``` */ export declare function unxzSync(buffer: Buffer | string, opts?: LZMAOptions): Buffer; /** * Compress a buffer asynchronously using callback. * @param buffer - Data to compress * @param callback - Callback with error or compressed data * * @example * ```ts * xz(data, { preset: 6 }, (err, result) => { * if (err) throw err; * fs.writeFileSync('data.xz', result); * }); * ``` */ export declare function xz(buffer: Buffer | string, callback: CompressionCallback): void; export declare function xz(buffer: Buffer | string, opts: LZMAOptions, callback: CompressionCallback): void; /** * Compress a buffer synchronously. * @param buffer - Data to compress * @param opts - LZMA compression options * @returns Compressed data buffer * * @example * ```ts * const compressed = xzSync(data, { preset: 9 }); * ``` */ export declare function xzSync(buffer: Buffer | string, opts?: LZMAOptions): Buffer; /** * Compress a buffer asynchronously using Promise. * @param buffer - Data to compress * @param opts - LZMA compression options * @returns Promise resolving to compressed data buffer * * @example * ```ts * const compressed = await xzAsync(data, { preset: 6 }); * ``` */ export declare function xzAsync(buffer: Buffer | string, opts?: LZMAOptions): Promise; /** * Decompress a buffer asynchronously using Promise. * @param buffer - Compressed data to decompress * @param opts - LZMA decompression options * @returns Promise resolving to decompressed data buffer * * @example * ```ts * const decompressed = await unxzAsync(compressedBuffer); * console.log(decompressed.toString()); * ``` */ export declare function unxzAsync(buffer: Buffer | string, opts?: LZMAOptions): Promise; /** * Compress a file using XZ compression * @param inputPath Path to input file * @param outputPath Path to output compressed file * @param opts LZMA compression options * @returns Promise that resolves when compression is complete */ export declare function xzFile(inputPath: string, outputPath: string, opts?: LZMAOptions): Promise; /** * Decompress an XZ compressed file * @param inputPath Path to compressed input file * @param outputPath Path to output decompressed file * @param opts LZMA decompression options * @returns Promise that resolves when decompression is complete */ export declare function unxzFile(inputPath: string, outputPath: string, opts?: LZMAOptions): Promise; //# sourceMappingURL=lzma.d.ts.map