Skip to content

Commit 9b0c199

Browse files
mkg20001vasco-santos
authored andcommitted
feat: add /p2p-stardust (#35)
This is part of the endeavor to replace ws-star with the newly created stardust protocol. See libp2p/js-libp2p-websocket-star#70 for a reference Note: This PR depends on this pr to be merged and the package being updated first: multiformats/js-multiaddr#78
1 parent abd7f1c commit 9b0c199

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"chai": "^4.2.0"
3333
},
3434
"dependencies": {
35-
"multiaddr": "^6.0.3"
35+
"multiaddr": "^6.0.4"
3636
},
3737
"contributors": [
3838
"Alan Shaw <alan@tableflip.io>",

src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ const Reliable = or(
7777
UTP
7878
)
7979

80+
// Unlike ws-star, stardust can run over any transport thus removing the requirement for websockets (but don't even think about running a stardust server over webrtc-star ;) )
81+
const Stardust = or(
82+
and(Reliable, base('p2p-stardust'), base('ipfs')),
83+
and(Reliable, base('p2p-stardust'))
84+
)
85+
8086
let _IPFS = or(
8187
and(Reliable, base('ipfs')),
8288
WebRTCStar,
@@ -122,6 +128,7 @@ exports.WebSocketStar = WebSocketStar
122128
exports.WebRTCStar = WebRTCStar
123129
exports.WebRTCDirect = WebRTCDirect
124130
exports.Reliable = Reliable
131+
exports.Stardust = Stardust
125132
exports.Circuit = Circuit
126133
exports.IPFS = IPFS
127134

test/index.spec.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ describe('multiaddr validation', function () {
115115
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3'
116116
]
117117

118+
const goodStardust = [
119+
'/ip4/1.2.3.4/tcp/3456/ws/p2p-stardust',
120+
'/ip6/::/tcp/0/ws/p2p-stardust',
121+
'/dnsaddr/localhost/ws/p2p-stardust/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
122+
'/ip4/1.2.3.4/tcp/3456/ws/p2p-stardust/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
123+
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-stardust/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3'
124+
]
125+
118126
const badWS = [
119127
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
120128
'/ip6/::/ip4/0.0.0.0/udp/1234/ws',
@@ -158,7 +166,12 @@ describe('multiaddr validation', function () {
158166
const tests = Array.from(arguments).slice(1)
159167
tests.forEach(function (test) {
160168
test.forEach(function (testcase) {
161-
expect(p.matches(testcase)).to.be.eql(true)
169+
try {
170+
expect(p.matches(testcase)).to.be.eql(true)
171+
} catch (err) {
172+
err.stack = '[testcase=' + JSON.stringify(testcase) + ', shouldMatch=true] ' + err.stack
173+
throw err
174+
}
162175
})
163176
})
164177
}
@@ -167,7 +180,12 @@ describe('multiaddr validation', function () {
167180
const tests = Array.from(arguments).slice(1)
168181
tests.forEach(function (test) {
169182
test.forEach(function (testcase) {
170-
expect(p.matches(testcase)).to.be.eql(false)
183+
try {
184+
expect(p.matches(testcase)).to.be.eql(false)
185+
} catch (err) {
186+
err.stack = '[testcase=' + JSON.stringify(testcase) + ', shouldMatch=false] ' + err.stack
187+
throw err
188+
}
171189
})
172190
})
173191
}
@@ -231,6 +249,11 @@ describe('multiaddr validation', function () {
231249
assertMismatches(mafmt.WebSocketStar, goodIP, goodUDP, badWS)
232250
})
233251

252+
it('Stardust validation', function () {
253+
assertMatches(mafmt.Stardust, goodStardust)
254+
assertMismatches(mafmt.Stardust, goodIP, goodUDP, badWS)
255+
})
256+
234257
it('WebRTCStar validation', function () {
235258
assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
236259
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)

0 commit comments

Comments
 (0)