-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
71 lines (69 loc) · 1.9 KB
/
utils.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
68
69
70
71
import { Express } from "express";
import chalk from "chalk";
export async function httpServerStart(
httpServer: Express,
port: number
): Promise<any> {
const initialPort = port;
return new Promise((resolve, reject) => {
const server = httpServer.listen(port, () => {
console.log(
chalk.greenBright(`App started on port`, `${chalk.bold(`${port}`)}...`)
);
resolve(server);
});
const onError = (e: Error & { code?: string }) => {
if (e.code === "EADDRINUSE") {
console.info(
chalk.yellow(
`Port ${chalk.bold(`${port}`)} is in use, trying another one...`
)
);
if (port - initialPort >= 30) {
console.error(
"port difference exceeded 30 please restart sserver with an open port"
);
process.exitCode = 1;
}
server.listen(++port);
} else {
reject(e);
}
};
server.on("error", onError);
});
}
export async function hostedHttpServerStart(
httpServer: Express,
localIp: string,
port: number
): Promise<any> {
const initialPort = ++port;
return new Promise((resolve, reject) => {
const server = httpServer.listen(port, localIp, () => {
console.log(
chalk.greenBright(`Network:`, `${chalk.bold(`${localIp}:${port}`)}...`)
);
resolve(server);
});
const onError = (e: Error & { code?: string }) => {
if (e.code === "EADDRINUSE") {
console.info(
chalk.yellow(
`Port ${chalk.bold(`${port}`)} is in use, trying another one...`
)
);
if (port - initialPort >= 30) {
console.error(
"port difference exceeded 30 please restart sserver with an open port"
);
process.exitCode = 1;
}
server.listen(++port, localIp);
} else {
reject(e);
}
};
server.on("error", onError);
});
}