-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathindex.ts
More file actions
37 lines (30 loc) · 1.03 KB
/
index.ts
File metadata and controls
37 lines (30 loc) · 1.03 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
/* eslint-disable no-console */
import { readFile } from 'node:fs/promises';
import { createServer } from 'node:http';
import { createServer as createSecureServer } from 'node:https';
import selfsigned from 'selfsigned';
import handleServe from 'serve-handler';
import { handleEsm } from './handleEsm';
const {
// eslint-disable-next-line no-magic-numbers
env: { PORT = 5081, PORTS = 5443 }
} = process;
(async () => {
const config = JSON.parse(await readFile('./serve.json', 'utf8'));
const attrs = [{ name: 'commonName', value: 'webchat2' }];
const pems = selfsigned.generate(attrs, { days: 365 });
const handler = (req, res) => {
if (req.url.startsWith('/esm/')) {
return handleEsm(req, res);
}
return handleServe(req, res, config);
};
createSecureServer(
{
cert: pems.cert,
key: pems.private
},
handler
).listen(PORTS, () => console.log(`Listening to port ${PORTS} (secure).`));
createServer(handler).listen(PORT, () => console.log(`Listening to port ${PORT} (insecure).`));
})();