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 = [];
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();

View File

@ -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() {