-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathutils.ts
More file actions
40 lines (34 loc) · 956 Bytes
/
utils.ts
File metadata and controls
40 lines (34 loc) · 956 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
39
40
/**
* Utility functions.
*/
import * as os from 'os';
/**
* Get the current time.
*
* @returns {String} The current time in the form YYYY-mm-ddTHH:MM:SS+00:00
*/
export function timestamp(): string {
const date = new Date().toISOString();
return date.replace(/\.\d{3}Z/, '+00:00');
}
/**
* Get all IP addresses.
*
* @returns {string[]} Array of addresses.
*/
export function getAddresses(): string[] {
const addresses = new Set<string>();
const ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach((iface) => {
ifaces[iface]!.forEach((addr) => {
const address = addr.address.toLowerCase();
// Filter out link-local addresses.
if (addr.family === 'IPv6' && !address.startsWith('fe80:')) {
addresses.add(`[${address}]`);
} else if (addr.family === 'IPv4' && !address.startsWith('169.254.')) {
addresses.add(address);
}
});
});
return Array.from(addresses).sort();
}