-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiaddr.js
79 lines (71 loc) · 2.39 KB
/
multiaddr.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
// MIRRORED FROM https://github.com/filecoin-station/spark/blob/main/lib/multiaddr.js
// @ts-nocheck
/**
* @param {string} addr Multiaddr, e.g. `/ip4/127.0.0.1/tcp/80/http`
* @returns {string} Parsed URI, e.g. `http://127.0.0.1:80`
*/
export function multiaddrToHttpUrl (addr) {
const [multiAddr, httpPathMultiAddr] = addr.split('/http-path')
const [, hostType, hostValue, ...multiAddrParts] = multiAddr.split('/')
let scheme, path, rest, port
if (addr.includes('/http-path')) {
[scheme, ...rest] = multiAddrParts
try {
// Remove leading slash and parse URI-encoded path
// See https://github.com/multiformats/multiaddr/blob/cab92e8e6da2e70c5f1b8aa59976e71e6922b392/protocols/http-path.md#usage
path = decodeURIComponent(httpPathMultiAddr.substring(1))
} catch (err) {
throw Object.assign(
new Error(`Cannot parse "${addr}": unsupported http path`, { cause: err }),
{ code: 'INVALID_HTTP_PATH' }
)
}
} else {
let ipProtocol
;[ipProtocol, port, scheme, ...rest] = multiAddrParts
if (ipProtocol !== 'tcp') {
throw Object.assign(
new Error(`Cannot parse "${addr}": unsupported protocol "${ipProtocol}"`),
{ code: 'UNSUPPORTED_MULTIADDR_PROTO' }
)
}
}
if (scheme !== 'http' && scheme !== 'https') {
throw Object.assign(
new Error(`Cannot parse "${addr}": unsupported scheme "${scheme}"`),
{ code: 'UNSUPPORTED_MULTIADDR_SCHEME' }
)
}
if (rest.length) {
throw Object.assign(
new Error(`Cannot parse "${addr}": too many parts`),
{ code: 'MULTIADDR_HAS_TOO_MANY_PARTS' }
)
}
let url = `${scheme}://${getUriHost(hostType, hostValue)}`
if (port) url += getUriPort(scheme, port)
if (path) url += path
return url
}
function getUriHost (hostType, hostValue) {
switch (hostType) {
case 'ip4':
case 'dns':
case 'dns4':
case 'dns6':
return hostValue
case 'ip6':
// See https://superuser.com/a/367788/135774:
// According to RFC2732, literal IPv6 addresses should be put inside square brackets in URLs
return `[${hostValue}]`
}
throw Object.assign(
new Error(`Unsupported multiaddr host type "${hostType}"`),
{ code: 'UNSUPPORTED_MULTIADDR_HOST_TYPE' }
)
}
function getUriPort (scheme, port) {
if (scheme === 'http' && port === '80') return ''
if (scheme === 'https' && port === '443') return ''
return `:${port}`
}