-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathutils.ts
More file actions
38 lines (31 loc) · 954 Bytes
/
utils.ts
File metadata and controls
38 lines (31 loc) · 954 Bytes
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
import { GeneralError } from '../errors/runtime';
import { RUNTIME_ERRORS } from '../errors/types';
import {
isMyHostname,
getIPAddress,
isFreePort,
getFreePort,
} from '../utils/endpoint-utils';
export async function getValidHostname (hostname: string): Promise<string> {
if (hostname) {
const valid = await isMyHostname(hostname);
if (!valid)
throw new GeneralError(RUNTIME_ERRORS.invalidHostname, hostname);
}
else {
hostname = getIPAddress() ?? '';
if (!hostname)
throw new GeneralError(RUNTIME_ERRORS.invalidHostname, hostname);
}
return hostname;
}
export async function getValidPort (port: number): Promise<number> {
if (port >= 0) {
const isFree = await isFreePort(port);
if (!isFree)
throw new GeneralError(RUNTIME_ERRORS.portIsNotFree, port);
}
else
port = await getFreePort();
return port;
}