Skip to content

Commit

Permalink
feat: Add /p2p-stardust
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mkg20001 committed Jan 25, 2019
1 parent eee56af commit ebaa240
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"chai": "^4.2.0"
},
"dependencies": {
"multiaddr": "^6.0.3"
"multiaddr": "^6.0.4"
},
"contributors": [
"Alan Shaw <alan@tableflip.io>",
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const Reliable = or(
UTP
)

// Unlike ws-star, stardust can run over any transport thus removing the requirement for ws-star (but don't even think about running a stardust server over webrtc-star ;) )
const Stardust = or(
and(Reliable, base('p2p-stardust'), base('ipfs')),
and(Reliable, base('p2p-stardust'))
)

let _IPFS = or(
and(Reliable, base('ipfs')),
WebRTCStar,
Expand Down Expand Up @@ -122,6 +128,7 @@ exports.WebSocketStar = WebSocketStar
exports.WebRTCStar = WebRTCStar
exports.WebRTCDirect = WebRTCDirect
exports.Reliable = Reliable
exports.Stardust = Stardust
exports.Circuit = Circuit
exports.IPFS = IPFS

Expand Down
27 changes: 25 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ describe('multiaddr validation', function () {
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3'
]

const goodStardust = [
'/ip4/1.2.3.4/tcp/3456/ws/p2p-stardust',
'/ip6/::/tcp/0/ws/p2p-stardust',
'/dnsaddr/localhost/ws/p2p-stardust/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ws/p2p-stardust/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-stardust/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3'
]

const badWS = [
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
'/ip6/::/ip4/0.0.0.0/udp/1234/ws',
Expand Down Expand Up @@ -158,7 +166,12 @@ describe('multiaddr validation', function () {
const tests = Array.from(arguments).slice(1)
tests.forEach(function (test) {
test.forEach(function (testcase) {
expect(p.matches(testcase)).to.be.eql(true)
try {
expect(p.matches(testcase)).to.be.eql(true)
} catch (err) {
err.stack = '[testcase=' + JSON.stringify(testcase) + ', shouldMatch=true] ' + err.stack
throw err
}
})
})
}
Expand All @@ -167,7 +180,12 @@ describe('multiaddr validation', function () {
const tests = Array.from(arguments).slice(1)
tests.forEach(function (test) {
test.forEach(function (testcase) {
expect(p.matches(testcase)).to.be.eql(false)
try {
expect(p.matches(testcase)).to.be.eql(false)
} catch (err) {
err.stack = '[testcase=' + JSON.stringify(testcase) + ', shouldMatch=false] ' + err.stack
throw err
}
})
})
}
Expand Down Expand Up @@ -231,6 +249,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.WebSocketStar, goodIP, goodUDP, badWS)
})

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

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

0 comments on commit ebaa240

Please sign in to comment.