index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 gzipped = file.stream().pipeThrough(toNativeStream(gzipStream));
  214. let gzSize = 0;
  215. gzipped.pipeTo(new WritableStream({
  216. write(chunk) {
  217. gzSize += chunk.length;
  218. },
  219. close() {
  220. callback('Length ' + gzSize);
  221. }
  222. }));`,
  223. uzip: `// UZIP doesn't support streaming to any extent
  224. callback(new Error('unsupported'));`,
  225. pako: `// Hundreds of lines of code to make this run on a Worker...
  226. const file = files[0];
  227. // In case this wasn't clear already, Pako doesn't actually support this,
  228. // you need to create a custom async stream. I suppose you could copy the
  229. // code used in this demo, which is on GitHub under the demo/ directory.
  230. const gzipStream = pakoWorker.createGzip();
  231. const gzipped = file.stream().pipeThrough(toNativeStream(gzipStream));
  232. let gzSize = 0;
  233. gzipped.pipeTo(new WritableStream({
  234. write(chunk) {
  235. gzSize += chunk.length;
  236. },
  237. close() {
  238. callback('Length ' + gzSize);
  239. }
  240. }));`
  241. };
  242. }
  243. const availablePresets = Object.keys(presets);
  244. const CodeHighlight: FC<{
  245. code: string;
  246. preset: string;
  247. onInput: (newCode: string) => void;
  248. }> = ({ code, preset, onInput }) => {
  249. const highlight = useMemo(() => ({
  250. __html: Prism.highlight(code + '\n', Prism.languages.javascript, 'javascript')
  251. }), [code]);
  252. const pre = useRef<HTMLPreElement>(null);
  253. const ta = useRef<HTMLTextAreaElement>(null);
  254. useEffect(() => {
  255. pre.current!.addEventListener('scroll', () => {
  256. ta.current!.scrollLeft = pre.current!.scrollLeft;
  257. ta.current!.style.left = pre.current!.scrollLeft + 'px';
  258. }, { passive: true });
  259. ta.current!.addEventListener('scroll', () => {
  260. pre.current!.scrollLeft = ta.current!.scrollLeft;
  261. }, { passive: true });
  262. }, []);
  263. useEffect(() => {
  264. ta.current!.value = code;
  265. }, [preset]);
  266. return (
  267. <pre ref={pre} style={{
  268. position: 'relative',
  269. backgroundColor: '#2a2734',
  270. color: '#9a86fd',
  271. maxWidth: 'calc(90vw - 2em)',
  272. fontSize: '0.7em',
  273. marginTop: '1em',
  274. marginBottom: '1em',
  275. padding: '1em',
  276. overflow: 'auto',
  277. fontFamily: 'Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace'
  278. }}>
  279. <div dangerouslySetInnerHTML={highlight} />
  280. <textarea
  281. ref={ta}
  282. autoComplete="off"
  283. autoCorrect="off"
  284. autoCapitalize="off"
  285. spellCheck="false"
  286. style={{
  287. border: 0,
  288. resize: 'none',
  289. outline: 'none',
  290. position: 'absolute',
  291. background: 'transparent',
  292. whiteSpace: 'pre',
  293. top: 0,
  294. left: 0,
  295. width: 'calc(100% - 1em)',
  296. height: 'calc(100% - 2em)',
  297. overflow: 'hidden',
  298. lineHeight: 'inherit',
  299. fontSize: 'inherit',
  300. padding: 'inherit',
  301. paddingRight: 0,
  302. color: 'transparent',
  303. caretColor: 'white',
  304. fontFamily: 'inherit'
  305. }}
  306. onKeyDown={e => {
  307. const t = e.currentTarget;
  308. let val = t.value;
  309. const loc = t.selectionStart;
  310. if (e.key == 'Enter') {
  311. const lastNL = val.lastIndexOf('\n', loc - 1);
  312. let indent = 0;
  313. for (; val.charCodeAt(indent + lastNL + 1) == 32; ++indent);
  314. const lastChar = val.charAt(loc - 1);
  315. const nextChar = val.charAt(loc);
  316. if (lastChar == '{'|| lastChar == '(' || lastChar == '[') indent += 2;
  317. const addNL = nextChar == '}' || nextChar == ')' || nextChar == ']';
  318. const tail = val.slice(t.selectionEnd);
  319. val = val.slice(0, loc) + '\n';
  320. for (let i = 0; i < indent; ++i) val += ' ';
  321. if (addNL) {
  322. if (
  323. (lastChar == '{' && nextChar == '}') ||
  324. (lastChar == '[' && nextChar == ']') ||
  325. (lastChar == '(' && nextChar == ')')
  326. ) {
  327. val += '\n';
  328. for (let i = 2; i < indent; ++i) val += ' ';
  329. } else {
  330. const end = Math.min(indent, 2);
  331. indent -= end;
  332. val = val.slice(0, -end);
  333. }
  334. }
  335. t.value = val += tail;
  336. t.selectionStart = t.selectionEnd = loc + indent + 1;
  337. ta.current!.scrollLeft = 0;
  338. } else if (e.key == 'Tab') {
  339. t.value = val = val.slice(0, loc) + ' ' + val.slice(t.selectionEnd);
  340. t.selectionStart = t.selectionEnd = loc + 2;
  341. } else if (t.selectionStart == t.selectionEnd) {
  342. if (e.key == 'Backspace') {
  343. if (val.charCodeAt(loc - 1) == 32 && !val.slice(val.lastIndexOf('\n', loc - 1), loc).trim().length) {
  344. t.value = val.slice(0, loc - 1) + val.slice(loc);
  345. t.selectionStart = t.selectionEnd = loc - 1;
  346. } else if (
  347. (val.charAt(loc - 1) == '{' && val.charAt(loc) == '}') ||
  348. (val.charAt(loc - 1) == '[' && val.charAt(loc) == ']') ||
  349. (val.charAt(loc - 1) == '(' && val.charAt(loc) == ')')
  350. ) {
  351. t.value = val.slice(0, loc) + val.slice(loc + 1);
  352. // hack, doesn't work always
  353. t.selectionStart = t.selectionEnd = loc;
  354. }
  355. return;
  356. } else {
  357. switch(e.key) {
  358. case '{':
  359. case '[':
  360. case '(':
  361. t.value = val = val.slice(0, loc) + (e.key == '{' ? '}' : e.key == '[' ? ']' : ')') + val.slice(loc);
  362. t.selectionStart = t.selectionEnd = loc;
  363. break;
  364. case '}':
  365. case ']':
  366. case ')':
  367. // BUG: if the cursor is moved, this false activates
  368. if (e.key == val.charAt(loc)) {
  369. t.value = val.slice(0, loc) + val.slice(loc + 1);
  370. t.selectionStart = t.selectionEnd = loc;
  371. } else {
  372. const lastNL = val.lastIndexOf('\n', loc - 1);
  373. const sl = val.slice(lastNL, loc);
  374. const o = loc - (sl.length > 1 && !sl.trim().length ? 2 : 0);
  375. t.value = val.slice(0, o) + val.slice(loc);
  376. t.selectionStart = t.selectionEnd = o;
  377. }
  378. }
  379. return;
  380. };
  381. } else return;
  382. e.preventDefault();
  383. onInput(val);
  384. }}
  385. onInput={e => onInput(e.currentTarget.value)}
  386. >
  387. {code}
  388. </textarea>
  389. </pre>
  390. )
  391. };
  392. const CodeBox: FC<{files: File[]; forwardRef: Ref<HTMLDivElement>}> = ({ files, forwardRef }) => {
  393. const [preset, setPreset] = useState('Basic GZIP compression');
  394. const [{ fflate, uzip, pako }, setCodes] = useState(presets[preset]);
  395. const [ffl, setFFL] = useState('');
  396. const [uz, setUZ] = useState('');
  397. const [pk, setPK] = useState('');
  398. useEffect(() => {
  399. if (!files) {
  400. setFFL('');
  401. setUZ('');
  402. setPK('');
  403. }
  404. }, [files]);
  405. const onInput = (lib: 'fflate' | 'uzip' | 'pako', code: string) => {
  406. const codes: Preset = {
  407. fflate,
  408. uzip,
  409. pako
  410. };
  411. codes[lib] = code;
  412. setCodes(codes);
  413. setPreset('Custom');
  414. }
  415. const [hover, setHover] = useState(false);
  416. return (
  417. <div ref={forwardRef} style={{
  418. display: 'flex',
  419. flexDirection: 'column',
  420. justifyContent: 'space-between',
  421. alignItems: 'center',
  422. width: '100%',
  423. flexWrap: 'wrap'
  424. }}>
  425. <div>
  426. <label>Preset: </label>
  427. <select value={preset} onChange={e => {
  428. let newPreset = e.currentTarget.value;
  429. if (newPreset != 'Custom') setCodes(presets[newPreset]);
  430. setPreset(newPreset);
  431. }} style={{
  432. marginTop: '2em'
  433. }}>
  434. {availablePresets.map(preset => <option key={preset} value={preset}>{preset}</option>)}
  435. <option value="Custom">Custom</option>
  436. </select>
  437. </div>
  438. <div style={{
  439. display: 'flex',
  440. flexDirection: 'row',
  441. justifyContent: 'space-around',
  442. whiteSpace: 'pre-wrap',
  443. textAlign: 'left',
  444. flexWrap: 'wrap'
  445. }}>
  446. <div style={{ padding: '2vmin' }}>
  447. fflate:
  448. <CodeHighlight code={fflate} preset={preset} onInput={t => onInput('fflate', t)} />
  449. <span dangerouslySetInnerHTML={{ __html: ffl }} />
  450. </div>
  451. <div style={{
  452. display: 'flex',
  453. flexDirection: 'row',
  454. flexWrap: 'wrap',
  455. justifyContent: 'space-around',
  456. }}>
  457. <div style={{ padding: '2vmin' }}>
  458. UZIP (shimmed):
  459. <CodeHighlight code={uzip} preset={preset} onInput={t => onInput('uzip', t)} />
  460. <span dangerouslySetInnerHTML={{ __html: uz }} />
  461. </div>
  462. <div style={{ padding: '2vmin' }}>
  463. Pako (shimmed):
  464. <CodeHighlight code={pako} preset={preset} onInput={t => onInput('pako', t)} />
  465. <span dangerouslySetInnerHTML={{ __html: pk }} />
  466. </div>
  467. </div>
  468. </div>
  469. <button disabled={pk == 'Waiting...' || pk == 'Running...'} style={{
  470. cursor: 'default',
  471. width: '20vmin',
  472. height: '6vh',
  473. fontSize: '1.25em',
  474. margin: '1vmin',
  475. padding: '1vmin',
  476. display: 'flex',
  477. alignItems: 'center',
  478. justifyContent: 'center',
  479. 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)',
  480. border: '1px solid black',
  481. borderRadius: '6px',
  482. transition: 'background-color 300ms ease-in-out',
  483. WebkitTouchCallout: 'none',
  484. WebkitUserSelect: 'none',
  485. msUserSelect: 'none',
  486. MozUserSelect: 'none',
  487. userSelect: 'none',
  488. outline: 'none',
  489. backgroundColor: hover ? 'rgba(0, 0, 0, 0.2)' : 'white'
  490. }} onMouseOver={() => setHover(true)} onMouseLeave={() => setHover(false)} onClick={() => {
  491. setHover(false);
  492. const ts = tm();
  493. setFFL(rn);
  494. setUZ(wt);
  495. setPK(wt);
  496. exec(fflate, files, out => {
  497. const tf = tm();
  498. setFFL('Finished in <span style="font-weight:bold">' + (tf - ts).toFixed(3) + 'ms</span>: ' + out);
  499. exec(uzip, files, out => {
  500. const tu = tm();
  501. setUZ('Finished in <span style="font-weight:bold">' + (tu - tf).toFixed(3) + 'ms:</span> ' + out);
  502. exec(pako, files, out => {
  503. setPK('Finished in <span style="font-weight:bold">' + (tm() - tu).toFixed(3) + 'ms:</span> ' + out);
  504. });
  505. });
  506. });
  507. }}>Run</button>
  508. </div>
  509. );
  510. }
  511. export default CodeBox;