index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. import React, { FC, Ref, useEffect, useMemo, useRef, useState } from 'react';
  2. import { Prism } from './prism';
  3. import './prism';
  4. import './prism.css';
  5. import exec from './sandbox';
  6. const canStream = 'stream' in File.prototype;
  7. const rn = 'Running...';
  8. const wt = 'Waiting...';
  9. const tm = typeof performance != 'undefined'
  10. ? () => performance.now()
  11. : () => Date.now();
  12. type Preset = {
  13. fflate: string;
  14. uzip: string;
  15. pako: string;
  16. };
  17. const presets: Record<string, Preset> = {
  18. 'Basic GZIP compression': {
  19. fflate: `var left = files.length;
  20. var filesLengths = {};
  21. // In a real app, use a list of file types to avoid compressing for better
  22. // performance
  23. var ALREADY_COMPRESSED = [
  24. 'zip', 'gz', 'png', 'jpg', 'jpeg', 'pdf', 'doc', 'docx', 'ppt', 'pptx',
  25. 'xls', 'xlsx', 'heic', 'heif', '7z', 'bz2', 'rar', 'gif', 'webp', 'webm'
  26. ];
  27. // This function binds the variable "file" to the local scope, which makes
  28. // parallel processing possible.
  29. // If you use ES6, you can declare variables with "let" to automatically bind
  30. // the variable to the scope rather than using a separate function.
  31. var processFile = function(i) {
  32. var file = files[i];
  33. fileToU8(file, function(buf) {
  34. fflate.gzip(buf, {
  35. // In a real app, instead of always compressing at a certain level,
  36. // you'll want to check if the file is already compressed. For fairness,
  37. // that's not done here.
  38. /*
  39. level: ALREADY_COMPRESSED.indexOf(
  40. file.name.slice(file.name.lastIndexOf('.') + 1)
  41. ) == -1 ? 6 : 0
  42. */
  43. level: 6,
  44. // You can uncomment the below for a contest of pure algorithm speed.
  45. // In a real app, you'll probably not need to set the memory level
  46. // because fflate picks a reasonable level based on file size by default.
  47. // If fflate performs worse than UZIP, you're probably passing in
  48. // incompressible files; switching the level or the mem will fix it.
  49. /*
  50. mem: 4
  51. */
  52. // The following are optional, but fflate supports metadata if you want
  53. mtime: file.lastModified,
  54. filename: file.name
  55. }, function(err, data) {
  56. if (err) callback(err);
  57. else {
  58. filesLengths[file.name] = [data.length, file.size];
  59. // If you want to download the file to check it for yourself:
  60. // download(data, 'myFile.gz');
  61. // If everyone else has finished processing already...
  62. if (!--left) {
  63. // Then return.
  64. callback(prettySizes(filesLengths));
  65. }
  66. }
  67. });
  68. });
  69. }
  70. for (var i = 0; i < files.length; ++i) {
  71. processFile(i);
  72. }`,
  73. uzip: `var left = files.length;
  74. var filesLengths = {};
  75. var processFile = function(i) {
  76. var file = files[i];
  77. fileToU8(file, function(buf) {
  78. // UZIP doesn't natively support GZIP, but I patched in support for it.
  79. // In other words, you're better off using fflate for GZIP.
  80. // Also, UZIP runs synchronously on the main thread. It relies on global
  81. // state, so you can't even run it in the background without causing bugs.
  82. // But just for the sake of a performance comparison, try it out.
  83. uzipWorker.gzip(buf, function(err, data) {
  84. if (err) callback(err);
  85. else {
  86. filesLengths[file.name] = [data.length, file.size];
  87. if (!--left) callback(prettySizes(filesLengths));
  88. }
  89. });
  90. });
  91. }
  92. for (var i = 0; i < files.length; ++i) {
  93. processFile(i);
  94. }`,
  95. pako: `var left = files.length;
  96. var filesLengths = {};
  97. var processFile = function(i) {
  98. var file = files[i];
  99. fileToU8(file, function(buf) {
  100. // Unlike UZIP, Pako natively supports GZIP, and it doesn't rely on global
  101. // state. However, it's still 46kB for this basic functionality as opposed
  102. // to fflate's 7kB, not to mention the fact that there's no easy way to use
  103. // it asynchronously. I had to add a worker proxy for this to work.
  104. pakoWorker.gzip(buf, function(err, data) {
  105. if (err) callback(err)
  106. else {
  107. filesLengths[file.name] = [data.length, file.size];
  108. if (!--left) callback(prettySizes(filesLengths));
  109. }
  110. });
  111. });
  112. }
  113. for (var i = 0; i < files.length; ++i) {
  114. processFile(i);
  115. }`
  116. },
  117. 'ZIP archive creation': {
  118. fflate: `// fflate's ZIP API is asynchronous and parallelized (multithreaded)
  119. var left = files.length;
  120. var zipObj = {};
  121. var ALREADY_COMPRESSED = [
  122. 'zip', 'gz', 'png', 'jpg', 'jpeg', 'pdf', 'doc', 'docx', 'ppt', 'pptx',
  123. 'xls', 'xlsx', 'heic', 'heif', '7z', 'bz2', 'rar', 'gif', 'webp', 'webm'
  124. ];
  125. // Yet again, this is necessary for parallelization.
  126. var processFile = function(i) {
  127. var file = files[i];
  128. var ext = file.name.slice(file.name.lastIndexOf('.') + 1);
  129. fileToU8(file, function(buf) {
  130. // With fflate, we can choose which files we want to compress
  131. zipObj[file.name] = [buf, {
  132. level: ALREADY_COMPRESSED.indexOf(ext) == -1 ? 6 : 0
  133. }];
  134. // If we didn't want to specify options:
  135. // zipObj[file.name] = buf;
  136. if (!--left) {
  137. fflate.zip(zipObj, {
  138. // If you want to control options for every file, you can do so here
  139. // They are merged with the per-file options (if they exist)
  140. // mem: 9
  141. }, function(err, out) {
  142. if (err) callback(err);
  143. else {
  144. // You may want to try downloading to see that fflate actually works:
  145. // download(out, 'fflate-demo.zip');
  146. callback('Length ' + out.length);
  147. }
  148. });
  149. }
  150. });
  151. }
  152. for (var i = 0; i < files.length; ++i) {
  153. processFile(i);
  154. }`,
  155. uzip: `var left = files.length;
  156. var processFile = function(i) {
  157. var file = files[i];
  158. fileToU8(file, function(buf) {
  159. // With UZIP, you cannot control the compression level of a file
  160. // However, it skips compressing ZIP, JPEG, and PNG files out of the box.
  161. zipObj.add(file.name, buf);
  162. if (!--left) {
  163. zipObj.ondata = function(err, out) {
  164. if (err) callback(err);
  165. else callback('Length ' + out.length);
  166. }
  167. zipObj.end();
  168. }
  169. });
  170. }
  171. // Reminder that this is custom sugar
  172. var zipObj = uzipWorker.zip();
  173. for (var i = 0; i < files.length; ++i) {
  174. processFile(i);
  175. }`,
  176. pako: `var left = files.length;
  177. // Internally, this uses JSZip. Despite its clean API, it suffers from
  178. // abysmal performance and awful compression ratios, particularly in v3.2.0
  179. // and up.
  180. // If you choose JSZip, make sure to use v3.1.5 for adequate performance
  181. // (2-3x slower than fflate) instead of the latest version, which is 20-30x
  182. // slower than fflate.
  183. var zipObj = pakoWorker.zip();
  184. var processFile = function(i) {
  185. var file = files[i];
  186. fileToU8(file, function(buf) {
  187. // With JSZip, you cannot control the compression level of a file
  188. zipObj.add(file.name, buf);
  189. if (!--left) {
  190. zipObj.ondata = function(err, out) {
  191. if (err) callback(err);
  192. else callback('Length ' + out.length);
  193. }
  194. zipObj.end();
  195. }
  196. });
  197. }
  198. for (var i = 0; i < files.length; ++i) {
  199. processFile(i);
  200. }`
  201. }
  202. }
  203. if (canStream) {
  204. presets['Streaming GZIP compression'] = {
  205. fflate: `const { AsyncGzip } = fflate;
  206. // Theoretically, you could do this on every file, but I haven't done that here
  207. // for the sake of simplicity.
  208. const file = files[0];
  209. const gzipStream = new AsyncGzip({ level: 6 });
  210. // We can stream the file through GZIP to reduce memory usage
  211. const fakeResponse = new Response(
  212. file.stream().pipeThrough(toNativeStream(gzipStream))
  213. );
  214. fakeResponse.arrayBuffer().then(buf => {
  215. callback('Length ' + buf.byteLength);
  216. });`,
  217. uzip: `// UZIP doesn't support streaming to any extent
  218. callback(new Error('unsupported'));`,
  219. pako: `// Hundreds of lines of code to make this run on a Worker...
  220. const file = files[0];
  221. // In case this wasn't clear already, Pako doesn't actually support this,
  222. // you need to create a custom async stream. I suppose you could copy the
  223. // code used in this demo, which is on GitHub under the demo/ directory.
  224. const gzipStream = pakoWorker.createGzip();
  225. const fakeResponse = new Response(
  226. file.stream().pipeThrough(toNativeStream(gzipStream))
  227. );
  228. fakeResponse.arrayBuffer().then(buf => {
  229. callback('Length ' + buf.byteLength);
  230. });`
  231. };
  232. }
  233. const availablePresets = Object.keys(presets);
  234. const CodeHighlight: FC<{
  235. code: string;
  236. preset: string;
  237. onInput: (newCode: string) => void;
  238. }> = ({ code, preset, onInput }) => {
  239. const highlight = useMemo(() => ({
  240. __html: Prism.highlight(code + '\n', Prism.languages.javascript, 'javascript')
  241. }), [code]);
  242. const pre = useRef<HTMLPreElement>(null);
  243. const ta = useRef<HTMLTextAreaElement>(null);
  244. useEffect(() => {
  245. pre.current!.addEventListener('scroll', () => {
  246. ta.current!.scrollLeft = pre.current!.scrollLeft;
  247. ta.current!.style.left = pre.current!.scrollLeft + 'px';
  248. }, { passive: true });
  249. ta.current!.addEventListener('scroll', () => {
  250. pre.current!.scrollLeft = ta.current!.scrollLeft;
  251. }, { passive: true });
  252. }, []);
  253. useEffect(() => {
  254. ta.current!.value = code;
  255. }, [preset]);
  256. return (
  257. <pre ref={pre} style={{
  258. position: 'relative',
  259. backgroundColor: '#2a2734',
  260. color: '#9a86fd',
  261. maxWidth: 'calc(90vw - 2em)',
  262. fontSize: '0.7em',
  263. marginTop: '1em',
  264. marginBottom: '1em',
  265. padding: '1em',
  266. overflow: 'auto',
  267. fontFamily: 'Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace'
  268. }}>
  269. <div dangerouslySetInnerHTML={highlight} />
  270. <textarea
  271. ref={ta}
  272. autoComplete="off"
  273. autoCorrect="off"
  274. autoCapitalize="off"
  275. spellCheck="false"
  276. style={{
  277. border: 0,
  278. resize: 'none',
  279. outline: 'none',
  280. position: 'absolute',
  281. background: 'transparent',
  282. whiteSpace: 'pre',
  283. top: 0,
  284. left: 0,
  285. width: 'calc(100% - 1em)',
  286. height: 'calc(100% - 2em)',
  287. overflow: 'hidden',
  288. lineHeight: 'inherit',
  289. fontSize: 'inherit',
  290. padding: 'inherit',
  291. paddingRight: 0,
  292. color: 'transparent',
  293. caretColor: 'white',
  294. fontFamily: 'inherit'
  295. }}
  296. onKeyDown={e => {
  297. const t = e.currentTarget;
  298. let val = t.value;
  299. const loc = t.selectionStart;
  300. if (e.key == 'Enter') {
  301. const lastNL = val.lastIndexOf('\n', loc - 1);
  302. let indent = 0;
  303. for (; val.charCodeAt(indent + lastNL + 1) == 32; ++indent);
  304. const lastChar = val.charAt(loc - 1);
  305. const nextChar = val.charAt(loc);
  306. if (lastChar == '{'|| lastChar == '(' || lastChar == '[') indent += 2;
  307. const addNL = nextChar == '}' || nextChar == ')' || nextChar == ']';
  308. const tail = val.slice(t.selectionEnd);
  309. val = val.slice(0, loc) + '\n';
  310. for (let i = 0; i < indent; ++i) val += ' ';
  311. if (addNL) {
  312. if (
  313. (lastChar == '{' && nextChar == '}') ||
  314. (lastChar == '[' && nextChar == ']') ||
  315. (lastChar == '(' && nextChar == ')')
  316. ) {
  317. val += '\n';
  318. for (let i = 2; i < indent; ++i) val += ' ';
  319. } else {
  320. const end = Math.min(indent, 2);
  321. indent -= end;
  322. val = val.slice(0, -end);
  323. }
  324. }
  325. t.value = val += tail;
  326. t.selectionStart = t.selectionEnd = loc + indent + 1;
  327. ta.current!.scrollLeft = 0;
  328. } else if (e.key == 'Tab') {
  329. t.value = val = val.slice(0, loc) + ' ' + val.slice(t.selectionEnd);
  330. t.selectionStart = t.selectionEnd = loc + 2;
  331. } else if (t.selectionStart == t.selectionEnd) {
  332. if (e.key == 'Backspace') {
  333. if (val.charCodeAt(loc - 1) == 32 && !val.slice(val.lastIndexOf('\n', loc - 1), loc).trim().length) {
  334. t.value = val.slice(0, loc - 1) + val.slice(loc);
  335. t.selectionStart = t.selectionEnd = loc - 1;
  336. } else if (
  337. (val.charAt(loc - 1) == '{' && val.charAt(loc) == '}') ||
  338. (val.charAt(loc - 1) == '[' && val.charAt(loc) == ']') ||
  339. (val.charAt(loc - 1) == '(' && val.charAt(loc) == ')')
  340. ) {
  341. t.value = val.slice(0, loc) + val.slice(loc + 1);
  342. // hack, doesn't work always
  343. t.selectionStart = t.selectionEnd = loc;
  344. }
  345. return;
  346. } else {
  347. switch(e.key) {
  348. case '{':
  349. case '[':
  350. case '(':
  351. t.value = val = val.slice(0, loc) + (e.key == '{' ? '}' : e.key == '[' ? ']' : ')') + val.slice(loc);
  352. t.selectionStart = t.selectionEnd = loc;
  353. break;
  354. case '}':
  355. case ']':
  356. case ')':
  357. // BUG: if the cursor is moved, this false activates
  358. if (e.key == val.charAt(loc)) {
  359. t.value = val.slice(0, loc) + val.slice(loc + 1);
  360. t.selectionStart = t.selectionEnd = loc;
  361. } else {
  362. const lastNL = val.lastIndexOf('\n', loc - 1);
  363. const sl = val.slice(lastNL, loc);
  364. const o = loc - (sl.length > 1 && !sl.trim().length ? 2 : 0);
  365. t.value = val.slice(0, o) + val.slice(loc);
  366. t.selectionStart = t.selectionEnd = o;
  367. }
  368. }
  369. return;
  370. };
  371. } else return;
  372. e.preventDefault();
  373. onInput(val);
  374. }}
  375. onInput={e => onInput(e.currentTarget.value)}
  376. >
  377. {code}
  378. </textarea>
  379. </pre>
  380. )
  381. };
  382. const CodeBox: FC<{files: File[]; forwardRef: Ref<HTMLDivElement>}> = ({ files, forwardRef }) => {
  383. const [preset, setPreset] = useState('Basic GZIP compression');
  384. const [{ fflate, uzip, pako }, setCodes] = useState(presets[preset]);
  385. const [ffl, setFFL] = useState('');
  386. const [uz, setUZ] = useState('');
  387. const [pk, setPK] = useState('');
  388. useEffect(() => {
  389. if (!files) {
  390. setFFL('');
  391. setUZ('');
  392. setPK('');
  393. }
  394. }, [files]);
  395. const onInput = (lib: 'fflate' | 'uzip' | 'pako', code: string) => {
  396. const codes: Preset = {
  397. fflate,
  398. uzip,
  399. pako
  400. };
  401. codes[lib] = code;
  402. setCodes(codes);
  403. setPreset('Custom');
  404. }
  405. const [hover, setHover] = useState(false);
  406. return (
  407. <div ref={forwardRef} style={{
  408. display: 'flex',
  409. flexDirection: 'column',
  410. justifyContent: 'space-between',
  411. alignItems: 'center',
  412. width: '100%',
  413. flexWrap: 'wrap'
  414. }}>
  415. <div>
  416. <label>Preset: </label>
  417. <select value={preset} onChange={e => {
  418. let newPreset = e.currentTarget.value;
  419. if (newPreset != 'Custom') setCodes(presets[newPreset]);
  420. setPreset(newPreset);
  421. }} style={{
  422. marginTop: '2em'
  423. }}>
  424. {availablePresets.map(preset => <option key={preset} value={preset}>{preset}</option>)}
  425. <option value="Custom">Custom</option>
  426. </select>
  427. </div>
  428. <div style={{
  429. display: 'flex',
  430. flexDirection: 'row',
  431. justifyContent: 'space-around',
  432. whiteSpace: 'pre-wrap',
  433. textAlign: 'left',
  434. flexWrap: 'wrap'
  435. }}>
  436. <div style={{ padding: '2vmin' }}>
  437. fflate:
  438. <CodeHighlight code={fflate} preset={preset} onInput={t => onInput('fflate', t)} />
  439. <span dangerouslySetInnerHTML={{ __html: ffl }} />
  440. </div>
  441. <div style={{
  442. display: 'flex',
  443. flexDirection: 'row',
  444. flexWrap: 'wrap',
  445. justifyContent: 'space-around',
  446. }}>
  447. <div style={{ padding: '2vmin' }}>
  448. UZIP (shimmed):
  449. <CodeHighlight code={uzip} preset={preset} onInput={t => onInput('uzip', t)} />
  450. <span dangerouslySetInnerHTML={{ __html: uz }} />
  451. </div>
  452. <div style={{ padding: '2vmin' }}>
  453. Pako (shimmed):
  454. <CodeHighlight code={pako} preset={preset} onInput={t => onInput('pako', t)} />
  455. <span dangerouslySetInnerHTML={{ __html: pk }} />
  456. </div>
  457. </div>
  458. </div>
  459. <button disabled={pk == 'Waiting...' || pk == 'Running...'} style={{
  460. cursor: 'default',
  461. width: '20vmin',
  462. height: '6vh',
  463. fontSize: '1.25em',
  464. margin: '1vmin',
  465. padding: '1vmin',
  466. display: 'flex',
  467. alignItems: 'center',
  468. justifyContent: 'center',
  469. boxShadow: '0 1px 2px 1px rgba(0, 0, 0, 0.2), 0 2px 4px 2px rgba(0, 0, 0, 0.15), 0 4px 8px 4px rgba(0, 0, 0, 0.12)',
  470. border: '1px solid black',
  471. borderRadius: '6px',
  472. transition: 'background-color 300ms ease-in-out',
  473. WebkitTouchCallout: 'none',
  474. WebkitUserSelect: 'none',
  475. msUserSelect: 'none',
  476. MozUserSelect: 'none',
  477. userSelect: 'none',
  478. outline: 'none',
  479. backgroundColor: hover ? 'rgba(0, 0, 0, 0.2)' : 'white'
  480. }} onMouseOver={() => setHover(true)} onMouseLeave={() => setHover(false)} onClick={() => {
  481. setHover(false);
  482. const ts = tm();
  483. setFFL(rn);
  484. setUZ(wt);
  485. setPK(wt);
  486. exec(fflate, files, out => {
  487. const tf = tm();
  488. setFFL('Finished in <span style="font-weight:bold">' + (tf - ts).toFixed(3) + 'ms</span>: ' + out);
  489. exec(uzip, files, out => {
  490. const tu = tm();
  491. setUZ('Finished in <span style="font-weight:bold">' + (tu - tf).toFixed(3) + 'ms:</span> ' + out);
  492. exec(pako, files, out => {
  493. setPK('Finished in <span style="font-weight:bold">' + (tm() - tu).toFixed(3) + 'ms:</span> ' + out);
  494. });
  495. });
  496. });
  497. }}>Run</button>
  498. </div>
  499. );
  500. }
  501. export default CodeBox;