@zip.js/zip.js 2.6.20 → 2.6.23

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
@@ -2453,7 +2453,7 @@
2453
2453
  const writer = this;
2454
2454
  writer.contentType = contentType;
2455
2455
  writer.transformStream = new TransformStream();
2456
- Object.defineProperty(this, "writable", {
2456
+ Object.defineProperty(writer, "writable", {
2457
2457
  get() {
2458
2458
  return writer.transformStream.writable;
2459
2459
  }
@@ -2467,7 +2467,11 @@
2467
2467
  transformStream,
2468
2468
  contentType
2469
2469
  } = writer;
2470
- writer.blob = new Response(transformStream.readable).blob({ type: contentType });
2470
+ const headers = [];
2471
+ if (contentType) {
2472
+ headers.push(["content-type", contentType]);
2473
+ }
2474
+ writer.blob = new Response(transformStream.readable, { headers }).blob();
2471
2475
  }
2472
2476
 
2473
2477
  getData() {
@@ -3539,7 +3543,7 @@
3539
3543
  files: new Map(),
3540
3544
  filenames: new Set(),
3541
3545
  offset: writer.writable.size,
3542
- pendingCompressedSize: 0,
3546
+ pendingEntriesSize: 0,
3543
3547
  pendingAddFileCalls: new Set()
3544
3548
  });
3545
3549
  }
@@ -3651,6 +3655,7 @@
3651
3655
  extendedTimestamp = true;
3652
3656
  }
3653
3657
  let maximumCompressedSize = 0;
3658
+ let maximumEntrySize = 0;
3654
3659
  let keepOrder = getOptionValue(zipWriter, options, "keepOrder");
3655
3660
  if (keepOrder === undefined) {
3656
3661
  keepOrder = true;
@@ -3672,7 +3677,7 @@
3672
3677
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
3673
3678
  }
3674
3679
  let zip64 = options.zip64 || zipWriter.options.zip64 || false;
