index.mjs 60 KB

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