@zip.js/zip.js 2.8.29 → 2.8.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/BENCHMARKS.md +35 -0
  2. package/deno.json +5 -4
  3. package/dist/zip-core.js +496 -218
  4. package/dist/zip-core.min.js +1 -1
  5. package/dist/zip-fs-core.js +398 -195
  6. package/dist/zip-fs-core.min.js +1 -1
  7. package/dist/zip-fs-native.js +588 -254
  8. package/dist/zip-fs-native.min.js +1 -1
  9. package/dist/zip-fs.js +587 -253
  10. package/dist/zip-fs.min.js +1 -1
  11. package/dist/zip-legacy.js +501 -223
  12. package/dist/zip-legacy.min.js +1 -1
  13. package/dist/zip-module.wasm +0 -0
  14. package/dist/zip-native.js +504 -226
  15. package/dist/zip-native.min.js +1 -1
  16. package/dist/zip-web-worker-native.js +1 -1
  17. package/dist/zip-web-worker.js +1 -1
  18. package/dist/zip.js +503 -225
  19. package/dist/zip.min.js +1 -1
  20. package/index-native.cjs +588 -254
  21. package/index-native.min.js +1 -1
  22. package/index.cjs +587 -253
  23. package/index.d.cts +2814 -0
  24. package/index.d.ts +364 -1
  25. package/index.min.js +1 -1
  26. package/lib/core/codec-worker.js +0 -10
  27. package/lib/core/configuration.js +21 -33
  28. package/lib/core/constants.js +3 -0
  29. package/lib/core/io.js +11 -19
  30. package/lib/core/options.js +11 -1
  31. package/lib/core/streams/aes-crypto-stream.js +16 -20
  32. package/lib/core/streams/codecs/crc32.js +39 -11
  33. package/lib/core/streams/common-crypto.js +0 -2
  34. package/lib/core/streams/zip-crypto-stream.js +12 -16
  35. package/lib/core/streams/zip-entry-stream.js +69 -19
  36. package/lib/core/streams/zlib-js/zlib-streams.min.js +1 -1
  37. package/lib/core/streams/zlib-wasm/zlib-streams-loader.js +1 -3
  38. package/lib/core/streams/zlib-wasm/zlib-streams.wasm +0 -0
  39. package/lib/core/util/base64.js +68 -0
  40. package/lib/core/util/decode-text.js +0 -1
  41. package/lib/core/util/default-mime-type.js +0 -3
  42. package/lib/core/util/inflate.js +194 -0
  43. package/lib/core/util/mime-type.js +26 -22
  44. package/lib/core/util/opfs-temp-stream.js +139 -0
  45. package/lib/core/web-worker-inline-native.js +1 -1
  46. package/lib/core/web-worker-inline-template-native.js +5 -5
  47. package/lib/core/web-worker-inline-wasm.js +1 -1
  48. package/lib/core/zip-fs.js +61 -10
  49. package/lib/core/zip-reader.js +162 -53
  50. package/lib/core/zip-writer.js +34 -41
  51. package/lib/core/zlib-streams-inline-template.js +3 -2
  52. package/lib/core/zlib-streams-inline.js +1 -1
  53. package/lib/zip-core-base.js +7 -2
  54. package/package.json +9 -6
  55. package/reserved-property-names.js +50 -0
  56. package/lib/core/util/mini-lz.js +0 -198
package/index.d.ts CHANGED
@@ -327,6 +327,60 @@ declare class CodecStream extends TransformStream {}
327
327
  */
328
328
  export function getMimeType(fileExtension: string): string;
329
329
 
