2016-01-19 17:23:52 +01:00
|
|
|
var multiaddr = require('multiaddr')
|
|
|
|
|
|
|
|
var IP = or(base('ip4'), base('ip6'))
|
|
|
|
var TCP = and(IP, base('tcp'))
|
|
|
|
var UDP = and(IP, base('udp'))
|
|
|
|
var UTP = and(UDP, base('utp'))
|
2016-03-12 12:36:19 +00:00
|
|
|
var WebSockets = and(TCP, base('websockets'))
|
2016-01-19 17:23:52 +01:00
|
|
|
var Reliable = or(TCP, UTP)
|
|
|
|
var IPFS = and(Reliable, base('ipfs'))
|
|
|
|
|
|
|
|
exports.IP = IP
|
|
|
|
exports.TCP = TCP
|
|
|
|
exports.UDP = UDP
|
|
|
|
exports.UTP = UTP
|
2016-03-12 12:36:19 +00:00
|
|
|
exports.WebSockets = WebSockets
|
2016-01-19 17:23:52 +01:00
|
|
|
exports.Reliable = Reliable
|
|
|
|
exports.IPFS = IPFS
|
|
|
|
|
|
|
|
function and () {
|
|
|
|
var args = Array.from(arguments)
|
|
|
|
|
|
|
|
function matches (a) {
|
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = multiaddr(a)
|
|
|
|
}
|
|
|
|
var out = partialMatch(a.protoNames())
|
|
|
|
if (out === null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return out.length === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function partialMatch (a) {
|
|
|
|
if (a.length < args.length) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
args.some(function (arg) {
|
|
|
|
a = arg.partialMatch(a)
|
|
|
|
if (a === null) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
input: args,
|
|
|
|
matches: matches,
|
|
|
|
partialMatch: partialMatch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function or () {
|
|
|
|
var args = Array.from(arguments)
|
|
|
|
|
|
|
|
function matches (a) {
|
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = multiaddr(a)
|
|
|
|
}
|
|
|
|
var out = partialMatch(a.protoNames())
|
|
|
|
if (out === null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return out.length === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function partialMatch (a) {
|
|
|
|
var out = null
|
|
|
|
args.some(function (arg) {
|
|
|
|
var res = arg.partialMatch(a)
|
|
|
|
if (res) {
|
|
|
|
out = res
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
toString: function () { return '{ ' + args.join(' ') + ' }' },
|
|
|
|
input: args,
|
|
|
|
matches: matches,
|
|
|
|
partialMatch: partialMatch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function base (n) {
|
|
|
|
var name = n
|
|
|
|
function matches (a) {
|
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = multiaddr(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pnames = a.protoNames()
|
|
|
|
if (pnames.length === 1 && pnames[0] === name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function partialMatch (protos) {
|
|
|
|
if (protos.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (protos[0] === name) {
|
|
|
|
return protos.slice(1)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
toString: function () { return name },
|
|
|
|
matches: matches,
|
|
|
|
partialMatch: partialMatch
|
|
|
|
}
|
|
|
|
}
|