@zip.js/zip.js 2.7.43 → 2.7.44

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.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
3
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
5
4
  /*
6
5
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
7
6
 
@@ -1042,7 +1041,7 @@ function Deflate() {
1042
1041
  bi_windup(); // align on byte boundary
1043
1042
  last_eob_len = 8; // enough lookahead for inflate
1044
1043
 
1045
- if (header) {
1044
+ {
1046
1045
  put_short(len);
1047
1046
  put_short(~len);
1048
1047
  }
@@ -1057,7 +1056,7 @@ function Deflate() {
1057
1056
  eof // true if this is the last block for a file
1058
1057
  ) {
1059
1058
  send_bits((STORED_BLOCK << 1) + (eof ? 1 : 0), 3); // send block type
1060
- copy_block(buf, stored_len, true); // with header
1059
+ copy_block(buf, stored_len); // with header
1061
1060
  }
1062
1061
 
1063
1062
  // Determine the best encoding for the current block: dynamic trees, static
@@ -4327,6 +4326,7 @@ const FUNCTION_TYPE = "function";
4327
4326
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4328
4327
  */
4329
4328
 
4329
+
4330
4330
  class StreamAdapter {
4331
4331
 
4332
4332
  constructor(Codec) {
@@ -4377,6 +4377,7 @@ class StreamAdapter {
4377
4377
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4378
4378
  */
4379
4379
 
4380
+
4380
4381
  const MINIMUM_CHUNK_SIZE = 64;
4381
4382
  let maxWorkers = 2;
4382
4383
  try {
@@ -4491,6 +4492,7 @@ function setIfDefined(propertyName, propertyValue) {
4491
4492
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4492
4493
  */
4493
4494
 
4495
+
4494
4496
  function getMimeType$1() {
4495
4497
  return "application/octet-stream";
4496
4498
  }
@@ -4523,6 +4525,7 @@ function getMimeType$1() {
4523
4525
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4524
4526
  */
4525
4527
 
4528
+
4526
4529
  const table$1 = {
4527
4530
  "application": {
4528
4531
  "andrew-inset": "ez",
@@ -6214,6 +6217,7 @@ class Crc32 {
6214
6217
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6215
6218
  */
6216
6219
 
6220
+
6217
6221
  class Crc32Stream extends TransformStream {
6218
6222
 
6219
6223
  constructor() {
@@ -6263,6 +6267,7 @@ class Crc32Stream extends TransformStream {
6263
6267
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6264
6268
  */
6265
6269
 
6270
+
6266
6271
  function encodeText(value) {
6267
6272
  if (typeof TextEncoder == UNDEFINED_TYPE) {
6268
6273
  value = unescape(encodeURIComponent(value));
@@ -7124,6 +7129,7 @@ misc.hmacSha1 = class {
7124
7129
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7125
7130
  */
7126
7131
 
7132
+
7127
7133
  const GET_RANDOM_VALUES_SUPPORTED = typeof crypto != UNDEFINED_TYPE && typeof crypto.getRandomValues == FUNCTION_TYPE;
7128
7134
 
7129
7135
  const ERR_INVALID_PASSWORD = "Invalid password";
@@ -7166,6 +7172,7 @@ function getRandomValues(array) {
7166
7172
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7167
7173
  */
7168
7174
 
7175
+
7169
7176
  const BLOCK_LENGTH = 16;
7170
7177
  const RAW_FORMAT = "raw";
7171
7178
  const PBKDF2_ALGORITHM = { name: "PBKDF2" };
@@ -7469,6 +7476,7 @@ function toBits(codecBytes, chunk) {
7469
7476
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7470
7477
  */
7471
7478
 
7479
+
7472
7480
  const HEADER_LENGTH = 12;
7473
7481
 
7474
7482
  class ZipCryptoDecryptionStream extends TransformStream {
@@ -7616,6 +7624,7 @@ function getInt32(number) {
7616
7624
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7617
7625
  */
7618
7626
 
7627
+
7619
7628
  const COMPRESSION_FORMAT = "deflate-raw";
7620
7629
 
7621
7630
  class DeflateStream extends TransformStream {
@@ -7756,6 +7765,7 @@ function pipeThrough(readable, transformStream) {
7756
7765
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7757
7766
  */
7758
7767
 
7768
+
7759
7769
  const MESSAGE_EVENT_TYPE = "message";
7760
7770
  const MESSAGE_START = "start";
7761
7771
  const MESSAGE_PULL = "pull";
@@ -7877,6 +7887,7 @@ class ChunkStream extends TransformStream {
7877
7887
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7878
7888
  */
7879
7889
 
7890
+
7880
7891
  // deno-lint-ignore valid-typeof
7881
7892
  let WEB_WORKERS_SUPPORTED = typeof Worker != UNDEFINED_TYPE;
7882
7893
 
@@ -8205,6 +8216,7 @@ async function onMessage({ data }, workerData) {
8205
8216
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8206
8217
  */
8207
8218
 
8219
+
8208
8220
  let pool = [];
8209
8221
  const pendingRequests = [];
8210
8222
 
@@ -8368,6 +8380,7 @@ function createCodecClass(constructor, constructorOptions, registerDataHandler)
8368
8380
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8369
8381
  */
8370
8382
 
8383
+
8371
8384
  const ERR_HTTP_STATUS = "HTTP error ";
8372
8385
  const ERR_HTTP_RANGE = "HTTP Range not supported";
8373
8386
  const ERR_ITERATOR_COMPLETED_TOO_SOON = "Writer iterator completed too soon";
@@ -9128,6 +9141,7 @@ function decodeCP437(stringValue) {
9128
9141
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9129
9142
  */
9130
9143
 
9144
+
9131
9145
  function decodeText(value, encoding) {
9132
9146
  if (encoding && encoding.trim().toLowerCase() == "cp437") {
9133
9147
  return decodeCP437(value);
@@ -9229,6 +9243,7 @@ class Entry {
9229
9243
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9230
9244
  */
9231
9245
 
9246
+
9232
9247
  const ERR_BAD_FORMAT = "File format is not recognized";
9233
9248
  const ERR_EOCDR_NOT_FOUND = "End of central directory not found";
9234
9249
  const ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND = "End of Zip64 central directory locator not found";
@@ -9407,10 +9422,16 @@ class ZipReader {
9407
9422
  rawExtraField: directoryArray.subarray(extraFieldOffset, commentOffset)
9408
9423
  });
9409
9424
  const decode = getOptionValue$1(zipReader, options, "decodeText") || decodeText;
9410
- const [filename, comment] = await Promise.all([
9411
- decode(rawFilename, filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437),
9412
- decode(rawComment, commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437)
9413
- ]);
9425
+ const rawFilenameEncoding = filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437;
9426
+ const rawCommentEncoding = commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437;
9427
+ let filename = decode(rawFilename, rawFilenameEncoding);
9428
+ if (filename === UNDEFINED_VALUE) {
9429
+ filename = decodeText(rawFilename, rawFilenameEncoding);
9430
+ }
9431
+ let comment = decode(rawComment, rawCommentEncoding);
9432
+ if (comment === UNDEFINED_VALUE) {
9433
+ comment = decodeText(rawComment, rawCommentEncoding);
9434
+ }
9414
9435
  Object.assign(fileEntry, {
9415
9436
  rawComment,
9416
9437
  filename,
@@ -9484,7 +9505,7 @@ class ZipReaderStream {
9484
9505
  }
9485
9506
  }
9486
9507
 
9487
- class ZipEntry$1 {
9508
+ let ZipEntry$1 = class ZipEntry {
9488
9509
 
9489
9510
  constructor(reader, config, options) {
9490
9511
  Object.assign(this, {
@@ -9598,7 +9619,7 @@ class ZipEntry$1 {
9598
9619
  }
9599
9620
  return checkPasswordOnly ? UNDEFINED_VALUE : writer.getData ? writer.getData() : writable;
9600
9621
  }
9601
- }
9622
+ };
9602
9623
 
9603
9624
  function readCommonHeader(directory, dataView, offset) {
9604
9625
  const rawBitFlag = directory.rawBitFlag = getUint16(dataView, offset + 2);
@@ -9892,6 +9913,7 @@ function getDataView$1(array) {
9892
9913
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9893
9914
  */
9894
9915
 
9916
+
9895
9917
  const ERR_DUPLICATED_NAME = "File already exists";
9896
9918
  const ERR_INVALID_COMMENT = "Zip file comment exceeds 64KB";
9897
9919
  const ERR_INVALID_ENTRY_COMMENT = "File entry comment exceeds 64KB";
@@ -10014,12 +10036,18 @@ async function addFile(zipWriter, name, reader, options) {
10014
10036
  options.directory = name.endsWith(DIRECTORY_SIGNATURE);
10015
10037
  }
10016
10038
  const encode = getOptionValue(zipWriter, options, "encodeText", encodeText);
10017
- const rawFilename = encode(name);
10039
+ let rawFilename = encode(name);
10040
+ if (rawFilename === UNDEFINED_VALUE) {
10041
+ rawFilename = encodeText(name);
10042
+ }
10018
10043
  if (getLength(rawFilename) > MAX_16_BITS) {
10019
10044
  throw new Error(ERR_INVALID_ENTRY_NAME);
10020
10045
  }
10021
10046
  const comment = options.comment || "";
10022
- const rawComment = encode(comment);
10047
+ let rawComment = encode(comment);
10048
+ if (rawComment === UNDEFINED_VALUE) {
10049
+ rawComment = encodeText(comment);
10050
+ }
10023
10051
  if (getLength(rawComment) > MAX_16_BITS) {
10024
10052
  throw new Error(ERR_INVALID_ENTRY_COMMENT);
10025
10053
  }
@@ -10085,7 +10113,7 @@ async function addFile(zipWriter, name, reader, options) {
10085
10113
  dataDescriptor = true;
10086
10114
  if (zip64 || zip64 === UNDEFINED_VALUE) {
10087
10115
  zip64 = true;
10088
- uncompressedSize = maximumCompressedSize = MAX_32_BITS;
10116
+ uncompressedSize = maximumCompressedSize = MAX_32_BITS + 1;
10089
10117
  }
10090
10118
  } else {
10091
10119
  uncompressedSize = reader.size;
@@ -10093,11 +10121,11 @@ async function addFile(zipWriter, name, reader, options) {
10093
10121
  }
10094
10122
  }
10095
10123
  const { diskOffset, diskNumber, maxSize } = zipWriter.writer;
10096
- const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
10097
- const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
10098
- const zip64Offset = zip64Enabled || zipWriter.offset + zipWriter.pendingEntriesSize - diskOffset >= MAX_32_BITS;
10124
+ const zip64UncompressedSize = zip64Enabled || uncompressedSize > MAX_32_BITS;
10125
+ const zip64CompressedSize = zip64Enabled || maximumCompressedSize > MAX_32_BITS;
10126
+ const zip64Offset = zip64Enabled || zipWriter.offset + zipWriter.pendingEntriesSize - diskOffset > MAX_32_BITS;
10099
10127
  const supportZip64SplitFile = getOptionValue(zipWriter, options, "supportZip64SplitFile", true);
10100
- const zip64DiskNumberStart = (supportZip64SplitFile && zip64Enabled) || diskNumber + Math.ceil(zipWriter.pendingEntriesSize / maxSize) >= MAX_16_BITS;
10128
+ const zip64DiskNumberStart = (supportZip64SplitFile && zip64Enabled) || diskNumber + Math.ceil(zipWriter.pendingEntriesSize / maxSize) > MAX_16_BITS;
10101
10129
  if (zip64Offset || zip64UncompressedSize || zip64CompressedSize || zip64DiskNumberStart) {
10102
10130
  if (zip64 === false || !keepOrder) {
10103
10131
  throw new Error(ERR_UNSUPPORTED_FORMAT);
@@ -10240,7 +10268,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
10240
10268
  fileEntry.offset = zipWriter.offset - diskOffset;
10241
10269
  if (fileEntry.zip64) {
10242
10270
  setZip64ExtraInfo(fileEntry, options);
10243
- } else if (fileEntry.offset >= MAX_32_BITS) {
10271
+ } else if (fileEntry.offset > MAX_32_BITS) {
10244
10272
  throw new Error(ERR_UNSUPPORTED_FORMAT);
10245
10273
  }
10246
10274
  zipWriter.offset += fileEntry.size;
@@ -10857,7 +10885,7 @@ async function closeFile(zipWriter, comment, options) {
10857
10885
  lastDiskNumber++;
10858
10886
  }
10859
10887
  let zip64 = getOptionValue(zipWriter, options, "zip64");
10860
- if (directoryOffset >= MAX_32_BITS || directoryDataLength >= MAX_32_BITS || filesLength >= MAX_16_BITS || lastDiskNumber >= MAX_16_BITS) {
10888
+ if (directoryOffset > MAX_32_BITS || directoryDataLength > MAX_32_BITS || filesLength > MAX_16_BITS || lastDiskNumber > MAX_16_BITS) {
10861
10889
  if (zip64 === false) {
10862
10890
  throw new Error(ERR_UNSUPPORTED_FORMAT);
10863
10891
  } else {
@@ -10996,6 +11024,7 @@ function getLength(...arrayLikes) {
10996
11024
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10997
11025
  */
10998
11026
 
11027
+
10999
11028
  class ZipEntry {
11000
11029
 
11001
11030
  constructor(fs, name, params, parent) {
@@ -11833,9 +11862,10 @@ function addChild(parent, name, params, directory) {
11833
11862
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11834
11863
  */
11835
11864
 
11865
+
11836
11866
  let baseURL;
11837
11867
  try {
11838
- baseURL = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href));
11868
+ baseURL = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href));
11839
11869
  } catch (_error) {
11840
11870
  // ignored
11841
11871
  }
@@ -11844,6 +11874,7 @@ e(configure);
11844
11874
 
11845
11875
  /// <reference types="./index.d.ts" />
11846
11876
 
11877
+
11847
11878
  configure({ Deflate: ZipDeflate, Inflate: ZipInflate });
11848
11879
 
11849
11880
  exports.BlobReader = BlobReader;
package/index.d.ts CHANGED
@@ -629,15 +629,21 @@ export class Uint8ArrayWriter extends Writer<Uint8Array> { }
629
629
  * Represents an instance used to create an unzipped stream.
630
630
  *
631
631
  * @example
632
- * This example will take a zip file, decompress it and then recompress each file in it, saving it to disk.
632
+ * This example will take a zip file, decompress it and then save its files and directories to disk.
633
633
  * ```
634
- * for await (const entry of (await fetch(urlToZippedFile)).body.pipeThrough(new ZipWriterStream()))
635
- * if (entry.readable) {
636
- * console.log(entry.filename)
637
- * entry.readable
638
- * .pipeThrough(ZipReaderStream().transform(entry.filename))
639
- * .pipeTo((await Deno.create(entry.filename + '.zip')).writable)
634
+ * import {resolve} from "https://deno.land/std/path/mod.ts";
635
+ * import {ensureDir, ensureFile} from "https://deno.land/std/fs/mod.ts";
636
+ *
637
+ * for await (const entry of (await fetch(urlToZippedFile)).body.pipeThrough(new ZipReaderStream())) {
638
+ * const fullPath = resolve(destination, entry.filename);
639
+ * if (entry.directory) {
640
+ * await ensureDir(fullPath);
641
+ * continue;
640
642
  * }
643
+ *
644
+ * await ensureFile(fullPath);
645
+ * await entry.readable?.pipeTo((await Deno.create(fullPath)).writable);
646
+ * }
641
647
  * ```
642
648
  */
643
649
  export class ZipReaderStream<T> {
@@ -786,9 +792,9 @@ export interface GetEntriesOptions {
786
792
  *
787
793
  * @param value The raw text value.
788
794
  * @param encoding The encoding of the text.
789
- * @returns The decoded text value.
795
+ * @returns The decoded text value or `undefined` if the raw text value should be decoded by zip.js.
790
796
  */
791
- decodeText?(value: Uint8Array, encoding: string): string
797
+ decodeText?(value: Uint8Array, encoding: string): string | undefined;
792
798
  }
793
799
 
794
800
  /**
@@ -1235,7 +1241,7 @@ export interface ZipWriterConstructorOptions {
1235
1241
  /**
1236
1242
  * `true` to keep the order of the entry physically in the zip file.
1237
1243
  *
1238
- * When set to `true`, the use of web workers will be improved. However, it also prevents files larger than 4GB from being created without setting the `zip54` option to `true` explicitly.
1244
+ * When set to `true`, the use of web workers will be improved. However, it also prevents files larger than 4GB from being created without setting the `zip64` option to `true` explicitly.
1239
1245
  * Another solution to improve the use of web workers is to add entries from smallest to largest in uncompressed size.
1240
1246
  *
1241
1247
  * @defaultValue true
@@ -1355,9 +1361,9 @@ export interface ZipWriterConstructorOptions {
1355
1361
  * Encode the filename and the comment of the entry.
1356
1362
  *
1357
1363
  * @param text The text to encode.
1358
- * @returns The encoded text.
1364
+ * @returns The encoded text or `undefined` if the text should be encoded by zip.js.
1359
1365
  */
1360
- encodeText?(text: string): Uint8Array
1366
+ encodeText?(text: string): Uint8Array | undefined;
1361
1367
  }
1362
1368
 
1363
1369
  /**