-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (27 loc) · 800 Bytes
/
index.js
File metadata and controls
31 lines (27 loc) · 800 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
import addrToIPPort from 'addr-to-ip-port'
import ipaddr from 'ipaddr.js'
const addrs = addrs => {
if (typeof addrs === 'string') {
addrs = [addrs]
}
return Buffer.concat(addrs.map(addr => {
const s = addrToIPPort(addr)
if (s.length !== 2) {
throw new Error('invalid address format, expecting: [IP]:[PORT]')
}
const ip = ipaddr.parse(s[0])
const ipBuf = Buffer.from(ip.toByteArray())
const port = s[1]
const portBuf = Buffer.allocUnsafe(2)
portBuf.writeUInt16BE(port, 0)
return Buffer.concat([ipBuf, portBuf])
}))
}
/**
* Also support this usage:
* string2compact.multi([ '10.10.10.5:128', '100.56.58.99:28525' ])
*
* for parallelism with the `compact2string` module.
*/
export default addrs
export { addrs as multi, addrs as multi6 }