@zip.js/zip.js 2.9.0 → 2.10.0

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.
package/index.d.ts CHANGED
@@ -1594,10 +1594,17 @@ export interface ZipReaderGetEntriesOptions
1594
1594
  export interface GetEntriesOptions {
1595
1595
  /**
1596
1596
  * The encoding of the filename of the entry.
1597
+ *
1598
+ * The option is ignored when the general purpose bit 11 is set in the header of the entry: such a
1599
+ * filename is always decoded as UTF-8. It is only read when the bit is not set, and the filename is
1600
+ * then decoded as IBM Code Page 437 when the option is not set either.
1597
1601
  */
1598
1602
  filenameEncoding?: string;
1599
1603
  /**
1600
1604
  * The encoding of the comment of the entry.
1605
+ *
1606
+ * The option is ignored when the general purpose bit 11 is set in the header of the entry, see
1607
+ * {@link GetEntriesOptions#filenameEncoding}.
1601
1608
  */
1602
1609
  commentEncoding?: string;
1603
1610
  /**
@@ -3244,6 +3251,10 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
3244
3251
  * Note that this option only sets the flag, it does not ensure that the file names are in the correct
3245
3252
  * encoding: when it is set to `false`, the names are still encoded in UTF-8 unless the
3246
3253
  * {@link ZipWriterConstructorOptions#encodeText} option is also set to encode them in the intended code page.
3254
+ * Setting it to `false` alone therefore produces an archive whose file names are mislabeled, holding UTF-8
3255
+ * bytes announced as Code Page 437: the names holding characters outside of ASCII are decoded incorrectly
3256
+ * by the readers honoring the flag, including {@link ZipReader} unless
3257
+ * {@link GetEntriesOptions#filenameEncoding} is set to `"utf-8"`.
3247
3258
  *
3248
3259
  * @defaultValue true
3249
3260
  */
@@ -3458,6 +3469,10 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
3458
3469
  /**
3459
3470
  * The function called for encoding the filename and the comment of the entry.
3460
3471
  *
3472
+ * zip.js encodes them in UTF-8 when the option is not set, so it must be set to write them in another
3473
+ * code page, together with {@link ZipWriterConstructorOptions#useUnicodeFileNames} set to `false` to
3474
+ * announce them as Code Page 437 instead of UTF-8.
3475
+ *
3461
3476
  * @param text The text to encode.
3462
3477
  * @param type The type of the encoded text, `"filename"` or `"comment"`.
3463
3478
  * @returns The encoded text or `undefined` if the text should be encoded by zip.js.
@@ -3545,6 +3560,9 @@ export class ZipEntry {
3545
3560
  * @remarks It is the size of the raw compressed content when the entry has been imported with the
3546
3561
  * `passThrough` option set to `true`, since the entry holds the compressed data in that case. The
3547
3562
  * uncompressed size of the original entry remains available in {@link ZipEntry#data}.
3563
+ *
3564
+ * It is updated by the `{@link ZipFileEntry}#replace*()` methods, and it is `0` for an entry holding
3565
+ * a `ReadableStream` instance, whose size is only known once the entry has been read.
3548
3566
  */
3549
3567
  uncompressedSize: number;
3550
3568
  /**
@@ -3594,6 +3612,12 @@ export class ZipEntry {
3594
3612
  /**
3595
3613
  * Set the name of the entry
3596
3614
  *
3615
+ * @remarks A name holding `"/"` is split into path components, like the name passed to a
3616
+ * `{@link ZipDirectoryEntry}#add*()` method, so it moves the entry into the directories it names,
3617
+ * creating them when they do not exist. Renaming an entry to the name it already has does nothing.
3618
+ * Renaming it onto an existing sibling throws an {@link ERR_ENTRY_EXISTS} error, and renaming it
3619
+ * into itself or into one of its descendants throws.
3620
+ *
3597
3621
  * @param name The new name of the entry.
3598
3622
  */
3599
3623
  rename(name: string): void;
@@ -3619,6 +3643,11 @@ export class ZipEntry {
3619
3643
 
3620
3644
  /**
3621
3645
  * Represents a file entry in the zip (Filesystem API).
3646
+ *
3647
+ * @remarks A `{@link ZipFileEntry}#replace*()` method describes the entry with the content it is
3648
+ * given: it updates {@link ZipEntry#uncompressedSize} and clears the pass-through state of an entry
3649
+ * imported with the {@link ZipReaderOptions#passThrough} option, since the bytes copied verbatim are
3650
+ * gone. Replacing the content of an entry therefore keeps {@link ZipFS#getExportedSize} exact.
3622
3651
  */
3623
3652
  export class ZipFileEntry<ReaderType, WriterType> extends ZipEntry {
3624
3653
  /**
@@ -3737,6 +3766,10 @@ export class ZipFileEntry<ReaderType, WriterType> extends ZipEntry {
3737
3766
  /**
3738
3767
  * Replaces the content of the entry with a `ReadableStream` instance
3739
3768
  *
3769
+ * @remarks The size of a `ReadableStream` instance is unknown, so the entry reports an undetermined
3770
+ * size, like an entry added with {@link ZipDirectoryEntry#addReadable}. {@link ZipFS#getExportedSize}
3771
+ * then throws an {@link ERR_UNDETERMINED_SIZE} error instead of returning a size it cannot predict.
3772
+ *
3740
3773
  * @param readable The `ReadableStream` instance.
3741
3774
  */
3742
3775
  replaceReadable(readable: ReadableStream): void;
@@ -3744,6 +3777,13 @@ export class ZipFileEntry<ReaderType, WriterType> extends ZipEntry {
3744
3777
 
3745
3778
  /**
3746
3779
  * Represents a directory entry in the zip (Filesystem API).
3780
+ *
3781
+ * @remarks The `name` passed to an `{@link ZipDirectoryEntry}#add*()` method is split into path
3782
+ * components, exactly like the filename of an imported entry, so `addText("a/b.txt", text)` adds
3783
+ * `"b.txt"` to the `"a"` directory and creates that directory when it does not exist. Empty
3784
+ * components and `"."` components are ignored. The directories created that way are navigable like
3785
+ * any other entry but are not written when the tree is exported, so the zip file holds the same
3786
+ * entries whichever way the path was built.
3747
3787
  */
3748
3788
  export class ZipDirectoryEntry extends ZipEntry {
3749
3789
  /**
@@ -3916,7 +3956,7 @@ export class ZipDirectoryEntry extends ZipEntry {
3916
3956
  */
3917
3957
  importBlob(
3918
3958
  blob: Blob,
3919
- options?: ZipReaderConstructorOptions
3959
+ options?: ZipDirectoryEntryImportOptions
3920
3960
  ): Promise<[ZipEntry]>;
3921
3961
  /**
3922
3962
  * Extracts a zip file provided as a Data URI `string` encoded in Base64 into the entry
@@ -3929,7 +3969,7 @@ export class ZipDirectoryEntry extends ZipEntry {
3929
3969
  */
3930
3970
  importData64URI(
3931
3971
  dataURI: string,
3932
- options?: ZipReaderConstructorOptions
3972
+ options?: ZipDirectoryEntryImportOptions
3933
3973
  ): Promise<[ZipEntry]>;
3934
3974
  /**
3935
3975
  * Extracts a zip file provided as a `Uint8Array` instance into the entry
@@ -3942,7 +3982,7 @@ export class ZipDirectoryEntry extends ZipEntry {
3942
3982
  */
3943
3983
  importUint8Array(
3944
3984
  array: Uint8Array,
3945
- options?: ZipReaderConstructorOptions
3985
+ options?: ZipDirectoryEntryImportOptions
3946
3986
  ): Promise<[ZipEntry]>;
3947
3987
  /**
3948
3988
  * Extracts a zip file fetched from a URL into the entry
@@ -3972,7 +4012,7 @@ export class ZipDirectoryEntry extends ZipEntry {
3972
4012
  */
3973
4013
  importReadable(
3974
4014
  readable: ReadableStream,
3975
- options?: ZipReaderConstructorOptions
4015
+ options?: ZipDirectoryEntryImportOptions
3976
4016
  ): Promise<[ZipEntry]>;
3977
4017
  /**
3978
4018
  * Extracts a zip file provided via a custom {@link Reader} instance or a {@link ZipReader} instance into
@@ -4008,7 +4048,7 @@ export class ZipDirectoryEntry extends ZipEntry {
4008
4048
  | ReadableReader[]
4009
4049
  | ReadableStream[]
4010
4050
  | ZipReader<unknown>,
4011
- options?: ZipReaderConstructorOptions
4051
+ options?: ZipDirectoryEntryImportOptions
4012
4052
  ): Promise<[ZipEntry]>;
4013
4053
  /**
4014
4054
  * Returns a `Blob` instance containing a zip file of the entry and its descendants
@@ -4112,7 +4152,9 @@ export class ZipDirectoryEntry extends ZipEntry {
4112
4152
  * This happens when `usdz` is set, since the alignment padding depends on the offset of each
4113
4153
  * entry, and when the archive exceeds 4GB, since the offsets recorded in the central directory
4114
4154
  * are then extended to 64 bits. Passing `bufferedWrite: false` makes both determinable again,
4115
- * as does exporting a directory whose children are all files. It is thrown as well when
4155
+ * as does exporting a directory whose children are all files. A name holding `"/"` creates the
4156
+ * directories it names, so `addText("a/b.txt", text)` builds a tree whose children are not all
4157
+ * files, even though the directories created that way are not written. It is thrown as well when
4116
4158
  * `signCentralDirectory` is set, the length of the signature being unknown until it is computed.
4117
4159
  *
4118
4160
  * @param options The options.
@@ -4122,11 +4164,39 @@ export class ZipDirectoryEntry extends ZipEntry {
4122
4164
  getExportedSize(options?: ZipDirectoryEntryExportOptions): Promise<number>;
4123
4165
  }
4124
4166
 
4167
+ /**
4168
+ * Represents the options passed to `{@link ZipDirectoryEntry}#import*()`.
4169
+ */
4170
+ export interface ZipDirectoryEntryImportOptions
4171
+ extends ZipReaderConstructorOptions {
4172
+ /**
4173
+ * The policy applied when two entries of the imported zip file claim the same node of the tree
4174
+ *
4175
+ * @remarks
4176
+ * The tree indexes the entries by path, whereas a zip file stores a flat list of filenames, so two
4177
+ * entries can claim one node: they can hold the same filename, hold filenames differing only by the
4178
+ * path components ignored when building the tree (e.g. `"a/b.txt"` and `"./a/b.txt"`), or one can be
4179
+ * a file and the other a directory holding it (e.g. `"a"` and `"a/b.txt"`).
4180
+ *
4181
+ * `"throw"` refuses the zip file with an {@link ERR_DUPLICATE_IMPORTED_ENTRY} error, whose `cause`
4182
+ * property holds the {@link EntryMetaData} instance of the entry that could not be imported. The
4183
+ * filesystem is left unchanged, i.e. the entries imported before the error are removed and the
4184
+ * content held before the import is restored.
4185
+ *
4186
+ * `"keep-first"` ignores the entry claiming a node already taken, and `"keep-last"` replaces the
4187
+ * entry holding the node, which is the behavior of most zip tools. The entries that do not collide
4188
+ * are imported in both cases.
4189
+ *
4190
+ * @defaultValue "throw"
4191
+ */
4192
+ duplicates?: "throw" | "keep-first" | "keep-last";
4193
+ }
4194
+
4125
4195
  /**
4126
4196
  * Represents the options passed to {@link ZipDirectoryEntry#importHttpContent}.
4127
4197
  */
4128
4198
  export interface ZipDirectoryEntryImportHttpOptions
4129
- extends ZipReaderConstructorOptions,
4199
+ extends ZipDirectoryEntryImportOptions,
4130
4200
  HttpOptions {}
4131
4201
 
4132
4202
  /**
@@ -4779,6 +4849,16 @@ export const ERR_ZIP_CRYPTO_LAST_MOD_DATE: string;
4779
4849
  * Entry already exists error (thrown by the filesystem API when adding an entry whose filename already exists)
4780
4850
  */
4781
4851
  export const ERR_ENTRY_EXISTS: string;
4852
+ /**
4853
+ * Duplicate imported entry error (thrown by `{@link ZipDirectoryEntry}#import*()` when two entries of the
4854
+ * zip file claim the same node of the tree and the {@link ZipDirectoryEntryImportOptions#duplicates} option
4855
+ * is set to `"throw"`)
4856
+ */
4857
+ export const ERR_DUPLICATE_IMPORTED_ENTRY: string;
4858
+ /**
4859
+ * Invalid duplicates option error
4860
+ */
4861
+ export const ERR_INVALID_DUPLICATES: string;
4782
4862
  /**
4783
4863
  * Readable stream already consumed error (thrown by the filesystem API when a readable stream is read more than once)
4784
4864
  */