forked from petersalomonsen/wasm-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserverwithgithubproxy.js
54 lines (46 loc) · 1.5 KB
/
webserverwithgithubproxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Simple webserver with proxy to github.com
*/
const http = require('http');
const https = require('https');
const fs = require('fs');
function onRequest(request, response) {
let path = request.url.substring(1);
console.log(path);
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Headers', '*');
if( path.indexOf('git-upload') > -1 ||
path.indexOf('git-receive') > -1) {
const options = {
hostname: 'github.com',
port: 443,
path: request.url,
method: request.method,
};
console.log(`Proxying ${options.method} request to ${options.hostname} with path ${options.path}`);
const proxy = https.request(options, function (res) {
res.pipe(response, {
end: true
});
});
request.pipe(proxy, {
end: true
});
} else {
if(path === '') {
path = 'index.html';
}
if(fs.existsSync(path)) {
if(path.indexOf('.js') === path.length-3) {
response.setHeader('Content-Type', 'application/javascript');
} else if(path.indexOf('.wasm') === path.length-5) {
response.setHeader('Content-Type', 'application/wasm');
}
response.end(fs.readFileSync(path));
} else {
response.statusCode = 404;
response.end('');
}
}
}
http.createServer(onRequest).listen(5000);