fflate.module.js 78 KB

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