@loaders.gl/zip 4.2.1 → 4.3.0-alpha.2

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/dist/dist.dev.js CHANGED
@@ -7720,7 +7720,65 @@ var __exports__ = (() => {
7720
7720
  }
7721
7721
  };
7722
7722
 
7723
+ // ../loader-utils/src/lib/file-provider/data-view-file.ts
7724
+ var toNumber = (bigint) => {
7725
+ if (bigint > Number.MAX_SAFE_INTEGER) {
7726
+ throw new Error("Offset is out of bounds");
7727
+ }
7728
+ return Number(bigint);
7729
+ };
7730
+ var DataViewFile = class {
7731
+ /** The DataView from which data is provided */
7732
+ file;
7733
+ constructor(file) {
7734
+ this.file = file;
7735
+ }
7736
+ async destroy() {
7737
+ }
7738
+ /**
7739
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
7740
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
7741
+ */
7742
+ async getUint8(offset) {
7743
+ return this.file.getUint8(toNumber(offset));
7744
+ }
7745
+ /**
7746
+ * Gets an unsigned 16-bit intege at the specified byte offset from the start of the file.
7747
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
7748
+ */
7749
+ async getUint16(offset) {
7750
+ return this.file.getUint16(toNumber(offset), true);
7751
+ }
7752
+ /**
7753
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
7754
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
7755
+ */
7756
+ async getUint32(offset) {
7757
+ return this.file.getUint32(toNumber(offset), true);
7758
+ }
7759
+ /**
7760
+ * Gets an unsigned 64-bit integer at the specified byte offset from the start of the file.
7761
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
7762
+ */
7763
+ async getBigUint64(offset) {
7764
+ return this.file.getBigUint64(toNumber(offset), true);
7765
+ }
7766
+ /**
7767
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
7768
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
7769
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
7770
+ */
7771
+ async slice(startOffset, endOffset) {
7772
+ return this.file.buffer.slice(toNumber(startOffset), toNumber(endOffset));
7773
+ }
7774
+ /** the length (in bytes) of the data. */
7775
+ get length() {
7776
+ return BigInt(this.file.byteLength);
7777
+ }
7778
+ };
7779
+
7723
7780
  // src/parse-zip/search-from-the-end.ts
