Merge pull request #3 from hacdias/master

fix: IPv6 rfc3986 compliancy
This commit is contained in:
Alan Shaw 2019-01-14 09:31:33 +00:00 committed by GitHub
commit 109f77ae76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -3,7 +3,11 @@ const reduceValue = (_, v) => v
const Reducers = { const Reducers = {
ip4: reduceValue, ip4: reduceValue,
ip6: reduceValue, ip6: (str, content, i, parts) => (
parts.length === 1 && parts[0].protocol === 'ip6'
? content
: `[${content}]`
),
tcp: (str, content, i, parts) => ( tcp: (str, content, i, parts) => (
parts.some(p => ['http', 'https', 'ws', 'wss'].includes(p.protocol)) parts.some(p => ['http', 'https', 'ws', 'wss'].includes(p.protocol))
? `${str}:${content}` ? `${str}:${content}`

16
test.js
View File

@ -6,13 +6,13 @@ test('should convert multiaddr to URI', (t) => {
['/ip4/127.0.0.1', '127.0.0.1'], ['/ip4/127.0.0.1', '127.0.0.1'],
['/ip4/127.0.0.1/http', 'http://127.0.0.1'], ['/ip4/127.0.0.1/http', 'http://127.0.0.1'],
['/ip6/fc00::', 'fc00::'], ['/ip6/fc00::', 'fc00::'],
['/ip6/fc00::/http', 'http://fc00::'], ['/ip6/fc00::/http', 'http://[fc00::]'],
['/ip4/0.0.7.6/tcp/1234', 'tcp://0.0.7.6:1234'], ['/ip4/0.0.7.6/tcp/1234', 'tcp://0.0.7.6:1234'],
['/ip4/0.0.7.6/tcp/1234/http', 'http://0.0.7.6:1234'], ['/ip4/0.0.7.6/tcp/1234/http', 'http://0.0.7.6:1234'],
['/ip4/0.0.7.6/tcp/1234/https', 'https://0.0.7.6:1234'], ['/ip4/0.0.7.6/tcp/1234/https', 'https://0.0.7.6:1234'],
['/ip6/::/tcp/0', 'tcp://:::0'], ['/ip6/::/tcp/0', 'tcp://[::]:0'],
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'], ['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
['/ip6/::/udp/0', 'udp://:::0'], ['/ip6/::/udp/0', 'udp://[::]:0'],
['/dnsaddr/ipfs.io', 'ipfs.io'], ['/dnsaddr/ipfs.io', 'ipfs.io'],
['/dns4/ipfs.io', 'ipfs.io'], ['/dns4/ipfs.io', 'ipfs.io'],
['/dns4/libp2p.io', 'libp2p.io'], ['/dns4/libp2p.io', 'libp2p.io'],
@ -26,10 +26,10 @@ test('should convert multiaddr to URI', (t) => {
['/dnsaddr/ipfs.io/http', 'http://ipfs.io'], ['/dnsaddr/ipfs.io/http', 'http://ipfs.io'],
['/dnsaddr/ipfs.io/https', 'https://ipfs.io'], ['/dnsaddr/ipfs.io/https', 'https://ipfs.io'],
['/ip4/1.2.3.4/tcp/3456/ws', 'ws://1.2.3.4:3456'], ['/ip4/1.2.3.4/tcp/3456/ws', 'ws://1.2.3.4:3456'],
['/ip6/::/tcp/0/ws', 'ws://:::0'], ['/ip6/::/tcp/0/ws', 'ws://[::]:0'],
['/dnsaddr/ipfs.io/wss', 'wss://ipfs.io'], ['/dnsaddr/ipfs.io/wss', 'wss://ipfs.io'],
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'], ['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
['/ip6/::/tcp/0/wss', 'wss://:::0'], ['/ip6/::/tcp/0/wss', 'wss://[::]:0'],
[ [
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo', '/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo',
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo' 'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo'
@ -44,16 +44,16 @@ test('should convert multiaddr to URI', (t) => {
], ],
[ [
'/ip6/::/tcp/0/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5', '/ip6/::/tcp/0/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5',
'ws://:::0/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5' 'ws://[::]:0/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5'
], ],
[ [
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79', '/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
'wss://wrtc-star.discovery.libp2p.io:443/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79' 'wss://wrtc-star.discovery.libp2p.io:443/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
], ],
['/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct', 'http://1.2.3.4:3456/p2p-webrtc-direct'], ['/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct', 'http://1.2.3.4:3456/p2p-webrtc-direct'],
['/ip6/::/tcp/0/http/p2p-webrtc-direct', 'http://:::0/p2p-webrtc-direct'], ['/ip6/::/tcp/0/http/p2p-webrtc-direct', 'http://[::]:0/p2p-webrtc-direct'],
['/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star', 'ws://1.2.3.4:3456/p2p-websocket-star'], ['/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'], ['/ip6/::/tcp/0/ws/p2p-websocket-star', 'ws://[::]:0/p2p-websocket-star'],
[ [
'/dnsaddr/localhost/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', '/dnsaddr/localhost/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'ws://localhost/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4' 'ws://localhost/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'