-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathauthentication-server.ts
67 lines (57 loc) · 1.75 KB
/
authentication-server.ts
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
import http from 'node:http';
import url from 'node:url';
import { body } from './body';
import { authServerPort } from '../../common/protocol/authentication-service';
export const authCallbackPath = 'callback';
export function createServer(
authCallback: (req: http.IncomingMessage, res: http.ServerResponse) => void
) {
const server = http.createServer(function (req, res) {
const reqUrl = url.parse(req.url!, /* parseQueryString */ true);
switch (reqUrl.pathname) {
case `/${authCallbackPath}`:
authCallback(req, res);
res.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/html; charset=utf-8',
});
res.end(body);
break;
default:
res.writeHead(404);
res.end();
break;
}
});
return server;
}
export async function startServer(server: http.Server): Promise<string> {
let portTimer: NodeJS.Timeout;
function cancelPortTimer() {
clearTimeout(portTimer);
}
const port = new Promise<string>((resolve, reject) => {
portTimer = setTimeout(() => {
reject(new Error('Timeout waiting for port'));
}, 5000);
server.on('listening', () => {
const address = server.address();
if (typeof address === 'undefined' || address === null) {
reject(new Error('address is null or undefined'));
} else if (typeof address === 'string') {
resolve(address);
} else {
resolve(address.port.toString());
}
});
server.on('error', (_) => {
reject(new Error('Error listening to server'));
});
server.on('close', () => {
reject(new Error('Closed'));
});
server.listen(authServerPort);
});
port.then(cancelPortTimer, cancelPortTimer);
return port;
}