Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/update webrtc star with dns support #12

Merged
merged 5 commits into from
Jan 22, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"homepage": "https://github.com/whyrusleeping/js-mafmt#readme",
"devDependencies": {
"aegir": "^9.3.0",
"aegir": "^9.3.2",
"chai": "^3.5.0",
"pre-commit": "^1.2.2"
},
Expand Down
93 changes: 64 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,79 @@
'use strict'

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'))
var WebSockets = and(TCP, base('ws'))
var HTTP = and(TCP, base('http'))
var WebRTCStar = and(base('libp2p-webrtc-star'), WebSockets, base('ipfs'))
var WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP)
var Reliable = or(WebSockets, TCP, UTP)

// required cause some transports are already IPFS aware (like WebRTCStar)
var IPFS = {
matches: function (args) {
var IPFS = and(Reliable, base('ipfs'))
return IPFS.matches(args) || WebRTCStar.matches(args)
}
}

const multiaddr = require('multiaddr')

/*
* Valid combinations
*/
const DNS = base('dns')
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'))
)

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.IP = IP
exports.TCP = TCP
exports.UDP = UDP
exports.UTP = UTP
exports.HTTP = HTTP
exports.WebSockets = WebSockets
exports.WebSocketsSecure = WebSocketsSecure
exports.WebRTCStar = WebRTCStar
exports.WebRTCDirect = WebRTCDirect
exports.Reliable = Reliable
exports.IPFS = IPFS

/*
* Validation funcs
*/

function and () {
var args = Array.from(arguments)
const args = Array.from(arguments)

function matches (a) {
if (typeof a === 'string') {
a = multiaddr(a)
}
var out = partialMatch(a.protoNames())
let out = partialMatch(a.protoNames())
if (out === null) {
return false
}
Expand Down Expand Up @@ -67,23 +102,23 @@ function and () {
}

function or () {
var args = Array.from(arguments)
const args = Array.from(arguments)

function matches (a) {
if (typeof a === 'string') {
a = multiaddr(a)
}
var out = partialMatch(a.protoNames())
const out = partialMatch(a.protoNames())
if (out === null) {
return false
}
return out.length === 0
}

function partialMatch (a) {
var out = null
let out = null
args.some(function (arg) {
var res = arg.partialMatch(a)
const res = arg.partialMatch(a)
if (res) {
out = res
return true
Expand All @@ -93,7 +128,7 @@ function or () {
return out
}

var result = {
const result = {
toString: function () { return '{ ' + args.join(' ') + ' }' },
input: args,
matches: matches,
Expand All @@ -104,13 +139,13 @@ function or () {
}

function base (n) {
var name = n
const name = n
function matches (a) {
if (typeof a === 'string') {
a = multiaddr(a)
}

var pnames = a.protoNames()
const pnames = a.protoNames()
if (pnames.length === 1 && pnames[0] === name) {
return true
}
Expand Down
59 changes: 42 additions & 17 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,93 @@

'use strict'

var expect = require('chai').expect
var mafmt = require('./../src')
const expect = require('chai').expect
const mafmt = require('./../src')

describe('multiaddr validation', function () {
var goodIP = [
const goodDNS = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have at least one example of bad dns as well

'/dns/ipfs.io',
'/dns/protocol.ai'
]

const goodIP = [
'/ip4/0.0.0.0',
'/ip6/fc00::'
]

var badIP = [
const badIP = [
'/ip4/0.0.0.0/tcp/555',
'/udp/789/ip6/fc00::'
]

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

var badTCP = [
const badTCP = [
'/tcp/12345',
'/ip6/fc00::/udp/5523/tcp/9543'
]

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

var badUDP = [
const badUDP = [
'/udp/12345',
'/ip6/fc00::/tcp/5523/udp/9543'
]

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

var badUTP = [
const badUTP = [
'/ip4/0.0.0.0/tcp/12345/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',
'/ip6/::/tcp/0/ws'
]

var goodWebRTCStar = [
const goodWSS = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, need a bad example

'/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/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/libp2p-webrtc-star/dns/ipfs.io/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/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/ip6/::/tcp/0/http'
]

var badWS = [
const badWS = [
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
'/ip6/::/ip4/0.0.0.0/udp/1234/ws'
]

var goodIPFS = [
const goodIPFS = [
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

function assertMatches (p) {
var tests = Array.from(arguments).slice(1)
const tests = Array.from(arguments).slice(1)
tests.forEach(function (test) {
test.forEach(function (testcase) {
expect(p.matches(testcase)).to.be.eql(true)
Expand All @@ -82,14 +97,19 @@ describe('multiaddr validation', function () {
}

function assertMismatches (p) {
var tests = Array.from(arguments).slice(1)
const tests = Array.from(arguments).slice(1)
tests.forEach(function (test) {
test.forEach(function (testcase) {
expect(p.matches(testcase)).to.be.eql(false)
})
})
}

it('DNS validation', function () {
assertMatches(mafmt.DNS, goodDNS)
assertMismatches(mafmt.DNS, badIP, goodTCP)
})

it('IP validation', function () {
assertMatches(mafmt.IP, goodIP)
assertMismatches(mafmt.IP, badIP, goodTCP)
Expand Down Expand Up @@ -120,6 +140,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.WebSockets, goodIP, goodUDP, badWS)
})

it('WebSocketsSecure validation', function () {
assertMatches(mafmt.WebSocketsSecure, goodWSS)
assertMismatches(mafmt.WebSocketsSecure, goodIP, goodUDP, badWS)
})

it('WebRTC-star validation', function () {
assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)
Expand Down