Merge pull request #12 from whyrusleeping/feat/update-webrtc-star-with-dns-support

feat/update webrtc star with dns support
This commit is contained in:
David Dias 2017-01-22 14:45:43 +00:00 committed by GitHub
commit 1bf81680cf
3 changed files with 126 additions and 46 deletions

View File

@ -33,12 +33,12 @@
}, },
"homepage": "https://github.com/whyrusleeping/js-mafmt#readme", "homepage": "https://github.com/whyrusleeping/js-mafmt#readme",
"devDependencies": { "devDependencies": {
"aegir": "^9.3.0", "aegir": "^9.3.3",
"chai": "^3.5.0", "chai": "^3.5.0",
"pre-commit": "^1.2.2" "pre-commit": "^1.2.2"
}, },
"dependencies": { "dependencies": {
"multiaddr": "^2.1.3" "multiaddr": "^2.2.0"
}, },
"contributors": [ "contributors": [
"David Dias <daviddias.p@gmail.com>", "David Dias <daviddias.p@gmail.com>",

View File

@ -1,44 +1,88 @@
'use strict' 'use strict'
var multiaddr = require('multiaddr') const multiaddr = require('multiaddr')
var IP = or(base('ip4'), base('ip6')) /*
var TCP = and(IP, base('tcp')) * Valid combinations
var UDP = and(IP, base('udp')) */
var UTP = and(UDP, base('utp')) const DNS4 = base('dns4')
var WebSockets = and(TCP, base('ws')) const DNS6 = base('dns6')
var HTTP = and(TCP, base('http')) const DNS = or(
var WebRTCStar = and(base('libp2p-webrtc-star'), WebSockets, base('ipfs')) base('dns'),
var WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP) DNS4,
var Reliable = or(WebSockets, TCP, UTP) DNS6
)
// required cause some transports are already IPFS aware (like WebRTCStar) const IP = or(base('ip4'), base('ip6'))
var IPFS = { const TCP = and(IP, base('tcp'))
matches: function (args) { const UDP = and(IP, base('udp'))
var IPFS = and(Reliable, base('ipfs')) const UTP = and(UDP, base('utp'))
return IPFS.matches(args) || WebRTCStar.matches(args)
}
}
const WebSockets = or(
and(TCP, base('ws')),
and(DNS, base('ws'))
)
const WebSocketsSecure = or(
and(TCP, base('wss')),
and(DNS, base('wss'))
)
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'))
)
const WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP)
const Reliable = or(
WebSockets,
WebSocketsSecure,
HTTP,
WebRTCStar,
WebRTCDirect,
TCP,
UTP
)
const IPFS = or(
and(Reliable, base('ipfs')),
WebRTCStar
)
exports.DNS = DNS
exports.DNS4 = DNS4
exports.DNS6 = DNS6
exports.IP = IP exports.IP = IP
exports.TCP = TCP exports.TCP = TCP
exports.UDP = UDP exports.UDP = UDP
exports.UTP = UTP exports.UTP = UTP
exports.HTTP = HTTP exports.HTTP = HTTP
exports.WebSockets = WebSockets exports.WebSockets = WebSockets
exports.WebSocketsSecure = WebSocketsSecure
exports.WebRTCStar = WebRTCStar exports.WebRTCStar = WebRTCStar
exports.WebRTCDirect = WebRTCDirect exports.WebRTCDirect = WebRTCDirect
exports.Reliable = Reliable exports.Reliable = Reliable
exports.IPFS = IPFS exports.IPFS = IPFS
/*
* Validation funcs
*/
function and () { function and () {
var args = Array.from(arguments) const args = Array.from(arguments)
function matches (a) { function matches (a) {
if (typeof a === 'string') { if (typeof a === 'string') {
a = multiaddr(a) a = multiaddr(a)
} }
var out = partialMatch(a.protoNames()) let out = partialMatch(a.protoNames())
if (out === null) { if (out === null) {
return false return false
} }
@ -67,13 +111,13 @@ function and () {
} }
function or () { function or () {
var args = Array.from(arguments) const args = Array.from(arguments)
function matches (a) { function matches (a) {
if (typeof a === 'string') { if (typeof a === 'string') {
a = multiaddr(a) a = multiaddr(a)
} }
var out = partialMatch(a.protoNames()) const out = partialMatch(a.protoNames())
if (out === null) { if (out === null) {
return false return false
} }
@ -81,9 +125,9 @@ function or () {
} }
function partialMatch (a) { function partialMatch (a) {
var out = null let out = null
args.some(function (arg) { args.some(function (arg) {
var res = arg.partialMatch(a) const res = arg.partialMatch(a)
if (res) { if (res) {
out = res out = res
return true return true
@ -93,7 +137,7 @@ function or () {
return out return out
} }
var result = { const result = {
toString: function () { return '{ ' + args.join(' ') + ' }' }, toString: function () { return '{ ' + args.join(' ') + ' }' },
input: args, input: args,
matches: matches, matches: matches,
@ -104,13 +148,13 @@ function or () {
} }
function base (n) { function base (n) {
var name = n const name = n
function matches (a) { function matches (a) {
if (typeof a === 'string') { if (typeof a === 'string') {
a = multiaddr(a) a = multiaddr(a)
} }
var pnames = a.protoNames() const pnames = a.protoNames()
if (pnames.length === 1 && pnames[0] === name) { if (pnames.length === 1 && pnames[0] === name) {
return true return true
} }

View File

@ -2,78 +2,104 @@
'use strict' 'use strict'
var expect = require('chai').expect const expect = require('chai').expect
var mafmt = require('./../src') const mafmt = require('./../src')
describe('multiaddr validation', function () { describe('multiaddr validation', function () {
var goodIP = [ const goodDNS = [
'/dns/ipfs.io',
'/dns4/ipfs.io',
'/dns4/libp2p.io',
'/dns6/protocol.ai'
]
const badDNS = [
'/ip4/127.0.0.1'
]
const goodIP = [
'/ip4/0.0.0.0', '/ip4/0.0.0.0',
'/ip6/fc00::' '/ip6/fc00::'
] ]
var badIP = [ const badIP = [
'/ip4/0.0.0.0/tcp/555', '/ip4/0.0.0.0/tcp/555',
'/udp/789/ip6/fc00::' '/udp/789/ip6/fc00::'
] ]
var goodTCP = [ const goodTCP = [
'/ip4/0.0.7.6/tcp/1234', '/ip4/0.0.7.6/tcp/1234',
'/ip6/::/tcp/0' '/ip6/::/tcp/0'
] ]
var badTCP = [ const badTCP = [
'/tcp/12345', '/tcp/12345',
'/ip6/fc00::/udp/5523/tcp/9543' '/ip6/fc00::/udp/5523/tcp/9543'
] ]
var goodUDP = [ const goodUDP = [
'/ip4/0.0.7.6/udp/1234', '/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0' '/ip6/::/udp/0'
] ]
var badUDP = [ const badUDP = [
'/udp/12345', '/udp/12345',
'/ip6/fc00::/tcp/5523/udp/9543' '/ip6/fc00::/tcp/5523/udp/9543'
] ]
var goodUTP = [ const goodUTP = [
'/ip4/1.2.3.4/udp/3456/utp', '/ip4/1.2.3.4/udp/3456/utp',
'/ip6/::/udp/0/utp' '/ip6/::/udp/0/utp'
] ]
var badUTP = [ const badUTP = [
'/ip4/0.0.0.0/tcp/12345/utp', '/ip4/0.0.0.0/tcp/12345/utp',
'/ip6/::/ip4/0.0.0.0/udp/1234/utp' '/ip6/::/ip4/0.0.0.0/udp/1234/utp'
] ]
var goodWS = [ const goodWS = [
'/dns/ipfs.io/ws',
'/ip4/1.2.3.4/tcp/3456/ws', '/ip4/1.2.3.4/tcp/3456/ws',
'/ip6/::/tcp/0/ws' '/ip6/::/tcp/0/ws'
] ]
var goodWebRTCStar = [ const goodWSS = [
'/dns/ipfs.io/wss',
'/ip4/1.2.3.4/tcp/3456/wss',
'/ip6/::/tcp/0/wss'
]
const goodWebRTCStar = [
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', '/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/libp2p-webrtc-star/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/libp2p-webrtc-star/dns/ipfs.io/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/libp2p-webrtc-star/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5' '/libp2p-webrtc-star/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5'
] ]
var goodWebRTCDirect = [ const goodWebRTCDirect = [
// '/libp2p-webrtc-direct/dns/ipfs.io',
'/libp2p-webrtc-direct/ip4/1.2.3.4/tcp/3456/http', '/libp2p-webrtc-direct/ip4/1.2.3.4/tcp/3456/http',
'/libp2p-webrtc-direct/ip6/::/tcp/0/http' '/libp2p-webrtc-direct/ip6/::/tcp/0/http'
] ]
var badWS = [ const badWS = [
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws', '/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
'/ip6/::/ip4/0.0.0.0/udp/1234/ws' '/ip6/::/ip4/0.0.0.0/udp/1234/ws'
] ]
var goodIPFS = [ const badWSS = [
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss',
'/ip6/::/ip4/0.0.0.0/udp/1234/wss'
]
const goodIPFS = [
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj', '/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', '/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4' '/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
] ]
function assertMatches (p) { function assertMatches (p) {
var tests = Array.from(arguments).slice(1) const tests = Array.from(arguments).slice(1)
tests.forEach(function (test) { tests.forEach(function (test) {
test.forEach(function (testcase) { test.forEach(function (testcase) {
expect(p.matches(testcase)).to.be.eql(true) expect(p.matches(testcase)).to.be.eql(true)
@ -82,7 +108,7 @@ describe('multiaddr validation', function () {
} }
function assertMismatches (p) { function assertMismatches (p) {
var tests = Array.from(arguments).slice(1) const tests = Array.from(arguments).slice(1)
tests.forEach(function (test) { tests.forEach(function (test) {
test.forEach(function (testcase) { test.forEach(function (testcase) {
expect(p.matches(testcase)).to.be.eql(false) expect(p.matches(testcase)).to.be.eql(false)
@ -90,6 +116,11 @@ describe('multiaddr validation', function () {
}) })
} }
it('DNS validation', function () {
assertMatches(mafmt.DNS, goodDNS)
assertMismatches(mafmt.DNS, badDNS, badIP, goodTCP)
})
it('IP validation', function () { it('IP validation', function () {
assertMatches(mafmt.IP, goodIP) assertMatches(mafmt.IP, goodIP)
assertMismatches(mafmt.IP, badIP, goodTCP) assertMismatches(mafmt.IP, badIP, goodTCP)
@ -120,6 +151,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.WebSockets, goodIP, goodUDP, badWS) assertMismatches(mafmt.WebSockets, goodIP, goodUDP, badWS)
}) })
it('WebSocketsSecure validation', function () {
assertMatches(mafmt.WebSocketsSecure, goodWSS)
assertMismatches(mafmt.WebSocketsSecure, goodIP, badWSS, goodUDP, badWS)
})
it('WebRTC-star validation', function () { it('WebRTC-star validation', function () {
assertMatches(mafmt.WebRTCStar, goodWebRTCStar) assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS) assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)