fflate.module.js 87 KB

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