|
1 | 1 | import fs from 'fs'; |
2 | 2 | import yazl from 'yazl'; |
3 | | -import extract from 'extract-zip'; |
| 3 | +import AdmZip from 'adm-zip'; |
4 | 4 | import { DirectoryPath } from '../types/file/directoryPath.js'; |
5 | 5 | import { FilePath } from '../types/file/filePath.js'; |
6 | 6 |
|
@@ -39,24 +39,29 @@ export class ZipService { |
39 | 39 | public async unArchive(sourceFile: FilePath, destinationDirectory: DirectoryPath): Promise<void> { |
40 | 40 | const MAX_FILES = 100_000; |
41 | 41 | const MAX_SIZE = 1_000_000_000; // 1 GB |
42 | | - let fileCount = 0; |
43 | | - let totalSize = 0; |
44 | 42 |
|
45 | | - await extract(sourceFile.toString(), { |
46 | | - dir: destinationDirectory.toString(), |
47 | | - onEntry: function (entry) { |
48 | | - fileCount++; |
49 | | - if (fileCount > MAX_FILES) { |
50 | | - throw new Error('Reached max. file count'); |
51 | | - } |
52 | | - // The uncompressedSize comes from the zip headers, so it might not be trustworthy. |
53 | | - // Alternatively, calculate the size from the readStream. |
54 | | - let entrySize = entry.uncompressedSize; |
55 | | - totalSize += entrySize; |
56 | | - if (totalSize > MAX_SIZE) { |
57 | | - throw new Error('Reached max. size'); |
58 | | - } |
| 43 | + // adm-zip extracts synchronously, with no per-entry read streams. This |
| 44 | + // avoids a hang on Node 22+ where yauzl/fd-slicer (used by extract-zip) |
| 45 | + // builds STORED-entry read streams that deliver every byte but never emit |
| 46 | + // `end`, leaving the extraction promise pending forever — even though all |
| 47 | + // files have already been written to disk — which crashes the CLI with |
| 48 | + // "unsettled top-level await" / exit code 13. |
| 49 | + const zip = new AdmZip(sourceFile.toString()); |
| 50 | + const entries = zip.getEntries(); |
| 51 | + |
| 52 | + if (entries.length > MAX_FILES) { |
| 53 | + throw new Error('Reached max. file count'); |
| 54 | + } |
| 55 | + // header.size is the uncompressed size declared in the zip headers, so it |
| 56 | + // might not be trustworthy — kept as a cheap guard against zip bombs. |
| 57 | + let totalSize = 0; |
| 58 | + for (const entry of entries) { |
| 59 | + totalSize += entry.header.size; |
| 60 | + if (totalSize > MAX_SIZE) { |
| 61 | + throw new Error('Reached max. size'); |
59 | 62 | } |
60 | | - }); |
| 63 | + } |
| 64 | + |
| 65 | + zip.extractAllTo(destinationDirectory.toString(), /* overwrite */ true); |
61 | 66 | } |
62 | 67 | } |
0 commit comments