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