source: imaps-frontend/node_modules/fflate/esm/index.mjs

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

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