From fe4ba459754181624eeb71cd5bd1cbab11af8e2d Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 19 Jan 2016 17:19:48 +0100 Subject: [PATCH 1/4] add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules From c6da9925dcd7cb971cf25c814de559d6d5b18b6d Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 19 Jan 2016 17:23:52 +0100 Subject: [PATCH 2/4] CR --- index.js | 118 -------------------------------------------------- package.json | 11 ++++- src/index.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 90 -------------------------------------- tests/test.js | 86 ++++++++++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+), 210 deletions(-) delete mode 100644 index.js create mode 100644 src/index.js delete mode 100644 test.js create mode 100644 tests/test.js diff --git a/index.js b/index.js deleted file mode 100644 index 192e8ca..0000000 --- a/index.js +++ /dev/null @@ -1,118 +0,0 @@ -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, - } -} diff --git a/package.json b/package.json index c00c9a3..90e9191 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,15 @@ "name": "js-mafmt", "version": "1.0.1", "description": "A multiaddr validator", - "main": "index.js", + "main": "src/index.js", "scripts": { - "test": "node test.js" + "lint": "standard", + "test": "node tests/test.js" }, + "pre-commit": [ + "lint", + "test" + ], "repository": { "type": "git", "url": "git+https://github.com/whyrusleeping/js-mafmt.git" @@ -20,6 +25,8 @@ }, "homepage": "https://github.com/whyrusleeping/js-mafmt#readme", "devDependencies": { + "pre-commit": "^1.1.2", + "standard": "^5.4.1", "tape": "^4.4.0" }, "dependencies": { diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..f0754c9 --- /dev/null +++ b/src/index.js @@ -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) + } + var 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) { + var 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) + } + + var 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 + } +} diff --git a/test.js b/test.js deleted file mode 100644 index 86034b4..0000000 --- a/test.js +++ /dev/null @@ -1,90 +0,0 @@ -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() -}) - - diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..e241f49 --- /dev/null +++ b/tests/test.js @@ -0,0 +1,86 @@ +var test = require('tape') +var mafmt = require('./../src') + +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() +}) From 94f1c8502597ae7db06b0b4aa1ac108376371c41 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 19 Jan 2016 17:53:57 +0100 Subject: [PATCH 3/4] Create README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e869ea --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +js-mafmt +======== + +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) + +> Javascript implementation of multiaddr validation + From dbe88783b2e87c7c7a73dd13d4d2ae4d271ee9d7 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 19 Jan 2016 17:54:33 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e869ea..3d95760 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ js-mafmt ======== -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) > Javascript implementation of multiaddr validation