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 Sep 4, 2017
1 parent 0508384 commit b1e01b9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 22 deletions.
52 changes: 39 additions & 13 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(WebSockets, base('p2p-webrtc-star'), base('ipfs')),
and(WebSocketsSecure, base('p2p-webrtc-star'), base('ipfs'))
and(_WebSockets, base('p2p-webrtc-star'), base('ipfs')),
and(_WebSocketsSecure, base('p2p-webrtc-star'), base('ipfs'))
)

const WebSocketsStar = or(
Expand All @@ -63,17 +93,14 @@ const Reliable = or(
)

let _IPFS = or(
and(Reliable, base('ipfs')),
WebRTCStar,
Reliable,
base('ipfs')
)

const _Circuit = or(
and(_IPFS, base('p2p-circuit'), _IPFS),
and(_IPFS, base('p2p-circuit')),
and(base('p2p-circuit'), _IPFS),
and(Reliable, base('p2p-circuit')),
and(base('p2p-circuit'), Reliable),
base('p2p-circuit')
)

Expand Down Expand Up @@ -102,7 +129,6 @@ exports.UTP = UTP
exports.HTTP = HTTP
exports.WebSockets = WebSockets
exports.WebSocketsSecure = WebSocketsSecure
exports.WebSocketsSecureIPFS = WebSocketsSecureIPFS
exports.WebSocketsStar = WebSocketsStar
exports.WebRTCStar = WebRTCStar
exports.WebRTCDirect = WebRTCDirect
Expand Down
54 changes: 45 additions & 9 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 @@ -104,7 +130,9 @@ describe('multiaddr validation', function () {
'/p2p-circuit',
'/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/p2p-circuit/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
// TODO: figure out how to make circuit addrs and /p2p-webrtc-star work together
// since they don't add to tuples when used together
// '/p2p-circuit/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/p2p-circuit/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA',
'/p2p-circuit/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA',
Expand All @@ -121,12 +149,15 @@ describe('multiaddr validation', function () {
]

const goodIPFS = [
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
// TODO: figure out how to make circuit addrs and /p2p-webrtc-star work together
// since they don't add to tuples when used together
// '/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit',
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
].concat(goodCircuit)
]

function assertMatches (p) {
const tests = Array.from(arguments).slice(1)
Expand All @@ -151,6 +182,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 Expand Up @@ -207,6 +243,6 @@ describe('multiaddr validation', function () {
})

it('IPFS validation', function () {
assertMatches(mafmt.IPFS, goodIPFS)
assertMatches(mafmt.IPFS, goodIPFS, goodCircuit)
})
})

0 comments on commit b1e01b9

Please sign in to comment.