diff --git a/src/index.js b/src/index.js index d75e23b..63071d3 100644 --- a/src/index.js +++ b/src/index.js @@ -16,20 +16,7 @@ limitations under the License. const validUrl = require('valid-url') const bent = require('bent') const req = bent('GET') -const { serviceprovidersList, serviceproviders } = require('./serviceproviders') - -const matchServiceproviders = (uri) => { - let matches = [], sp - - serviceprovidersList.forEach((spName, i) => { - sp = serviceproviders[spName] - if (sp.reURI.test(uri)) { - matches.push(sp.processURI(uri)) - } - }) - - return matches -} +const serviceproviders = require('./serviceproviders') const verify = async (uri, fingerprint, opts) => { if (!opts) { opts = {} } @@ -38,7 +25,7 @@ const verify = async (uri, fingerprint, opts) => { throw new Error('Not a valid URI') } - const spMatches = matchServiceproviders(uri) + const spMatches = serviceproviders.match(uri) if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { return spMatches @@ -72,4 +59,3 @@ const verify = async (uri, fingerprint, opts) => { exports.verify = verify exports.serviceproviders = serviceproviders -exports.serviceprovidersList = serviceprovidersList diff --git a/src/serviceproviders.js b/src/serviceproviders.js index c2c3e56..7f1d085 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -13,16 +13,29 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -exports.serviceprovidersList = [ +exports.list = [ 'dns', 'xmpp', 'twitter', 'hackernews', ] -exports.serviceproviders = { +exports.data = { dns: require('./serviceproviders/dns'), xmpp: require('./serviceproviders/xmpp'), twitter: require('./serviceproviders/twitter'), hackernews: require('./serviceproviders/hackernews'), } + +exports.match = (uri) => { + let matches = [], sp + + serviceprovidersList.forEach((spName, i) => { + sp = serviceproviders[spName] + if (sp.reURI.test(uri)) { + matches.push(sp.processURI(uri)) + } + }) + + return matches +} diff --git a/test/serviceproviders.test.js b/test/serviceproviders.test.js index 03f6e30..6452dee 100644 --- a/test/serviceproviders.test.js +++ b/test/serviceproviders.test.js @@ -18,30 +18,30 @@ const assert = require('chai').assert const request = require('supertest') const doipjs = require('../src') -doipjs.serviceprovidersList.forEach((sp, i) => { +doipjs.serviceproviders.list.forEach((sp, i) => { describe(`serviceproviders.${sp}`, () => { it('should be an object', () => { - expect(doipjs.serviceproviders[sp]).to.be.a('object') + expect(doipjs.serviceproviders.data[sp]).to.be.a('object') }) it('should have a RegExp instance named "reURI"', () => { - expect(doipjs.serviceproviders[sp].reURI).to.be.instanceof(RegExp) + expect(doipjs.serviceproviders.data[sp].reURI).to.be.instanceof(RegExp) }) it('should have a function named "processURI" (2 arguments)', () => { - expect(doipjs.serviceproviders[sp].processURI).to.be.a('function') - expect(doipjs.serviceproviders[sp].processURI).to.have.length(2) + expect(doipjs.serviceproviders.data[sp].processURI).to.be.a('function') + expect(doipjs.serviceproviders.data[sp].processURI).to.have.length(2) }) it('should have an array named "tests"', () => { - expect(doipjs.serviceproviders[sp].tests).to.be.instanceof(Array) + expect(doipjs.serviceproviders.data[sp].tests).to.be.instanceof(Array) }) - doipjs.serviceproviders[sp].tests.forEach((test, j) => { + doipjs.serviceproviders.data[sp].tests.forEach((test, j) => { if (test.shouldMatch) { it(`should match "${test.uri}"`, () => { - expect(doipjs.serviceproviders[sp].reURI.test(test.uri)).to.be.true + expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.true }) } else { it(`should not match "${test.uri}"`, () => { - expect(doipjs.serviceproviders[sp].reURI.test(test.uri)).to.be.false + expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.false }) } })