7781
+ var buffLength = 1024;
7724
7782
  var searchFromTheEnd = async (file, target) => {
7725
7783
  const searchWindow = [
7726
7784
  await file.getUint8(file.length - 1n),
@@ -7728,18 +7786,25 @@ var __exports__ = (() => {
7728
7786
  await file.getUint8(file.length - 3n),
7729
7787
  void 0
7730
7788
  ];
7731
- let targetOffset = 0n;
7732
- for (let i = file.length - 4n; i > -1; i--) {
7733
- searchWindow[3] = searchWindow[2];
7734
- searchWindow[2] = searchWindow[1];
7735
- searchWindow[1] = searchWindow[0];
7736
- searchWindow[0] = await file.getUint8(i);
7737
- if (searchWindow.every((val, index) => val === target[index])) {
7738
- targetOffset = i;
7739
- break;
7789
+ let targetOffset = -1;
7790
+ let point = file.length - 4n;
7791
+ do {
7792
+ const prevPoint = point;
7793
+ point -= BigInt(buffLength);
7794
+ point = point >= 0n ? point : 0n;
7795
+ const buff = new Uint8Array(await file.slice(point, prevPoint));
7796
+ for (let i = buff.length - 1; i > -1; i--) {
7797
+ searchWindow[3] = searchWindow[2];
7798
+ searchWindow[2] = searchWindow[1];
7799
+ searchWindow[1] = searchWindow[0];
7800
+ searchWindow[0] = buff[i];
7801
+ if (searchWindow.every((val, index) => val === target[index])) {
7802
+ targetOffset = i;
7803
+ break;
7804
+ }
7740
7805
  }
7741
- }
7742
- return targetOffset;
7806
+ } while (targetOffset === -1 && point > 0n);
7807
+ return point + BigInt(targetOffset);
7743
7808
  };
7744
7809
 
7745
7810
  // src/parse-zip/zip64-info-generation.ts
@@ -7820,6 +7885,7 @@ var __exports__ = (() => {
7820
7885
  var parseEoCDRecord = async (file) => {
7821
7886
  const zipEoCDOffset = await searchFromTheEnd(file, eoCDSignature);
7822
7887
  let cdRecordsNumber = BigInt(await file.getUint16(zipEoCDOffset + CD_RECORDS_NUMBER_OFFSET));
7888
+ let cdByteSize = BigInt(await file.getUint32(zipEoCDOffset + CD_CD_BYTE_SIZE_OFFSET));
7823
7889
  let cdStartOffset = BigInt(await file.getUint32(zipEoCDOffset + CD_START_OFFSET_OFFSET));
7824
7890
  let zip64EoCDLocatorOffset = zipEoCDOffset - 20n;
7825
7891
  let zip64EoCDOffset = 0n;
@@ -7833,6 +7899,7 @@ var __exports__ = (() => {
7833
7899
  throw new Error("zip64 EoCD not found");
7834
7900
  }
7835
7901
  cdRecordsNumber = await file.getBigUint64(zip64EoCDOffset + ZIP64_CD_RECORDS_NUMBER_OFFSET);
7902
+ cdByteSize = await file.getBigUint64(zip64EoCDOffset + ZIP64_CD_CD_BYTE_SIZE_OFFSET);
7836
7903
  cdStartOffset = await file.getBigUint64(zip64EoCDOffset + ZIP64_CD_START_OFFSET_OFFSET);
7837
7904
  } else {
7838
7905
  zip64EoCDLocatorOffset = 0n;
@@ -7840,6 +7907,7 @@ var __exports__ = (() => {
7840
7907
  return {
7841
7908
  cdRecordsNumber,
7842
7909
  cdStartOffset,
7910
+ cdByteSize,
7843
7911
  offsets: {
7844
7912
  zip64EoCDOffset,
7845
7913
  zip64EoCDLocatorOffset,
@@ -8058,34 +8126,41 @@ var __exports__ = (() => {
8058
8126
  ];
8059
8127
 
8060
8128
  // src/parse-zip/cd-file-header.ts
8061
- var CD_COMPRESSED_SIZE_OFFSET = 20n;
8062
- var CD_UNCOMPRESSED_SIZE_OFFSET = 24n;
8063
- var CD_FILE_NAME_LENGTH_OFFSET = 28n;
8064
- var CD_EXTRA_FIELD_LENGTH_OFFSET = 30n;
8065
- var CD_START_DISK_OFFSET = 32n;
8066
- var CD_LOCAL_HEADER_OFFSET_OFFSET = 42n;
8129
+ var CD_COMPRESSED_SIZE_OFFSET = 20;
8130
+ var CD_UNCOMPRESSED_SIZE_OFFSET = 24;
8131
+ var CD_FILE_NAME_LENGTH_OFFSET = 28;
8132
+ var CD_EXTRA_FIELD_LENGTH_OFFSET = 30;
8133
+ var CD_START_DISK_OFFSET = 32;
8134
+ var CD_LOCAL_HEADER_OFFSET_OFFSET = 42;
8067
8135
  var CD_FILE_NAME_OFFSET = 46n;
8068
8136
  var signature2 = new Uint8Array([80, 75, 1, 2]);
8069
8137
  var parseZipCDFileHeader = async (headerOffset, file) => {
8070
- const magicBytes = await file.slice(headerOffset, headerOffset + 4n);
8138
+ if (headerOffset >= file.length) {
8139
+ return null;
8140
+ }
8141
+ const mainHeader = new DataView(
8142
+ await file.slice(headerOffset, headerOffset + CD_FILE_NAME_OFFSET)
8143
+ );
8144
+ const magicBytes = mainHeader.buffer.slice(0, 4);
8071
8145
  if (!compareArrayBuffers(magicBytes, signature2.buffer)) {
8072
8146
  return null;
8073
8147
  }
8074
- const compressedSize = BigInt(await file.getUint32(headerOffset + CD_COMPRESSED_SIZE_OFFSET));
8075
- const uncompressedSize = BigInt(await file.getUint32(headerOffset + CD_UNCOMPRESSED_SIZE_OFFSET));
8076
- const extraFieldLength = await file.getUint16(headerOffset + CD_EXTRA_FIELD_LENGTH_OFFSET);
8077
- const startDisk = BigInt(await file.getUint16(headerOffset + CD_START_DISK_OFFSET));
8078
- const fileNameLength = await file.getUint16(headerOffset + CD_FILE_NAME_LENGTH_OFFSET);
8079
- const filenameBytes = await file.slice(
8148
+ const compressedSize = BigInt(mainHeader.getUint32(CD_COMPRESSED_SIZE_OFFSET, true));
8149
+ const uncompressedSize = BigInt(mainHeader.getUint32(CD_UNCOMPRESSED_SIZE_OFFSET, true));
8150
+ const extraFieldLength = mainHeader.getUint16(CD_EXTRA_FIELD_LENGTH_OFFSET, true);
8151
+ const startDisk = BigInt(mainHeader.getUint16(CD_START_DISK_OFFSET, true));
8152
+ const fileNameLength = mainHeader.getUint16(CD_FILE_NAME_LENGTH_OFFSET, true);
8153
+ const additionalHeader = await file.slice(
8080
8154
  headerOffset + CD_FILE_NAME_OFFSET,
8081
- headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength)
8155
+ headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength)
8082
8156
  );
8157
+ const filenameBytes = additionalHeader.slice(0, fileNameLength);
8083
8158
  const fileName = new TextDecoder().decode(filenameBytes);
8084
8159
  const extraOffset = headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength);
8085
- const oldFormatOffset = await file.getUint32(headerOffset + CD_LOCAL_HEADER_OFFSET_OFFSET);
8160
+ const oldFormatOffset = mainHeader.getUint32(CD_LOCAL_HEADER_OFFSET_OFFSET, true);
8086
8161
  const localHeaderOffset = BigInt(oldFormatOffset);
8087
8162
  const extraField = new DataView(
8088
- await file.slice(extraOffset, extraOffset + BigInt(extraFieldLength))
8163
+ additionalHeader.slice(fileNameLength, additionalHeader.byteLength)
8089
8164
  );
8090
8165
  const zip64data = {
8091
8166
  uncompressedSize,
@@ -8104,13 +8179,16 @@ var __exports__ = (() => {
8104
8179
  };
8105
8180
  };
8106
8181
  async function* makeZipCDHeaderIterator(fileProvider) {
8107
- const { cdStartOffset } = await parseEoCDRecord(fileProvider);
8108
- let cdHeader = await parseZipCDFileHeader(cdStartOffset, fileProvider);
8182
+ const { cdStartOffset, cdByteSize } = await parseEoCDRecord(fileProvider);
8183
+ const centralDirectory = new DataViewFile(
8184
+ new DataView(await fileProvider.slice(cdStartOffset, cdStartOffset + cdByteSize))
8185
+ );
8186
+ let cdHeader = await parseZipCDFileHeader(0n, centralDirectory);
8109
8187
  while (cdHeader) {
8110
8188
  yield cdHeader;
8111
8189
  cdHeader = await parseZipCDFileHeader(
8112
8190
  cdHeader.extraOffset + BigInt(cdHeader.extraFieldLength),
8113
- fileProvider
8191
+ centralDirectory
8114
8192
  );
8115
8193
  }
8116
8194
  }
@@ -8290,42 +8368,45 @@ var __exports__ = (() => {
8290
8368
  ];
8291
8369
 
8292
8370
  // src/parse-zip/local-file-header.ts
8293
- var COMPRESSION_METHOD_OFFSET = 8n;
8294
- var COMPRESSED_SIZE_OFFSET = 18n;
8295
- var UNCOMPRESSED_SIZE_OFFSET = 22n;
8296
- var FILE_NAME_LENGTH_OFFSET = 26n;
8297
- var EXTRA_FIELD_LENGTH_OFFSET = 28n;
8371
+ var COMPRESSION_METHOD_OFFSET = 8;
8372
+ var COMPRESSED_SIZE_OFFSET = 18;
8373
+ var UNCOMPRESSED_SIZE_OFFSET = 22;
8374
+ var FILE_NAME_LENGTH_OFFSET = 26;
8375
+ var EXTRA_FIELD_LENGTH_OFFSET = 28;
8298
8376
  var FILE_NAME_OFFSET = 30n;
8299
8377
  var signature3 = new Uint8Array([80, 75, 3, 4]);
8300
- var parseZipLocalFileHeader = async (headerOffset, buffer) => {
8301
- const magicBytes = await buffer.slice(headerOffset, headerOffset + 4n);
8378
+ var parseZipLocalFileHeader = async (headerOffset, file) => {
8379
+ const mainHeader = new DataView(await file.slice(headerOffset, headerOffset + FILE_NAME_OFFSET));
8380
+ const magicBytes = mainHeader.buffer.slice(0, 4);
8302
8381
  if (!compareArrayBuffers(magicBytes, signature3)) {
8303
8382
  return null;
8304
8383
  }
8305
- const fileNameLength = await buffer.getUint16(headerOffset + FILE_NAME_LENGTH_OFFSET);
8306
- const fileName = new TextDecoder().decode(
8307
- await buffer.slice(
8308
- headerOffset + FILE_NAME_OFFSET,
8309
- headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength)
8310
- )
8311
- ).split("\\").join("/");
8312
- const extraFieldLength = await buffer.getUint16(headerOffset + EXTRA_FIELD_LENGTH_OFFSET);
8384
+ const fileNameLength = mainHeader.getUint16(FILE_NAME_LENGTH_OFFSET, true);
8385
+ const extraFieldLength = mainHeader.getUint16(EXTRA_FIELD_LENGTH_OFFSET, true);
8386
+ const additionalHeader = await file.slice(
8387
+ headerOffset + FILE_NAME_OFFSET,
8388
+ headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength)
8389
+ );
8390
+ const fileNameBuffer = additionalHeader.slice(0, fileNameLength);
8391
+ const extraDataBuffer = new DataView(
8392
+ additionalHeader.slice(fileNameLength, additionalHeader.byteLength)
8393
+ );
8394
+ const fileName = new TextDecoder().decode(fileNameBuffer).split("\\").join("/");
8313
8395
  let fileDataOffset = headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength);
8314
- const compressionMethod = await buffer.getUint16(headerOffset + COMPRESSION_METHOD_OFFSET);
8315
- let compressedSize = BigInt(await buffer.getUint32(headerOffset + COMPRESSED_SIZE_OFFSET));
8316
- let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + UNCOMPRESSED_SIZE_OFFSET));
8317
- const extraOffset = headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength);
8318
- let offsetInZip64Data = 4n;
8396
+ const compressionMethod = mainHeader.getUint16(COMPRESSION_METHOD_OFFSET, true);
8397
+ let compressedSize = BigInt(mainHeader.getUint32(COMPRESSED_SIZE_OFFSET, true));
8398
+ let uncompressedSize = BigInt(mainHeader.getUint32(UNCOMPRESSED_SIZE_OFFSET, true));
8399
+ let offsetInZip64Data = 4;
8319
8400
  if (uncompressedSize === BigInt(4294967295)) {
8320
- uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
8321
- offsetInZip64Data += 8n;
8401
+ uncompressedSize = extraDataBuffer.getBigUint64(offsetInZip64Data, true);
8402
+ offsetInZip64Data += 8;
8322
8403
  }
8323
8404
  if (compressedSize === BigInt(4294967295)) {
8324
- compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
8325
- offsetInZip64Data += 8n;
8405
+ compressedSize = extraDataBuffer.getBigUint64(offsetInZip64Data, true);
8406
+ offsetInZip64Data += 8;
8326
8407
  }
8327
8408
  if (fileDataOffset === BigInt(4294967295)) {
8328
- fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
8409
+ fileDataOffset = extraDataBuffer.getBigUint64(offsetInZip64Data, true);
8329
8410
  }
8330
8411
  return {
8331
8412
  fileNameLength,