2016-03-23 15:07:25 +01:00
|
|
|
'use strict'
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Valid combinations
|
|
|
|
*/
|
2017-01-22 14:36:58 +00:00
|
|
|
const DNS4 = base('dns4')
|
|
|
|
const DNS6 = base('dns6')
|
|
|
|
const DNS = or(
|
|
|
|
base('dns'),
|
|
|
|
DNS4,
|
|
|
|
DNS6
|
|
|
|
)
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
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 WebSockets = or(
|
|
|
|
and(TCP, base('ws')),
|
|
|
|
and(DNS, base('ws'))
|
|
|
|
)
|
2017-01-20 17:46:26 +00:00
|
|
|
|
|
|
|
const WebSocketsSecure = or(
|
|
|
|
and(TCP, base('wss')),
|
|
|
|
and(DNS, base('wss'))
|
|
|
|
)
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
const HTTP = or(
|
|
|
|
and(TCP, base('http')),
|
|
|
|
and(DNS),
|
|
|
|
and(DNS, base('http'))
|
|
|
|
)
|
|
|
|
|
2017-01-20 17:46:26 +00:00
|
|
|
const WebRTCStar = or(
|
|
|
|
and(base('libp2p-webrtc-star'), WebSockets, base('ipfs')),
|
|
|
|
and(base('libp2p-webrtc-star'), WebSocketsSecure, base('ipfs'))
|
|
|
|
)
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
const WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP)
|
|
|
|
|
2017-01-20 17:46:26 +00:00
|
|
|
const Reliable = or(
|
|
|
|
WebSockets,
|
|
|
|
WebSocketsSecure,
|
|
|
|
HTTP,
|
|
|
|
WebRTCStar,
|
|
|
|
WebRTCDirect,
|
|
|
|
TCP,
|
|
|
|
UTP
|
|
|
|
)
|
2017-01-20 17:27:50 +00:00
|
|
|
|
2017-02-25 23:41:14 -08:00
|
|
|
let _IPFS = or(
|
2017-01-20 17:27:50 +00:00
|
|
|
and(Reliable, base('ipfs')),
|
2017-02-25 23:41:14 -08:00
|
|
|
WebRTCStar,
|
|
|
|
base('ipfs')
|
|
|
|
)
|
|
|
|
|
|
|
|
const Circuit = or(
|
|
|
|
and(_IPFS, base('p2p-circuit'), _IPFS),
|
|
|
|
and(_IPFS, base('p2p-circuit')),
|
|
|
|
and(base('p2p-circuit'), _IPFS),
|
|
|
|
base('p2p-circuit')
|
|
|
|
)
|
|
|
|
|
|
|
|
const IPFS = or(
|
|
|
|
and(_IPFS, Circuit),
|
|
|
|
_IPFS
|
2017-01-20 17:27:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
exports.DNS = DNS
|
2017-01-22 14:36:58 +00:00
|
|
|
exports.DNS4 = DNS4
|
|
|
|
exports.DNS6 = DNS6
|
2016-01-19 17:23:52 +01:00
|
|
|
exports.IP = IP
|
|
|
|
exports.TCP = TCP
|
|
|
|
exports.UDP = UDP
|
|
|
|
exports.UTP = UTP
|
2017-01-16 14:59:35 +00:00
|
|
|
exports.HTTP = HTTP
|
2016-03-12 12:36:19 +00:00
|
|
|
exports.WebSockets = WebSockets
|
2017-01-20 17:46:26 +00:00
|
|
|
exports.WebSocketsSecure = WebSocketsSecure
|
2016-05-21 19:57:26 +01:00
|
|
|
exports.WebRTCStar = WebRTCStar
|
2017-01-16 14:59:35 +00:00
|
|
|
exports.WebRTCDirect = WebRTCDirect
|
2016-01-19 17:23:52 +01:00
|
|
|
exports.Reliable = Reliable
|
2017-02-25 23:41:14 -08:00
|
|
|
exports.Circuit = Circuit
|
2016-01-19 17:23:52 +01:00
|
|
|
exports.IPFS = IPFS
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
/*
|
|
|
|
* Validation funcs
|
|
|
|
*/
|
|
|
|
|
2016-01-19 17:23:52 +01:00
|
|
|
function and () {
|
2017-01-20 17:27:50 +00:00
|
|
|
const args = Array.from(arguments)
|
2016-01-19 17:23:52 +01:00
|
|
|
|
|
|
|
function matches (a) {
|
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = multiaddr(a)
|
|
|
|
}
|
2017-01-20 17:27:50 +00:00
|
|
|
let out = partialMatch(a.protoNames())
|
2016-01-19 17:23:52 +01:00
|
|
|
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 () {
|
2017-01-20 17:27:50 +00:00
|
|
|
const args = Array.from(arguments)
|
2016-01-19 17:23:52 +01:00
|
|
|
|
|
|
|
function matches (a) {
|
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = multiaddr(a)
|
|
|
|
}
|
2017-01-20 17:27:50 +00:00
|
|
|
const out = partialMatch(a.protoNames())
|
2016-01-19 17:23:52 +01:00
|
|
|
if (out === null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return out.length === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function partialMatch (a) {
|
2017-01-20 17:27:50 +00:00
|
|
|
let out = null
|
2016-01-19 17:23:52 +01:00
|
|
|
args.some(function (arg) {
|
2017-01-20 17:27:50 +00:00
|
|
|
const res = arg.partialMatch(a)
|
2016-01-19 17:23:52 +01:00
|
|
|
if (res) {
|
|
|
|
out = res
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
const result = {
|
2016-01-19 17:23:52 +01:00
|
|
|
toString: function () { return '{ ' + args.join(' ') + ' }' },
|
|
|
|
input: args,
|
|
|
|
matches: matches,
|
|
|
|
partialMatch: partialMatch
|
|
|
|
}
|
2016-05-28 15:34:50 +01:00
|
|
|
|
|
|
|
return result
|
2016-01-19 17:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function base (n) {
|
2017-01-20 17:27:50 +00:00
|
|
|
const name = n
|
2016-01-19 17:23:52 +01:00
|
|
|
function matches (a) {
|
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = multiaddr(a)
|
|
|
|
}
|
|
|
|
|
2017-01-20 17:27:50 +00:00
|
|
|
const pnames = a.protoNames()
|
2016-01-19 17:23:52 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|