index.js 92 KB

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