Skip to content

Commit 1bf8168

Browse files
authored
Merge pull request #12 from whyrusleeping/feat/update-webrtc-star-with-dns-support
feat/update webrtc star with dns support
2 parents 7bee5c2 + af4db1d commit 1bf8168

File tree

3 files changed

+129
-49
lines changed

3 files changed

+129
-49
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@
3333
},
3434
"homepage": "https://github.com/whyrusleeping/js-mafmt#readme",
3535
"devDependencies": {
36-
"aegir": "^9.3.0",
36+
"aegir": "^9.3.3",
3737
"chai": "^3.5.0",
3838
"pre-commit": "^1.2.2"
3939
},
4040
"dependencies": {
41-
"multiaddr": "^2.1.3"
41+
"multiaddr": "^2.2.0"
4242
},
4343
"contributors": [
4444
"David Dias <[email protected]>",
4545
"Jeromy <[email protected]>",
4646
"Jeromy Johnson <[email protected]>",
4747
"dignifiedquire <[email protected]>"
4848
]
49-
}
49+
}

src/index.js

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,88 @@
11
'use strict'
22

3-
var multiaddr = require('multiaddr')
4-
5-
var IP = or(base('ip4'), base('ip6'))
6-
var TCP = and(IP, base('tcp'))
7-
var UDP = and(IP, base('udp'))
8-
var UTP = and(UDP, base('utp'))
9-
var WebSockets = and(TCP, base('ws'))
10-
var HTTP = and(TCP, base('http'))
11-
var WebRTCStar = and(base('libp2p-webrtc-star'), WebSockets, base('ipfs'))
12-
var WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP)
13-
var Reliable = or(WebSockets, TCP, UTP)
14-
15-
// required cause some transports are already IPFS aware (like WebRTCStar)
16-
var IPFS = {
17-
matches: function (args) {
18-
var IPFS = and(Reliable, base('ipfs'))
19-
return IPFS.matches(args) || WebRTCStar.matches(args)
20-
}
21-
}
22-
3+
const multiaddr = require('multiaddr')
4+
5+
/*
6+
* Valid combinations
7+
*/
8+
const DNS4 = base('dns4')
9+
const DNS6 = base('dns6')
10+
const DNS = or(
11+
base('dns'),
12+
DNS4,
13+
DNS6
14+
)
15+
16+
const IP = or(base('ip4'), base('ip6'))
17+
const TCP = and(IP, base('tcp'))
18+
const UDP = and(IP, base('udp'))
19+
const UTP = and(UDP, base('utp'))
20+
21+
const WebSockets = or(
22+
and(TCP, base('ws')),
23+
and(DNS, base('ws'))
24+
)
25+
26+
const WebSocketsSecure = or(
27+
and(TCP, base('wss')),
28+
and(DNS, base('wss'))
29+
)
30+
31+
const HTTP = or(
32+
and(TCP, base('http')),
33+
and(DNS),
34+
and(DNS, base('http'))
35+
)
36+
37+
const WebRTCStar = or(
38+
and(base('libp2p-webrtc-star'), WebSockets, base('ipfs')),
39+
and(base('libp2p-webrtc-star'), WebSocketsSecure, base('ipfs'))
40+
)
41+
42+
const WebRTCDirect = and(base('libp2p-webrtc-direct'), HTTP)
43+
44+
const Reliable = or(
45+
WebSockets,
46+
WebSocketsSecure,
47+
HTTP,
48+
WebRTCStar,
49+
WebRTCDirect,
50+
TCP,
51+
UTP
52+
)
53+
54+
const IPFS = or(
55+
and(Reliable, base('ipfs')),
56+
WebRTCStar
57+
)
58+
59+
exports.DNS = DNS
60+
exports.DNS4 = DNS4
61+
exports.DNS6 = DNS6
2362
exports.IP = IP
2463
exports.TCP = TCP
2564
exports.UDP = UDP
2665
exports.UTP = UTP
2766
exports.HTTP = HTTP
2867
exports.WebSockets = WebSockets
68+
exports.WebSocketsSecure = WebSocketsSecure
2969
exports.WebRTCStar = WebRTCStar
3070
exports.WebRTCDirect = WebRTCDirect
3171
exports.Reliable = Reliable
3272
exports.IPFS = IPFS
3373

