@yorozu/utils 0.3.0 → 0.5.0
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/arrays/u8/misc.d.ts +1 -1
- package/arrays/u8/pool.d.ts +5 -1
- package/async/async-interval.d.ts +9 -1
- package/async/async-lock.d.ts +5 -1
- package/async/async-queue.d.ts +13 -1
- package/async/async-resource.d.ts +9 -1
- package/async/condition-variable.d.ts +2 -1
- package/async/deferred.d.ts +2 -1
- package/async/emitter.d.ts +5 -1
- package/compress/bits.d.ts +5 -0
- package/compress/checksum.d.ts +15 -0
- package/compress/compress.d.ts +3 -0
- package/compress/decompress.d.ts +4 -0
- package/compress/deflate.d.ts +22 -0
- package/compress/detect.d.ts +2 -0
- package/compress/errors.d.ts +24 -0
- package/compress/gzip.d.ts +31 -0
- package/compress/huffman.d.ts +6 -0
- package/compress/index.d.ts +5 -0
- package/compress/inflate.d.ts +21 -0
- package/compress/tables.d.ts +10 -0
- package/compress/types.d.ts +14 -0
- package/compress/zlib.d.ts +30 -0
- package/index.d.ts +1 -0
- package/index.js +1640 -276
- package/package.json +1 -1
- package/structures/custom-map.d.ts +3 -1
- package/structures/custom-set.d.ts +3 -1
- package/structures/deque.d.ts +4 -1
- package/structures/lru-map.d.ts +2 -1
- package/structures/lru-set.d.ts +2 -1
package/index.js
CHANGED
|
@@ -44,9 +44,14 @@ function lastIndexOf(haystack, needle, start = haystack.length - 1) {
|
|
|
44
44
|
return -1;
|
|
45
45
|
}
|
|
46
46
|
function indexOfArray(haystack, needle, start = 0) {
|
|
47
|
+
let length = haystack.length;
|
|
48
|
+
if (start < 0) {
|
|
49
|
+
start += length;
|
|
50
|
+
if (start < 0) start = 0;
|
|
51
|
+
}
|
|
47
52
|
if (needle.length === 0) return start;
|
|
48
53
|
if (needle.length === 1) return indexOf(haystack, needle[0], start);
|
|
49
|
-
let max =
|
|
54
|
+
let max = length - needle.length;
|
|
50
55
|
for (let i = start; i <= max; i++) if (haystack[i] === needle[0]) {
|
|
51
56
|
let j = 1;
|
|
52
57
|
for (; j < needle.length; j++) if (haystack[i + j] !== needle[j]) break;
|
|
@@ -55,9 +60,14 @@ function indexOfArray(haystack, needle, start = 0) {
|
|
|
55
60
|
return -1;
|
|
56
61
|
}
|
|
57
62
|
function lastIndexOfArray(haystack, needle, start = haystack.length - 1) {
|
|
63
|
+
let length = haystack.length;
|
|
64
|
+
if (start < 0) {
|
|
65
|
+
start += length;
|
|
66
|
+
if (start < 0) return -1;
|
|
67
|
+
} else if (start >= length) start = length - 1;
|
|
58
68
|
if (needle.length === 0) return start;
|
|
59
69
|
if (needle.length === 1) return lastIndexOf(haystack, needle[0], start);
|
|
60
|
-
let maxStart =
|
|
70
|
+
let maxStart = length - needle.length;
|
|
61
71
|
let effectiveStart = start > maxStart ? maxStart : start;
|
|
62
72
|
for (let i = effectiveStart; i >= 0; i--) {
|
|
63
73
|
let j = 0;
|
|
@@ -114,7 +124,7 @@ var typed_exports = /* @__PURE__ */ __exportAll({
|
|
|
114
124
|
});
|
|
115
125
|
//#endregion
|
|
116
126
|
//#region src/arrays/u8/misc.ts
|
|
117
|
-
var empty = new Uint8Array(0);
|
|
127
|
+
var empty$1 = new Uint8Array(0);
|
|
118
128
|
function clone(buf) {
|
|
119
129
|
return new Uint8Array(buf);
|
|
120
130
|
}
|
|
@@ -126,36 +136,36 @@ function readNthBit(byte, bit) {
|
|
|
126
136
|
var BufferPool = class {
|
|
127
137
|
size;
|
|
128
138
|
maxAllocSize;
|
|
129
|
-
|
|
130
|
-
|
|
139
|
+
_pool;
|
|
140
|
+
_offset = 0;
|
|
131
141
|
constructor(size = 16 * 1024) {
|
|
132
142
|
this.size = size;
|
|
133
143
|
this.maxAllocSize = size >>> 1;
|
|
134
|
-
this
|
|
144
|
+
this._reallocate();
|
|
135
145
|
}
|
|
136
|
-
get
|
|
137
|
-
return this.size - this
|
|
146
|
+
get _remaining() {
|
|
147
|
+
return this.size - this._offset;
|
|
138
148
|
}
|
|
139
149
|
allocate(size) {
|
|
140
150
|
if (!Number.isInteger(size) || size < 0) throw new RangeError(`Invalid allocation size: ${size}.`);
|
|
141
|
-
if (size === 0) return empty;
|
|
151
|
+
if (size === 0) return empty$1;
|
|
142
152
|
if (size > this.maxAllocSize) return new Uint8Array(size);
|
|
143
|
-
if (size > this
|
|
144
|
-
let start = this
|
|
145
|
-
this
|
|
146
|
-
this
|
|
147
|
-
return new Uint8Array(this
|
|
153
|
+
if (size > this._remaining) this._reallocate();
|
|
154
|
+
let start = this._offset;
|
|
155
|
+
this._offset += size;
|
|
156
|
+
this._align();
|
|
157
|
+
return new Uint8Array(this._pool, start, size);
|
|
148
158
|
}
|
|
149
159
|
reset() {
|
|
150
|
-
this
|
|
160
|
+
this._reallocate();
|
|
151
161
|
}
|
|
152
|
-
|
|
153
|
-
this
|
|
154
|
-
this
|
|
162
|
+
_reallocate() {
|
|
163
|
+
this._pool = new ArrayBuffer(this.size);
|
|
164
|
+
this._offset = 0;
|
|
155
165
|
}
|
|
156
|
-
|
|
157
|
-
let misalignment = this
|
|
158
|
-
if (misalignment !== 0) this
|
|
166
|
+
_align() {
|
|
167
|
+
let misalignment = this._offset & 7;
|
|
168
|
+
if (misalignment !== 0) this._offset += 8 - misalignment;
|
|
159
169
|
}
|
|
160
170
|
};
|
|
161
171
|
var defaultPool = new BufferPool(16 * 1024);
|
|
@@ -292,7 +302,7 @@ var u8_exports = /* @__PURE__ */ __exportAll({
|
|
|
292
302
|
concat: () => concat,
|
|
293
303
|
concat2: () => concat2,
|
|
294
304
|
concat3: () => concat3,
|
|
295
|
-
empty: () => empty,
|
|
305
|
+
empty: () => empty$1,
|
|
296
306
|
readNthBit: () => readNthBit,
|
|
297
307
|
reverse: () => reverse,
|
|
298
308
|
setDefaultPool: () => setDefaultPool,
|
|
@@ -769,51 +779,51 @@ var clearIntervalWrap = ((...args) => clearInterval(...args));
|
|
|
769
779
|
//#endregion
|
|
770
780
|
//#region src/async/async-interval.ts
|
|
771
781
|
var AsyncInterval = class {
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
782
|
+
_handler;
|
|
783
|
+
_interval;
|
|
784
|
+
_timer;
|
|
785
|
+
_onError = noop;
|
|
786
|
+
_stopped = true;
|
|
787
|
+
_generation = 0;
|
|
788
|
+
_abortController = new AbortController();
|
|
779
789
|
constructor(handler, interval) {
|
|
780
|
-
this
|
|
781
|
-
this
|
|
790
|
+
this._handler = handler;
|
|
791
|
+
this._interval = interval;
|
|
782
792
|
}
|
|
783
|
-
start(after = this
|
|
793
|
+
start(after = this._interval) {
|
|
784
794
|
this.stop();
|
|
785
|
-
this
|
|
786
|
-
let generation = this
|
|
787
|
-
this
|
|
795
|
+
this._stopped = false;
|
|
796
|
+
let generation = this._generation;
|
|
797
|
+
this._timer = setTimeoutWrap(() => this._onTimeout(generation), after);
|
|
788
798
|
}
|
|
789
799
|
startNow() {
|
|
790
800
|
this.stop();
|
|
791
|
-
this
|
|
792
|
-
this
|
|
801
|
+
this._stopped = false;
|
|
802
|
+
this._onTimeout(this._generation);
|
|
793
803
|
}
|
|
794
804
|
stop() {
|
|
795
|
-
this
|
|
796
|
-
this
|
|
797
|
-
this
|
|
798
|
-
if (this
|
|
799
|
-
clearTimeoutWrap(this
|
|
800
|
-
this
|
|
805
|
+
this._generation++;
|
|
806
|
+
this._abortController.abort();
|
|
807
|
+
this._abortController = new AbortController();
|
|
808
|
+
if (this._timer != null) {
|
|
809
|
+
clearTimeoutWrap(this._timer);
|
|
810
|
+
this._timer = void 0;
|
|
801
811
|
}
|
|
802
|
-
this
|
|
812
|
+
this._stopped = true;
|
|
803
813
|
}
|
|
804
814
|
onError(handler) {
|
|
805
|
-
this
|
|
815
|
+
this._onError = handler;
|
|
806
816
|
}
|
|
807
|
-
|
|
808
|
-
this
|
|
817
|
+
_onTimeout = (generation) => {
|
|
818
|
+
this._timer = void 0;
|
|
809
819
|
(async () => {
|
|
810
820
|
try {
|
|
811
|
-
await this
|
|
821
|
+
await this._handler(this._abortController.signal);
|
|
812
822
|
} catch (err) {
|
|
813
|
-
this
|
|
823
|
+
this._onError(err);
|
|
814
824
|
}
|
|
815
|
-
if (this
|
|
816
|
-
this
|
|
825
|
+
if (this._stopped || generation !== this._generation) return;
|
|
826
|
+
this._timer = setTimeoutWrap(() => this._onTimeout(generation), this._interval);
|
|
817
827
|
})();
|
|
818
828
|
};
|
|
819
829
|
};
|
|
@@ -829,55 +839,55 @@ function maybeWrapIterator(iter) {
|
|
|
829
839
|
//#region src/structures/custom-map.ts
|
|
830
840
|
var CustomMap = class {
|
|
831
841
|
clear;
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
842
|
+
_map;
|
|
843
|
+
_mapperTo;
|
|
844
|
+
_mapperFrom;
|
|
835
845
|
constructor(externalToInternal, internalToExternal) {
|
|
836
|
-
this
|
|
837
|
-
this
|
|
838
|
-
this.clear = (this
|
|
846
|
+
this._mapperTo = externalToInternal;
|
|
847
|
+
this._mapperFrom = internalToExternal;
|
|
848
|
+
this.clear = (this._map = /* @__PURE__ */ new Map()).clear.bind(this._map);
|
|
839
849
|
}
|
|
840
850
|
get size() {
|
|
841
|
-
return this
|
|
851
|
+
return this._map.size;
|
|
842
852
|
}
|
|
843
853
|
get [Symbol.toStringTag]() {
|
|
844
|
-
return this
|
|
854
|
+
return this._map[Symbol.toStringTag];
|
|
845
855
|
}
|
|
846
856
|
getInternalMap() {
|
|
847
|
-
return this
|
|
857
|
+
return this._map;
|
|
848
858
|
}
|
|
849
859
|
delete(key) {
|
|
850
|
-
return this
|
|
860
|
+
return this._map.delete(this._mapperTo(key));
|
|
851
861
|
}
|
|
852
862
|
forEach(cb, thisArg) {
|
|
853
|
-
return this
|
|
854
|
-
cb.call(thisArg, value, this
|
|
863
|
+
return this._map.forEach((value, key) => {
|
|
864
|
+
cb.call(thisArg, value, this._mapperFrom(key), this);
|
|
855
865
|
});
|
|
856
866
|
}
|
|
857
867
|
get(key) {
|
|
858
|
-
return this
|
|
868
|
+
return this._map.get(this._mapperTo(key));
|
|
859
869
|
}
|
|
860
870
|
has(key) {
|
|
861
|
-
return this
|
|
871
|
+
return this._map.has(this._mapperTo(key));
|
|
862
872
|
}
|
|
863
873
|
set(key, value) {
|
|
864
|
-
this
|
|
874
|
+
this._map.set(this._mapperTo(key), value);
|
|
865
875
|
return this;
|
|
866
876
|
}
|
|
867
877
|
getOrInsert(key, value) {
|
|
868
|
-
if (this
|
|
869
|
-
let k = this
|
|
870
|
-
if (!this
|
|
871
|
-
return this
|
|
878
|
+
if (this._map.getOrInsert) return this._map.getOrInsert(this._mapperTo(key), value);
|
|
879
|
+
let k = this._mapperTo(key);
|
|
880
|
+
if (!this._map.has(k)) this._map.set(k, value);
|
|
881
|
+
return this._map.get(k);
|
|
872
882
|
}
|
|
873
883
|
getOrInsertComputed(key, callback) {
|
|
874
|
-
if (this
|
|
875
|
-
let k = this
|
|
876
|
-
if (!this
|
|
877
|
-
return this
|
|
884
|
+
if (this._map.getOrInsertComputed) return this._map.getOrInsertComputed(this._mapperTo(key), (k) => callback(this._mapperFrom(k)));
|
|
885
|
+
let k = this._mapperTo(key);
|
|
886
|
+
if (!this._map.has(k)) this._map.set(k, callback(key));
|
|
887
|
+
return this._map.get(k);
|
|
878
888
|
}
|
|
879
889
|
entries() {
|
|
880
|
-
let inner = this
|
|
890
|
+
let inner = this._map.entries();
|
|
881
891
|
const iterator = {
|
|
882
892
|
[Symbol.iterator]: () => iterator,
|
|
883
893
|
next: () => {
|
|
@@ -888,14 +898,14 @@ var CustomMap = class {
|
|
|
888
898
|
};
|
|
889
899
|
return {
|
|
890
900
|
done,
|
|
891
|
-
value: [this
|
|
901
|
+
value: [this._mapperFrom(value[0]), value[1]]
|
|
892
902
|
};
|
|
893
903
|
}
|
|
894
904
|
};
|
|
895
905
|
return maybeWrapIterator(iterator);
|
|
896
906
|
}
|
|
897
907
|
keys() {
|
|
898
|
-
let inner = this
|
|
908
|
+
let inner = this._map.keys();
|
|
899
909
|
const iterator = {
|
|
900
910
|
[Symbol.iterator]: () => iterator,
|
|
901
911
|
next: () => {
|
|
@@ -906,14 +916,14 @@ var CustomMap = class {
|
|
|
906
916
|
};
|
|
907
917
|
return {
|
|
908
918
|
done,
|
|
909
|
-
value: this
|
|
919
|
+
value: this._mapperFrom(value)
|
|
910
920
|
};
|
|
911
921
|
}
|
|
912
922
|
};
|
|
913
923
|
return maybeWrapIterator(iterator);
|
|
914
924
|
}
|
|
915
925
|
values() {
|
|
916
|
-
let inner = this
|
|
926
|
+
let inner = this._map.values();
|
|
917
927
|
const iterator = {
|
|
918
928
|
[Symbol.iterator]: () => iterator,
|
|
919
929
|
next: () => {
|
|
@@ -938,39 +948,39 @@ var CustomMap = class {
|
|
|
938
948
|
//#region src/structures/custom-set.ts
|
|
939
949
|
var CustomSet = class CustomSet {
|
|
940
950
|
clear;
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
951
|
+
_set;
|
|
952
|
+
_mapperTo;
|
|
953
|
+
_mapperFrom;
|
|
944
954
|
constructor(externalToInternal, internalToExternal) {
|
|
945
|
-
this
|
|
946
|
-
this
|
|
947
|
-
let set = this
|
|
955
|
+
this._mapperTo = externalToInternal;
|
|
956
|
+
this._mapperFrom = internalToExternal;
|
|
957
|
+
let set = this._set = /* @__PURE__ */ new Set();
|
|
948
958
|
this.clear = set.clear.bind(set);
|
|
949
959
|
}
|
|
950
960
|
get size() {
|
|
951
|
-
return this
|
|
961
|
+
return this._set.size;
|
|
952
962
|
}
|
|
953
963
|
get [Symbol.toStringTag]() {
|
|
954
|
-
return this
|
|
964
|
+
return this._set[Symbol.toStringTag];
|
|
955
965
|
}
|
|
956
966
|
add(value) {
|
|
957
|
-
this
|
|
967
|
+
this._set.add(this._mapperTo(value));
|
|
958
968
|
return this;
|
|
959
969
|
}
|
|
960
970
|
delete(value) {
|
|
961
|
-
return this
|
|
971
|
+
return this._set.delete(this._mapperTo(value));
|
|
962
972
|
}
|
|
963
973
|
forEach(cb, thisArg) {
|
|
964
|
-
this
|
|
965
|
-
let mapped = this
|
|
974
|
+
this._set.forEach((value) => {
|
|
975
|
+
let mapped = this._mapperFrom(value);
|
|
966
976
|
cb.call(thisArg, mapped, mapped, this);
|
|
967
977
|
});
|
|
968
978
|
}
|
|
969
979
|
has(value) {
|
|
970
|
-
return this
|
|
980
|
+
return this._set.has(this._mapperTo(value));
|
|
971
981
|
}
|
|
972
982
|
entries() {
|
|
973
|
-
let inner = this
|
|
983
|
+
let inner = this._set.entries();
|
|
974
984
|
const iterator = {
|
|
975
985
|
[Symbol.iterator]: () => iterator,
|
|
976
986
|
next: () => {
|
|
@@ -979,7 +989,7 @@ var CustomSet = class CustomSet {
|
|
|
979
989
|
done,
|
|
980
990
|
value
|
|
981
991
|
};
|
|
982
|
-
let mapped = this
|
|
992
|
+
let mapped = this._mapperFrom(value[0]);
|
|
983
993
|
return {
|
|
984
994
|
done,
|
|
985
995
|
value: [mapped, mapped]
|
|
@@ -989,7 +999,7 @@ var CustomSet = class CustomSet {
|
|
|
989
999
|
return maybeWrapIterator(iterator);
|
|
990
1000
|
}
|
|
991
1001
|
keys() {
|
|
992
|
-
let inner = this
|
|
1002
|
+
let inner = this._set.keys();
|
|
993
1003
|
const iterator = {
|
|
994
1004
|
[Symbol.iterator]: () => iterator,
|
|
995
1005
|
next: () => {
|
|
@@ -1000,7 +1010,7 @@ var CustomSet = class CustomSet {
|
|
|
1000
1010
|
};
|
|
1001
1011
|
return {
|
|
1002
1012
|
done,
|
|
1003
|
-
value: this
|
|
1013
|
+
value: this._mapperFrom(value)
|
|
1004
1014
|
};
|
|
1005
1015
|
}
|
|
1006
1016
|
};
|
|
@@ -1010,31 +1020,31 @@ var CustomSet = class CustomSet {
|
|
|
1010
1020
|
return this.keys();
|
|
1011
1021
|
}
|
|
1012
1022
|
union(other) {
|
|
1013
|
-
let newSet = new CustomSet((k) => this
|
|
1014
|
-
this
|
|
1023
|
+
let newSet = new CustomSet((k) => this._mapperTo(k), (k) => this._mapperFrom(k));
|
|
1024
|
+
this._set.forEach((v) => newSet.add(this._mapperFrom(v)));
|
|
1015
1025
|
other.forEach((v) => newSet.add(v));
|
|
1016
1026
|
return newSet;
|
|
1017
1027
|
}
|
|
1018
1028
|
intersection(other) {
|
|
1019
|
-
let newSet = new CustomSet((k) => this
|
|
1020
|
-
this
|
|
1021
|
-
let ext = this
|
|
1029
|
+
let newSet = new CustomSet((k) => this._mapperTo(k), (k) => this._mapperFrom(k));
|
|
1030
|
+
this._set.forEach((v) => {
|
|
1031
|
+
let ext = this._mapperFrom(v);
|
|
1022
1032
|
if (other.has(ext)) newSet.add(ext);
|
|
1023
1033
|
});
|
|
1024
1034
|
return newSet;
|
|
1025
1035
|
}
|
|
1026
1036
|
difference(other) {
|
|
1027
|
-
let newSet = new CustomSet(this
|
|
1028
|
-
this
|
|
1029
|
-
let ext = this
|
|
1037
|
+
let newSet = new CustomSet(this._mapperTo, this._mapperFrom);
|
|
1038
|
+
this._set.forEach((v) => {
|
|
1039
|
+
let ext = this._mapperFrom(v);
|
|
1030
1040
|
if (!other.has(ext)) newSet.add(ext);
|
|
1031
1041
|
});
|
|
1032
1042
|
return newSet;
|
|
1033
1043
|
}
|
|
1034
1044
|
symmetricDifference(other) {
|
|
1035
|
-
let newSet = new CustomSet((k) => this
|
|
1036
|
-
this
|
|
1037
|
-
let ext = this
|
|
1045
|
+
let newSet = new CustomSet((k) => this._mapperTo(k), (k) => this._mapperFrom(k));
|
|
1046
|
+
this._set.forEach((v) => {
|
|
1047
|
+
let ext = this._mapperFrom(v);
|
|
1038
1048
|
if (!other.has(ext)) newSet.add(ext);
|
|
1039
1049
|
});
|
|
1040
1050
|
other.forEach((v) => {
|
|
@@ -1043,7 +1053,7 @@ var CustomSet = class CustomSet {
|
|
|
1043
1053
|
return newSet;
|
|
1044
1054
|
}
|
|
1045
1055
|
isSubsetOf(other) {
|
|
1046
|
-
for (let v of this
|
|
1056
|
+
for (let v of this._set) if (!other.has(this._mapperFrom(v))) return false;
|
|
1047
1057
|
return true;
|
|
1048
1058
|
}
|
|
1049
1059
|
isSupersetOf(other) {
|
|
@@ -1058,7 +1068,7 @@ var CustomSet = class CustomSet {
|
|
|
1058
1068
|
return this.keys();
|
|
1059
1069
|
}
|
|
1060
1070
|
getInternalSet() {
|
|
1061
|
-
return this
|
|
1071
|
+
return this._set;
|
|
1062
1072
|
}
|
|
1063
1073
|
};
|
|
1064
1074
|
//#endregion
|
|
@@ -1077,7 +1087,7 @@ var Deque = class {
|
|
|
1077
1087
|
_size = 0;
|
|
1078
1088
|
constructor(array, options = {}) {
|
|
1079
1089
|
this._capacity = options.capacity;
|
|
1080
|
-
if (array) this
|
|
1090
|
+
if (array) this._fromArray(array);
|
|
1081
1091
|
else this._list = new Array(4);
|
|
1082
1092
|
}
|
|
1083
1093
|
get length() {
|
|
@@ -1101,7 +1111,7 @@ var Deque = class {
|
|
|
1101
1111
|
return this.isEmpty() ? void 0 : this._list[this._tail - 1 + this._list.length & this._capacityMask];
|
|
1102
1112
|
}
|
|
1103
1113
|
pushFront(item) {
|
|
1104
|
-
if (this._size === this._list.length) this
|
|
1114
|
+
if (this._size === this._list.length) this._growArray();
|
|
1105
1115
|
this._head = this._head - 1 + this._list.length & this._capacityMask;
|
|
1106
1116
|
this._list[this._head] = item;
|
|
1107
1117
|
this._size++;
|
|
@@ -1109,7 +1119,7 @@ var Deque = class {
|
|
|
1109
1119
|
return this._size;
|
|
1110
1120
|
}
|
|
1111
1121
|
pushBack(item) {
|
|
1112
|
-
if (this._size === this._list.length) this
|
|
1122
|
+
if (this._size === this._list.length) this._growArray();
|
|
1113
1123
|
this._list[this._tail] = item;
|
|
1114
1124
|
this._tail = this._tail + 1 & this._capacityMask;
|
|
1115
1125
|
this._size++;
|
|
@@ -1122,7 +1132,7 @@ var Deque = class {
|
|
|
1122
1132
|
this._list[this._head] = void 0;
|
|
1123
1133
|
this._head = this._head + 1 & this._capacityMask;
|
|
1124
1134
|
this._size--;
|
|
1125
|
-
if (this._size <= this._list.length / 4 && this._list.length > 4) this
|
|
1135
|
+
if (this._size <= this._list.length / 4 && this._list.length > 4) this._shrinkArray();
|
|
1126
1136
|
return item;
|
|
1127
1137
|
}
|
|
1128
1138
|
popBack() {
|
|
@@ -1131,7 +1141,7 @@ var Deque = class {
|
|
|
1131
1141
|
let item = this._list[this._tail];
|
|
1132
1142
|
this._list[this._tail] = void 0;
|
|
1133
1143
|
this._size--;
|
|
1134
|
-
if (this._size <= this._list.length / 4 && this._list.length > 4) this
|
|
1144
|
+
if (this._size <= this._list.length / 4 && this._list.length > 4) this._shrinkArray();
|
|
1135
1145
|
return item;
|
|
1136
1146
|
}
|
|
1137
1147
|
removeOne(idx) {
|
|
@@ -1140,7 +1150,7 @@ var Deque = class {
|
|
|
1140
1150
|
if (idx < 0) idx += len;
|
|
1141
1151
|
let realIdx = this._head + idx & this._capacityMask;
|
|
1142
1152
|
let item = this._list[realIdx];
|
|
1143
|
-
this
|
|
1153
|
+
this._remove(realIdx);
|
|
1144
1154
|
return item;
|
|
1145
1155
|
}
|
|
1146
1156
|
removeBy(predicate) {
|
|
@@ -1148,7 +1158,7 @@ var Deque = class {
|
|
|
1148
1158
|
let i = this._head + pos & this._capacityMask;
|
|
1149
1159
|
let item = this._list[i];
|
|
1150
1160
|
if (item !== void 0 && predicate(item)) {
|
|
1151
|
-
this
|
|
1161
|
+
this._remove(i);
|
|
1152
1162
|
return;
|
|
1153
1163
|
}
|
|
1154
1164
|
}
|
|
@@ -1196,7 +1206,7 @@ var Deque = class {
|
|
|
1196
1206
|
};
|
|
1197
1207
|
} };
|
|
1198
1208
|
}
|
|
1199
|
-
|
|
1209
|
+
_fromArray(array) {
|
|
1200
1210
|
let start = 0;
|
|
1201
1211
|
let length = array.length;
|
|
1202
1212
|
if (this._capacity !== void 0 && length > this._capacity) {
|
|
@@ -1211,7 +1221,7 @@ var Deque = class {
|
|
|
1211
1221
|
this._size = length;
|
|
1212
1222
|
for (let i = 0; i < length; i++) this._list[i] = array[start + i];
|
|
1213
1223
|
}
|
|
1214
|
-
|
|
1224
|
+
_growArray() {
|
|
1215
1225
|
let oldMask = this._capacityMask;
|
|
1216
1226
|
let oldList = this._list;
|
|
1217
1227
|
let oldHead = this._head;
|
|
@@ -1223,7 +1233,7 @@ var Deque = class {
|
|
|
1223
1233
|
this._tail = size;
|
|
1224
1234
|
this._capacityMask = newList.length - 1;
|
|
1225
1235
|
}
|
|
1226
|
-
|
|
1236
|
+
_shrinkArray() {
|
|
1227
1237
|
if (this._list.length <= 4) return;
|
|
1228
1238
|
let oldMask = this._capacityMask;
|
|
1229
1239
|
let oldList = this._list;
|
|
@@ -1236,7 +1246,7 @@ var Deque = class {
|
|
|
1236
1246
|
this._tail = size;
|
|
1237
1247
|
this._capacityMask = newList.length - 1;
|
|
1238
1248
|
}
|
|
1239
|
-
|
|
1249
|
+
_remove(idx) {
|
|
1240
1250
|
let mask = this._capacityMask;
|
|
1241
1251
|
let len = this._list.length;
|
|
1242
1252
|
let distFromHead = idx - this._head & mask;
|
|
@@ -1261,92 +1271,92 @@ var Deque = class {
|
|
|
1261
1271
|
this._tail = last;
|
|
1262
1272
|
}
|
|
1263
1273
|
this._size--;
|
|
1264
|
-
if (this._size <= this._list.length / 4 && this._list.length > 4) this
|
|
1274
|
+
if (this._size <= this._list.length / 4 && this._list.length > 4) this._shrinkArray();
|
|
1265
1275
|
}
|
|
1266
1276
|
};
|
|
1267
1277
|
//#endregion
|
|
1268
1278
|
//#region src/structures/lru-map.ts
|
|
1269
1279
|
var LruMap = class {
|
|
1270
|
-
|
|
1271
|
-
|
|
1280
|
+
_capacity;
|
|
1281
|
+
_map;
|
|
1272
1282
|
constructor(capacity, MapImpl = Map) {
|
|
1273
|
-
this
|
|
1274
|
-
this
|
|
1283
|
+
this._capacity = capacity;
|
|
1284
|
+
this._map = new MapImpl();
|
|
1275
1285
|
}
|
|
1276
1286
|
get size() {
|
|
1277
|
-
return this
|
|
1287
|
+
return this._map.size;
|
|
1278
1288
|
}
|
|
1279
1289
|
get(key) {
|
|
1280
|
-
if (!this
|
|
1281
|
-
let value = this
|
|
1282
|
-
this
|
|
1283
|
-
this
|
|
1290
|
+
if (!this._map.has(key)) return void 0;
|
|
1291
|
+
let value = this._map.get(key);
|
|
1292
|
+
this._map.delete(key);
|
|
1293
|
+
this._map.set(key, value);
|
|
1284
1294
|
return value;
|
|
1285
1295
|
}
|
|
1286
1296
|
has(key) {
|
|
1287
|
-
return this
|
|
1297
|
+
return this._map.has(key);
|
|
1288
1298
|
}
|
|
1289
1299
|
set(key, value) {
|
|
1290
|
-
if (this
|
|
1291
|
-
this
|
|
1292
|
-
if (this
|
|
1293
|
-
let oldest = this
|
|
1294
|
-
if (!oldest.done) this
|
|
1300
|
+
if (this._map.has(key)) this._map.delete(key);
|
|
1301
|
+
this._map.set(key, value);
|
|
1302
|
+
if (this._map.size > this._capacity) {
|
|
1303
|
+
let oldest = this._map.keys().next();
|
|
1304
|
+
if (!oldest.done) this._map.delete(oldest.value);
|
|
1295
1305
|
}
|
|
1296
1306
|
}
|
|
1297
1307
|
delete(key) {
|
|
1298
|
-
this
|
|
1308
|
+
this._map.delete(key);
|
|
1299
1309
|
}
|
|
1300
1310
|
clear() {
|
|
1301
|
-
this
|
|
1311
|
+
this._map.clear();
|
|
1302
1312
|
}
|
|
1303
1313
|
*[Symbol.iterator]() {
|
|
1304
|
-
yield* this
|
|
1314
|
+
yield* this._map;
|
|
1305
1315
|
}
|
|
1306
1316
|
entries() {
|
|
1307
|
-
return this
|
|
1317
|
+
return this._map.entries();
|
|
1308
1318
|
}
|
|
1309
1319
|
keys() {
|
|
1310
|
-
return this
|
|
1320
|
+
return this._map.keys();
|
|
1311
1321
|
}
|
|
1312
1322
|
values() {
|
|
1313
|
-
return this
|
|
1323
|
+
return this._map.values();
|
|
1314
1324
|
}
|
|
1315
1325
|
};
|
|
1316
1326
|
//#endregion
|
|
1317
1327
|
//#region src/structures/lru-set.ts
|
|
1318
1328
|
var LruSet = class {
|
|
1319
|
-
|
|
1320
|
-
|
|
1329
|
+
_capacity;
|
|
1330
|
+
_set;
|
|
1321
1331
|
constructor(capacity, SetImpl = Set) {
|
|
1322
|
-
this
|
|
1323
|
-
this
|
|
1332
|
+
this._capacity = capacity;
|
|
1333
|
+
this._set = new SetImpl();
|
|
1324
1334
|
}
|
|
1325
1335
|
get size() {
|
|
1326
|
-
return this
|
|
1336
|
+
return this._set.size;
|
|
1327
1337
|
}
|
|
1328
1338
|
add(value) {
|
|
1329
|
-
if (this
|
|
1330
|
-
this
|
|
1331
|
-
if (this
|
|
1332
|
-
let oldest = this
|
|
1333
|
-
if (!oldest.done) this
|
|
1339
|
+
if (this._set.has(value)) this._set.delete(value);
|
|
1340
|
+
this._set.add(value);
|
|
1341
|
+
if (this._set.size > this._capacity) {
|
|
1342
|
+
let oldest = this._set.keys().next();
|
|
1343
|
+
if (!oldest.done) this._set.delete(oldest.value);
|
|
1334
1344
|
}
|
|
1335
1345
|
}
|
|
1336
1346
|
has(value) {
|
|
1337
|
-
return this
|
|
1347
|
+
return this._set.has(value);
|
|
1338
1348
|
}
|
|
1339
1349
|
delete(value) {
|
|
1340
|
-
return this
|
|
1350
|
+
return this._set.delete(value);
|
|
1341
1351
|
}
|
|
1342
1352
|
clear() {
|
|
1343
|
-
this
|
|
1353
|
+
this._set.clear();
|
|
1344
1354
|
}
|
|
1345
1355
|
*[Symbol.iterator]() {
|
|
1346
|
-
yield* this
|
|
1356
|
+
yield* this._set;
|
|
1347
1357
|
}
|
|
1348
1358
|
toArray() {
|
|
1349
|
-
return Array.from(this
|
|
1359
|
+
return Array.from(this._set);
|
|
1350
1360
|
}
|
|
1351
1361
|
};
|
|
1352
1362
|
//#endregion
|
|
@@ -1354,16 +1364,16 @@ var LruSet = class {
|
|
|
1354
1364
|
var AsyncLock = class {
|
|
1355
1365
|
_queue = new Deque();
|
|
1356
1366
|
async acquire() {
|
|
1357
|
-
let info;
|
|
1358
|
-
while (info = this._queue.peekFront()) await info[0];
|
|
1359
1367
|
let unlock;
|
|
1360
|
-
|
|
1368
|
+
let promise = new Promise((resolve) => {
|
|
1361
1369
|
unlock = resolve;
|
|
1362
1370
|
});
|
|
1371
|
+
let prev = this._queue.peekBack();
|
|
1363
1372
|
this._queue.pushBack([promise, unlock]);
|
|
1373
|
+
if (prev) await prev[0];
|
|
1364
1374
|
}
|
|
1365
1375
|
release() {
|
|
1366
|
-
|
|
1376
|
+
let front = this._queue.popFront();
|
|
1367
1377
|
if (!front) throw new Error("Nothing to release.", { cause: this._queue });
|
|
1368
1378
|
front[1]();
|
|
1369
1379
|
}
|
|
@@ -1394,13 +1404,13 @@ var Deferred = class {
|
|
|
1394
1404
|
var DeferredTracked = class {
|
|
1395
1405
|
promise;
|
|
1396
1406
|
status;
|
|
1397
|
-
|
|
1398
|
-
|
|
1407
|
+
_resolve;
|
|
1408
|
+
_reject;
|
|
1399
1409
|
constructor() {
|
|
1400
1410
|
this.status = { type: "pending" };
|
|
1401
1411
|
this.promise = new Promise((resolve, reject) => {
|
|
1402
|
-
this
|
|
1403
|
-
this
|
|
1412
|
+
this._resolve = resolve;
|
|
1413
|
+
this._reject = reject;
|
|
1404
1414
|
});
|
|
1405
1415
|
}
|
|
1406
1416
|
get result() {
|
|
@@ -1415,7 +1425,7 @@ var DeferredTracked = class {
|
|
|
1415
1425
|
type: "fulfilled",
|
|
1416
1426
|
value
|
|
1417
1427
|
};
|
|
1418
|
-
this
|
|
1428
|
+
this._resolve(value);
|
|
1419
1429
|
}
|
|
1420
1430
|
reject(reason) {
|
|
1421
1431
|
if (this.status.type !== "pending") return;
|
|
@@ -1423,7 +1433,7 @@ var DeferredTracked = class {
|
|
|
1423
1433
|
type: "rejected",
|
|
1424
1434
|
reason
|
|
1425
1435
|
};
|
|
1426
|
-
this
|
|
1436
|
+
this._reject(reason);
|
|
1427
1437
|
}
|
|
1428
1438
|
};
|
|
1429
1439
|
//#endregion
|
|
@@ -1431,9 +1441,9 @@ var DeferredTracked = class {
|
|
|
1431
1441
|
var AsyncQueue = class {
|
|
1432
1442
|
queue;
|
|
1433
1443
|
maxSize;
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1444
|
+
_consumerWaiters = new Deque();
|
|
1445
|
+
_producerWaiters = new Deque();
|
|
1446
|
+
_ended = false;
|
|
1437
1447
|
constructor(from, maxSize) {
|
|
1438
1448
|
if (maxSize !== void 0 && maxSize < 1) throw new Error("maxSize must be at least 1");
|
|
1439
1449
|
this.maxSize = maxSize;
|
|
@@ -1452,13 +1462,16 @@ var AsyncQueue = class {
|
|
|
1452
1462
|
return this.maxSize !== void 0 ? this.maxSize - this.queue.length : Infinity;
|
|
1453
1463
|
}
|
|
1454
1464
|
get ended() {
|
|
1455
|
-
return this
|
|
1465
|
+
return this._ended;
|
|
1456
1466
|
}
|
|
1457
1467
|
async enqueue(item) {
|
|
1458
|
-
if (this
|
|
1468
|
+
if (this._ended) throw new Error("Cannot enqueue after .end() has been called");
|
|
1459
1469
|
while (true) {
|
|
1460
|
-
if (this
|
|
1461
|
-
this
|
|
1470
|
+
if (this._consumerWaiters.length > 0) {
|
|
1471
|
+
this._consumerWaiters.popFront().resolve({
|
|
1472
|
+
done: false,
|
|
1473
|
+
value: item
|
|
1474
|
+
});
|
|
1462
1475
|
return;
|
|
1463
1476
|
}
|
|
1464
1477
|
if (!this.isFull) {
|
|
@@ -1466,28 +1479,31 @@ var AsyncQueue = class {
|
|
|
1466
1479
|
return;
|
|
1467
1480
|
}
|
|
1468
1481
|
let waiter = new Deferred();
|
|
1469
|
-
this
|
|
1482
|
+
this._producerWaiters.pushBack(waiter);
|
|
1470
1483
|
await waiter.promise;
|
|
1471
|
-
if (this
|
|
1484
|
+
if (this._ended) throw new Error("Queue was ended while waiting to enqueue.");
|
|
1472
1485
|
}
|
|
1473
1486
|
}
|
|
1474
1487
|
tryEnqueue(item) {
|
|
1475
|
-
if (this
|
|
1476
|
-
if (this
|
|
1477
|
-
this
|
|
1488
|
+
if (this._ended || this.isFull) return false;
|
|
1489
|
+
if (this._consumerWaiters.length > 0) {
|
|
1490
|
+
this._consumerWaiters.popFront().resolve({
|
|
1491
|
+
done: false,
|
|
1492
|
+
value: item
|
|
1493
|
+
});
|
|
1478
1494
|
return true;
|
|
1479
1495
|
}
|
|
1480
1496
|
this.queue.pushBack(item);
|
|
1481
1497
|
return true;
|
|
1482
1498
|
}
|
|
1483
1499
|
end() {
|
|
1484
|
-
if (this
|
|
1485
|
-
this
|
|
1486
|
-
for (let waiter of this
|
|
1487
|
-
this
|
|
1500
|
+
if (this._ended) throw new Error(".end() has already been called.");
|
|
1501
|
+
this._ended = true;
|
|
1502
|
+
for (let waiter of this._consumerWaiters) waiter.resolve({ done: true });
|
|
1503
|
+
this._consumerWaiters.clear();
|
|
1488
1504
|
let err = /* @__PURE__ */ new Error("Queue has been ended.");
|
|
1489
|
-
for (let waiter of this
|
|
1490
|
-
this
|
|
1505
|
+
for (let waiter of this._producerWaiters) waiter.reject(err);
|
|
1506
|
+
this._producerWaiters.clear();
|
|
1491
1507
|
}
|
|
1492
1508
|
peek() {
|
|
1493
1509
|
return this.queue.peekFront();
|
|
@@ -1495,64 +1511,77 @@ var AsyncQueue = class {
|
|
|
1495
1511
|
next() {
|
|
1496
1512
|
if (this.queue.length === 0) return void 0;
|
|
1497
1513
|
let item = this.queue.popFront();
|
|
1498
|
-
this
|
|
1514
|
+
this._wakeProducerIfNeeded();
|
|
1499
1515
|
return item;
|
|
1500
1516
|
}
|
|
1501
1517
|
async nextOrWait() {
|
|
1502
1518
|
if (this.queue.length > 0) {
|
|
1503
1519
|
let item = this.queue.popFront();
|
|
1504
|
-
this
|
|
1520
|
+
this._wakeProducerIfNeeded();
|
|
1505
1521
|
return item;
|
|
1506
1522
|
}
|
|
1507
|
-
if (this
|
|
1508
|
-
let
|
|
1509
|
-
|
|
1510
|
-
return
|
|
1523
|
+
if (this._ended) return;
|
|
1524
|
+
let result = await this._waitForItem();
|
|
1525
|
+
if (result.done) return void 0;
|
|
1526
|
+
return result.value;
|
|
1511
1527
|
}
|
|
1512
1528
|
[Symbol.asyncIterator]() {
|
|
1513
1529
|
let iterator = {
|
|
1514
1530
|
[Symbol.asyncIterator]: () => iterator,
|
|
1515
1531
|
next: async () => {
|
|
1516
|
-
|
|
1517
|
-
|
|
1532
|
+
if (this.queue.length > 0) return {
|
|
1533
|
+
value: this.next(),
|
|
1534
|
+
done: false
|
|
1535
|
+
};
|
|
1536
|
+
if (this._ended) return {
|
|
1537
|
+
done: true,
|
|
1538
|
+
value: void 0
|
|
1539
|
+
};
|
|
1540
|
+
let result = await this._waitForItem();
|
|
1541
|
+
if (result.done) return {
|
|
1518
1542
|
done: true,
|
|
1519
1543
|
value: void 0
|
|
1520
1544
|
};
|
|
1521
1545
|
return {
|
|
1522
|
-
value:
|
|
1546
|
+
value: result.value,
|
|
1523
1547
|
done: false
|
|
1524
1548
|
};
|
|
1525
1549
|
}
|
|
1526
1550
|
};
|
|
1527
1551
|
return iterator;
|
|
1528
1552
|
}
|
|
1529
|
-
|
|
1530
|
-
|
|
1553
|
+
_waitForItem() {
|
|
1554
|
+
let waiter = new Deferred();
|
|
1555
|
+
this._consumerWaiters.pushBack(waiter);
|
|
1556
|
+
return waiter.promise;
|
|
1557
|
+
}
|
|
1558
|
+
_wakeProducerIfNeeded() {
|
|
1559
|
+
if (this._producerWaiters.length > 0 && !this.isFull) this._producerWaiters.popFront().resolve();
|
|
1531
1560
|
}
|
|
1532
1561
|
};
|
|
1533
1562
|
//#endregion
|
|
1534
1563
|
//#region src/async/emitter.ts
|
|
1535
1564
|
var Emitter = class {
|
|
1536
|
-
|
|
1537
|
-
|
|
1565
|
+
_listeners = [];
|
|
1566
|
+
_emit = noop;
|
|
1538
1567
|
get length() {
|
|
1539
|
-
return this
|
|
1568
|
+
return this._listeners.length;
|
|
1540
1569
|
}
|
|
1541
1570
|
add(listener) {
|
|
1542
|
-
this
|
|
1543
|
-
this
|
|
1571
|
+
this._listeners.push(listener);
|
|
1572
|
+
this._updateEmit();
|
|
1544
1573
|
}
|
|
1545
1574
|
forwardTo(emitter) {
|
|
1546
1575
|
this.add(emitter.emit.bind(emitter));
|
|
1547
1576
|
}
|
|
1548
1577
|
remove(listener) {
|
|
1549
|
-
let idx = this
|
|
1578
|
+
let idx = this._listeners.indexOf(listener);
|
|
1550
1579
|
if (idx === -1) return;
|
|
1551
|
-
this
|
|
1552
|
-
this
|
|
1580
|
+
this._listeners.splice(idx, 1);
|
|
1581
|
+
this._updateEmit();
|
|
1553
1582
|
}
|
|
1554
1583
|
emit(value) {
|
|
1555
|
-
this
|
|
1584
|
+
this._emit(value);
|
|
1556
1585
|
}
|
|
1557
1586
|
once(listener) {
|
|
1558
1587
|
const once = (value) => {
|
|
@@ -1562,14 +1591,14 @@ var Emitter = class {
|
|
|
1562
1591
|
this.add(once);
|
|
1563
1592
|
}
|
|
1564
1593
|
listeners() {
|
|
1565
|
-
return this
|
|
1594
|
+
return this._listeners;
|
|
1566
1595
|
}
|
|
1567
1596
|
clear() {
|
|
1568
|
-
this
|
|
1569
|
-
this
|
|
1597
|
+
this._listeners.length = 0;
|
|
1598
|
+
this._emit = noop;
|
|
1570
1599
|
}
|
|
1571
|
-
|
|
1572
|
-
let listeners = this
|
|
1600
|
+
_emitFew = (value) => {
|
|
1601
|
+
let listeners = this._listeners.slice();
|
|
1573
1602
|
let len = listeners.length;
|
|
1574
1603
|
listeners[0](value);
|
|
1575
1604
|
len > 1 && listeners[1](value);
|
|
@@ -1577,29 +1606,29 @@ var Emitter = class {
|
|
|
1577
1606
|
len > 3 && listeners[3](value);
|
|
1578
1607
|
len > 4 && listeners[4](value);
|
|
1579
1608
|
};
|
|
1580
|
-
|
|
1581
|
-
let listeners = this
|
|
1609
|
+
_emitAll = (value) => {
|
|
1610
|
+
let listeners = this._listeners.slice();
|
|
1582
1611
|
for (let i = 0; i < listeners.length; i++) listeners[i](value);
|
|
1583
1612
|
};
|
|
1584
|
-
|
|
1585
|
-
let len = this
|
|
1586
|
-
if (len === 0) this
|
|
1587
|
-
else if (len <= 5) this
|
|
1588
|
-
else this
|
|
1613
|
+
_updateEmit = () => {
|
|
1614
|
+
let len = this._listeners.length;
|
|
1615
|
+
if (len === 0) this._emit = noop;
|
|
1616
|
+
else if (len <= 5) this._emit = this._emitFew;
|
|
1617
|
+
else this._emit = this._emitAll;
|
|
1589
1618
|
};
|
|
1590
1619
|
};
|
|
1591
1620
|
//#endregion
|
|
1592
1621
|
//#region src/async/async-resource.ts
|
|
1593
1622
|
var AsyncResource = class {
|
|
1594
1623
|
onUpdated = new Emitter();
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1624
|
+
_abort;
|
|
1625
|
+
_ctx;
|
|
1626
|
+
_updating;
|
|
1627
|
+
_timeout;
|
|
1628
|
+
_destroyed = false;
|
|
1600
1629
|
constructor(options) {
|
|
1601
1630
|
this.options = options;
|
|
1602
|
-
this
|
|
1631
|
+
this._ctx = {
|
|
1603
1632
|
current: null,
|
|
1604
1633
|
currentFetchedAt: 0,
|
|
1605
1634
|
currentExpiresAt: 0,
|
|
@@ -1608,102 +1637,102 @@ var AsyncResource = class {
|
|
|
1608
1637
|
};
|
|
1609
1638
|
}
|
|
1610
1639
|
get isStale() {
|
|
1611
|
-
return this
|
|
1640
|
+
return this._ctx.current === null || this._ctx.currentExpiresAt <= performance.now();
|
|
1612
1641
|
}
|
|
1613
1642
|
setData(data, expiresIn) {
|
|
1614
|
-
if (this
|
|
1643
|
+
if (this._destroyed) return;
|
|
1615
1644
|
let now = performance.now();
|
|
1616
|
-
this
|
|
1617
|
-
this
|
|
1618
|
-
this
|
|
1619
|
-
this.onUpdated.emit(this
|
|
1645
|
+
this._ctx.current = data;
|
|
1646
|
+
this._ctx.currentExpiresAt = now + expiresIn;
|
|
1647
|
+
this._ctx.currentFetchedAt = now;
|
|
1648
|
+
this.onUpdated.emit(this._ctx);
|
|
1620
1649
|
if (this.options.autoReload) {
|
|
1621
|
-
this
|
|
1622
|
-
this
|
|
1623
|
-
if (this
|
|
1624
|
-
this
|
|
1650
|
+
this._clearTimer();
|
|
1651
|
+
this._timeout = setTimeoutWrap(() => {
|
|
1652
|
+
if (this._destroyed) return;
|
|
1653
|
+
this._ctx.isBackground = true;
|
|
1625
1654
|
this.update(true).catch(noop);
|
|
1626
1655
|
}, expiresIn + (this.options.autoReloadAfter ?? this.options.authReloadAfter ?? 0));
|
|
1627
1656
|
}
|
|
1628
1657
|
}
|
|
1629
1658
|
async update(force = false) {
|
|
1630
|
-
if (this
|
|
1631
|
-
if (this
|
|
1632
|
-
await this
|
|
1659
|
+
if (this._destroyed) return;
|
|
1660
|
+
if (this._updating) {
|
|
1661
|
+
await this._updating.promise;
|
|
1633
1662
|
return;
|
|
1634
1663
|
}
|
|
1635
1664
|
if (!force && !this.isStale) return;
|
|
1636
|
-
this
|
|
1637
|
-
this
|
|
1638
|
-
this
|
|
1639
|
-
this
|
|
1665
|
+
this._abort?.abort();
|
|
1666
|
+
this._abort = new AbortController();
|
|
1667
|
+
this._ctx.abort = this._abort.signal;
|
|
1668
|
+
this._updating = new Deferred();
|
|
1640
1669
|
let result;
|
|
1641
1670
|
try {
|
|
1642
|
-
result = await this.options.fetcher(this
|
|
1671
|
+
result = await this.options.fetcher(this._ctx);
|
|
1643
1672
|
} catch (err) {
|
|
1644
1673
|
if (err instanceof Error && err.name === "AbortError") {
|
|
1645
|
-
this
|
|
1646
|
-
this
|
|
1647
|
-
this
|
|
1674
|
+
this._updating?.resolve();
|
|
1675
|
+
this._updating = void 0;
|
|
1676
|
+
this._ctx.abort = null;
|
|
1648
1677
|
return;
|
|
1649
1678
|
}
|
|
1650
|
-
if (this.options.onError) this.options.onError(err, this
|
|
1679
|
+
if (this.options.onError) this.options.onError(err, this._ctx);
|
|
1651
1680
|
else console.error(err);
|
|
1652
|
-
this
|
|
1653
|
-
this
|
|
1654
|
-
this
|
|
1681
|
+
this._updating?.resolve();
|
|
1682
|
+
this._updating = void 0;
|
|
1683
|
+
this._ctx.abort = null;
|
|
1655
1684
|
return;
|
|
1656
1685
|
}
|
|
1657
|
-
this
|
|
1658
|
-
this
|
|
1659
|
-
this
|
|
1660
|
-
if (!this
|
|
1686
|
+
this._updating?.resolve();
|
|
1687
|
+
this._updating = void 0;
|
|
1688
|
+
this._ctx.abort = null;
|
|
1689
|
+
if (!this._destroyed) this.setData(result.data, result.expiresIn);
|
|
1661
1690
|
}
|
|
1662
1691
|
async get() {
|
|
1663
|
-
if (this
|
|
1664
|
-
if (this.options.swr === true && this
|
|
1692
|
+
if (this._destroyed) return null;
|
|
1693
|
+
if (this.options.swr === true && this._ctx.current !== null) {
|
|
1665
1694
|
let validator = this.options.swrValidator;
|
|
1666
|
-
if (!validator || validator(this
|
|
1667
|
-
this
|
|
1695
|
+
if (!validator || validator(this._ctx)) {
|
|
1696
|
+
this._ctx.isBackground = true;
|
|
1668
1697
|
this.update(true).catch(noop);
|
|
1669
|
-
return this
|
|
1698
|
+
return this._ctx.current;
|
|
1670
1699
|
}
|
|
1671
1700
|
}
|
|
1672
|
-
this
|
|
1701
|
+
this._ctx.isBackground = false;
|
|
1673
1702
|
await this.update();
|
|
1674
|
-
return this
|
|
1703
|
+
return this._ctx.current;
|
|
1675
1704
|
}
|
|
1676
1705
|
getCached() {
|
|
1677
|
-
return this
|
|
1706
|
+
return this._ctx.current;
|
|
1678
1707
|
}
|
|
1679
1708
|
destroy() {
|
|
1680
|
-
if (this
|
|
1681
|
-
this
|
|
1682
|
-
this
|
|
1683
|
-
this
|
|
1709
|
+
if (this._destroyed) return;
|
|
1710
|
+
this._destroyed = true;
|
|
1711
|
+
this._clearTimer();
|
|
1712
|
+
this._abort?.abort();
|
|
1684
1713
|
this.onUpdated.clear();
|
|
1685
|
-
this
|
|
1686
|
-
this
|
|
1714
|
+
this._updating?.resolve();
|
|
1715
|
+
this._updating = void 0;
|
|
1687
1716
|
}
|
|
1688
|
-
|
|
1689
|
-
if (this
|
|
1690
|
-
clearTimeoutWrap(this
|
|
1691
|
-
this
|
|
1717
|
+
_clearTimer() {
|
|
1718
|
+
if (this._timeout) {
|
|
1719
|
+
clearTimeoutWrap(this._timeout);
|
|
1720
|
+
this._timeout = void 0;
|
|
1692
1721
|
}
|
|
1693
1722
|
}
|
|
1694
1723
|
};
|
|
1695
1724
|
//#endregion
|
|
1696
1725
|
//#region src/async/condition-variable.ts
|
|
1697
1726
|
var ConditionVariable = class {
|
|
1698
|
-
|
|
1727
|
+
_resolvers = [];
|
|
1699
1728
|
wait() {
|
|
1700
1729
|
return new Promise((resolve) => {
|
|
1701
|
-
this
|
|
1730
|
+
this._resolvers.push(resolve);
|
|
1702
1731
|
});
|
|
1703
1732
|
}
|
|
1704
1733
|
notify() {
|
|
1705
|
-
let resolvers = this
|
|
1706
|
-
this
|
|
1734
|
+
let resolvers = this._resolvers;
|
|
1735
|
+
this._resolvers = [];
|
|
1707
1736
|
for (let resolve of resolvers) resolve();
|
|
1708
1737
|
}
|
|
1709
1738
|
};
|
|
@@ -1784,7 +1813,7 @@ function sleep(ms, signal) {
|
|
|
1784
1813
|
}
|
|
1785
1814
|
let onAbort = () => {
|
|
1786
1815
|
clearTimeout(timeoutId);
|
|
1787
|
-
reject(signal
|
|
1816
|
+
reject(signal?.reason ?? new DOMException("The operation was aborted.", "AbortError"));
|
|
1788
1817
|
};
|
|
1789
1818
|
let timeoutId = setTimeout(() => {
|
|
1790
1819
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -1816,4 +1845,1339 @@ function throwUnreachable() {
|
|
|
1816
1845
|
throw new Error("Unreachable");
|
|
1817
1846
|
}
|
|
1818
1847
|
//#endregion
|
|
1819
|
-
|
|
1848
|
+
//#region src/compress/checksum.ts
|
|
1849
|
+
var crcTable = new Int32Array(256);
|
|
1850
|
+
for (let i = 0; i < 256; i++) {
|
|
1851
|
+
let c = i;
|
|
1852
|
+
for (let k = 0; k < 8; k++) c = (c & 1 ? -306674912 : 0) ^ c >>> 1;
|
|
1853
|
+
crcTable[i] = c;
|
|
1854
|
+
}
|
|
1855
|
+
var Crc32 = class {
|
|
1856
|
+
_c;
|
|
1857
|
+
constructor(seed = 0) {
|
|
1858
|
+
this._c = ~seed;
|
|
1859
|
+
}
|
|
1860
|
+
update(data) {
|
|
1861
|
+
let c = this._c;
|
|
1862
|
+
for (let i = 0; i < data.length; i++) c = crcTable[c & 255 ^ data[i]] ^ c >>> 8;
|
|
1863
|
+
this._c = c;
|
|
1864
|
+
return this;
|
|
1865
|
+
}
|
|
1866
|
+
digest() {
|
|
1867
|
+
return ~this._c >>> 0;
|
|
1868
|
+
}
|
|
1869
|
+
};
|
|
1870
|
+
function crc32(data, seed = 0) {
|
|
1871
|
+
return new Crc32(seed).update(data).digest();
|
|
1872
|
+
}
|
|
1873
|
+
var Adler32 = class {
|
|
1874
|
+
_a;
|
|
1875
|
+
_b;
|
|
1876
|
+
constructor(seed = 1) {
|
|
1877
|
+
this._a = seed & 65535;
|
|
1878
|
+
this._b = seed >>> 16 & 65535;
|
|
1879
|
+
}
|
|
1880
|
+
update(data) {
|
|
1881
|
+
let a = this._a;
|
|
1882
|
+
let b = this._b;
|
|
1883
|
+
let i = 0;
|
|
1884
|
+
let n = data.length;
|
|
1885
|
+
while (i < n) {
|
|
1886
|
+
let end = Math.min(i + 2654, n);
|
|
1887
|
+
for (; i < end; i++) {
|
|
1888
|
+
a += data[i];
|
|
1889
|
+
b += a;
|
|
1890
|
+
}
|
|
1891
|
+
a %= 65521;
|
|
1892
|
+
b %= 65521;
|
|
1893
|
+
}
|
|
1894
|
+
this._a = a;
|
|
1895
|
+
this._b = b;
|
|
1896
|
+
return this;
|
|
1897
|
+
}
|
|
1898
|
+
digest() {
|
|
1899
|
+
return (this._b << 16 | this._a) >>> 0;
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
function adler32(data, seed = 1) {
|
|
1903
|
+
return new Adler32(seed).update(data).digest();
|
|
1904
|
+
}
|
|
1905
|
+
//#endregion
|
|
1906
|
+
//#region src/compress/bits.ts
|
|
1907
|
+
function readBits(data, bitPos, mask) {
|
|
1908
|
+
let offset = bitPos / 8 | 0;
|
|
1909
|
+
return (data[offset] | data[offset + 1] << 8) >> (bitPos & 7) & mask;
|
|
1910
|
+
}
|
|
1911
|
+
function readBits16(data, bitPos) {
|
|
1912
|
+
let offset = bitPos / 8 | 0;
|
|
1913
|
+
return (data[offset] | data[offset + 1] << 8 | data[offset + 2] << 16) >> (bitPos & 7);
|
|
1914
|
+
}
|
|
1915
|
+
function writeBits(data, bitPos, value) {
|
|
1916
|
+
value <<= bitPos & 7;
|
|
1917
|
+
let offset = bitPos / 8 | 0;
|
|
1918
|
+
data[offset] |= value;
|
|
1919
|
+
data[offset + 1] |= value >> 8;
|
|
1920
|
+
}
|
|
1921
|
+
function writeBits16(data, bitPos, value) {
|
|
1922
|
+
value <<= bitPos & 7;
|
|
1923
|
+
let offset = bitPos / 8 | 0;
|
|
1924
|
+
data[offset] |= value;
|
|
1925
|
+
data[offset + 1] |= value >> 8;
|
|
1926
|
+
data[offset + 2] |= value >> 16;
|
|
1927
|
+
}
|
|
1928
|
+
function byteCeil(bitPos) {
|
|
1929
|
+
return (bitPos + 7) / 8 | 0;
|
|
1930
|
+
}
|
|
1931
|
+
//#endregion
|
|
1932
|
+
//#region src/compress/errors.ts
|
|
1933
|
+
var FlateError = class extends Error {
|
|
1934
|
+
constructor(message, options) {
|
|
1935
|
+
super(message, options);
|
|
1936
|
+
this.name = new.target.name;
|
|
1937
|
+
}
|
|
1938
|
+
};
|
|
1939
|
+
var UnexpectedEofError = class extends FlateError {
|
|
1940
|
+
constructor(options) {
|
|
1941
|
+
super("Unexpected end of DEFLATE stream.", options);
|
|
1942
|
+
}
|
|
1943
|
+
};
|
|
1944
|
+
var InvalidBlockTypeError = class extends FlateError {
|
|
1945
|
+
constructor(options) {
|
|
1946
|
+
super("Invalid DEFLATE block type.", options);
|
|
1947
|
+
}
|
|
1948
|
+
};
|
|
1949
|
+
var InvalidLengthLiteralError = class extends FlateError {
|
|
1950
|
+
constructor(options) {
|
|
1951
|
+
super("Invalid DEFLATE length or literal symbol.", options);
|
|
1952
|
+
}
|
|
1953
|
+
};
|
|
1954
|
+
var InvalidDistanceError = class extends FlateError {
|
|
1955
|
+
constructor(options) {
|
|
1956
|
+
super("Invalid DEFLATE distance.", options);
|
|
1957
|
+
}
|
|
1958
|
+
};
|
|
1959
|
+
var InvalidHeaderError = class extends FlateError {
|
|
1960
|
+
constructor(message = "Invalid compressed stream header.", options) {
|
|
1961
|
+
super(message, options);
|
|
1962
|
+
}
|
|
1963
|
+
};
|
|
1964
|
+
var StreamFinishedError = class extends FlateError {
|
|
1965
|
+
constructor(options) {
|
|
1966
|
+
super("Stream already finished.", options);
|
|
1967
|
+
}
|
|
1968
|
+
};
|
|
1969
|
+
var ChecksumMismatchError = class extends FlateError {
|
|
1970
|
+
constructor(options) {
|
|
1971
|
+
super("Checksum mismatch.", options);
|
|
1972
|
+
}
|
|
1973
|
+
};
|
|
1974
|
+
//#endregion
|
|
1975
|
+
//#region src/compress/tables.ts
|
|
1976
|
+
var fixedLengthExtraBits = new Uint8Array([
|
|
1977
|
+
0,
|
|
1978
|
+
0,
|
|
1979
|
+
0,
|
|
1980
|
+
0,
|
|
1981
|
+
0,
|
|
1982
|
+
0,
|
|
1983
|
+
0,
|
|
1984
|
+
0,
|
|
1985
|
+
1,
|
|
1986
|
+
1,
|
|
1987
|
+
1,
|
|
1988
|
+
1,
|
|
1989
|
+
2,
|
|
1990
|
+
2,
|
|
1991
|
+
2,
|
|
1992
|
+
2,
|
|
1993
|
+
3,
|
|
1994
|
+
3,
|
|
1995
|
+
3,
|
|
1996
|
+
3,
|
|
1997
|
+
4,
|
|
1998
|
+
4,
|
|
1999
|
+
4,
|
|
2000
|
+
4,
|
|
2001
|
+
5,
|
|
2002
|
+
5,
|
|
2003
|
+
5,
|
|
2004
|
+
5,
|
|
2005
|
+
0,
|
|
2006
|
+
0,
|
|
2007
|
+
0
|
|
2008
|
+
]);
|
|
2009
|
+
var fixedDistanceExtraBits = new Uint8Array([
|
|
2010
|
+
0,
|
|
2011
|
+
0,
|
|
2012
|
+
0,
|
|
2013
|
+
0,
|
|
2014
|
+
1,
|
|
2015
|
+
1,
|
|
2016
|
+
2,
|
|
2017
|
+
2,
|
|
2018
|
+
3,
|
|
2019
|
+
3,
|
|
2020
|
+
4,
|
|
2021
|
+
4,
|
|
2022
|
+
5,
|
|
2023
|
+
5,
|
|
2024
|
+
6,
|
|
2025
|
+
6,
|
|
2026
|
+
7,
|
|
2027
|
+
7,
|
|
2028
|
+
8,
|
|
2029
|
+
8,
|
|
2030
|
+
9,
|
|
2031
|
+
9,
|
|
2032
|
+
10,
|
|
2033
|
+
10,
|
|
2034
|
+
11,
|
|
2035
|
+
11,
|
|
2036
|
+
12,
|
|
2037
|
+
12,
|
|
2038
|
+
13,
|
|
2039
|
+
13,
|
|
2040
|
+
0,
|
|
2041
|
+
0
|
|
2042
|
+
]);
|
|
2043
|
+
var codeLengthOrder = new Uint8Array([
|
|
2044
|
+
16,
|
|
2045
|
+
17,
|
|
2046
|
+
18,
|
|
2047
|
+
0,
|
|
2048
|
+
8,
|
|
2049
|
+
7,
|
|
2050
|
+
9,
|
|
2051
|
+
6,
|
|
2052
|
+
10,
|
|
2053
|
+
5,
|
|
2054
|
+
11,
|
|
2055
|
+
4,
|
|
2056
|
+
12,
|
|
2057
|
+
3,
|
|
2058
|
+
13,
|
|
2059
|
+
2,
|
|
2060
|
+
14,
|
|
2061
|
+
1,
|
|
2062
|
+
15
|
|
2063
|
+
]);
|
|
2064
|
+
function basesFromExtra(extraBits, start) {
|
|
2065
|
+
let bases = new Uint16Array(31);
|
|
2066
|
+
for (let i = 0; i < 31; i++) {
|
|
2067
|
+
start += 1 << (i === 0 ? 0 : extraBits[i - 1]);
|
|
2068
|
+
bases[i] = start;
|
|
2069
|
+
}
|
|
2070
|
+
let reverse = new Int32Array(bases[30]);
|
|
2071
|
+
for (let i = 1; i < 30; i++) for (let value = bases[i]; value < bases[i + 1]; value++) reverse[value] = value - bases[i] << 5 | i;
|
|
2072
|
+
return {
|
|
2073
|
+
bases,
|
|
2074
|
+
reverse
|
|
2075
|
+
};
|
|
2076
|
+
}
|
|
2077
|
+
var lengthTables = basesFromExtra(fixedLengthExtraBits, 2);
|
|
2078
|
+
lengthTables.bases[28] = 258;
|
|
2079
|
+
lengthTables.reverse[258] = 28;
|
|
2080
|
+
var lengthBase = lengthTables.bases;
|
|
2081
|
+
var lengthReverse = lengthTables.reverse;
|
|
2082
|
+
var distanceTables = basesFromExtra(fixedDistanceExtraBits, 0);
|
|
2083
|
+
var distanceBase = distanceTables.bases;
|
|
2084
|
+
var distanceReverse = distanceTables.reverse;
|
|
2085
|
+
var reverseBits15 = new Uint16Array(32768);
|
|
2086
|
+
for (let i = 0; i < 32768; i++) {
|
|
2087
|
+
let x = (i & 43690) >> 1 | (i & 21845) << 1;
|
|
2088
|
+
x = (x & 52428) >> 2 | (x & 13107) << 2;
|
|
2089
|
+
x = (x & 61680) >> 4 | (x & 3855) << 4;
|
|
2090
|
+
reverseBits15[i] = ((x & 65280) >> 8 | (x & 255) << 8) >> 1;
|
|
2091
|
+
}
|
|
2092
|
+
var fixedLiteralLengths = new Uint8Array(288);
|
|
2093
|
+
for (let i = 0; i < 144; i++) fixedLiteralLengths[i] = 8;
|
|
2094
|
+
for (let i = 144; i < 256; i++) fixedLiteralLengths[i] = 9;
|
|
2095
|
+
for (let i = 256; i < 280; i++) fixedLiteralLengths[i] = 7;
|
|
2096
|
+
for (let i = 280; i < 288; i++) fixedLiteralLengths[i] = 8;
|
|
2097
|
+
var fixedDistanceLengths = new Uint8Array(32);
|
|
2098
|
+
fixedDistanceLengths.fill(5);
|
|
2099
|
+
//#endregion
|
|
2100
|
+
//#region src/compress/huffman.ts
|
|
2101
|
+
function buildEncodeMap(lengths, maxBits) {
|
|
2102
|
+
return buildCodeMap(lengths, maxBits, false);
|
|
2103
|
+
}
|
|
2104
|
+
function buildDecodeMap(lengths, maxBits) {
|
|
2105
|
+
return buildCodeMap(lengths, maxBits, true);
|
|
2106
|
+
}
|
|
2107
|
+
function buildCodeMap(lengths, maxBits, decode) {
|
|
2108
|
+
let count = new Uint16Array(maxBits);
|
|
2109
|
+
for (let i = 0; i < lengths.length; i++) if (lengths[i]) count[lengths[i] - 1]++;
|
|
2110
|
+
let nextCode = new Uint16Array(maxBits);
|
|
2111
|
+
for (let i = 1; i < maxBits; i++) nextCode[i] = nextCode[i - 1] + count[i - 1] << 1;
|
|
2112
|
+
if (decode) {
|
|
2113
|
+
let map = new Uint16Array(1 << maxBits);
|
|
2114
|
+
let drop = 15 - maxBits;
|
|
2115
|
+
for (let symbol = 0; symbol < lengths.length; symbol++) {
|
|
2116
|
+
let bits = lengths[symbol];
|
|
2117
|
+
if (!bits) continue;
|
|
2118
|
+
let packed = symbol << 4 | bits;
|
|
2119
|
+
let pad = maxBits - bits;
|
|
2120
|
+
let value = nextCode[bits - 1]++ << pad;
|
|
2121
|
+
let last = value | (1 << pad) - 1;
|
|
2122
|
+
for (; value <= last; value++) map[reverseBits15[value] >> drop] = packed;
|
|
2123
|
+
}
|
|
2124
|
+
return map;
|
|
2125
|
+
}
|
|
2126
|
+
let map = new Uint16Array(lengths.length);
|
|
2127
|
+
for (let symbol = 0; symbol < lengths.length; symbol++) {
|
|
2128
|
+
let bits = lengths[symbol];
|
|
2129
|
+
if (bits) map[symbol] = reverseBits15[nextCode[bits - 1]++] >> 15 - bits;
|
|
2130
|
+
}
|
|
2131
|
+
return map;
|
|
2132
|
+
}
|
|
2133
|
+
function buildLengthLimitedTree(freqs, maxBits) {
|
|
2134
|
+
let nodes = [];
|
|
2135
|
+
for (let i = 0; i < freqs.length; i++) if (freqs[i]) nodes.push({
|
|
2136
|
+
symbol: i,
|
|
2137
|
+
freq: freqs[i]
|
|
2138
|
+
});
|
|
2139
|
+
let n = nodes.length;
|
|
2140
|
+
let leaves = nodes.slice();
|
|
2141
|
+
if (!n) return {
|
|
2142
|
+
lengths: new Uint8Array(0),
|
|
2143
|
+
maxBits: 0
|
|
2144
|
+
};
|
|
2145
|
+
if (n === 1) {
|
|
2146
|
+
let lengths = new Uint8Array(nodes[0].symbol + 1);
|
|
2147
|
+
lengths[nodes[0].symbol] = 1;
|
|
2148
|
+
return {
|
|
2149
|
+
lengths,
|
|
2150
|
+
maxBits: 1
|
|
2151
|
+
};
|
|
2152
|
+
}
|
|
2153
|
+
nodes.sort((a, b) => a.freq - b.freq);
|
|
2154
|
+
nodes.push({
|
|
2155
|
+
symbol: -1,
|
|
2156
|
+
freq: 25001
|
|
2157
|
+
});
|
|
2158
|
+
let left = nodes[0];
|
|
2159
|
+
let right = nodes[1];
|
|
2160
|
+
let lookbehind = 0;
|
|
2161
|
+
let write = 1;
|
|
2162
|
+
let lookahead = 2;
|
|
2163
|
+
nodes[0] = {
|
|
2164
|
+
symbol: -1,
|
|
2165
|
+
freq: left.freq + right.freq,
|
|
2166
|
+
left,
|
|
2167
|
+
right
|
|
2168
|
+
};
|
|
2169
|
+
while (write !== n - 1) {
|
|
2170
|
+
left = nodes[nodes[lookbehind].freq < nodes[lookahead].freq ? lookbehind++ : lookahead++];
|
|
2171
|
+
right = nodes[lookbehind !== write && nodes[lookbehind].freq < nodes[lookahead].freq ? lookbehind++ : lookahead++];
|
|
2172
|
+
nodes[write++] = {
|
|
2173
|
+
symbol: -1,
|
|
2174
|
+
freq: left.freq + right.freq,
|
|
2175
|
+
left,
|
|
2176
|
+
right
|
|
2177
|
+
};
|
|
2178
|
+
}
|
|
2179
|
+
let maxSymbol = leaves[0].symbol;
|
|
2180
|
+
for (let i = 1; i < n; i++) if (leaves[i].symbol > maxSymbol) maxSymbol = leaves[i].symbol;
|
|
2181
|
+
let bitLengths = new Uint16Array(maxSymbol + 1);
|
|
2182
|
+
let treeBits = assignLengths(nodes[write - 1], bitLengths, 0);
|
|
2183
|
+
if (treeBits > maxBits) {
|
|
2184
|
+
let debt = 0;
|
|
2185
|
+
let overflow = treeBits - maxBits;
|
|
2186
|
+
let cost = 1 << overflow;
|
|
2187
|
+
leaves.sort((a, b) => bitLengths[b.symbol] - bitLengths[a.symbol] || a.freq - b.freq);
|
|
2188
|
+
let i = 0;
|
|
2189
|
+
for (; i < n; i++) {
|
|
2190
|
+
let symbol = leaves[i].symbol;
|
|
2191
|
+
if (bitLengths[symbol] > maxBits) {
|
|
2192
|
+
debt += cost - (1 << treeBits - bitLengths[symbol]);
|
|
2193
|
+
bitLengths[symbol] = maxBits;
|
|
2194
|
+
} else break;
|
|
2195
|
+
}
|
|
2196
|
+
debt >>= overflow;
|
|
2197
|
+
while (debt > 0) {
|
|
2198
|
+
let symbol = leaves[i].symbol;
|
|
2199
|
+
if (bitLengths[symbol] < maxBits) debt -= 1 << maxBits - bitLengths[symbol]++ - 1;
|
|
2200
|
+
else i++;
|
|
2201
|
+
}
|
|
2202
|
+
for (; i >= 0 && debt; i--) {
|
|
2203
|
+
let symbol = leaves[i].symbol;
|
|
2204
|
+
if (bitLengths[symbol] === maxBits) {
|
|
2205
|
+
bitLengths[symbol]--;
|
|
2206
|
+
debt++;
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
treeBits = maxBits;
|
|
2210
|
+
}
|
|
2211
|
+
return {
|
|
2212
|
+
lengths: new Uint8Array(bitLengths),
|
|
2213
|
+
maxBits: treeBits
|
|
2214
|
+
};
|
|
2215
|
+
}
|
|
2216
|
+
function assignLengths(node, lengths, depth) {
|
|
2217
|
+
if (node.symbol === -1) return Math.max(assignLengths(node.left, lengths, depth + 1), assignLengths(node.right, lengths, depth + 1));
|
|
2218
|
+
lengths[node.symbol] = depth;
|
|
2219
|
+
return depth;
|
|
2220
|
+
}
|
|
2221
|
+
//#endregion
|
|
2222
|
+
//#region src/compress/deflate.ts
|
|
2223
|
+
var levelOptions = new Int32Array([
|
|
2224
|
+
65540,
|
|
2225
|
+
131080,
|
|
2226
|
+
131088,
|
|
2227
|
+
131104,
|
|
2228
|
+
262176,
|
|
2229
|
+
1048704,
|
|
2230
|
+
1048832,
|
|
2231
|
+
2114560,
|
|
2232
|
+
2117632
|
|
2233
|
+
]);
|
|
2234
|
+
var empty = empty$1;
|
|
2235
|
+
var fixedLiteralEncode = /* @__PURE__ */ buildEncodeMap(fixedLiteralLengths, 9);
|
|
2236
|
+
var fixedDistanceEncode = /* @__PURE__ */ buildEncodeMap(fixedDistanceLengths, 5);
|
|
2237
|
+
function copySlice$1(buffer, start, end) {
|
|
2238
|
+
if (start < 0) start = 0;
|
|
2239
|
+
if (end == null || end > buffer.length) end = buffer.length;
|
|
2240
|
+
return new Uint8Array(buffer.subarray(start, end));
|
|
2241
|
+
}
|
|
2242
|
+
function encodedSize(freqs, lengths) {
|
|
2243
|
+
let bits = 0;
|
|
2244
|
+
for (let i = 0; i < lengths.length; i++) bits += freqs[i] * lengths[i];
|
|
2245
|
+
return bits;
|
|
2246
|
+
}
|
|
2247
|
+
function runLengthCodeLengths(lengths) {
|
|
2248
|
+
let end = lengths.length;
|
|
2249
|
+
while (end && !lengths[--end]);
|
|
2250
|
+
let packed = new Uint16Array(++end);
|
|
2251
|
+
let count = 0;
|
|
2252
|
+
let value = lengths[0];
|
|
2253
|
+
let run = 1;
|
|
2254
|
+
let write = (code) => {
|
|
2255
|
+
packed[count++] = code;
|
|
2256
|
+
};
|
|
2257
|
+
for (let i = 1; i <= end; i++) if (lengths[i] === value && i !== end) run++;
|
|
2258
|
+
else {
|
|
2259
|
+
if (!value && run > 2) {
|
|
2260
|
+
for (; run > 138; run -= 138) write(32754);
|
|
2261
|
+
if (run > 2) {
|
|
2262
|
+
write(run > 10 ? run - 11 << 5 | 28690 : run - 3 << 5 | 12305);
|
|
2263
|
+
run = 0;
|
|
2264
|
+
}
|
|
2265
|
+
} else if (run > 3) {
|
|
2266
|
+
write(value);
|
|
2267
|
+
run--;
|
|
2268
|
+
for (; run > 6; run -= 6) write(8304);
|
|
2269
|
+
if (run > 2) {
|
|
2270
|
+
write(run - 3 << 5 | 8208);
|
|
2271
|
+
run = 0;
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
while (run--) write(value);
|
|
2275
|
+
run = 1;
|
|
2276
|
+
value = lengths[i];
|
|
2277
|
+
}
|
|
2278
|
+
return {
|
|
2279
|
+
codes: packed.subarray(0, count),
|
|
2280
|
+
used: end
|
|
2281
|
+
};
|
|
2282
|
+
}
|
|
2283
|
+
function writeStoredBlock(out, bitPos, data) {
|
|
2284
|
+
let length = data.length;
|
|
2285
|
+
let offset = byteCeil(bitPos + 2);
|
|
2286
|
+
out[offset] = length & 255;
|
|
2287
|
+
out[offset + 1] = length >> 8;
|
|
2288
|
+
out[offset + 2] = out[offset] ^ 255;
|
|
2289
|
+
out[offset + 3] = out[offset + 1] ^ 255;
|
|
2290
|
+
out.set(data, offset + 4);
|
|
2291
|
+
return (offset + 4 + length) * 8;
|
|
2292
|
+
}
|
|
2293
|
+
function writeBlock(data, out, final, symbols, literalFreq, distanceFreq, extraBits, symbolCount, blockStart, blockLength, bitPos) {
|
|
2294
|
+
writeBits(out, bitPos++, final);
|
|
2295
|
+
literalFreq[256]++;
|
|
2296
|
+
let literalTree = buildLengthLimitedTree(literalFreq, 15);
|
|
2297
|
+
let distanceTree = buildLengthLimitedTree(distanceFreq, 15);
|
|
2298
|
+
let literalRuns = runLengthCodeLengths(literalTree.lengths);
|
|
2299
|
+
let distanceRuns = runLengthCodeLengths(distanceTree.lengths);
|
|
2300
|
+
let codeLengthFreq = new Uint16Array(19);
|
|
2301
|
+
for (let i = 0; i < literalRuns.codes.length; i++) codeLengthFreq[literalRuns.codes[i] & 31]++;
|
|
2302
|
+
for (let i = 0; i < distanceRuns.codes.length; i++) codeLengthFreq[distanceRuns.codes[i] & 31]++;
|
|
2303
|
+
let codeLengthTree = buildLengthLimitedTree(codeLengthFreq, 7);
|
|
2304
|
+
let codeLengthCount = 19;
|
|
2305
|
+
while (codeLengthCount > 4 && !codeLengthTree.lengths[codeLengthOrder[codeLengthCount - 1]]) codeLengthCount--;
|
|
2306
|
+
let storedBits = blockLength + 5 << 3;
|
|
2307
|
+
let fixedBits = encodedSize(literalFreq, fixedLiteralLengths) + encodedSize(distanceFreq, fixedDistanceLengths) + extraBits;
|
|
2308
|
+
let dynamicBits = encodedSize(literalFreq, literalTree.lengths) + encodedSize(distanceFreq, distanceTree.lengths) + extraBits + 14 + 3 * codeLengthCount + encodedSize(codeLengthFreq, codeLengthTree.lengths) + 2 * codeLengthFreq[16] + 3 * codeLengthFreq[17] + 7 * codeLengthFreq[18];
|
|
2309
|
+
if (blockStart >= 0 && storedBits <= fixedBits && storedBits <= dynamicBits) return writeStoredBlock(out, bitPos, data.subarray(blockStart, blockStart + blockLength));
|
|
2310
|
+
let literalMap;
|
|
2311
|
+
let literalLengths;
|
|
2312
|
+
let distanceMap;
|
|
2313
|
+
let distanceLengths;
|
|
2314
|
+
writeBits(out, bitPos, 1 + (dynamicBits < fixedBits ? 1 : 0));
|
|
2315
|
+
bitPos += 2;
|
|
2316
|
+
if (dynamicBits < fixedBits) {
|
|
2317
|
+
literalMap = buildEncodeMap(literalTree.lengths, literalTree.maxBits);
|
|
2318
|
+
literalLengths = literalTree.lengths;
|
|
2319
|
+
distanceMap = buildEncodeMap(distanceTree.lengths, distanceTree.maxBits);
|
|
2320
|
+
distanceLengths = distanceTree.lengths;
|
|
2321
|
+
let codeLengthMap = buildEncodeMap(codeLengthTree.lengths, codeLengthTree.maxBits);
|
|
2322
|
+
writeBits(out, bitPos, literalRuns.used - 257);
|
|
2323
|
+
writeBits(out, bitPos + 5, distanceRuns.used - 1);
|
|
2324
|
+
writeBits(out, bitPos + 10, codeLengthCount - 4);
|
|
2325
|
+
bitPos += 14;
|
|
2326
|
+
for (let i = 0; i < codeLengthCount; i++) writeBits(out, bitPos + 3 * i, codeLengthTree.lengths[codeLengthOrder[i]]);
|
|
2327
|
+
bitPos += 3 * codeLengthCount;
|
|
2328
|
+
let runs = [literalRuns.codes, distanceRuns.codes];
|
|
2329
|
+
for (let set = 0; set < 2; set++) {
|
|
2330
|
+
let codes = runs[set];
|
|
2331
|
+
for (let i = 0; i < codes.length; i++) {
|
|
2332
|
+
let symbol = codes[i] & 31;
|
|
2333
|
+
writeBits(out, bitPos, codeLengthMap[symbol]);
|
|
2334
|
+
bitPos += codeLengthTree.lengths[symbol];
|
|
2335
|
+
if (symbol > 15) {
|
|
2336
|
+
writeBits(out, bitPos, codes[i] >> 5 & 127);
|
|
2337
|
+
bitPos += codes[i] >> 12;
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
} else {
|
|
2342
|
+
literalMap = fixedLiteralEncode;
|
|
2343
|
+
literalLengths = fixedLiteralLengths;
|
|
2344
|
+
distanceMap = fixedDistanceEncode;
|
|
2345
|
+
distanceLengths = fixedDistanceLengths;
|
|
2346
|
+
}
|
|
2347
|
+
for (let i = 0; i < symbolCount; i++) {
|
|
2348
|
+
let symbol = symbols[i];
|
|
2349
|
+
if (symbol > 255) {
|
|
2350
|
+
let lengthIndex = symbol >> 18 & 31;
|
|
2351
|
+
writeBits16(out, bitPos, literalMap[lengthIndex + 257]);
|
|
2352
|
+
bitPos += literalLengths[lengthIndex + 257];
|
|
2353
|
+
if (lengthIndex > 7) {
|
|
2354
|
+
writeBits(out, bitPos, symbol >> 23 & 31);
|
|
2355
|
+
bitPos += fixedLengthExtraBits[lengthIndex];
|
|
2356
|
+
}
|
|
2357
|
+
let distanceIndex = symbol & 31;
|
|
2358
|
+
writeBits16(out, bitPos, distanceMap[distanceIndex]);
|
|
2359
|
+
bitPos += distanceLengths[distanceIndex];
|
|
2360
|
+
if (distanceIndex > 3) {
|
|
2361
|
+
writeBits16(out, bitPos, symbol >> 5 & 8191);
|
|
2362
|
+
bitPos += fixedDistanceExtraBits[distanceIndex];
|
|
2363
|
+
}
|
|
2364
|
+
} else {
|
|
2365
|
+
writeBits16(out, bitPos, literalMap[symbol]);
|
|
2366
|
+
bitPos += literalLengths[symbol];
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
writeBits16(out, bitPos, literalMap[256]);
|
|
2370
|
+
return bitPos + literalLengths[256];
|
|
2371
|
+
}
|
|
2372
|
+
function deflateRaw(data, level, hashBits, pre, post, state) {
|
|
2373
|
+
let size = state.end || data.length;
|
|
2374
|
+
let out = new Uint8Array(pre + size + 5 * (1 + Math.ceil(size / 7e3)) + post);
|
|
2375
|
+
let dest = out.subarray(pre, out.length - post);
|
|
2376
|
+
let last = state.last;
|
|
2377
|
+
let pos = (state.remainder || 0) & 7;
|
|
2378
|
+
if (level) {
|
|
2379
|
+
if (pos) dest[0] = state.remainder >> 3;
|
|
2380
|
+
let opt = levelOptions[level - 1];
|
|
2381
|
+
let nice = opt >> 13;
|
|
2382
|
+
let chain = opt & 8191;
|
|
2383
|
+
let mask = (1 << hashBits) - 1;
|
|
2384
|
+
let prev = state.prev || new Uint16Array(32768);
|
|
2385
|
+
let head = state.head || new Uint16Array(mask + 1);
|
|
2386
|
+
let shift1 = Math.ceil(hashBits / 3);
|
|
2387
|
+
let shift2 = 2 * shift1;
|
|
2388
|
+
let hashAt = (i) => (data[i] ^ data[i + 1] << shift1 ^ data[i + 2] << shift2) & mask;
|
|
2389
|
+
let symbols = new Int32Array(25e3);
|
|
2390
|
+
let literalFreq = new Uint16Array(288);
|
|
2391
|
+
let distanceFreq = new Uint16Array(32);
|
|
2392
|
+
let matches = 0;
|
|
2393
|
+
let extraBits = 0;
|
|
2394
|
+
let i = state.index || 0;
|
|
2395
|
+
let symbolCount = 0;
|
|
2396
|
+
let wait = state.wait || 0;
|
|
2397
|
+
let blockStart = 0;
|
|
2398
|
+
for (; i + 2 < size; i++) {
|
|
2399
|
+
let hv = hashAt(i);
|
|
2400
|
+
let imod = i & 32767;
|
|
2401
|
+
let previous = head[hv];
|
|
2402
|
+
prev[imod] = previous;
|
|
2403
|
+
head[hv] = imod;
|
|
2404
|
+
if (wait <= i) {
|
|
2405
|
+
let remaining = size - i;
|
|
2406
|
+
if ((matches > 7e3 || symbolCount > 24576) && (remaining > 423 || !last)) {
|
|
2407
|
+
pos = writeBlock(data, dest, 0, symbols, literalFreq, distanceFreq, extraBits, symbolCount, blockStart, i - blockStart, pos);
|
|
2408
|
+
symbolCount = matches = extraBits = 0;
|
|
2409
|
+
blockStart = i;
|
|
2410
|
+
literalFreq.fill(0);
|
|
2411
|
+
distanceFreq.fill(0);
|
|
2412
|
+
}
|
|
2413
|
+
let bestLen = 2;
|
|
2414
|
+
let bestDist = 0;
|
|
2415
|
+
let tries = chain;
|
|
2416
|
+
let dist = imod - previous & 32767;
|
|
2417
|
+
if (remaining > 2 && hv === hashAt(i - dist)) {
|
|
2418
|
+
let niceLen = Math.min(nice, remaining) - 1;
|
|
2419
|
+
let maxDist = Math.min(32767, i);
|
|
2420
|
+
let maxLen = Math.min(258, remaining);
|
|
2421
|
+
while (dist <= maxDist && --tries && imod !== previous) {
|
|
2422
|
+
if (data[i + bestLen] === data[i + bestLen - dist]) {
|
|
2423
|
+
let len = 0;
|
|
2424
|
+
for (; len < maxLen && data[i + len] === data[i + len - dist]; len++);
|
|
2425
|
+
if (len > bestLen) {
|
|
2426
|
+
bestLen = len;
|
|
2427
|
+
bestDist = dist;
|
|
2428
|
+
if (len > niceLen) break;
|
|
2429
|
+
let search = Math.min(dist, len - 2);
|
|
2430
|
+
let rarest = 0;
|
|
2431
|
+
for (let j = 0; j < search; j++) {
|
|
2432
|
+
let ti = i - dist + j & 32767;
|
|
2433
|
+
let candidate = ti - prev[ti] & 32767;
|
|
2434
|
+
if (candidate > rarest) {
|
|
2435
|
+
rarest = candidate;
|
|
2436
|
+
previous = ti;
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
imod = previous;
|
|
2442
|
+
previous = prev[imod];
|
|
2443
|
+
dist += imod - previous & 32767;
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
if (bestDist) {
|
|
2447
|
+
symbols[symbolCount++] = 268435456 | lengthReverse[bestLen] << 18 | distanceReverse[bestDist];
|
|
2448
|
+
let lengthIndex = lengthReverse[bestLen] & 31;
|
|
2449
|
+
let distanceIndex = distanceReverse[bestDist] & 31;
|
|
2450
|
+
extraBits += fixedLengthExtraBits[lengthIndex] + fixedDistanceExtraBits[distanceIndex];
|
|
2451
|
+
literalFreq[257 + lengthIndex]++;
|
|
2452
|
+
distanceFreq[distanceIndex]++;
|
|
2453
|
+
wait = i + bestLen;
|
|
2454
|
+
matches++;
|
|
2455
|
+
} else {
|
|
2456
|
+
symbols[symbolCount++] = data[i];
|
|
2457
|
+
literalFreq[data[i]]++;
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
for (i = Math.max(i, wait); i < size; i++) {
|
|
2462
|
+
symbols[symbolCount++] = data[i];
|
|
2463
|
+
literalFreq[data[i]]++;
|
|
2464
|
+
}
|
|
2465
|
+
pos = writeBlock(data, dest, last, symbols, literalFreq, distanceFreq, extraBits, symbolCount, blockStart, i - blockStart, pos);
|
|
2466
|
+
if (!last) {
|
|
2467
|
+
state.remainder = pos & 7 | dest[pos / 8 | 0] << 3;
|
|
2468
|
+
pos -= 7;
|
|
2469
|
+
state.head = head;
|
|
2470
|
+
state.prev = prev;
|
|
2471
|
+
state.index = i;
|
|
2472
|
+
state.wait = wait;
|
|
2473
|
+
}
|
|
2474
|
+
} else {
|
|
2475
|
+
for (let i = state.wait || 0; i < size + last; i += 65535) {
|
|
2476
|
+
let end = i + 65535;
|
|
2477
|
+
if (end >= size) {
|
|
2478
|
+
dest[pos / 8 | 0] = last;
|
|
2479
|
+
end = size;
|
|
2480
|
+
}
|
|
2481
|
+
pos = writeStoredBlock(dest, pos + 1, data.subarray(i, end));
|
|
2482
|
+
}
|
|
2483
|
+
state.index = size;
|
|
2484
|
+
}
|
|
2485
|
+
return copySlice$1(out, 0, pre + byteCeil(pos) + post);
|
|
2486
|
+
}
|
|
2487
|
+
function defaultHashBits(length, last) {
|
|
2488
|
+
if (!last) return 20;
|
|
2489
|
+
return Math.ceil(Math.max(8, Math.min(13, Math.log(Math.max(length, 1)))) * 1.5);
|
|
2490
|
+
}
|
|
2491
|
+
function deflateWithOptions(data, options, pre, post, state) {
|
|
2492
|
+
if (!state) {
|
|
2493
|
+
state = { last: 1 };
|
|
2494
|
+
if (options.dictionary) {
|
|
2495
|
+
let dict = options.dictionary.subarray(-32768);
|
|
2496
|
+
let prefixed = new Uint8Array(dict.length + data.length);
|
|
2497
|
+
prefixed.set(dict);
|
|
2498
|
+
prefixed.set(data, dict.length);
|
|
2499
|
+
data = prefixed;
|
|
2500
|
+
state.wait = dict.length;
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
let level = options.level == null ? 6 : options.level;
|
|
2504
|
+
let hashBits = options.mem == null ? defaultHashBits(data.length, state.last) : 12 + options.mem;
|
|
2505
|
+
return deflateRaw(data, level, hashBits, pre, post, state);
|
|
2506
|
+
}
|
|
2507
|
+
function compress$2(data, options = {}) {
|
|
2508
|
+
return deflateWithOptions(data, options, 0, 0);
|
|
2509
|
+
}
|
|
2510
|
+
var Compressor$2 = class {
|
|
2511
|
+
_options;
|
|
2512
|
+
_state;
|
|
2513
|
+
_buffer;
|
|
2514
|
+
_done = false;
|
|
2515
|
+
constructor(options = {}) {
|
|
2516
|
+
this._options = options;
|
|
2517
|
+
this._state = {
|
|
2518
|
+
last: 0,
|
|
2519
|
+
index: 32768,
|
|
2520
|
+
wait: 32768,
|
|
2521
|
+
end: 32768
|
|
2522
|
+
};
|
|
2523
|
+
this._buffer = new Uint8Array(98304);
|
|
2524
|
+
if (options.dictionary) {
|
|
2525
|
+
let dict = options.dictionary.subarray(-32768);
|
|
2526
|
+
this._buffer.set(dict, 32768 - dict.length);
|
|
2527
|
+
this._state.index = 32768 - dict.length;
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
_emit(final) {
|
|
2531
|
+
return deflateWithOptions(this._buffer, this._options, 0, 0, this._state);
|
|
2532
|
+
}
|
|
2533
|
+
push(chunk, final) {
|
|
2534
|
+
if (this._done) throw new StreamFinishedError();
|
|
2535
|
+
let endLen = chunk.length + this._state.end;
|
|
2536
|
+
if (endLen > this._buffer.length) {
|
|
2537
|
+
if (endLen > 2 * this._buffer.length - 32768) {
|
|
2538
|
+
let next = new Uint8Array(endLen & -32768);
|
|
2539
|
+
next.set(this._buffer.subarray(0, this._state.end));
|
|
2540
|
+
this._buffer = next;
|
|
2541
|
+
}
|
|
2542
|
+
let split = this._buffer.length - this._state.end;
|
|
2543
|
+
this._buffer.set(chunk.subarray(0, split), this._state.end);
|
|
2544
|
+
this._state.end = this._buffer.length;
|
|
2545
|
+
let first = this._emit(false);
|
|
2546
|
+
this._buffer.set(this._buffer.subarray(-32768));
|
|
2547
|
+
this._buffer.set(chunk.subarray(split), 32768);
|
|
2548
|
+
this._state.end = chunk.length - split + 32768;
|
|
2549
|
+
this._state.index = 32766;
|
|
2550
|
+
this._state.wait = 32768;
|
|
2551
|
+
this._state.last = final ? 1 : 0;
|
|
2552
|
+
let rest = new Uint8Array(0);
|
|
2553
|
+
if (this._state.end > this._state.wait + 8191 || final) {
|
|
2554
|
+
rest = this._emit(!!final);
|
|
2555
|
+
this._state.wait = this._state.index;
|
|
2556
|
+
this._state.index -= 2;
|
|
2557
|
+
}
|
|
2558
|
+
if (final) {
|
|
2559
|
+
this._done = true;
|
|
2560
|
+
this._buffer = empty;
|
|
2561
|
+
this._state = { last: 1 };
|
|
2562
|
+
}
|
|
2563
|
+
if (!rest.length) return first;
|
|
2564
|
+
let joined = new Uint8Array(first.length + rest.length);
|
|
2565
|
+
joined.set(first);
|
|
2566
|
+
joined.set(rest, first.length);
|
|
2567
|
+
return joined;
|
|
2568
|
+
}
|
|
2569
|
+
this._buffer.set(chunk, this._state.end);
|
|
2570
|
+
this._state.end += chunk.length;
|
|
2571
|
+
this._state.last = final ? 1 : 0;
|
|
2572
|
+
let out = empty$1;
|
|
2573
|
+
if (this._state.end > this._state.wait + 8191 || final) {
|
|
2574
|
+
out = this._emit(!!final);
|
|
2575
|
+
this._state.wait = this._state.index;
|
|
2576
|
+
this._state.index -= 2;
|
|
2577
|
+
}
|
|
2578
|
+
if (final) {
|
|
2579
|
+
this._done = true;
|
|
2580
|
+
this._buffer = empty;
|
|
2581
|
+
this._state = { last: 1 };
|
|
2582
|
+
}
|
|
2583
|
+
return out;
|
|
2584
|
+
}
|
|
2585
|
+
flush(sync) {
|
|
2586
|
+
if (this._done) throw new StreamFinishedError();
|
|
2587
|
+
let out = this._emit(false);
|
|
2588
|
+
this._state.wait = this._state.index;
|
|
2589
|
+
this._state.index -= 2;
|
|
2590
|
+
if (!sync) return out;
|
|
2591
|
+
let block = new Uint8Array(6);
|
|
2592
|
+
block[0] = this._state.remainder >> 3;
|
|
2593
|
+
let end = writeStoredBlock(block, this._state.remainder, empty);
|
|
2594
|
+
this._state.remainder = 0;
|
|
2595
|
+
let trailer = block.subarray(0, end >> 3);
|
|
2596
|
+
if (!out.length) return new Uint8Array(trailer);
|
|
2597
|
+
let joined = new Uint8Array(out.length + trailer.length);
|
|
2598
|
+
joined.set(out);
|
|
2599
|
+
joined.set(trailer, out.length);
|
|
2600
|
+
return joined;
|
|
2601
|
+
}
|
|
2602
|
+
};
|
|
2603
|
+
//#endregion
|
|
2604
|
+
//#region src/compress/inflate.ts
|
|
2605
|
+
var fixedLengthMap = /* @__PURE__ */ buildDecodeMap(fixedLiteralLengths, 9);
|
|
2606
|
+
var fixedDistanceMap = /* @__PURE__ */ buildDecodeMap(fixedDistanceLengths, 5);
|
|
2607
|
+
function copySlice(buffer, start, end) {
|
|
2608
|
+
if (start < 0) start = 0;
|
|
2609
|
+
if (end == null || end > buffer.length) end = buffer.length;
|
|
2610
|
+
return new Uint8Array(buffer.subarray(start, end));
|
|
2611
|
+
}
|
|
2612
|
+
function maxValue(values) {
|
|
2613
|
+
let max = values[0];
|
|
2614
|
+
for (let i = 1; i < values.length; i++) if (values[i] > max) max = values[i];
|
|
2615
|
+
return max;
|
|
2616
|
+
}
|
|
2617
|
+
function inflateRaw(data, state, buf, dictionary) {
|
|
2618
|
+
let sourceLength = data.length;
|
|
2619
|
+
let dictLength = dictionary ? dictionary.length : 0;
|
|
2620
|
+
if (!sourceLength || state.final && !state.lengthMap) return buf || empty$1;
|
|
2621
|
+
let noBuf = !buf;
|
|
2622
|
+
let resize = noBuf || state.mode !== 2;
|
|
2623
|
+
let throwOnEof = state.mode !== 0;
|
|
2624
|
+
if (noBuf) buf = new Uint8Array(sourceLength * 3);
|
|
2625
|
+
let ensure = (need) => {
|
|
2626
|
+
if (need <= buf.length) return;
|
|
2627
|
+
let next = new Uint8Array(Math.max(buf.length * 2, need));
|
|
2628
|
+
next.set(buf);
|
|
2629
|
+
buf = next;
|
|
2630
|
+
};
|
|
2631
|
+
let final = state.final || 0;
|
|
2632
|
+
let pos = state.bitPos || 0;
|
|
2633
|
+
let written = state.outputLength || 0;
|
|
2634
|
+
let lengthMap = state.lengthMap;
|
|
2635
|
+
let distanceMap = state.distanceMap;
|
|
2636
|
+
let lengthBits = state.lengthBits;
|
|
2637
|
+
let distanceBits = state.distanceBits;
|
|
2638
|
+
let totalBits = sourceLength * 8;
|
|
2639
|
+
do {
|
|
2640
|
+
if (!lengthMap) {
|
|
2641
|
+
final = readBits(data, pos, 1);
|
|
2642
|
+
let type = readBits(data, pos + 1, 3);
|
|
2643
|
+
pos += 3;
|
|
2644
|
+
if (!type) {
|
|
2645
|
+
let start = byteCeil(pos) + 4;
|
|
2646
|
+
let length = data[start - 4] | data[start - 3] << 8;
|
|
2647
|
+
let end = start + length;
|
|
2648
|
+
if (end > sourceLength) {
|
|
2649
|
+
if (throwOnEof) throw new UnexpectedEofError();
|
|
2650
|
+
break;
|
|
2651
|
+
}
|
|
2652
|
+
if (resize) ensure(written + length);
|
|
2653
|
+
let room = buf.length - written;
|
|
2654
|
+
if (room > 0) buf.set(data.subarray(start, start + Math.min(length, room)), written);
|
|
2655
|
+
state.outputLength = written += length;
|
|
2656
|
+
state.bitPos = pos = end * 8;
|
|
2657
|
+
state.final = final;
|
|
2658
|
+
continue;
|
|
2659
|
+
} else if (type === 1) {
|
|
2660
|
+
lengthMap = fixedLengthMap;
|
|
2661
|
+
distanceMap = fixedDistanceMap;
|
|
2662
|
+
lengthBits = 9;
|
|
2663
|
+
distanceBits = 5;
|
|
2664
|
+
} else if (type === 2) {
|
|
2665
|
+
let literalCount = readBits(data, pos, 31) + 257;
|
|
2666
|
+
let distanceCount = readBits(data, pos + 5, 31) + 1;
|
|
2667
|
+
let codeLengthCount = readBits(data, pos + 10, 15) + 4;
|
|
2668
|
+
let totalCodes = literalCount + distanceCount;
|
|
2669
|
+
pos += 14;
|
|
2670
|
+
let lengths = new Uint8Array(totalCodes);
|
|
2671
|
+
let codeLengthTree = new Uint8Array(19);
|
|
2672
|
+
for (let i = 0; i < codeLengthCount; i++) codeLengthTree[codeLengthOrder[i]] = readBits(data, pos + i * 3, 7);
|
|
2673
|
+
pos += codeLengthCount * 3;
|
|
2674
|
+
let codeLengthBits = maxValue(codeLengthTree);
|
|
2675
|
+
let codeLengthMask = (1 << codeLengthBits) - 1;
|
|
2676
|
+
let codeLengthMap = buildDecodeMap(codeLengthTree, codeLengthBits);
|
|
2677
|
+
for (let i = 0; i < totalCodes;) {
|
|
2678
|
+
let entry = codeLengthMap[readBits(data, pos, codeLengthMask)];
|
|
2679
|
+
pos += entry & 15;
|
|
2680
|
+
let symbol = entry >> 4;
|
|
2681
|
+
if (symbol < 16) lengths[i++] = symbol;
|
|
2682
|
+
else {
|
|
2683
|
+
let fill = 0;
|
|
2684
|
+
let count = 0;
|
|
2685
|
+
if (symbol === 16) {
|
|
2686
|
+
count = 3 + readBits(data, pos, 3);
|
|
2687
|
+
pos += 2;
|
|
2688
|
+
fill = lengths[i - 1];
|
|
2689
|
+
} else if (symbol === 17) {
|
|
2690
|
+
count = 3 + readBits(data, pos, 7);
|
|
2691
|
+
pos += 3;
|
|
2692
|
+
} else if (symbol === 18) {
|
|
2693
|
+
count = 11 + readBits(data, pos, 127);
|
|
2694
|
+
pos += 7;
|
|
2695
|
+
}
|
|
2696
|
+
while (count--) lengths[i++] = fill;
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
let literalLengths = lengths.subarray(0, literalCount);
|
|
2700
|
+
let distanceLengths = lengths.subarray(literalCount);
|
|
2701
|
+
lengthBits = maxValue(literalLengths);
|
|
2702
|
+
distanceBits = maxValue(distanceLengths);
|
|
2703
|
+
lengthMap = buildDecodeMap(literalLengths, lengthBits);
|
|
2704
|
+
distanceMap = buildDecodeMap(distanceLengths, distanceBits);
|
|
2705
|
+
} else throw new InvalidBlockTypeError();
|
|
2706
|
+
if (pos > totalBits) {
|
|
2707
|
+
if (throwOnEof) throw new UnexpectedEofError();
|
|
2708
|
+
break;
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
if (resize) ensure(written + 131072);
|
|
2712
|
+
let lengthMask = (1 << lengthBits) - 1;
|
|
2713
|
+
let distanceMask = (1 << distanceBits) - 1;
|
|
2714
|
+
let lastPos = pos;
|
|
2715
|
+
for (;; lastPos = pos) {
|
|
2716
|
+
let entry = lengthMap[readBits16(data, pos) & lengthMask];
|
|
2717
|
+
let symbol = entry >> 4;
|
|
2718
|
+
pos += entry & 15;
|
|
2719
|
+
if (pos > totalBits) {
|
|
2720
|
+
if (throwOnEof) throw new UnexpectedEofError();
|
|
2721
|
+
break;
|
|
2722
|
+
}
|
|
2723
|
+
if (!entry) throw new InvalidLengthLiteralError();
|
|
2724
|
+
if (symbol < 256) {
|
|
2725
|
+
if (written < buf.length) buf[written] = symbol;
|
|
2726
|
+
written++;
|
|
2727
|
+
} else if (symbol === 256) {
|
|
2728
|
+
lastPos = pos;
|
|
2729
|
+
lengthMap = void 0;
|
|
2730
|
+
break;
|
|
2731
|
+
} else {
|
|
2732
|
+
let add = symbol - 254;
|
|
2733
|
+
if (symbol > 264) {
|
|
2734
|
+
let index = symbol - 257;
|
|
2735
|
+
let extra = fixedLengthExtraBits[index];
|
|
2736
|
+
add = readBits(data, pos, (1 << extra) - 1) + lengthBase[index];
|
|
2737
|
+
pos += extra;
|
|
2738
|
+
}
|
|
2739
|
+
let distEntry = distanceMap[readBits16(data, pos) & distanceMask];
|
|
2740
|
+
let distSymbol = distEntry >> 4;
|
|
2741
|
+
if (!distEntry) throw new InvalidDistanceError();
|
|
2742
|
+
pos += distEntry & 15;
|
|
2743
|
+
let distance = distanceBase[distSymbol];
|
|
2744
|
+
if (distSymbol > 3) {
|
|
2745
|
+
let extra = fixedDistanceExtraBits[distSymbol];
|
|
2746
|
+
distance += readBits16(data, pos) & (1 << extra) - 1;
|
|
2747
|
+
pos += extra;
|
|
2748
|
+
}
|
|
2749
|
+
if (pos > totalBits) {
|
|
2750
|
+
if (throwOnEof) throw new UnexpectedEofError();
|
|
2751
|
+
break;
|
|
2752
|
+
}
|
|
2753
|
+
if (resize) ensure(written + 131072);
|
|
2754
|
+
let end = written + add;
|
|
2755
|
+
if (written < distance) {
|
|
2756
|
+
let shift = dictLength - distance;
|
|
2757
|
+
let dictEnd = Math.min(distance, end);
|
|
2758
|
+
if (shift + written < 0) throw new InvalidDistanceError();
|
|
2759
|
+
for (; written < dictEnd; written++) if (written < buf.length) buf[written] = dictionary[shift + written];
|
|
2760
|
+
}
|
|
2761
|
+
for (; written < end; written++) if (written < buf.length) buf[written] = buf[written - distance];
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
state.lengthMap = lengthMap;
|
|
2765
|
+
state.bitPos = lastPos;
|
|
2766
|
+
state.outputLength = written;
|
|
2767
|
+
state.final = final;
|
|
2768
|
+
if (lengthMap) {
|
|
2769
|
+
final = 1;
|
|
2770
|
+
state.lengthBits = lengthBits;
|
|
2771
|
+
state.distanceMap = distanceMap;
|
|
2772
|
+
state.distanceBits = distanceBits;
|
|
2773
|
+
}
|
|
2774
|
+
} while (!final);
|
|
2775
|
+
let length = Math.min(written, buf.length);
|
|
2776
|
+
if (length === buf.length) return buf;
|
|
2777
|
+
return noBuf ? copySlice(buf, 0, length) : buf.subarray(0, length);
|
|
2778
|
+
}
|
|
2779
|
+
function decompress$3(data, options) {
|
|
2780
|
+
return inflateRaw(data, { mode: 2 }, options?.out, options?.dictionary);
|
|
2781
|
+
}
|
|
2782
|
+
var Decompressor$2 = class {
|
|
2783
|
+
_state;
|
|
2784
|
+
_window;
|
|
2785
|
+
_pending;
|
|
2786
|
+
_done = false;
|
|
2787
|
+
constructor(options) {
|
|
2788
|
+
let dict = options?.dictionary?.subarray(-32768);
|
|
2789
|
+
this._state = {
|
|
2790
|
+
mode: 0,
|
|
2791
|
+
outputLength: dict ? dict.length : 0
|
|
2792
|
+
};
|
|
2793
|
+
this._window = new Uint8Array(32768);
|
|
2794
|
+
this._pending = empty$1;
|
|
2795
|
+
if (dict) this._window.set(dict);
|
|
2796
|
+
}
|
|
2797
|
+
push(chunk, final) {
|
|
2798
|
+
if (this._done) throw new StreamFinishedError();
|
|
2799
|
+
if (!this._pending.length) this._pending = chunk;
|
|
2800
|
+
else if (chunk.length) {
|
|
2801
|
+
let next = new Uint8Array(this._pending.length + chunk.length);
|
|
2802
|
+
next.set(this._pending);
|
|
2803
|
+
next.set(chunk, this._pending.length);
|
|
2804
|
+
this._pending = next;
|
|
2805
|
+
}
|
|
2806
|
+
this._done = !!final;
|
|
2807
|
+
this._state.mode = this._done ? 1 : 0;
|
|
2808
|
+
let start = this._state.outputLength || 0;
|
|
2809
|
+
let out = inflateRaw(this._pending, this._state, this._window);
|
|
2810
|
+
let produced = copySlice(out, start, this._state.outputLength);
|
|
2811
|
+
this._window = copySlice(out, (this._state.outputLength || 0) - 32768);
|
|
2812
|
+
this._state.outputLength = this._window.length;
|
|
2813
|
+
this._pending = copySlice(this._pending, (this._state.bitPos || 0) / 8 | 0);
|
|
2814
|
+
this._state.bitPos = (this._state.bitPos || 0) & 7;
|
|
2815
|
+
return produced.length ? produced : empty$1;
|
|
2816
|
+
}
|
|
2817
|
+
};
|
|
2818
|
+
//#endregion
|
|
2819
|
+
//#region src/compress/gzip.ts
|
|
2820
|
+
function writeU32LE(buf, offset, value) {
|
|
2821
|
+
buf[offset] = value;
|
|
2822
|
+
buf[offset + 1] = value >>> 8;
|
|
2823
|
+
buf[offset + 2] = value >>> 16;
|
|
2824
|
+
buf[offset + 3] = value >>> 24;
|
|
2825
|
+
}
|
|
2826
|
+
function readU32LE(buf, offset) {
|
|
2827
|
+
return (buf[offset] | buf[offset + 1] << 8 | buf[offset + 2] << 16 | buf[offset + 3] << 24) >>> 0;
|
|
2828
|
+
}
|
|
2829
|
+
function gzipHeaderSize(options) {
|
|
2830
|
+
return 10 + (options.filename ? options.filename.length + 1 : 0);
|
|
2831
|
+
}
|
|
2832
|
+
function writeGzipHeader(out, options) {
|
|
2833
|
+
out[0] = 31;
|
|
2834
|
+
out[1] = 139;
|
|
2835
|
+
out[2] = 8;
|
|
2836
|
+
let level = options.level ?? 6;
|
|
2837
|
+
out[8] = level < 2 ? 4 : level === 9 ? 2 : 0;
|
|
2838
|
+
out[9] = 3;
|
|
2839
|
+
if (options.mtime !== 0) writeU32LE(out, 4, Math.floor((options.mtime != null ? new Date(options.mtime).getTime() : Date.now()) / 1e3) >>> 0);
|
|
2840
|
+
if (options.filename) {
|
|
2841
|
+
out[3] = 8;
|
|
2842
|
+
for (let i = 0; i < options.filename.length; i++) out[10 + i] = options.filename.charCodeAt(i);
|
|
2843
|
+
out[10 + options.filename.length] = 0;
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
function gzipHeaderLength(data) {
|
|
2847
|
+
if (data.length < 10) throw new UnexpectedEofError();
|
|
2848
|
+
if (data[0] !== 31 || data[1] !== 139 || data[2] !== 8) throw new InvalidHeaderError("Invalid gzip header.");
|
|
2849
|
+
let flags = data[3];
|
|
2850
|
+
let offset = 10;
|
|
2851
|
+
if (flags & 4) {
|
|
2852
|
+
if (offset + 2 > data.length) throw new UnexpectedEofError();
|
|
2853
|
+
offset += (data[offset] | data[offset + 1] << 8) + 2;
|
|
2854
|
+
}
|
|
2855
|
+
let strings = (flags >> 3 & 1) + (flags >> 4 & 1);
|
|
2856
|
+
while (strings) {
|
|
2857
|
+
if (offset >= data.length) throw new UnexpectedEofError();
|
|
2858
|
+
if (!data[offset++]) strings--;
|
|
2859
|
+
}
|
|
2860
|
+
if (flags & 2) offset += 2;
|
|
2861
|
+
if (offset > data.length) throw new UnexpectedEofError();
|
|
2862
|
+
return offset;
|
|
2863
|
+
}
|
|
2864
|
+
function concatParts(parts) {
|
|
2865
|
+
if (parts.length === 0) return empty$1;
|
|
2866
|
+
if (parts.length === 1) return parts[0];
|
|
2867
|
+
let out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
2868
|
+
let offset = 0;
|
|
2869
|
+
for (let part of parts) {
|
|
2870
|
+
out.set(part, offset);
|
|
2871
|
+
offset += part.length;
|
|
2872
|
+
}
|
|
2873
|
+
return out;
|
|
2874
|
+
}
|
|
2875
|
+
function verifyFooter(footer, data, check) {
|
|
2876
|
+
if (!check) return;
|
|
2877
|
+
if (readU32LE(footer, 0) !== crc32(data) || readU32LE(footer, 4) !== data.length >>> 0) throw new ChecksumMismatchError();
|
|
2878
|
+
}
|
|
2879
|
+
function compress$1(data, options = {}) {
|
|
2880
|
+
let out = deflateWithOptions(data, options, gzipHeaderSize(options), 8);
|
|
2881
|
+
writeGzipHeader(out, options);
|
|
2882
|
+
writeU32LE(out, out.length - 8, crc32(data));
|
|
2883
|
+
writeU32LE(out, out.length - 4, data.length >>> 0);
|
|
2884
|
+
return out;
|
|
2885
|
+
}
|
|
2886
|
+
function decompress$2(data, options) {
|
|
2887
|
+
let parts = [];
|
|
2888
|
+
let offset = 0;
|
|
2889
|
+
while (offset < data.length) {
|
|
2890
|
+
let header = gzipHeaderLength(data.subarray(offset));
|
|
2891
|
+
offset += header;
|
|
2892
|
+
let state = { mode: 2 };
|
|
2893
|
+
let payload = inflateRaw(data.subarray(offset), state);
|
|
2894
|
+
parts.push(payload);
|
|
2895
|
+
offset += byteCeil(state.bitPos || 0);
|
|
2896
|
+
if (offset + 8 > data.length) throw new UnexpectedEofError();
|
|
2897
|
+
verifyFooter(data.subarray(offset, offset + 8), payload, options?.check !== false);
|
|
2898
|
+
offset += 8;
|
|
2899
|
+
}
|
|
2900
|
+
return concatParts(parts);
|
|
2901
|
+
}
|
|
2902
|
+
var Compressor$1 = class {
|
|
2903
|
+
_inner;
|
|
2904
|
+
_options;
|
|
2905
|
+
_crc = new Crc32();
|
|
2906
|
+
_length = 0;
|
|
2907
|
+
_header = false;
|
|
2908
|
+
_done = false;
|
|
2909
|
+
constructor(options = {}) {
|
|
2910
|
+
this._options = options;
|
|
2911
|
+
this._inner = new Compressor$2(options);
|
|
2912
|
+
}
|
|
2913
|
+
_wrap(raw, final) {
|
|
2914
|
+
let header = 0;
|
|
2915
|
+
if (!this._header) {
|
|
2916
|
+
header = gzipHeaderSize(this._options);
|
|
2917
|
+
this._header = true;
|
|
2918
|
+
}
|
|
2919
|
+
let footer = final ? 8 : 0;
|
|
2920
|
+
if (!header && !footer) return raw;
|
|
2921
|
+
let out = new Uint8Array(header + raw.length + footer);
|
|
2922
|
+
if (header) writeGzipHeader(out, this._options);
|
|
2923
|
+
out.set(raw, header);
|
|
2924
|
+
if (footer) {
|
|
2925
|
+
writeU32LE(out, header + raw.length, this._crc.digest());
|
|
2926
|
+
writeU32LE(out, header + raw.length + 4, this._length >>> 0);
|
|
2927
|
+
}
|
|
2928
|
+
return out;
|
|
2929
|
+
}
|
|
2930
|
+
push(chunk, final) {
|
|
2931
|
+
if (this._done) throw new StreamFinishedError();
|
|
2932
|
+
this._crc.update(chunk);
|
|
2933
|
+
this._length += chunk.length;
|
|
2934
|
+
this._done = !!final;
|
|
2935
|
+
return this._wrap(this._inner.push(chunk, final), !!final);
|
|
2936
|
+
}
|
|
2937
|
+
flush(sync) {
|
|
2938
|
+
if (this._done) throw new StreamFinishedError();
|
|
2939
|
+
return this._wrap(this._inner.flush(sync), false);
|
|
2940
|
+
}
|
|
2941
|
+
};
|
|
2942
|
+
var Decompressor$1 = class {
|
|
2943
|
+
_pending = empty$1;
|
|
2944
|
+
_needHeader = true;
|
|
2945
|
+
_state = { mode: 0 };
|
|
2946
|
+
_window = new Uint8Array(32768);
|
|
2947
|
+
_crc = new Crc32();
|
|
2948
|
+
_length = 0;
|
|
2949
|
+
_check;
|
|
2950
|
+
_done = false;
|
|
2951
|
+
constructor(options) {
|
|
2952
|
+
this._check = options?.check !== false;
|
|
2953
|
+
}
|
|
2954
|
+
push(chunk, final) {
|
|
2955
|
+
if (this._done) throw new StreamFinishedError();
|
|
2956
|
+
if (!this._pending.length) this._pending = chunk;
|
|
2957
|
+
else if (chunk.length) {
|
|
2958
|
+
let next = new Uint8Array(this._pending.length + chunk.length);
|
|
2959
|
+
next.set(this._pending);
|
|
2960
|
+
next.set(chunk, this._pending.length);
|
|
2961
|
+
this._pending = next;
|
|
2962
|
+
}
|
|
2963
|
+
let parts = [];
|
|
2964
|
+
for (;;) {
|
|
2965
|
+
if (this._needHeader) {
|
|
2966
|
+
if (!this._pending.length) break;
|
|
2967
|
+
if (this._pending.length < 10 && !final) break;
|
|
2968
|
+
let header = gzipHeaderLength(this._pending);
|
|
2969
|
+
this._pending = this._pending.subarray(header);
|
|
2970
|
+
this._needHeader = false;
|
|
2971
|
+
this._state = { mode: 0 };
|
|
2972
|
+
this._window = new Uint8Array(32768);
|
|
2973
|
+
this._crc = new Crc32();
|
|
2974
|
+
this._length = 0;
|
|
2975
|
+
}
|
|
2976
|
+
this._state.mode = 0;
|
|
2977
|
+
let start = this._state.outputLength || 0;
|
|
2978
|
+
let out = inflateRaw(this._pending, this._state, this._window);
|
|
2979
|
+
let produced = out.subarray(start, Math.min(this._state.outputLength || 0, out.length));
|
|
2980
|
+
if (produced.length) {
|
|
2981
|
+
this._crc.update(produced);
|
|
2982
|
+
this._length += produced.length;
|
|
2983
|
+
parts.push(new Uint8Array(produced));
|
|
2984
|
+
}
|
|
2985
|
+
if (this._state.final && !this._state.lengthMap) {
|
|
2986
|
+
this._pending = this._pending.subarray(byteCeil(this._state.bitPos || 0));
|
|
2987
|
+
this._state.bitPos = 0;
|
|
2988
|
+
} else {
|
|
2989
|
+
this._pending = this._pending.subarray((this._state.bitPos || 0) / 8 | 0);
|
|
2990
|
+
this._state.bitPos = (this._state.bitPos || 0) & 7;
|
|
2991
|
+
}
|
|
2992
|
+
this._window = new Uint8Array(out.subarray(Math.max(0, (this._state.outputLength || 0) - 32768)));
|
|
2993
|
+
this._state.outputLength = this._window.length;
|
|
2994
|
+
if (this._state.final && !this._state.lengthMap) {
|
|
2995
|
+
if (this._pending.length < 8) {
|
|
2996
|
+
if (!final) break;
|
|
2997
|
+
throw new UnexpectedEofError();
|
|
2998
|
+
}
|
|
2999
|
+
if (this._check) {
|
|
3000
|
+
if (readU32LE(this._pending, 0) !== this._crc.digest() || readU32LE(this._pending, 4) !== this._length >>> 0) throw new ChecksumMismatchError();
|
|
3001
|
+
}
|
|
3002
|
+
this._pending = this._pending.subarray(8);
|
|
3003
|
+
this._needHeader = true;
|
|
3004
|
+
continue;
|
|
3005
|
+
}
|
|
3006
|
+
break;
|
|
3007
|
+
}
|
|
3008
|
+
if (final) this._done = true;
|
|
3009
|
+
return concatParts(parts);
|
|
3010
|
+
}
|
|
3011
|
+
};
|
|
3012
|
+
//#endregion
|
|
3013
|
+
//#region src/compress/zlib.ts
|
|
3014
|
+
function writeU32BE(buf, offset, value) {
|
|
3015
|
+
buf[offset] = value >>> 24;
|
|
3016
|
+
buf[offset + 1] = value >>> 16;
|
|
3017
|
+
buf[offset + 2] = value >>> 8;
|
|
3018
|
+
buf[offset + 3] = value;
|
|
3019
|
+
}
|
|
3020
|
+
function readU32BE(buf, offset) {
|
|
3021
|
+
return (buf[offset] << 24 | buf[offset + 1] << 16 | buf[offset + 2] << 8 | buf[offset + 3]) >>> 0;
|
|
3022
|
+
}
|
|
3023
|
+
function zlibHeaderSize(options) {
|
|
3024
|
+
return options.dictionary ? 6 : 2;
|
|
3025
|
+
}
|
|
3026
|
+
function writeZlibHeader(out, options) {
|
|
3027
|
+
let level = options.level ?? 6;
|
|
3028
|
+
let flevel = level === 0 ? 0 : level < 6 ? 1 : level === 9 ? 3 : 2;
|
|
3029
|
+
out[0] = 120;
|
|
3030
|
+
out[1] = flevel << 6 | (options.dictionary ? 32 : 0);
|
|
3031
|
+
out[1] += 31 - (out[0] << 8 | out[1]) % 31;
|
|
3032
|
+
if (options.dictionary) writeU32BE(out, 2, adler32(options.dictionary));
|
|
3033
|
+
}
|
|
3034
|
+
function zlibHeaderLength(data, dictionary) {
|
|
3035
|
+
if (data.length < 2) throw new UnexpectedEofError();
|
|
3036
|
+
if ((data[0] & 15) !== 8 || data[0] >> 4 > 7 || (data[0] << 8 | data[1]) % 31 !== 0) throw new InvalidHeaderError("Invalid zlib header.");
|
|
3037
|
+
let hasDict = data[1] >> 5 & 1;
|
|
3038
|
+
if (hasDict && !dictionary) throw new InvalidHeaderError("zlib stream requires a dictionary.");
|
|
3039
|
+
if (!hasDict && dictionary) throw new InvalidHeaderError("zlib stream has no dictionary.");
|
|
3040
|
+
let size = hasDict ? 6 : 2;
|
|
3041
|
+
if (data.length < size) throw new UnexpectedEofError();
|
|
3042
|
+
return size;
|
|
3043
|
+
}
|
|
3044
|
+
function compress(data, options = {}) {
|
|
3045
|
+
let out = deflateWithOptions(data, options, zlibHeaderSize(options), 4);
|
|
3046
|
+
writeZlibHeader(out, options);
|
|
3047
|
+
writeU32BE(out, out.length - 4, adler32(data));
|
|
3048
|
+
return out;
|
|
3049
|
+
}
|
|
3050
|
+
function decompress$1(data, options) {
|
|
3051
|
+
let header = zlibHeaderLength(data, options?.dictionary);
|
|
3052
|
+
let state = { mode: 2 };
|
|
3053
|
+
let payload = inflateRaw(data.subarray(header), state, options?.out, options?.dictionary);
|
|
3054
|
+
let trailer = header + byteCeil(state.bitPos || 0);
|
|
3055
|
+
if (options?.check !== false) {
|
|
3056
|
+
if (trailer + 4 > data.length) throw new UnexpectedEofError();
|
|
3057
|
+
if (readU32BE(data, trailer) !== adler32(payload)) throw new ChecksumMismatchError();
|
|
3058
|
+
}
|
|
3059
|
+
return payload;
|
|
3060
|
+
}
|
|
3061
|
+
var Compressor = class {
|
|
3062
|
+
_inner;
|
|
3063
|
+
_options;
|
|
3064
|
+
_sum = new Adler32();
|
|
3065
|
+
_header = false;
|
|
3066
|
+
_done = false;
|
|
3067
|
+
constructor(options = {}) {
|
|
3068
|
+
this._options = options;
|
|
3069
|
+
this._inner = new Compressor$2(options);
|
|
3070
|
+
}
|
|
3071
|
+
_wrap(raw, final) {
|
|
3072
|
+
let header = 0;
|
|
3073
|
+
if (!this._header) {
|
|
3074
|
+
header = zlibHeaderSize(this._options);
|
|
3075
|
+
this._header = true;
|
|
3076
|
+
}
|
|
3077
|
+
let footer = final ? 4 : 0;
|
|
3078
|
+
if (!header && !footer) return raw;
|
|
3079
|
+
let out = new Uint8Array(header + raw.length + footer);
|
|
3080
|
+
if (header) writeZlibHeader(out, this._options);
|
|
3081
|
+
out.set(raw, header);
|
|
3082
|
+
if (footer) writeU32BE(out, header + raw.length, this._sum.digest());
|
|
3083
|
+
return out;
|
|
3084
|
+
}
|
|
3085
|
+
push(chunk, final) {
|
|
3086
|
+
if (this._done) throw new StreamFinishedError();
|
|
3087
|
+
this._sum.update(chunk);
|
|
3088
|
+
this._done = !!final;
|
|
3089
|
+
return this._wrap(this._inner.push(chunk, final), !!final);
|
|
3090
|
+
}
|
|
3091
|
+
flush(sync) {
|
|
3092
|
+
if (this._done) throw new StreamFinishedError();
|
|
3093
|
+
return this._wrap(this._inner.flush(sync), false);
|
|
3094
|
+
}
|
|
3095
|
+
};
|
|
3096
|
+
var Decompressor = class {
|
|
3097
|
+
_pending = empty$1;
|
|
3098
|
+
_header = false;
|
|
3099
|
+
_state = { mode: 0 };
|
|
3100
|
+
_window = new Uint8Array(32768);
|
|
3101
|
+
_sum = new Adler32();
|
|
3102
|
+
_dictionary;
|
|
3103
|
+
_check;
|
|
3104
|
+
_done = false;
|
|
3105
|
+
constructor(options) {
|
|
3106
|
+
this._dictionary = options?.dictionary;
|
|
3107
|
+
this._check = options?.check !== false;
|
|
3108
|
+
if (options?.dictionary) this._window.set(options.dictionary.subarray(-32768), 32768 - Math.min(32768, options.dictionary.length));
|
|
3109
|
+
}
|
|
3110
|
+
push(chunk, final) {
|
|
3111
|
+
if (this._done) throw new StreamFinishedError();
|
|
3112
|
+
if (!this._pending.length) this._pending = chunk;
|
|
3113
|
+
else if (chunk.length) {
|
|
3114
|
+
let next = new Uint8Array(this._pending.length + chunk.length);
|
|
3115
|
+
next.set(this._pending);
|
|
3116
|
+
next.set(chunk, this._pending.length);
|
|
3117
|
+
this._pending = next;
|
|
3118
|
+
}
|
|
3119
|
+
if (!this._header) {
|
|
3120
|
+
if (this._pending.length < 2 && !final) return empty$1;
|
|
3121
|
+
let size = zlibHeaderLength(this._pending, this._dictionary);
|
|
3122
|
+
if (this._dictionary) {
|
|
3123
|
+
this._state.outputLength = Math.min(32768, this._dictionary.length);
|
|
3124
|
+
this._window = new Uint8Array(32768);
|
|
3125
|
+
this._window.set(this._dictionary.subarray(-32768));
|
|
3126
|
+
}
|
|
3127
|
+
this._pending = this._pending.subarray(size);
|
|
3128
|
+
this._header = true;
|
|
3129
|
+
}
|
|
3130
|
+
this._state.mode = final ? 1 : 0;
|
|
3131
|
+
let start = this._state.outputLength || 0;
|
|
3132
|
+
let out = inflateRaw(this._pending, this._state, this._window, this._header && !start ? this._dictionary : void 0);
|
|
3133
|
+
let produced = new Uint8Array(out.subarray(start, Math.min(this._state.outputLength || 0, out.length)));
|
|
3134
|
+
if (produced.length) this._sum.update(produced);
|
|
3135
|
+
if (this._state.final && !this._state.lengthMap) {
|
|
3136
|
+
this._pending = this._pending.subarray(byteCeil(this._state.bitPos || 0));
|
|
3137
|
+
this._state.bitPos = 0;
|
|
3138
|
+
} else {
|
|
3139
|
+
this._pending = this._pending.subarray((this._state.bitPos || 0) / 8 | 0);
|
|
3140
|
+
this._state.bitPos = (this._state.bitPos || 0) & 7;
|
|
3141
|
+
}
|
|
3142
|
+
this._window = new Uint8Array(out.subarray(Math.max(0, (this._state.outputLength || 0) - 32768)));
|
|
3143
|
+
this._state.outputLength = this._window.length;
|
|
3144
|
+
if (final) {
|
|
3145
|
+
this._done = true;
|
|
3146
|
+
if (this._check && this._state.final) {
|
|
3147
|
+
if (this._pending.length < 4) throw new UnexpectedEofError();
|
|
3148
|
+
if (readU32BE(this._pending, 0) !== this._sum.digest()) throw new ChecksumMismatchError();
|
|
3149
|
+
}
|
|
3150
|
+
}
|
|
3151
|
+
return produced.length ? produced : empty$1;
|
|
3152
|
+
}
|
|
3153
|
+
};
|
|
3154
|
+
//#endregion
|
|
3155
|
+
//#region src/compress/compress.ts
|
|
3156
|
+
var compress_exports = /* @__PURE__ */ __exportAll({
|
|
3157
|
+
Deflate: () => Compressor$2,
|
|
3158
|
+
Gzip: () => Compressor$1,
|
|
3159
|
+
Zlib: () => Compressor,
|
|
3160
|
+
deflate: () => compress$2,
|
|
3161
|
+
gzip: () => compress$1,
|
|
3162
|
+
zlib: () => compress
|
|
3163
|
+
});
|
|
3164
|
+
//#endregion
|
|
3165
|
+
//#region src/compress/detect.ts
|
|
3166
|
+
function decompress(data, options) {
|
|
3167
|
+
if (data.length >= 2 && data[0] === 31 && data[1] === 139) return decompress$2(data, options);
|
|
3168
|
+
if (data.length >= 2 && (data[0] & 15) === 8 && data[0] >> 4 <= 7 && (data[0] << 8 | data[1]) % 31 === 0) return decompress$1(data, options);
|
|
3169
|
+
return decompress$3(data, options);
|
|
3170
|
+
}
|
|
3171
|
+
//#endregion
|
|
3172
|
+
//#region src/compress/decompress.ts
|
|
3173
|
+
var decompress_exports = /* @__PURE__ */ __exportAll({
|
|
3174
|
+
Deflate: () => Decompressor$2,
|
|
3175
|
+
Gzip: () => Decompressor$1,
|
|
3176
|
+
Zlib: () => Decompressor,
|
|
3177
|
+
auto: () => decompress,
|
|
3178
|
+
deflate: () => decompress$3,
|
|
3179
|
+
gzip: () => decompress$2,
|
|
3180
|
+
zlib: () => decompress$1
|
|
3181
|
+
});
|
|
3182
|
+
//#endregion
|
|
3183
|
+
export { Adler32, AggregateError, AsyncInterval, AsyncLock, AsyncQueue, AsyncResource, ChecksumMismatchError, ConditionVariable, Crc32, CustomMap, CustomSet, Deferred, DeferredTracked, Deque, Emitter, FlateError, InvalidBlockTypeError, InvalidDistanceError, InvalidHeaderError, InvalidLengthLiteralError, LruMap, LruSet, NotImplementedError, StreamFinishedError, UnexpectedEofError, abs, adler32, asNonNull, assert, assertEndsWith, assertEndsWith as assertsEndsWith, assertHashKey, assertMatches, assertNotNull, assertStartsWith, asyncPool, base64_exports as base64, bitLength, clearUndefinedInPlace, composeMiddlewares, compress_exports as compress, crc32, decompress_exports as decompress, deepMerge, enumerate, euclideanGcd, fromBytes, hex_exports as hex, isBigInt, isBoolean, isFalsy, isFunction, isNotNull, isNotUndefined, isNumber, isObject, isString, isSymbol, isTruthy, max, max2, min, min2, modInv, modPowBinary, noop, objectEntries, objectKeys, parallelMap, sleep, splitOnce, throwNotImplemented, throwUnreachable, timers_exports as timers, toBytes, twoMultiplicity, typed_exports as typed, u8_exports as u8, unknownToError, unsafeCastType, utf8_exports as utf8 };
|