index.js 61 KB

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