x_ite-sog-parser 1.0.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/LICENSE.md +21 -0
- package/README.md +40 -0
- package/dist/x_ite-sog-parser-2.js +3148 -0
- package/package.json +49 -0
|
@@ -0,0 +1,3148 @@
|
|
|
1
|
+
|
|
2
|
+
;// ./node_modules/fflate/esm/browser.js
|
|
3
|
+
// DEFLATE is a complex format; to read this code, you should probably check the RFC first:
|
|
4
|
+
// https://tools.ietf.org/html/rfc1951
|
|
5
|
+
// You may also wish to take a look at the guide I made about this program:
|
|
6
|
+
// https://gist.github.com/101arrowz/253f31eb5abc3d9275ab943003ffecad
|
|
7
|
+
// Some of the following code is similar to that of UZIP.js:
|
|
8
|
+
// https://github.com/photopea/UZIP.js
|
|
9
|
+
// However, the vast majority of the codebase has diverged from UZIP.js to increase performance and reduce bundle size.
|
|
10
|
+
// Sometimes 0 will appear where -1 would be more appropriate. This is because using a uint
|
|
11
|
+
// is better for memory in most engines (I *think*).
|
|
12
|
+
var ch2 = (/* unused pure expression or super */ null && ({}));
|
|
13
|
+
var wk = (function (c, id, msg, transfer, cb) {
|
|
14
|
+
var w = new Worker(ch2[id] || (ch2[id] = URL.createObjectURL(new Blob([
|
|
15
|
+
c + ';addEventListener("error",function(e){e=e.error;postMessage({$e$:[e.message,e.code,e.stack]})})'
|
|
16
|
+
], { type: 'text/javascript' }))));
|
|
17
|
+
w.onmessage = function (e) {
|
|
18
|
+
var d = e.data, ed = d.$e$;
|
|
19
|
+
if (ed) {
|
|
20
|
+
var err = new Error(ed[0]);
|
|
21
|
+
err['code'] = ed[1];
|
|
22
|
+
err.stack = ed[2];
|
|
23
|
+
cb(err, null);
|
|
24
|
+
}
|
|
25
|
+
else
|
|
26
|
+
cb(null, d);
|
|
27
|
+
};
|
|
28
|
+
w.postMessage(msg, transfer);
|
|
29
|
+
return w;
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// aliases for shorter compressed code (most minifers don't do this)
|
|
33
|
+
var u8 = Uint8Array, u16 = Uint16Array, i32 = Int32Array;
|
|
34
|
+
// fixed length extra bits
|
|
35
|
+
var fleb = new u8([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, /* unused */ 0, 0, /* impossible */ 0]);
|
|
36
|
+
// fixed distance extra bits
|
|
37
|
+
var fdeb = new u8([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, /* unused */ 0, 0]);
|
|
38
|
+
// code length index map
|
|
39
|
+
var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
|
|
40
|
+
// get base, reverse index map from extra bits
|
|
41
|
+
var freb = function (eb, start) {
|
|
42
|
+
var b = new u16(31);
|
|
43
|
+
for (var i = 0; i < 31; ++i) {
|
|
44
|
+
b[i] = start += 1 << eb[i - 1];
|
|
45
|
+
}
|
|
46
|
+
// numbers here are at max 18 bits
|
|
47
|
+
var r = new i32(b[30]);
|
|
48
|
+
for (var i = 1; i < 30; ++i) {
|
|
49
|
+
for (var j = b[i]; j < b[i + 1]; ++j) {
|
|
50
|
+
r[j] = ((j - b[i]) << 5) | i;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { b: b, r: r };
|
|
54
|
+
};
|
|
55
|
+
var _a = freb(fleb, 2), fl = _a.b, revfl = _a.r;
|
|
56
|
+
// we can ignore the fact that the other numbers are wrong; they never happen anyway
|
|
57
|
+
fl[28] = 258, revfl[258] = 28;
|
|
58
|
+
var _b = freb(fdeb, 0), fd = _b.b, revfd = _b.r;
|
|
59
|
+
// map of value to reverse (assuming 16 bits)
|
|
60
|
+
var rev = new u16(32768);
|
|
61
|
+
for (var i = 0; i < 32768; ++i) {
|
|
62
|
+
// reverse table algorithm from SO
|
|
63
|
+
var x = ((i & 0xAAAA) >> 1) | ((i & 0x5555) << 1);
|
|
64
|
+
x = ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2);
|
|
65
|
+
x = ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4);
|
|
66
|
+
rev[i] = (((x & 0xFF00) >> 8) | ((x & 0x00FF) << 8)) >> 1;
|
|
67
|
+
}
|
|
68
|
+
// create huffman tree from u8 "map": index -> code length for code index
|
|
69
|
+
// mb (max bits) must be at most 15
|
|
70
|
+
// TODO: optimize/split up?
|
|
71
|
+
var hMap = (function (cd, mb, r) {
|
|
72
|
+
var s = cd.length;
|
|
73
|
+
// index
|
|
74
|
+
var i = 0;
|
|
75
|
+
// u16 "map": index -> # of codes with bit length = index
|
|
76
|
+
var l = new u16(mb);
|
|
77
|
+
// length of cd must be 288 (total # of codes)
|
|
78
|
+
for (; i < s; ++i) {
|
|
79
|
+
if (cd[i])
|
|
80
|
+
++l[cd[i] - 1];
|
|
81
|
+
}
|
|
82
|
+
// u16 "map": index -> minimum code for bit length = index
|
|
83
|
+
var le = new u16(mb);
|
|
84
|
+
for (i = 1; i < mb; ++i) {
|
|
85
|
+
le[i] = (le[i - 1] + l[i - 1]) << 1;
|
|
86
|
+
}
|
|
87
|
+
var co;
|
|
88
|
+
if (r) {
|
|
89
|
+
// u16 "map": index -> number of actual bits, symbol for code
|
|
90
|
+
co = new u16(1 << mb);
|
|
91
|
+
// bits to remove for reverser
|
|
92
|
+
var rvb = 15 - mb;
|
|
93
|
+
for (i = 0; i < s; ++i) {
|
|
94
|
+
// ignore 0 lengths
|
|
95
|
+
if (cd[i]) {
|
|
96
|
+
// num encoding both symbol and bits read
|
|
97
|
+
var sv = (i << 4) | cd[i];
|
|
98
|
+
// free bits
|
|
99
|
+
var r_1 = mb - cd[i];
|
|
100
|
+
// start value
|
|
101
|
+
var v = le[cd[i] - 1]++ << r_1;
|
|
102
|
+
// m is end value
|
|
103
|
+
for (var m = v | ((1 << r_1) - 1); v <= m; ++v) {
|
|
104
|
+
// every 16 bit value starting with the code yields the same result
|
|
105
|
+
co[rev[v] >> rvb] = sv;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
co = new u16(s);
|
|
112
|
+
for (i = 0; i < s; ++i) {
|
|
113
|
+
if (cd[i]) {
|
|
114
|
+
co[i] = rev[le[cd[i] - 1]++] >> (15 - cd[i]);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return co;
|
|
119
|
+
});
|
|
120
|
+
// fixed length tree
|
|
121
|
+
var flt = new u8(288);
|
|
122
|
+
for (var i = 0; i < 144; ++i)
|
|
123
|
+
flt[i] = 8;
|
|
124
|
+
for (var i = 144; i < 256; ++i)
|
|
125
|
+
flt[i] = 9;
|
|
126
|
+
for (var i = 256; i < 280; ++i)
|
|
127
|
+
flt[i] = 7;
|
|
128
|
+
for (var i = 280; i < 288; ++i)
|
|
129
|
+
flt[i] = 8;
|
|
130
|
+
// fixed distance tree
|
|
131
|
+
var fdt = new u8(32);
|
|
132
|
+
for (var i = 0; i < 32; ++i)
|
|
133
|
+
fdt[i] = 5;
|
|
134
|
+
// fixed length map
|
|
135
|
+
var flm = /*#__PURE__*/ (/* unused pure expression or super */ null && (hMap(flt, 9, 0))), flrm = /*#__PURE__*/ hMap(flt, 9, 1);
|
|
136
|
+
// fixed distance map
|
|
137
|
+
var fdm = /*#__PURE__*/ (/* unused pure expression or super */ null && (hMap(fdt, 5, 0))), fdrm = /*#__PURE__*/ hMap(fdt, 5, 1);
|
|
138
|
+
// find max of array
|
|
139
|
+
var max = function (a) {
|
|
140
|
+
var m = a[0];
|
|
141
|
+
for (var i = 1; i < a.length; ++i) {
|
|
142
|
+
if (a[i] > m)
|
|
143
|
+
m = a[i];
|
|
144
|
+
}
|
|
145
|
+
return m;
|
|
146
|
+
};
|
|
147
|
+
// read d, starting at bit p and mask with m
|
|
148
|
+
var bits = function (d, p, m) {
|
|
149
|
+
var o = (p / 8) | 0;
|
|
150
|
+
return ((d[o] | (d[o + 1] << 8)) >> (p & 7)) & m;
|
|
151
|
+
};
|
|
152
|
+
// read d, starting at bit p continuing for at least 16 bits
|
|
153
|
+
var bits16 = function (d, p) {
|
|
154
|
+
var o = (p / 8) | 0;
|
|
155
|
+
return ((d[o] | (d[o + 1] << 8) | (d[o + 2] << 16)) >> (p & 7));
|
|
156
|
+
};
|
|
157
|
+
// get end of byte
|
|
158
|
+
var shft = function (p) { return ((p + 7) / 8) | 0; };
|
|
159
|
+
// typed array slice - allows garbage collector to free original reference,
|
|
160
|
+
// while being more compatible than .slice
|
|
161
|
+
var slc = function (v, s, e) {
|
|
162
|
+
if (s == null || s < 0)
|
|
163
|
+
s = 0;
|
|
164
|
+
if (e == null || e > v.length)
|
|
165
|
+
e = v.length;
|
|
166
|
+
// can't use .constructor in case user-supplied
|
|
167
|
+
return new u8(v.subarray(s, e));
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Codes for errors generated within this library
|
|
171
|
+
*/
|
|
172
|
+
var FlateErrorCode = (/* unused pure expression or super */ null && ({
|
|
173
|
+
UnexpectedEOF: 0,
|
|
174
|
+
InvalidBlockType: 1,
|
|
175
|
+
InvalidLengthLiteral: 2,
|
|
176
|
+
InvalidDistance: 3,
|
|
177
|
+
StreamFinished: 4,
|
|
178
|
+
NoStreamHandler: 5,
|
|
179
|
+
InvalidHeader: 6,
|
|
180
|
+
NoCallback: 7,
|
|
181
|
+
InvalidUTF8: 8,
|
|
182
|
+
ExtraFieldTooLong: 9,
|
|
183
|
+
InvalidDate: 10,
|
|
184
|
+
FilenameTooLong: 11,
|
|
185
|
+
StreamFinishing: 12,
|
|
186
|
+
InvalidZipData: 13,
|
|
187
|
+
UnknownCompressionMethod: 14
|
|
188
|
+
}));
|
|
189
|
+
// error codes
|
|
190
|
+
var ec = [
|
|
191
|
+
'unexpected EOF',
|
|
192
|
+
'invalid block type',
|
|
193
|
+
'invalid length/literal',
|
|
194
|
+
'invalid distance',
|
|
195
|
+
'stream finished',
|
|
196
|
+
'no stream handler',
|
|
197
|
+
, // determined by compression function
|
|
198
|
+
'no callback',
|
|
199
|
+
'invalid UTF-8 data',
|
|
200
|
+
'extra field too long',
|
|
201
|
+
'date not in range 1980-2099',
|
|
202
|
+
'filename too long',
|
|
203
|
+
'stream finishing',
|
|
204
|
+
'invalid zip data'
|
|
205
|
+
// determined by unknown compression method
|
|
206
|
+
];
|
|
207
|
+
;
|
|
208
|
+
var err = function (ind, msg, nt) {
|
|
209
|
+
var e = new Error(msg || ec[ind]);
|
|
210
|
+
e.code = ind;
|
|
211
|
+
if (Error.captureStackTrace)
|
|
212
|
+
Error.captureStackTrace(e, err);
|
|
213
|
+
if (!nt)
|
|
214
|
+
throw e;
|
|
215
|
+
return e;
|
|
216
|
+
};
|
|
217
|
+
// expands raw DEFLATE data
|
|
218
|
+
var inflt = function (dat, st, buf, dict) {
|
|
219
|
+
// source length dict length
|
|
220
|
+
var sl = dat.length, dl = dict ? dict.length : 0;
|
|
221
|
+
if (!sl || st.f && !st.l)
|
|
222
|
+
return buf || new u8(0);
|
|
223
|
+
var noBuf = !buf;
|
|
224
|
+
// have to estimate size
|
|
225
|
+
var resize = noBuf || st.i != 2;
|
|
226
|
+
// no state
|
|
227
|
+
var noSt = st.i;
|
|
228
|
+
// Assumes roughly 33% compression ratio average
|
|
229
|
+
if (noBuf)
|
|
230
|
+
buf = new u8(sl * 3);
|
|
231
|
+
// ensure buffer can fit at least l elements
|
|
232
|
+
var cbuf = function (l) {
|
|
233
|
+
var bl = buf.length;
|
|
234
|
+
// need to increase size to fit
|
|
235
|
+
if (l > bl) {
|
|
236
|
+
// Double or set to necessary, whichever is greater
|
|
237
|
+
var nbuf = new u8(Math.max(bl * 2, l));
|
|
238
|
+
nbuf.set(buf);
|
|
239
|
+
buf = nbuf;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
// last chunk bitpos bytes
|
|
243
|
+
var final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n;
|
|
244
|
+
// total bits
|
|
245
|
+
var tbts = sl * 8;
|
|
246
|
+
do {
|
|
247
|
+
if (!lm) {
|
|
248
|
+
// BFINAL - this is only 1 when last chunk is next
|
|
249
|
+
final = bits(dat, pos, 1);
|
|
250
|
+
// type: 0 = no compression, 1 = fixed huffman, 2 = dynamic huffman
|
|
251
|
+
var type = bits(dat, pos + 1, 3);
|
|
252
|
+
pos += 3;
|
|
253
|
+
if (!type) {
|
|
254
|
+
// go to end of byte boundary
|
|
255
|
+
var s = shft(pos) + 4, l = dat[s - 4] | (dat[s - 3] << 8), t = s + l;
|
|
256
|
+
if (t > sl) {
|
|
257
|
+
if (noSt)
|
|
258
|
+
err(0);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
// ensure size
|
|
262
|
+
if (resize)
|
|
263
|
+
cbuf(bt + l);
|
|
264
|
+
// Copy over uncompressed data
|
|
265
|
+
buf.set(dat.subarray(s, t), bt);
|
|
266
|
+
// Get new bitpos, update byte count
|
|
267
|
+
st.b = bt += l, st.p = pos = t * 8, st.f = final;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
else if (type == 1)
|
|
271
|
+
lm = flrm, dm = fdrm, lbt = 9, dbt = 5;
|
|
272
|
+
else if (type == 2) {
|
|
273
|
+
// literal lengths
|
|
274
|
+
var hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4;
|
|
275
|
+
var tl = hLit + bits(dat, pos + 5, 31) + 1;
|
|
276
|
+
pos += 14;
|
|
277
|
+
// length+distance tree
|
|
278
|
+
var ldt = new u8(tl);
|
|
279
|
+
// code length tree
|
|
280
|
+
var clt = new u8(19);
|
|
281
|
+
for (var i = 0; i < hcLen; ++i) {
|
|
282
|
+
// use index map to get real code
|
|
283
|
+
clt[clim[i]] = bits(dat, pos + i * 3, 7);
|
|
284
|
+
}
|
|
285
|
+
pos += hcLen * 3;
|
|
286
|
+
// code lengths bits
|
|
287
|
+
var clb = max(clt), clbmsk = (1 << clb) - 1;
|
|
288
|
+
// code lengths map
|
|
289
|
+
var clm = hMap(clt, clb, 1);
|
|
290
|
+
for (var i = 0; i < tl;) {
|
|
291
|
+
var r = clm[bits(dat, pos, clbmsk)];
|
|
292
|
+
// bits read
|
|
293
|
+
pos += r & 15;
|
|
294
|
+
// symbol
|
|
295
|
+
var s = r >> 4;
|
|
296
|
+
// code length to copy
|
|
297
|
+
if (s < 16) {
|
|
298
|
+
ldt[i++] = s;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
// copy count
|
|
302
|
+
var c = 0, n = 0;
|
|
303
|
+
if (s == 16)
|
|
304
|
+
n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1];
|
|
305
|
+
else if (s == 17)
|
|
306
|
+
n = 3 + bits(dat, pos, 7), pos += 3;
|
|
307
|
+
else if (s == 18)
|
|
308
|
+
n = 11 + bits(dat, pos, 127), pos += 7;
|
|
309
|
+
while (n--)
|
|
310
|
+
ldt[i++] = c;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// length tree distance tree
|
|
314
|
+
var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
|
|
315
|
+
// max length bits
|
|
316
|
+
lbt = max(lt);
|
|
317
|
+
// max dist bits
|
|
318
|
+
dbt = max(dt);
|
|
319
|
+
lm = hMap(lt, lbt, 1);
|
|
320
|
+
dm = hMap(dt, dbt, 1);
|
|
321
|
+
}
|
|
322
|
+
else
|
|
323
|
+
err(1);
|
|
324
|
+
if (pos > tbts) {
|
|
325
|
+
if (noSt)
|
|
326
|
+
err(0);
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// Make sure the buffer can hold this + the largest possible addition
|
|
331
|
+
// Maximum chunk size (practically, theoretically infinite) is 2^17
|
|
332
|
+
if (resize)
|
|
333
|
+
cbuf(bt + 131072);
|
|
334
|
+
var lms = (1 << lbt) - 1, dms = (1 << dbt) - 1;
|
|
335
|
+
var lpos = pos;
|
|
336
|
+
for (;; lpos = pos) {
|
|
337
|
+
// bits read, code
|
|
338
|
+
var c = lm[bits16(dat, pos) & lms], sym = c >> 4;
|
|
339
|
+
pos += c & 15;
|
|
340
|
+
if (pos > tbts) {
|
|
341
|
+
if (noSt)
|
|
342
|
+
err(0);
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
if (!c)
|
|
346
|
+
err(2);
|
|
347
|
+
if (sym < 256)
|
|
348
|
+
buf[bt++] = sym;
|
|
349
|
+
else if (sym == 256) {
|
|
350
|
+
lpos = pos, lm = null;
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
var add = sym - 254;
|
|
355
|
+
// no extra bits needed if less
|
|
356
|
+
if (sym > 264) {
|
|
357
|
+
// index
|
|
358
|
+
var i = sym - 257, b = fleb[i];
|
|
359
|
+
add = bits(dat, pos, (1 << b) - 1) + fl[i];
|
|
360
|
+
pos += b;
|
|
361
|
+
}
|
|
362
|
+
// dist
|
|
363
|
+
var d = dm[bits16(dat, pos) & dms], dsym = d >> 4;
|
|
364
|
+
if (!d)
|
|
365
|
+
err(3);
|
|
366
|
+
pos += d & 15;
|
|
367
|
+
var dt = fd[dsym];
|
|
368
|
+
if (dsym > 3) {
|
|
369
|
+
var b = fdeb[dsym];
|
|
370
|
+
dt += bits16(dat, pos) & (1 << b) - 1, pos += b;
|
|
371
|
+
}
|
|
372
|
+
if (pos > tbts) {
|
|
373
|
+
if (noSt)
|
|
374
|
+
err(0);
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
if (resize)
|
|
378
|
+
cbuf(bt + 131072);
|
|
379
|
+
var end = bt + add;
|
|
380
|
+
if (bt < dt) {
|
|
381
|
+
var shift = dl - dt, dend = Math.min(dt, end);
|
|
382
|
+
if (shift + bt < 0)
|
|
383
|
+
err(3);
|
|
384
|
+
for (; bt < dend; ++bt)
|
|
385
|
+
buf[bt] = dict[shift + bt];
|
|
386
|
+
}
|
|
387
|
+
for (; bt < end; ++bt)
|
|
388
|
+
buf[bt] = buf[bt - dt];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
st.l = lm, st.p = lpos, st.b = bt, st.f = final;
|
|
392
|
+
if (lm)
|
|
393
|
+
final = 1, st.m = lbt, st.d = dm, st.n = dbt;
|
|
394
|
+
} while (!final);
|
|
395
|
+
// don't reallocate for streams or user buffers
|
|
396
|
+
return bt != buf.length && noBuf ? slc(buf, 0, bt) : buf.subarray(0, bt);
|
|
397
|
+
};
|
|
398
|
+
// starting at p, write the minimum number of bits that can hold v to d
|
|
399
|
+
var wbits = function (d, p, v) {
|
|
400
|
+
v <<= p & 7;
|
|
401
|
+
var o = (p / 8) | 0;
|
|
402
|
+
d[o] |= v;
|
|
403
|
+
d[o + 1] |= v >> 8;
|
|
404
|
+
};
|
|
405
|
+
// starting at p, write the minimum number of bits (>8) that can hold v to d
|
|
406
|
+
var wbits16 = function (d, p, v) {
|
|
407
|
+
v <<= p & 7;
|
|
408
|
+
var o = (p / 8) | 0;
|
|
409
|
+
d[o] |= v;
|
|
410
|
+
d[o + 1] |= v >> 8;
|
|
411
|
+
d[o + 2] |= v >> 16;
|
|
412
|
+
};
|
|
413
|
+
// creates code lengths from a frequency table
|
|
414
|
+
var hTree = function (d, mb) {
|
|
415
|
+
// Need extra info to make a tree
|
|
416
|
+
var t = [];
|
|
417
|
+
for (var i = 0; i < d.length; ++i) {
|
|
418
|
+
if (d[i])
|
|
419
|
+
t.push({ s: i, f: d[i] });
|
|
420
|
+
}
|
|
421
|
+
var s = t.length;
|
|
422
|
+
var t2 = t.slice();
|
|
423
|
+
if (!s)
|
|
424
|
+
return { t: et, l: 0 };
|
|
425
|
+
if (s == 1) {
|
|
426
|
+
var v = new u8(t[0].s + 1);
|
|
427
|
+
v[t[0].s] = 1;
|
|
428
|
+
return { t: v, l: 1 };
|
|
429
|
+
}
|
|
430
|
+
t.sort(function (a, b) { return a.f - b.f; });
|
|
431
|
+
// after i2 reaches last ind, will be stopped
|
|
432
|
+
// freq must be greater than largest possible number of symbols
|
|
433
|
+
t.push({ s: -1, f: 25001 });
|
|
434
|
+
var l = t[0], r = t[1], i0 = 0, i1 = 1, i2 = 2;
|
|
435
|
+
t[0] = { s: -1, f: l.f + r.f, l: l, r: r };
|
|
436
|
+
// efficient algorithm from UZIP.js
|
|
437
|
+
// i0 is lookbehind, i2 is lookahead - after processing two low-freq
|
|
438
|
+
// symbols that combined have high freq, will start processing i2 (high-freq,
|
|
439
|
+
// non-composite) symbols instead
|
|
440
|
+
// see https://reddit.com/r/photopea/comments/ikekht/uzipjs_questions/
|
|
441
|
+
while (i1 != s - 1) {
|
|
442
|
+
l = t[t[i0].f < t[i2].f ? i0++ : i2++];
|
|
443
|
+
r = t[i0 != i1 && t[i0].f < t[i2].f ? i0++ : i2++];
|
|
444
|
+
t[i1++] = { s: -1, f: l.f + r.f, l: l, r: r };
|
|
445
|
+
}
|
|
446
|
+
var maxSym = t2[0].s;
|
|
447
|
+
for (var i = 1; i < s; ++i) {
|
|
448
|
+
if (t2[i].s > maxSym)
|
|
449
|
+
maxSym = t2[i].s;
|
|
450
|
+
}
|
|
451
|
+
// code lengths
|
|
452
|
+
var tr = new u16(maxSym + 1);
|
|
453
|
+
// max bits in tree
|
|
454
|
+
var mbt = ln(t[i1 - 1], tr, 0);
|
|
455
|
+
if (mbt > mb) {
|
|
456
|
+
// more algorithms from UZIP.js
|
|
457
|
+
// TODO: find out how this code works (debt)
|
|
458
|
+
// ind debt
|
|
459
|
+
var i = 0, dt = 0;
|
|
460
|
+
// left cost
|
|
461
|
+
var lft = mbt - mb, cst = 1 << lft;
|
|
462
|
+
t2.sort(function (a, b) { return tr[b.s] - tr[a.s] || a.f - b.f; });
|
|
463
|
+
for (; i < s; ++i) {
|
|
464
|
+
var i2_1 = t2[i].s;
|
|
465
|
+
if (tr[i2_1] > mb) {
|
|
466
|
+
dt += cst - (1 << (mbt - tr[i2_1]));
|
|
467
|
+
tr[i2_1] = mb;
|
|
468
|
+
}
|
|
469
|
+
else
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
dt >>= lft;
|
|
473
|
+
while (dt > 0) {
|
|
474
|
+
var i2_2 = t2[i].s;
|
|
475
|
+
if (tr[i2_2] < mb)
|
|
476
|
+
dt -= 1 << (mb - tr[i2_2]++ - 1);
|
|
477
|
+
else
|
|
478
|
+
++i;
|
|
479
|
+
}
|
|
480
|
+
for (; i >= 0 && dt; --i) {
|
|
481
|
+
var i2_3 = t2[i].s;
|
|
482
|
+
if (tr[i2_3] == mb) {
|
|
483
|
+
--tr[i2_3];
|
|
484
|
+
++dt;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
mbt = mb;
|
|
488
|
+
}
|
|
489
|
+
return { t: new u8(tr), l: mbt };
|
|
490
|
+
};
|
|
491
|
+
// get the max length and assign length codes
|
|
492
|
+
var ln = function (n, l, d) {
|
|
493
|
+
return n.s == -1
|
|
494
|
+
? Math.max(ln(n.l, l, d + 1), ln(n.r, l, d + 1))
|
|
495
|
+
: (l[n.s] = d);
|
|
496
|
+
};
|
|
497
|
+
// length codes generation
|
|
498
|
+
var lc = function (c) {
|
|
499
|
+
var s = c.length;
|
|
500
|
+
// Note that the semicolon was intentional
|
|
501
|
+
while (s && !c[--s])
|
|
502
|
+
;
|
|
503
|
+
var cl = new u16(++s);
|
|
504
|
+
// ind num streak
|
|
505
|
+
var cli = 0, cln = c[0], cls = 1;
|
|
506
|
+
var w = function (v) { cl[cli++] = v; };
|
|
507
|
+
for (var i = 1; i <= s; ++i) {
|
|
508
|
+
if (c[i] == cln && i != s)
|
|
509
|
+
++cls;
|
|
510
|
+
else {
|
|
511
|
+
if (!cln && cls > 2) {
|
|
512
|
+
for (; cls > 138; cls -= 138)
|
|
513
|
+
w(32754);
|
|
514
|
+
if (cls > 2) {
|
|
515
|
+
w(cls > 10 ? ((cls - 11) << 5) | 28690 : ((cls - 3) << 5) | 12305);
|
|
516
|
+
cls = 0;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
else if (cls > 3) {
|
|
520
|
+
w(cln), --cls;
|
|
521
|
+
for (; cls > 6; cls -= 6)
|
|
522
|
+
w(8304);
|
|
523
|
+
if (cls > 2)
|
|
524
|
+
w(((cls - 3) << 5) | 8208), cls = 0;
|
|
525
|
+
}
|
|
526
|
+
while (cls--)
|
|
527
|
+
w(cln);
|
|
528
|
+
cls = 1;
|
|
529
|
+
cln = c[i];
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return { c: cl.subarray(0, cli), n: s };
|
|
533
|
+
};
|
|
534
|
+
// calculate the length of output from tree, code lengths
|
|
535
|
+
var clen = function (cf, cl) {
|
|
536
|
+
var l = 0;
|
|
537
|
+
for (var i = 0; i < cl.length; ++i)
|
|
538
|
+
l += cf[i] * cl[i];
|
|
539
|
+
return l;
|
|
540
|
+
};
|
|
541
|
+
// writes a fixed block
|
|
542
|
+
// returns the new bit pos
|
|
543
|
+
var wfblk = function (out, pos, dat) {
|
|
544
|
+
// no need to write 00 as type: TypedArray defaults to 0
|
|
545
|
+
var s = dat.length;
|
|
546
|
+
var o = shft(pos + 2);
|
|
547
|
+
out[o] = s & 255;
|
|
548
|
+
out[o + 1] = s >> 8;
|
|
549
|
+
out[o + 2] = out[o] ^ 255;
|
|
550
|
+
out[o + 3] = out[o + 1] ^ 255;
|
|
551
|
+
for (var i = 0; i < s; ++i)
|
|
552
|
+
out[o + i + 4] = dat[i];
|
|
553
|
+
return (o + 4 + s) * 8;
|
|
554
|
+
};
|
|
555
|
+
// writes a block
|
|
556
|
+
var wblk = function (dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
|
|
557
|
+
wbits(out, p++, final);
|
|
558
|
+
++lf[256];
|
|
559
|
+
var _a = hTree(lf, 15), dlt = _a.t, mlb = _a.l;
|
|
560
|
+
var _b = hTree(df, 15), ddt = _b.t, mdb = _b.l;
|
|
561
|
+
var _c = lc(dlt), lclt = _c.c, nlc = _c.n;
|
|
562
|
+
var _d = lc(ddt), lcdt = _d.c, ndc = _d.n;
|
|
563
|
+
var lcfreq = new u16(19);
|
|
564
|
+
for (var i = 0; i < lclt.length; ++i)
|
|
565
|
+
++lcfreq[lclt[i] & 31];
|
|
566
|
+
for (var i = 0; i < lcdt.length; ++i)
|
|
567
|
+
++lcfreq[lcdt[i] & 31];
|
|
568
|
+
var _e = hTree(lcfreq, 7), lct = _e.t, mlcb = _e.l;
|
|
569
|
+
var nlcc = 19;
|
|
570
|
+
for (; nlcc > 4 && !lct[clim[nlcc - 1]]; --nlcc)
|
|
571
|
+
;
|
|
572
|
+
var flen = (bl + 5) << 3;
|
|
573
|
+
var ftlen = clen(lf, flt) + clen(df, fdt) + eb;
|
|
574
|
+
var dtlen = clen(lf, dlt) + clen(df, ddt) + eb + 14 + 3 * nlcc + clen(lcfreq, lct) + 2 * lcfreq[16] + 3 * lcfreq[17] + 7 * lcfreq[18];
|
|
575
|
+
if (bs >= 0 && flen <= ftlen && flen <= dtlen)
|
|
576
|
+
return wfblk(out, p, dat.subarray(bs, bs + bl));
|
|
577
|
+
var lm, ll, dm, dl;
|
|
578
|
+
wbits(out, p, 1 + (dtlen < ftlen)), p += 2;
|
|
579
|
+
if (dtlen < ftlen) {
|
|
580
|
+
lm = hMap(dlt, mlb, 0), ll = dlt, dm = hMap(ddt, mdb, 0), dl = ddt;
|
|
581
|
+
var llm = hMap(lct, mlcb, 0);
|
|
582
|
+
wbits(out, p, nlc - 257);
|
|
583
|
+
wbits(out, p + 5, ndc - 1);
|
|
584
|
+
wbits(out, p + 10, nlcc - 4);
|
|
585
|
+
p += 14;
|
|
586
|
+
for (var i = 0; i < nlcc; ++i)
|
|
587
|
+
wbits(out, p + 3 * i, lct[clim[i]]);
|
|
588
|
+
p += 3 * nlcc;
|
|
589
|
+
var lcts = [lclt, lcdt];
|
|
590
|
+
for (var it = 0; it < 2; ++it) {
|
|
591
|
+
var clct = lcts[it];
|
|
592
|
+
for (var i = 0; i < clct.length; ++i) {
|
|
593
|
+
var len = clct[i] & 31;
|
|
594
|
+
wbits(out, p, llm[len]), p += lct[len];
|
|
595
|
+
if (len > 15)
|
|
596
|
+
wbits(out, p, (clct[i] >> 5) & 127), p += clct[i] >> 12;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
lm = flm, ll = flt, dm = fdm, dl = fdt;
|
|
602
|
+
}
|
|
603
|
+
for (var i = 0; i < li; ++i) {
|
|
604
|
+
var sym = syms[i];
|
|
605
|
+
if (sym > 255) {
|
|
606
|
+
var len = (sym >> 18) & 31;
|
|
607
|
+
wbits16(out, p, lm[len + 257]), p += ll[len + 257];
|
|
608
|
+
if (len > 7)
|
|
609
|
+
wbits(out, p, (sym >> 23) & 31), p += fleb[len];
|
|
610
|
+
var dst = sym & 31;
|
|
611
|
+
wbits16(out, p, dm[dst]), p += dl[dst];
|
|
612
|
+
if (dst > 3)
|
|
613
|
+
wbits16(out, p, (sym >> 5) & 8191), p += fdeb[dst];
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
wbits16(out, p, lm[sym]), p += ll[sym];
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
wbits16(out, p, lm[256]);
|
|
620
|
+
return p + ll[256];
|
|
621
|
+
};
|
|
622
|
+
// deflate options (nice << 13) | chain
|
|
623
|
+
var deo = /*#__PURE__*/ (/* unused pure expression or super */ null && (new i32([65540, 131080, 131088, 131104, 262176, 1048704, 1048832, 2114560, 2117632])));
|
|
624
|
+
// empty
|
|
625
|
+
var et = /*#__PURE__*/ new u8(0);
|
|
626
|
+
// compresses data into a raw DEFLATE buffer
|
|
627
|
+
var dflt = function (dat, lvl, plvl, pre, post, st) {
|
|
628
|
+
var s = st.z || dat.length;
|
|
629
|
+
var o = new u8(pre + s + 5 * (1 + Math.ceil(s / 7000)) + post);
|
|
630
|
+
// writing to this writes to the output buffer
|
|
631
|
+
var w = o.subarray(pre, o.length - post);
|
|
632
|
+
var lst = st.l;
|
|
633
|
+
var pos = (st.r || 0) & 7;
|
|
634
|
+
if (lvl) {
|
|
635
|
+
if (pos)
|
|
636
|
+
w[0] = st.r >> 3;
|
|
637
|
+
var opt = deo[lvl - 1];
|
|
638
|
+
var n = opt >> 13, c = opt & 8191;
|
|
639
|
+
var msk_1 = (1 << plvl) - 1;
|
|
640
|
+
// prev 2-byte val map curr 2-byte val map
|
|
641
|
+
var prev = st.p || new u16(32768), head = st.h || new u16(msk_1 + 1);
|
|
642
|
+
var bs1_1 = Math.ceil(plvl / 3), bs2_1 = 2 * bs1_1;
|
|
643
|
+
var hsh = function (i) { return (dat[i] ^ (dat[i + 1] << bs1_1) ^ (dat[i + 2] << bs2_1)) & msk_1; };
|
|
644
|
+
// 24576 is an arbitrary number of maximum symbols per block
|
|
645
|
+
// 424 buffer for last block
|
|
646
|
+
var syms = new i32(25000);
|
|
647
|
+
// length/literal freq distance freq
|
|
648
|
+
var lf = new u16(288), df = new u16(32);
|
|
649
|
+
// l/lcnt exbits index l/lind waitdx blkpos
|
|
650
|
+
var lc_1 = 0, eb = 0, i = st.i || 0, li = 0, wi = st.w || 0, bs = 0;
|
|
651
|
+
for (; i + 2 < s; ++i) {
|
|
652
|
+
// hash value
|
|
653
|
+
var hv = hsh(i);
|
|
654
|
+
// index mod 32768 previous index mod
|
|
655
|
+
var imod = i & 32767, pimod = head[hv];
|
|
656
|
+
prev[imod] = pimod;
|
|
657
|
+
head[hv] = imod;
|
|
658
|
+
// We always should modify head and prev, but only add symbols if
|
|
659
|
+
// this data is not yet processed ("wait" for wait index)
|
|
660
|
+
if (wi <= i) {
|
|
661
|
+
// bytes remaining
|
|
662
|
+
var rem = s - i;
|
|
663
|
+
if ((lc_1 > 7000 || li > 24576) && (rem > 423 || !lst)) {
|
|
664
|
+
pos = wblk(dat, w, 0, syms, lf, df, eb, li, bs, i - bs, pos);
|
|
665
|
+
li = lc_1 = eb = 0, bs = i;
|
|
666
|
+
for (var j = 0; j < 286; ++j)
|
|
667
|
+
lf[j] = 0;
|
|
668
|
+
for (var j = 0; j < 30; ++j)
|
|
669
|
+
df[j] = 0;
|
|
670
|
+
}
|
|
671
|
+
// len dist chain
|
|
672
|
+
var l = 2, d = 0, ch_1 = c, dif = imod - pimod & 32767;
|
|
673
|
+
if (rem > 2 && hv == hsh(i - dif)) {
|
|
674
|
+
var maxn = Math.min(n, rem) - 1;
|
|
675
|
+
var maxd = Math.min(32767, i);
|
|
676
|
+
// max possible length
|
|
677
|
+
// not capped at dif because decompressors implement "rolling" index population
|
|
678
|
+
var ml = Math.min(258, rem);
|
|
679
|
+
while (dif <= maxd && --ch_1 && imod != pimod) {
|
|
680
|
+
if (dat[i + l] == dat[i + l - dif]) {
|
|
681
|
+
var nl = 0;
|
|
682
|
+
for (; nl < ml && dat[i + nl] == dat[i + nl - dif]; ++nl)
|
|
683
|
+
;
|
|
684
|
+
if (nl > l) {
|
|
685
|
+
l = nl, d = dif;
|
|
686
|
+
// break out early when we reach "nice" (we are satisfied enough)
|
|
687
|
+
if (nl > maxn)
|
|
688
|
+
break;
|
|
689
|
+
// now, find the rarest 2-byte sequence within this
|
|
690
|
+
// length of literals and search for that instead.
|
|
691
|
+
// Much faster than just using the start
|
|
692
|
+
var mmd = Math.min(dif, nl - 2);
|
|
693
|
+
var md = 0;
|
|
694
|
+
for (var j = 0; j < mmd; ++j) {
|
|
695
|
+
var ti = i - dif + j & 32767;
|
|
696
|
+
var pti = prev[ti];
|
|
697
|
+
var cd = ti - pti & 32767;
|
|
698
|
+
if (cd > md)
|
|
699
|
+
md = cd, pimod = ti;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
// check the previous match
|
|
704
|
+
imod = pimod, pimod = prev[imod];
|
|
705
|
+
dif += imod - pimod & 32767;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
// d will be nonzero only when a match was found
|
|
709
|
+
if (d) {
|
|
710
|
+
// store both dist and len data in one int32
|
|
711
|
+
// Make sure this is recognized as a len/dist with 28th bit (2^28)
|
|
712
|
+
syms[li++] = 268435456 | (revfl[l] << 18) | revfd[d];
|
|
713
|
+
var lin = revfl[l] & 31, din = revfd[d] & 31;
|
|
714
|
+
eb += fleb[lin] + fdeb[din];
|
|
715
|
+
++lf[257 + lin];
|
|
716
|
+
++df[din];
|
|
717
|
+
wi = i + l;
|
|
718
|
+
++lc_1;
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
syms[li++] = dat[i];
|
|
722
|
+
++lf[dat[i]];
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
for (i = Math.max(i, wi); i < s; ++i) {
|
|
727
|
+
syms[li++] = dat[i];
|
|
728
|
+
++lf[dat[i]];
|
|
729
|
+
}
|
|
730
|
+
pos = wblk(dat, w, lst, syms, lf, df, eb, li, bs, i - bs, pos);
|
|
731
|
+
if (!lst) {
|
|
732
|
+
st.r = (pos & 7) | w[(pos / 8) | 0] << 3;
|
|
733
|
+
// shft(pos) now 1 less if pos & 7 != 0
|
|
734
|
+
pos -= 7;
|
|
735
|
+
st.h = head, st.p = prev, st.i = i, st.w = wi;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
for (var i = st.w || 0; i < s + lst; i += 65535) {
|
|
740
|
+
// end
|
|
741
|
+
var e = i + 65535;
|
|
742
|
+
if (e >= s) {
|
|
743
|
+
// write final block
|
|
744
|
+
w[(pos / 8) | 0] = lst;
|
|
745
|
+
e = s;
|
|
746
|
+
}
|
|
747
|
+
pos = wfblk(w, pos + 1, dat.subarray(i, e));
|
|
748
|
+
}
|
|
749
|
+
st.i = s;
|
|
750
|
+
}
|
|
751
|
+
return slc(o, 0, pre + shft(pos) + post);
|
|
752
|
+
};
|
|
753
|
+
// CRC32 table
|
|
754
|
+
var crct = /*#__PURE__*/ (/* unused pure expression or super */ null && ((function () {
|
|
755
|
+
var t = new Int32Array(256);
|
|
756
|
+
for (var i = 0; i < 256; ++i) {
|
|
757
|
+
var c = i, k = 9;
|
|
758
|
+
while (--k)
|
|
759
|
+
c = ((c & 1) && -306674912) ^ (c >>> 1);
|
|
760
|
+
t[i] = c;
|
|
761
|
+
}
|
|
762
|
+
return t;
|
|
763
|
+
})()));
|
|
764
|
+
// CRC32
|
|
765
|
+
var crc = function () {
|
|
766
|
+
var c = -1;
|
|
767
|
+
return {
|
|
768
|
+
p: function (d) {
|
|
769
|
+
// closures have awful performance
|
|
770
|
+
var cr = c;
|
|
771
|
+
for (var i = 0; i < d.length; ++i)
|
|
772
|
+
cr = crct[(cr & 255) ^ d[i]] ^ (cr >>> 8);
|
|
773
|
+
c = cr;
|
|
774
|
+
},
|
|
775
|
+
d: function () { return ~c; }
|
|
776
|
+
};
|
|
777
|
+
};
|
|
778
|
+
// Adler32
|
|
779
|
+
var adler = function () {
|
|
780
|
+
var a = 1, b = 0;
|
|
781
|
+
return {
|
|
782
|
+
p: function (d) {
|
|
783
|
+
// closures have awful performance
|
|
784
|
+
var n = a, m = b;
|
|
785
|
+
var l = d.length | 0;
|
|
786
|
+
for (var i = 0; i != l;) {
|
|
787
|
+
var e = Math.min(i + 2655, l);
|
|
788
|
+
for (; i < e; ++i)
|
|
789
|
+
m += n += d[i];
|
|
790
|
+
n = (n & 65535) + 15 * (n >> 16), m = (m & 65535) + 15 * (m >> 16);
|
|
791
|
+
}
|
|
792
|
+
a = n, b = m;
|
|
793
|
+
},
|
|
794
|
+
d: function () {
|
|
795
|
+
a %= 65521, b %= 65521;
|
|
796
|
+
return (a & 255) << 24 | (a & 0xFF00) << 8 | (b & 255) << 8 | (b >> 8);
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
};
|
|
800
|
+
;
|
|
801
|
+
// deflate with opts
|
|
802
|
+
var dopt = function (dat, opt, pre, post, st) {
|
|
803
|
+
if (!st) {
|
|
804
|
+
st = { l: 1 };
|
|
805
|
+
if (opt.dictionary) {
|
|
806
|
+
var dict = opt.dictionary.subarray(-32768);
|
|
807
|
+
var newDat = new u8(dict.length + dat.length);
|
|
808
|
+
newDat.set(dict);
|
|
809
|
+
newDat.set(dat, dict.length);
|
|
810
|
+
dat = newDat;
|
|
811
|
+
st.w = dict.length;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return dflt(dat, opt.level == null ? 6 : opt.level, opt.mem == null ? (st.l ? Math.ceil(Math.max(8, Math.min(13, Math.log(dat.length))) * 1.5) : 20) : (12 + opt.mem), pre, post, st);
|
|
815
|
+
};
|
|
816
|
+
// Walmart object spread
|
|
817
|
+
var mrg = function (a, b) {
|
|
818
|
+
var o = {};
|
|
819
|
+
for (var k in a)
|
|
820
|
+
o[k] = a[k];
|
|
821
|
+
for (var k in b)
|
|
822
|
+
o[k] = b[k];
|
|
823
|
+
return o;
|
|
824
|
+
};
|
|
825
|
+
// worker clone
|
|
826
|
+
// This is possibly the craziest part of the entire codebase, despite how simple it may seem.
|
|
827
|
+
// The only parameter to this function is a closure that returns an array of variables outside of the function scope.
|
|
828
|
+
// We're going to try to figure out the variable names used in the closure as strings because that is crucial for workerization.
|
|
829
|
+
// We will return an object mapping of true variable name to value (basically, the current scope as a JS object).
|
|
830
|
+
// The reason we can't just use the original variable names is minifiers mangling the toplevel scope.
|
|
831
|
+
// This took me three weeks to figure out how to do.
|
|
832
|
+
var wcln = function (fn, fnStr, td) {
|
|
833
|
+
var dt = fn();
|
|
834
|
+
var st = fn.toString();
|
|
835
|
+
var ks = st.slice(st.indexOf('[') + 1, st.lastIndexOf(']')).replace(/\s+/g, '').split(',');
|
|
836
|
+
for (var i = 0; i < dt.length; ++i) {
|
|
837
|
+
var v = dt[i], k = ks[i];
|
|
838
|
+
if (typeof v == 'function') {
|
|
839
|
+
fnStr += ';' + k + '=';
|
|
840
|
+
var st_1 = v.toString();
|
|
841
|
+
if (v.prototype) {
|
|
842
|
+
// for global objects
|
|
843
|
+
if (st_1.indexOf('[native code]') != -1) {
|
|
844
|
+
var spInd = st_1.indexOf(' ', 8) + 1;
|
|
845
|
+
fnStr += st_1.slice(spInd, st_1.indexOf('(', spInd));
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
fnStr += st_1;
|
|
849
|
+
for (var t in v.prototype)
|
|
850
|
+
fnStr += ';' + k + '.prototype.' + t + '=' + v.prototype[t].toString();
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
else
|
|
854
|
+
fnStr += st_1;
|
|
855
|
+
}
|
|
856
|
+
else
|
|
857
|
+
td[k] = v;
|
|
858
|
+
}
|
|
859
|
+
return fnStr;
|
|
860
|
+
};
|
|
861
|
+
var ch = (/* unused pure expression or super */ null && ([]));
|
|
862
|
+
// clone bufs
|
|
863
|
+
var cbfs = function (v) {
|
|
864
|
+
var tl = [];
|
|
865
|
+
for (var k in v) {
|
|
866
|
+
if (v[k].buffer) {
|
|
867
|
+
tl.push((v[k] = new v[k].constructor(v[k])).buffer);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
return tl;
|
|
871
|
+
};
|
|
872
|
+
// use a worker to execute code
|
|
873
|
+
var wrkr = function (fns, init, id, cb) {
|
|
874
|
+
if (!ch[id]) {
|
|
875
|
+
var fnStr = '', td_1 = {}, m = fns.length - 1;
|
|
876
|
+
for (var i = 0; i < m; ++i)
|
|
877
|
+
fnStr = wcln(fns[i], fnStr, td_1);
|
|
878
|
+
ch[id] = { c: wcln(fns[m], fnStr, td_1), e: td_1 };
|
|
879
|
+
}
|
|
880
|
+
var td = mrg({}, ch[id].e);
|
|
881
|
+
return wk(ch[id].c + ';onmessage=function(e){for(var k in e.data)self[k]=e.data[k];onmessage=' + init.toString() + '}', id, td, cbfs(td), cb);
|
|
882
|
+
};
|
|
883
|
+
// base async inflate fn
|
|
884
|
+
var bInflt = function () { return [u8, u16, i32, fleb, fdeb, clim, fl, fd, flrm, fdrm, rev, ec, hMap, max, bits, bits16, shft, slc, err, inflt, inflateSync, pbf, gopt]; };
|
|
885
|
+
var bDflt = function () { return [u8, u16, i32, fleb, fdeb, clim, revfl, revfd, flm, flt, fdm, fdt, rev, deo, et, hMap, wbits, wbits16, hTree, ln, lc, clen, wfblk, wblk, shft, slc, dflt, dopt, deflateSync, pbf]; };
|
|
886
|
+
// gzip extra
|
|
887
|
+
var gze = function () { return [gzh, gzhl, wbytes, crc, crct]; };
|
|
888
|
+
// gunzip extra
|
|
889
|
+
var guze = function () { return [gzs, gzl]; };
|
|
890
|
+
// zlib extra
|
|
891
|
+
var zle = function () { return [zlh, wbytes, adler]; };
|
|
892
|
+
// unzlib extra
|
|
893
|
+
var zule = function () { return [zls]; };
|
|
894
|
+
// post buf
|
|
895
|
+
var pbf = function (msg) { return postMessage(msg, [msg.buffer]); };
|
|
896
|
+
// get opts
|
|
897
|
+
var gopt = function (o) { return o && {
|
|
898
|
+
out: o.size && new u8(o.size),
|
|
899
|
+
dictionary: o.dictionary
|
|
900
|
+
}; };
|
|
901
|
+
// async helper
|
|
902
|
+
var cbify = function (dat, opts, fns, init, id, cb) {
|
|
903
|
+
var w = wrkr(fns, init, id, function (err, dat) {
|
|
904
|
+
w.terminate();
|
|
905
|
+
cb(err, dat);
|
|
906
|
+
});
|
|
907
|
+
w.postMessage([dat, opts], opts.consume ? [dat.buffer] : []);
|
|
908
|
+
return function () { w.terminate(); };
|
|
909
|
+
};
|
|
910
|
+
// auto stream
|
|
911
|
+
var astrm = function (strm) {
|
|
912
|
+
strm.ondata = function (dat, final) { return postMessage([dat, final], [dat.buffer]); };
|
|
913
|
+
return function (ev) {
|
|
914
|
+
if (ev.data[0]) {
|
|
915
|
+
strm.push(ev.data[0], ev.data[1]);
|
|
916
|
+
postMessage([ev.data[0].length]);
|
|
917
|
+
}
|
|
918
|
+
else
|
|
919
|
+
strm.flush(ev.data[1]);
|
|
920
|
+
};
|
|
921
|
+
};
|
|
922
|
+
// async stream attach
|
|
923
|
+
var astrmify = function (fns, strm, opts, init, id, flush, ext) {
|
|
924
|
+
var t;
|
|
925
|
+
var w = wrkr(fns, init, id, function (err, dat) {
|
|
926
|
+
if (err)
|
|
927
|
+
w.terminate(), strm.ondata.call(strm, err);
|
|
928
|
+
else if (!Array.isArray(dat))
|
|
929
|
+
ext(dat);
|
|
930
|
+
else if (dat.length == 1) {
|
|
931
|
+
strm.queuedSize -= dat[0];
|
|
932
|
+
if (strm.ondrain)
|
|
933
|
+
strm.ondrain(dat[0]);
|
|
934
|
+
}
|
|
935
|
+
else {
|
|
936
|
+
if (dat[1])
|
|
937
|
+
w.terminate();
|
|
938
|
+
strm.ondata.call(strm, err, dat[0], dat[1]);
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
w.postMessage(opts);
|
|
942
|
+
strm.queuedSize = 0;
|
|
943
|
+
strm.push = function (d, f) {
|
|
944
|
+
if (!strm.ondata)
|
|
945
|
+
err(5);
|
|
946
|
+
if (t)
|
|
947
|
+
strm.ondata(err(4, 0, 1), null, !!f);
|
|
948
|
+
strm.queuedSize += d.length;
|
|
949
|
+
// can fail for cross-realm Uint8Array, but ok - only a small performance penalty
|
|
950
|
+
w.postMessage([d, t = f], d.buffer instanceof ArrayBuffer ? [d.buffer] : []);
|
|
951
|
+
};
|
|
952
|
+
strm.terminate = function () { w.terminate(); };
|
|
953
|
+
if (flush) {
|
|
954
|
+
strm.flush = function (sync) { w.postMessage([0, sync]); };
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
// read 2 bytes
|
|
958
|
+
var b2 = function (d, b) { return d[b] | (d[b + 1] << 8); };
|
|
959
|
+
// read 4 bytes
|
|
960
|
+
var b4 = function (d, b) { return (d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24)) >>> 0; };
|
|
961
|
+
// read 8 bytes
|
|
962
|
+
var b8 = function (d, b) { return b4(d, b) + (b4(d, b + 4) * 4294967296); };
|
|
963
|
+
// write bytes
|
|
964
|
+
var wbytes = function (d, b, v) {
|
|
965
|
+
for (; v; ++b)
|
|
966
|
+
d[b] = v, v >>>= 8;
|
|
967
|
+
};
|
|
968
|
+
// gzip header
|
|
969
|
+
var gzh = function (c, o) {
|
|
970
|
+
var fn = o.filename;
|
|
971
|
+
c[0] = 31, c[1] = 139, c[2] = 8, c[8] = o.level < 2 ? 4 : o.level == 9 ? 2 : 0, c[9] = 3; // assume Unix
|
|
972
|
+
if (o.mtime != 0)
|
|
973
|
+
wbytes(c, 4, Math.floor(new Date(o.mtime || Date.now()) / 1000));
|
|
974
|
+
if (fn) {
|
|
975
|
+
c[3] = 8;
|
|
976
|
+
for (var i = 0; i <= fn.length; ++i)
|
|
977
|
+
c[i + 10] = fn.charCodeAt(i);
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
// gzip footer: -8 to -4 = CRC, -4 to -0 is length
|
|
981
|
+
// gzip start
|
|
982
|
+
var gzs = function (d) {
|
|
983
|
+
if (d[0] != 31 || d[1] != 139 || d[2] != 8)
|
|
984
|
+
err(6, 'invalid gzip data');
|
|
985
|
+
var flg = d[3];
|
|
986
|
+
var st = 10;
|
|
987
|
+
if (flg & 4)
|
|
988
|
+
st += (d[10] | d[11] << 8) + 2;
|
|
989
|
+
for (var zs = (flg >> 3 & 1) + (flg >> 4 & 1); zs > 0; zs -= !d[st++])
|
|
990
|
+
;
|
|
991
|
+
return st + (flg & 2);
|
|
992
|
+
};
|
|
993
|
+
// gzip length
|
|
994
|
+
var gzl = function (d) {
|
|
995
|
+
var l = d.length;
|
|
996
|
+
return (d[l - 4] | d[l - 3] << 8 | d[l - 2] << 16 | d[l - 1] << 24) >>> 0;
|
|
997
|
+
};
|
|
998
|
+
// gzip header length
|
|
999
|
+
var gzhl = function (o) { return 10 + (o.filename ? o.filename.length + 1 : 0); };
|
|
1000
|
+
// zlib header
|
|
1001
|
+
var zlh = function (c, o) {
|
|
1002
|
+
var lv = o.level, fl = lv == 0 ? 0 : lv < 6 ? 1 : lv == 9 ? 3 : 2;
|
|
1003
|
+
c[0] = 120, c[1] = (fl << 6) | (o.dictionary && 32);
|
|
1004
|
+
c[1] |= 31 - ((c[0] << 8) | c[1]) % 31;
|
|
1005
|
+
if (o.dictionary) {
|
|
1006
|
+
var h = adler();
|
|
1007
|
+
h.p(o.dictionary);
|
|
1008
|
+
wbytes(c, 2, h.d());
|
|
1009
|
+
}
|
|
1010
|
+
};
|
|
1011
|
+
// zlib start
|
|
1012
|
+
var zls = function (d, dict) {
|
|
1013
|
+
if ((d[0] & 15) != 8 || (d[0] >> 4) > 7 || ((d[0] << 8 | d[1]) % 31))
|
|
1014
|
+
err(6, 'invalid zlib data');
|
|
1015
|
+
if ((d[1] >> 5 & 1) == +!dict)
|
|
1016
|
+
err(6, 'invalid zlib data: ' + (d[1] & 32 ? 'need' : 'unexpected') + ' dictionary');
|
|
1017
|
+
return (d[1] >> 3 & 4) + 2;
|
|
1018
|
+
};
|
|
1019
|
+
function StrmOpt(opts, cb) {
|
|
1020
|
+
if (typeof opts == 'function')
|
|
1021
|
+
cb = opts, opts = {};
|
|
1022
|
+
this.ondata = cb;
|
|
1023
|
+
return opts;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Streaming DEFLATE compression
|
|
1027
|
+
*/
|
|
1028
|
+
var Deflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1029
|
+
function Deflate(opts, cb) {
|
|
1030
|
+
if (typeof opts == 'function')
|
|
1031
|
+
cb = opts, opts = {};
|
|
1032
|
+
this.ondata = cb;
|
|
1033
|
+
this.o = opts || {};
|
|
1034
|
+
this.s = { l: 0, i: 32768, w: 32768, z: 32768 };
|
|
1035
|
+
// Buffer length must always be 0 mod 32768 for index calculations to be correct when modifying head and prev
|
|
1036
|
+
// 98304 = 32768 (lookback) + 65536 (common chunk size)
|
|
1037
|
+
this.b = new u8(98304);
|
|
1038
|
+
if (this.o.dictionary) {
|
|
1039
|
+
var dict = this.o.dictionary.subarray(-32768);
|
|
1040
|
+
this.b.set(dict, 32768 - dict.length);
|
|
1041
|
+
this.s.i = 32768 - dict.length;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
Deflate.prototype.p = function (c, f) {
|
|
1045
|
+
this.ondata(dopt(c, this.o, 0, 0, this.s), f);
|
|
1046
|
+
};
|
|
1047
|
+
/**
|
|
1048
|
+
* Pushes a chunk to be deflated
|
|
1049
|
+
* @param chunk The chunk to push
|
|
1050
|
+
* @param final Whether this is the last chunk
|
|
1051
|
+
*/
|
|
1052
|
+
Deflate.prototype.push = function (chunk, final) {
|
|
1053
|
+
if (!this.ondata)
|
|
1054
|
+
err(5);
|
|
1055
|
+
if (this.s.l)
|
|
1056
|
+
err(4);
|
|
1057
|
+
var endLen = chunk.length + this.s.z;
|
|
1058
|
+
if (endLen > this.b.length) {
|
|
1059
|
+
if (endLen > 2 * this.b.length - 32768) {
|
|
1060
|
+
var newBuf = new u8(endLen & -32768);
|
|
1061
|
+
newBuf.set(this.b.subarray(0, this.s.z));
|
|
1062
|
+
this.b = newBuf;
|
|
1063
|
+
}
|
|
1064
|
+
var split = this.b.length - this.s.z;
|
|
1065
|
+
this.b.set(chunk.subarray(0, split), this.s.z);
|
|
1066
|
+
this.s.z = this.b.length;
|
|
1067
|
+
this.p(this.b, false);
|
|
1068
|
+
this.b.set(this.b.subarray(-32768));
|
|
1069
|
+
this.b.set(chunk.subarray(split), 32768);
|
|
1070
|
+
this.s.z = chunk.length - split + 32768;
|
|
1071
|
+
this.s.i = 32766, this.s.w = 32768;
|
|
1072
|
+
}
|
|
1073
|
+
else {
|
|
1074
|
+
this.b.set(chunk, this.s.z);
|
|
1075
|
+
this.s.z += chunk.length;
|
|
1076
|
+
}
|
|
1077
|
+
this.s.l = final & 1;
|
|
1078
|
+
if (this.s.z > this.s.w + 8191 || final) {
|
|
1079
|
+
this.p(this.b, final || false);
|
|
1080
|
+
this.s.w = this.s.i, this.s.i -= 2;
|
|
1081
|
+
}
|
|
1082
|
+
if (final) {
|
|
1083
|
+
// cleanup unneeded buffers/state to reduce memory usage
|
|
1084
|
+
this.s = this.o = {};
|
|
1085
|
+
this.b = et;
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
/**
|
|
1089
|
+
* Flushes buffered uncompressed data. Useful to immediately retrieve the
|
|
1090
|
+
* deflated output for small inputs.
|
|
1091
|
+
* @param sync Whether to flush to a byte boundary. A sync flush takes 4-5
|
|
1092
|
+
* extra bytes, but guarantees all pushed data is immediately
|
|
1093
|
+
* decompressible. A separate DEFLATE stream may be concatenated
|
|
1094
|
+
* with the current output after a sync flush.
|
|
1095
|
+
*/
|
|
1096
|
+
Deflate.prototype.flush = function (sync) {
|
|
1097
|
+
if (!this.ondata)
|
|
1098
|
+
err(5);
|
|
1099
|
+
if (this.s.l)
|
|
1100
|
+
err(4);
|
|
1101
|
+
this.p(this.b, false);
|
|
1102
|
+
this.s.w = this.s.i, this.s.i -= 2;
|
|
1103
|
+
// could technically skip writing the type-0 block for (this.s.r & 7) == 0,
|
|
1104
|
+
// but the deterministic trailer (00 00 FF FF) is useful in some situations
|
|
1105
|
+
if (sync) {
|
|
1106
|
+
var c = new u8(6);
|
|
1107
|
+
c[0] = this.s.r >> 3;
|
|
1108
|
+
// write empty, non-final type-0 block
|
|
1109
|
+
var ep = wfblk(c, this.s.r, et);
|
|
1110
|
+
this.s.r = 0;
|
|
1111
|
+
this.ondata(c.subarray(0, ep >> 3), false);
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
return Deflate;
|
|
1115
|
+
}())));
|
|
1116
|
+
|
|
1117
|
+
/**
|
|
1118
|
+
* Asynchronous streaming DEFLATE compression
|
|
1119
|
+
*/
|
|
1120
|
+
var AsyncDeflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1121
|
+
function AsyncDeflate(opts, cb) {
|
|
1122
|
+
astrmify([
|
|
1123
|
+
bDflt,
|
|
1124
|
+
function () { return [astrm, Deflate]; }
|
|
1125
|
+
], this, StrmOpt.call(this, opts, cb), function (ev) {
|
|
1126
|
+
var strm = new Deflate(ev.data);
|
|
1127
|
+
onmessage = astrm(strm);
|
|
1128
|
+
}, 6, 1);
|
|
1129
|
+
}
|
|
1130
|
+
return AsyncDeflate;
|
|
1131
|
+
}())));
|
|
1132
|
+
|
|
1133
|
+
function deflate(data, opts, cb) {
|
|
1134
|
+
if (!cb)
|
|
1135
|
+
cb = opts, opts = {};
|
|
1136
|
+
if (typeof cb != 'function')
|
|
1137
|
+
err(7);
|
|
1138
|
+
return cbify(data, opts, [
|
|
1139
|
+
bDflt,
|
|
1140
|
+
], function (ev) { return pbf(deflateSync(ev.data[0], ev.data[1])); }, 0, cb);
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Compresses data with DEFLATE without any wrapper
|
|
1144
|
+
* @param data The data to compress
|
|
1145
|
+
* @param opts The compression options
|
|
1146
|
+
* @returns The deflated version of the data
|
|
1147
|
+
*/
|
|
1148
|
+
function deflateSync(data, opts) {
|
|
1149
|
+
return dopt(data, opts || {}, 0, 0);
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Streaming DEFLATE decompression
|
|
1153
|
+
*/
|
|
1154
|
+
var Inflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1155
|
+
function Inflate(opts, cb) {
|
|
1156
|
+
// no StrmOpt here to avoid adding to workerizer
|
|
1157
|
+
if (typeof opts == 'function')
|
|
1158
|
+
cb = opts, opts = {};
|
|
1159
|
+
this.ondata = cb;
|
|
1160
|
+
var dict = opts && opts.dictionary && opts.dictionary.subarray(-32768);
|
|
1161
|
+
this.s = { i: 0, b: dict ? dict.length : 0 };
|
|
1162
|
+
this.o = new u8(32768);
|
|
1163
|
+
this.p = new u8(0);
|
|
1164
|
+
if (dict)
|
|
1165
|
+
this.o.set(dict);
|
|
1166
|
+
}
|
|
1167
|
+
Inflate.prototype.e = function (c) {
|
|
1168
|
+
if (!this.ondata)
|
|
1169
|
+
err(5);
|
|
1170
|
+
if (this.d)
|
|
1171
|
+
err(4);
|
|
1172
|
+
if (!this.p.length)
|
|
1173
|
+
this.p = c;
|
|
1174
|
+
else if (c.length) {
|
|
1175
|
+
var n = new u8(this.p.length + c.length);
|
|
1176
|
+
n.set(this.p), n.set(c, this.p.length), this.p = n;
|
|
1177
|
+
}
|
|
1178
|
+
};
|
|
1179
|
+
Inflate.prototype.c = function (final) {
|
|
1180
|
+
this.s.i = +(this.d = final || false);
|
|
1181
|
+
var bts = this.s.b;
|
|
1182
|
+
var dt = inflt(this.p, this.s, this.o);
|
|
1183
|
+
this.ondata(slc(dt, bts, this.s.b), this.d);
|
|
1184
|
+
this.o = slc(dt, this.s.b - 32768), this.s.b = this.o.length;
|
|
1185
|
+
this.p = slc(this.p, (this.s.p / 8) | 0), this.s.p &= 7;
|
|
1186
|
+
};
|
|
1187
|
+
/**
|
|
1188
|
+
* Pushes a chunk to be inflated
|
|
1189
|
+
* @param chunk The chunk to push
|
|
1190
|
+
* @param final Whether this is the final chunk
|
|
1191
|
+
*/
|
|
1192
|
+
Inflate.prototype.push = function (chunk, final) {
|
|
1193
|
+
this.e(chunk), this.c(final);
|
|
1194
|
+
};
|
|
1195
|
+
return Inflate;
|
|
1196
|
+
}())));
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* Asynchronous streaming DEFLATE decompression
|
|
1200
|
+
*/
|
|
1201
|
+
var AsyncInflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1202
|
+
function AsyncInflate(opts, cb) {
|
|
1203
|
+
astrmify([
|
|
1204
|
+
bInflt,
|
|
1205
|
+
function () { return [astrm, Inflate]; }
|
|
1206
|
+
], this, StrmOpt.call(this, opts, cb), function (ev) {
|
|
1207
|
+
var strm = new Inflate(ev.data);
|
|
1208
|
+
onmessage = astrm(strm);
|
|
1209
|
+
}, 7, 0);
|
|
1210
|
+
}
|
|
1211
|
+
return AsyncInflate;
|
|
1212
|
+
}())));
|
|
1213
|
+
|
|
1214
|
+
function inflate(data, opts, cb) {
|
|
1215
|
+
if (!cb)
|
|
1216
|
+
cb = opts, opts = {};
|
|
1217
|
+
if (typeof cb != 'function')
|
|
1218
|
+
err(7);
|
|
1219
|
+
return cbify(data, opts, [
|
|
1220
|
+
bInflt
|
|
1221
|
+
], function (ev) { return pbf(inflateSync(ev.data[0], gopt(ev.data[1]))); }, 1, cb);
|
|
1222
|
+
}
|
|
1223
|
+
function inflateSync(data, opts) {
|
|
1224
|
+
return inflt(data, { i: 2 }, opts && opts.out, opts && opts.dictionary);
|
|
1225
|
+
}
|
|
1226
|
+
// before you yell at me for not just using extends, my reason is that TS inheritance is hard to workerize.
|
|
1227
|
+
/**
|
|
1228
|
+
* Streaming GZIP compression
|
|
1229
|
+
*/
|
|
1230
|
+
var Gzip = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1231
|
+
function Gzip(opts, cb) {
|
|
1232
|
+
this.c = crc();
|
|
1233
|
+
this.l = 0;
|
|
1234
|
+
this.v = 1;
|
|
1235
|
+
Deflate.call(this, opts, cb);
|
|
1236
|
+
}
|
|
1237
|
+
/**
|
|
1238
|
+
* Pushes a chunk to be GZIPped
|
|
1239
|
+
* @param chunk The chunk to push
|
|
1240
|
+
* @param final Whether this is the last chunk
|
|
1241
|
+
*/
|
|
1242
|
+
Gzip.prototype.push = function (chunk, final) {
|
|
1243
|
+
this.c.p(chunk);
|
|
1244
|
+
this.l += chunk.length;
|
|
1245
|
+
Deflate.prototype.push.call(this, chunk, final);
|
|
1246
|
+
};
|
|
1247
|
+
Gzip.prototype.p = function (c, f) {
|
|
1248
|
+
var raw = dopt(c, this.o, this.v && gzhl(this.o), f && 8, this.s);
|
|
1249
|
+
if (this.v)
|
|
1250
|
+
gzh(raw, this.o), this.v = 0;
|
|
1251
|
+
if (f)
|
|
1252
|
+
wbytes(raw, raw.length - 8, this.c.d()), wbytes(raw, raw.length - 4, this.l);
|
|
1253
|
+
this.ondata(raw, f);
|
|
1254
|
+
};
|
|
1255
|
+
/**
|
|
1256
|
+
* Flushes buffered uncompressed data. Useful to immediately retrieve the
|
|
1257
|
+
* GZIPped output for small inputs.
|
|
1258
|
+
* @param sync Whether to flush to a byte boundary. A sync flush takes 4-5
|
|
1259
|
+
* extra bytes, but guarantees all pushed data is immediately
|
|
1260
|
+
* decompressible.
|
|
1261
|
+
*/
|
|
1262
|
+
Gzip.prototype.flush = function (sync) {
|
|
1263
|
+
Deflate.prototype.flush.call(this, sync);
|
|
1264
|
+
};
|
|
1265
|
+
return Gzip;
|
|
1266
|
+
}())));
|
|
1267
|
+
|
|
1268
|
+
/**
|
|
1269
|
+
* Asynchronous streaming GZIP compression
|
|
1270
|
+
*/
|
|
1271
|
+
var AsyncGzip = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1272
|
+
function AsyncGzip(opts, cb) {
|
|
1273
|
+
astrmify([
|
|
1274
|
+
bDflt,
|
|
1275
|
+
gze,
|
|
1276
|
+
function () { return [astrm, Deflate, Gzip]; }
|
|
1277
|
+
], this, StrmOpt.call(this, opts, cb), function (ev) {
|
|
1278
|
+
var strm = new Gzip(ev.data);
|
|
1279
|
+
onmessage = astrm(strm);
|
|
1280
|
+
}, 8, 1);
|
|
1281
|
+
}
|
|
1282
|
+
return AsyncGzip;
|
|
1283
|
+
}())));
|
|
1284
|
+
|
|
1285
|
+
function gzip(data, opts, cb) {
|
|
1286
|
+
if (!cb)
|
|
1287
|
+
cb = opts, opts = {};
|
|
1288
|
+
if (typeof cb != 'function')
|
|
1289
|
+
err(7);
|
|
1290
|
+
return cbify(data, opts, [
|
|
1291
|
+
bDflt,
|
|
1292
|
+
gze,
|
|
1293
|
+
function () { return [gzipSync]; }
|
|
1294
|
+
], function (ev) { return pbf(gzipSync(ev.data[0], ev.data[1])); }, 2, cb);
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Compresses data with GZIP
|
|
1298
|
+
* @param data The data to compress
|
|
1299
|
+
* @param opts The compression options
|
|
1300
|
+
* @returns The gzipped version of the data
|
|
1301
|
+
*/
|
|
1302
|
+
function gzipSync(data, opts) {
|
|
1303
|
+
if (!opts)
|
|
1304
|
+
opts = {};
|
|
1305
|
+
var c = crc(), l = data.length;
|
|
1306
|
+
c.p(data);
|
|
1307
|
+
var d = dopt(data, opts, gzhl(opts), 8), s = d.length;
|
|
1308
|
+
return gzh(d, opts), wbytes(d, s - 8, c.d()), wbytes(d, s - 4, l), d;
|
|
1309
|
+
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Streaming single or multi-member GZIP decompression
|
|
1312
|
+
*/
|
|
1313
|
+
var Gunzip = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1314
|
+
function Gunzip(opts, cb) {
|
|
1315
|
+
this.v = 1;
|
|
1316
|
+
this.r = 0;
|
|
1317
|
+
Inflate.call(this, opts, cb);
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Pushes a chunk to be GUNZIPped
|
|
1321
|
+
* @param chunk The chunk to push
|
|
1322
|
+
* @param final Whether this is the last chunk
|
|
1323
|
+
*/
|
|
1324
|
+
Gunzip.prototype.push = function (chunk, final) {
|
|
1325
|
+
Inflate.prototype.e.call(this, chunk);
|
|
1326
|
+
this.r += chunk.length;
|
|
1327
|
+
if (this.v) {
|
|
1328
|
+
var p = this.p.subarray(this.v - 1);
|
|
1329
|
+
var s = p.length > 3 ? gzs(p) : 4;
|
|
1330
|
+
if (s > p.length) {
|
|
1331
|
+
if (!final)
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
else if (this.v > 1 && this.onmember) {
|
|
1335
|
+
this.onmember(this.r - p.length);
|
|
1336
|
+
}
|
|
1337
|
+
this.p = p.subarray(s), this.v = 0;
|
|
1338
|
+
}
|
|
1339
|
+
// necessary to prevent TS from using the closure value
|
|
1340
|
+
// This allows for workerization to function correctly
|
|
1341
|
+
Inflate.prototype.c.call(this, 0);
|
|
1342
|
+
// process concatenated GZIP
|
|
1343
|
+
if (this.s.f && !this.s.l) {
|
|
1344
|
+
this.v = shft(this.s.p) + 9;
|
|
1345
|
+
this.s = { i: 0 };
|
|
1346
|
+
this.o = new u8(0);
|
|
1347
|
+
this.push(new u8(0), final);
|
|
1348
|
+
}
|
|
1349
|
+
else if (final) {
|
|
1350
|
+
Inflate.prototype.c.call(this, final);
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
return Gunzip;
|
|
1354
|
+
}())));
|
|
1355
|
+
|
|
1356
|
+
/**
|
|
1357
|
+
* Asynchronous streaming single or multi-member GZIP decompression
|
|
1358
|
+
*/
|
|
1359
|
+
var AsyncGunzip = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1360
|
+
function AsyncGunzip(opts, cb) {
|
|
1361
|
+
var _this = this;
|
|
1362
|
+
astrmify([
|
|
1363
|
+
bInflt,
|
|
1364
|
+
guze,
|
|
1365
|
+
function () { return [astrm, Inflate, Gunzip]; }
|
|
1366
|
+
], this, StrmOpt.call(this, opts, cb), function (ev) {
|
|
1367
|
+
var strm = new Gunzip(ev.data);
|
|
1368
|
+
strm.onmember = function (offset) { return postMessage(offset); };
|
|
1369
|
+
onmessage = astrm(strm);
|
|
1370
|
+
}, 9, 0, function (offset) { return _this.onmember && _this.onmember(offset); });
|
|
1371
|
+
}
|
|
1372
|
+
return AsyncGunzip;
|
|
1373
|
+
}())));
|
|
1374
|
+
|
|
1375
|
+
function gunzip(data, opts, cb) {
|
|
1376
|
+
if (!cb)
|
|
1377
|
+
cb = opts, opts = {};
|
|
1378
|
+
if (typeof cb != 'function')
|
|
1379
|
+
err(7);
|
|
1380
|
+
return cbify(data, opts, [
|
|
1381
|
+
bInflt,
|
|
1382
|
+
guze,
|
|
1383
|
+
function () { return [gunzipSync]; }
|
|
1384
|
+
], function (ev) { return pbf(gunzipSync(ev.data[0], ev.data[1])); }, 3, cb);
|
|
1385
|
+
}
|
|
1386
|
+
function gunzipSync(data, opts) {
|
|
1387
|
+
var st = gzs(data);
|
|
1388
|
+
if (st + 8 > data.length)
|
|
1389
|
+
err(6, 'invalid gzip data');
|
|
1390
|
+
return inflt(data.subarray(st, -8), { i: 2 }, opts && opts.out || new u8(gzl(data)), opts && opts.dictionary);
|
|
1391
|
+
}
|
|
1392
|
+
/**
|
|
1393
|
+
* Streaming Zlib compression
|
|
1394
|
+
*/
|
|
1395
|
+
var Zlib = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1396
|
+
function Zlib(opts, cb) {
|
|
1397
|
+
this.c = adler();
|
|
1398
|
+
this.v = 1;
|
|
1399
|
+
Deflate.call(this, opts, cb);
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Pushes a chunk to be zlibbed
|
|
1403
|
+
* @param chunk The chunk to push
|
|
1404
|
+
* @param final Whether this is the last chunk
|
|
1405
|
+
*/
|
|
1406
|
+
Zlib.prototype.push = function (chunk, final) {
|
|
1407
|
+
this.c.p(chunk);
|
|
1408
|
+
Deflate.prototype.push.call(this, chunk, final);
|
|
1409
|
+
};
|
|
1410
|
+
Zlib.prototype.p = function (c, f) {
|
|
1411
|
+
var raw = dopt(c, this.o, this.v && (this.o.dictionary ? 6 : 2), f && 4, this.s);
|
|
1412
|
+
if (this.v)
|
|
1413
|
+
zlh(raw, this.o), this.v = 0;
|
|
1414
|
+
if (f)
|
|
1415
|
+
wbytes(raw, raw.length - 4, this.c.d());
|
|
1416
|
+
this.ondata(raw, f);
|
|
1417
|
+
};
|
|
1418
|
+
/**
|
|
1419
|
+
* Flushes buffered uncompressed data. Useful to immediately retrieve the
|
|
1420
|
+
* zlibbed output for small inputs.
|
|
1421
|
+
* @param sync Whether to flush to a byte boundary. A sync flush takes 4-5
|
|
1422
|
+
* extra bytes, but guarantees all pushed data is immediately
|
|
1423
|
+
* decompressible.
|
|
1424
|
+
*/
|
|
1425
|
+
Zlib.prototype.flush = function (sync) {
|
|
1426
|
+
Deflate.prototype.flush.call(this, sync);
|
|
1427
|
+
};
|
|
1428
|
+
return Zlib;
|
|
1429
|
+
}())));
|
|
1430
|
+
|
|
1431
|
+
/**
|
|
1432
|
+
* Asynchronous streaming Zlib compression
|
|
1433
|
+
*/
|
|
1434
|
+
var AsyncZlib = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1435
|
+
function AsyncZlib(opts, cb) {
|
|
1436
|
+
astrmify([
|
|
1437
|
+
bDflt,
|
|
1438
|
+
zle,
|
|
1439
|
+
function () { return [astrm, Deflate, Zlib]; }
|
|
1440
|
+
], this, StrmOpt.call(this, opts, cb), function (ev) {
|
|
1441
|
+
var strm = new Zlib(ev.data);
|
|
1442
|
+
onmessage = astrm(strm);
|
|
1443
|
+
}, 10, 1);
|
|
1444
|
+
}
|
|
1445
|
+
return AsyncZlib;
|
|
1446
|
+
}())));
|
|
1447
|
+
|
|
1448
|
+
function zlib(data, opts, cb) {
|
|
1449
|
+
if (!cb)
|
|
1450
|
+
cb = opts, opts = {};
|
|
1451
|
+
if (typeof cb != 'function')
|
|
1452
|
+
err(7);
|
|
1453
|
+
return cbify(data, opts, [
|
|
1454
|
+
bDflt,
|
|
1455
|
+
zle,
|
|
1456
|
+
function () { return [zlibSync]; }
|
|
1457
|
+
], function (ev) { return pbf(zlibSync(ev.data[0], ev.data[1])); }, 4, cb);
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* Compress data with Zlib
|
|
1461
|
+
* @param data The data to compress
|
|
1462
|
+
* @param opts The compression options
|
|
1463
|
+
* @returns The zlib-compressed version of the data
|
|
1464
|
+
*/
|
|
1465
|
+
function zlibSync(data, opts) {
|
|
1466
|
+
if (!opts)
|
|
1467
|
+
opts = {};
|
|
1468
|
+
var a = adler();
|
|
1469
|
+
a.p(data);
|
|
1470
|
+
var d = dopt(data, opts, opts.dictionary ? 6 : 2, 4);
|
|
1471
|
+
return zlh(d, opts), wbytes(d, d.length - 4, a.d()), d;
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Streaming Zlib decompression
|
|
1475
|
+
*/
|
|
1476
|
+
var Unzlib = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1477
|
+
function Unzlib(opts, cb) {
|
|
1478
|
+
Inflate.call(this, opts, cb);
|
|
1479
|
+
this.v = opts && opts.dictionary ? 2 : 1;
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Pushes a chunk to be unzlibbed
|
|
1483
|
+
* @param chunk The chunk to push
|
|
1484
|
+
* @param final Whether this is the last chunk
|
|
1485
|
+
*/
|
|
1486
|
+
Unzlib.prototype.push = function (chunk, final) {
|
|
1487
|
+
Inflate.prototype.e.call(this, chunk);
|
|
1488
|
+
if (this.v) {
|
|
1489
|
+
if (this.p.length < 6 && !final)
|
|
1490
|
+
return;
|
|
1491
|
+
this.p = this.p.subarray(zls(this.p, this.v - 1)), this.v = 0;
|
|
1492
|
+
}
|
|
1493
|
+
if (final) {
|
|
1494
|
+
if (this.p.length < 4)
|
|
1495
|
+
err(6, 'invalid zlib data');
|
|
1496
|
+
this.p = this.p.subarray(0, -4);
|
|
1497
|
+
}
|
|
1498
|
+
// necessary to prevent TS from using the closure value
|
|
1499
|
+
// This allows for workerization to function correctly
|
|
1500
|
+
Inflate.prototype.c.call(this, final);
|
|
1501
|
+
};
|
|
1502
|
+
return Unzlib;
|
|
1503
|
+
}())));
|
|
1504
|
+
|
|
1505
|
+
/**
|
|
1506
|
+
* Asynchronous streaming Zlib decompression
|
|
1507
|
+
*/
|
|
1508
|
+
var AsyncUnzlib = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1509
|
+
function AsyncUnzlib(opts, cb) {
|
|
1510
|
+
astrmify([
|
|
1511
|
+
bInflt,
|
|
1512
|
+
zule,
|
|
1513
|
+
function () { return [astrm, Inflate, Unzlib]; }
|
|
1514
|
+
], this, StrmOpt.call(this, opts, cb), function (ev) {
|
|
1515
|
+
var strm = new Unzlib(ev.data);
|
|
1516
|
+
onmessage = astrm(strm);
|
|
1517
|
+
}, 11, 0);
|
|
1518
|
+
}
|
|
1519
|
+
return AsyncUnzlib;
|
|
1520
|
+
}())));
|
|
1521
|
+
|
|
1522
|
+
function unzlib(data, opts, cb) {
|
|
1523
|
+
if (!cb)
|
|
1524
|
+
cb = opts, opts = {};
|
|
1525
|
+
if (typeof cb != 'function')
|
|
1526
|
+
err(7);
|
|
1527
|
+
return cbify(data, opts, [
|
|
1528
|
+
bInflt,
|
|
1529
|
+
zule,
|
|
1530
|
+
function () { return [unzlibSync]; }
|
|
1531
|
+
], function (ev) { return pbf(unzlibSync(ev.data[0], gopt(ev.data[1]))); }, 5, cb);
|
|
1532
|
+
}
|
|
1533
|
+
function unzlibSync(data, opts) {
|
|
1534
|
+
return inflt(data.subarray(zls(data, opts && opts.dictionary), -4), { i: 2 }, opts && opts.out, opts && opts.dictionary);
|
|
1535
|
+
}
|
|
1536
|
+
// Default algorithm for compression (used because having a known output size allows faster decompression)
|
|
1537
|
+
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* Streaming GZIP, Zlib, or raw DEFLATE decompression
|
|
1541
|
+
*/
|
|
1542
|
+
var Decompress = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1543
|
+
function Decompress(opts, cb) {
|
|
1544
|
+
this.o = StrmOpt.call(this, opts, cb) || {};
|
|
1545
|
+
this.G = Gunzip;
|
|
1546
|
+
this.I = Inflate;
|
|
1547
|
+
this.Z = Unzlib;
|
|
1548
|
+
}
|
|
1549
|
+
// init substream
|
|
1550
|
+
// overriden by AsyncDecompress
|
|
1551
|
+
Decompress.prototype.i = function () {
|
|
1552
|
+
var _this = this;
|
|
1553
|
+
this.s.ondata = function (dat, final) {
|
|
1554
|
+
_this.ondata(dat, final);
|
|
1555
|
+
};
|
|
1556
|
+
};
|
|
1557
|
+
/**
|
|
1558
|
+
* Pushes a chunk to be decompressed
|
|
1559
|
+
* @param chunk The chunk to push
|
|
1560
|
+
* @param final Whether this is the last chunk
|
|
1561
|
+
*/
|
|
1562
|
+
Decompress.prototype.push = function (chunk, final) {
|
|
1563
|
+
if (!this.ondata)
|
|
1564
|
+
err(5);
|
|
1565
|
+
if (!this.s) {
|
|
1566
|
+
if (this.p && this.p.length) {
|
|
1567
|
+
var n = new u8(this.p.length + chunk.length);
|
|
1568
|
+
n.set(this.p), n.set(chunk, this.p.length);
|
|
1569
|
+
}
|
|
1570
|
+
else
|
|
1571
|
+
this.p = chunk;
|
|
1572
|
+
if (this.p.length > 2) {
|
|
1573
|
+
this.s = (this.p[0] == 31 && this.p[1] == 139 && this.p[2] == 8)
|
|
1574
|
+
? new this.G(this.o)
|
|
1575
|
+
: ((this.p[0] & 15) != 8 || (this.p[0] >> 4) > 7 || ((this.p[0] << 8 | this.p[1]) % 31))
|
|
1576
|
+
? new this.I(this.o)
|
|
1577
|
+
: new this.Z(this.o);
|
|
1578
|
+
this.i();
|
|
1579
|
+
this.s.push(this.p, final);
|
|
1580
|
+
this.p = null;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
else
|
|
1584
|
+
this.s.push(chunk, final);
|
|
1585
|
+
};
|
|
1586
|
+
return Decompress;
|
|
1587
|
+
}())));
|
|
1588
|
+
|
|
1589
|
+
/**
|
|
1590
|
+
* Asynchronous streaming GZIP, Zlib, or raw DEFLATE decompression
|
|
1591
|
+
*/
|
|
1592
|
+
var AsyncDecompress = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1593
|
+
function AsyncDecompress(opts, cb) {
|
|
1594
|
+
Decompress.call(this, opts, cb);
|
|
1595
|
+
this.queuedSize = 0;
|
|
1596
|
+
this.G = AsyncGunzip;
|
|
1597
|
+
this.I = AsyncInflate;
|
|
1598
|
+
this.Z = AsyncUnzlib;
|
|
1599
|
+
}
|
|
1600
|
+
AsyncDecompress.prototype.i = function () {
|
|
1601
|
+
var _this = this;
|
|
1602
|
+
this.s.ondata = function (err, dat, final) {
|
|
1603
|
+
_this.ondata(err, dat, final);
|
|
1604
|
+
};
|
|
1605
|
+
this.s.ondrain = function (size) {
|
|
1606
|
+
_this.queuedSize -= size;
|
|
1607
|
+
if (_this.ondrain)
|
|
1608
|
+
_this.ondrain(size);
|
|
1609
|
+
};
|
|
1610
|
+
};
|
|
1611
|
+
/**
|
|
1612
|
+
* Pushes a chunk to be decompressed
|
|
1613
|
+
* @param chunk The chunk to push
|
|
1614
|
+
* @param final Whether this is the last chunk
|
|
1615
|
+
*/
|
|
1616
|
+
AsyncDecompress.prototype.push = function (chunk, final) {
|
|
1617
|
+
this.queuedSize += chunk.length;
|
|
1618
|
+
Decompress.prototype.push.call(this, chunk, final);
|
|
1619
|
+
};
|
|
1620
|
+
return AsyncDecompress;
|
|
1621
|
+
}())));
|
|
1622
|
+
|
|
1623
|
+
function decompress(data, opts, cb) {
|
|
1624
|
+
if (!cb)
|
|
1625
|
+
cb = opts, opts = {};
|
|
1626
|
+
if (typeof cb != 'function')
|
|
1627
|
+
err(7);
|
|
1628
|
+
return (data[0] == 31 && data[1] == 139 && data[2] == 8)
|
|
1629
|
+
? gunzip(data, opts, cb)
|
|
1630
|
+
: ((data[0] & 15) != 8 || (data[0] >> 4) > 7 || ((data[0] << 8 | data[1]) % 31))
|
|
1631
|
+
? inflate(data, opts, cb)
|
|
1632
|
+
: unzlib(data, opts, cb);
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Expands compressed GZIP, Zlib, or raw DEFLATE data, automatically detecting the format
|
|
1636
|
+
* @param data The data to decompress
|
|
1637
|
+
* @param opts The decompression options
|
|
1638
|
+
* @returns The decompressed version of the data
|
|
1639
|
+
*/
|
|
1640
|
+
function decompressSync(data, opts) {
|
|
1641
|
+
return (data[0] == 31 && data[1] == 139 && data[2] == 8)
|
|
1642
|
+
? gunzipSync(data, opts)
|
|
1643
|
+
: ((data[0] & 15) != 8 || (data[0] >> 4) > 7 || ((data[0] << 8 | data[1]) % 31))
|
|
1644
|
+
? inflateSync(data, opts)
|
|
1645
|
+
: unzlibSync(data, opts);
|
|
1646
|
+
}
|
|
1647
|
+
// flatten a directory structure
|
|
1648
|
+
var fltn = function (d, p, t, o) {
|
|
1649
|
+
for (var k in d) {
|
|
1650
|
+
var val = d[k], n = p + k, op = o;
|
|
1651
|
+
if (Array.isArray(val))
|
|
1652
|
+
op = mrg(o, val[1]), val = val[0];
|
|
1653
|
+
if (ArrayBuffer.isView(val))
|
|
1654
|
+
t[n] = [val, op];
|
|
1655
|
+
else {
|
|
1656
|
+
t[n += '/'] = [new u8(0), op];
|
|
1657
|
+
fltn(val, n, t, o);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
};
|
|
1661
|
+
// text encoder
|
|
1662
|
+
var te = typeof TextEncoder != 'undefined' && /*#__PURE__*/ new TextEncoder();
|
|
1663
|
+
// text decoder
|
|
1664
|
+
var td = typeof TextDecoder != 'undefined' && /*#__PURE__*/ new TextDecoder();
|
|
1665
|
+
// text decoder stream
|
|
1666
|
+
var tds = 0;
|
|
1667
|
+
try {
|
|
1668
|
+
td.decode(et, { stream: true });
|
|
1669
|
+
tds = 1;
|
|
1670
|
+
}
|
|
1671
|
+
catch (e) { }
|
|
1672
|
+
// decode UTF8
|
|
1673
|
+
var dutf8 = function (d) {
|
|
1674
|
+
for (var r = '', i = 0;;) {
|
|
1675
|
+
var c = d[i++];
|
|
1676
|
+
var eb = (c > 127) + (c > 223) + (c > 239);
|
|
1677
|
+
if (i + eb > d.length)
|
|
1678
|
+
return { s: r, r: slc(d, i - 1) };
|
|
1679
|
+
if (!eb)
|
|
1680
|
+
r += String.fromCharCode(c);
|
|
1681
|
+
else if (eb == 3) {
|
|
1682
|
+
c = ((c & 15) << 18 | (d[i++] & 63) << 12 | (d[i++] & 63) << 6 | (d[i++] & 63)) - 65536,
|
|
1683
|
+
r += String.fromCharCode(55296 | (c >> 10), 56320 | (c & 1023));
|
|
1684
|
+
}
|
|
1685
|
+
else if (eb & 1)
|
|
1686
|
+
r += String.fromCharCode((c & 31) << 6 | (d[i++] & 63));
|
|
1687
|
+
else
|
|
1688
|
+
r += String.fromCharCode((c & 15) << 12 | (d[i++] & 63) << 6 | (d[i++] & 63));
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
/**
|
|
1692
|
+
* Streaming UTF-8 decoding
|
|
1693
|
+
*/
|
|
1694
|
+
var DecodeUTF8 = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1695
|
+
/**
|
|
1696
|
+
* Creates a UTF-8 decoding stream
|
|
1697
|
+
* @param cb The callback to call whenever data is decoded
|
|
1698
|
+
*/
|
|
1699
|
+
function DecodeUTF8(cb) {
|
|
1700
|
+
this.ondata = cb;
|
|
1701
|
+
if (tds)
|
|
1702
|
+
this.t = new TextDecoder();
|
|
1703
|
+
else
|
|
1704
|
+
this.p = et;
|
|
1705
|
+
}
|
|
1706
|
+
/**
|
|
1707
|
+
* Pushes a chunk to be decoded from UTF-8 binary
|
|
1708
|
+
* @param chunk The chunk to push
|
|
1709
|
+
* @param final Whether this is the last chunk
|
|
1710
|
+
*/
|
|
1711
|
+
DecodeUTF8.prototype.push = function (chunk, final) {
|
|
1712
|
+
if (!this.ondata)
|
|
1713
|
+
err(5);
|
|
1714
|
+
final = !!final;
|
|
1715
|
+
if (this.t) {
|
|
1716
|
+
this.ondata(this.t.decode(chunk, { stream: true }), final);
|
|
1717
|
+
if (final) {
|
|
1718
|
+
if (this.t.decode().length)
|
|
1719
|
+
err(8);
|
|
1720
|
+
this.t = null;
|
|
1721
|
+
}
|
|
1722
|
+
return;
|
|
1723
|
+
}
|
|
1724
|
+
if (!this.p)
|
|
1725
|
+
err(4);
|
|
1726
|
+
var dat = new u8(this.p.length + chunk.length);
|
|
1727
|
+
dat.set(this.p);
|
|
1728
|
+
dat.set(chunk, this.p.length);
|
|
1729
|
+
var _a = dutf8(dat), s = _a.s, r = _a.r;
|
|
1730
|
+
if (final) {
|
|
1731
|
+
if (r.length)
|
|
1732
|
+
err(8);
|
|
1733
|
+
this.p = null;
|
|
1734
|
+
}
|
|
1735
|
+
else
|
|
1736
|
+
this.p = r;
|
|
1737
|
+
this.ondata(s, final);
|
|
1738
|
+
};
|
|
1739
|
+
return DecodeUTF8;
|
|
1740
|
+
}())));
|
|
1741
|
+
|
|
1742
|
+
/**
|
|
1743
|
+
* Streaming UTF-8 encoding
|
|
1744
|
+
*/
|
|
1745
|
+
var EncodeUTF8 = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1746
|
+
/**
|
|
1747
|
+
* Creates a UTF-8 decoding stream
|
|
1748
|
+
* @param cb The callback to call whenever data is encoded
|
|
1749
|
+
*/
|
|
1750
|
+
function EncodeUTF8(cb) {
|
|
1751
|
+
this.ondata = cb;
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* Pushes a chunk to be encoded to UTF-8
|
|
1755
|
+
* @param chunk The string data to push
|
|
1756
|
+
* @param final Whether this is the last chunk
|
|
1757
|
+
*/
|
|
1758
|
+
EncodeUTF8.prototype.push = function (chunk, final) {
|
|
1759
|
+
if (!this.ondata)
|
|
1760
|
+
err(5);
|
|
1761
|
+
if (this.d)
|
|
1762
|
+
err(4);
|
|
1763
|
+
this.ondata(strToU8(chunk), this.d = final || false);
|
|
1764
|
+
};
|
|
1765
|
+
return EncodeUTF8;
|
|
1766
|
+
}())));
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* Converts a string into a Uint8Array for use with compression/decompression methods
|
|
1770
|
+
* @param str The string to encode
|
|
1771
|
+
* @param latin1 Whether or not to interpret the data as Latin-1. This should
|
|
1772
|
+
* not need to be true unless decoding a binary string.
|
|
1773
|
+
* @returns The string encoded in UTF-8/Latin-1 binary
|
|
1774
|
+
*/
|
|
1775
|
+
function strToU8(str, latin1) {
|
|
1776
|
+
if (latin1) {
|
|
1777
|
+
var ar_1 = new u8(str.length);
|
|
1778
|
+
for (var i = 0; i < str.length; ++i)
|
|
1779
|
+
ar_1[i] = str.charCodeAt(i);
|
|
1780
|
+
return ar_1;
|
|
1781
|
+
}
|
|
1782
|
+
if (te)
|
|
1783
|
+
return te.encode(str);
|
|
1784
|
+
var l = str.length;
|
|
1785
|
+
var ar = new u8(str.length + (str.length >> 1));
|
|
1786
|
+
var ai = 0;
|
|
1787
|
+
var w = function (v) { ar[ai++] = v; };
|
|
1788
|
+
for (var i = 0; i < l; ++i) {
|
|
1789
|
+
if (ai + 5 > ar.length) {
|
|
1790
|
+
var n = new u8(ai + 8 + ((l - i) << 1));
|
|
1791
|
+
n.set(ar);
|
|
1792
|
+
ar = n;
|
|
1793
|
+
}
|
|
1794
|
+
var c = str.charCodeAt(i);
|
|
1795
|
+
if (c < 128 || latin1)
|
|
1796
|
+
w(c);
|
|
1797
|
+
else if (c < 2048)
|
|
1798
|
+
w(192 | (c >> 6)), w(128 | (c & 63));
|
|
1799
|
+
else if (c > 55295 && c < 57344)
|
|
1800
|
+
c = 65536 + (c & 1023 << 10) | (str.charCodeAt(++i) & 1023),
|
|
1801
|
+
w(240 | (c >> 18)), w(128 | ((c >> 12) & 63)), w(128 | ((c >> 6) & 63)), w(128 | (c & 63));
|
|
1802
|
+
else
|
|
1803
|
+
w(224 | (c >> 12)), w(128 | ((c >> 6) & 63)), w(128 | (c & 63));
|
|
1804
|
+
}
|
|
1805
|
+
return slc(ar, 0, ai);
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1808
|
+
* Converts a Uint8Array to a string
|
|
1809
|
+
* @param dat The data to decode to string
|
|
1810
|
+
* @param latin1 Whether or not to interpret the data as Latin-1. This should
|
|
1811
|
+
* not need to be true unless encoding to binary string.
|
|
1812
|
+
* @returns The original UTF-8/Latin-1 string
|
|
1813
|
+
*/
|
|
1814
|
+
function strFromU8(dat, latin1) {
|
|
1815
|
+
if (latin1) {
|
|
1816
|
+
var r = '';
|
|
1817
|
+
for (var i = 0; i < dat.length; i += 16384)
|
|
1818
|
+
r += String.fromCharCode.apply(null, dat.subarray(i, i + 16384));
|
|
1819
|
+
return r;
|
|
1820
|
+
}
|
|
1821
|
+
else if (td) {
|
|
1822
|
+
return td.decode(dat);
|
|
1823
|
+
}
|
|
1824
|
+
else {
|
|
1825
|
+
var _a = dutf8(dat), s = _a.s, r = _a.r;
|
|
1826
|
+
if (r.length)
|
|
1827
|
+
err(8);
|
|
1828
|
+
return s;
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
;
|
|
1832
|
+
// deflate bit flag
|
|
1833
|
+
var dbf = function (l) { return l == 1 ? 3 : l < 6 ? 2 : l == 9 ? 1 : 0; };
|
|
1834
|
+
// skip local zip header
|
|
1835
|
+
var slzh = function (d, b) { return b + 30 + b2(d, b + 26) + b2(d, b + 28); };
|
|
1836
|
+
// read zip header
|
|
1837
|
+
var zh = function (d, b, z) {
|
|
1838
|
+
var fnl = b2(d, b + 28), efl = b2(d, b + 30), fn = strFromU8(d.subarray(b + 46, b + 46 + fnl), !(b2(d, b + 8) & 2048)), es = b + 46 + fnl;
|
|
1839
|
+
var _a = z64hs(d, es, efl, z, b4(d, b + 20), b4(d, b + 24), b4(d, b + 42)), sc = _a[0], su = _a[1], off = _a[2];
|
|
1840
|
+
return [b2(d, b + 10), sc, su, fn, es + efl + b2(d, b + 32), off];
|
|
1841
|
+
};
|
|
1842
|
+
// read zip64 header sizes
|
|
1843
|
+
var z64hs = function (d, b, l, z, sc, su, off) {
|
|
1844
|
+
var nsc = sc == 4294967295, nsu = su == 4294967295, noff = off == 4294967295, e = b + l;
|
|
1845
|
+
var nf = nsc + nsu + noff;
|
|
1846
|
+
if (z && nf) {
|
|
1847
|
+
for (; b + 4 < e; b += 4 + b2(d, b + 2)) {
|
|
1848
|
+
if (b2(d, b) == 1) {
|
|
1849
|
+
return [
|
|
1850
|
+
nsc ? b8(d, b + 4 + 8 * nsu) : sc,
|
|
1851
|
+
nsu ? b8(d, b + 4) : su,
|
|
1852
|
+
noff ? b8(d, b + 4 + 8 * (nsu + nsc)) : off,
|
|
1853
|
+
1
|
|
1854
|
+
];
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
// z == 2 for unknown whether or not zip64
|
|
1858
|
+
if (z < 2)
|
|
1859
|
+
err(13);
|
|
1860
|
+
}
|
|
1861
|
+
return [sc, su, off, 0];
|
|
1862
|
+
};
|
|
1863
|
+
// extra field length
|
|
1864
|
+
var exfl = function (ex) {
|
|
1865
|
+
var le = 0;
|
|
1866
|
+
if (ex) {
|
|
1867
|
+
for (var k in ex) {
|
|
1868
|
+
var l = ex[k].length;
|
|
1869
|
+
if (l > 65535)
|
|
1870
|
+
err(9);
|
|
1871
|
+
le += l + 4;
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
return le;
|
|
1875
|
+
};
|
|
1876
|
+
// write zip header
|
|
1877
|
+
var wzh = function (d, b, f, fn, u, c, ce, co) {
|
|
1878
|
+
var fl = fn.length, ex = f.extra, col = co && co.length;
|
|
1879
|
+
var exl = exfl(ex);
|
|
1880
|
+
wbytes(d, b, ce != null ? 0x2014B50 : 0x4034B50), b += 4;
|
|
1881
|
+
if (ce != null)
|
|
1882
|
+
d[b++] = 20, d[b++] = f.os;
|
|
1883
|
+
d[b] = 20, b += 2; // spec compliance? what's that?
|
|
1884
|
+
d[b++] = (f.flag << 1) | (c < 0 && 8), d[b++] = u && 8;
|
|
1885
|
+
d[b++] = f.compression & 255, d[b++] = f.compression >> 8;
|
|
1886
|
+
var dt = new Date(f.mtime == null ? Date.now() : f.mtime), y = dt.getFullYear() - 1980;
|
|
1887
|
+
if (y < 0 || y > 119)
|
|
1888
|
+
err(10);
|
|
1889
|
+
wbytes(d, b, (y << 25) | ((dt.getMonth() + 1) << 21) | (dt.getDate() << 16) | (dt.getHours() << 11) | (dt.getMinutes() << 5) | (dt.getSeconds() >> 1)), b += 4;
|
|
1890
|
+
if (c != -1) {
|
|
1891
|
+
wbytes(d, b, f.crc);
|
|
1892
|
+
wbytes(d, b + 4, c < 0 ? -c - 2 : c);
|
|
1893
|
+
wbytes(d, b + 8, f.size);
|
|
1894
|
+
}
|
|
1895
|
+
wbytes(d, b + 12, fl);
|
|
1896
|
+
wbytes(d, b + 14, exl), b += 16;
|
|
1897
|
+
if (ce != null) {
|
|
1898
|
+
wbytes(d, b, col);
|
|
1899
|
+
wbytes(d, b + 6, f.attrs);
|
|
1900
|
+
wbytes(d, b + 10, ce), b += 14;
|
|
1901
|
+
}
|
|
1902
|
+
d.set(fn, b);
|
|
1903
|
+
b += fl;
|
|
1904
|
+
if (exl) {
|
|
1905
|
+
for (var k in ex) {
|
|
1906
|
+
var exf = ex[k], l = exf.length;
|
|
1907
|
+
wbytes(d, b, +k);
|
|
1908
|
+
wbytes(d, b + 2, l);
|
|
1909
|
+
d.set(exf, b + 4), b += 4 + l;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
if (col)
|
|
1913
|
+
d.set(co, b), b += col;
|
|
1914
|
+
return b;
|
|
1915
|
+
};
|
|
1916
|
+
// write zip footer (end of central directory)
|
|
1917
|
+
var wzf = function (o, b, c, d, e) {
|
|
1918
|
+
wbytes(o, b, 0x6054B50); // skip disk
|
|
1919
|
+
wbytes(o, b + 8, c);
|
|
1920
|
+
wbytes(o, b + 10, c);
|
|
1921
|
+
wbytes(o, b + 12, d);
|
|
1922
|
+
wbytes(o, b + 16, e);
|
|
1923
|
+
};
|
|
1924
|
+
/**
|
|
1925
|
+
* A pass-through stream to keep data uncompressed in a ZIP archive.
|
|
1926
|
+
*/
|
|
1927
|
+
var ZipPassThrough = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1928
|
+
/**
|
|
1929
|
+
* Creates a pass-through stream that can be added to ZIP archives
|
|
1930
|
+
* @param filename The filename to associate with this data stream
|
|
1931
|
+
*/
|
|
1932
|
+
function ZipPassThrough(filename) {
|
|
1933
|
+
this.filename = filename;
|
|
1934
|
+
this.c = crc();
|
|
1935
|
+
this.size = 0;
|
|
1936
|
+
this.compression = 0;
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Processes a chunk and pushes to the output stream. You can override this
|
|
1940
|
+
* method in a subclass for custom behavior, but by default this passes
|
|
1941
|
+
* the data through. You must call this.ondata(err, chunk, final) at some
|
|
1942
|
+
* point in this method.
|
|
1943
|
+
* @param chunk The chunk to process
|
|
1944
|
+
* @param final Whether this is the last chunk
|
|
1945
|
+
*/
|
|
1946
|
+
ZipPassThrough.prototype.process = function (chunk, final) {
|
|
1947
|
+
this.ondata(null, chunk, final);
|
|
1948
|
+
};
|
|
1949
|
+
/**
|
|
1950
|
+
* Pushes a chunk to be added. If you are subclassing this with a custom
|
|
1951
|
+
* compression algorithm, note that you must push data from the source
|
|
1952
|
+
* file only, pre-compression.
|
|
1953
|
+
* @param chunk The chunk to push
|
|
1954
|
+
* @param final Whether this is the last chunk
|
|
1955
|
+
*/
|
|
1956
|
+
ZipPassThrough.prototype.push = function (chunk, final) {
|
|
1957
|
+
if (!this.ondata)
|
|
1958
|
+
err(5);
|
|
1959
|
+
this.c.p(chunk);
|
|
1960
|
+
this.size += chunk.length;
|
|
1961
|
+
if (final)
|
|
1962
|
+
this.crc = this.c.d();
|
|
1963
|
+
// we shouldn't really do this cast, but properly handling ArrayBufferLike
|
|
1964
|
+
// makes the API unergonomic with Buffer
|
|
1965
|
+
this.process(chunk, final || false);
|
|
1966
|
+
};
|
|
1967
|
+
return ZipPassThrough;
|
|
1968
|
+
}())));
|
|
1969
|
+
|
|
1970
|
+
// I don't extend because TypeScript extension adds 1kB of runtime bloat
|
|
1971
|
+
/**
|
|
1972
|
+
* Streaming DEFLATE compression for ZIP archives. Prefer using AsyncZipDeflate
|
|
1973
|
+
* for better performance
|
|
1974
|
+
*/
|
|
1975
|
+
var ZipDeflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
1976
|
+
/**
|
|
1977
|
+
* Creates a DEFLATE stream that can be added to ZIP archives
|
|
1978
|
+
* @param filename The filename to associate with this data stream
|
|
1979
|
+
* @param opts The compression options
|
|
1980
|
+
*/
|
|
1981
|
+
function ZipDeflate(filename, opts) {
|
|
1982
|
+
var _this = this;
|
|
1983
|
+
if (!opts)
|
|
1984
|
+
opts = {};
|
|
1985
|
+
ZipPassThrough.call(this, filename);
|
|
1986
|
+
this.d = new Deflate(opts, function (dat, final) {
|
|
1987
|
+
_this.ondata(null, dat, final);
|
|
1988
|
+
});
|
|
1989
|
+
this.compression = 8;
|
|
1990
|
+
this.flag = dbf(opts.level);
|
|
1991
|
+
}
|
|
1992
|
+
ZipDeflate.prototype.process = function (chunk, final) {
|
|
1993
|
+
try {
|
|
1994
|
+
this.d.push(chunk, final);
|
|
1995
|
+
}
|
|
1996
|
+
catch (e) {
|
|
1997
|
+
this.ondata(e, null, final);
|
|
1998
|
+
}
|
|
1999
|
+
};
|
|
2000
|
+
/**
|
|
2001
|
+
* Pushes a chunk to be deflated
|
|
2002
|
+
* @param chunk The chunk to push
|
|
2003
|
+
* @param final Whether this is the last chunk
|
|
2004
|
+
*/
|
|
2005
|
+
ZipDeflate.prototype.push = function (chunk, final) {
|
|
2006
|
+
ZipPassThrough.prototype.push.call(this, chunk, final);
|
|
2007
|
+
};
|
|
2008
|
+
return ZipDeflate;
|
|
2009
|
+
}())));
|
|
2010
|
+
|
|
2011
|
+
/**
|
|
2012
|
+
* Asynchronous streaming DEFLATE compression for ZIP archives
|
|
2013
|
+
*/
|
|
2014
|
+
var AsyncZipDeflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
2015
|
+
/**
|
|
2016
|
+
* Creates an asynchronous DEFLATE stream that can be added to ZIP archives
|
|
2017
|
+
* @param filename The filename to associate with this data stream
|
|
2018
|
+
* @param opts The compression options
|
|
2019
|
+
*/
|
|
2020
|
+
function AsyncZipDeflate(filename, opts) {
|
|
2021
|
+
var _this = this;
|
|
2022
|
+
if (!opts)
|
|
2023
|
+
opts = {};
|
|
2024
|
+
ZipPassThrough.call(this, filename);
|
|
2025
|
+
this.d = new AsyncDeflate(opts, function (err, dat, final) {
|
|
2026
|
+
_this.ondata(err, dat, final);
|
|
2027
|
+
});
|
|
2028
|
+
this.compression = 8;
|
|
2029
|
+
this.flag = dbf(opts.level);
|
|
2030
|
+
this.terminate = this.d.terminate;
|
|
2031
|
+
}
|
|
2032
|
+
AsyncZipDeflate.prototype.process = function (chunk, final) {
|
|
2033
|
+
this.d.push(chunk, final);
|
|
2034
|
+
};
|
|
2035
|
+
/**
|
|
2036
|
+
* Pushes a chunk to be deflated
|
|
2037
|
+
* @param chunk The chunk to push
|
|
2038
|
+
* @param final Whether this is the last chunk
|
|
2039
|
+
*/
|
|
2040
|
+
AsyncZipDeflate.prototype.push = function (chunk, final) {
|
|
2041
|
+
ZipPassThrough.prototype.push.call(this, chunk, final);
|
|
2042
|
+
};
|
|
2043
|
+
return AsyncZipDeflate;
|
|
2044
|
+
}())));
|
|
2045
|
+
|
|
2046
|
+
// TODO: Better tree shaking
|
|
2047
|
+
/**
|
|
2048
|
+
* A zippable archive to which files can incrementally be added
|
|
2049
|
+
*/
|
|
2050
|
+
var Zip = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
2051
|
+
/**
|
|
2052
|
+
* Creates an empty ZIP archive to which files can be added
|
|
2053
|
+
* @param cb The callback to call whenever data for the generated ZIP archive
|
|
2054
|
+
* is available
|
|
2055
|
+
*/
|
|
2056
|
+
function Zip(cb) {
|
|
2057
|
+
this.ondata = cb;
|
|
2058
|
+
this.u = [];
|
|
2059
|
+
this.d = 1;
|
|
2060
|
+
}
|
|
2061
|
+
/**
|
|
2062
|
+
* Adds a file to the ZIP archive
|
|
2063
|
+
* @param file The file stream to add
|
|
2064
|
+
*/
|
|
2065
|
+
Zip.prototype.add = function (file) {
|
|
2066
|
+
var _this = this;
|
|
2067
|
+
if (!this.ondata)
|
|
2068
|
+
err(5);
|
|
2069
|
+
// finishing or finished
|
|
2070
|
+
if (this.d & 2)
|
|
2071
|
+
this.ondata(err(4 + (this.d & 1) * 8, 0, 1), null, false);
|
|
2072
|
+
else {
|
|
2073
|
+
var f = strToU8(file.filename), fl_1 = f.length;
|
|
2074
|
+
var com = file.comment, o = com && strToU8(com);
|
|
2075
|
+
var u = fl_1 != file.filename.length || (o && (com.length != o.length));
|
|
2076
|
+
var hl_1 = fl_1 + exfl(file.extra) + 30;
|
|
2077
|
+
if (fl_1 > 65535)
|
|
2078
|
+
this.ondata(err(11, 0, 1), null, false);
|
|
2079
|
+
var header = new u8(hl_1);
|
|
2080
|
+
wzh(header, 0, file, f, u, -1);
|
|
2081
|
+
var chks_1 = [header];
|
|
2082
|
+
var pAll_1 = function () {
|
|
2083
|
+
for (var _i = 0, chks_2 = chks_1; _i < chks_2.length; _i++) {
|
|
2084
|
+
var chk = chks_2[_i];
|
|
2085
|
+
_this.ondata(null, chk, false);
|
|
2086
|
+
}
|
|
2087
|
+
chks_1 = [];
|
|
2088
|
+
};
|
|
2089
|
+
var tr_1 = this.d;
|
|
2090
|
+
this.d = 0;
|
|
2091
|
+
var ind_1 = this.u.length;
|
|
2092
|
+
var uf_1 = mrg(file, {
|
|
2093
|
+
f: f,
|
|
2094
|
+
u: u,
|
|
2095
|
+
o: o,
|
|
2096
|
+
t: function () {
|
|
2097
|
+
if (file.terminate)
|
|
2098
|
+
file.terminate();
|
|
2099
|
+
},
|
|
2100
|
+
r: function () {
|
|
2101
|
+
pAll_1();
|
|
2102
|
+
if (tr_1) {
|
|
2103
|
+
var nxt = _this.u[ind_1 + 1];
|
|
2104
|
+
if (nxt)
|
|
2105
|
+
nxt.r();
|
|
2106
|
+
else
|
|
2107
|
+
_this.d = 1;
|
|
2108
|
+
}
|
|
2109
|
+
tr_1 = 1;
|
|
2110
|
+
}
|
|
2111
|
+
});
|
|
2112
|
+
var cl_1 = 0;
|
|
2113
|
+
file.ondata = function (err, dat, final) {
|
|
2114
|
+
if (err) {
|
|
2115
|
+
_this.ondata(err, dat, final);
|
|
2116
|
+
_this.terminate();
|
|
2117
|
+
}
|
|
2118
|
+
else {
|
|
2119
|
+
cl_1 += dat.length;
|
|
2120
|
+
chks_1.push(dat);
|
|
2121
|
+
if (final) {
|
|
2122
|
+
var dd = new u8(16);
|
|
2123
|
+
wbytes(dd, 0, 0x8074B50);
|
|
2124
|
+
wbytes(dd, 4, file.crc);
|
|
2125
|
+
wbytes(dd, 8, cl_1);
|
|
2126
|
+
wbytes(dd, 12, file.size);
|
|
2127
|
+
chks_1.push(dd);
|
|
2128
|
+
uf_1.c = cl_1, uf_1.b = hl_1 + cl_1 + 16, uf_1.crc = file.crc, uf_1.size = file.size;
|
|
2129
|
+
if (tr_1)
|
|
2130
|
+
uf_1.r();
|
|
2131
|
+
tr_1 = 1;
|
|
2132
|
+
}
|
|
2133
|
+
else if (tr_1)
|
|
2134
|
+
pAll_1();
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
this.u.push(uf_1);
|
|
2138
|
+
}
|
|
2139
|
+
};
|
|
2140
|
+
/**
|
|
2141
|
+
* Ends the process of adding files and prepares to emit the final chunks.
|
|
2142
|
+
* This *must* be called after adding all desired files for the resulting
|
|
2143
|
+
* ZIP file to work properly.
|
|
2144
|
+
*/
|
|
2145
|
+
Zip.prototype.end = function () {
|
|
2146
|
+
var _this = this;
|
|
2147
|
+
if (this.d & 2) {
|
|
2148
|
+
this.ondata(err(4 + (this.d & 1) * 8, 0, 1), null, true);
|
|
2149
|
+
return;
|
|
2150
|
+
}
|
|
2151
|
+
if (this.d)
|
|
2152
|
+
this.e();
|
|
2153
|
+
else
|
|
2154
|
+
this.u.push({
|
|
2155
|
+
r: function () {
|
|
2156
|
+
if (!(_this.d & 1))
|
|
2157
|
+
return;
|
|
2158
|
+
_this.u.splice(-1, 1);
|
|
2159
|
+
_this.e();
|
|
2160
|
+
},
|
|
2161
|
+
t: function () { }
|
|
2162
|
+
});
|
|
2163
|
+
this.d = 3;
|
|
2164
|
+
};
|
|
2165
|
+
Zip.prototype.e = function () {
|
|
2166
|
+
var bt = 0, l = 0, tl = 0;
|
|
2167
|
+
for (var _i = 0, _a = this.u; _i < _a.length; _i++) {
|
|
2168
|
+
var f = _a[_i];
|
|
2169
|
+
tl += 46 + f.f.length + exfl(f.extra) + (f.o ? f.o.length : 0);
|
|
2170
|
+
}
|
|
2171
|
+
var out = new u8(tl + 22);
|
|
2172
|
+
for (var _b = 0, _c = this.u; _b < _c.length; _b++) {
|
|
2173
|
+
var f = _c[_b];
|
|
2174
|
+
wzh(out, bt, f, f.f, f.u, -f.c - 2, l, f.o);
|
|
2175
|
+
bt += 46 + f.f.length + exfl(f.extra) + (f.o ? f.o.length : 0), l += f.b;
|
|
2176
|
+
}
|
|
2177
|
+
wzf(out, bt, this.u.length, tl, l);
|
|
2178
|
+
this.ondata(null, out, true);
|
|
2179
|
+
this.d = 2;
|
|
2180
|
+
};
|
|
2181
|
+
/**
|
|
2182
|
+
* A method to terminate any internal workers used by the stream. Subsequent
|
|
2183
|
+
* calls to add() will fail.
|
|
2184
|
+
*/
|
|
2185
|
+
Zip.prototype.terminate = function () {
|
|
2186
|
+
for (var _i = 0, _a = this.u; _i < _a.length; _i++) {
|
|
2187
|
+
var f = _a[_i];
|
|
2188
|
+
f.t();
|
|
2189
|
+
}
|
|
2190
|
+
this.d = 2;
|
|
2191
|
+
};
|
|
2192
|
+
return Zip;
|
|
2193
|
+
}())));
|
|
2194
|
+
|
|
2195
|
+
function zip(data, opts, cb) {
|
|
2196
|
+
if (!cb)
|
|
2197
|
+
cb = opts, opts = {};
|
|
2198
|
+
if (typeof cb != 'function')
|
|
2199
|
+
err(7);
|
|
2200
|
+
var r = {};
|
|
2201
|
+
fltn(data, '', r, opts);
|
|
2202
|
+
var k = Object.keys(r);
|
|
2203
|
+
var lft = k.length, o = 0, tot = 0;
|
|
2204
|
+
var slft = lft, files = new Array(lft);
|
|
2205
|
+
var term = [];
|
|
2206
|
+
var tAll = function () {
|
|
2207
|
+
for (var i = 0; i < term.length; ++i)
|
|
2208
|
+
term[i]();
|
|
2209
|
+
};
|
|
2210
|
+
var cbd = function (a, b) {
|
|
2211
|
+
mt(function () { cb(a, b); });
|
|
2212
|
+
};
|
|
2213
|
+
mt(function () { cbd = cb; });
|
|
2214
|
+
var cbf = function () {
|
|
2215
|
+
var out = new u8(tot + 22), oe = o, cdl = tot - o;
|
|
2216
|
+
tot = 0;
|
|
2217
|
+
for (var i = 0; i < slft; ++i) {
|
|
2218
|
+
var f = files[i];
|
|
2219
|
+
try {
|
|
2220
|
+
var l = f.c.length;
|
|
2221
|
+
wzh(out, tot, f, f.f, f.u, l);
|
|
2222
|
+
var badd = 30 + f.f.length + exfl(f.extra);
|
|
2223
|
+
var loc = tot + badd;
|
|
2224
|
+
out.set(f.c, loc);
|
|
2225
|
+
wzh(out, o, f, f.f, f.u, l, tot, f.m), o += 16 + badd + (f.m ? f.m.length : 0), tot = loc + l;
|
|
2226
|
+
}
|
|
2227
|
+
catch (e) {
|
|
2228
|
+
return cbd(e, null);
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
wzf(out, o, files.length, cdl, oe);
|
|
2232
|
+
cbd(null, out);
|
|
2233
|
+
};
|
|
2234
|
+
if (!lft)
|
|
2235
|
+
cbf();
|
|
2236
|
+
var _loop_1 = function (i) {
|
|
2237
|
+
var fn = k[i];
|
|
2238
|
+
var _a = r[fn], file = _a[0], p = _a[1];
|
|
2239
|
+
var c = crc(), size = file.length;
|
|
2240
|
+
c.p(file);
|
|
2241
|
+
var f = strToU8(fn), s = f.length;
|
|
2242
|
+
var com = p.comment, m = com && strToU8(com), ms = m && m.length;
|
|
2243
|
+
var exl = exfl(p.extra);
|
|
2244
|
+
var compression = p.level == 0 ? 0 : 8;
|
|
2245
|
+
var cbl = function (e, d) {
|
|
2246
|
+
if (e) {
|
|
2247
|
+
tAll();
|
|
2248
|
+
cbd(e, null);
|
|
2249
|
+
}
|
|
2250
|
+
else {
|
|
2251
|
+
var l = d.length;
|
|
2252
|
+
files[i] = mrg(p, {
|
|
2253
|
+
size: size,
|
|
2254
|
+
crc: c.d(),
|
|
2255
|
+
c: d,
|
|
2256
|
+
f: f,
|
|
2257
|
+
m: m,
|
|
2258
|
+
u: s != fn.length || (m && (com.length != ms)),
|
|
2259
|
+
compression: compression
|
|
2260
|
+
});
|
|
2261
|
+
o += 30 + s + exl + l;
|
|
2262
|
+
tot += 76 + 2 * (s + exl) + (ms || 0) + l;
|
|
2263
|
+
if (!--lft)
|
|
2264
|
+
cbf();
|
|
2265
|
+
}
|
|
2266
|
+
};
|
|
2267
|
+
if (s > 65535)
|
|
2268
|
+
cbl(err(11, 0, 1), null);
|
|
2269
|
+
if (!compression)
|
|
2270
|
+
cbl(null, file);
|
|
2271
|
+
else if (size < 160000) {
|
|
2272
|
+
try {
|
|
2273
|
+
cbl(null, deflateSync(file, p));
|
|
2274
|
+
}
|
|
2275
|
+
catch (e) {
|
|
2276
|
+
cbl(e, null);
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
else
|
|
2280
|
+
term.push(deflate(file, p, cbl));
|
|
2281
|
+
};
|
|
2282
|
+
// Cannot use lft because it can decrease
|
|
2283
|
+
for (var i = 0; i < slft; ++i) {
|
|
2284
|
+
_loop_1(i);
|
|
2285
|
+
}
|
|
2286
|
+
return tAll;
|
|
2287
|
+
}
|
|
2288
|
+
/**
|
|
2289
|
+
* Synchronously creates a ZIP file. Prefer using `zip` for better performance
|
|
2290
|
+
* with more than one file.
|
|
2291
|
+
* @param data The directory structure for the ZIP archive
|
|
2292
|
+
* @param opts The main options, merged with per-file options
|
|
2293
|
+
* @returns The generated ZIP archive
|
|
2294
|
+
*/
|
|
2295
|
+
function zipSync(data, opts) {
|
|
2296
|
+
if (!opts)
|
|
2297
|
+
opts = {};
|
|
2298
|
+
var r = {};
|
|
2299
|
+
var files = [];
|
|
2300
|
+
fltn(data, '', r, opts);
|
|
2301
|
+
var o = 0;
|
|
2302
|
+
var tot = 0;
|
|
2303
|
+
for (var fn in r) {
|
|
2304
|
+
var _a = r[fn], file = _a[0], p = _a[1];
|
|
2305
|
+
var compression = p.level == 0 ? 0 : 8;
|
|
2306
|
+
var f = strToU8(fn), s = f.length;
|
|
2307
|
+
var com = p.comment, m = com && strToU8(com), ms = m && m.length;
|
|
2308
|
+
var exl = exfl(p.extra);
|
|
2309
|
+
if (s > 65535)
|
|
2310
|
+
err(11);
|
|
2311
|
+
var d = compression ? deflateSync(file, p) : file, l = d.length;
|
|
2312
|
+
var c = crc();
|
|
2313
|
+
c.p(file);
|
|
2314
|
+
files.push(mrg(p, {
|
|
2315
|
+
size: file.length,
|
|
2316
|
+
crc: c.d(),
|
|
2317
|
+
c: d,
|
|
2318
|
+
f: f,
|
|
2319
|
+
m: m,
|
|
2320
|
+
u: s != fn.length || (m && (com.length != ms)),
|
|
2321
|
+
o: o,
|
|
2322
|
+
compression: compression
|
|
2323
|
+
}));
|
|
2324
|
+
o += 30 + s + exl + l;
|
|
2325
|
+
tot += 76 + 2 * (s + exl) + (ms || 0) + l;
|
|
2326
|
+
}
|
|
2327
|
+
var out = new u8(tot + 22), oe = o, cdl = tot - o;
|
|
2328
|
+
for (var i = 0; i < files.length; ++i) {
|
|
2329
|
+
var f = files[i];
|
|
2330
|
+
wzh(out, f.o, f, f.f, f.u, f.c.length);
|
|
2331
|
+
var badd = 30 + f.f.length + exfl(f.extra);
|
|
2332
|
+
out.set(f.c, f.o + badd);
|
|
2333
|
+
wzh(out, o, f, f.f, f.u, f.c.length, f.o, f.m), o += 16 + badd + (f.m ? f.m.length : 0);
|
|
2334
|
+
}
|
|
2335
|
+
wzf(out, o, files.length, cdl, oe);
|
|
2336
|
+
return out;
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* Streaming pass-through decompression for ZIP archives
|
|
2340
|
+
*/
|
|
2341
|
+
var UnzipPassThrough = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
2342
|
+
function UnzipPassThrough() {
|
|
2343
|
+
}
|
|
2344
|
+
UnzipPassThrough.prototype.push = function (chunk, final) {
|
|
2345
|
+
// same as ZipPassThrough: cast to retain Buffer ergonomics
|
|
2346
|
+
this.ondata(null, chunk, final);
|
|
2347
|
+
};
|
|
2348
|
+
UnzipPassThrough.compression = 0;
|
|
2349
|
+
return UnzipPassThrough;
|
|
2350
|
+
}())));
|
|
2351
|
+
|
|
2352
|
+
/**
|
|
2353
|
+
* Streaming DEFLATE decompression for ZIP archives. Prefer AsyncZipInflate for
|
|
2354
|
+
* better performance.
|
|
2355
|
+
*/
|
|
2356
|
+
var UnzipInflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
2357
|
+
/**
|
|
2358
|
+
* Creates a DEFLATE decompression that can be used in ZIP archives
|
|
2359
|
+
*/
|
|
2360
|
+
function UnzipInflate() {
|
|
2361
|
+
var _this = this;
|
|
2362
|
+
this.i = new Inflate(function (dat, final) {
|
|
2363
|
+
_this.ondata(null, dat, final);
|
|
2364
|
+
});
|
|
2365
|
+
}
|
|
2366
|
+
UnzipInflate.prototype.push = function (chunk, final) {
|
|
2367
|
+
try {
|
|
2368
|
+
this.i.push(chunk, final);
|
|
2369
|
+
}
|
|
2370
|
+
catch (e) {
|
|
2371
|
+
this.ondata(e, null, final);
|
|
2372
|
+
}
|
|
2373
|
+
};
|
|
2374
|
+
UnzipInflate.compression = 8;
|
|
2375
|
+
return UnzipInflate;
|
|
2376
|
+
}())));
|
|
2377
|
+
|
|
2378
|
+
/**
|
|
2379
|
+
* Asynchronous streaming DEFLATE decompression for ZIP archives
|
|
2380
|
+
*/
|
|
2381
|
+
var AsyncUnzipInflate = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
2382
|
+
/**
|
|
2383
|
+
* Creates a DEFLATE decompression that can be used in ZIP archives
|
|
2384
|
+
*/
|
|
2385
|
+
function AsyncUnzipInflate(_, sz) {
|
|
2386
|
+
var _this = this;
|
|
2387
|
+
if (sz < 320000) {
|
|
2388
|
+
this.i = new Inflate(function (dat, final) {
|
|
2389
|
+
_this.ondata(null, dat, final);
|
|
2390
|
+
});
|
|
2391
|
+
}
|
|
2392
|
+
else {
|
|
2393
|
+
this.i = new AsyncInflate(function (err, dat, final) {
|
|
2394
|
+
_this.ondata(err, dat, final);
|
|
2395
|
+
});
|
|
2396
|
+
this.terminate = this.i.terminate;
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
AsyncUnzipInflate.prototype.push = function (chunk, final) {
|
|
2400
|
+
if (this.i.terminate)
|
|
2401
|
+
chunk = slc(chunk, 0);
|
|
2402
|
+
this.i.push(chunk, final);
|
|
2403
|
+
};
|
|
2404
|
+
AsyncUnzipInflate.compression = 8;
|
|
2405
|
+
return AsyncUnzipInflate;
|
|
2406
|
+
}())));
|
|
2407
|
+
|
|
2408
|
+
/**
|
|
2409
|
+
* A ZIP archive decompression stream that emits files as they are discovered
|
|
2410
|
+
*/
|
|
2411
|
+
var Unzip = /*#__PURE__*/ ((/* unused pure expression or super */ null && (function () {
|
|
2412
|
+
/**
|
|
2413
|
+
* Creates a ZIP decompression stream
|
|
2414
|
+
* @param cb The callback to call whenever a file in the ZIP archive is found
|
|
2415
|
+
*/
|
|
2416
|
+
function Unzip(cb) {
|
|
2417
|
+
this.onfile = cb;
|
|
2418
|
+
this.k = [];
|
|
2419
|
+
this.o = {
|
|
2420
|
+
0: UnzipPassThrough
|
|
2421
|
+
};
|
|
2422
|
+
this.p = et;
|
|
2423
|
+
}
|
|
2424
|
+
/**
|
|
2425
|
+
* Pushes a chunk to be unzipped
|
|
2426
|
+
* @param chunk The chunk to push
|
|
2427
|
+
* @param final Whether this is the last chunk
|
|
2428
|
+
*/
|
|
2429
|
+
Unzip.prototype.push = function (chunk, final) {
|
|
2430
|
+
var _this = this;
|
|
2431
|
+
if (!this.onfile)
|
|
2432
|
+
err(5);
|
|
2433
|
+
if (!this.p)
|
|
2434
|
+
err(4);
|
|
2435
|
+
if (this.c > 0) {
|
|
2436
|
+
var len = Math.min(this.c, chunk.length);
|
|
2437
|
+
var toAdd = chunk.subarray(0, len);
|
|
2438
|
+
this.c -= len;
|
|
2439
|
+
if (this.d)
|
|
2440
|
+
this.d.push(toAdd, !this.c);
|
|
2441
|
+
else
|
|
2442
|
+
this.k[0].push(toAdd);
|
|
2443
|
+
chunk = chunk.subarray(len);
|
|
2444
|
+
if (chunk.length)
|
|
2445
|
+
return this.push(chunk, final);
|
|
2446
|
+
}
|
|
2447
|
+
else {
|
|
2448
|
+
var f = 0, i = 0, is = void 0, buf = void 0;
|
|
2449
|
+
if (!this.p.length)
|
|
2450
|
+
buf = chunk;
|
|
2451
|
+
else if (!chunk.length)
|
|
2452
|
+
buf = this.p;
|
|
2453
|
+
else {
|
|
2454
|
+
buf = new u8(this.p.length + chunk.length);
|
|
2455
|
+
buf.set(this.p), buf.set(chunk, this.p.length);
|
|
2456
|
+
}
|
|
2457
|
+
var l = buf.length, oc = this.c, add = oc && this.d;
|
|
2458
|
+
var _loop_2 = function () {
|
|
2459
|
+
var sig = b4(buf, i);
|
|
2460
|
+
if (sig == 0x4034B50) {
|
|
2461
|
+
f = 1, is = i;
|
|
2462
|
+
this_1.d = null;
|
|
2463
|
+
this_1.c = 0;
|
|
2464
|
+
var bf = b2(buf, i + 6), cmp_1 = b2(buf, i + 8), u = bf & 2048, dd = bf & 8, fnl = b2(buf, i + 26), es = b2(buf, i + 28);
|
|
2465
|
+
if (l > i + 30 + fnl + es) {
|
|
2466
|
+
var chks_3 = [];
|
|
2467
|
+
this_1.k.unshift(chks_3);
|
|
2468
|
+
f = 2;
|
|
2469
|
+
var lsc = b4(buf, i + 18), lsu = b4(buf, i + 22);
|
|
2470
|
+
var fn_1 = strFromU8(buf.subarray(i + 30, i += 30 + fnl), !u);
|
|
2471
|
+
var _a = z64hs(buf, i, es, 2, lsc, lsu, 0), sc_1 = _a[0], su_1 = _a[1], z64 = _a[3];
|
|
2472
|
+
if (dd)
|
|
2473
|
+
sc_1 = -1 - z64;
|
|
2474
|
+
i += es;
|
|
2475
|
+
this_1.c = sc_1;
|
|
2476
|
+
var d_1;
|
|
2477
|
+
var file_1 = {
|
|
2478
|
+
name: fn_1,
|
|
2479
|
+
compression: cmp_1,
|
|
2480
|
+
start: function () {
|
|
2481
|
+
if (!file_1.ondata)
|
|
2482
|
+
err(5);
|
|
2483
|
+
if (!sc_1)
|
|
2484
|
+
file_1.ondata(null, et, true);
|
|
2485
|
+
else {
|
|
2486
|
+
var ctr = _this.o[cmp_1];
|
|
2487
|
+
if (!ctr)
|
|
2488
|
+
file_1.ondata(err(14, 'unknown compression type ' + cmp_1, 1), null, false);
|
|
2489
|
+
d_1 = sc_1 < 0 ? new ctr(fn_1) : new ctr(fn_1, sc_1, su_1);
|
|
2490
|
+
d_1.ondata = function (err, dat, final) { file_1.ondata(err, dat, final); };
|
|
2491
|
+
for (var _i = 0, chks_4 = chks_3; _i < chks_4.length; _i++) {
|
|
2492
|
+
var dat = chks_4[_i];
|
|
2493
|
+
d_1.push(dat, false);
|
|
2494
|
+
}
|
|
2495
|
+
if (_this.k[0] == chks_3 && _this.c)
|
|
2496
|
+
_this.d = d_1;
|
|
2497
|
+
else
|
|
2498
|
+
d_1.push(et, true);
|
|
2499
|
+
}
|
|
2500
|
+
},
|
|
2501
|
+
terminate: function () {
|
|
2502
|
+
if (d_1 && d_1.terminate)
|
|
2503
|
+
d_1.terminate();
|
|
2504
|
+
}
|
|
2505
|
+
};
|
|
2506
|
+
if (sc_1 >= 0)
|
|
2507
|
+
file_1.size = sc_1, file_1.originalSize = su_1;
|
|
2508
|
+
this_1.onfile(file_1);
|
|
2509
|
+
}
|
|
2510
|
+
return "break";
|
|
2511
|
+
}
|
|
2512
|
+
else if (oc) {
|
|
2513
|
+
if (sig == 0x8074B50) {
|
|
2514
|
+
is = i += 12 + (oc == -2 && 8), f = 3, this_1.c = 0;
|
|
2515
|
+
return "break";
|
|
2516
|
+
}
|
|
2517
|
+
else if (sig == 0x2014B50) {
|
|
2518
|
+
is = i -= 4, f = 3, this_1.c = 0;
|
|
2519
|
+
return "break";
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
};
|
|
2523
|
+
var this_1 = this;
|
|
2524
|
+
for (; i < l - 4; ++i) {
|
|
2525
|
+
var state_1 = _loop_2();
|
|
2526
|
+
if (state_1 === "break")
|
|
2527
|
+
break;
|
|
2528
|
+
}
|
|
2529
|
+
this.p = et;
|
|
2530
|
+
if (oc < 0) {
|
|
2531
|
+
var dat = f ? buf.subarray(0, is - 12 - (oc == -2 && 8) - (b4(buf, is - 16) == 0x8074B50 && 4)) : buf.subarray(0, i);
|
|
2532
|
+
if (add)
|
|
2533
|
+
add.push(dat, !!f);
|
|
2534
|
+
else
|
|
2535
|
+
this.k[+(f == 2)].push(dat);
|
|
2536
|
+
}
|
|
2537
|
+
if (f & 2)
|
|
2538
|
+
return this.push(buf.subarray(i), final);
|
|
2539
|
+
this.p = buf.subarray(i);
|
|
2540
|
+
}
|
|
2541
|
+
if (final) {
|
|
2542
|
+
if (this.c)
|
|
2543
|
+
err(13);
|
|
2544
|
+
this.p = null;
|
|
2545
|
+
}
|
|
2546
|
+
};
|
|
2547
|
+
/**
|
|
2548
|
+
* Registers a decoder with the stream, allowing for files compressed with
|
|
2549
|
+
* the compression type provided to be expanded correctly
|
|
2550
|
+
* @param decoder The decoder constructor
|
|
2551
|
+
*/
|
|
2552
|
+
Unzip.prototype.register = function (decoder) {
|
|
2553
|
+
this.o[decoder.compression] = decoder;
|
|
2554
|
+
};
|
|
2555
|
+
return Unzip;
|
|
2556
|
+
}())));
|
|
2557
|
+
|
|
2558
|
+
var mt = typeof queueMicrotask == 'function' ? queueMicrotask : typeof setTimeout == 'function' ? setTimeout : function (fn) { fn(); };
|
|
2559
|
+
function unzip(data, opts, cb) {
|
|
2560
|
+
if (!cb)
|
|
2561
|
+
cb = opts, opts = {};
|
|
2562
|
+
if (typeof cb != 'function')
|
|
2563
|
+
err(7);
|
|
2564
|
+
var term = [];
|
|
2565
|
+
var tAll = function () {
|
|
2566
|
+
for (var i = 0; i < term.length; ++i)
|
|
2567
|
+
term[i]();
|
|
2568
|
+
};
|
|
2569
|
+
var files = {};
|
|
2570
|
+
var cbd = function (a, b) {
|
|
2571
|
+
mt(function () { cb(a, b); });
|
|
2572
|
+
};
|
|
2573
|
+
mt(function () { cbd = cb; });
|
|
2574
|
+
var e = data.length - 22;
|
|
2575
|
+
for (; b4(data, e) != 0x6054B50; --e) {
|
|
2576
|
+
if (!e || data.length - e > 65558) {
|
|
2577
|
+
cbd(err(13, 0, 1), null);
|
|
2578
|
+
return tAll;
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
;
|
|
2582
|
+
var lft = b2(data, e + 8);
|
|
2583
|
+
if (lft) {
|
|
2584
|
+
var c = lft;
|
|
2585
|
+
var o = b4(data, e + 16);
|
|
2586
|
+
var z = b4(data, e - 20) == 0x7064B50;
|
|
2587
|
+
if (z) {
|
|
2588
|
+
var ze = b4(data, e - 12);
|
|
2589
|
+
z = b4(data, ze) == 0x6064B50;
|
|
2590
|
+
if (z) {
|
|
2591
|
+
c = lft = b4(data, ze + 32);
|
|
2592
|
+
o = b4(data, ze + 48);
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
var fltr = opts && opts.filter;
|
|
2596
|
+
var _loop_3 = function (i) {
|
|
2597
|
+
var _a = zh(data, o, z), c_1 = _a[0], sc = _a[1], su = _a[2], fn = _a[3], no = _a[4], off = _a[5], b = slzh(data, off);
|
|
2598
|
+
o = no;
|
|
2599
|
+
var cbl = function (e, d) {
|
|
2600
|
+
if (e) {
|
|
2601
|
+
tAll();
|
|
2602
|
+
cbd(e, null);
|
|
2603
|
+
}
|
|
2604
|
+
else {
|
|
2605
|
+
if (d)
|
|
2606
|
+
files[fn] = d;
|
|
2607
|
+
if (!--lft)
|
|
2608
|
+
cbd(null, files);
|
|
2609
|
+
}
|
|
2610
|
+
};
|
|
2611
|
+
if (!fltr || fltr({
|
|
2612
|
+
name: fn,
|
|
2613
|
+
size: sc,
|
|
2614
|
+
originalSize: su,
|
|
2615
|
+
compression: c_1
|
|
2616
|
+
})) {
|
|
2617
|
+
if (!c_1)
|
|
2618
|
+
cbl(null, slc(data, b, b + sc));
|
|
2619
|
+
else if (c_1 == 8) {
|
|
2620
|
+
var infl = data.subarray(b, b + sc);
|
|
2621
|
+
// Synchronously decompress under 512KB, or barely-compressed data
|
|
2622
|
+
if (su < 524288 || sc > 0.8 * su) {
|
|
2623
|
+
try {
|
|
2624
|
+
cbl(null, inflateSync(infl, { out: new u8(su) }));
|
|
2625
|
+
}
|
|
2626
|
+
catch (e) {
|
|
2627
|
+
cbl(e, null);
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2630
|
+
else
|
|
2631
|
+
term.push(inflate(infl, { size: su }, cbl));
|
|
2632
|
+
}
|
|
2633
|
+
else
|
|
2634
|
+
cbl(err(14, 'unknown compression type ' + c_1, 1), null);
|
|
2635
|
+
}
|
|
2636
|
+
else
|
|
2637
|
+
cbl(null, null);
|
|
2638
|
+
};
|
|
2639
|
+
for (var i = 0; i < c; ++i) {
|
|
2640
|
+
_loop_3(i);
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
else
|
|
2644
|
+
cbd(null, {});
|
|
2645
|
+
return tAll;
|
|
2646
|
+
}
|
|
2647
|
+
/**
|
|
2648
|
+
* Synchronously decompresses a ZIP archive. Prefer using `unzip` for better
|
|
2649
|
+
* performance with more than one file.
|
|
2650
|
+
* @param data The raw compressed ZIP file
|
|
2651
|
+
* @param opts The ZIP extraction options
|
|
2652
|
+
* @returns The decompressed files
|
|
2653
|
+
*/
|
|
2654
|
+
function unzipSync(data, opts) {
|
|
2655
|
+
var files = {};
|
|
2656
|
+
var e = data.length - 22;
|
|
2657
|
+
for (; b4(data, e) != 0x6054B50; --e) {
|
|
2658
|
+
if (!e || data.length - e > 65558)
|
|
2659
|
+
err(13);
|
|
2660
|
+
}
|
|
2661
|
+
;
|
|
2662
|
+
var c = b2(data, e + 8);
|
|
2663
|
+
if (!c)
|
|
2664
|
+
return {};
|
|
2665
|
+
var o = b4(data, e + 16);
|
|
2666
|
+
var z = b4(data, e - 20) == 0x7064B50;
|
|
2667
|
+
if (z) {
|
|
2668
|
+
var ze = b4(data, e - 12);
|
|
2669
|
+
z = b4(data, ze) == 0x6064B50;
|
|
2670
|
+
if (z) {
|
|
2671
|
+
c = b4(data, ze + 32);
|
|
2672
|
+
o = b4(data, ze + 48);
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2675
|
+
var fltr = opts && opts.filter;
|
|
2676
|
+
for (var i = 0; i < c; ++i) {
|
|
2677
|
+
var _a = zh(data, o, z), c_2 = _a[0], sc = _a[1], su = _a[2], fn = _a[3], no = _a[4], off = _a[5], b = slzh(data, off);
|
|
2678
|
+
o = no;
|
|
2679
|
+
if (!fltr || fltr({
|
|
2680
|
+
name: fn,
|
|
2681
|
+
size: sc,
|
|
2682
|
+
originalSize: su,
|
|
2683
|
+
compression: c_2
|
|
2684
|
+
})) {
|
|
2685
|
+
if (!c_2)
|
|
2686
|
+
files[fn] = slc(data, b, b + sc);
|
|
2687
|
+
else if (c_2 == 8)
|
|
2688
|
+
files[fn] = inflateSync(data.subarray(b, b + sc), { out: new u8(su) });
|
|
2689
|
+
else
|
|
2690
|
+
err(14, 'unknown compression type ' + c_2);
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
return files;
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
;// ./src/x_ite-sog-parser-2.js
|
|
2697
|
+
|
|
2698
|
+
|
|
2699
|
+
const X3D = window [Symbol .for ("X_ITE.X3D")];
|
|
2700
|
+
|
|
2701
|
+
/*
|
|
2702
|
+
* Parser
|
|
2703
|
+
* Reference: https://github.com/playcanvas/splat-transform/blob/main/src/lib/readers/read-sog.ts
|
|
2704
|
+
*/
|
|
2705
|
+
|
|
2706
|
+
class SOGParser extends X3D .X3DParser
|
|
2707
|
+
{
|
|
2708
|
+
constructor (scene)
|
|
2709
|
+
{
|
|
2710
|
+
super (scene);
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
getEncoding ()
|
|
2714
|
+
{
|
|
2715
|
+
return "ARRAY_BUFFER";
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
setInput (buffer)
|
|
2719
|
+
{
|
|
2720
|
+
this .buffer = buffer;
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
isValid ()
|
|
2724
|
+
{
|
|
2725
|
+
// Check magic.
|
|
2726
|
+
|
|
2727
|
+
const dataView = new DataView (this .buffer);
|
|
2728
|
+
|
|
2729
|
+
if (dataView .getUint32 (0, false) !== 0x504B0304)
|
|
2730
|
+
return false;
|
|
2731
|
+
|
|
2732
|
+
// Check minimum requirement for Gaussian Splats.
|
|
2733
|
+
|
|
2734
|
+
const keys = [
|
|
2735
|
+
"means_l.webp",
|
|
2736
|
+
"means_u.webp",
|
|
2737
|
+
"scales.webp",
|
|
2738
|
+
"sh0.webp",
|
|
2739
|
+
"quats.webp",
|
|
2740
|
+
"meta.json",
|
|
2741
|
+
];
|
|
2742
|
+
|
|
2743
|
+
this .files = unzipSync (new Uint8Array (this .buffer));
|
|
2744
|
+
|
|
2745
|
+
if (!keys .every (key => this .files [key]))
|
|
2746
|
+
return false;
|
|
2747
|
+
|
|
2748
|
+
// Check version.
|
|
2749
|
+
|
|
2750
|
+
const meta = this .parseMeta ();
|
|
2751
|
+
|
|
2752
|
+
if (meta .version < 2 || meta .version > 2)
|
|
2753
|
+
return false;
|
|
2754
|
+
|
|
2755
|
+
return true;
|
|
2756
|
+
}
|
|
2757
|
+
|
|
2758
|
+
parseIntoScene (resolve, reject)
|
|
2759
|
+
{
|
|
2760
|
+
this .sog ()
|
|
2761
|
+
.then (resolve)
|
|
2762
|
+
.catch (reject);
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
async sog ()
|
|
2766
|
+
{
|
|
2767
|
+
const
|
|
2768
|
+
browser = this .getBrowser (),
|
|
2769
|
+
scene = this .getScene ();
|
|
2770
|
+
|
|
2771
|
+
scene .setEncoding ("SOG");
|
|
2772
|
+
scene .setProfile (browser .getProfile ("Interchange"));
|
|
2773
|
+
scene .addComponent (browser .getComponent ("X_ITE"));
|
|
2774
|
+
|
|
2775
|
+
await this .getBrowser () .loadComponents (scene);
|
|
2776
|
+
|
|
2777
|
+
const
|
|
2778
|
+
transform = scene .createNode ("Transform"),
|
|
2779
|
+
gaussianSplats = scene .createNode ("GaussianSplats"),
|
|
2780
|
+
gaussianCloud = await this .unpackFiles ();
|
|
2781
|
+
|
|
2782
|
+
gaussianSplats .positions = gaussianCloud .positions;
|
|
2783
|
+
gaussianSplats .orientations = gaussianCloud .rotations;
|
|
2784
|
+
gaussianSplats .scales = gaussianCloud .scales;
|
|
2785
|
+
gaussianSplats .opacities = gaussianCloud .alphas;
|
|
2786
|
+
|
|
2787
|
+
gaussianSplats .sphericalHarmonicsDegree0Coef0 = gaussianCloud .colors;
|
|
2788
|
+
|
|
2789
|
+
// Set spherical harmonics.
|
|
2790
|
+
|
|
2791
|
+
if (gaussianCloud .shs .length)
|
|
2792
|
+
{
|
|
2793
|
+
const {
|
|
2794
|
+
meta: { count, shN: { bands } },
|
|
2795
|
+
} = this .files;
|
|
2796
|
+
|
|
2797
|
+
const
|
|
2798
|
+
shs = gaussianCloud .shs,
|
|
2799
|
+
shDegree = bands;
|
|
2800
|
+
|
|
2801
|
+
this .setSphericalHarmonics (count, shs, shDegree, gaussianSplats)
|
|
2802
|
+
}
|
|
2803
|
+
|
|
2804
|
+
// Add nodes to scene.
|
|
2805
|
+
|
|
2806
|
+
transform .rotation = new X3D .Rotation4 (1, 0, 0, Math .PI);
|
|
2807
|
+
|
|
2808
|
+
transform .children .push (gaussianSplats);
|
|
2809
|
+
scene .rootNodes .push (transform);
|
|
2810
|
+
|
|
2811
|
+
return scene;
|
|
2812
|
+
}
|
|
2813
|
+
|
|
2814
|
+
setSphericalHarmonics (numSplats, shs, shDegree, gaussianSplats)
|
|
2815
|
+
{
|
|
2816
|
+
const
|
|
2817
|
+
shCoeffs = this .dimForDegree (shDegree),
|
|
2818
|
+
shCoeffs3 = this .dimForDegree (shDegree) * 3,
|
|
2819
|
+
splatShs = Array .from ({ length: shCoeffs }, () => [ ]);
|
|
2820
|
+
|
|
2821
|
+
for (let c = 0; c < shCoeffs; ++ c)
|
|
2822
|
+
{
|
|
2823
|
+
const
|
|
2824
|
+
c3 = c * 3,
|
|
2825
|
+
splatSh = splatShs [c];
|
|
2826
|
+
|
|
2827
|
+
for (let i = 0; i < numSplats; ++ i)
|
|
2828
|
+
{
|
|
2829
|
+
const offset = shCoeffs3 * i + c3;
|
|
2830
|
+
|
|
2831
|
+
for (let j = 0; j < 3; ++ j)
|
|
2832
|
+
splatSh .push (shs [offset + j]);
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
// GaussianSplats node only supports up to degree 3.
|
|
2837
|
+
const shDegreeMax = Math .min (shDegree, 3);
|
|
2838
|
+
|
|
2839
|
+
for (let d = 0, i = 0; d < shDegreeMax; ++ d)
|
|
2840
|
+
{
|
|
2841
|
+
const coefs = this .coefsForDegree (d);
|
|
2842
|
+
|
|
2843
|
+
for (let c = 0; c < coefs; ++ c, ++ i)
|
|
2844
|
+
gaussianSplats [`sphericalHarmonicsDegree${d + 1}Coef${c}`] = splatShs [i];
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
|
|
2848
|
+
parseMeta ()
|
|
2849
|
+
{
|
|
2850
|
+
return this .files .meta = JSON .parse (new TextDecoder () .decode (this .files ["meta.json"]));
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
async unpackFiles ()
|
|
2854
|
+
{
|
|
2855
|
+
await this .unpackImages ();
|
|
2856
|
+
|
|
2857
|
+
const
|
|
2858
|
+
positions = this .unpackPositions (),
|
|
2859
|
+
rotations = this .unpackRotations (),
|
|
2860
|
+
scales = this .unpackScales (),
|
|
2861
|
+
[alphas, colors] = this .unpackAlphasAndAColors (),
|
|
2862
|
+
shs = this .unpackSphericalHarmonics ();
|
|
2863
|
+
|
|
2864
|
+
return {
|
|
2865
|
+
positions,
|
|
2866
|
+
rotations,
|
|
2867
|
+
scales,
|
|
2868
|
+
alphas,
|
|
2869
|
+
colors,
|
|
2870
|
+
shs,
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
|
|
2874
|
+
async unpackImages ()
|
|
2875
|
+
{
|
|
2876
|
+
return Promise .all (Object .keys (this .files)
|
|
2877
|
+
.filter (key => key .endsWith (".webp"))
|
|
2878
|
+
.map (key => this .unpackImage (key)));
|
|
2879
|
+
}
|
|
2880
|
+
|
|
2881
|
+
async unpackImage (key)
|
|
2882
|
+
{
|
|
2883
|
+
const
|
|
2884
|
+
bytes = this .files [key],
|
|
2885
|
+
blob = new Blob ([bytes], { type: "image/webp" }),
|
|
2886
|
+
url = URL .createObjectURL (blob),
|
|
2887
|
+
image = await this .loadImage (url),
|
|
2888
|
+
data = this .readPixels (image);
|
|
2889
|
+
|
|
2890
|
+
this .files [key] = image;
|
|
2891
|
+
this .files [key .replace (".webp", "")] = data;
|
|
2892
|
+
|
|
2893
|
+
URL .revokeObjectURL (url);
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
loadImage (url)
|
|
2897
|
+
{
|
|
2898
|
+
return new Promise ((resolve, reject) =>
|
|
2899
|
+
{
|
|
2900
|
+
const image = new Image ();
|
|
2901
|
+
|
|
2902
|
+
image .onload = () => resolve (image);
|
|
2903
|
+
|
|
2904
|
+
image .onerror =
|
|
2905
|
+
image .onabort = event => reject (new Error (`Couldn't load WebP image: ${event .type}.`));
|
|
2906
|
+
|
|
2907
|
+
image .src = url;
|
|
2908
|
+
});
|
|
2909
|
+
}
|
|
2910
|
+
|
|
2911
|
+
readPixels (image)
|
|
2912
|
+
{
|
|
2913
|
+
const
|
|
2914
|
+
gl = this .getBrowser () .getContext (),
|
|
2915
|
+
texture = gl .createTexture (),
|
|
2916
|
+
framebuffer = gl .createFramebuffer (),
|
|
2917
|
+
width = image .width,
|
|
2918
|
+
height = image .height,
|
|
2919
|
+
data = new Uint8Array (width * height * 4);
|
|
2920
|
+
|
|
2921
|
+
// Create texture.
|
|
2922
|
+
|
|
2923
|
+
gl .bindTexture (gl .TEXTURE_2D, texture);
|
|
2924
|
+
gl .pixelStorei (gl .UNPACK_COLORSPACE_CONVERSION_WEBGL, gl .NONE);
|
|
2925
|
+
gl .texImage2D (gl .TEXTURE_2D, 0, gl .RGBA, width, height, 0, gl .RGBA, gl .UNSIGNED_BYTE, image);
|
|
2926
|
+
gl .pixelStorei (gl .UNPACK_COLORSPACE_CONVERSION_WEBGL, gl .BROWSER_DEFAULT_WEBGL);
|
|
2927
|
+
|
|
2928
|
+
// Read pixels from framebuffer.
|
|
2929
|
+
|
|
2930
|
+
gl .bindFramebuffer (gl .FRAMEBUFFER, framebuffer);
|
|
2931
|
+
gl .framebufferTexture2D (gl .FRAMEBUFFER, gl .COLOR_ATTACHMENT0, gl .TEXTURE_2D, texture, 0);
|
|
2932
|
+
gl .readPixels (0, 0, width, height, gl .RGBA, gl .UNSIGNED_BYTE, data);
|
|
2933
|
+
|
|
2934
|
+
// Clean up.
|
|
2935
|
+
|
|
2936
|
+
gl .deleteFramebuffer (framebuffer);
|
|
2937
|
+
gl .deleteTexture (texture);
|
|
2938
|
+
|
|
2939
|
+
return data;
|
|
2940
|
+
}
|
|
2941
|
+
|
|
2942
|
+
unpackPositions ()
|
|
2943
|
+
{
|
|
2944
|
+
const lerp = (a, b, t) => a + t * (b - a);
|
|
2945
|
+
|
|
2946
|
+
const unlog = n => Math .sign (n) * (Math .exp (Math .abs (n)) - 1);
|
|
2947
|
+
|
|
2948
|
+
const {
|
|
2949
|
+
meta: { count, means: { mins, maxs }},
|
|
2950
|
+
means_l,
|
|
2951
|
+
means_u,
|
|
2952
|
+
} = this .files;
|
|
2953
|
+
|
|
2954
|
+
const
|
|
2955
|
+
N = count * 4,
|
|
2956
|
+
array = [ ];
|
|
2957
|
+
|
|
2958
|
+
for (let i = 0; i < N; i += 4)
|
|
2959
|
+
{
|
|
2960
|
+
// 16-bit normalized value per axis (0..65535)
|
|
2961
|
+
|
|
2962
|
+
const
|
|
2963
|
+
qx = (means_u [i] << 8) | means_l [i],
|
|
2964
|
+
qy = (means_u [i + 1] << 8) | means_l [i + 1],
|
|
2965
|
+
qz = (means_u [i + 2] << 8) | means_l [i + 2];
|
|
2966
|
+
|
|
2967
|
+
// Dequantize into *log-domain* nx,ny,nz using per-axis ranges from meta:
|
|
2968
|
+
|
|
2969
|
+
const
|
|
2970
|
+
nx = lerp (mins [0], maxs [0], qx / 65535),
|
|
2971
|
+
ny = lerp (mins [1], maxs [1], qy / 65535),
|
|
2972
|
+
nz = lerp (mins [2], maxs [2], qz / 65535);
|
|
2973
|
+
|
|
2974
|
+
// Undo the symmetric log transform used at encode time:
|
|
2975
|
+
|
|
2976
|
+
array .push (
|
|
2977
|
+
unlog (nx),
|
|
2978
|
+
unlog (ny),
|
|
2979
|
+
unlog (nz),
|
|
2980
|
+
);
|
|
2981
|
+
}
|
|
2982
|
+
|
|
2983
|
+
return array;
|
|
2984
|
+
}
|
|
2985
|
+
|
|
2986
|
+
unpackRotations ()
|
|
2987
|
+
{
|
|
2988
|
+
const toComp = c => (c / 255 - 0.5) * 2 / Math .SQRT2;
|
|
2989
|
+
|
|
2990
|
+
const {
|
|
2991
|
+
meta: { count },
|
|
2992
|
+
quats,
|
|
2993
|
+
} = this .files;
|
|
2994
|
+
|
|
2995
|
+
const
|
|
2996
|
+
N = count * 4,
|
|
2997
|
+
array = [ ];
|
|
2998
|
+
|
|
2999
|
+
for (let i = 0; i < N; i += 4)
|
|
3000
|
+
{
|
|
3001
|
+
const
|
|
3002
|
+
a = toComp (quats [i]),
|
|
3003
|
+
b = toComp (quats [i + 1]),
|
|
3004
|
+
c = toComp (quats [i + 2]);
|
|
3005
|
+
|
|
3006
|
+
const mode = quats [i + 3] - 252; // 0..3 → omitted component is w, x, y or z respectively
|
|
3007
|
+
|
|
3008
|
+
// Reconstruct the omitted component so that ||q|| = 1 and w.l.o.g. the omitted one is non-negative.
|
|
3009
|
+
|
|
3010
|
+
const
|
|
3011
|
+
t = Math .hypot (a, b, c),
|
|
3012
|
+
d = Math .sqrt (Math .max (0, 1 - t));
|
|
3013
|
+
|
|
3014
|
+
// Place components according to mode; q is ordered [x, y, z, w].
|
|
3015
|
+
|
|
3016
|
+
switch (mode)
|
|
3017
|
+
{
|
|
3018
|
+
case 0: array .push (a, b, c, d); break; // omitted = w
|
|
3019
|
+
case 1: array .push (d, b, c, a); break; // omitted = x
|
|
3020
|
+
case 2: array .push (b, d, c, a); break; // omitted = y
|
|
3021
|
+
case 3: array .push (b, c, d, a); break; // omitted = z
|
|
3022
|
+
default: throw new Error ("Invalid quaternion mode");
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
|
|
3026
|
+
return array;
|
|
3027
|
+
}
|
|
3028
|
+
|
|
3029
|
+
unpackScales ()
|
|
3030
|
+
{
|
|
3031
|
+
const {
|
|
3032
|
+
meta: { count, scales: { codebook } },
|
|
3033
|
+
scales,
|
|
3034
|
+
} = this .files;
|
|
3035
|
+
|
|
3036
|
+
const
|
|
3037
|
+
N = count * 4,
|
|
3038
|
+
array = [ ];
|
|
3039
|
+
|
|
3040
|
+
for (let i = 0; i < N; i += 4)
|
|
3041
|
+
{
|
|
3042
|
+
array .push (
|
|
3043
|
+
Math .exp (codebook [scales [i]]), // r,g,b are 0..255
|
|
3044
|
+
Math .exp (codebook [scales [i + 1]]),
|
|
3045
|
+
Math .exp (codebook [scales [i + 2]]),
|
|
3046
|
+
);
|
|
3047
|
+
}
|
|
3048
|
+
|
|
3049
|
+
return array;
|
|
3050
|
+
}
|
|
3051
|
+
|
|
3052
|
+
unpackAlphasAndAColors ()
|
|
3053
|
+
{
|
|
3054
|
+
const {
|
|
3055
|
+
meta: { count, sh0: { codebook } },
|
|
3056
|
+
sh0,
|
|
3057
|
+
} = this .files;
|
|
3058
|
+
|
|
3059
|
+
const
|
|
3060
|
+
N = count * 4,
|
|
3061
|
+
array = [ ],
|
|
3062
|
+
alphas = [ ];
|
|
3063
|
+
|
|
3064
|
+
for (let i = 0; i < N; i += 4)
|
|
3065
|
+
{
|
|
3066
|
+
array .push (
|
|
3067
|
+
codebook [sh0 [i]],
|
|
3068
|
+
codebook [sh0 [i + 1]],
|
|
3069
|
+
codebook [sh0 [i + 2]],
|
|
3070
|
+
);
|
|
3071
|
+
|
|
3072
|
+
alphas .push (sh0 [i + 3] / 255);
|
|
3073
|
+
}
|
|
3074
|
+
|
|
3075
|
+
return [alphas, array];
|
|
3076
|
+
}
|
|
3077
|
+
|
|
3078
|
+
unpackSphericalHarmonics ()
|
|
3079
|
+
{
|
|
3080
|
+
if (!this .files .meta .shN)
|
|
3081
|
+
return [ ];
|
|
3082
|
+
|
|
3083
|
+
const {
|
|
3084
|
+
meta: { count, shN: { bands, codebook } },
|
|
3085
|
+
shN_labels,
|
|
3086
|
+
shN_centroids,
|
|
3087
|
+
"shN_centroids.webp": { width },
|
|
3088
|
+
} = this .files;
|
|
3089
|
+
|
|
3090
|
+
const getIndex = (u, v) => u + v * width;
|
|
3091
|
+
|
|
3092
|
+
const
|
|
3093
|
+
coeffs = [0, 3, 8, 15],
|
|
3094
|
+
shCoeffs = coeffs [bands]; // coefficients per color channel
|
|
3095
|
+
|
|
3096
|
+
if (!shCoeffs)
|
|
3097
|
+
return [ ];
|
|
3098
|
+
|
|
3099
|
+
const
|
|
3100
|
+
N = count * 4,
|
|
3101
|
+
array = [ ];
|
|
3102
|
+
|
|
3103
|
+
for (let i = 0; i < N; i += 4)
|
|
3104
|
+
{
|
|
3105
|
+
// 1. Look up this gaussian's palette entry.
|
|
3106
|
+
|
|
3107
|
+
const label = shN_labels [i] + (shN_labels [i + 1] << 8);
|
|
3108
|
+
|
|
3109
|
+
// 2. For each coefficient, read RGB from the centroids pixel and map through the codebook.
|
|
3110
|
+
|
|
3111
|
+
for (let c = 0; c < shCoeffs; ++ c)
|
|
3112
|
+
{
|
|
3113
|
+
const
|
|
3114
|
+
u = (label % 64) * shCoeffs + c,
|
|
3115
|
+
v = Math .floor (label / 64);
|
|
3116
|
+
|
|
3117
|
+
// pixel (u, v) in shN_centroids.webp
|
|
3118
|
+
const index = getIndex (u, v) * 4;
|
|
3119
|
+
|
|
3120
|
+
const
|
|
3121
|
+
r = shN_centroids [index],
|
|
3122
|
+
g = shN_centroids [index + 1],
|
|
3123
|
+
b = shN_centroids [index + 2];
|
|
3124
|
+
|
|
3125
|
+
array .push (
|
|
3126
|
+
codebook [r], // SH AC coefficient for color channel 0
|
|
3127
|
+
codebook [g], // SH AC coefficient for color channel 1
|
|
3128
|
+
codebook [b], // SH AC coefficient for color channel 2
|
|
3129
|
+
);
|
|
3130
|
+
}
|
|
3131
|
+
}
|
|
3132
|
+
|
|
3133
|
+
return array;
|
|
3134
|
+
}
|
|
3135
|
+
|
|
3136
|
+
dimForDegree (degree)
|
|
3137
|
+
{
|
|
3138
|
+
return (degree + 1) ** 2 - 1;
|
|
3139
|
+
}
|
|
3140
|
+
|
|
3141
|
+
coefsForDegree (degree)
|
|
3142
|
+
{
|
|
3143
|
+
return degree * 2 + 3;
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
X3D .GoldenGate .addParsers (SOGParser);
|
|
3148
|
+
|