From 70fae5335cd1c9188a0e3b9c1db0d77323ff2928 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 3 Jun 2016 16:31:54 -0400 Subject: [PATCH] node: optional getAPIMethods and getPublishEvents --- lib/node.js | 42 +++++++++++++++++++++----------------- test/node.unit.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/lib/node.js b/lib/node.js index 1c97c361..7bd0e1a2 100644 --- a/lib/node.js +++ b/lib/node.js @@ -102,7 +102,9 @@ Node.prototype.getAllAPIMethods = function() { var methods = []; for(var i in this.services) { var mod = this.services[i]; - methods = methods.concat(mod.getAPIMethods()); + if (mod.getAPIMethods) { + methods = methods.concat(mod.getAPIMethods()); + } } return methods; }; @@ -115,7 +117,9 @@ Node.prototype.getAllPublishEvents = function() { var events = []; for (var i in this.services) { var mod = this.services[i]; - events = events.concat(mod.getPublishEvents()); + if (mod.getPublishEvents) { + events = events.concat(mod.getPublishEvents()); + } } return events; }; @@ -199,24 +203,26 @@ Node.prototype._startService = function(serviceInfo, callback) { } // add API methods - var methodData = service.getAPIMethods(); - var methodNameConflicts = []; - methodData.forEach(function(data) { - var name = data[0]; - var instance = data[1]; - var method = data[2]; + if (service.getAPIMethods) { + var methodData = service.getAPIMethods(); + var methodNameConflicts = []; + methodData.forEach(function(data) { + var name = data[0]; + var instance = data[1]; + var method = data[2]; - if (self[name]) { - methodNameConflicts.push(name); - } else { - self[name] = function() { - return method.apply(instance, arguments); - }; + if (self[name]) { + methodNameConflicts.push(name); + } else { + self[name] = function() { + return method.apply(instance, arguments); + }; + } + }); + + if (methodNameConflicts.length > 0) { + return callback(new Error('Existing API method(s) exists: ' + methodNameConflicts.join(', '))); } - }); - - if (methodNameConflicts.length > 0) { - return callback(new Error('Existing API method(s) exists: ' + methodNameConflicts.join(', '))); } callback(); diff --git a/test/node.unit.js b/test/node.unit.js index dc7b4a3e..de6ef4a5 100644 --- a/test/node.unit.js +++ b/test/node.unit.js @@ -139,6 +139,21 @@ describe('Bitcore Node', function() { var methods = node.getAllAPIMethods(); methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']); }); + it('will handle service without getAPIMethods defined', function() { + var node = new Node(baseConfig); + node.services = { + db: { + getAPIMethods: sinon.stub().returns(['db1', 'db2']), + }, + service1: {}, + service2: { + getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2']) + } + }; + + var methods = node.getAllAPIMethods(); + methods.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']); + }); }); describe('#getAllPublishEvents', function() { @@ -158,6 +173,20 @@ describe('Bitcore Node', function() { var events = node.getAllPublishEvents(); events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']); }); + it('will handle service without getPublishEvents defined', function() { + var node = new Node(baseConfig); + node.services = { + db: { + getPublishEvents: sinon.stub().returns(['db1', 'db2']), + }, + service1: {}, + service2: { + getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2']) + } + }; + var events = node.getAllPublishEvents(); + events.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']); + }); }); describe('#getServiceOrder', function() { @@ -343,6 +372,28 @@ describe('Bitcore Node', function() { }); }); + it('will handle service with getAPIMethods undefined', function(done) { + var node = new Node(baseConfig); + + function TestService() {} + util.inherits(TestService, BaseService); + TestService.prototype.start = sinon.stub().callsArg(0); + TestService.prototype.getData = function() {}; + + node.getServiceOrder = sinon.stub().returns([ + { + name: 'test', + module: TestService, + config: {} + }, + ]); + + node.start(function() { + TestService.prototype.start.callCount.should.equal(1); + done(); + }); + + }); }); describe('#getNetworkName', function() {