Repository navigation
Expand file tree
/
Copy pathindex.mjs
More file actions
80 lines (64 loc) · 1.92 KB
/
Copy pathindex.mjs
File metadata and controls
80 lines (64 loc) · 1.92 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// import path from 'path';
import fs from 'fs';
import http from 'http';
import https from 'https';
// import url from 'url';
// import child_process from 'child_process';
import express from 'express';
import {
handler,
} from './handler.js';
//
const isProduction = process.env.NODE_ENV === 'production';
const vercelJson = JSON.parse(fs.readFileSync('./vercel.json', 'utf8'));
const _tryReadFile = p => {
try {
return fs.readFileSync(p);
} catch(err) {
// console.warn(err);
return null;
}
};
const certs = {
key: _tryReadFile('./certs/privkey.pem') || _tryReadFile('./certs-local/privkey.pem'),
cert: _tryReadFile('./certs/fullchain.pem') || _tryReadFile('./certs-local/fullchain.pem'),
};
//
const SERVER_NAME = `local-compiler.webaverse.com`;
const port = parseInt(process.env.PORT, 10) || 443;
console.log('compiler port', port);
//
const {headers: headerSpecs} = vercelJson;
const headerSpec0 = headerSpecs[0];
const {headers} = headerSpec0;
const _setHeaders = res => {
for (const {key, value} of headers) {
res.setHeader(key, value);
}
};
//
(async () => {
const app = express();
app.all('*', async (req, res, next) => {
// console.log('compiler got request', req);
_setHeaders(res);
// const {pathname} = url.parse(req.url);
if (req.method === 'OPTIONS') {
res.end();
} else {
handler(req, res);
}
});
const isHttps = false; // !process.env.HTTP_ONLY && (!!certs.key && !!certs.cert);
const _makeHttpServer = () => isHttps ? https.createServer(certs, app) : http.createServer(app);
const httpServer = _makeHttpServer();
await new Promise((accept, reject) => {
console.log('compiler listen on port', {port});
httpServer.listen(port, '0.0.0.0', () => {
accept();
});
httpServer.on('error', reject);
});
// console.log('pid', process.pid);
console.log(` > Compiler ready: http${isHttps ? 's' : ''}://${SERVER_NAME}:${port}/`);
})();