Comparing version 0.0.13 to 0.0.14
@@ -23,9 +23,7 @@ 'use strict'; | ||
this[name + 'Client'] = function (options, cb) { | ||
var client = new Client(transporter, options, cb); | ||
return client; | ||
return this.client(name, options, cb); | ||
}; | ||
this[name + 'Worker'] = function (options) { | ||
var worker = new Server(transporter, options); | ||
return worker; | ||
this[name + 'Server'] = function (options) { | ||
return this.server(name, options); | ||
}; | ||
@@ -43,3 +41,4 @@ | ||
} | ||
var client = new Client(this._transporters[name], options, cb); | ||
var clientTransporter = new this._transporters[name].ClientTransporter(options); | ||
var client = new Client(clientTransporter, options, cb); | ||
return client; | ||
@@ -55,6 +54,7 @@ }; | ||
} | ||
var worker = new Server(this._transporters[name], options); | ||
return worker; | ||
var serverTransporter = new this._transporters[name].ServerTransporter(options); | ||
var server = new Server(serverTransporter, options); | ||
return server; | ||
}; | ||
module.exports = Bograch; |
@@ -29,11 +29,13 @@ 'use strict'; | ||
this.call('methodList', function (err, methods) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
transporter.connect(function () { | ||
this.call('methodList', function (err, methods) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
this._existingMethods = methods; | ||
this.register(methods); | ||
this._isConnected = true; | ||
return cb(null, this); | ||
this._existingMethods = methods; | ||
this.register(methods); | ||
this._isConnected = true; | ||
return cb(null, this); | ||
}.bind(this)); | ||
}.bind(this)); | ||
@@ -40,0 +42,0 @@ } |
@@ -18,2 +18,4 @@ 'use strict'; | ||
this._methods = []; | ||
this._isUp = false; | ||
this.exports = {}; | ||
@@ -30,11 +32,11 @@ this._init(); | ||
Server.prototype.on = function (methodName, method) { | ||
Server.prototype.addMethod = function (methodName, method) { | ||
if (!methodName) { | ||
throw new TypeError('Bograch.on requires a methodName'); | ||
throw new TypeError('Bograch.addMethod requires a methodName'); | ||
} | ||
if (typeof methodName !== 'string') { | ||
throw new TypeError('Bograch.on requires a methodName of string type'); | ||
throw new TypeError('Bograch.addMethod requires a methodName of string type'); | ||
} | ||
if (method === null || typeof method !== 'function') { | ||
throw new TypeError('Bograch.on requires a callback function'); | ||
throw new TypeError('Bograch.addMethod requires a callback function'); | ||
} | ||
@@ -61,6 +63,29 @@ | ||
for (var methodName in scope) { | ||
this.on(methodName, scope[methodName]); | ||
this.addMethod(methodName, scope[methodName]); | ||
} | ||
}; | ||
Server.prototype.start = function (cb) { | ||
if (!this._isUp) { | ||
this.addMethods(this.exports); | ||
this._transporter.start(function (err) { | ||
if (!err) { | ||
this._isUp = true; | ||
} | ||
cb(err); | ||
}.bind(this)); | ||
} | ||
}; | ||
Server.prototype.end = function (cb) { | ||
if (this._isUp) { | ||
this._transporter.stop(function (err) { | ||
if (!err) { | ||
this._isUp = false; | ||
} | ||
cb(err); | ||
}.bind(this)); | ||
} | ||
}; | ||
module.exports = Server; |
{ | ||
"name": "bograch", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "A communication gateway for NodeJS microservices.", | ||
@@ -24,3 +24,4 @@ "main": "./lib", | ||
"chai": "~2.1.1" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var expect = require('chai').expect; | ||
var transporterMock = require('./transporter-mock'); | ||
@@ -57,6 +58,3 @@ describe('Bograch use', function () { | ||
it('should return client if transporter is found', function () { | ||
bo.use('foo', { | ||
name: 'foo', | ||
call: function () {} | ||
}); | ||
bo.use('foo', transporterMock); | ||
expect(bo.client('foo', { name: 'test' })).not.to.be.a('null'); | ||
@@ -84,8 +82,5 @@ }); | ||
it('should return server if transporter is found', function () { | ||
bo.use('foo', { | ||
name: 'foo', | ||
on: function () {} | ||
}); | ||
bo.use('foo', transporterMock); | ||
expect(bo.server('foo', { name: 'test' })).not.to.be.a('null'); | ||
}); | ||
}); |
@@ -5,8 +5,9 @@ 'use strict'; | ||
var expect = require('chai').expect; | ||
var TransporterMock = require('./transporter-mock'); | ||
var transporter = require('./transporter-mock'); | ||
var ClientTransporter = transporter.ClientTransporter; | ||
var bo = require('../lib'); | ||
var noop = function () {}; | ||
var transporter = new TransporterMock(); | ||
var boClient = new Client(transporter, { | ||
var clientTransporter = new ClientTransporter(); | ||
var boClient = new Client(clientTransporter, { | ||
name: 'test' | ||
@@ -68,3 +69,3 @@ }); | ||
boClient.register(['fooBar']); | ||
transporter._onCall.push(function (method, args, cb) { | ||
transporter.ee.once('call', function (method, args, cb) { | ||
expect(method).to.be.equal('test.fooBar'); | ||
@@ -77,3 +78,2 @@ expect(args.length).to.be.equal(3); | ||
done(); | ||
transporter._onCall.pop(); | ||
}); | ||
@@ -80,0 +80,0 @@ |
@@ -6,15 +6,16 @@ 'use strict'; | ||
var expect = require('chai').expect; | ||
var TransporterMock = require('./transporter-mock'); | ||
var ServerTransporter = require('./transporter-mock').ServerTransporter; | ||
var ClientTransporter = require('./transporter-mock').ClientTransporter; | ||
var noop = function () {}; | ||
var transporter = new TransporterMock(); | ||
var boServer = new Server(transporter, { | ||
var serverTransporter = new ServerTransporter(); | ||
var boServer = new Server(serverTransporter, { | ||
name: 'test' | ||
}); | ||
describe('Bograch server on', function () { | ||
describe('Bograch server addMethod', function () { | ||
it('should throw an error if no parameters were passed', function () { | ||
expect(function () { | ||
boServer.on(); | ||
}).to.throw(TypeError, 'Bograch.on requires a methodName'); | ||
boServer.addMethod(); | ||
}).to.throw(TypeError, 'Bograch.addMethod requires a methodName'); | ||
}); | ||
@@ -24,4 +25,4 @@ | ||
expect(function () { | ||
boServer.on('foo'); | ||
}).to.throw(TypeError, 'Bograch.on requires a callback function'); | ||
boServer.addMethod('foo'); | ||
}).to.throw(TypeError, 'Bograch.addMethod requires a callback function'); | ||
}); | ||
@@ -31,4 +32,4 @@ | ||
expect(function () { | ||
boServer.on('foo', 234); | ||
}).to.throw(TypeError, 'Bograch.on requires a callback function'); | ||
boServer.addMethod('foo', 234); | ||
}).to.throw(TypeError, 'Bograch.addMethod requires a callback function'); | ||
}); | ||
@@ -38,3 +39,3 @@ | ||
expect(function () { | ||
boServer.on('foo', noop); | ||
boServer.addMethod('foo', noop); | ||
}).not.to.throw(Error); | ||
@@ -45,3 +46,3 @@ }); | ||
describe('Bograch client/server communication', function () { | ||
var boClient = new Client(transporter, { | ||
var boClient = new Client(new ClientTransporter(), { | ||
name: 'test', | ||
@@ -52,3 +53,3 @@ ttl: 10 | ||
it('should pass all the arguments', function (done) { | ||
boServer.on('sum', function (a, b, cb) { | ||
boServer.addMethod('sum', function (a, b, cb) { | ||
expect(a).to.be.equal(32); | ||
@@ -63,3 +64,3 @@ expect(b).to.be.equal(54); | ||
it('should return error if remote method has an uncaught exception', function (done) { | ||
boServer.on('exception', function (cb) { | ||
boServer.addMethod('exception', function (cb) { | ||
throw 'foo'; | ||
@@ -74,3 +75,3 @@ }); | ||
it('should return error if remote method timed out', function (done) { | ||
boServer.on('timeout', function (cb) { | ||
boServer.addMethod('timeout', function (cb) { | ||
'I am not calling the callback function. Ever!'; | ||
@@ -77,0 +78,0 @@ }); |
'use strict'; | ||
function Transporter() { | ||
this.name = 'mock'; | ||
this._methods = {}; | ||
this._onCall = []; | ||
var EventEmitter = require('events').EventEmitter; | ||
var methods = {}; | ||
var ee = new EventEmitter(); | ||
// | ||
// ServerTransporter | ||
// | ||
function ServerTransporter() { | ||
} | ||
Transporter.prototype.on = function (method, cb) { | ||
this._methods[method] = cb; | ||
ServerTransporter.prototype.start = function (cb) { | ||
cb(); | ||
}; | ||
Transporter.prototype.call = function (method, args, cb) { | ||
this._onCall.forEach(function (fn) { | ||
fn(method, args, cb); | ||
}); | ||
if (this._methods[method]) { | ||
this._methods[method](args, cb); | ||
ServerTransporter.prototype.on = function (method, cb) { | ||
methods[method] = cb; | ||
}; | ||
// | ||
// ClientTransporter | ||
// | ||
function ClientTransporter() { | ||
} | ||
ClientTransporter.prototype.call = function (method, args, cb) { | ||
ee.emit('call', method, args, cb); | ||
if (methods[method]) { | ||
methods[method](args, cb); | ||
} | ||
}; | ||
module.exports = Transporter; | ||
ClientTransporter.prototype.connect = function (cb) { | ||
cb(); | ||
}; | ||
exports.name = 'mock'; | ||
exports.ServerTransporter = ServerTransporter; | ||
exports.ClientTransporter = ClientTransporter; | ||
exports.ee = ee; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 3 instances in 1 package
518
0
26431
17
1