/** * Base compression utilities using Web Streams API * Shared between Node.js and Browser implementations * * Uses CompressionStream/DecompressionStream API with "deflate-raw" format * (raw DEFLATE without zlib header/trailer, required for ZIP files) * * Browser fallback: For browsers without deflate-raw support (Firefox < 113, Safari < 16.4), * see deflate-fallback.ts for pure JS implementation */ /** * Compression options */ export interface CompressOptions { /** * Compression level (0-9) * - 0: No compression (STORE) * - 1: Fastest compression * - 6: Default compression (good balance) * - 9: Best compression (slowest) * * Note: CompressionStream does not support level configuration, * it uses a fixed level (~6) */ level?: number; /** * Threshold (in bytes) to choose sync vs async path (Node.js only). * - Node.js: inputs <= threshold use sync zlib (avoid threadpool overhead) * * This option is ignored in browsers. * Default: 8MB. */ thresholdBytes?: number; /** * Use Web Workers for compression/decompression (browser only). * - true: Always use worker * - false: Never use worker * - undefined: Auto-detect based on data size (use worker when >= autoWorkerThreshold) * * Note: This option is ignored in Node.js (which uses native zlib thread pool). */ useWorker?: boolean; /** * Threshold (in bytes) for auto-worker decision (browser only). * When useWorker is undefined, data >= this threshold will use workers. * * Default: 1MB. */ autoWorkerThreshold?: number; /** * Allow transferring the input buffer to the worker (browser only). * When true, the input buffer will be transferred (zero-copy) and become unusable. * * Use this for better performance when you don't need the input data after compression. */ allowTransfer?: boolean; /** * Abort signal for cancellation when using worker pool (browser only). * * Note: This option is ignored in Node.js. */ signal?: AbortSignal; } /** * Default threshold (in bytes) to choose the lower-overhead path. * * This is a performance knob, not a correctness requirement. * Default: 8MB. */ export declare const DEFAULT_COMPRESS_THRESHOLD_BYTES: number; /** * Resolve the effective threshold bytes. */ export declare function resolveCompressThresholdBytes(options: CompressOptions): number; /** * Check if CompressionStream is available */ export declare function hasCompressionStream(): boolean; /** * Non-cached probe for CompressionStream("deflate-raw") support. * * Prefer this in code paths that want up-to-date environment checks * (e.g. tests that stub globals). */ export declare function probeDeflateRawCompressionStream(): boolean; /** * Non-cached probe for DecompressionStream("deflate-raw") support. */ export declare function probeDeflateRawDecompressionStream(): boolean; /** * Non-cached probe for full deflate-raw Web Streams support. * * Returns true only if BOTH CompressionStream("deflate-raw") and * DecompressionStream("deflate-raw") are supported. */ export declare function probeDeflateRawWebStreams(): boolean; /** * Check if CompressionStream supports the "deflate-raw" format. * * This is a stricter check than {@link hasCompressionStream} because some * environments expose CompressionStream but do not support "deflate-raw". */ export declare function hasDeflateRawCompressionStream(): boolean; /** * Check if DecompressionStream supports the "deflate-raw" format. */ export declare function hasDeflateRawDecompressionStream(): boolean; /** * Cached check for full deflate-raw Web Streams support. * * Returns true only if BOTH CompressionStream("deflate-raw") and * DecompressionStream("deflate-raw") are supported. */ export declare function hasDeflateRawWebStreams(): boolean; export declare function streamToUint8Array(reader: ReadableStreamDefaultReader): Promise; export declare function transformWithStream(data: Uint8Array, stream: CompressionStream | DecompressionStream): Promise; /** * Compress using CompressionStream API * Uses "deflate-raw" format (required for ZIP files) * * @param data - Data to compress * @returns Compressed data */ export declare function compressWithStream(data: Uint8Array): Promise; /** * Decompress using DecompressionStream API * * @param data - Compressed data (deflate-raw format) * @returns Decompressed data */ export declare function decompressWithStream(data: Uint8Array): Promise; /** * Compute Adler-32 checksum of data. * * Adler-32 is used in the Zlib format trailer (RFC 1950). * It's faster than CRC32 but has weaker error detection. * * @param data - Input data * @returns 32-bit Adler-32 checksum */ export declare function adler32(data: Uint8Array): number; /** GZIP magic number byte 1 */ export declare const GZIP_ID1 = 31; /** GZIP magic number byte 2 */ export declare const GZIP_ID2 = 139; /** Compression method: DEFLATE */ export declare const GZIP_CM_DEFLATE = 8; export declare const GZIP_FLAG_FTEXT = 1; export declare const GZIP_FLAG_FHCRC = 2; export declare const GZIP_FLAG_FEXTRA = 4; export declare const GZIP_FLAG_FNAME = 8; export declare const GZIP_FLAG_FCOMMENT = 16; /** Minimum valid GZIP size: 10-byte header + 8-byte trailer */ export declare const GZIP_MIN_SIZE = 18; /** * Check if data appears to be GZIP compressed (magic number check) */ export declare function isGzipData(data: Uint8Array): boolean; export declare const hasGzipCompressionStream: () => boolean; export declare const hasGzipDecompressionStream: () => boolean; export declare const hasDeflateCompressionStream: () => boolean; export declare const hasDeflateDecompressionStream: () => boolean; /** * Zlib compression method: DEFLATE (8) * Stored in lower 4 bits of CMF byte */ export declare const ZLIB_CM_DEFLATE = 8; /** * Maximum window size exponent for CINFO field (7 = 32KB window) * Stored in upper 4 bits of CMF byte */ export declare const ZLIB_CINFO_MAX = 7; /** * Minimum valid Zlib size: 2-byte header + 4-byte Adler-32 trailer */ export declare const ZLIB_MIN_SIZE = 6; /** * Check if data appears to be Zlib compressed. * * Zlib header format (RFC 1950): * - Byte 0 (CMF): CM (4 bits) + CINFO (4 bits) * - CM must be 8 (DEFLATE) * - CINFO must be <= 7 * - Byte 1 (FLG): FCHECK (5 bits) + FDICT (1 bit) + FLEVEL (2 bits) * - FCHECK: (CMF * 256 + FLG) % 31 == 0 * * This distinguishes Zlib from: * - GZIP: starts with 0x1f 0x8b * - Raw DEFLATE: first byte typically doesn't satisfy zlib checksum */ export declare function isZlibData(data: Uint8Array): boolean; /** * Detect the compression format of data. * * Returns: * - "gzip": GZIP format (RFC 1952) * - "zlib": Zlib format (RFC 1950) * - "deflate-raw": Raw DEFLATE (RFC 1951) or unknown */ export declare function detectCompressionFormat(data: Uint8Array): "gzip" | "zlib" | "deflate-raw"; /** * Get Zlib header for a given compression level. */ export declare function getZlibHeader(level: number): Uint8Array; /** * Build Zlib trailer (4 bytes) - Adler-32 checksum (big-endian) */ export declare function buildZlibTrailer(adlerValue: number): Uint8Array; /** * Parse Zlib header and extract offset to DEFLATE payload. * Returns the byte offset where raw DEFLATE data starts. * @throws If header is invalid or uses preset dictionary. */ export declare function parseZlibHeader(data: Uint8Array): number; /** * Read Adler-32 from Zlib trailer (big-endian). */ export declare function readZlibTrailer(data: Uint8Array): number; /** * Verify Adler-32 checksum. * @throws If checksum doesn't match. */ export declare function verifyAdler32(data: Uint8Array, expected: number): void;