330
+ /**
331
+ * A `TransformStream`-like temporary buffer returned by a {@link ZipWriterConstructorOptions.createTempStream} factory.
332
+ */
333
+ export interface TempStream {
334
+ /**
335
+ * The writable side, receiving the compressed data of a buffered entry.
336
+ */
337
+ writable: WritableStream;
338
+ /**
339
+ * The readable side, replayed into the final zip stream once the entry is ready.
340
+ */
341
+ readable: ReadableStream;
342
+ /**
343
+ * Optional cleanup, called once the entry has been processed (on success, error, or abort) to release any backing resource.
344
+ */
345
+ dispose?: () => void | Promise<void>;
346
+ }
347
+
348
+ /**
349
+ * Options for {@link createOPFSTempStream}.
350
+ */
351
+ export interface OPFSTempStreamOptions {
352
+ /**
353
+ * Spill a buffered entry to a file once its buffered data exceeds this size, in bytes. Smaller entries stay in memory.
354
+ *
355
+ * @defaultValue 1048576
356
+ */
357
+ thresholdBytes?: number;
358
+ /**
359
+ * Name of the OPFS sub-directory holding the temporary files.
360
+ *
361
+ * @defaultValue ".zip.js-temp"
362
+ */
363
+ directoryName?: string;
364
+ /**
365
+ * Returns (or resolves to) the root `FileSystemDirectoryHandle`. Defaults to `navigator.storage.getDirectory()`.
366
+ *
367
+ * Provide it to run inside a worker with a pre-obtained handle, or to test against a mock.
368
+ */
369
+ getDirectory?: () => FileSystemDirectoryHandle | Promise<FileSystemDirectoryHandle>;
370
+ }
371
+
372
+ /**
373
+ * Builds a {@link ZipWriterConstructorOptions.createTempStream} factory that spills the data of buffered entries to the Origin Private File System (OPFS) instead of keeping it in memory.
374
+ *
375
+ * An entry stays in memory until it exceeds `thresholdBytes`, then spills to a temporary OPFS file that is streamed back and deleted afterwards, so peak memory stays bounded on large buffered entries.
376
+ *
377
+ * OPFS is a browser/worker feature; feature-detect `navigator.storage.getDirectory` (or pass `getDirectory`) before using it, and let the writer use its in-memory default elsewhere.
378
+ *
379
+ * @param options The options.
380
+ * @returns A factory suitable for {@link ZipWriterConstructorOptions.createTempStream}.
381
+ */
382
+ export function createOPFSTempStream(options?: OPFSTempStreamOptions): () => Promise<TempStream>;
383
+
330
384
  /**
331
385
  * Represents an instance used to read or write unknown type of data.
332
386
  *
@@ -338,6 +392,10 @@ export interface Initializable {
338
392
  * Initializes the instance asynchronously
339
393
  */
340
394
  init?(): Promise<void>;
395
+ /**
396
+ * `true` if the instance is initialized.
397
+ */
398
+ initialized?: boolean;
341
399
  }
342
400
 
343
401
  /**
@@ -646,6 +704,22 @@ export class SplitDataWriter implements Initializable, WritableWriter {
646
704
  * The `WritableStream` instance.
647
705
  */
648
706
  writable: WritableStream;
707
+ /**
708
+ * The number of the disk being written.
709
+ */
710
+ diskNumber: number;
711
+ /**
712
+ * The byte offset of the disk being written.
713
+ */
714
+ diskOffset: number;
715
+ /**
716
+ * The maximum size of each disk in bytes.
717
+ */
718
+ maxSize: number;
719
+ /**
720
+ * The number of bytes still available on the disk being written.
721
+ */
722
+ availableSize: number;
649
723
  /**
650
724
  * Initializes the instance asynchronously
651
725
  */
@@ -861,12 +935,55 @@ export interface GetEntriesOptions {
861
935
  * @defaultValue false
862
936
  */
863
937
  checkAmbiguity?: boolean;
938
+ /**
939
+ * How tolerant the reader should be when the archive can be parsed in more than one way.
940
+ *
941
+ * - `"strict"`: reject anything another tool could interpret differently. The end of central directory
942
+ * record must sit exactly at the end of the file, no data may precede the zip structure, and the local file
943
+ * headers must agree with the central directory records. Equivalent to {@link GetEntriesOptions#checkAmbiguity}
944
+ * set to `true`.
945
+ * - `"balanced"`: select the last end of central directory record whose comment reaches the end of the file
946
+ * and that points to a central directory, ignore stale records left by in-place updates as well as records
947
+ * forged inside a comment, and tolerate a self-extracting stub or up to
948
+ * {@link GetEntriesOptions#maxAppendedDataSize} bytes of appended data. Throw an {@link ERR_AMBIGUOUS_ARCHIVE}
949
+ * error only when two or more records reach the end of the file and each points to a central directory, which
950
+ * cannot be disambiguated. A record that reaches the end of the file but points to no central directory (an
951
+ * empty archive) is only selected when no record points to one.
952
+ * - `"tolerant"`: never reject a parseable archive, except when {@link GetEntriesOptions#maxAppendedDataSize}
953
+ * is set explicitly and exceeded; recover by selecting the last end of central directory record that reaches
954
+ * the end of the file and points to a central directory (or, failing that, the last one that reaches the end
955
+ * of the file).
956
+ *
957
+ * @defaultValue "balanced"
958
+ */
959
+ strictness?: "strict" | "balanced" | "tolerant";
960
+ /**
961
+ * The maximum number of bytes tolerated after the zip structure before the archive is rejected. Defaults to
962
+ * `0` when {@link GetEntriesOptions#strictness} is `"strict"`, `65535` when it is `"balanced"`, and `Infinity`
963
+ * when it is `"tolerant"`.
964
+ *
965
+ * An explicit value takes precedence over the strictness default at every level, so it can loosen `"strict"`
966
+ * or reintroduce a rejection under `"tolerant"`. It also bounds how far back the end of central directory
967
+ * record is searched for, so a value smaller than the amount of data actually appended surfaces an
968
+ * {@link ERR_EOCDR_NOT_FOUND} error when the record lies beyond the searched region and an
969
+ * {@link ERR_AMBIGUOUS_ARCHIVE} error otherwise.
970
+ */
971
+ maxAppendedDataSize?: number;
864
972
  }
