Skip to content

Commit

Permalink
feat: allow /ipfs to come as part of multiaddrs
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Aug 29, 2017
1 parent 8a05396 commit 3f192ba
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
48 changes: 39 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,64 @@ const _DNS = or(
)

const IP = or(base('ip4'), base('ip6'))
const TCP = and(IP, base('tcp'))
const UDP = and(IP, base('udp'))
const UTP = and(UDP, base('utp'))

const DNS = or(
const _UDP = and(IP, base('udp'))
const UDP = or(
and(_UDP, base('ipfs')),
_UDP
)

const _UTP = and(UDP, base('utp'))
const UTP = or(
and(_UTP, base('ipfs')),
_UTP
)

const _TCP = and(IP, base('tcp'))
const TCP = or(
and(_TCP, base('ipfs')),
_TCP
)

const __DNS = or(
and(_DNS, base('tcp')),
_DNS
)

const WebSockets = or(
const DNS = or(
and(__DNS, base('ipfs')),
__DNS
)

const _WebSockets = or(
and(TCP, base('ws')),
and(DNS, base('ws'))
)

const WebSocketsSecure = or(
const WebSockets = or(
and(_WebSockets, base('ipfs')),
_WebSockets
)

const _WebSocketsSecure = or(
and(TCP, base('wss')),
and(DNS, base('wss'))
)

const WebSocketsSecure = or(
and(_WebSocketsSecure, base('ipfs')),
_WebSocketsSecure
)

const HTTP = or(
and(TCP, base('http')),
and(DNS),
and(DNS, base('http'))
)

const WebRTCStar = or(
and(base('libp2p-webrtc-star'), WebSockets, base('ipfs')),
and(base('libp2p-webrtc-star'), WebSocketsSecure, base('ipfs'))
and(base('libp2p-webrtc-star'), _WebSockets, base('ipfs')),
and(base('libp2p-webrtc-star'), _WebSocketsSecure, base('ipfs'))
)

const WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP)
Expand All @@ -57,7 +87,7 @@ const Reliable = or(
)

let _IPFS = or(
and(Reliable, base('ipfs')),
Reliable,
WebRTCStar,
base('ipfs')
)
Expand Down
41 changes: 36 additions & 5 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ describe('multiaddr validation', function () {
'/ip4/127.0.0.1'
]

const goodDnsIPFS = [
'/dns/ipfs.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/ipfs.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/libp2p.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns6/protocol.ai/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns6/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badDnsIPFS = [
'/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodIP = [
'/ip4/0.0.0.0',
'/ip6/fc00::'
Expand All @@ -32,7 +46,9 @@ describe('multiaddr validation', function () {

const goodTCP = [
'/ip4/0.0.7.6/tcp/1234',
'/ip6/::/tcp/0'
'/ip6/::/tcp/0',
'/ip4/0.0.7.6/tcp/1234/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badTCP = [
Expand All @@ -42,7 +58,9 @@ describe('multiaddr validation', function () {

const goodUDP = [
'/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0'
'/ip6/::/udp/0',
'/ip4/0.0.7.6/udp/1234/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/udp/0/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badUDP = [
Expand All @@ -52,7 +70,9 @@ describe('multiaddr validation', function () {

const goodUTP = [
'/ip4/1.2.3.4/udp/3456/utp',
'/ip6/::/udp/0/utp'
'/ip6/::/udp/0/utp',
'/ip4/1.2.3.4/udp/3456/utp/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/udp/0/utp/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badUTP = [
Expand All @@ -63,13 +83,19 @@ describe('multiaddr validation', function () {
const goodWS = [
'/dns/ipfs.io/ws',
'/ip4/1.2.3.4/tcp/3456/ws',
'/ip6/::/tcp/0/ws'
'/ip6/::/tcp/0/ws',
'/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodWSS = [
'/dns/ipfs.io/wss',
'/ip4/1.2.3.4/tcp/3456/wss',
'/ip6/::/tcp/0/wss'
'/ip6/::/tcp/0/wss',
'/dns/ipfs.io/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodWebRTCStar = [
Expand Down Expand Up @@ -147,6 +173,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.DNS, badDNS, badIP, goodTCP)
})

it('DNS IPFS validation', function () {
assertMatches(mafmt.DNS, goodDnsIPFS)
assertMismatches(mafmt.DNS, badDnsIPFS, badDNS, badIP, goodTCP)
})

it('IP validation', function () {
assertMatches(mafmt.IP, goodIP)
assertMismatches(mafmt.IP, badIP, goodTCP)
Expand Down

0 comments on commit 3f192ba

Please sign in to comment.