mirror of
https://github.com/fluencelabs/js-multiaddr-to-uri
synced 2024-12-03 21:30:24 +00:00
feat: retain aliased protocols (#11)
The upgrade to multiaddr 7 brings with it a change that means multiaddrs with libp2p peer ID keys like `/ipfs/QmXXX` will be transformed to `/p 2p/QmXXX` when converted from a string, to a multiaddr instance and back to a string. This is because `p2p` and `ipfs` are name aliases for the same codec. `p2p` has now become the default and older `ipfs` names do not round trip(they are converted to `p2p` - as stated above). This change allows us to use the new multiaddr 7 lib but retain backward compatibility by ensuring the protocol specified in a string multiaddr is the protocol used in the resulting URI.
This commit is contained in:
parent
5afa996d08
commit
3021c00543
18
index.js
18
index.js
@ -10,8 +10,8 @@ const tcpUri = (str, port, parts, opts) => {
|
||||
const last = parts[parts.length - 1]
|
||||
if (last.protocol === 'tcp') {
|
||||
// assume http and produce clean urls
|
||||
protocol = port === 443 ? 'https' : 'http'
|
||||
explicitPort = port === 443 || port === 80 ? '' : explicitPort
|
||||
protocol = port === '443' ? 'https' : 'http'
|
||||
explicitPort = port === '443' || port === '80' ? '' : explicitPort
|
||||
}
|
||||
return `${protocol}://${str}${explicitPort}`
|
||||
}
|
||||
@ -43,16 +43,18 @@ const Reducers = {
|
||||
'p2p-webrtc-direct': str => `${str}/p2p-webrtc-direct`
|
||||
}
|
||||
|
||||
module.exports = (multiaddr, opts) => (
|
||||
Multiaddr(multiaddr)
|
||||
.stringTuples()
|
||||
module.exports = (multiaddr, opts) => {
|
||||
const ma = Multiaddr(multiaddr)
|
||||
const parts = multiaddr.toString().split('/').slice(1)
|
||||
return ma
|
||||
.tuples()
|
||||
.map(tuple => ({
|
||||
protocol: Multiaddr.protocols.codes[tuple[0]].name,
|
||||
content: tuple[1]
|
||||
protocol: parts.shift(),
|
||||
content: tuple[1] ? parts.shift() : null
|
||||
}))
|
||||
.reduce((str, part, i, parts) => {
|
||||
const reduce = Reducers[part.protocol]
|
||||
if (!reduce) throw new Error(`Unsupported protocol ${part.protocol}`)
|
||||
return reduce(str, part.content, i, parts, opts)
|
||||
}, '')
|
||||
)
|
||||
}
|
||||
|
2404
package-lock.json
generated
2404
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -16,12 +16,12 @@
|
||||
"author": "Alan Shaw",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"ava": "^2.3.0",
|
||||
"nyc": "^14.1.1",
|
||||
"standard": "^14.0.0"
|
||||
"ava": "^2.4.0",
|
||||
"nyc": "^15.0.0",
|
||||
"standard": "^14.3.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"multiaddr": "^6.1.0"
|
||||
"multiaddr": "^7.2.1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
36
test.js
36
test.js
@ -26,16 +26,16 @@ test('should convert multiaddr to URI', (t) => {
|
||||
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
|
||||
['/ip6/::/tcp/0/wss', 'wss://[::]:0'],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo',
|
||||
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo'
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmWo6sLpfuhLnPNF1d2X6s9PXC5NvsRbC69uvHAJhZW9bk',
|
||||
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmWo6sLpfuhLnPNF1d2X6s9PXC5NvsRbC69uvHAJhZW9bk'
|
||||
],
|
||||
[
|
||||
'/dnsaddr/ipfs.io/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://ipfs.io/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
'/dnsaddr/ipfs.io/ws/p2p-webrtc-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'ws://ipfs.io/p2p-webrtc-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
],
|
||||
[
|
||||
'/dnsaddr/ipfs.io/wss/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'wss://ipfs.io/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
'/dnsaddr/ipfs.io/wss/p2p-webrtc-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'wss://ipfs.io/p2p-webrtc-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
],
|
||||
[
|
||||
'/ip6/::/tcp/0/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5',
|
||||
@ -50,28 +50,32 @@ test('should convert multiaddr to URI', (t) => {
|
||||
['/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star', 'ws://1.2.3.4:3456/p2p-websocket-star'],
|
||||
['/ip6/::/tcp/0/ws/p2p-websocket-star', 'ws://[::]:0/p2p-websocket-star'],
|
||||
[
|
||||
'/dnsaddr/localhost/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://localhost/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
'/dnsaddr/localhost/ws/p2p-websocket-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'ws://localhost/p2p-websocket-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://1.2.3.4:3456/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'ws://1.2.3.4:3456/p2p-websocket-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
],
|
||||
[
|
||||
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3',
|
||||
'wss://ws-star.discovery.libp2p.io:443/p2p-websocket-star/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3'
|
||||
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/QmP3vadpN9dqZ7j6KtmwP5Y4prg7XqdS7ixgZMWtXxBAbp',
|
||||
'wss://ws-star.discovery.libp2p.io:443/p2p-websocket-star/ipfs/QmP3vadpN9dqZ7j6KtmwP5Y4prg7XqdS7ixgZMWtXxBAbp'
|
||||
],
|
||||
[
|
||||
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
|
||||
'ws://127.0.0.1:20008/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'tcp://1.2.3.4:3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'tcp://1.2.3.4:3456/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
|
||||
'tcp://1.2.3.4:3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
|
||||
]
|
||||
]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user