865
973
 
866
974
  /**
867
975
  * Represents options passed to the constructor of {@link ZipReader} and {@link FileEntry#getData}.
868
976
  */
869
977
  export interface ZipReaderOptions {
978
+ /**
979
+ * How tolerant the reader should be when the local file header of an entry disagrees with its central
980
+ * directory record. `"strict"` throws an {@link ERR_AMBIGUOUS_ARCHIVE} error (equivalent to
981
+ * {@link ZipReaderOptions#checkAmbiguity} set to `true`); `"balanced"` and `"tolerant"` trust the central
982
+ * directory record.
983
+ *
984
+ * @defaultValue "balanced"
985
+ */
986
+ strictness?: "strict" | "balanced" | "tolerant";
870
987
  /**
871
988
  * `true` to throw an {@link ERR_AMBIGUOUS_ARCHIVE} error when calling {@link FileEntry#getData} if the local
872
989
  * file header of the entry disagrees with its central directory record in a way that could make other tools
@@ -931,6 +1048,168 @@ export interface ZipReaderOptions {
931
1048
  preventClose?: boolean;
932
1049
  }
933
1050
 
1051
+ /**
1052
+ * Represents the parsed general purpose bit flag of an entry.
1053
+ */
1054
+ export interface EntryBitFlag {
1055
+ /**
1056
+ * The compression option bits.
1057
+ */
1058
+ level: number;
1059
+ /**
1060
+ * `true` if the entry data is followed by a data descriptor.
1061
+ */
1062
+ dataDescriptor: boolean;
1063
+ /**
1064
+ * `true` if the filename and the comment are encoded in UTF-8 (EFS).
1065
+ */
1066
+ languageEncodingFlag: boolean;
1067
+ }
1068
+ /**
1069
+ * Represents an extra field record of an entry.
1070
+ */
1071
+ export interface EntryExtraField {
1072
+ /**
1073
+ * The type (header id) of the extra field.
1074
+ */
1075
+ type: number;
1076
+ /**
1077
+ * The data of the extra field.
1078
+ */
1079
+ data: Uint8Array;
1080
+ }
1081
+ /**
1082
+ * Represents the AES extra field record of an entry.
1083
+ */
1084
+ export interface EntryExtraFieldAES extends EntryExtraField {
1085
+ /**
1086
+ * The encryption strength (1, 2 or 3).
1087
+ */
1088
+ strength?: number;
1089
+ /**
1090
+ * The compression method stored in the AES extra field.
1091
+ */
1092
+ originalCompressionMethod?: number;
1093
+ }
1094
+ /**
1095
+ * Represents a Unicode path or comment extra field record of an entry.
1096
+ */
1097
+ export interface EntryExtraFieldUnicode extends EntryExtraField {
1098
+ /**
1099
+ * `true` if the extra field is consistent with the entry metadata.
1100
+ */
1101
+ valid?: boolean;
1102
+ }
1103
+ /**
1104
+ * Represents the local file header fields of an entry, read when getting the entry data.
1105
+ */
1106
+ export interface LocalDirectory {
1107
+ /**
1108
+ * The "Version" field.
1109
+ */
1110
+ version: number;
1111
+ /**
1112
+ * `true` if the entry is encrypted.
1113
+ */
1114
+ encrypted: boolean;
1115
+ /**
1116
+ * The general purpose bit flag (raw).
1117
+ */
1118
+ rawBitFlag: number;
1119
+ /**
1120
+ * The general purpose bit flag.
1121
+ */
1122
+ bitFlag: EntryBitFlag;
1123
+ /**
1124
+ * The last modification date (raw).
1125
+ */
1126
+ rawLastModDate: number;
1127
+ /**
1128
+ * The last modification date.
1129
+ */
1130
+ lastModDate: Date;
1131
+ /**
1132
+ * The length of the filename in bytes.
1133
+ */
1134
+ filenameLength: number;
1135
+ /**
1136
+ * The length of the extra field in bytes.
1137
+ */
1138
+ extraFieldLength: number;
1139
+ /**
1140
+ * The extra field (raw).
1141
+ */
1142
+ rawExtraField: Uint8Array;
1143
+ /**
1144
+ * The extra field.
1145
+ */
1146
+ extraField?: Map<number, EntryExtraField>;
1147
+ /**
1148
+ * The signature (CRC32 checksum) of the content.
1149
+ */
1150
+ signature?: number;
1151
+ /**
1152
+ * The compressed size of the content.
1153
+ */
1154
+ compressedSize?: number;
1155
+ /**
1156
+ * The uncompressed size of the content.
1157
+ */
1158
+ uncompressedSize?: number;
1159
+ /**
1160
+ * The compression method.
1161
+ */
1162
+ compressionMethod?: number;
1163
+ /**
1164
+ * The Zip64 extra field.
1165
+ */
1166
+ extraFieldZip64?: EntryExtraField;
1167
+ /**
1168
+ * The AES extra field.
1169
+ */
1170
+ extraFieldAES?: EntryExtraFieldAES;
1171
+ /**
1172
+ * The NTFS extra field.
1173
+ */
1174
+ extraFieldNTFS?: EntryExtraField;
1175
+ /**
1176
+ * The Unix extra field.
1177
+ */
1178
+ extraFieldUnix?: EntryExtraField;
1179
+ /**
1180
+ * The Info-ZIP Unix extra field.
1181
+ */
1182
+ extraFieldInfoZip?: EntryExtraField;
1183
+ /**
1184
+ * The extended timestamp extra field.
1185
+ */
1186
+ extraFieldExtendedTimestamp?: EntryExtraField;
1187
+ /**
1188
+ * The Unicode path extra field.
1189
+ */
1190
+ extraFieldUnicodePath?: EntryExtraFieldUnicode;
1191
+ /**
1192
+ * The Unicode comment extra field.
1193
+ */
1194
+ extraFieldUnicodeComment?: EntryExtraFieldUnicode;
1195
+ /**
1196
+ * The USDZ extra field.
1197
+ */
1198
+ extraFieldUSDZ?: EntryExtraField;
1199
+ }
1200
+ /**
1201
+ * Represents an error raised while processing an entry, decorated with entry context.
1202
+ */
1203
+ export interface EntryError extends Error {
1204
+ /**
1205
+ * `true` if the zip file is corrupted because the entry data could not be written entirely.
1206
+ */
1207
+ corruptedEntry?: boolean;
1208
+ /**
1209
+ * The id of the related {@link ZipEntry} (filesystem API).
1210
+ */
1211
+ entryId?: number;
1212
+ }
934
1213
  /**
935
1214
  * Represents the metadata of an entry in a zip file (Core API).
936
1215
  */
@@ -1128,6 +1407,62 @@ export interface EntryMetaData {
1128
1407
  * The compression method.
1129
1408
  */
1130
1409
  compressionMethod: number;
1410
+ /**
1411
+ * The general purpose bit flag (raw).
1412
+ */
1413
+ rawBitFlag?: number;
1414
+ /**
1415
+ * The general purpose bit flag.
1416
+ */
1417
+ bitFlag?: EntryBitFlag;
1418
+ /**
1419
+ * The length of the filename in bytes.
1420
+ */
1421
+ filenameLength?: number;
1422
+ /**
1423
+ * The length of the extra field in bytes.
1424
+ */
1425
+ extraFieldLength?: number;
1426
+ /**
1427
+ * The Zip64 extra field.
1428
+ */
1429
+ extraFieldZip64?: EntryExtraField;
1430
+ /**
1431
+ * The AES extra field.
1432
+ */
1433
+ extraFieldAES?: EntryExtraFieldAES;
1434
+ /**
1435
+ * The NTFS extra field.
1436
+ */
1437
+ extraFieldNTFS?: EntryExtraField;
1438
+ /**
1439
+ * The Unix extra field.
1440
+ */
1441
+ extraFieldUnix?: EntryExtraField;
1442
+ /**
1443
+ * The Info-ZIP Unix extra field.
1444
+ */
1445
+ extraFieldInfoZip?: EntryExtraField;
1446
+ /**
1447
+ * The extended timestamp extra field.
1448
+ */
1449
+ extraFieldExtendedTimestamp?: EntryExtraField;
1450
+ /**
1451
+ * The Unicode path extra field.
1452
+ */
1453
+ extraFieldUnicodePath?: EntryExtraFieldUnicode;
1454
+ /**
1455
+ * The Unicode comment extra field.
1456
+ */
1457
+ extraFieldUnicodeComment?: EntryExtraFieldUnicode;
1458
+ /**
1459
+ * The USDZ extra field.
1460
+ */
1461
+ extraFieldUSDZ?: EntryExtraField;
1462
+ /**
1463
+ * The local file header fields, set when the entry data has been read.
1464
+ */
1465
+ localDirectory?: LocalDirectory;
1131
1466
  }
1132
1467
  export interface DirectoryEntry extends EntryMetaData {
1133
1468
  /**
@@ -1486,8 +1821,11 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
1486
1821
  *
1487
1822
  * When provided, this replaces the default in-memory `TransformStream` buffer, allowing data to be stored externally (e.g. filesystem, OPFS, network).
1488
1823
  * The `writable` side receives compressed entry data. The `readable` side is consumed when the entry is replayed into the final zip stream.
1824
+ * The optional `dispose` method is called once the entry has been processed (on success, error, or abort) so a resource-backed buffer can release its resource.
1825
+ *
1826
+ * See {@link createOPFSTempStream} for a ready-made OPFS-backed implementation.
1489
1827
  */
1490
- createTempStream?: () => Promise<{ writable: WritableStream; readable: ReadableStream }>;
1828
+ createTempStream?: () => TempStream | Promise<TempStream>;
1491
1829
  /**
1492
1830
  * `true` to keep the order of the entry physically in the zip file.
1493
1831
  *
@@ -2165,6 +2503,17 @@ export class ZipDirectoryEntry extends ZipEntry {
2165
2503
  writable?: WritableStream,
2166
2504
  options?: ZipDirectoryEntryExportOptions
2167
2505
  ): Promise<WritableStream>;
2506
+ /**
2507
+ * Writes the entry and its descendants into a directory as files and sub-directories via the File System Access API (e.g. the Origin Private File System). Files are streamed and directories are merged into the target; colliding files are overwritten. This is the inverse of {@link ZipDirectoryEntry#addFileSystemHandle}.
2508
+ *
2509
+ * @param directoryHandle The target `FileSystemDirectoryHandle` instance.
2510
+ * @param options The options.
2511
+ * @returns A promise resolving to the target `FileSystemDirectoryHandle` instance.
2512
+ */
2513
+ exportFileSystemHandle(
2514
+ directoryHandle: FileSystemDirectoryHandle,
2515
+ options?: ZipDirectoryEntryExportFileSystemHandleOptions
2516
+ ): Promise<FileSystemDirectoryHandle>;
2168
2517
  /**
2169
2518
  * Creates a zip file via a custom {@link Writer} instance containing the entry and its descendants
2170
2519
  *
@@ -2209,6 +2558,19 @@ export interface ZipDirectoryEntryExportOptions
2209
2558
  readerOptions?: ZipReaderConstructorOptions;
2210
2559
  }
2211
2560
 
2561
+ /**
2562
+ * Represents the options passed to {@link ZipDirectoryEntry#exportFileSystemHandle} and {@link FS#exportFileSystemHandle}.
2563
+ */
2564
+ export interface ZipDirectoryEntryExportFileSystemHandleOptions
2565
+ extends EntryGetDataOptions {
2566
+ /**
2567
+ * `true` to write independent files concurrently instead of one after another.
2568
+ *
2569
+ * @defaultValue false
2570
+ */
2571
+ concurrent?: boolean;
2572
+ }
2573
+
2212
2574
  /**
2213
2575
  * Represents a Filesystem instance.
2214
2576
  *
@@ -2251,6 +2613,7 @@ export interface FS
2251
2613
  | "exportData64URI"
2252
2614
  | "exportUint8Array"
2253
2615
  | "exportWritable"
2616
+ | "exportFileSystemHandle"
2254
2617
  | "exportZip"
2255
2618
  | "isPasswordProtected"
2256
2619
  | "checkPassword"