mirror of
https://github.com/fluencelabs/js-mafmt
synced 2025-04-25 18:42:24 +00:00
initial
This commit is contained in:
commit
81d0426a76
118
index.js
Normal file
118
index.js
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
var multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
var IP = or(base('ip4'), base('ip6'))
|
||||||
|
var TCP = and(IP, base('tcp'))
|
||||||
|
var UDP = and(IP, base('udp'))
|
||||||
|
var UTP = and(UDP, base('utp'))
|
||||||
|
var Reliable = or(TCP, UTP)
|
||||||
|
var IPFS = and(Reliable, base('ipfs'))
|
||||||
|
|
||||||
|
exports.IP = IP
|
||||||
|
exports.TCP = TCP
|
||||||
|
exports.UDP = UDP
|
||||||
|
exports.UTP = UTP
|
||||||
|
exports.Reliable = Reliable
|
||||||
|
exports.IPFS = IPFS
|
||||||
|
|
||||||
|
function and() {
|
||||||
|
var args = Array.from(arguments)
|
||||||
|
|
||||||
|
function matches(a) {
|
||||||
|
if (typeof a === 'string') {
|
||||||
|
a = multiaddr(a)
|
||||||
|
}
|
||||||
|
out = partialMatch(a.protoNames())
|
||||||
|
if (out === null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return out.length === 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function partialMatch(a) {
|
||||||
|
if (a.length < args.length) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
args.some(function(arg) {
|
||||||
|
a = arg.partialMatch(a)
|
||||||
|
if (a === null) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
input: args,
|
||||||
|
matches: matches,
|
||||||
|
partialMatch: partialMatch,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function or() {
|
||||||
|
var args = Array.from(arguments)
|
||||||
|
|
||||||
|
function matches(a) {
|
||||||
|
if (typeof a === 'string') {
|
||||||
|
a = multiaddr(a)
|
||||||
|
}
|
||||||
|
var out = partialMatch(a.protoNames())
|
||||||
|
if (out === null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return out.length === 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function partialMatch(a) {
|
||||||
|
var out = null
|
||||||
|
args.some(function(arg) {
|
||||||
|
res = arg.partialMatch(a)
|
||||||
|
if (res) {
|
||||||
|
out = res
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
toString: function() { return "{ " + args.join(" ") + " }" },
|
||||||
|
input: args,
|
||||||
|
matches: matches,
|
||||||
|
partialMatch: partialMatch,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function base(n) {
|
||||||
|
var name = n
|
||||||
|
function matches(a) {
|
||||||
|
if (typeof a === 'string') {
|
||||||
|
a = multiaddr(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
pnames = a.protoNames()
|
||||||
|
if (pnames.length === 1 && pnames[0] === name) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function partialMatch(protos) {
|
||||||
|
if (protos.length === 0) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (protos[0] === name) {
|
||||||
|
return protos.slice(1)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
toString: function() {return name} ,
|
||||||
|
matches: matches,
|
||||||
|
partialMatch: partialMatch,
|
||||||
|
}
|
||||||
|
}
|
22
package.json
Normal file
22
package.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "js-mafmt",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "A multiaddr validator",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "node test.js"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/whyrusleeping/js-mafmt.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"multiaddr"
|
||||||
|
],
|
||||||
|
"author": "whyrusleeping",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/whyrusleeping/js-mafmt/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/whyrusleeping/js-mafmt#readme"
|
||||||
|
}
|
90
test.js
Normal file
90
test.js
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
var test = require('tape')
|
||||||
|
var mafmt = require('./')
|
||||||
|
|
||||||
|
test('basic stuff works', function(t) {
|
||||||
|
|
||||||
|
var good_ip = [
|
||||||
|
"/ip4/0.0.0.0",
|
||||||
|
"/ip6/fc00::",
|
||||||
|
]
|
||||||
|
|
||||||
|
var bad_ip = [
|
||||||
|
"/ip4/0.0.0.0/tcp/555",
|
||||||
|
"/udp/789/ip6/fc00::",
|
||||||
|
]
|
||||||
|
|
||||||
|
var good_tcp = [
|
||||||
|
"/ip4/0.0.7.6/tcp/1234",
|
||||||
|
"/ip6/::/tcp/0",
|
||||||
|
]
|
||||||
|
|
||||||
|
var bad_tcp = [
|
||||||
|
"/tcp/12345",
|
||||||
|
"/ip6/fc00::/udp/5523/tcp/9543",
|
||||||
|
]
|
||||||
|
|
||||||
|
var good_udp = [
|
||||||
|
"/ip4/0.0.7.6/udp/1234",
|
||||||
|
"/ip6/::/udp/0",
|
||||||
|
]
|
||||||
|
|
||||||
|
var bad_udp = [
|
||||||
|
"/udp/12345",
|
||||||
|
"/ip6/fc00::/tcp/5523/udp/9543",
|
||||||
|
]
|
||||||
|
|
||||||
|
/*
|
||||||
|
var good_utp = [
|
||||||
|
"/ip4/1.2.3.4/udp/3456/utp",
|
||||||
|
"/ip6/::/udp/0/utp",
|
||||||
|
]
|
||||||
|
|
||||||
|
var bad_utp = [
|
||||||
|
"/ip4/0.0.0.0/tcp/12345/utp",
|
||||||
|
"/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/utp",
|
||||||
|
"/utp",
|
||||||
|
]
|
||||||
|
*/
|
||||||
|
|
||||||
|
function assertMatches(p) {
|
||||||
|
var tests = Array.from(arguments).slice(1)
|
||||||
|
tests.forEach(function(test) {
|
||||||
|
test.forEach(function(testcase) {
|
||||||
|
if (!p.matches(testcase)) {
|
||||||
|
t.fail("should have matched: " + testcase + " " + p)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertMismatches(p) {
|
||||||
|
var tests = Array.from(arguments).slice(1)
|
||||||
|
tests.forEach(function(test) {
|
||||||
|
test.forEach(function(testcase) {
|
||||||
|
t.equal(p.matches(testcase), false, "should not have matched: "+testcase+" "+p)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
assertMatches(mafmt.IP, good_ip)
|
||||||
|
assertMismatches(mafmt.IP, bad_ip, good_tcp)
|
||||||
|
|
||||||
|
assertMatches(mafmt.TCP, good_tcp)
|
||||||
|
assertMismatches(mafmt.TCP, bad_tcp, good_ip)
|
||||||
|
|
||||||
|
assertMatches(mafmt.UDP, good_udp)
|
||||||
|
assertMismatches(mafmt.UDP, bad_udp, good_ip, good_tcp /*, good_utp*/)
|
||||||
|
|
||||||
|
/*
|
||||||
|
assertMatches(mafmt.UTP, good_utp)
|
||||||
|
assertMismatches(mafmt.UTP, bad_utp, good_ip, good_tcp, good_udp)
|
||||||
|
|
||||||
|
assertMatches(mafmt.Reliable, good_utp, good_tcp)
|
||||||
|
assertMismatches(mafmt.Reliable, good_ip, good_udp)
|
||||||
|
*/
|
||||||
|
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user