single-file-core 1.5.89 → 1.5.91

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