Skip to content

Commit 31ee7ec

Browse files
committed
Download traces lib
1 parent dfec3da commit 31ee7ec

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/backend/apis/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import Promise from 'bluebird';
22
import axios from 'axios';
3+
import fs from 'fs';
4+
import { githubClientId, githubClientSecret } from '/environment';
5+
6+
axios.interceptors.request.use(request => {
7+
request.params = { client_id: githubClientId, client_secret: githubClientSecret, ...request.params };
8+
return request;
9+
});
310

411
axios.interceptors.response.use(response => {
512
return response.data;
@@ -54,9 +61,18 @@ const PATCH = URL => {
5461
};
5562

5663
const GitHubApi = {
57-
auth: (client_id, client_secret) => axios.defaults.params = { client_id, client_secret },
5864
listCommits: GET('/repos/:owner/:repo/commits'),
5965
getAccessToken: code => axios.post('https://github.com/login/oauth/access_token', { code }, { headers: { Accept: 'application/json' } }),
66+
getLatestRelease: GET('/repos/:owner/:repo/releases/latest'),
67+
download: (url, path) => axios({
68+
method: 'get',
69+
url,
70+
responseType: 'stream',
71+
}).then(data => new Promise((resolve, reject) => {
72+
data.pipe(fs.createWriteStream(path));
73+
data.on('end', resolve);
74+
data.on('error', reject);
75+
})),
6076
};
6177

6278
export {

src/backend/controllers/compilers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import Promise from 'bluebird';
12
import express from 'express';
23
import path from 'path';
4+
import { GitHubApi } from '/apis';
35

46
const router = express.Router();
57

8+
const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'libs', ...args);
9+
10+
const downloadLibs = () => {
11+
GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(release => {
12+
return Promise.each(release.assets, asset => GitHubApi.download(asset.browser_download_url, getPath(asset.name)));
13+
});
14+
};
15+
downloadLibs(); // TODO: download again when webhooked
16+
617
const getJsWorker = (req, res, next) => {
718
res.sendFile(path.resolve(__dirname, '..', 'tracers', 'languages', 'js', 'build', 'index.js'));
819
};

src/backend/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import cookieParser from 'cookie-parser';
44
import bodyParser from 'body-parser';
55
import * as controllers from '/controllers';
66
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
7-
import { GitHubApi } from '/apis';
8-
import { githubClientId, githubClientSecret } from '/environment';
97

10-
GitHubApi.auth(githubClientId, githubClientSecret);
118
const app = express();
129
app.use(morgan('tiny'));
1310
app.use(cookieParser());

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy