-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwww.ts
More file actions
executable file
·79 lines (66 loc) · 2.24 KB
/
Copy pathwww.ts
File metadata and controls
executable file
·79 lines (66 loc) · 2.24 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
import http from 'http';
import app from '../app';
import { getLoggers } from '../utils/loggingHelpers';
import { PORT } from '../utils/process';
const server = http.createServer(app);
const { localLogger, errorLogger } = getLoggers();
const processTraceId = { traceId: 'process', uidTraceId: 'process' };
function isHttpError(error: Error): error is Error & { syscall: string; code: string } {
return Object.prototype.hasOwnProperty.call(error, 'syscall') && Object.prototype.hasOwnProperty.call(error, 'code');
}
process.on('SIGINT', () => {
process.exit();
});
// Diagnostic only: per-handler try/catch is the real defence. This listener exists
// so any rejection that still escapes gets logged to the monitoring pipeline before
// Node's default behaviour takes over. We deliberately do NOT add an uncaughtException
// handler — Node's docs are explicit that resuming after one leaves the process in
// an undefined state; the pod supervisor will restart us cleanly instead.
process.on('unhandledRejection', (reason) => {
errorLogger.error(`Unhandled rejection: ${reason instanceof Error ? (reason.stack ?? reason.message) : String(reason)}`, processTraceId);
});
/**
* Event listener for HTTP server "error" event.
*/
function onError(error: Error) {
if (!isHttpError(error)) {
throw error;
}
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof PORT === 'string'
? `Pipe ${PORT}`
: `Port ${PORT}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
localLogger.log('error', `${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
localLogger.log('error', `${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
const addr = server.address();
if (!addr) {
localLogger.log('error', 'No address available');
return;
}
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `port ${addr.port}`;
localLogger.log('info', `Listening on ${bind}`);
}
app.set('port', PORT);
server.listen(PORT);
server.on('error', onError);
server.on('listening', onListening);