3675
- if (zipWriter.offset + zipWriter.pendingCompressedSize >= MAX_32_BITS ||
3680
+ if (zipWriter.offset + zipWriter.pendingEntriesSize >= MAX_32_BITS ||
3676
3681
  uncompressedSize >= MAX_32_BITS ||
3677
3682
  maximumCompressedSize >= MAX_32_BITS) {
3678
3683
  if (options.zip64 === false || zipWriter.options.zip64 === false || !keepOrder) {
@@ -3681,8 +3686,6 @@
3681
3686
  zip64 = true;
3682
3687
  }
3683
3688
  }
3684
- zipWriter.pendingCompressedSize += maximumCompressedSize;
3685
- await Promise.resolve();
3686
3689
  const level = getOptionValue(zipWriter, options, "level");
3687
3690
  const useWebWorkers = getOptionValue(zipWriter, options, "useWebWorkers");
3688
3691
  const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
@@ -3696,7 +3699,7 @@
3696
3699
  if (dataDescriptor && dataDescriptorSignature === undefined) {
3697
3700
  dataDescriptorSignature = false;
3698
3701
  }
3699
- const fileEntry = await getFileEntry(zipWriter, name, reader, Object.assign({}, options, {
3702
+ options = Object.assign({}, options, {
3700
3703
  rawFilename,
3701
3704
  rawComment,
3702
3705
  version,
@@ -3721,15 +3724,20 @@
3721
3724
  internalFileAttribute,
3722
3725
  externalFileAttribute,
3723
3726
  useCompressionStream
3724
- }));
3725
- if (maximumCompressedSize) {
3726
- zipWriter.pendingCompressedSize -= maximumCompressedSize;
3727
+ });
3728
+ const headerInfo = getHeaderInfo(options);
3729
+ const dataDescriptorInfo = getDataDescriptorInfo(options);
3730
+ maximumEntrySize = headerInfo.localHeaderArray.length + maximumCompressedSize + dataDescriptorInfo.dataDescriptorArray.length;
3731
+ zipWriter.pendingEntriesSize += maximumEntrySize;
3732
+ const fileEntry = await getFileEntry(zipWriter, name, reader, { headerInfo, dataDescriptorInfo }, options);
3733
+ if (maximumEntrySize) {
3734
+ zipWriter.pendingEntriesSize -= maximumEntrySize;
3727
3735
  }
3728
3736
  Object.assign(fileEntry, { name, comment, extraField });
3729
3737
  return new Entry(fileEntry);
3730
3738
  }
3731
3739
 
3732
- async function getFileEntry(zipWriter, name, reader, options) {
3740
+ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
3733
3741
  const {
3734
3742
  files,
3735
3743
  writer
@@ -3768,7 +3776,8 @@
3768
3776
  if (!bufferedWrite) {
3769
3777
  await lockPreviousFileEntry;
3770
3778
  }
3771
- fileEntry = await createFileEntry(reader, fileWriter, fileEntry, zipWriter.config, options);
3779
+ fileEntry.writingData = true;
3780
+ fileEntry = await createFileEntry(reader, fileWriter, fileEntry, entryInfo, zipWriter.config, options);
3772
3781
  files.set(name, fileEntry);
3773
3782
  fileEntry.filename = name;
3774
3783
  if (bufferedWrite) {
@@ -3844,7 +3853,25 @@
3844
3853
  }
3845
3854
  }
3846
3855
 
3847
- async function createFileEntry(reader, writer, pendingFileEntry, config, options) {
3856
+ async function createFileEntry(reader, writer, pendingFileEntry, entryInfo, config, options) {
3857
+ const {
3858
+ headerInfo,
3859
+ dataDescriptorInfo
3860
+ } = entryInfo;
3861
+ const {
3862
+ localHeaderArray,
3863
+ headerArray,
3864
+ lastModDate,
3865
+ rawLastModDate,
3866
+ encrypted,
3867
+ compressed,
3868
+ version,
3869
+ compressionMethod,
3870
+ rawExtraFieldExtendedTimestamp,
3871
+ rawExtraFieldNTFS,
3872
+ rawExtraFieldAES
3873
+ } = headerInfo;
3874
+ const { dataDescriptorArray } = dataDescriptorInfo;
3848
3875
  const {
3849
3876
  rawFilename,
3850
3877
  lastAccessDate,
@@ -3854,9 +3881,7 @@
3854
3881
  zip64,
3855
3882
  zipCrypto,
3856
3883
  dataDescriptor,
3857
- dataDescriptorSignature,
3858
3884
  directory,
3859
- version,
3860
3885
  versionMadeBy,
3861
3886
  rawComment,
3862
3887
  rawExtraField,
@@ -3872,8 +3897,108 @@
3872
3897
  externalFileAttribute,
3873
3898
  useCompressionStream
3874
3899
  } = options;
3875
- const encrypted = Boolean(password && password.length);
3900
+ const fileEntry = {
3901
+ lock: pendingFileEntry.lock,
3902
+ versionMadeBy,
3903
+ zip64,
3904
+ directory: Boolean(directory),
3905
+ filenameUTF8: true,
3906
+ rawFilename,
3907
+ commentUTF8: true,
3908
+ rawComment,
3909
+ rawExtraFieldExtendedTimestamp,
3910
+ rawExtraFieldNTFS,
3911
+ rawExtraFieldAES,
3912
+ rawExtraField,
3913
+ extendedTimestamp,
3914
+ msDosCompatible,
3915
+ internalFileAttribute,
3916
+ externalFileAttribute
3917
+ };
3918
+ let uncompressedSize = 0;
3919
+ let signature;
3920
+ let compressedSize = 0;
3921
+ const { writable } = writer;
3922
+ if (reader) {
3923
+ reader.chunkSize = getChunkSize(config);
3924
+ await writeData(writable, localHeaderArray);
3925
+ const size = () => reader.size;
3926
+ const readable = reader.readable;
3927
+ readable.size = size;
3928
+ const workerOptions = {
3929
+ options: {
3930
+ codecType: CODEC_DEFLATE,
3931
+ level,
3932
+ password,
3933
+ encryptionStrength,
3934
+ zipCrypto: encrypted && zipCrypto,
3935
+ passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
3936
+ signed: true,
3937
+ compressed,
3938
+ encrypted,
3939
+ useWebWorkers,
3940
+ useCompressionStream
3941
+ },
3942
+ config,
3943
+ streamOptions: { signal, size, onstart, onprogress, onend },
3944
+ codecConstructor: config.Deflate
3945
+ };
3946
+ const previousSize = writable.size;
3947
+ signature = (await runWorker({ readable, writable }, workerOptions)).signature;
3948
+ uncompressedSize = reader.size = readable.size();
3949
+ compressedSize = writable.size - previousSize;
3950
+ } else {
3951
+ await writeData(writable, localHeaderArray);
3952
+ }
3953
+ const rawExtraFieldZip64 = zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array();
3954
+ if (reader) {
3955
+ setEntryInfo({
3956
+ signature,
3957
+ rawExtraFieldZip64,
3958
+ compressedSize,
3959
+ uncompressedSize,
3960
+ headerInfo,
3961
+ dataDescriptorInfo
3962
+ }, options);
3963
+ }
3964
+ if (dataDescriptor) {
3965
+ await writeData(writable, dataDescriptorArray);
3966
+ }
3967
+ Object.assign(fileEntry, {
3968
+ compressedSize,
3969
+ lastModDate,
3970
+ rawLastModDate,
3971
+ creationDate,
3972
+ lastAccessDate,
3973
+ encrypted,
3974
+ length: localHeaderArray.length + compressedSize + dataDescriptorArray.length,
3975
+ compressionMethod,
3976
+ version,
3977
+ headerArray,
3978
+ signature,
3979
+ rawExtraFieldZip64
3980
+ });
3981
+ return fileEntry;
3982
+ }
3983
+
3984
+ function getHeaderInfo(options) {
3985
+ const {
3986
+ rawFilename,
3987
+ lastAccessDate,
3988
+ creationDate,
3989
+ password,
3990
+ level,
3991
+ zip64,
3992
+ zipCrypto,
3993
+ dataDescriptor,
3994
+ directory,
3995
+ rawExtraField,
3996
+ encryptionStrength,
3997
+ extendedTimestamp,
3998
+ } = options;
3999
+ let version = options.version || VERSION_DEFLATE;
3876
4000
  const compressed = level !== 0 && !directory;
4001
+ const encrypted = Boolean(password && password.length);
3877
4002
  let rawExtraFieldAES;
3878
4003
  if (encrypted && !zipCrypto) {
3879
4004
  rawExtraFieldAES = new Uint8Array(EXTRAFIELD_DATA_AES.length + 2);
@@ -3917,27 +4042,6 @@
3917
4042
  } else {
3918
4043
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
3919
4044
  }
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
4045
  let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
3942
4046
  if (dataDescriptor) {
3943
4047
  bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
@@ -3947,22 +4051,21 @@
3947
4051
  compressionMethod = COMPRESSION_METHOD_DEFLATE;
3948
4052
  }
3949
4053
  if (zip64) {
3950
- fileEntry.version = fileEntry.version > VERSION_ZIP64 ? fileEntry.version : VERSION_ZIP64;
4054
+ version = version > VERSION_ZIP64 ? version : VERSION_ZIP64;
3951
4055
  }
3952
4056
  if (encrypted) {
3953
4057
  bitFlag = bitFlag | BITFLAG_ENCRYPTED;
3954
4058
  if (!zipCrypto) {
3955
- fileEntry.version = fileEntry.version > VERSION_AES ? fileEntry.version : VERSION_AES;
4059
+ version = version > VERSION_AES ? version : VERSION_AES;
3956
4060
  compressionMethod = COMPRESSION_METHOD_AES;
3957
4061
  if (compressed) {
3958
- fileEntry.rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
4062
+ rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
3959
4063
  }
3960
4064
  }
3961
4065
  }
3962
- fileEntry.compressionMethod = compressionMethod;
3963
- const headerArray = fileEntry.headerArray = new Uint8Array(26);
4066
+ const headerArray = new Uint8Array(26);
3964
4067
  const headerView = getDataView(headerArray);
3965
- setUint16(headerView, 0, fileEntry.version);
4068
+ setUint16(headerView, 0, version);
3966
4069
  setUint16(headerView, 2, bitFlag);
3967
4070
  setUint16(headerView, 4, compressionMethod);
3968
4071
  const dateArray = new Uint32Array(1);
@@ -3991,41 +4094,28 @@
3991
4094
  arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + rawFilename.length + rawExtraFieldAES.length);
3992
4095
  arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length);
3993
4096
  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
- }
4097
+ return {
4098
+ localHeaderArray,
4099
+ headerArray,
4100
+ headerView,
4101
+ lastModDate,
4102
+ rawLastModDate,
4103
+ encrypted,
4104
+ compressed,
4105
+ version,
4106
+ compressionMethod,
4107
+ rawExtraFieldExtendedTimestamp,
4108
+ rawExtraFieldNTFS,
4109
+ rawExtraFieldAES
4110
+ };
4111
+ }
4112
+
4113
+ function getDataDescriptorInfo(options) {
4114
+ const {
4115
+ zip64,
4116
+ dataDescriptor,
4117
+ dataDescriptorSignature
4118
+ } = options;
4029
4119
  let dataDescriptorArray = new Uint8Array();
4030
4120
  let dataDescriptorView, dataDescriptorOffset = 0;
4031
4121
  if (dataDescriptor) {
@@ -4036,51 +4126,61 @@
4036
4126
  setUint32(dataDescriptorView, 0, DATA_DESCRIPTOR_RECORD_SIGNATURE);
4037
4127
  }
4038
4128
  }
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
- }
4129
+ return {
4130
+ dataDescriptorArray,
4131
+ dataDescriptorView,
4132
+ dataDescriptorOffset
4133
+ };
4134
+ }
4135
+
4136
+ function setEntryInfo(entryInfo, options) {
4137
+ const {
4138
+ signature,
4139
+ rawExtraFieldZip64,
4140
+ compressedSize,
4141
+ uncompressedSize,
4142
+ headerInfo,
4143
+ dataDescriptorInfo
4144
+ } = entryInfo;
4145
+ const {
4146
+ headerView,
4147
+ encrypted
4148
+ } = headerInfo;
4149
+ const {
4150
+ dataDescriptorView,
4151
+ dataDescriptorOffset
4152
+ } = dataDescriptorInfo;
4153
+ const {
4154
+ zip64,
4155
+ zipCrypto,
4156
+ dataDescriptor
4157
+ } = options;
4158
+ if ((!encrypted || zipCrypto) && signature !== undefined) {
4159
+ setUint32(headerView, 10, signature);
4160
+ if (dataDescriptor) {
4161
+ setUint32(dataDescriptorView, dataDescriptorOffset, signature);
4067
4162
  }
4068
4163
  }
4069
- if (dataDescriptor) {
4070
- await writeData(writable, dataDescriptorArray);
4164
+ if (zip64) {
4165
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
4166
+ setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
4167
+ setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
4168
+ setUint32(headerView, 14, MAX_32_BITS);
4169
+ setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
4170
+ setUint32(headerView, 18, MAX_32_BITS);
4171
+ setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
4172
+ if (dataDescriptor) {
4173
+ setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
4174
+ setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
4175
+ }
4176
+ } else {
4177
+ setUint32(headerView, 14, compressedSize);
4178
+ setUint32(headerView, 18, uncompressedSize);
4179
+ if (dataDescriptor) {
4180
+ setUint32(dataDescriptorView, dataDescriptorOffset + 4, compressedSize);
4181
+ setUint32(dataDescriptorView, dataDescriptorOffset + 8, uncompressedSize);
4182
+ }
4071
4183
  }
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
4184
  }
4085
4185
 
4086
4186
  async function closeFile(zipWriter, comment, options) {