-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.js
48 lines (41 loc) · 1.38 KB
/
util.js
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
var os
try {
os = require('os')
} catch (_) {
// Uncaught because this should work in a browser.
}
const metaAddresses = [
'0.0.0.0',
'::'
]
const getNetworkAddresses = ({ internal, family } = {}) =>
Object.values(os.networkInterfaces())
// Flatten
.reduce((acc, val) => acc.concat(val), [])
// Filter `internal`
.filter(item => internal == null || item.internal === internal)
// Filter `family`
.filter(item => family == null || item.family === family)
// Filter scoped IPv6 addresses, which don't play nicely with Node.js
.filter(item => item.scopeid == null || item.scopeid === 0)
// Only return the address.
.map(item => item.address)
// Choose a dynamic port between 49152 and 65535
// https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic,_private_or_ephemeral_ports
module.exports.getRandomPort = () =>
Math.floor(49152 + (65535 - 49152 + 1) * Math.random())
module.exports.protocolToAddress = (protocol) =>
(host, port) => [protocol, host, port ].join(':')
// returns array of hosts
module.exports.getAddresses = (host, scope) => {
if (scope === 'device') {
return [ 'localhost' ] // legacy
}
if (typeof host === 'string' && metaAddresses.includes(host) === false) {
return [ host ]
}
return getNetworkAddresses({
internal: (scope === 'device'),
family: (host === '0.0.0.0' ? 'IPv4' : null)
})
}