browser.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. // DEFLATE is a complex format; to read this code, you should probably check the RFC first:
  2. // https://tools.ietf.org/html/rfc1951
  3. // You may also wish to take a look at the guide I made about this program:
  4. // https://gist.github.com/101arrowz/253f31eb5abc3d9275ab943003ffecad
  5. // Much of the following code is similar to that of UZIP.js:
  6. // https://github.com/photopea/UZIP.js
  7. // Many optimizations have been made, so the bundle size is ultimately smaller but performance is similar.
  8. // Sometimes 0 will appear where -1 would be more appropriate. This is because using a uint
  9. // is better for memory in most engines (I *think*).
  10. var ch2 = {};
  11. var wk = (function (c, id, msg, transfer, cb) {
  12. var u = ch2[id] || (ch2[id] = URL.createObjectURL(new Blob([c], { type: 'text/javascript' })));
  13. var w = new Worker(u);
  14. w.onerror = function (e) { return cb(e.error, null); };
  15. w.onmessage = function (e) { return cb(null, e.data); };
  16. w.postMessage(msg, transfer);
  17. return w;
  18. });
  19. // aliases for shorter compressed code (most minifers don't do this)
  20. var u8 = Uint8Array, u16 = Uint16Array, u32 = Uint32Array;
  21. var mskr = function (v, o) {
  22. for (var i = 0; i < 32; ++i)
  23. o[i] = (1 << v[i]) - 1;
  24. return o;
  25. };
  26. // fixed length extra bits
  27. 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]), flebmsk = mskr(fleb, new u8(32));
  28. // fixed distance extra bits
  29. // see fleb note
  30. 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]), fdebmsk = mskr(fdeb, new u16(32));
  31. // code length index map
  32. var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  33. // get base, reverse index map from extra bits
  34. var freb = function (eb, start) {
  35. var b = new u16(31);
  36. for (var i = 0; i < 31; ++i) {
  37. b[i] = start += 1 << eb[i - 1];
  38. }
  39. // numbers here are at max 18 bits
  40. var r = new u32(b[30]);
  41. for (var i = 1; i < 30; ++i) {
  42. for (var j = b[i]; j < b[i + 1]; ++j) {
  43. r[j] = ((j - b[i]) << 5) | i;
  44. }
  45. }
  46. return [b, r];
  47. };
  48. var _a = freb(fleb, 2), fl = _a[0], revfl = _a[1];
  49. // we can ignore the fact that the other numbers are wrong; they never happen anyway
  50. fl[28] = 258, revfl[258] = 28;
  51. var _b = freb(fdeb, 0), fd = _b[0], revfd = _b[1];
  52. // map of value to reverse (assuming 16 bits)
  53. var rev = new u16(32768);
  54. for (var i = 0; i < 32768; ++i) {
  55. // reverse table algorithm from UZIP.js
  56. var x = ((i & 0xAAAAAAAA) >>> 1) | ((i & 0x55555555) << 1);
  57. x = ((x & 0xCCCCCCCC) >>> 2) | ((x & 0x33333333) << 2);
  58. x = ((x & 0xF0F0F0F0) >>> 4) | ((x & 0x0F0F0F0F) << 4);
  59. rev[i] = (((x & 0xFF00FF00) >>> 8) | ((x & 0x00FF00FF) << 8)) >>> 1;
  60. }
  61. // create huffman tree from u8 "map": index -> code length for code index
  62. // mb (max bits) must be at most 15
  63. // TODO: optimize/split up?
  64. var hMap = (function (cd, mb, r) {
  65. var s = cd.length;
  66. // index
  67. var i = 0;
  68. // u16 "map": index -> # of codes with bit length = index
  69. var l = new u16(mb);
  70. // length of cd must be 288 (total # of codes)
  71. for (; i < s; ++i)
  72. ++l[cd[i] - 1];
  73. // u16 "map": index -> minimum code for bit length = index
  74. var le = new u16(mb);
  75. for (i = 0; i < mb; ++i) {
  76. le[i] = (le[i - 1] + l[i - 1]) << 1;
  77. }
  78. var co;
  79. if (r) {
  80. // u16 "map": index -> number of actual bits, symbol for code
  81. co = new u16(1 << mb);
  82. // bits to remove for reverser
  83. var rvb = 15 - mb;
  84. for (i = 0; i < s; ++i) {
  85. // ignore 0 lengths
  86. if (cd[i]) {
  87. // num encoding both symbol and bits read
  88. var sv = (i << 4) | cd[i];
  89. // free bits
  90. var r_1 = mb - cd[i];
  91. // start value
  92. var v = le[cd[i] - 1]++ << r_1;
  93. // m is end value
  94. for (var m = v | ((1 << r_1) - 1); v <= m; ++v) {
  95. // every 16 bit value starting with the code yields the same result
  96. co[rev[v] >>> rvb] = sv;
  97. }
  98. }
  99. }
  100. }
  101. else {
  102. co = new u16(s);
  103. for (i = 0; i < s; ++i)
  104. co[i] = rev[le[cd[i] - 1]++] >>> (15 - cd[i]);
  105. }
  106. return co;
  107. });
  108. // fixed length tree
  109. var flt = new u8(288);
  110. for (var i = 0; i < 144; ++i)
  111. flt[i] = 8;
  112. for (var i = 144; i < 256; ++i)
  113. flt[i] = 9;
  114. for (var i = 256; i < 280; ++i)
  115. flt[i] = 7;
  116. for (var i = 280; i < 288; ++i)
  117. flt[i] = 8;
  118. // fixed distance tree
  119. var fdt = new u8(32);
  120. for (var i = 0; i < 32; ++i)
  121. fdt[i] = 5;
  122. // fixed length map
  123. var flm = hMap(flt, 9, 0), flrm = hMap(flt, 9, 1);
  124. // fixed distance map
  125. var fdm = hMap(fdt, 5, 0), fdrm = hMap(fdt, 5, 1);
  126. // find max of array
  127. var max = function (a) {
  128. var m = a[0];
  129. for (var i = 1; i < a.length; ++i) {
  130. if (a[i] > m)
  131. m = a[i];
  132. }
  133. return m;
  134. };
  135. // read d, starting at bit p and mask with m
  136. var bits = function (d, p, m) {
  137. var o = p >>> 3;
  138. return ((d[o] | (d[o + 1] << 8)) >>> (p & 7)) & m;
  139. };
  140. // read d, starting at bit p continuing for at least 16 bits
  141. var bits16 = function (d, p) {
  142. var o = p >>> 3;
  143. return ((d[o] | (d[o + 1] << 8) | (d[o + 2] << 16)) >>> (p & 7));
  144. };
  145. // get end of byte
  146. var shft = function (p) { return (p >>> 3) + (p & 7 && 1); };
  147. // typed array slice - allows garbage collector to free original reference,
  148. // while being more compatible than .slice
  149. var slc = function (v, s, e) {
  150. if (s == null || s < 0)
  151. s = 0;
  152. if (e == null || e > v.length)
  153. e = v.length;
  154. // can't use .constructor in case user-supplied
  155. var n = new (v instanceof u16 ? u16 : v instanceof u32 ? u32 : u8)(e - s);
  156. n.set(v.subarray(s, e));
  157. return n;
  158. };
  159. // expands raw DEFLATE data
  160. var inflt = function (dat, buf, st) {
  161. var noSt = !st || st.i;
  162. if (!st)
  163. st = {};
  164. // source length
  165. var sl = dat.length;
  166. // have to estimate size
  167. var noBuf = !buf || !noSt;
  168. // Assumes roughly 33% compression ratio average
  169. if (!buf)
  170. buf = new u8(sl * 3);
  171. // ensure buffer can fit at least l elements
  172. var cbuf = function (l) {
  173. var bl = buf.length;
  174. // need to increase size to fit
  175. if (l > bl) {
  176. // Double or set to necessary, whichever is greater
  177. var nbuf = new u8(Math.max(bl << 1, l));
  178. nbuf.set(buf);
  179. buf = nbuf;
  180. }
  181. };
  182. // last chunk bitpos bytes
  183. 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;
  184. if (final && !lm)
  185. return buf;
  186. // total bits
  187. var tbts = sl << 3;
  188. do {
  189. if (!lm) {
  190. // BFINAL - this is only 1 when last chunk is next
  191. st.f = final = bits(dat, pos, 1);
  192. // type: 0 = no compression, 1 = fixed huffman, 2 = dynamic huffman
  193. var type = bits(dat, pos + 1, 3);
  194. pos += 3;
  195. if (!type) {
  196. // go to end of byte boundary
  197. var s = shft(pos) + 4, l = dat[s - 4] | (dat[s - 3] << 8), t = s + l;
  198. if (t > sl) {
  199. if (noSt)
  200. throw 'unexpected EOF';
  201. break;
  202. }
  203. // ensure size
  204. if (noBuf)
  205. cbuf(bt + l);
  206. // Copy over uncompressed data
  207. buf.set(dat.subarray(s, t), bt);
  208. // Get new bitpos, update byte count
  209. st.b = bt += l, st.p = pos = t << 3;
  210. continue;
  211. }
  212. else if (type == 1)
  213. lm = flrm, dm = fdrm, lbt = 9, dbt = 5;
  214. else if (type == 2) {
  215. // literal dist lengths
  216. var hLit = bits(dat, pos, 31) + 257, hDist = bits(dat, pos + 5, 31) + 1, hcLen = bits(dat, pos + 10, 15) + 4;
  217. var tl = hLit + hDist;
  218. pos += 14;
  219. // length+distance tree
  220. var ldt = new u8(tl);
  221. // code length tree
  222. var clt = new u8(19);
  223. for (var i = 0; i < hcLen; ++i) {
  224. // use index map to get real code
  225. clt[clim[i]] = bits(dat, pos + i * 3, 7);
  226. }
  227. pos += hcLen * 3;
  228. // code lengths bits
  229. var clb = max(clt), clbmsk = (1 << clb) - 1;
  230. if (!noSt && pos + tl * (clb + 7) > tbts)
  231. break;
  232. // code lengths map
  233. var clm = hMap(clt, clb, 1);
  234. for (var i = 0; i < tl;) {
  235. var r = clm[bits(dat, pos, clbmsk)];
  236. // bits read
  237. pos += r & 15;
  238. // symbol
  239. var s = r >>> 4;
  240. // code length to copy
  241. if (s < 16) {
  242. ldt[i++] = s;
  243. }
  244. else {
  245. // copy count
  246. var c = 0, n = 0;
  247. if (s == 16)
  248. n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1];
  249. else if (s == 17)
  250. n = 3 + bits(dat, pos, 7), pos += 3;
  251. else if (s == 18)
  252. n = 11 + bits(dat, pos, 127), pos += 7;
  253. while (n--)
  254. ldt[i++] = c;
  255. }
  256. }
  257. // length tree distance tree
  258. var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
  259. // max length bits
  260. lbt = max(lt);
  261. // max dist bits
  262. dbt = max(dt);
  263. lm = hMap(lt, lbt, 1);
  264. dm = hMap(dt, dbt, 1);
  265. }
  266. else
  267. throw 'invalid block type';
  268. if (pos > tbts)
  269. throw 'unexpected EOF';
  270. }
  271. // Make sure the buffer can hold this + the largest possible addition
  272. // maximum chunk size (practically, theoretically infinite) is 2^17;
  273. if (noBuf)
  274. cbuf(bt + 131072);
  275. var lms = (1 << lbt) - 1, dms = (1 << dbt) - 1;
  276. var mxa = lbt + dbt + 18;
  277. while (noSt || pos + mxa < tbts) {
  278. // bits read, code
  279. var c = lm[bits16(dat, pos) & lms], sym = c >>> 4;
  280. pos += c & 15;
  281. if (pos > tbts)
  282. throw 'unexpected EOF';
  283. if (!c)
  284. throw 'invalid length/literal';
  285. if (sym < 256)
  286. buf[bt++] = sym;
  287. else if (sym == 256) {
  288. lm = null;
  289. break;
  290. }
  291. else {
  292. var add = sym - 254;
  293. // no extra bits needed if less
  294. if (sym > 264) {
  295. // index
  296. var i = sym - 257;
  297. add = bits(dat, pos, flebmsk[i]) + fl[i];
  298. pos += fleb[i];
  299. }
  300. // dist
  301. var d = dm[bits16(dat, pos) & dms], dsym = d >>> 4;
  302. if (!d)
  303. throw 'invalid distance';
  304. pos += d & 15;
  305. var dt = fd[dsym];
  306. if (dsym > 3)
  307. dt += bits16(dat, pos) & fdebmsk[dsym], pos += fdeb[dsym];
  308. if (pos > tbts)
  309. throw 'unexpected EOF';
  310. if (noBuf)
  311. cbuf(bt + 131072);
  312. var end = bt + add;
  313. for (; bt < end; bt += 4) {
  314. buf[bt] = buf[bt - dt];
  315. buf[bt + 1] = buf[bt + 1 - dt];
  316. buf[bt + 2] = buf[bt + 2 - dt];
  317. buf[bt + 3] = buf[bt + 3 - dt];
  318. }
  319. bt = end;
  320. }
  321. }
  322. st.l = lm, st.p = pos, st.b = bt;
  323. if (lm)
  324. final = 1, st.m = lbt, st.d = dm, st.n = dbt;
  325. } while (!final);
  326. return bt == buf.length ? buf : slc(buf, 0, bt);
  327. };
  328. // starting at p, write the minimum number of bits that can hold v to d
  329. var wbits = function (d, p, v) {
  330. v <<= p & 7;
  331. var o = p >>> 3;
  332. d[o] |= v;
  333. d[o + 1] |= v >>> 8;
  334. };
  335. // starting at p, write the minimum number of bits (>8) that can hold v to d
  336. var wbits16 = function (d, p, v) {
  337. v <<= p & 7;
  338. var o = p >>> 3;
  339. d[o] |= v;
  340. d[o + 1] |= v >>> 8;
  341. d[o + 2] |= v >>> 16;
  342. };
  343. // creates code lengths from a frequency table
  344. var hTree = function (d, mb) {
  345. // Need extra info to make a tree
  346. var t = [];
  347. for (var i = 0; i < d.length; ++i) {
  348. if (d[i])
  349. t.push({ s: i, f: d[i] });
  350. }
  351. var s = t.length;
  352. var t2 = t.slice();
  353. if (!s)
  354. return [new u8(0), 0];
  355. if (s == 1) {
  356. var v = new u8(t[0].s + 1);
  357. v[t[0].s] = 1;
  358. return [v, 1];
  359. }
  360. t.sort(function (a, b) { return a.f - b.f; });
  361. // after i2 reaches last ind, will be stopped
  362. // freq must be greater than largest possible number of symbols
  363. t.push({ s: -1, f: 25001 });
  364. var l = t[0], r = t[1], i0 = 0, i1 = 1, i2 = 2;
  365. t[0] = { s: -1, f: l.f + r.f, l: l, r: r };
  366. // efficient algorithm from UZIP.js
  367. // i0 is lookbehind, i2 is lookahead - after processing two low-freq
  368. // symbols that combined have high freq, will start processing i2 (high-freq,
  369. // non-composite) symbols instead
  370. // see https://reddit.com/r/photopea/comments/ikekht/uzipjs_questions/
  371. while (i1 != s - 1) {
  372. l = t[t[i0].f < t[i2].f ? i0++ : i2++];
  373. r = t[i0 != i1 && t[i0].f < t[i2].f ? i0++ : i2++];
  374. t[i1++] = { s: -1, f: l.f + r.f, l: l, r: r };
  375. }
  376. var maxSym = t2[0].s;
  377. for (var i = 1; i < s; ++i) {
  378. if (t2[i].s > maxSym)
  379. maxSym = t2[i].s;
  380. }
  381. // code lengths
  382. var tr = new u16(maxSym + 1);
  383. // max bits in tree
  384. var mbt = ln(t[i1 - 1], tr, 0);
  385. if (mbt > mb) {
  386. // more algorithms from UZIP.js
  387. // TODO: find out how this code works (debt)
  388. // ind debt
  389. var i = 0, dt = 0;
  390. // left cost
  391. var lft = mbt - mb, cst = 1 << lft;
  392. t2.sort(function (a, b) { return tr[b.s] - tr[a.s] || a.f - b.f; });
  393. for (; i < s; ++i) {
  394. var i2_1 = t2[i].s;
  395. if (tr[i2_1] > mb) {
  396. dt += cst - (1 << (mbt - tr[i2_1]));
  397. tr[i2_1] = mb;
  398. }
  399. else
  400. break;
  401. }
  402. dt >>>= lft;
  403. while (dt > 0) {
  404. var i2_2 = t2[i].s;
  405. if (tr[i2_2] < mb)
  406. dt -= 1 << (mb - tr[i2_2]++ - 1);
  407. else
  408. ++i;
  409. }
  410. for (; i >= 0 && dt; --i) {
  411. var i2_3 = t2[i].s;
  412. if (tr[i2_3] == mb) {
  413. --tr[i2_3];
  414. ++dt;
  415. }
  416. }
  417. mbt = mb;
  418. }
  419. return [new u8(tr), mbt];
  420. };
  421. // get the max length and assign length codes
  422. var ln = function (n, l, d) {
  423. return n.s == -1
  424. ? Math.max(ln(n.l, l, d + 1), ln(n.r, l, d + 1))
  425. : (l[n.s] = d);
  426. };
  427. // length codes generation
  428. var lc = function (c) {
  429. var s = c.length;
  430. // Note that the semicolon was intentional
  431. while (s && !c[--s])
  432. ;
  433. var cl = new u16(++s);
  434. // ind num streak
  435. var cli = 0, cln = c[0], cls = 1;
  436. var w = function (v) { cl[cli++] = v; };
  437. for (var i = 1; i <= s; ++i) {
  438. if (c[i] == cln && i != s)
  439. ++cls;
  440. else {
  441. if (!cln && cls > 2) {
  442. for (; cls > 138; cls -= 138)
  443. w(32754);
  444. if (cls > 2) {
  445. w(cls > 10 ? ((cls - 11) << 5) | 28690 : ((cls - 3) << 5) | 12305);
  446. cls = 0;
  447. }
  448. }
  449. else if (cls > 3) {
  450. w(cln), --cls;
  451. for (; cls > 6; cls -= 6)
  452. w(8304);
  453. if (cls > 2)
  454. w(((cls - 3) << 5) | 8208), cls = 0;
  455. }
  456. while (cls--)
  457. w(cln);
  458. cls = 1;
  459. cln = c[i];
  460. }
  461. }
  462. return [cl.subarray(0, cli), s];
  463. };
  464. // calculate the length of output from tree, code lengths
  465. var clen = function (cf, cl) {
  466. var l = 0;
  467. for (var i = 0; i < cl.length; ++i)
  468. l += cf[i] * cl[i];
  469. return l;
  470. };
  471. // writes a fixed block
  472. // returns the new bit pos
  473. var wfblk = function (out, pos, dat) {
  474. // no need to write 00 as type: TypedArray defaults to 0
  475. var s = dat.length;
  476. var o = shft(pos + 2);
  477. out[o] = s & 255;
  478. out[o + 1] = s >>> 8;
  479. out[o + 2] = out[o] ^ 255;
  480. out[o + 3] = out[o + 1] ^ 255;
  481. for (var i = 0; i < s; ++i)
  482. out[o + i + 4] = dat[i];
  483. return (o + 4 + s) << 3;
  484. };
  485. // writes a block
  486. var wblk = function (dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
  487. wbits(out, p++, final);
  488. ++lf[256];
  489. var _a = hTree(lf, 15), dlt = _a[0], mlb = _a[1];
  490. var _b = hTree(df, 15), ddt = _b[0], mdb = _b[1];
  491. var _c = lc(dlt), lclt = _c[0], nlc = _c[1];
  492. var _d = lc(ddt), lcdt = _d[0], ndc = _d[1];
  493. var lcfreq = new u16(19);
  494. for (var i = 0; i < lclt.length; ++i)
  495. lcfreq[lclt[i] & 31]++;
  496. for (var i = 0; i < lcdt.length; ++i)
  497. lcfreq[lcdt[i] & 31]++;
  498. var _e = hTree(lcfreq, 7), lct = _e[0], mlcb = _e[1];
  499. var nlcc = 19;
  500. for (; nlcc > 4 && !lct[clim[nlcc - 1]]; --nlcc)
  501. ;
  502. var flen = (bl + 5) << 3;
  503. var ftlen = clen(lf, flt) + clen(df, fdt) + eb;
  504. var dtlen = clen(lf, dlt) + clen(df, ddt) + eb + 14 + 3 * nlcc + clen(lcfreq, lct) + (2 * lcfreq[16] + 3 * lcfreq[17] + 7 * lcfreq[18]);
  505. if (flen <= ftlen && flen <= dtlen)
  506. return wfblk(out, p, dat.subarray(bs, bs + bl));
  507. var lm, ll, dm, dl;
  508. wbits(out, p, 1 + (dtlen < ftlen)), p += 2;
  509. if (dtlen < ftlen) {
  510. lm = hMap(dlt, mlb, 0), ll = dlt, dm = hMap(ddt, mdb, 0), dl = ddt;
  511. var llm = hMap(lct, mlcb, 0);
  512. wbits(out, p, nlc - 257);
  513. wbits(out, p + 5, ndc - 1);
  514. wbits(out, p + 10, nlcc - 4);
  515. p += 14;
  516. for (var i = 0; i < nlcc; ++i)
  517. wbits(out, p + 3 * i, lct[clim[i]]);
  518. p += 3 * nlcc;
  519. var lcts = [lclt, lcdt];
  520. for (var it = 0; it < 2; ++it) {
  521. var clct = lcts[it];
  522. for (var i = 0; i < clct.length; ++i) {
  523. var len = clct[i] & 31;
  524. wbits(out, p, llm[len]), p += lct[len];
  525. if (len > 15)
  526. wbits(out, p, (clct[i] >>> 5) & 127), p += clct[i] >>> 12;
  527. }
  528. }
  529. }
  530. else {
  531. lm = flm, ll = flt, dm = fdm, dl = fdt;
  532. }
  533. for (var i = 0; i < li; ++i) {
  534. if (syms[i] > 255) {
  535. var len = (syms[i] >>> 18) & 31;
  536. wbits16(out, p, lm[len + 257]), p += ll[len + 257];
  537. if (len > 7)
  538. wbits(out, p, (syms[i] >>> 23) & 31), p += fleb[len];
  539. var dst = syms[i] & 31;
  540. wbits16(out, p, dm[dst]), p += dl[dst];
  541. if (dst > 3)
  542. wbits16(out, p, (syms[i] >>> 5) & 8191), p += fdeb[dst];
  543. }
  544. else {
  545. wbits16(out, p, lm[syms[i]]), p += ll[syms[i]];
  546. }
  547. }
  548. wbits16(out, p, lm[256]);
  549. return p + ll[256];
  550. };
  551. // deflate options (nice << 13) | chain
  552. var deo = new u32([65540, 131080, 131088, 131104, 262176, 1048704, 1048832, 2114560, 2117632]);
  553. // empty
  554. var et = new u8(0);
  555. // compresses data into a raw DEFLATE buffer
  556. var dflt = function (dat, lvl, plvl, pre, post, lst) {
  557. var s = dat.length;
  558. var o = new u8(pre + s + 5 * (1 + Math.floor(s / 7000)) + post);
  559. // writing to this writes to the output buffer
  560. var w = o.subarray(pre, o.length - post);
  561. var pos = 0;
  562. if (!lvl || s < 8) {
  563. for (var i = 0; i <= s; i += 65535) {
  564. // end
  565. var e = i + 65535;
  566. if (e < s) {
  567. // write full block
  568. pos = wfblk(w, pos, dat.subarray(i, e));
  569. }
  570. else {
  571. // write final block
  572. w[i] = lst;
  573. pos = wfblk(w, pos, dat.subarray(i, s));
  574. }
  575. }
  576. }
  577. else {
  578. var opt = deo[lvl - 1];
  579. var n = opt >>> 13, c = opt & 8191;
  580. var msk_1 = (1 << plvl) - 1;
  581. // prev 2-byte val map curr 2-byte val map
  582. var prev = new u16(32768), head = new u16(msk_1 + 1);
  583. var bs1_1 = Math.ceil(plvl / 3), bs2_1 = 2 * bs1_1;
  584. var hsh = function (i) { return (dat[i] ^ (dat[i + 1] << bs1_1) ^ (dat[i + 2] << bs2_1)) & msk_1; };
  585. // 24576 is an arbitrary number of maximum symbols per block
  586. // 424 buffer for last block
  587. var syms = new u32(25000);
  588. // length/literal freq distance freq
  589. var lf = new u16(288), df = new u16(32);
  590. // l/lcnt exbits index l/lind waitdx bitpos
  591. var lc_1 = 0, eb = 0, i = 0, li = 0, wi = 0, bs = 0;
  592. for (; i < s; ++i) {
  593. // hash value
  594. var hv = hsh(i);
  595. // index mod 32768
  596. var imod = i & 32767;
  597. // previous index with this value
  598. var pimod = head[hv];
  599. prev[imod] = pimod;
  600. head[hv] = imod;
  601. // We always should modify head and prev, but only add symbols if
  602. // this data is not yet processed ("wait" for wait index)
  603. if (wi <= i) {
  604. // bytes remaining
  605. var rem = s - i;
  606. if ((lc_1 > 7000 || li > 24576) && rem > 423) {
  607. pos = wblk(dat, w, 0, syms, lf, df, eb, li, bs, i - bs, pos);
  608. li = lc_1 = eb = 0, bs = i;
  609. for (var j = 0; j < 286; ++j)
  610. lf[j] = 0;
  611. for (var j = 0; j < 30; ++j)
  612. df[j] = 0;
  613. }
  614. // len dist chain
  615. var l = 2, d = 0, ch_1 = c, dif = (imod - pimod) & 32767;
  616. if (rem > 2 && hv == hsh(i - dif)) {
  617. var maxn = Math.min(n, rem) - 1;
  618. var maxd = Math.min(32767, i);
  619. // max possible length
  620. // not capped at dif because decompressors implement "rolling" index population
  621. var ml = Math.min(258, rem);
  622. while (dif <= maxd && --ch_1 && imod != pimod) {
  623. if (dat[i + l] == dat[i + l - dif]) {
  624. var nl = 0;
  625. for (; nl < ml && dat[i + nl] == dat[i + nl - dif]; ++nl)
  626. ;
  627. if (nl > l) {
  628. l = nl, d = dif;
  629. // break out early when we reach "nice" (we are satisfied enough)
  630. if (nl > maxn)
  631. break;
  632. // now, find the rarest 2-byte sequence within this
  633. // length of literals and search for that instead.
  634. // Much faster than just using the start
  635. var mmd = Math.min(dif, nl - 2);
  636. var md = 0;
  637. for (var j = 0; j < mmd; ++j) {
  638. var ti = (i - dif + j + 32768) & 32767;
  639. var pti = prev[ti];
  640. var cd = (ti - pti + 32768) & 32767;
  641. if (cd > md)
  642. md = cd, pimod = ti;
  643. }
  644. }
  645. }
  646. // check the previous match
  647. imod = pimod, pimod = prev[imod];
  648. dif += (imod - pimod + 32768) & 32767;
  649. }
  650. }
  651. // d will be nonzero only when a match was found
  652. if (d) {
  653. // store both dist and len data in one Uint32
  654. // Make sure this is recognized as a len/dist with 28th bit (2^28)
  655. syms[li++] = 268435456 | (revfl[l] << 18) | revfd[d];
  656. var lin = revfl[l] & 31, din = revfd[d] & 31;
  657. eb += fleb[lin] + fdeb[din];
  658. ++lf[257 + lin];
  659. ++df[din];
  660. wi = i + l;
  661. ++lc_1;
  662. }
  663. else {
  664. syms[li++] = dat[i];
  665. ++lf[dat[i]];
  666. }
  667. }
  668. }
  669. pos = wblk(dat, w, lst, syms, lf, df, eb, li, bs, i - bs, pos);
  670. // this is the easiest way to avoid needing to maintain state
  671. if (!lst)
  672. pos = wfblk(w, pos, et);
  673. }
  674. return slc(o, 0, pre + shft(pos) + post);
  675. };
  676. // CRC32 table
  677. var crct = new u32(256);
  678. for (var i = 0; i < 256; ++i) {
  679. var c = i, k = 9;
  680. while (--k)
  681. c = ((c & 1) && 0xEDB88320) ^ (c >>> 1);
  682. crct[i] = c;
  683. }
  684. // CRC32
  685. var crc = function () {
  686. var c = 0xFFFFFFFF;
  687. return {
  688. p: function (d) {
  689. // closures have awful performance
  690. var cr = c;
  691. for (var i = 0; i < d.length; ++i)
  692. cr = crct[(cr & 255) ^ d[i]] ^ (cr >>> 8);
  693. c = cr;
  694. },
  695. d: function () { return c ^ 0xFFFFFFFF; }
  696. };
  697. };
  698. // Alder32
  699. var adler = function () {
  700. var a = 1, b = 0;
  701. return {
  702. p: function (d) {
  703. // closures have awful performance
  704. var n = a, m = b;
  705. var l = d.length;
  706. for (var i = 0; i != l;) {
  707. var e = Math.min(i + 5552, l);
  708. for (; i < e; ++i)
  709. n += d[i], m += n;
  710. n %= 65521, m %= 65521;
  711. }
  712. a = n, b = m;
  713. },
  714. d: function () { return (a & 255) << 24 | (a >>> 8) << 16 | (b & 255) << 8 | (b >>> 8); }
  715. };
  716. };
  717. ;
  718. // deflate with opts
  719. var dopt = function (dat, opt, pre, post, st) {
  720. return dflt(dat, opt.level == null ? 6 : opt.level, opt.mem == null ? Math.ceil(Math.max(8, Math.min(13, Math.log(dat.length))) * 1.5) : (12 + opt.mem), pre, post, !st);
  721. };
  722. // Walmart object spread
  723. var mrg = function (a, b) {
  724. var o = {};
  725. for (var k in a)
  726. o[k] = a[k];
  727. for (var k in b)
  728. o[k] = b[k];
  729. return o;
  730. };
  731. // worker clone
  732. // This is possibly the craziest part of the entire codebase, despite how simple it may seem.
  733. // The only parameter to this function is a closure that returns an array of variables outside of the function scope.
  734. // We're going to try to figure out the variable names used in the closure as strings because that is crucial for workerization.
  735. // We will return an object mapping of true variable name to value (basically, the current scope as a JS object).
  736. // The reason we can't just use the original variable names is minifiers mangling the toplevel scope.
  737. // This took me three weeks to figure out how to do.
  738. var wcln = function (fn, fnStr, td) {
  739. var dt = fn();
  740. var st = fn.toString();
  741. var ks = st.slice(st.indexOf('[') + 1, st.lastIndexOf(']')).replace(/ /g, '').split(',');
  742. for (var i = 0; i < dt.length; ++i) {
  743. var v = dt[i], k = ks[i];
  744. if (typeof v == 'function') {
  745. fnStr += ';' + k + '=';
  746. var st_1 = v.toString();
  747. if (v.prototype) {
  748. // for global objects
  749. if (st_1.indexOf('[native code]') != -1)
  750. fnStr += st_1.slice(9, st_1.indexOf('(', 11));
  751. else {
  752. fnStr += st_1;
  753. for (var t in v.prototype)
  754. fnStr += ';' + k + '.prototype.' + t + '=' + v.prototype[t].toString();
  755. }
  756. }
  757. else
  758. fnStr += st_1;
  759. }
  760. else
  761. td[k] = v;
  762. }
  763. return [fnStr, td];
  764. };
  765. // worker onmessage
  766. var wom = function (ev) {
  767. for (var k in ev.data[0])
  768. self[k] = ev.data[0][k];
  769. onmessage = new Function('return ' + ev.data[1])();
  770. };
  771. var ch = [];
  772. // clone bufs
  773. var cbfs = function (v) {
  774. var tl = [];
  775. for (var k in v) {
  776. if (v[k] instanceof u8 || v[k] instanceof u16 || v[k] instanceof u32)
  777. tl.push((v[k] = new v[k].constructor(v[k])).buffer);
  778. }
  779. return tl;
  780. };
  781. // use a worker to execute code
  782. var wrkr = function (fns, init, id, cb) {
  783. var _a;
  784. if (!ch[id]) {
  785. var fnStr = '', td_1 = {}, m = fns.length - 1;
  786. for (var i = 0; i < m; ++i)
  787. _a = wcln(fns[i], fnStr, td_1), fnStr = _a[0], td_1 = _a[1];
  788. ch[id] = wcln(fns[m], fnStr, td_1);
  789. }
  790. var td = mrg({}, ch[id][1]);
  791. return wk(ch[id][0] + ';onmessage=' + wom.toString(), id, [td, init.toString()], cbfs(td), cb);
  792. };
  793. // base async inflate fn
  794. var bInflt = function () { return [u8, u16, fleb, flebmsk, fdeb, fdebmsk, clim, fl, fd, flrm, fdrm, rev, hMap, max, bits, bits16, shft, slc, inflt, inflateSync, pbf, gu8]; };
  795. var bDflt = function () { return [u8, u16, u32, 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]; };
  796. // gzip extra
  797. var gze = function () { return [gzh, gzhl, wbytes, crc, crct]; };
  798. // gunzip extra
  799. var guze = function () { return [gzs, gzl]; };
  800. // zlib extra
  801. var zle = function () { return [zlh, wbytes, adler]; };
  802. // unzlib extra
  803. var zule = function () { return [zlv]; };
  804. // post buf
  805. var pbf = function (msg) { return postMessage(msg, [msg.buffer]); };
  806. // get u8
  807. var gu8 = function (o) { return o && o.size && new u8(o.size); };
  808. // async helper
  809. var cbify = function (dat, opts, fns, init, id, cb) {
  810. var w = wrkr(fns, init, id, function (err, dat) {
  811. w.terminate();
  812. cb(err, dat);
  813. });
  814. if (!opts.consume)
  815. dat = new u8(dat);
  816. w.postMessage([dat, opts], [dat.buffer]);
  817. return function () { w.terminate(); };
  818. };
  819. // auto stream
  820. var astrm = function (strm) {
  821. strm.ondata = function (dat, final) { return postMessage([dat, final], [dat.buffer]); };
  822. return function (ev) { return strm.push(ev.data[0], ev.data[1]); };
  823. };
  824. // async stream attach
  825. var astrmify = function (fns, strm, opts, init, id) {
  826. var t;
  827. var w = wrkr(fns, init, id, function (err, dat) {
  828. if (err)
  829. w.terminate(), strm.ondata.call(strm, err);
  830. else {
  831. if (dat[1])
  832. w.terminate();
  833. strm.ondata.call(strm, err, dat[0], dat[1]);
  834. }
  835. });
  836. w.postMessage(opts);
  837. strm.push = function (d, f) {
  838. if (t)
  839. throw 'stream finished';
  840. if (!strm.ondata)
  841. throw 'no stream handler';
  842. w.postMessage([d, t = f], [d.buffer]);
  843. };
  844. strm.terminate = function () { w.terminate(); };
  845. };
  846. // read 2 bytes
  847. var b2 = function (d, b) { return d[b] | (d[b + 1] << 8); };
  848. // read 4 bytes
  849. var b4 = function (d, b) { return d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24); };
  850. // write bytes
  851. var wbytes = function (d, b, v) {
  852. for (; v; ++b)
  853. d[b] = v, v >>>= 8;
  854. };
  855. // gzip header
  856. var gzh = function (c, o) {
  857. var fn = o.filename;
  858. 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
  859. if (o.mtime != 0)
  860. wbytes(c, 4, Math.floor(new Date(o.mtime || Date.now()) / 1000));
  861. if (fn) {
  862. c[3] = 8;
  863. for (var i = 0; i <= fn.length; ++i)
  864. c[i + 10] = fn.charCodeAt(i);
  865. }
  866. };
  867. // gzip footer: -8 to -4 = CRC, -4 to -0 is length
  868. // gzip start
  869. var gzs = function (d) {
  870. if (d[0] != 31 || d[1] != 139 || d[2] != 8)
  871. throw 'invalid gzip data';
  872. var flg = d[3];
  873. var st = 10;
  874. if (flg & 4)
  875. st += d[10] | (d[11] << 8) + 2;
  876. for (var zs = (flg >> 3 & 1) + (flg >> 4 & 1); zs > 0; zs -= !d[st++])
  877. ;
  878. return st + (flg & 2);
  879. };
  880. // gzip length
  881. var gzl = function (d) {
  882. var l = d.length;
  883. return (d[l - 4] | d[l - 3] << 8 | d[l - 2] << 16 | d[l - 1] << 24);
  884. };
  885. // gzip header length
  886. var gzhl = function (o) { return 10 + ((o.filename && (o.filename.length + 1)) || 0); };
  887. // zlib header
  888. var zlh = function (c, o) {
  889. var lv = o.level, fl = lv == 0 ? 0 : lv < 6 ? 1 : lv == 9 ? 3 : 2;
  890. c[0] = 120, c[1] = (fl << 6) | (fl ? (32 - 2 * fl) : 1);
  891. };
  892. // zlib valid
  893. var zlv = function (d) {
  894. if ((d[0] & 15) != 8 || (d[0] >>> 4) > 7 || ((d[0] << 8 | d[1]) % 31))
  895. throw 'invalid zlib data';
  896. if (d[1] & 32)
  897. throw 'invalid zlib data: preset dictionaries not supported';
  898. };
  899. function AsyncCmpStrm(opts, cb) {
  900. if (!cb && typeof opts == 'function')
  901. cb = opts, opts = {};
  902. this.ondata = cb;
  903. return opts;
  904. }
  905. // zlib footer: -4 to -0 is Adler32
  906. /**
  907. * Streaming DEFLATE compression
  908. */
  909. var Deflate = /*#__PURE__*/ (function () {
  910. function Deflate(opts, cb) {
  911. if (!cb && typeof opts == 'function')
  912. cb = opts, opts = {};
  913. this.ondata = cb;
  914. this.o = opts || {};
  915. }
  916. Deflate.prototype.p = function (c, f) {
  917. this.ondata(dopt(c, this.o, 0, 0, !f), f);
  918. };
  919. /**
  920. * Pushes a chunk to be deflated
  921. * @param chunk The chunk to push
  922. * @param final Whether this is the last chunk
  923. */
  924. Deflate.prototype.push = function (chunk, final) {
  925. if (this.d)
  926. throw 'stream finished';
  927. if (!this.ondata)
  928. throw 'no stream handler';
  929. this.d = final;
  930. this.p(chunk, final || false);
  931. };
  932. return Deflate;
  933. }());
  934. export { Deflate };
  935. /**
  936. * Asynchronous streaming DEFLATE compression
  937. */
  938. var AsyncDeflate = /*#__PURE__*/ (function () {
  939. function AsyncDeflate(opts, cb) {
  940. astrmify([
  941. bDflt,
  942. function () { return [astrm, Deflate]; }
  943. ], this, AsyncCmpStrm.call(this, opts, cb), function (ev) {
  944. var strm = new Deflate(ev.data);
  945. onmessage = astrm(strm);
  946. }, 6);
  947. }
  948. return AsyncDeflate;
  949. }());
  950. export { AsyncDeflate };
  951. export function deflate(data, opts, cb) {
  952. if (!cb)
  953. cb = opts, opts = {};
  954. if (typeof cb != 'function')
  955. throw 'no callback';
  956. return cbify(data, opts, [
  957. bDflt,
  958. ], function (ev) { return pbf(deflateSync(ev.data[0], ev.data[1])); }, 0, cb);
  959. }
  960. /**
  961. * Compresses data with DEFLATE without any wrapper
  962. * @param data The data to compress
  963. * @param opts The compression options
  964. * @returns The deflated version of the data
  965. */
  966. export function deflateSync(data, opts) {
  967. if (opts === void 0) { opts = {}; }
  968. return dopt(data, opts, 0, 0);
  969. }
  970. /**
  971. * Streaming DEFLATE decompression
  972. */
  973. var Inflate = /*#__PURE__*/ (function () {
  974. /**
  975. * Creates an inflation stream
  976. * @param cb The callback to call whenever data is inflated
  977. */
  978. function Inflate(cb) {
  979. this.s = {};
  980. this.p = new u8(0);
  981. this.ondata = cb;
  982. }
  983. Inflate.prototype.e = function (c) {
  984. if (this.d)
  985. throw 'stream finished';
  986. if (!this.ondata)
  987. throw 'no stream handler';
  988. var l = this.p.length;
  989. var n = new u8(l + c.length);
  990. n.set(this.p), n.set(c, l), this.p = n;
  991. };
  992. Inflate.prototype.c = function (c, final) {
  993. this.d = this.s.i = final;
  994. var bts = this.s.b;
  995. var dt = inflt(this.p, this.o, this.s);
  996. this.ondata(slc(dt, bts, this.s.b), final || false);
  997. this.o = slc(dt, this.s.b - 32768), this.s.b = 32768;
  998. this.p = slc(this.p, this.s.p >>> 3), this.s.p &= 7;
  999. };
  1000. /**
  1001. * Pushes a chunk to be inflated
  1002. * @param chunk The chunk to push
  1003. * @param final Whether this is the final chunk
  1004. */
  1005. Inflate.prototype.push = function (chunk, final) {
  1006. this.e(chunk), this.c(chunk, final);
  1007. };
  1008. return Inflate;
  1009. }());
  1010. export { Inflate };
  1011. /**
  1012. * Asynchronous streaming DEFLATE decompression
  1013. */
  1014. var AsyncInflate = /*#__PURE__*/ (function () {
  1015. /**
  1016. * Creates an asynchronous inflation stream
  1017. * @param cb The callback to call whenever data is deflated
  1018. */
  1019. function AsyncInflate(cb) {
  1020. this.ondata = cb;
  1021. astrmify([
  1022. bInflt,
  1023. function () { return [astrm, Inflate]; }
  1024. ], this, 0, function () {
  1025. var strm = new Inflate();
  1026. onmessage = astrm(strm);
  1027. }, 7);
  1028. }
  1029. return AsyncInflate;
  1030. }());
  1031. export { AsyncInflate };
  1032. export function inflate(data, opts, cb) {
  1033. if (!cb)
  1034. cb = opts, opts = {};
  1035. if (typeof cb != 'function')
  1036. throw 'no callback';
  1037. return cbify(data, opts, [
  1038. bInflt
  1039. ], function (ev) { return pbf(inflateSync(ev.data[0], gu8(ev.data[1]))); }, 1, cb);
  1040. }
  1041. /**
  1042. * Expands DEFLATE data with no wrapper
  1043. * @param data The data to decompress
  1044. * @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
  1045. * @returns The decompressed version of the data
  1046. */
  1047. export function inflateSync(data, out) {
  1048. return inflt(data, out);
  1049. }
  1050. // before you yell at me for not just using extends, my reason is that TS inheritance is hard to workerize.
  1051. /**
  1052. * Streaming GZIP compression
  1053. */
  1054. var Gzip = /*#__PURE__*/ (function () {
  1055. function Gzip(opts, cb) {
  1056. this.c = crc();
  1057. this.l = 0;
  1058. this.v = 1;
  1059. Deflate.call(this, opts, cb);
  1060. }
  1061. /**
  1062. * Pushes a chunk to be GZIPped
  1063. * @param chunk The chunk to push
  1064. * @param final Whether this is the last chunk
  1065. */
  1066. Gzip.prototype.push = function (chunk, final) {
  1067. Deflate.prototype.push.call(this, chunk, final);
  1068. };
  1069. Gzip.prototype.p = function (c, f) {
  1070. this.c.p(c);
  1071. this.l += c.length;
  1072. var raw = dopt(c, this.o, this.v && gzhl(this.o), f && 8, !f);
  1073. if (this.v)
  1074. gzh(raw, this.o), this.v = 0;
  1075. if (f)
  1076. wbytes(raw, raw.length - 8, this.c.d()), wbytes(raw, raw.length - 4, this.l);
  1077. this.ondata(raw, f);
  1078. };
  1079. return Gzip;
  1080. }());
  1081. export { Gzip };
  1082. /**
  1083. * Asynchronous streaming GZIP compression
  1084. */
  1085. var AsyncGzip = /*#__PURE__*/ (function () {
  1086. function AsyncGzip(opts, cb) {
  1087. astrmify([
  1088. bDflt,
  1089. gze,
  1090. function () { return [astrm, Deflate, Gzip]; }
  1091. ], this, AsyncCmpStrm.call(this, opts, cb), function (ev) {
  1092. var strm = new Gzip(ev.data);
  1093. onmessage = astrm(strm);
  1094. }, 8);
  1095. }
  1096. return AsyncGzip;
  1097. }());
  1098. export { AsyncGzip };
  1099. export function gzip(data, opts, cb) {
  1100. if (!cb)
  1101. cb = opts, opts = {};
  1102. if (typeof cb != 'function')
  1103. throw 'no callback';
  1104. return cbify(data, opts, [
  1105. bDflt,
  1106. gze,
  1107. function () { return [gzipSync]; }
  1108. ], function (ev) { return pbf(gzipSync(ev.data[0], ev.data[1])); }, 2, cb);
  1109. }
  1110. /**
  1111. * Compresses data with GZIP
  1112. * @param data The data to compress
  1113. * @param opts The compression options
  1114. * @returns The gzipped version of the data
  1115. */
  1116. export function gzipSync(data, opts) {
  1117. if (opts === void 0) { opts = {}; }
  1118. var c = crc(), l = data.length;
  1119. c.p(data);
  1120. var d = dopt(data, opts, gzhl(opts), 8), s = d.length;
  1121. return gzh(d, opts), wbytes(d, s - 8, c.d()), wbytes(d, s - 4, l), d;
  1122. }
  1123. /**
  1124. * Streaming GZIP decompression
  1125. */
  1126. var Gunzip = /*#__PURE__*/ (function () {
  1127. /**
  1128. * Creates a GUNZIP stream
  1129. * @param cb The callback to call whenever data is inflated
  1130. */
  1131. function Gunzip(cb) {
  1132. this.v = 1;
  1133. Inflate.call(this, cb);
  1134. }
  1135. /**
  1136. * Pushes a chunk to be GUNZIPped
  1137. * @param chunk The chunk to push
  1138. * @param final Whether this is the last chunk
  1139. */
  1140. Gunzip.prototype.push = function (chunk, final) {
  1141. Inflate.prototype.e.call(this, chunk);
  1142. if (this.v) {
  1143. var s = gzs(this.p);
  1144. if (s >= this.p.length && !final)
  1145. return;
  1146. this.p = this.p.subarray(s), this.v = 0;
  1147. }
  1148. if (final) {
  1149. if (this.p.length < 8)
  1150. throw 'invalid gzip stream';
  1151. this.p = this.p.subarray(0, -8);
  1152. }
  1153. // necessary to prevent TS from using the closure value
  1154. // This allows for workerization to function correctly
  1155. Inflate.prototype.c.call(this, chunk, final);
  1156. };
  1157. return Gunzip;
  1158. }());
  1159. export { Gunzip };
  1160. /**
  1161. * Asynchronous streaming GZIP decompression
  1162. */
  1163. var AsyncGunzip = /*#__PURE__*/ (function () {
  1164. /**
  1165. * Creates an asynchronous GUNZIP stream
  1166. * @param cb The callback to call whenever data is deflated
  1167. */
  1168. function AsyncGunzip(cb) {
  1169. this.ondata = cb;
  1170. astrmify([
  1171. bInflt,
  1172. guze,
  1173. function () { return [astrm, Inflate, Gunzip]; }
  1174. ], this, 0, function () {
  1175. var strm = new Gunzip();
  1176. onmessage = astrm(strm);
  1177. }, 9);
  1178. }
  1179. return AsyncGunzip;
  1180. }());
  1181. export { AsyncGunzip };
  1182. export function gunzip(data, opts, cb) {
  1183. if (!cb)
  1184. cb = opts, opts = {};
  1185. if (typeof cb != 'function')
  1186. throw 'no callback';
  1187. return cbify(data, opts, [
  1188. bInflt,
  1189. guze,
  1190. function () { return [gunzipSync]; }
  1191. ], function (ev) { return pbf(gunzipSync(ev.data[0])); }, 3, cb);
  1192. }
  1193. /**
  1194. * Expands GZIP data
  1195. * @param data The data to decompress
  1196. * @param out Where to write the data. GZIP already encodes the output size, so providing this doesn't save memory.
  1197. * @returns The decompressed version of the data
  1198. */
  1199. export function gunzipSync(data, out) {
  1200. return inflt(data.subarray(gzs(data), -8), out || new u8(gzl(data)));
  1201. }
  1202. /**
  1203. * Streaming Zlib compression
  1204. */
  1205. var Zlib = /*#__PURE__*/ (function () {
  1206. function Zlib(opts, cb) {
  1207. this.c = adler();
  1208. this.v = 1;
  1209. Deflate.call(this, opts, cb);
  1210. }
  1211. /**
  1212. * Pushes a chunk to be zlibbed
  1213. * @param chunk The chunk to push
  1214. * @param final Whether this is the last chunk
  1215. */
  1216. Zlib.prototype.push = function (chunk, final) {
  1217. Deflate.prototype.push.call(this, chunk, final);
  1218. };
  1219. Zlib.prototype.p = function (c, f) {
  1220. this.c.p(c);
  1221. var raw = dopt(c, this.o, this.v && 2, f && 4, !f);
  1222. if (this.v)
  1223. zlh(raw, this.o), this.v = 0;
  1224. if (f)
  1225. wbytes(raw, raw.length - 4, this.c.d());
  1226. this.ondata(raw, f);
  1227. };
  1228. return Zlib;
  1229. }());
  1230. export { Zlib };
  1231. /**
  1232. * Asynchronous streaming Zlib compression
  1233. */
  1234. var AsyncZlib = /*#__PURE__*/ (function () {
  1235. function AsyncZlib(opts, cb) {
  1236. astrmify([
  1237. bDflt,
  1238. zle,
  1239. function () { return [astrm, Deflate, Zlib]; }
  1240. ], this, AsyncCmpStrm.call(this, opts, cb), function (ev) {
  1241. var strm = new Zlib(ev.data);
  1242. onmessage = astrm(strm);
  1243. }, 10);
  1244. }
  1245. return AsyncZlib;
  1246. }());
  1247. export { AsyncZlib };
  1248. export function zlib(data, opts, cb) {
  1249. if (!cb)
  1250. cb = opts, opts = {};
  1251. if (typeof cb != 'function')
  1252. throw 'no callback';
  1253. return cbify(data, opts, [
  1254. bDflt,
  1255. zle,
  1256. function () { return [zlibSync]; }
  1257. ], function (ev) { return pbf(zlibSync(ev.data[0], ev.data[1])); }, 4, cb);
  1258. }
  1259. /**
  1260. * Compress data with Zlib
  1261. * @param data The data to compress
  1262. * @param opts The compression options
  1263. * @returns The zlib-compressed version of the data
  1264. */
  1265. export function zlibSync(data, opts) {
  1266. if (opts === void 0) { opts = {}; }
  1267. var a = adler();
  1268. a.p(data);
  1269. var d = dopt(data, opts, 2, 4);
  1270. return zlh(d, opts), wbytes(d, d.length - 4, a.d()), d;
  1271. }
  1272. /**
  1273. * Streaming Zlib decompression
  1274. */
  1275. var Unzlib = /*#__PURE__*/ (function () {
  1276. /**
  1277. * Creates a Zlib decompression stream
  1278. * @param cb The callback to call whenever data is inflated
  1279. */
  1280. function Unzlib(cb) {
  1281. this.v = 1;
  1282. Inflate.call(this, cb);
  1283. }
  1284. /**
  1285. * Pushes a chunk to be unzlibbed
  1286. * @param chunk The chunk to push
  1287. * @param final Whether this is the last chunk
  1288. */
  1289. Unzlib.prototype.push = function (chunk, final) {
  1290. Inflate.prototype.e.call(this, chunk);
  1291. if (this.v) {
  1292. if (this.p.length < 2 && !final)
  1293. return;
  1294. this.p = this.p.subarray(2), this.v = 0;
  1295. }
  1296. if (final) {
  1297. if (this.p.length < 8)
  1298. throw 'invalid zlib stream';
  1299. this.p = this.p.subarray(0, -4);
  1300. }
  1301. // necessary to prevent TS from using the closure value
  1302. // This allows for workerization to function correctly
  1303. Inflate.prototype.c.call(this, chunk, final);
  1304. };
  1305. return Unzlib;
  1306. }());
  1307. export { Unzlib };
  1308. /**
  1309. * Asynchronous streaming Zlib decompression
  1310. */
  1311. var AsyncUnzlib = /*#__PURE__*/ (function () {
  1312. /**
  1313. * Creates an asynchronous Zlib decompression stream
  1314. * @param cb The callback to call whenever data is deflated
  1315. */
  1316. function AsyncUnzlib(cb) {
  1317. this.ondata = cb;
  1318. astrmify([
  1319. bInflt,
  1320. zule,
  1321. function () { return [astrm, Inflate, Unzlib]; }
  1322. ], this, 0, function () {
  1323. var strm = new Unzlib();
  1324. onmessage = astrm(strm);
  1325. }, 11);
  1326. }
  1327. return AsyncUnzlib;
  1328. }());
  1329. export { AsyncUnzlib };
  1330. export function unzlib(data, opts, cb) {
  1331. if (!cb)
  1332. cb = opts, opts = {};
  1333. if (typeof cb != 'function')
  1334. throw 'no callback';
  1335. return cbify(data, opts, [
  1336. bInflt,
  1337. zule,
  1338. function () { return [unzlibSync]; }
  1339. ], function (ev) { return pbf(unzlibSync(ev.data[0], gu8(ev.data[1]))); }, 5, cb);
  1340. }
  1341. /**
  1342. * Expands Zlib data
  1343. * @param data The data to decompress
  1344. * @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
  1345. * @returns The decompressed version of the data
  1346. */
  1347. export function unzlibSync(data, out) {
  1348. return inflt((zlv(data), data.subarray(2, -4)), out);
  1349. }
  1350. // Default algorithm for compression (used because having a known output size allows faster decompression)
  1351. export { gzip as compress, AsyncGzip as AsyncCompress };
  1352. // Default algorithm for compression (used because having a known output size allows faster decompression)
  1353. export { gzipSync as compressSync, Gzip as Compress };
  1354. /**
  1355. * Streaming GZIP, Zlib, or raw DEFLATE decompression
  1356. */
  1357. var Decompress = /*#__PURE__*/ (function () {
  1358. /**
  1359. * Creates a decompression stream
  1360. * @param cb The callback to call whenever data is decompressed
  1361. */
  1362. function Decompress(cb) {
  1363. this.G = Gunzip;
  1364. this.I = Inflate;
  1365. this.Z = Unzlib;
  1366. this.ondata = cb;
  1367. }
  1368. /**
  1369. * Pushes a chunk to be decompressed
  1370. * @param chunk The chunk to push
  1371. * @param final Whether this is the last chunk
  1372. */
  1373. Decompress.prototype.push = function (chunk, final) {
  1374. if (!this.ondata)
  1375. throw 'no stream handler';
  1376. if (!this.s) {
  1377. if (this.p && this.p.length) {
  1378. var n = new u8(this.p.length + chunk.length);
  1379. n.set(this.p), n.set(chunk, this.p.length);
  1380. }
  1381. else
  1382. this.p = chunk;
  1383. if (this.p.length > 2) {
  1384. var _this_1 = this;
  1385. var cb = function () { _this_1.ondata.apply(_this_1, arguments); };
  1386. this.s = (this.p[0] == 31 && this.p[1] == 139 && this.p[2] == 8)
  1387. ? new this.G(cb)
  1388. : ((this.p[0] & 15) != 8 || (this.p[0] >> 4) > 7 || ((this.p[0] << 8 | this.p[1]) % 31))
  1389. ? new this.I(cb)
  1390. : new this.Z(cb);
  1391. this.s.push(this.p, final);
  1392. this.p = null;
  1393. }
  1394. }
  1395. else
  1396. this.s.push(chunk, final);
  1397. };
  1398. return Decompress;
  1399. }());
  1400. export { Decompress };
  1401. /**
  1402. * Asynchronous streaming GZIP, Zlib, or raw DEFLATE decompression
  1403. */
  1404. var AsyncDecompress = /*#__PURE__*/ (function () {
  1405. /**
  1406. * Creates an asynchronous decompression stream
  1407. * @param cb The callback to call whenever data is decompressed
  1408. */
  1409. function AsyncDecompress(cb) {
  1410. this.G = AsyncGunzip;
  1411. this.I = AsyncInflate;
  1412. this.Z = AsyncUnzlib;
  1413. this.ondata = cb;
  1414. }
  1415. /**
  1416. * Pushes a chunk to be decompressed
  1417. * @param chunk The chunk to push
  1418. * @param final Whether this is the last chunk
  1419. */
  1420. AsyncDecompress.prototype.push = function (chunk, final) {
  1421. Decompress.prototype.push.call(this, chunk, final);
  1422. };
  1423. return AsyncDecompress;
  1424. }());
  1425. export { AsyncDecompress };
  1426. export function decompress(data, opts, cb) {
  1427. if (!cb)
  1428. cb = opts, opts = {};
  1429. if (typeof cb != 'function')
  1430. throw 'no callback';
  1431. return (data[0] == 31 && data[1] == 139 && data[2] == 8)
  1432. ? gunzip(data, opts, cb)
  1433. : ((data[0] & 15) != 8 || (data[0] >> 4) > 7 || ((data[0] << 8 | data[1]) % 31))
  1434. ? inflate(data, opts, cb)
  1435. : unzlib(data, opts, cb);
  1436. }
  1437. /**
  1438. * Expands compressed GZIP, Zlib, or raw DEFLATE data, automatically detecting the format
  1439. * @param data The data to decompress
  1440. * @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
  1441. * @returns The decompressed version of the data
  1442. */
  1443. export function decompressSync(data, out) {
  1444. return (data[0] == 31 && data[1] == 139 && data[2] == 8)
  1445. ? gunzipSync(data, out)
  1446. : ((data[0] & 15) != 8 || (data[0] >> 4) > 7 || ((data[0] << 8 | data[1]) % 31))
  1447. ? inflateSync(data, out)
  1448. : unzlibSync(data, out);
  1449. }
  1450. // flatten a directory structure
  1451. var fltn = function (d, p, t, o) {
  1452. for (var k in d) {
  1453. var val = d[k], n = p + k;
  1454. if (val instanceof u8)
  1455. t[n] = [val, o];
  1456. else if (Array.isArray(val))
  1457. t[n] = [val[0], mrg(o, val[1])];
  1458. else
  1459. fltn(val, n + '/', t, o);
  1460. }
  1461. };
  1462. /**
  1463. * Converts a string into a Uint8Array for use with compression/decompression methods
  1464. * @param str The string to encode
  1465. * @param latin1 Whether or not to interpret the data as Latin-1. This should
  1466. * not need to be true unless decoding a binary string.
  1467. * @returns The string encoded in UTF-8/Latin-1 binary
  1468. */
  1469. export function strToU8(str, latin1) {
  1470. var l = str.length;
  1471. if (!latin1 && typeof TextEncoder != 'undefined')
  1472. return new TextEncoder().encode(str);
  1473. var ar = new u8(str.length + (str.length >>> 1));
  1474. var ai = 0;
  1475. var w = function (v) { ar[ai++] = v; };
  1476. for (var i = 0; i < l; ++i) {
  1477. if (ai + 5 > ar.length) {
  1478. var n = new u8(ai + 8 + ((l - i) << 1));
  1479. n.set(ar);
  1480. ar = n;
  1481. }
  1482. var c = str.charCodeAt(i);
  1483. if (c < 128 || latin1)
  1484. w(c);
  1485. else if (c < 2048)
  1486. w(192 | (c >>> 6)), w(128 | (c & 63));
  1487. else if (c > 55295 && c < 57344)
  1488. c = 65536 + (c & 1023 << 10) | (str.charCodeAt(++i) & 1023),
  1489. w(240 | (c >>> 18)), w(128 | ((c >>> 12) & 63)), w(128 | ((c >>> 6) & 63)), w(128 | (c & 63));
  1490. else
  1491. w(224 | (c >>> 12)), w(128 | ((c >>> 6) & 63)), w(128 | (c & 63));
  1492. }
  1493. return slc(ar, 0, ai);
  1494. }
  1495. /**
  1496. * Converts a Uint8Array to a string
  1497. * @param dat The data to decode to string
  1498. * @param latin1 Whether or not to interpret the data as Latin-1. This should
  1499. * not need to be true unless encoding to binary string.
  1500. * @returns The original UTF-8/Latin-1 string
  1501. */
  1502. export function strFromU8(dat, latin1) {
  1503. var r = '';
  1504. if (!latin1 && typeof TextDecoder != 'undefined')
  1505. return new TextDecoder().decode(dat);
  1506. for (var i = 0; i < dat.length;) {
  1507. var c = dat[i++];
  1508. if (c < 128 || latin1)
  1509. r += String.fromCharCode(c);
  1510. else if (c < 224)
  1511. r += String.fromCharCode((c & 31) << 6 | (dat[i++] & 63));
  1512. else if (c < 240)
  1513. r += String.fromCharCode((c & 15) << 12 | (dat[i++] & 63) << 6 | (dat[i++] & 63));
  1514. else
  1515. c = ((c & 15) << 18 | (dat[i++] & 63) << 12 | (dat[i++] & 63) << 6 | (dat[i++] & 63)) - 65536,
  1516. r += String.fromCharCode(55296 | (c >> 10), 56320 | (c & 1023));
  1517. }
  1518. return r;
  1519. }
  1520. ;
  1521. // read zip header
  1522. var zh = function (d, b) {
  1523. var bf = b2(d, b + 6), dd = bf & 4, c = b2(d, b + 8), sc = dd ? null : b4(d, b + 18), su = dd ? null : b4(d, b + 22), fnl = b2(d, b + 26), exl = b2(d, b + 28), fn = strFromU8(d.subarray(b += 30, b += fnl), !(bf & 2048));
  1524. return [sc, c, su, fn, b + exl];
  1525. };
  1526. // write zip header
  1527. var wzh = function (d, b, c, cmp, su, fn, u, o, ce, t) {
  1528. var fl = fn.length, l = cmp.length;
  1529. wbytes(d, b, ce != null ? 0x2014B50 : 0x4034B50), b += 4;
  1530. if (ce != null)
  1531. d[b] = 20, b += 2;
  1532. d[b] = 20, b += 2; // spec compliance? what's that?
  1533. d[b++] = (t == 8 && (o.level == 1 ? 6 : o.level < 6 ? 4 : o.level == 9 ? 2 : 0)), d[b++] = u && 8;
  1534. d[b] = t, b += 2;
  1535. var dt = new Date(o.mtime || Date.now()), y = dt.getFullYear() - 1980;
  1536. if (y < 0 || y > 119)
  1537. throw 'date not in range 1980-2099';
  1538. wbytes(d, b, (y << 25) | ((dt.getMonth() + 1) << 21) | (dt.getDate() << 16) | (dt.getHours() << 11) | (dt.getMinutes() << 5) | (dt.getSeconds() >>> 1));
  1539. b += 4;
  1540. wbytes(d, b, c);
  1541. wbytes(d, b + 4, l);
  1542. wbytes(d, b + 8, su);
  1543. wbytes(d, b + 12, fl), b += 16; // skip extra field, comment
  1544. if (ce != null)
  1545. wbytes(d, b += 10, ce), b += 4;
  1546. d.set(fn, b);
  1547. b += fl;
  1548. if (ce == null)
  1549. d.set(cmp, b);
  1550. };
  1551. // write zip footer (end of central directory)
  1552. var wzf = function (o, b, c, d, e) {
  1553. wbytes(o, b, 0x6054B50); // skip disk
  1554. wbytes(o, b + 8, c);
  1555. wbytes(o, b + 10, c);
  1556. wbytes(o, b + 12, d);
  1557. wbytes(o, b + 16, e);
  1558. };
  1559. export function zip(data, opts, cb) {
  1560. if (!cb)
  1561. cb = opts, opts = {};
  1562. if (typeof cb != 'function')
  1563. throw 'no callback';
  1564. var r = {};
  1565. fltn(data, '', r, opts);
  1566. var k = Object.keys(r);
  1567. var lft = k.length, o = 0, tot = 0;
  1568. var slft = lft, files = new Array(lft);
  1569. var term = [];
  1570. var tAll = function () {
  1571. for (var i = 0; i < term.length; ++i)
  1572. term[i]();
  1573. };
  1574. var cbf = function () {
  1575. var out = new u8(tot + 22), oe = o, cdl = tot - o;
  1576. tot = 0;
  1577. for (var i = 0; i < slft; ++i) {
  1578. var f = files[i];
  1579. wzh(out, tot, f.c, f.d, f.m, f.n, f.u, f.p, null, f.t);
  1580. wzh(out, o, f.c, f.d, f.m, f.n, f.u, f.p, tot, f.t), o += 46 + f.n.length, tot += 30 + f.n.length + f.d.length;
  1581. }
  1582. wzf(out, o, files.length, cdl, oe);
  1583. cb(null, out);
  1584. };
  1585. if (!lft)
  1586. cbf();
  1587. var _loop_1 = function (i) {
  1588. var fn = k[i];
  1589. var _a = r[fn], file = _a[0], p = _a[1];
  1590. var c = crc(), m = file.length;
  1591. c.p(file);
  1592. var n = strToU8(fn), s = n.length;
  1593. var t = p.level == 0 ? 0 : 8;
  1594. var cbl = function (e, d) {
  1595. if (e) {
  1596. tAll();
  1597. cb(e, null);
  1598. }
  1599. else {
  1600. var l = d.length;
  1601. files[i] = {
  1602. t: t,
  1603. d: d,
  1604. m: m,
  1605. c: c.d(),
  1606. u: fn.length != l,
  1607. n: n,
  1608. p: p
  1609. };
  1610. o += 30 + s + l;
  1611. tot += 76 + 2 * s + l;
  1612. if (!--lft)
  1613. cbf();
  1614. }
  1615. };
  1616. if (n.length > 65535)
  1617. cbl(new Error('filename too long'), null);
  1618. if (!t)
  1619. cbl(null, file);
  1620. else if (m < 160000) {
  1621. try {
  1622. cbl(null, deflateSync(file, opts));
  1623. }
  1624. catch (e) {
  1625. cbl(e, null);
  1626. }
  1627. }
  1628. else
  1629. term.push(deflate(file, opts, cbl));
  1630. };
  1631. // Cannot use lft because it can decrease
  1632. for (var i = 0; i < slft; ++i) {
  1633. _loop_1(i);
  1634. }
  1635. return tAll;
  1636. }
  1637. /**
  1638. * Synchronously creates a ZIP file. Prefer using `zip` for better performance
  1639. * with more than one file.
  1640. * @param data The directory structure for the ZIP archive
  1641. * @param opts The main options, merged with per-file options
  1642. * @returns The generated ZIP archive
  1643. */
  1644. export function zipSync(data, opts) {
  1645. if (opts === void 0) { opts = {}; }
  1646. var r = {};
  1647. var files = [];
  1648. fltn(data, '', r, opts);
  1649. var o = 0;
  1650. var tot = 0;
  1651. for (var fn in r) {
  1652. var _a = r[fn], file = _a[0], p = _a[1];
  1653. var t = p.level == 0 ? 0 : 8;
  1654. var n = strToU8(fn), s = n.length;
  1655. if (n.length > 65535)
  1656. throw 'filename too long';
  1657. var d = t ? deflateSync(file, p) : file, l = d.length;
  1658. var c = crc();
  1659. c.p(file);
  1660. files.push({
  1661. t: t,
  1662. d: d,
  1663. m: file.length,
  1664. c: c.d(),
  1665. u: fn.length != s,
  1666. n: n,
  1667. o: o,
  1668. p: p
  1669. });
  1670. o += 30 + s + l;
  1671. tot += 76 + 2 * s + l;
  1672. }
  1673. var out = new u8(tot + 22), oe = o, cdl = tot - o;
  1674. for (var i = 0; i < files.length; ++i) {
  1675. var f = files[i];
  1676. wzh(out, f.o, f.c, f.d, f.m, f.n, f.u, f.p, null, f.t);
  1677. wzh(out, o, f.c, f.d, f.m, f.n, f.u, f.p, f.o, f.t), o += 46 + f.n.length;
  1678. }
  1679. wzf(out, o, files.length, cdl, oe);
  1680. return out;
  1681. }
  1682. /**
  1683. * Asynchronously decompresses a ZIP archive
  1684. * @param data The raw compressed ZIP file
  1685. * @param cb The callback to call with the decompressed files
  1686. * @returns A function that can be used to immediately terminate the unzipping
  1687. */
  1688. export function unzip(data, cb) {
  1689. if (typeof cb != 'function')
  1690. throw 'no callback';
  1691. var term = [];
  1692. var tAll = function () {
  1693. for (var i = 0; i < term.length; ++i)
  1694. term[i]();
  1695. };
  1696. var files = {};
  1697. var e = data.length - 22;
  1698. for (; b4(data, e) != 0x6054B50; --e) {
  1699. if (!e || data.length - e > 65558) {
  1700. cb(new Error('invalid zip file'), null);
  1701. return;
  1702. }
  1703. }
  1704. ;
  1705. var lft = b2(data, e + 8);
  1706. if (!lft)
  1707. cb(null, {});
  1708. var c = lft;
  1709. var o = b4(data, e + 16);
  1710. var _loop_2 = function (i) {
  1711. var off = b4(data, o + 42);
  1712. o += 46 + b2(data, o + 28) + b2(data, o + 30) + b2(data, o + 32);
  1713. var _a = zh(data, off), sc = _a[0], c_1 = _a[1], su = _a[2], fn = _a[3], b = _a[4];
  1714. var cbl = function (e, d) {
  1715. if (e) {
  1716. tAll();
  1717. cb(e, null);
  1718. }
  1719. else {
  1720. files[fn] = d;
  1721. if (!--lft)
  1722. cb(null, files);
  1723. }
  1724. };
  1725. if (!c_1)
  1726. cbl(null, slc(data, b, b + sc));
  1727. else if (c_1 == 8) {
  1728. var infl = data.subarray(b, sc ? b + sc : data.length);
  1729. if (sc < 320000) {
  1730. try {
  1731. cbl(null, inflateSync(infl, su != null && new u8(su)));
  1732. }
  1733. catch (e) {
  1734. cbl(e, null);
  1735. }
  1736. }
  1737. else
  1738. inflate(infl, { size: su }, cbl);
  1739. }
  1740. else
  1741. throw 'unknown compression type ' + c_1;
  1742. };
  1743. for (var i = 0; i < c; ++i) {
  1744. _loop_2(i);
  1745. }
  1746. return tAll;
  1747. }
  1748. /**
  1749. * Synchronously decompresses a ZIP archive. Prefer using `unzip` for better
  1750. * performance with more than one file.
  1751. * @param data The raw compressed ZIP file
  1752. * @returns The decompressed files
  1753. */
  1754. export function unzipSync(data) {
  1755. var files = {};
  1756. var e = data.length - 22;
  1757. for (; b4(data, e) != 0x6054B50; --e) {
  1758. if (!e || data.length - e > 65558)
  1759. throw 'invalid zip file';
  1760. }
  1761. ;
  1762. var c = b2(data, e + 8);
  1763. if (!c)
  1764. return {};
  1765. var o = b4(data, e + 16);
  1766. for (var i = 0; i < c; ++i) {
  1767. var off = b4(data, o + 42);
  1768. o += 46 + b2(data, o + 28) + b2(data, o + 30) + b2(data, o + 32);
  1769. var _a = zh(data, off), sc = _a[0], c_2 = _a[1], su = _a[2], fn = _a[3], b = _a[4];
  1770. if (!c_2)
  1771. files[fn] = slc(data, b, b + sc);
  1772. else if (c_2 == 8)
  1773. files[fn] = inflateSync(data.subarray(b, sc ? b + sc : data.length), su != null && new u8(su));
  1774. else
  1775. throw 'unknown compression type ' + c_2;
  1776. }
  1777. return files;
  1778. }