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