74+
/*
75+
* Validation funcs
76+
*/
77+
3478
function and () {
35-
var args = Array.from(arguments)
79+
const args = Array.from(arguments)
3680

3781
function matches (a) {
3882
if (typeof a === 'string') {
3983
a = multiaddr(a)
4084
}
41-
var out = partialMatch(a.protoNames())
85+
let out = partialMatch(a.protoNames())
4286
if (out === null) {
4387
return false
4488
}
@@ -67,23 +111,23 @@ function and () {
67111
}
68112

69113
function or () {
70-
var args = Array.from(arguments)
114+
const args = Array.from(arguments)
71115

72116
function matches (a) {
73117
if (typeof a === 'string') {
74118
a = multiaddr(a)
75119
}
76-
var out = partialMatch(a.protoNames())
120+
const out = partialMatch(a.protoNames())
77121
if (out === null) {
78122
return false
79123
}
80124
return out.length === 0
81125
}
82126

83127
function partialMatch (a) {
84-
var out = null
128+
let out = null
85129
args.some(function (arg) {
86-
var res = arg.partialMatch(a)
130+
const res = arg.partialMatch(a)
87131
if (res) {
88132
out = res
89133
return true
@@ -93,7 +137,7 @@ function or () {
93137
return out
94138
}
95139

96-
var result = {
140+
const result = {
97141
toString: function () { return '{ ' + args.join(' ') + ' }' },
98142
input: args,
99143
matches: matches,
@@ -104,13 +148,13 @@ function or () {
104148
}
105149

106150
function base (n) {
107-
var name = n
151+
const name = n
108152
function matches (a) {
109153
if (typeof a === 'string') {
110154
a = multiaddr(a)
111155
}
112156

113-
var pnames = a.protoNames()
157+
const pnames = a.protoNames()
114158
if (pnames.length === 1 && pnames[0] === name) {
115159
return true
116160
}

test/index.spec.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,104 @@
22

33
'use strict'
44

5-
var expect = require('chai').expect
6-
var mafmt = require('./../src')
5+
const expect = require('chai').expect
6+
const mafmt = require('./../src')
77

88
describe('multiaddr validation', function () {
9-
var goodIP = [
9+
const goodDNS = [
10+
'/dns/ipfs.io',
11+
'/dns4/ipfs.io',
12+
'/dns4/libp2p.io',
13+
'/dns6/protocol.ai'
14+
]
15+
16+
const badDNS = [
17+
'/ip4/127.0.0.1'
18+
]
19+
20+
const goodIP = [
1021
'/ip4/0.0.0.0',
1122
'/ip6/fc00::'
1223
]
1324

14-
var badIP = [
25+
const badIP = [
1526
'/ip4/0.0.0.0/tcp/555',
1627
'/udp/789/ip6/fc00::'
1728
]
1829

19-
var goodTCP = [
30+
const goodTCP = [
2031
'/ip4/0.0.7.6/tcp/1234',
2132
'/ip6/::/tcp/0'
2233
]
2334

24-
var badTCP = [
35+
const badTCP = [
2536
'/tcp/12345',
2637
'/ip6/fc00::/udp/5523/tcp/9543'
2738
]
2839

29-
var goodUDP = [
40+
const goodUDP = [
3041
'/ip4/0.0.7.6/udp/1234',
3142
'/ip6/::/udp/0'
3243
]
3344

34-
var badUDP = [
45+
const badUDP = [
3546
'/udp/12345',
3647
'/ip6/fc00::/tcp/5523/udp/9543'
3748
]
3849

39-
var goodUTP = [
50+
const goodUTP = [
4051
'/ip4/1.2.3.4/udp/3456/utp',
4152
'/ip6/::/udp/0/utp'
4253
]
4354

44-
var badUTP = [
55+
const badUTP = [
4556
'/ip4/0.0.0.0/tcp/12345/utp',
4657
'/ip6/::/ip4/0.0.0.0/udp/1234/utp'
4758
]
4859

49-
var goodWS = [
60+
const goodWS = [
61+
'/dns/ipfs.io/ws',
5062
'/ip4/1.2.3.4/tcp/3456/ws',
5163
'/ip6/::/tcp/0/ws'
5264
]
5365

54-
var goodWebRTCStar = [
66+
const goodWSS = [
67+
'/dns/ipfs.io/wss',
68+
'/ip4/1.2.3.4/tcp/3456/wss',
69+
'/ip6/::/tcp/0/wss'
70+
]
71+
72+
const goodWebRTCStar = [
5573
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
74+
'/libp2p-webrtc-star/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
75+
'/libp2p-webrtc-star/dns/ipfs.io/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
5676
'/libp2p-webrtc-star/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5'
5777
]
5878

59-
var goodWebRTCDirect = [
79+
const goodWebRTCDirect = [
80+
// '/libp2p-webrtc-direct/dns/ipfs.io',
6081
'/libp2p-webrtc-direct/ip4/1.2.3.4/tcp/3456/http',
6182
'/libp2p-webrtc-direct/ip6/::/tcp/0/http'
6283
]
6384

64-
var badWS = [
85+
const badWS = [
6586
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
6687
'/ip6/::/ip4/0.0.0.0/udp/1234/ws'
6788
]
6889

69-
var goodIPFS = [
90+
const badWSS = [
91+
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss',
92+
'/ip6/::/ip4/0.0.0.0/udp/1234/wss'
93+
]
94+
95+
const goodIPFS = [
7096
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
7197
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
7298
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
7399
]
74100

75101
function assertMatches (p) {
76-
var tests = Array.from(arguments).slice(1)
102+
const tests = Array.from(arguments).slice(1)
77103
tests.forEach(function (test) {
78104
test.forEach(function (testcase) {
79105
expect(p.matches(testcase)).to.be.eql(true)
@@ -82,14 +108,19 @@ describe('multiaddr validation', function () {
82108
}
83109

84110
function assertMismatches (p) {
85-
var tests = Array.from(arguments).slice(1)
111+
const tests = Array.from(arguments).slice(1)
86112
tests.forEach(function (test) {
87113
test.forEach(function (testcase) {
88114
expect(p.matches(testcase)).to.be.eql(false)
89115
})
90116
})
91117
}
92118

119+
it('DNS validation', function () {
120+
assertMatches(mafmt.DNS, goodDNS)
121+
assertMismatches(mafmt.DNS, badDNS, badIP, goodTCP)
122+
})
123+
93124
it('IP validation', function () {
94125
assertMatches(mafmt.IP, goodIP)
95126
assertMismatches(mafmt.IP, badIP, goodTCP)
@@ -120,6 +151,11 @@ describe('multiaddr validation', function () {
120151
assertMismatches(mafmt.WebSockets, goodIP, goodUDP, badWS)
121152
})
122153

154+
it('WebSocketsSecure validation', function () {
155+
assertMatches(mafmt.WebSocketsSecure, goodWSS)
156+
assertMismatches(mafmt.WebSocketsSecure, goodIP, badWSS, goodUDP, badWS)
157+
})
158+
123159
it('WebRTC-star validation', function () {
124160
assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
125161
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)

0 commit comments

Comments
 (0)