index.tsx 18 KB

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