index.tsx 17 KB

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