index.js 60 KB

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