-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
89 lines (80 loc) · 2.26 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const isIp = require('is-ip')
const Multiaddr = require('multiaddr')
/**
* Convert a URI to a multiaddr
*
* http://foobar.com => /dns4/foobar.com/tcp/80/http
* https://foobar.com => /dns4/foobar.com/tcp/443/https
* https://foobar.com:5001 => /dns4/foobar.com/tcp/5001/https
* https://127.0.0.1:8080 => /ip4/127.0.0.1/tcp/8080/https
* http://[::1]:8080 => /ip6/::1/tcp/8080/http
* tcp://foobar.com:8080 => /dns4/foobar.com/tcp/8080
* udp://foobar.com:8080 => /dns4/foobar.com/udp/8080
*/
function multiaddrFromUri (uriStr, opts) {
opts = opts || {}
const defaultDnsType = opts.defaultDnsType || 'dns4'
const { scheme, hostname, port } = parseUri(uriStr)
const parts = [
tupleForHostname(hostname, defaultDnsType),
tupleForPort(port, scheme),
tupleForScheme(scheme)
]
const multiaddrStr = '/' + parts
.filter(x => !!x)
.reduce((a, b) => a.concat(b))
.join('/')
return Multiaddr(multiaddrStr)
}
function parseUri (uriStr) {
// Use the WHATWG URL global, in node >= 10 and the browser
let { protocol, hostname, port } = new URL(uriStr)
const scheme = protocol.slice(0, -1)
if (!port && port !== 0) {
port = portForProtocol(scheme)
}
return { scheme, hostname, port }
}
function tupleForHostname (hostname, defaultDnsType) {
if (!hostname) return null
if (isIp.v4(hostname)) {
return ['ip4', hostname]
}
if (isIp.v6(hostname)) {
return ['ip6', hostname]
}
// literal ipv6 in url should be wrapped in square brackets [x:y:z]
// https://www.ietf.org/rfc/rfc2732.txt
if (hostname[0] === '[') {
const trimmed = hostname.substring(1, hostname.length - 1)
if (isIp.v6(trimmed)) {
return ['ip6', trimmed]
}
}
// assumes that any non-ip hostname is a dns4 address.
return [defaultDnsType, hostname]
}
function tupleForPort (port, scheme) {
if (!port) return null
if (scheme === 'udp') {
return ['udp', port]
}
return ['tcp', port]
}
function tupleForScheme (scheme) {
if (scheme.match(/^tcp$|^udp$/)) {
return null
}
return [scheme]
}
function portForProtocol (protocol) {
if (!protocol) return null
const portFor = {
http: '80',
https: '443',
ws: '80',
wss: '443'
}
return portFor[protocol]
}
module.exports = multiaddrFromUri