/** * True Streaming ZIP creator - shared implementation. * * This module is intentionally platform-agnostic. * - In Node builds it uses `./compression/crc32` + `./compression/streaming-compress` (zlib-backed). * - In browser builds the bundler aliases those imports to their browser variants. */ import { type ZipEncryptionMethod } from "../crypto/index.js"; import { type ZipStringCodec, type ZipStringEncoding } from "../shared/text.js"; import type { ZipTimestampMode } from "../zip-spec/timestamps.js"; import type { ZipEntryInfo } from "../zip-spec/zip-entry-info.js"; import { type ZipPathOptions } from "../zip-spec/zip-path.js"; import { type Zip64Mode } from "../zip-spec/zip-records.js"; import type { ZipCentralDirEntry, ZipWritableFile } from "./writable-file.js"; export type { Zip64Mode } from "../zip-spec/zip-records.js"; export type { ZipCentralDirEntry, ZipWritableFile } from "./writable-file.js"; /** * Encryption options for streaming ZIP creation. */ export interface StreamingZipEncryptionOptions { /** Encryption method to use */ encryptionMethod?: ZipEncryptionMethod; /** Password for encryption */ password?: string | Uint8Array; } /** * True Streaming ZIP File - compresses chunk by chunk */ export declare class ZipDeflateFile { private _deflate; private _crc; private _uncompressedSize; private _compressedSize; private _finalized; private _headerEmitted; private _ondata; private _onerror; private _centralDirEntryInfo; private _pendingEnd; private _emittedDataDescriptor; private _localHeader; private _zip64Mode; private _zip64; private _deflateWanted; private _pendingChunks; private _sampleLen; private _smartStore; private _completeResolve; private _completeReject; private _completePromise; private _completeError; private _encryptionMethod; private _password; private _zipCryptoState; private _aesKeyStrength; private _aesBuffer; private _aesBufferSize; private _originalCompressionMethod; private _aesExtraField; private _dataQueue; private _finalQueued; private _pushChain; private _inputBuf; private _inputPos; private _syncDeflater; private _syncZlibReady; readonly name: string; readonly level: number; readonly nameBytes: Uint8Array; readonly commentBytes: Uint8Array; readonly dosTime: number; readonly dosDate: number; extraField: Uint8Array; private _flags; private _compressionMethod; private readonly _modTime; private _externalAttributes; private _versionMadeBy?; private readonly _stringCodec; constructor(name: string, options?: { level?: number; modTime?: Date; atime?: Date; ctime?: Date; birthTime?: Date; timestamps?: ZipTimestampMode; comment?: string; smartStore?: boolean; zip64?: Zip64Mode; /** Encryption method to use */ encryptionMethod?: ZipEncryptionMethod; /** Password for encryption */ password?: string | Uint8Array; /** Optional Unix mode/permissions (may include type bits). */ mode?: number; /** Optional MS-DOS attributes (low 8 bits). */ msDosAttributes?: number; /** Advanced override for external attributes. */ externalAttributes?: number; /** Advanced override for central directory versionMadeBy. */ versionMadeBy?: number; /** Optional entry name normalization. */ path?: false | ZipPathOptions; /** Optional string encoding for this entry name/comment. */ encoding?: ZipStringEncoding; }); private _buildCompressionMethod; /** * Get or build the AES extra field (cached for reuse). */ private _getAesExtraField; /** * Initialize ZipCrypto encryption state and emit header. * Called once before first data write. */ private _initZipCryptoEncryption; /** * Encrypt data chunk using ZipCrypto (streaming). * Uses the exported zipCryptoEncryptByte for each byte. */ private _zipCryptoEncryptChunk; private _initDeflateStream; /** * Finalize encryption (if needed) and emit data descriptor. */ private _finalizeEncryptionAndEmitDescriptor; /** * Finalize AES encryption: encrypt buffered data and emit. */ private _finalizeAesEncryption; private _buildLocalHeader; private _accumulateSampleLen; private _shouldDecide; private _decideCompressionIfNeeded; private _emitHeaderIfNeeded; private _flushPendingChunks; private _enqueueData; private _flushQueue; get ondata(): ((data: Uint8Array, final: boolean) => void) | null; set ondata(cb: (data: Uint8Array, final: boolean) => void); get onerror(): ((err: Error) => void) | null; set onerror(cb: (err: Error) => void); private _resolveComplete; private _rejectComplete; private _ensureCompletePromise; private _tapCallback; private _writeDataSync; private _writeData; private _endDeflateAndWait; private _finalizeAfterWrite; private _pushUnchained; /** * Push data — compresses and outputs immediately. * * Returns a Promise that resolves when the write is complete. * If final=true, it resolves after the data descriptor is emitted. * * When no async deflate stream is needed (the common case: smartStore without * encryption), data is compressed and emitted synchronously via SyncDeflater. * This avoids _pushChain closure accumulation that would cause unbounded * memory growth when callers push data in a tight synchronous loop. */ push(data: Uint8Array, final?: boolean, callback?: (err?: Error | null) => void): Promise; /** Enqueue an async push through the _pushChain serialization. */ private _pushAsync; /** * Synchronous push path — compresses and emits data without any Promises. * * Uses SyncDeflater (native zlib.deflateRawSync on Node.js, pure-JS LZ77 on * browsers) so the entire data flow is synchronous: * push → _writeDataSync → SyncDeflater.write → _enqueueData → ondata * * Called automatically by push() when no async deflate stream is active. */ private _pushSyncPath; private _finalizeSyncAfterWrite; /** * Emit local file header with Data Descriptor flag */ private _emitHeader; /** * Emit Data Descriptor with CRC and sizes */ private _emitDataDescriptor; /** * Returns a promise that resolves when the file is completely written * (including data descriptor) */ complete(): Promise; /** * Get entry metadata in the same shape as unzip parser outputs. * This is best-effort: writer-only fields like encryption are always false. */ getEntryInfo(): ZipEntryInfo | null; /** Writer-only metadata for building the Central Directory. */ getCentralDirectoryEntryInfo(): ZipCentralDirEntry | null; isComplete(): boolean; abort(reason?: unknown): void; } /** * Passthrough ZIP entry writer. * * Emits a local header with data-descriptor flag, then streams the provided * raw payload (already compressed and/or encrypted), then emits a data descriptor. */ export declare class ZipRawFile implements ZipWritableFile { private _headerEmitted; private _finalized; private _started; private _zip64Mode; private _zip64; private _dataQueue; private _dataQueueHead; private _finalQueued; private _ondata; private _onerror; private _centralDirEntryInfo; readonly name: string; readonly nameBytes: Uint8Array; readonly commentBytes: Uint8Array; readonly dosTime: number; readonly dosDate: number; readonly extraField: Uint8Array; private readonly _flags; private readonly _compressionMethod; private readonly _crc32; private readonly _compressedSize; private readonly _uncompressedSize; private readonly _externalAttributes; private readonly _versionMadeBy?; private _source; private _chunkSize; private readonly _stringCodec; private _doneResolve; private _doneReject; private _donePromise; constructor(name: string, options: { compressedData: Uint8Array | AsyncIterable; crc32: number; compressedSize: number; uncompressedSize: number; compressionMethod: number; flags?: number; comment?: Uint8Array; extraField?: Uint8Array; dosTime: number; dosDate: number; zip64?: Zip64Mode; externalAttributes?: number; versionMadeBy?: number; chunkSize?: number; codec?: ZipStringCodec; }); /** * Resolves when the file has fully emitted its local header, payload, * and trailing data descriptor. */ done(): Promise; get ondata(): ((data: Uint8Array, final: boolean) => void) | null; set ondata(fn: ((data: Uint8Array, final: boolean) => void) | null); get onerror(): ((err: Error) => void) | null; set onerror(fn: ((err: Error) => void) | null); getCentralDirectoryEntryInfo(): ZipCentralDirEntry | null; private _enqueueData; private _drainQueue; private _buildLocalHeader; private _buildDataDescriptor; start(): Promise; abort(reason?: unknown): void; } /** * Streaming ZIP Creator - processes files sequentially */ export declare class StreamingZip { private callback; private entries; private currentOffset; private ended; private endPending; private addedEntryCount; private zipComment; private zip64Mode; private readonly _stringCodec; private fileQueue; private fileQueueIndex; private activeFile; constructor(callback: (err: Error | null, data: Uint8Array, final: boolean) => void, options?: { comment?: string; zip64?: Zip64Mode; encoding?: ZipStringEncoding; codec?: ZipStringCodec; }); add(file: ZipWritableFile): void; private _processNextFile; private _finalize; end(): void; abort(reason?: unknown): void; } export { StreamingZip as Zip, ZipDeflateFile as ZipDeflate };