@zip.js/zip.js 2.6.20 → 2.6.21

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.
@@ -8774,7 +8774,7 @@
8774
8774
  files: new Map(),
8775
8775
  filenames: new Set(),
8776
8776
  offset: writer.writable.size,
8777
- pendingCompressedSize: 0,
8777
+ pendingEntriesSize: 0,
8778
8778
  pendingAddFileCalls: new Set()
8779
8779
  });
8780
8780
  }
@@ -8886,6 +8886,7 @@
8886
8886
  extendedTimestamp = true;
8887
8887
  }
8888
8888
  let maximumCompressedSize = 0;
8889
+ let maximumEntrySize = 0;
8889
8890
  let keepOrder = getOptionValue(zipWriter, options, "keepOrder");
8890
8891
  if (keepOrder === undefined) {
8891
8892
  keepOrder = true;
@@ -8907,7 +8908,7 @@
8907
8908
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
8908
8909
  }
8909
8910
  let zip64 = options.zip64 || zipWriter.options.zip64 || false;
8910
- if (zipWriter.offset + zipWriter.pendingCompressedSize >= MAX_32_BITS ||
8911
+ if (zipWriter.offset + zipWriter.pendingEntriesSize >= MAX_32_BITS ||
8911
8912
  uncompressedSize >= MAX_32_BITS ||
8912
8913
  maximumCompressedSize >= MAX_32_BITS) {
8913
8914
  if (options.zip64 === false || zipWriter.options.zip64 === false || !keepOrder) {
@@ -8916,8 +8917,6 @@
8916
8917
  zip64 = true;
8917
8918
  }
8918
8919
  }
8919
- zipWriter.pendingCompressedSize += maximumCompressedSize;
8920
- await Promise.resolve();
8921
8920
  const level = getOptionValue(zipWriter, options, "level");
8922
8921
  const useWebWorkers = getOptionValue(zipWriter, options, "useWebWorkers");
8923
8922
  const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
@@ -8931,7 +8930,7 @@
8931
8930
  if (dataDescriptor && dataDescriptorSignature === undefined) {
8932
8931
  dataDescriptorSignature = false;
8933
8932
  }
8934
- const fileEntry = await getFileEntry(zipWriter, name, reader, Object.assign({}, options, {
8933
+ options = Object.assign({}, options, {
8935
8934
  rawFilename,
8936
8935
  rawComment,
8937
8936
  version,
@@ -8956,15 +8955,20 @@
8956
8955
  internalFileAttribute,
8957
8956
  externalFileAttribute,
8958
8957
  useCompressionStream
8959
- }));
8960
- if (maximumCompressedSize) {
8961
- zipWriter.pendingCompressedSize -= maximumCompressedSize;
8958
+ });
8959
+ const localHeaderInfo = getLocalHeaderInfo(options);
8960
+ const dataDescriptorInfo = getDataDescriptorInfo(options);
8961
+ maximumEntrySize = localHeaderInfo.localHeaderArray.length + maximumCompressedSize + dataDescriptorInfo.dataDescriptorArray.length;
8962
+ zipWriter.pendingEntriesSize += maximumEntrySize;
8963
+ const fileEntry = await getFileEntry(zipWriter, name, reader, { localHeaderInfo, dataDescriptorInfo }, options);
8964
+ if (maximumEntrySize) {
8965
+ zipWriter.pendingEntriesSize -= maximumEntrySize;
8962
8966
  }
8963
8967
  Object.assign(fileEntry, { name, comment, extraField });
8964
8968
  return new Entry(fileEntry);
8965
8969
  }
8966
8970
 
8967
- async function getFileEntry(zipWriter, name, reader, options) {
8971
+ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
8968
8972
  const {
8969
8973
  files,
8970
8974
  writer
@@ -9003,7 +9007,8 @@
9003
9007
  if (!bufferedWrite) {
9004
9008
  await lockPreviousFileEntry;
9005
9009
  }
9006
- fileEntry = await createFileEntry(reader, fileWriter, fileEntry, zipWriter.config, options);
9010
+ fileEntry.writingData = true;
9011
+ fileEntry = await createFileEntry(reader, fileWriter, fileEntry, entryInfo, zipWriter.config, options);
9007
9012
  files.set(name, fileEntry);
9008
9013
  fileEntry.filename = name;
9009
9014
  if (bufferedWrite) {
@@ -9079,7 +9084,25 @@
9079
9084
  }
9080
9085
  }
9081
9086
 
9082
- async function createFileEntry(reader, writer, pendingFileEntry, config, options) {
9087
+ async function createFileEntry(reader, writer, pendingFileEntry, entryInfo, config, options) {
9088
+ const {
9089
+ localHeaderInfo,
9090
+ dataDescriptorInfo
9091
+ } = entryInfo;
9092
+ const {
9093
+ localHeaderArray,
9094
+ headerArray,
9095
+ lastModDate,
9096
+ rawLastModDate,
9097
+ encrypted,
9098
+ compressed,
9099
+ version,
9100
+ compressionMethod,
9101
+ rawExtraFieldExtendedTimestamp,
9102
+ rawExtraFieldNTFS,
9103
+ rawExtraFieldAES
9104
+ } = localHeaderInfo;
9105
+ const { dataDescriptorArray } = dataDescriptorInfo;
9083
9106
  const {
9084
9107
  rawFilename,
9085
9108
  lastAccessDate,
@@ -9089,9 +9112,7 @@
9089
9112
  zip64,
9090
9113
  zipCrypto,
9091
9114
  dataDescriptor,
9092
- dataDescriptorSignature,
9093
9115
  directory,
9094
- version,
9095
9116
  versionMadeBy,
9096
9117
  rawComment,
9097
9118
  rawExtraField,
@@ -9107,8 +9128,108 @@
9107
9128
  externalFileAttribute,
9108
9129
  useCompressionStream
9109
9130
  } = options;
9110
- const encrypted = Boolean(password && password.length);
9131
+ const fileEntry = {
9132
+ lock: pendingFileEntry.lock,
9133
+ versionMadeBy,
9134
+ zip64,
9135
+ directory: Boolean(directory),
9136
+ filenameUTF8: true,
9137
+ rawFilename,
9138
+ commentUTF8: true,
9139
+ rawComment,
9140
+ rawExtraFieldExtendedTimestamp,
9141
+ rawExtraFieldNTFS,
9142
+ rawExtraFieldAES,
9143
+ rawExtraField,
9144
+ extendedTimestamp,
9145
+ msDosCompatible,
9146
+ internalFileAttribute,
9147
+ externalFileAttribute
9148
+ };
9149
+ let uncompressedSize = 0;
9150
+ let signature;
9151
+ let compressedSize = 0;
9152
+ const { writable } = writer;
9153
+ if (reader) {
9154
+ reader.chunkSize = getChunkSize(config);
9155
+ await writeData(writable, localHeaderArray);
9156
+ const size = () => reader.size;
9157
+ const readable = reader.readable;
9158
+ readable.size = size;
9159
+ const workerOptions = {
9160
+ options: {
9161
+ codecType: CODEC_DEFLATE,
9162
+ level,
9163
+ password,
9164
+ encryptionStrength,
9165
+ zipCrypto: encrypted && zipCrypto,
9166
+ passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
9167
+ signed: true,
9168
+ compressed,
9169
+ encrypted,
9170
+ useWebWorkers,
9171
+ useCompressionStream
9172
+ },
9173
+ config,
9174
+ streamOptions: { signal, size, onstart, onprogress, onend },
9175
+ codecConstructor: config.Deflate
9176
+ };
9177
+ const previousSize = writable.size;
9178
+ signature = (await runWorker({ readable, writable }, workerOptions)).signature;
9179
+ uncompressedSize = reader.size = readable.size();
9180
+ compressedSize = writable.size - previousSize;
9181
+ } else {
9182
+ await writeData(writable, localHeaderArray);
9183
+ }
9184
+ const rawExtraFieldZip64 = zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array();
9185
+ if (reader) {
9186
+ setEntryInfo({
9187
+ signature,
9188
+ rawExtraFieldZip64,
9189
+ compressedSize,
9190
+ uncompressedSize,
9191
+ localHeaderInfo,
9192
+ dataDescriptorInfo
9193
+ }, options);
9194
+ }
9195
+ if (dataDescriptor) {
9196
+ await writeData(writable, dataDescriptorArray);
9197
+ }
9198
+ Object.assign(fileEntry, {
9199
+ compressedSize,
9200
+ lastModDate,
9201
+ rawLastModDate,
9202
+ creationDate,
9203
+ lastAccessDate,
9204
+ encrypted,
9205
+ length: localHeaderArray.length + compressedSize + dataDescriptorArray.length,
9206
+ compressionMethod,
9207
+ version,
9208
+ headerArray,
9209
+ signature,
9210
+ rawExtraFieldZip64
9211
+ });
9212
+ return fileEntry;
9213
+ }
9214
+
9215
+ function getLocalHeaderInfo(options) {
9216
+ const {
9217
+ rawFilename,
9218
+ lastAccessDate,
9219
+ creationDate,
9220
+ password,
9221
+ level,
9222
+ zip64,
9223
+ zipCrypto,
9224
+ dataDescriptor,
9225
+ directory,
9226
+ rawExtraField,
9227
+ encryptionStrength,
9228
+ extendedTimestamp,
9229
+ } = options;
9230
+ let version = options.version || VERSION_DEFLATE;
9111
9231
  const compressed = level !== 0 && !directory;
9232
+ const encrypted = Boolean(password && password.length);
9112
9233
  let rawExtraFieldAES;
9113
9234
  if (encrypted && !zipCrypto) {
9114
9235
  rawExtraFieldAES = new Uint8Array(EXTRAFIELD_DATA_AES.length + 2);
@@ -9152,27 +9273,6 @@
9152
9273
  } else {
9153
9274
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
9154
9275
  }
9155
- const fileEntry = {
9156
- lock: pendingFileEntry.lock,
9157
- version: version || VERSION_DEFLATE,
9158
- versionMadeBy,
9159
- zip64,
9160
- directory: Boolean(directory),
9161
- filenameUTF8: true,
9162
- rawFilename,
9163
- commentUTF8: true,
9164
- rawComment,
9165
- rawExtraFieldZip64: zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array(),
9166
- rawExtraFieldExtendedTimestamp,
9167
- rawExtraFieldNTFS,
9168
- rawExtraFieldAES,
9169
- rawExtraField,
9170
- extendedTimestamp,
9171
- msDosCompatible,
9172
- internalFileAttribute,
9173
- externalFileAttribute
9174
- };
9175
- let uncompressedSize = 0;
9176
9276
  let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
9177
9277
  if (dataDescriptor) {
9178
9278
  bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
@@ -9182,22 +9282,21 @@
9182
9282
  compressionMethod = COMPRESSION_METHOD_DEFLATE;
9183
9283
  }
9184
9284
  if (zip64) {
9185
- fileEntry.version = fileEntry.version > VERSION_ZIP64 ? fileEntry.version : VERSION_ZIP64;
9285
+ version = version > VERSION_ZIP64 ? version : VERSION_ZIP64;
9186
9286
  }
9187
9287
  if (encrypted) {
9188
9288
  bitFlag = bitFlag | BITFLAG_ENCRYPTED;
9189
9289
  if (!zipCrypto) {
9190
- fileEntry.version = fileEntry.version > VERSION_AES ? fileEntry.version : VERSION_AES;
9290
+ version = version > VERSION_AES ? version : VERSION_AES;
9191
9291
  compressionMethod = COMPRESSION_METHOD_AES;
9192
9292
  if (compressed) {
9193
- fileEntry.rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
9293
+ rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
9194
9294
  }
9195
9295
  }
9196
9296
  }
9197
- fileEntry.compressionMethod = compressionMethod;
9198
- const headerArray = fileEntry.headerArray = new Uint8Array(26);
9297
+ const headerArray = new Uint8Array(26);
9199
9298
  const headerView = getDataView(headerArray);
9200
- setUint16(headerView, 0, fileEntry.version);
9299
+ setUint16(headerView, 0, version);
9201
9300
  setUint16(headerView, 2, bitFlag);
9202
9301
  setUint16(headerView, 4, compressionMethod);
9203
9302
  const dateArray = new Uint32Array(1);
@@ -9226,41 +9325,28 @@
9226
9325
  arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + rawFilename.length + rawExtraFieldAES.length);
9227
9326
  arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length);
9228
9327
  arraySet(localHeaderArray, rawExtraField, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length + rawExtraFieldNTFS.length);
9229
- let result;
9230
- let compressedSize = 0;
9231
- const { writable } = writer;
9232
- fileEntry.writingData = pendingFileEntry.writingData = true;
9233
- if (reader) {
9234
- reader.chunkSize = getChunkSize(config);
9235
- await writeData(writable, localHeaderArray);
9236
- const size = () => reader.size;
9237
- const readable = reader.readable;
9238
- readable.size = size;
9239
- const workerOptions = {
9240
- options: {
9241
- codecType: CODEC_DEFLATE,
9242
- level,
9243
- password,
9244
- encryptionStrength,
9245
- zipCrypto: encrypted && zipCrypto,
9246
- passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
9247
- signed: true,
9248
- compressed,
9249
- encrypted,
9250
- useWebWorkers,
9251
- useCompressionStream
9252
- },
9253
- config,
9254
- streamOptions: { signal, size, onstart, onprogress, onend },
9255
- codecConstructor: config.Deflate
9256
- };
9257
- const previousSize = writable.size;
9258
- result = await runWorker({ readable, writable }, workerOptions);
9259
- uncompressedSize = reader.size = readable.size();
9260
- compressedSize = writable.size - previousSize;
9261
- } else {
9262
- await writeData(writable, localHeaderArray);
9263
- }
9328
+ return {
9329
+ localHeaderArray,
9330
+ headerArray,
9331
+ headerView,
9332
+ lastModDate,
9333
+ rawLastModDate,
9334
+ encrypted,
9335
+ compressed,
9336
+ version,
9337
+ compressionMethod,
9338
+ rawExtraFieldExtendedTimestamp,
9339
+ rawExtraFieldNTFS,
9340
+ rawExtraFieldAES
9341
+ };
9342
+ }
9343
+
9344
+ function getDataDescriptorInfo(options) {
9345
+ const {
9346
+ zip64,
9347
+ dataDescriptor,
9348
+ dataDescriptorSignature
9349
+ } = options;
9264
9350
  let dataDescriptorArray = new Uint8Array();
9265
9351
  let dataDescriptorView, dataDescriptorOffset = 0;
9266
9352
  if (dataDescriptor) {
@@ -9271,51 +9357,61 @@
9271
9357
  setUint32(dataDescriptorView, 0, DATA_DESCRIPTOR_RECORD_SIGNATURE);
9272
9358
  }
9273
9359
  }
9274
- if (reader) {
9275
- const { signature } = result;
9276
- if ((!encrypted || zipCrypto) && signature !== undefined) {
9277
- setUint32(headerView, 10, signature);
9278
- fileEntry.signature = signature;
9279
- if (dataDescriptor) {
9280
- setUint32(dataDescriptorView, dataDescriptorOffset, signature);
9281
- }
9282
- }
9283
- if (zip64) {
9284
- const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
9285
- setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
9286
- setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
9287
- setUint32(headerView, 14, MAX_32_BITS);
9288
- setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
9289
- setUint32(headerView, 18, MAX_32_BITS);
9290
- setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
9291
- if (dataDescriptor) {
9292
- setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
9293
- setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
9294
- }
9295
- } else {
9296
- setUint32(headerView, 14, compressedSize);
9297
- setUint32(headerView, 18, uncompressedSize);
9298
- if (dataDescriptor) {
9299
- setUint32(dataDescriptorView, dataDescriptorOffset + 4, compressedSize);
9300
- setUint32(dataDescriptorView, dataDescriptorOffset + 8, uncompressedSize);
9301
- }
9360
+ return {
9361
+ dataDescriptorArray,
9362
+ dataDescriptorView,
9363
+ dataDescriptorOffset
9364
+ };
9365
+ }
9366
+
9367
+ function setEntryInfo(entryInfo, options) {
9368
+ const {
9369
+ signature,
9370
+ rawExtraFieldZip64,
9371
+ compressedSize,
9372
+ uncompressedSize,
9373
+ localHeaderInfo,
9374
+ dataDescriptorInfo
9375
+ } = entryInfo;
9376
+ const {
9377
+ headerView,
9378
+ encrypted
9379
+ } = localHeaderInfo;
9380
+ const {
9381
+ dataDescriptorView,
9382
+ dataDescriptorOffset
9383
+ } = dataDescriptorInfo;
9384
+ const {
9385
+ zip64,
9386
+ zipCrypto,
9387
+ dataDescriptor
9388
+ } = options;
9389
+ if ((!encrypted || zipCrypto) && signature !== undefined) {
9390
+ setUint32(headerView, 10, signature);
9391
+ if (dataDescriptor) {
9392
+ setUint32(dataDescriptorView, dataDescriptorOffset, signature);
9302
9393
  }
9303
9394
  }
9304
- if (dataDescriptor) {
9305
- await writeData(writable, dataDescriptorArray);
9395
+ if (zip64) {
9396
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
9397
+ setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
9398
+ setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
9399
+ setUint32(headerView, 14, MAX_32_BITS);
9400
+ setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
9401
+ setUint32(headerView, 18, MAX_32_BITS);
9402
+ setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
9403
+ if (dataDescriptor) {
9404
+ setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
9405
+ setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
9406
+ }
9407
+ } else {
9408
+ setUint32(headerView, 14, compressedSize);
9409
+ setUint32(headerView, 18, uncompressedSize);
9410
+ if (dataDescriptor) {
9411
+ setUint32(dataDescriptorView, dataDescriptorOffset + 4, compressedSize);
9412
+ setUint32(dataDescriptorView, dataDescriptorOffset + 8, uncompressedSize);
9413
+ }
9306
9414
  }
9307
- const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
9308
- Object.assign(fileEntry, {
9309
- compressedSize,
9310
- lastModDate,
9311
- rawLastModDate,
9312
- creationDate,
9313
- lastAccessDate,
9314
- encrypted,
9315
- length,
9316
- writingData: false
9317
- });
9318
- return fileEntry;
9319
9415
  }
9320
9416
 
9321
9417
  async function closeFile(zipWriter, comment, options) {