index.ts 73 KB

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