index.ts 67 KB

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