Skip to content

Commit

Permalink
Remove dependencies on unpkg.com (algorithm-visualizer#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
64json committed Feb 5, 2019
1 parent ece2847 commit 7271b34
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"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.",
Expand Down
10 changes: 10 additions & 0 deletions src/backend/common/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Promise from 'bluebird';
import axios from 'axios';
import child_process from 'child_process';
import path from 'path';
import fs from 'fs-extra';
Expand Down Expand Up @@ -32,11 +33,20 @@ 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,
isDirectory,
listFiles,
listDirectories,
getDescription,
download,
};
3 changes: 2 additions & 1 deletion src/backend/controllers/tracers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/backend/tracers/WorkerBuilder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/backend/tracers/js/worker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
eval(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);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/core/renderers/Array2DRenderer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
))
Expand Down

0 comments on commit 7271b34

Please sign in to comment.