diff --git a/src/index.js b/src/index.js index 22e6aa4..0f71d58 100644 --- a/src/index.js +++ b/src/index.js @@ -129,12 +129,14 @@ exports.IPFS = IPFS * Validation funcs */ -function and () { - const args = Array.from(arguments) - - function matches (a) { +function makeMatchesFunction (partialMatch) { + return function matches (a) { if (typeof a === 'string') { - a = multiaddr(a) + try { + a = multiaddr(a) + } catch (err) { // catch error + return false // also if it's invalid it's propably not matching as well so return false + } } let out = partialMatch(a.protoNames()) if (out === null) { @@ -142,7 +144,10 @@ function and () { } return out.length === 0 } +} +function and () { + const args = Array.from(arguments) function partialMatch (a) { if (a.length < args.length) { return null @@ -163,7 +168,7 @@ function and () { return { toString: function () { return '{ ' + args.join(' ') + ' }' }, input: args, - matches: matches, + matches: makeMatchesFunction(partialMatch), partialMatch: partialMatch } } @@ -171,17 +176,6 @@ function and () { function or () { const args = Array.from(arguments) - function matches (a) { - if (typeof a === 'string') { - a = multiaddr(a) - } - const out = partialMatch(a.protoNames()) - if (out === null) { - return false - } - return out.length === 0 - } - function partialMatch (a) { let out = null args.some((arg) => { @@ -200,7 +194,7 @@ function or () { const result = { toString: function () { return '{ ' + args.join(' ') + ' }' }, input: args, - matches: matches, + matches: makeMatchesFunction(partialMatch), partialMatch: partialMatch } @@ -212,7 +206,11 @@ function base (n) { function matches (a) { if (typeof a === 'string') { - a = multiaddr(a) + try { + a = multiaddr(a) + } catch (err) { // catch error + return false // also if it's invalid it's propably not matching as well so return false + } } const pnames = a.protoNames() diff --git a/test/index.spec.js b/test/index.spec.js index 9b89930..1c7d343 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -172,6 +172,10 @@ describe('multiaddr validation', function () { }) } + it('do not throw if multiaddr str is invalid', function () { + expect(mafmt.HTTP.matches('/http-google-com')).to.be.eql(false) + }) + it('DNS validation', function () { assertMatches(mafmt.DNS, goodDNS) assertMismatches(mafmt.DNS, badDNS, badIP)