@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.
package/dist/zip.js CHANGED
@@ -3539,7 +3539,7 @@
3539
3539
  files: new Map(),
3540
3540
  filenames: new Set(),
3541
3541
  offset: writer.writable.size,
3542
- pendingCompressedSize: 0,
3542
+ pendingEntriesSize: 0,
3543
3543
  pendingAddFileCalls: new Set()
3544
3544
  });
3545
3545
  }
@@ -3651,6 +3651,7 @@
3651
3651
  extendedTimestamp = true;
3652
3652
  }
3653
3653
  let maximumCompressedSize = 0;
3654
+ let maximumEntrySize = 0;
3654
3655
  let keepOrder = getOptionValue(zipWriter, options, "keepOrder");
3655
3656
  if (keepOrder === undefined) {
3656
3657
  keepOrder = true;
@@ -3672,7 +3673,7 @@
3672
3673
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
3673
3674
  }
3674
3675
  let zip64 = options.zip64 || zipWriter.options.zip64 || false;
3675
- if (zipWriter.offset + zipWriter.pendingCompressedSize >= MAX_32_BITS ||
3676
+ if (zipWriter.offset + zipWriter.pendingEntriesSize >= MAX_32_BITS ||
3676
3677
  uncompressedSize >= MAX_32_BITS ||
3677
3678
  maximumCompressedSize >= MAX_32_BITS) {
3678
3679
  if (options.zip64 === false || zipWriter.options.zip64 === false || !keepOrder) {
@@ -3681,8 +3682,6 @@
3681
3682
  zip64 = true;
3682
3683
  }
3683
3684
  }
3684
- zipWriter.pendingCompressedSize += maximumCompressedSize;
3685
- await Promise.resolve();
3686
3685
  const level = getOptionValue(zipWriter, options, "level");
3687
3686
  const useWebWorkers = getOptionValue(zipWriter, options, "useWebWorkers");
3688
3687
  const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
@@ -3696,7 +3695,7 @@
3696
3695
  if (dataDescriptor && dataDescriptorSignature === undefined) {
3697
3696
  dataDescriptorSignature = false;
3698
3697
  }
3699
- const fileEntry = await getFileEntry(zipWriter, name, reader, Object.assign({}, options, {
3698
+ options = Object.assign({}, options, {
3700
3699
  rawFilename,
3701
3700
  rawComment,
3702
3701
  version,
@@ -3721,15 +3720,20 @@
3721
3720
  internalFileAttribute,
3722
3721
  externalFileAttribute,
3723
3722
  useCompressionStream
3724
- }));
3725
- if (maximumCompressedSize) {
3726
- zipWriter.pendingCompressedSize -= maximumCompressedSize;
3723
+ });
3724
+ const localHeaderInfo = getLocalHeaderInfo(options);
3725
+ const dataDescriptorInfo = getDataDescriptorInfo(options);
3726
+ maximumEntrySize = localHeaderInfo.localHeaderArray.length + maximumCompressedSize + dataDescriptorInfo.dataDescriptorArray.length;
3727
+ zipWriter.pendingEntriesSize += maximumEntrySize;
3728
+ const fileEntry = await getFileEntry(zipWriter, name, reader, { localHeaderInfo, dataDescriptorInfo }, options);
3729
+ if (maximumEntrySize) {
3730
+ zipWriter.pendingEntriesSize -= maximumEntrySize;
3727
3731
  }
3728
3732
  Object.assign(fileEntry, { name, comment, extraField });
3729
3733
  return new Entry(fileEntry);
3730
3734
  }
3731
3735
 
3732
- async function getFileEntry(zipWriter, name, reader, options) {
3736
+ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
3733
3737
  const {
3734
3738
  files,
3735
3739
  writer
@@ -3768,7 +3772,8 @@
3768
3772
  if (!bufferedWrite) {
3769
3773
  await lockPreviousFileEntry;
3770
3774
  }
3771
- fileEntry = await createFileEntry(reader, fileWriter, fileEntry, zipWriter.config, options);
3775
+ fileEntry.writingData = true;
3776
+ fileEntry = await createFileEntry(reader, fileWriter, fileEntry, entryInfo, zipWriter.config, options);
3772
3777
  files.set(name, fileEntry);
3773
3778
  fileEntry.filename = name;
3774
3779
  if (bufferedWrite) {
@@ -3844,7 +3849,25 @@
3844
3849
  }
3845
3850
  }
3846
3851
 
3847
- async function createFileEntry(reader, writer, pendingFileEntry, config, options) {
3852
+ async function createFileEntry(reader, writer, pendingFileEntry, entryInfo, config, options) {
3853
+ const {
3854
+ localHeaderInfo,
3855
+ dataDescriptorInfo
3856
+ } = entryInfo;
3857
+ const {
3858
+ localHeaderArray,
3859
+ headerArray,
3860
+ lastModDate,
3861
+ rawLastModDate,
3862
+ encrypted,
3863
+ compressed,
3864
+ version,
3865
+ compressionMethod,
3866
+ rawExtraFieldExtendedTimestamp,
3867
+ rawExtraFieldNTFS,
3868
+ rawExtraFieldAES
3869
+ } = localHeaderInfo;
3870
+ const { dataDescriptorArray } = dataDescriptorInfo;
3848
3871
  const {
3849
3872
  rawFilename,
3850
3873
  lastAccessDate,
@@ -3854,9 +3877,7 @@
3854
3877
  zip64,
3855
3878
  zipCrypto,
3856
3879
  dataDescriptor,
3857
- dataDescriptorSignature,
3858
3880
  directory,
3859
- version,
3860
3881
  versionMadeBy,
3861
3882
  rawComment,
3862
3883
  rawExtraField,
@@ -3872,8 +3893,108 @@
3872
3893
  externalFileAttribute,
3873
3894
  useCompressionStream
3874
3895
  } = options;
3875
- const encrypted = Boolean(password && password.length);
3896
+ const fileEntry = {
3897
+ lock: pendingFileEntry.lock,
3898
+ versionMadeBy,
3899
+ zip64,
3900
+ directory: Boolean(directory),
3901
+ filenameUTF8: true,
3902
+ rawFilename,
3903
+ commentUTF8: true,
3904
+ rawComment,
3905
+ rawExtraFieldExtendedTimestamp,
3906
+ rawExtraFieldNTFS,
3907
+ rawExtraFieldAES,
3908
+ rawExtraField,
3909
+ extendedTimestamp,
3910
+ msDosCompatible,
3911
+ internalFileAttribute,
3912
+ externalFileAttribute
3913
+ };
3914
+ let uncompressedSize = 0;
3915
+ let signature;
3916
+ let compressedSize = 0;
3917
+ const { writable } = writer;
3918
+ if (reader) {
3919
+ reader.chunkSize = getChunkSize(config);
3920
+ await writeData(writable, localHeaderArray);
3921
+ const size = () => reader.size;
3922
+ const readable = reader.readable;
3923
+ readable.size = size;
3924
+ const workerOptions = {
3925
+ options: {
3926
+ codecType: CODEC_DEFLATE,
3927
+ level,
3928
+ password,
3929
+ encryptionStrength,
3930
+ zipCrypto: encrypted && zipCrypto,
3931
+ passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
3932
+ signed: true,
3933
+ compressed,
3934
+ encrypted,
3935
+ useWebWorkers,
3936
+ useCompressionStream
3937
+ },
3938
+ config,
3939
+ streamOptions: { signal, size, onstart, onprogress, onend },
3940
+ codecConstructor: config.Deflate
3941
+ };
3942
+ const previousSize = writable.size;
3943
+ signature = (await runWorker({ readable, writable }, workerOptions)).signature;
3944
+ uncompressedSize = reader.size = readable.size();
3945
+ compressedSize = writable.size - previousSize;
3946
+ } else {
3947
+ await writeData(writable, localHeaderArray);
3948
+ }
3949
+ const rawExtraFieldZip64 = zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array();
3950
+ if (reader) {
3951
+ setEntryInfo({
3952
+ signature,
3953
+ rawExtraFieldZip64,
3954
+ compressedSize,
3955
+ uncompressedSize,
3956
+ localHeaderInfo,
3957
+ dataDescriptorInfo
3958
+ }, options);
3959
+ }
3960
+ if (dataDescriptor) {
3961
+ await writeData(writable, dataDescriptorArray);
3962
+ }
3963
+ Object.assign(fileEntry, {
3964
+ compressedSize,
3965
+ lastModDate,
3966
+ rawLastModDate,
3967
+ creationDate,
3968
+ lastAccessDate,
3969
+ encrypted,
3970
+ length: localHeaderArray.length + compressedSize + dataDescriptorArray.length,
3971
+ compressionMethod,
3972
+ version,
3973
+ headerArray,
3974
+ signature,
3975
+ rawExtraFieldZip64
3976
+ });
3977
+ return fileEntry;
3978
+ }
3979
+
3980
+ function getLocalHeaderInfo(options) {
3981
+ const {
3982
+ rawFilename,
3983
+ lastAccessDate,
3984
+ creationDate,
3985
+ password,
3986
+ level,
3987
+ zip64,
3988
+ zipCrypto,
3989
+ dataDescriptor,
3990
+ directory,
3991
+ rawExtraField,
3992
+ encryptionStrength,
3993
+ extendedTimestamp,
3994
+ } = options;
3995
+ let version = options.version || VERSION_DEFLATE;
3876
3996
  const compressed = level !== 0 && !directory;
3997
+ const encrypted = Boolean(password && password.length);
3877
3998
  let rawExtraFieldAES;
3878
3999
  if (encrypted && !zipCrypto) {
3879
4000
  rawExtraFieldAES = new Uint8Array(EXTRAFIELD_DATA_AES.length + 2);
@@ -3917,27 +4038,6 @@
3917
4038
  } else {
3918
4039
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
3919
4040
  }
3920
- const fileEntry = {
3921
- lock: pendingFileEntry.lock,
3922
- version: version || VERSION_DEFLATE,
3923
- versionMadeBy,
3924
- zip64,
3925
- directory: Boolean(directory),
3926
- filenameUTF8: true,
3927
- rawFilename,
3928
- commentUTF8: true,
3929
- rawComment,
3930
- rawExtraFieldZip64: zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array(),
3931
- rawExtraFieldExtendedTimestamp,
3932
- rawExtraFieldNTFS,
3933
- rawExtraFieldAES,
3934
- rawExtraField,
3935
- extendedTimestamp,
3936
- msDosCompatible,
3937
- internalFileAttribute,
3938
- externalFileAttribute
3939
- };
3940
- let uncompressedSize = 0;
3941
4041
  let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
3942
4042
  if (dataDescriptor) {
3943
4043
  bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
@@ -3947,22 +4047,21 @@
3947
4047
  compressionMethod = COMPRESSION_METHOD_DEFLATE;
3948
4048
  }
3949
4049
  if (zip64) {
3950
- fileEntry.version = fileEntry.version > VERSION_ZIP64 ? fileEntry.version : VERSION_ZIP64;
4050
+ version = version > VERSION_ZIP64 ? version : VERSION_ZIP64;
3951
4051
  }
3952
4052
  if (encrypted) {
3953
4053
  bitFlag = bitFlag | BITFLAG_ENCRYPTED;
3954
4054
  if (!zipCrypto) {
3955
- fileEntry.version = fileEntry.version > VERSION_AES ? fileEntry.version : VERSION_AES;
4055
+ version = version > VERSION_AES ? version : VERSION_AES;
3956
4056
  compressionMethod = COMPRESSION_METHOD_AES;
3957
4057
  if (compressed) {
3958
- fileEntry.rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
4058
+ rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
3959
4059
  }
3960
4060
  }
3961
4061
  }
3962
- fileEntry.compressionMethod = compressionMethod;
3963
- const headerArray = fileEntry.headerArray = new Uint8Array(26);
4062
+ const headerArray = new Uint8Array(26);
3964
4063
  const headerView = getDataView(headerArray);
3965
- setUint16(headerView, 0, fileEntry.version);
4064
+ setUint16(headerView, 0, version);
3966
4065
  setUint16(headerView, 2, bitFlag);
3967
4066
  setUint16(headerView, 4, compressionMethod);
3968
4067
  const dateArray = new Uint32Array(1);
@@ -3991,41 +4090,28 @@
3991
4090
  arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + rawFilename.length + rawExtraFieldAES.length);
3992
4091
  arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length);
3993
4092
  arraySet(localHeaderArray, rawExtraField, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length + rawExtraFieldNTFS.length);
3994
- let result;
3995
- let compressedSize = 0;
3996
- const { writable } = writer;
3997
- fileEntry.writingData = pendingFileEntry.writingData = true;
3998
- if (reader) {
3999
- reader.chunkSize = getChunkSize(config);
4000
- await writeData(writable, localHeaderArray);
4001
- const size = () => reader.size;
4002
- const readable = reader.readable;
4003
- readable.size = size;
4004
- const workerOptions = {
4005
- options: {
4006
- codecType: CODEC_DEFLATE,
4007
- level,
4008
- password,
4009
- encryptionStrength,
4010
- zipCrypto: encrypted && zipCrypto,
4011
- passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
4012
- signed: true,
4013
- compressed,
4014
- encrypted,
4015
- useWebWorkers,
4016
- useCompressionStream
4017
- },
4018
- config,
4019
- streamOptions: { signal, size, onstart, onprogress, onend },
4020
- codecConstructor: config.Deflate
4021
- };
4022
- const previousSize = writable.size;
4023
- result = await runWorker({ readable, writable }, workerOptions);
4024
- uncompressedSize = reader.size = readable.size();
4025
- compressedSize = writable.size - previousSize;
4026
- } else {
4027
- await writeData(writable, localHeaderArray);
4028
- }
4093
+ return {
4094
+ localHeaderArray,
4095
+ headerArray,
4096
+ headerView,
4097
+ lastModDate,
4098
+ rawLastModDate,
4099
+ encrypted,
4100
+ compressed,
4101
+ version,
4102
+ compressionMethod,
4103
+ rawExtraFieldExtendedTimestamp,
4104
+ rawExtraFieldNTFS,
4105
+ rawExtraFieldAES
4106
+ };
4107
+ }
4108
+
4109
+ function getDataDescriptorInfo(options) {
4110
+ const {
4111
+ zip64,
4112
+ dataDescriptor,
4113
+ dataDescriptorSignature
4114
+ } = options;
4029
4115
  let dataDescriptorArray = new Uint8Array();
4030
4116
  let dataDescriptorView, dataDescriptorOffset = 0;
4031
4117
  if (dataDescriptor) {
@@ -4036,51 +4122,61 @@
4036
4122
  setUint32(dataDescriptorView, 0, DATA_DESCRIPTOR_RECORD_SIGNATURE);
4037
4123
  }
4038
4124
  }
4039
- if (reader) {
4040
- const { signature } = result;
4041
- if ((!encrypted || zipCrypto) && signature !== undefined) {
4042
- setUint32(headerView, 10, signature);
4043
- fileEntry.signature = signature;
4044
- if (dataDescriptor) {
4045
- setUint32(dataDescriptorView, dataDescriptorOffset, signature);
4046
- }
4047
- }
4048
- if (zip64) {
4049
- const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
4050
- setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
4051
- setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
4052
- setUint32(headerView, 14, MAX_32_BITS);
4053
- setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
4054
- setUint32(headerView, 18, MAX_32_BITS);
4055
- setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
4056
- if (dataDescriptor) {
4057
- setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
4058
- setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
4059
- }
4060
- } else {
4061
- setUint32(headerView, 14, compressedSize);
4062
- setUint32(headerView, 18, uncompressedSize);
4063
- if (dataDescriptor) {
4064
- setUint32(dataDescriptorView, dataDescriptorOffset + 4, compressedSize);
4065
- setUint32(dataDescriptorView, dataDescriptorOffset + 8, uncompressedSize);
4066
- }
4125
+ return {
4126
+ dataDescriptorArray,
4127
+ dataDescriptorView,
4128
+ dataDescriptorOffset
4129
+ };
4130
+ }
4131
+
4132
+ function setEntryInfo(entryInfo, options) {
4133
+ const {
4134
+ signature,
4135
+ rawExtraFieldZip64,
4136
+ compressedSize,
4137
+ uncompressedSize,
4138
+ localHeaderInfo,
4139
+ dataDescriptorInfo
4140
+ } = entryInfo;
4141
+ const {
4142
+ headerView,
4143
+ encrypted
4144
+ } = localHeaderInfo;
4145
+ const {
4146
+ dataDescriptorView,
4147
+ dataDescriptorOffset
4148
+ } = dataDescriptorInfo;
4149
+ const {
4150
+ zip64,
4151
+ zipCrypto,
4152
+ dataDescriptor
4153
+ } = options;
4154
+ if ((!encrypted || zipCrypto) && signature !== undefined) {
4155
+ setUint32(headerView, 10, signature);
4156
+ if (dataDescriptor) {
4157
+ setUint32(dataDescriptorView, dataDescriptorOffset, signature);
4067
4158
  }
4068
4159
  }
4069
- if (dataDescriptor) {
4070
- await writeData(writable, dataDescriptorArray);
4160
+ if (zip64) {
4161
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
4162
+ setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
4163
+ setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
4164
+ setUint32(headerView, 14, MAX_32_BITS);
4165
+ setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
4166
+ setUint32(headerView, 18, MAX_32_BITS);
4167
+ setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
4168
+ if (dataDescriptor) {
4169
+ setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
4170
+ setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
4171
+ }
4172
+ } else {
4173
+ setUint32(headerView, 14, compressedSize);
4174
+ setUint32(headerView, 18, uncompressedSize);
4175
+ if (dataDescriptor) {
4176
+ setUint32(dataDescriptorView, dataDescriptorOffset + 4, compressedSize);
4177
+ setUint32(dataDescriptorView, dataDescriptorOffset + 8, uncompressedSize);
4178
+ }
4071
4179
  }
4072
- const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
4073
- Object.assign(fileEntry, {
4074
- compressedSize,
4075
- lastModDate,
4076
- rawLastModDate,
4077
- creationDate,
4078
- lastAccessDate,
4079
- encrypted,
4080
- length,
4081
- writingData: false
4082
- });
4083
- return fileEntry;
4084
4180
  }
4085
4181
 
4086
4182
  async function closeFile(zipWriter, comment, options) {