fflate.module.js 81 KB

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