提交 7271b346 编写于 作者: J Jason Park

Remove dependencies on unpkg.com (#234)

上级 ece28477
{
"name": "algorithm-visualizer",
"name": "@algorithm-visualizer/algorithm-visualizer",
"version": "2.0.0",
"lockfileVersion": 1,
"requires": true,
......
{
"name": "algorithm-visualizer",
"name": "@algorithm-visualizer/algorithm-visualizer",
"version": "2.0.0",
"title": "Algorithm Visualizer",
"description": "Algorithm Visualizer is an interactive online platform that visualizes algorithms from code.",
......
import Promise from 'bluebird';
import axios from 'axios';
import child_process from 'child_process';
import path from 'path';
import fs from 'fs-extra';
......@@ -32,6 +33,14 @@ const getDescription = files => {
return removeMarkdown(descriptionLines.join(' '));
};
const download = (url, localPath) => axios({ url, method: 'GET', responseType: 'stream' })
.then(response => new Promise((resolve, reject) => {
const writer = fs.createWriteStream(localPath);
writer.on('finish', resolve);
writer.on('error', reject);
response.data.pipe(writer);
}));
export {
execute,
createKey,
......@@ -39,4 +48,5 @@ export {
listFiles,
listDirectories,
getDescription,
download,
};
......@@ -73,7 +73,8 @@ Object.keys(builderMap).forEach(lang => {
if (builder instanceof ImageBuilder) {
router.post(`/${lang}`, trace(lang));
} else if (builder instanceof WorkerBuilder) {
router.get(`/${lang}`, (req, res) => res.sendFile(builder.workerPath));
router.get(`/${lang}`, (req, res) => res.sendFile(builder.tracerPath));
router.get(`/${lang}/worker`, (req, res) => res.sendFile(builder.workerPath));
}
});
......
import path from 'path';
import { download } from '/common/util';
class WorkerBuilder {
constructor() {
this.tracerPath = path.resolve(__dirname, '..', 'public', 'algorithm-visualizer.js');
this.workerPath = path.resolve(__dirname, 'js', 'worker.js');
this.build = this.build.bind(this);
}
build(release) {
const { tag_name } = release;
return download(`https://github.com/algorithm-visualizer/tracers.js/releases/download/${tag_name}/algorithm-visualizer.js`, this.tracerPath);
}
}
......
const process = { env: { ALGORITHM_VISUALIZER: '1' } };
importScripts('https://unpkg.com/algorithm-visualizer@latest/dist/algorithm-visualizer.js');
importScripts('/api/tracers/js');
const sandbox = code => {
const require = name => ({ 'algorithm-visualizer': AlgorithmVisualizer }[name]); // fake require
......@@ -7,7 +7,7 @@ const sandbox = code => {
};
onmessage = e => {
const lines = e.data.split('\n').map((line, i) => line.replace(/(.+\. *delay *)(\( *\))/g, `$1(${i})`));
const lines = e.data.split('\n').map((line, i) => line.replace(/(\.\s*delay\s*)\(\s*\)/g, `$1(${i})`));
const code = lines.join('\n');
sandbox(code);
postMessage(AlgorithmVisualizer.Tracer.traces);
......
......@@ -79,7 +79,7 @@ const TracerApi = {
}]),
json: ({ code }) => new Promise(resolve => resolve(JSON.parse(code))),
js: ({ code }, params, cancelToken) => new Promise((resolve, reject) => {
const worker = new Worker('/api/tracers/js');
const worker = new Worker('/api/tracers/js/worker');
if (cancelToken) {
cancelToken.promise.then(cancel => {
worker.terminate();
......
......@@ -28,7 +28,7 @@ class Array2DRenderer extends Renderer {
}
{
longestRow.map((_, i) => (
<td className={classes(styles.col, styles.index)}>
<td className={classes(styles.col, styles.index)} key={i}>
<span className={styles.value}>{i}</span>
</td>
))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册