-
Notifications
You must be signed in to change notification settings - Fork 19
feat: adding circuit addresses #14
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
Changes from all commits
c05c1af
14db530
2f604c3
95c5922
a1f9ccd
08a72b1
b640e9d
c0d2d05
2e28d27
c80fb35
40f8c9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ dist | |
coverage | ||
|
||
*.log | ||
|
||
.vscode | ||
.history |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "mafmt", | ||
"version": "2.1.6", | ||
"version": "2.1.7", | ||
"description": "A multiaddr validator", | ||
"main": "src/index.js", | ||
"scripts": { | ||
|
@@ -33,17 +33,18 @@ | |
}, | ||
"homepage": "https://github.com/whyrusleeping/js-mafmt#readme", | ||
"devDependencies": { | ||
"aegir": "^9.3.3", | ||
"aegir": "^11.0.0", | ||
"chai": "^3.5.0", | ||
"pre-commit": "^1.2.2" | ||
}, | ||
"dependencies": { | ||
"multiaddr": "^2.2.0" | ||
"multiaddr": "^2.2.2" | ||
}, | ||
"contributors": [ | ||
"David Dias <[email protected]>", | ||
"Jeromy <[email protected]>", | ||
"Jeromy Johnson <[email protected]>", | ||
"dignifiedquire <[email protected]>" | ||
"dignifiedquire <[email protected]>", | ||
"dmitriy ryajov <[email protected]>" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ const multiaddr = require('multiaddr') | |
*/ | ||
const DNS4 = base('dns4') | ||
const DNS6 = base('dns6') | ||
const DNS = or( | ||
const _DNS = or( | ||
base('dns'), | ||
DNS4, | ||
DNS6 | ||
|
@@ -18,6 +18,11 @@ const TCP = and(IP, base('tcp')) | |
const UDP = and(IP, base('udp')) | ||
const UTP = and(UDP, base('utp')) | ||
|
||
const DNS = or( | ||
and(_DNS, base('tcp')), | ||
_DNS | ||
) | ||
|
||
const WebSockets = or( | ||
and(TCP, base('ws')), | ||
and(DNS, base('ws')) | ||
|
@@ -51,9 +56,34 @@ const Reliable = or( | |
UTP | ||
) | ||
|
||
const IPFS = or( | ||
let _IPFS = or( | ||
and(Reliable, base('ipfs')), | ||
WebRTCStar | ||
WebRTCStar, | ||
base('ipfs') | ||
) | ||
|
||
const _Circuit = or( | ||
and(_IPFS, base('p2p-circuit'), _IPFS), | ||
and(_IPFS, base('p2p-circuit')), | ||
and(base('p2p-circuit'), _IPFS), | ||
and(Reliable, base('p2p-circuit')), | ||
and(base('p2p-circuit'), Reliable), | ||
base('p2p-circuit') | ||
) | ||
|
||
const CircuitRecursive = () => or( | ||
and(_Circuit, CircuitRecursive), | ||
_Circuit | ||
) | ||
|
||
const Circuit = CircuitRecursive() | ||
|
||
const IPFS = or( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference on circuit addresses take a look at - https://github.com/libp2p/specs/tree/master/relay#addressing. The above seems to take care of most of the cases, but it hasn't been cleaned up, I'll try to do it once I've got everything ironed out. |
||
and(Circuit, _IPFS, Circuit), | ||
and(_IPFS, Circuit), | ||
and(Circuit, _IPFS), | ||
Circuit, | ||
_IPFS | ||
) | ||
|
||
exports.DNS = DNS | ||
|
@@ -69,6 +99,7 @@ exports.WebSocketsSecure = WebSocketsSecure | |
exports.WebRTCStar = WebRTCStar | ||
exports.WebRTCDirect = WebRTCDirect | ||
exports.Reliable = Reliable | ||
exports.Circuit = Circuit | ||
exports.IPFS = IPFS | ||
|
||
/* | ||
|
@@ -94,7 +125,7 @@ function and () { | |
return null | ||
} | ||
args.some(function (arg) { | ||
a = arg.partialMatch(a) | ||
a = typeof arg === 'function' ? arg().partialMatch(a) : arg.partialMatch(a) | ||
if (a === null) { | ||
return true | ||
} | ||
|
@@ -127,7 +158,7 @@ function or () { | |
function partialMatch (a) { | ||
let out = null | ||
args.some(function (arg) { | ||
const res = arg.partialMatch(a) | ||
const res = typeof arg === 'function' ? arg().partialMatch(a) : arg.partialMatch(a) | ||
if (res) { | ||
out = res | ||
return true | ||
|
@@ -149,6 +180,7 @@ function or () { | |
|
||
function base (n) { | ||
const name = n | ||
|
||
function matches (a) { | ||
if (typeof a === 'string') { | ||
a = multiaddr(a) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,10 @@ describe('multiaddr validation', function () { | |
'/dns/ipfs.io', | ||
'/dns4/ipfs.io', | ||
'/dns4/libp2p.io', | ||
'/dns6/protocol.ai' | ||
'/dns6/protocol.ai', | ||
'/dns4/protocol.ai/tcp/80', | ||
'/dns6/protocol.ai/tcp/80', | ||
'/dns/protocol.ai/tcp/80' | ||
] | ||
|
||
const badDNS = [ | ||
|
@@ -92,11 +95,33 @@ describe('multiaddr validation', function () { | |
'/ip6/::/ip4/0.0.0.0/udp/1234/wss' | ||
] | ||
|
||
const goodCircuit = [ | ||
'/p2p-circuit', | ||
'/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj', | ||
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj', | ||
'/p2p-circuit/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/p2p-circuit/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA', | ||
'/p2p-circuit/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA', | ||
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/' + | ||
'p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @whyrusleeping check this examples, @dryajov pretty much covered them all from @lgierth notes. |
||
] | ||
|
||
const badCircuit = [ | ||
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss', | ||
'/ip4/0.0.7.6/udp/1234', | ||
'/ip6/::/udp/0/utp', | ||
'/dns/ipfs.io/ws', | ||
'/libp2p-webrtc-direct/ip4/1.2.3.4/tcp/3456/http' | ||
] | ||
|
||
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' | ||
] | ||
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit', | ||
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj' | ||
].concat(goodCircuit) | ||
|
||
function assertMatches (p) { | ||
const tests = Array.from(arguments).slice(1) | ||
|
@@ -166,6 +191,11 @@ describe('multiaddr validation', function () { | |
assertMismatches(mafmt.WebRTCDirect, goodIP, goodUDP, badWS) | ||
}) | ||
|
||
it('Circuit validation', function () { | ||
assertMatches(mafmt.Circuit, goodCircuit) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have tests for mismatches as well. |
||
assertMismatches(mafmt.Circuit, badCircuit) | ||
}) | ||
|
||
it('IPFS validation', function () { | ||
assertMatches(mafmt.IPFS, goodIPFS) | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't need to be a function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mm, that's just to make it recursive, the idea is that if a function is encountered during n match, it will be called, which will expand into addidional match conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing, the reason it has to be a function, is because I can reference variables that haven't finished being declared yet... I.e. I can reference circuit from it's own declaration block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I know what your talking about, we can just do
() => Circuit
when we need recursivity there.