Skip to content

Commit 7271b34

Browse files
committed
Remove dependencies on unpkg.com (algorithm-visualizer#234)
1 parent ece2847 commit 7271b34

File tree

8 files changed

+22
-7
lines changed

8 files changed

+22
-7
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "algorithm-visualizer",
2+
"name": "@algorithm-visualizer/algorithm-visualizer",
33
"version": "2.0.0",
44
"title": "Algorithm Visualizer",
55
"description": "Algorithm Visualizer is an interactive online platform that visualizes algorithms from code.",

src/backend/common/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Promise from 'bluebird';
2+
import axios from 'axios';
23
import child_process from 'child_process';
34
import path from 'path';
45
import fs from 'fs-extra';
@@ -32,11 +33,20 @@ const getDescription = files => {
3233
return removeMarkdown(descriptionLines.join(' '));
3334
};
3435

36+
const download = (url, localPath) => axios({ url, method: 'GET', responseType: 'stream' })
37+
.then(response => new Promise((resolve, reject) => {
38+
const writer = fs.createWriteStream(localPath);
39+
writer.on('finish', resolve);
40+
writer.on('error', reject);
41+
response.data.pipe(writer);
42+
}));
43+
3544
export {
3645
execute,
3746
createKey,
3847
isDirectory,
3948
listFiles,
4049
listDirectories,
4150
getDescription,
51+
download,
4252
};

src/backend/controllers/tracers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Object.keys(builderMap).forEach(lang => {
7373
if (builder instanceof ImageBuilder) {
7474
router.post(`/${lang}`, trace(lang));
7575
} else if (builder instanceof WorkerBuilder) {
76-
router.get(`/${lang}`, (req, res) => res.sendFile(builder.workerPath));
76+
router.get(`/${lang}`, (req, res) => res.sendFile(builder.tracerPath));
77+
router.get(`/${lang}/worker`, (req, res) => res.sendFile(builder.workerPath));
7778
}
7879
});
7980

src/backend/tracers/WorkerBuilder.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import path from 'path';
2+
import { download } from '/common/util';
23

34
class WorkerBuilder {
45
constructor() {
6+
this.tracerPath = path.resolve(__dirname, '..', 'public', 'algorithm-visualizer.js');
57
this.workerPath = path.resolve(__dirname, 'js', 'worker.js');
68

79
this.build = this.build.bind(this);
810
}
911

1012
build(release) {
13+
const { tag_name } = release;
14+
return download(`https://github.com/algorithm-visualizer/tracers.js/releases/download/${tag_name}/algorithm-visualizer.js`, this.tracerPath);
1115
}
1216
}
1317

src/backend/tracers/js/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const process = { env: { ALGORITHM_VISUALIZER: '1' } };
2-
importScripts('https://unpkg.com/algorithm-visualizer@latest/dist/algorithm-visualizer.js');
2+
importScripts('/api/tracers/js');
33

44
const sandbox = code => {
55
const require = name => ({ 'algorithm-visualizer': AlgorithmVisualizer }[name]); // fake require
66
eval(code);
77
};
88

99
onmessage = e => {
10-
const lines = e.data.split('\n').map((line, i) => line.replace(/(.+\. *delay *)(\( *\))/g, `$1(${i})`));
10+
const lines = e.data.split('\n').map((line, i) => line.replace(/(\.\s*delay\s*)\(\s*\)/g, `$1(${i})`));
1111
const code = lines.join('\n');
1212
sandbox(code);
1313
postMessage(AlgorithmVisualizer.Tracer.traces);

src/frontend/apis/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const TracerApi = {
7979
}]),
8080
json: ({ code }) => new Promise(resolve => resolve(JSON.parse(code))),
8181
js: ({ code }, params, cancelToken) => new Promise((resolve, reject) => {
82-
const worker = new Worker('/api/tracers/js');
82+
const worker = new Worker('/api/tracers/js/worker');
8383
if (cancelToken) {
8484
cancelToken.promise.then(cancel => {
8585
worker.terminate();

src/frontend/core/renderers/Array2DRenderer/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Array2DRenderer extends Renderer {
2828
}
2929
{
3030
longestRow.map((_, i) => (
31-
<td className={classes(styles.col, styles.index)}>
31+
<td className={classes(styles.col, styles.index)} key={i}>
3232
<span className={styles.value}>{i}</span>
3333
</td>
3434
))

0 commit comments

Comments
 (0)