node: optional getAPIMethods and getPublishEvents

This commit is contained in:
Braydon Fuller 2016-06-03 16:31:54 -04:00
parent e87f628e7a
commit 70fae5335c
2 changed files with 75 additions and 18 deletions

View File

@ -102,7 +102,9 @@ Node.prototype.getAllAPIMethods = function() {
var methods = []; var methods = [];
for(var i in this.services) { for(var i in this.services) {
var mod = this.services[i]; var mod = this.services[i];
methods = methods.concat(mod.getAPIMethods()); if (mod.getAPIMethods) {
methods = methods.concat(mod.getAPIMethods());
}
} }
return methods; return methods;
}; };
@ -115,7 +117,9 @@ Node.prototype.getAllPublishEvents = function() {
var events = []; var events = [];
for (var i in this.services) { for (var i in this.services) {
var mod = this.services[i]; var mod = this.services[i];
events = events.concat(mod.getPublishEvents()); if (mod.getPublishEvents) {
events = events.concat(mod.getPublishEvents());
}
} }
return events; return events;
}; };
@ -199,24 +203,26 @@ Node.prototype._startService = function(serviceInfo, callback) {
} }
// add API methods // add API methods
var methodData = service.getAPIMethods(); if (service.getAPIMethods) {
var methodNameConflicts = []; var methodData = service.getAPIMethods();
methodData.forEach(function(data) { var methodNameConflicts = [];
var name = data[0]; methodData.forEach(function(data) {
var instance = data[1]; var name = data[0];
var method = data[2]; var instance = data[1];
var method = data[2];
if (self[name]) { if (self[name]) {
methodNameConflicts.push(name); methodNameConflicts.push(name);
} else { } else {
self[name] = function() { self[name] = function() {
return method.apply(instance, arguments); 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(); callback();

View File

@ -139,6 +139,21 @@ describe('Bitcore Node', function() {
var methods = node.getAllAPIMethods(); var methods = node.getAllAPIMethods();
methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']); 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() { describe('#getAllPublishEvents', function() {
@ -158,6 +173,20 @@ describe('Bitcore Node', function() {
var events = node.getAllPublishEvents(); var events = node.getAllPublishEvents();
events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']); 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() { 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() { describe('#getNetworkName', function() {