Skip to content

Commit ae7359c

Browse files
authored
Merge pull request #443 from braydonf/opt-service-methods
node: optional getAPIMethods and getPublishEvents
2 parents 4a220d8 + 70fae53 commit ae7359c

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

lib/node.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ Node.prototype.getAllAPIMethods = function() {
102102
var methods = [];
103103
for(var i in this.services) {
104104
var mod = this.services[i];
105-
methods = methods.concat(mod.getAPIMethods());
105+
if (mod.getAPIMethods) {
106+
methods = methods.concat(mod.getAPIMethods());
107+
}
106108
}
107109
return methods;
108110
};
@@ -115,7 +117,9 @@ Node.prototype.getAllPublishEvents = function() {
115117
var events = [];
116118
for (var i in this.services) {
117119
var mod = this.services[i];
118-
events = events.concat(mod.getPublishEvents());
120+
if (mod.getPublishEvents) {
121+
events = events.concat(mod.getPublishEvents());
122+
}
119123
}
120124
return events;
121125
};
@@ -204,24 +208,26 @@ Node.prototype._startService = function(serviceInfo, callback) {
204208
}
205209

206210
// add API methods
207-
var methodData = service.getAPIMethods();
208-
var methodNameConflicts = [];
209-
methodData.forEach(function(data) {
210-
var name = data[0];
211-
var instance = data[1];
212-
var method = data[2];
213-
214-
if (self[name]) {
215-
methodNameConflicts.push(name);
216-
} else {
217-
self[name] = function() {
218-
return method.apply(instance, arguments);
219-
};
211+
if (service.getAPIMethods) {
212+
var methodData = service.getAPIMethods();
213+
var methodNameConflicts = [];
214+
methodData.forEach(function(data) {
215+
var name = data[0];
216+
var instance = data[1];
217+
var method = data[2];
218+
219+
if (self[name]) {
220+
methodNameConflicts.push(name);
221+
} else {
222+
self[name] = function() {
223+
return method.apply(instance, arguments);
224+
};
225+
}
226+
});
227+
228+
if (methodNameConflicts.length > 0) {
229+
return callback(new Error('Existing API method(s) exists: ' + methodNameConflicts.join(', ')));
220230
}
221-
});
222-
223-
if (methodNameConflicts.length > 0) {
224-
return callback(new Error('Existing API method(s) exists: ' + methodNameConflicts.join(', ')));
225231
}
226232

227233
callback();

test/node.unit.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ describe('Bitcore Node', function() {
139139
var methods = node.getAllAPIMethods();
140140
methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
141141
});
142+
it('will handle service without getAPIMethods defined', function() {
143+
var node = new Node(baseConfig);
144+
node.services = {
145+
db: {
146+
getAPIMethods: sinon.stub().returns(['db1', 'db2']),
147+
},
148+
service1: {},
149+
service2: {
150+
getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2'])
151+
}
152+
};
153+
154+
var methods = node.getAllAPIMethods();
155+
methods.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']);
156+
});
142157
});
143158

144159
describe('#getAllPublishEvents', function() {
@@ -158,6 +173,20 @@ describe('Bitcore Node', function() {
158173
var events = node.getAllPublishEvents();
159174
events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
160175
});
176+
it('will handle service without getPublishEvents defined', function() {
177+
var node = new Node(baseConfig);
178+
node.services = {
179+
db: {
180+
getPublishEvents: sinon.stub().returns(['db1', 'db2']),
181+
},
182+
service1: {},
183+
service2: {
184+
getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2'])
185+
}
186+
};
187+
var events = node.getAllPublishEvents();
188+
events.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']);
189+
});
161190
});
162191

163192
describe('#getServiceOrder', function() {
@@ -370,6 +399,28 @@ describe('Bitcore Node', function() {
370399
});
371400

372401
});
402+
it('will handle service with getAPIMethods undefined', function(done) {
403+
var node = new Node(baseConfig);
404+
405+
function TestService() {}
406+
util.inherits(TestService, BaseService);
407+
TestService.prototype.start = sinon.stub().callsArg(0);
408+
TestService.prototype.getData = function() {};
409+
410+
node.getServiceOrder = sinon.stub().returns([
411+
{
412+
name: 'test',
413+
module: TestService,
414+
config: {}
415+
},
416+
]);
417+
418+
node.start(function() {
419+
TestService.prototype.start.callCount.should.equal(1);
420+
done();
421+
});
422+
423+
});
373424
});
374425

375426
describe('#getNetworkName', function() {

0 commit comments

Comments
 (0)