Skip to content

Commit 09160b5

Browse files
jacobheunvasco-santos
authored andcommitted
feat: p2p and dns support (#42)
1 parent 3fc9234 commit 09160b5

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Where `<FORMAT>` may be:
5353
| `WebRTCDirect` | a "p2p-webrtc-direct" over `HTTP` or "p2p-webrtc-direct" over `HTTPS` format multiaddr | `/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct` |
5454
| `Reliable` | a `WebSockets` or `WebSocketsSecure` or `HTTP` or `HTTPS` or `WebRTCStar` or `WebRTCDirect` or `TCP` or `UTP` format multiaddr | `/dnsaddr/ipfs.io/wss` |
5555
| `Circuit` | | `/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj` |
56-
| `IPFS` | "ipfs" over `Reliable` or `WebRTCStar` or "ipfs" format multiaddr | `/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4` <br> `/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj` |
56+
| `P2P` | "p2p", aka "ipfs", over `Reliable` or `WebRTCStar` or "p2p" format multiaddr | `/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4` <br> `/ip4/127.0.0.1/tcp/20008/ws/p2p/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj` |
5757

5858
Where `multiaddr` may be:
5959

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
"bugs": "https://github.com/multiformats/js-mafmt/issues",
2929
"homepage": "https://github.com/multiformats/js-mafmt#readme",
3030
"devDependencies": {
31-
"aegir": "^18.0.3",
31+
"aegir": "^20.0.0",
3232
"chai": "^4.2.0"
3333
},
3434
"dependencies": {
35-
"multiaddr": "^6.0.4"
35+
"multiaddr": "^6.1.0"
3636
},
3737
"contributors": [
3838
"Alan Shaw <alan@tableflip.io>",

src/index.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const multiaddr = require('multiaddr')
77
*/
88
const DNS4 = base('dns4')
99
const DNS6 = base('dns6')
10-
const _DNS = or(
10+
const DNS = or(
11+
base('dns'),
1112
base('dnsaddr'),
1213
DNS4,
1314
DNS6
@@ -16,16 +17,11 @@ const _DNS = or(
1617
const IP = or(base('ip4'), base('ip6'))
1718
const TCP = or(
1819
and(IP, base('tcp')),
19-
and(_DNS, base('tcp'))
20+
and(DNS, base('tcp'))
2021
)
2122
const UDP = and(IP, base('udp'))
2223
const UTP = and(UDP, base('utp'))
2324

24-
const DNS = or(
25-
and(_DNS, base('tcp')),
26-
_DNS
27-
)
28-
2925
const WebSockets = or(
3026
and(TCP, base('ws')),
3127
and(DNS, base('ws'))
@@ -39,8 +35,7 @@ const WebSocketsSecure = or(
3935
const HTTP = or(
4036
and(TCP, base('http')),
4137
and(IP, base('http')),
42-
and(DNS, base('http')),
43-
and(DNS)
38+
and(DNS, base('http'))
4439
)
4540

4641
const HTTPS = or(
@@ -51,12 +46,16 @@ const HTTPS = or(
5146

5247
const WebRTCStar = or(
5348
and(WebSockets, base('p2p-webrtc-star'), base('ipfs')),
54-
and(WebSocketsSecure, base('p2p-webrtc-star'), base('ipfs'))
49+
and(WebSocketsSecure, base('p2p-webrtc-star'), base('ipfs')),
50+
and(WebSockets, base('p2p-webrtc-star'), base('p2p')),
51+
and(WebSocketsSecure, base('p2p-webrtc-star'), base('p2p'))
5552
)
5653

5754
const WebSocketStar = or(
5855
and(WebSockets, base('p2p-websocket-star'), base('ipfs')),
5956
and(WebSocketsSecure, base('p2p-websocket-star'), base('ipfs')),
57+
and(WebSockets, base('p2p-websocket-star'), base('p2p')),
58+
and(WebSocketsSecure, base('p2p-websocket-star'), base('p2p')),
6059
and(WebSockets, base('p2p-websocket-star')),
6160
and(WebSocketsSecure, base('p2p-websocket-star'))
6261
)
@@ -83,16 +82,18 @@ const Stardust = or(
8382
and(Reliable, base('p2p-stardust'))
8483
)
8584

86-
let _IPFS = or(
85+
const _P2P = or(
8786
and(Reliable, base('ipfs')),
87+
and(Reliable, base('p2p')),
8888
WebRTCStar,
89-
base('ipfs')
89+
base('ipfs'),
90+
base('p2p')
9091
)
9192

9293
const _Circuit = or(
93-
and(_IPFS, base('p2p-circuit'), _IPFS),
94-
and(_IPFS, base('p2p-circuit')),
95-
and(base('p2p-circuit'), _IPFS),
94+
and(_P2P, base('p2p-circuit'), _P2P),
95+
and(_P2P, base('p2p-circuit')),
96+
and(base('p2p-circuit'), _P2P),
9697
and(Reliable, base('p2p-circuit')),
9798
and(base('p2p-circuit'), Reliable),
9899
base('p2p-circuit')
@@ -105,12 +106,12 @@ const CircuitRecursive = () => or(
105106

106107
const Circuit = CircuitRecursive()
107108

108-
const IPFS = or(
109-
and(Circuit, _IPFS, Circuit),
110-
and(_IPFS, Circuit),
111-
and(Circuit, _IPFS),
109+
const P2P = or(
110+
and(Circuit, _P2P, Circuit),
111+
and(_P2P, Circuit),
112+
and(Circuit, _P2P),
112113
Circuit,
113-
_IPFS
114+
_P2P
114115
)
115116

116117
exports.DNS = DNS
@@ -130,7 +131,8 @@ exports.WebRTCDirect = WebRTCDirect
130131
exports.Reliable = Reliable
131132
exports.Stardust = Stardust
132133
exports.Circuit = Circuit
133-
exports.IPFS = IPFS
134+
exports.P2P = P2P
135+
exports.IPFS = P2P
134136

135137
/*
136138
* Validation funcs
@@ -145,7 +147,7 @@ function makeMatchesFunction (partialMatch) {
145147
return false // also if it's invalid it's propably not matching as well so return false
146148
}
147149
}
148-
let out = partialMatch(a.protoNames())
150+
const out = partialMatch(a.protoNames())
149151
if (out === null) {
150152
return false
151153
}

test/index.spec.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ const multiaddr = require('multiaddr')
88

99
describe('multiaddr validation', function () {
1010
const goodDNS = [
11+
'/dns/ipfs.io',
1112
'/dnsaddr/ipfs.io',
1213
'/dns4/ipfs.io',
1314
'/dns4/libp2p.io',
14-
'/dns6/protocol.ai',
15-
'/dns4/protocol.ai/tcp/80',
16-
'/dns6/protocol.ai/tcp/80',
17-
'/dnsaddr/protocol.ai/tcp/80'
15+
'/dns6/protocol.ai'
1816
]
1917

2018
const badDNS = [
@@ -35,6 +33,7 @@ describe('multiaddr validation', function () {
3533
'/ip4/0.0.7.6/tcp/1234',
3634
'/ip6/::/tcp/0',
3735
'/dns4/protocol.ai/tcp/80',
36+
'/dns6/protocol.ai/tcp/80',
3837
'/dnsaddr/protocol.ai/tcp/80'
3938
]
4039

@@ -65,7 +64,6 @@ describe('multiaddr validation', function () {
6564
]
6665

6766
const goodHTTP = [
68-
'/dnsaddr/ipfs.io',
6967
'/dnsaddr/ipfs.io/http',
7068
'/dnsaddr/ipfs.io/tcp/3456/http',
7169
'/ip4/0.0.0.0/http',
@@ -100,7 +98,9 @@ describe('multiaddr validation', function () {
10098
'/dnsaddr/ipfs.io/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
10199
'/dnsaddr/ipfs.io/wss/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
102100
'/ip6/::/tcp/0/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5',
103-
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
101+
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
102+
'/dns/wrtc-star.discovery.libp2p.io/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
103+
'/dns/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
104104
]
105105

106106
const goodWebRTCDirect = [
@@ -160,7 +160,12 @@ describe('multiaddr validation', function () {
160160
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
161161
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
162162
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit',
163-
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
163+
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
164+
'/ip4/127.0.0.1/tcp/20008/ws/p2p/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
165+
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
166+
'/ip4/1.2.3.4/tcp/3456/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
167+
'/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit',
168+
'/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/p2p/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
164169
].concat(goodCircuit)
165170

166171
function assertMatches (p) {

0 commit comments

Comments
 (0)