ee-soa-service-manager
Advanced tools
Comparing version 0.1.10 to 1.0.0
@@ -1,2 +0,3 @@ | ||
(function() { | ||
module.exports = require('./lib/ServiceManager'); | ||
})(); |
@@ -0,38 +1,24 @@ | ||
(function() { | ||
'use strict'; | ||
!function() { | ||
'use strict'; | ||
const log = require('ee-log'); | ||
const type = require('ee-types'); | ||
const EventEmitter = require('events'); | ||
var Class = require('ee-class') | ||
, log = require('ee-log') | ||
, EventEmitter = require('ee-event-emitter') | ||
, argv = require('ee-argv') | ||
, debug = argv.has('dev-service'); | ||
module.exports = new Class({ | ||
inherits: EventEmitter | ||
, init: function(options) { | ||
this.options = options || {}; | ||
this._controllerList = {}; | ||
this._serviceList = []; | ||
this._servicesLoadCount = 0; | ||
this._loaded = false; | ||
} | ||
module.exports = class ServiceManager extends EventEmitter { | ||
constructor() { | ||
super(); | ||
/** | ||
* returns a map of all controllers | ||
*/ | ||
, getControllerMap: function(map) { | ||
Object.keys(this._controllerList).forEach(function(serviceName) { | ||
this._controllerList[serviceName].getControllerMap(map); | ||
}.bind(this)); | ||
this.controllerMap = new Map(); | ||
this.services = new Set(); | ||
} | ||
return map; | ||
} | ||
@@ -42,92 +28,121 @@ | ||
, _hasController: function(controllerName) { | ||
return Object.hasOwnProperty.call(this._controllerList, controllerName); | ||
} | ||
/** | ||
* returns a map of all controllers | ||
*/ | ||
getControllerMap(map) { | ||
for (const serviceName of this.controllerMap.keys()) this.controllerMap.get(serviceName).getControllerMap(map); | ||
return map; | ||
} | ||
, request: function(req, res) { | ||
if(debug) log(req); | ||
// for(var property in req){ | ||
// var buffer = property; | ||
// if(typeof req[property] === 'function'){ | ||
// buffer += '()'; | ||
// } | ||
// log(buffer); | ||
// } | ||
var collection = req.getCollection(); | ||
if (this._hasController(collection)) { | ||
this._controllerList[collection].request(req, res); | ||
} | ||
else { | ||
if (debug) log.warn('The service manager doesn\'t know the controller «'+collection+'». It was not registered by any service!'); | ||
res.send(res.statusCodes.SERVICE_EXCEPTION, {error: 1, msg: 'The service manager doesn\'t know the controller «'+collection+'». It was not registered by any service!'}); | ||
} | ||
} | ||
, _handleRequest: function(req, res) { | ||
var collection = req.getCollection(); | ||
if(this._hasController(collection)) { | ||
this._controllerList[collection].request(req, res); | ||
/** | ||
* checks if a controlelr exists | ||
*/ | ||
hasController(controllerName) { | ||
return this.controllerMap.has(controllerName); | ||
} | ||
/** | ||
* incoming request handling | ||
*/ | ||
request(request, response) { | ||
const collection = request.getCollection(); | ||
if (this.hasController(collection)) this.controllerMap.get(collection).request(request, response); | ||
else response.send(response.statusCodes.SERVICE_EXCEPTION, {error: 1, msg: `Unknown controller '${collection}'!`}); | ||
} | ||
/** | ||
* register services | ||
*/ | ||
use(service) { | ||
if (!service.isService()) throw new Error('Attempt to register non service as service on the service manager. Make sure you register only services inheriting from the default service implementation!'); | ||
this.services.add(service); | ||
} | ||
/** | ||
* load the services | ||
*/ | ||
load() { | ||
return Promise.all(Array.from(this.services).map((service) => { | ||
// listen for outgoing requests | ||
service.on('request', (request, response) => { | ||
this.request(request, response); | ||
}); | ||
if (type.function(service.onLoad)) { | ||
// legacy | ||
return new Promise((resolve, reject) => { | ||
service.onLoad((err) => { | ||
if (err) reject(err); | ||
else { | ||
this.registerControllers(service); | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
else { | ||
// TODO: replace later --> why? | ||
if (debug) log.warn('The service manager doesn\'t know the controller «'+collection+'». It was not registered by any service!'); | ||
res.send(res.statusCodes.SERVICE_EXCEPTION, {error: 1, msg: 'The service manager doesn\'t know the controller «'+collection+'». It was not registered by any service!'}); | ||
//this.emit('request', req, res); | ||
} | ||
} | ||
, use: function(service) { | ||
if(!service.isService()) { | ||
throw new Error('Attempt to register non service as service on the service manager. Make sure you register only services inheriting from the default service implementation!'); | ||
// the new way to do things | ||
return service.ready().then(() => { | ||
this.registerControllers(service); | ||
return Promise.resolve(); | ||
}); | ||
} | ||
})); | ||
} | ||
var loaded = function() { | ||
this._servicesLoadCount--; | ||
if(this._servicesLoadCount === 0) { | ||
this._loaded = true; | ||
this.emit('load'); | ||
} | ||
}.bind(this); | ||
this._servicesLoadCount++; | ||
this._loaded = false; | ||
service.onLoad(function(err) { | ||
this._serviceList.push(service); | ||
this._addControllers(service.getControllerNames(), service); | ||
loaded(); | ||
}.bind(this)); | ||
service.on('request', this._handleRequest.bind(this)); | ||
} | ||
, useMiddleware: function(middleware) { | ||
this.onLoad(function(err) { | ||
this._serviceList.forEach(function(service) { | ||
service.useMiddleware(middleware); | ||
}.bind(this)); | ||
}.bind(this)); | ||
} | ||
, onLoad: function(callback) { | ||
if(this._loaded) callback(); | ||
else this.on('load', callback); | ||
} | ||
/** | ||
* add all controlelrs of a loaded service to the map | ||
*/ | ||
registerControllers(service) { | ||
service.getControllerNames().forEach((controllerName) => { | ||
if (this.controllerMap.has(controllerName)) throw new Error(`Cannot register controller '${controllerName}' for service '${service.name}': it was already registered by the service '${this.controllerMap.get(controllerName).name}'!`); | ||
this.controllerMap.set(controllerName, service); | ||
}); | ||
} | ||
, _addControllers: function(controllerNames, service) { | ||
controllerNames.forEach(function(controllerName) { | ||
this._addController(controllerName, service); | ||
}.bind(this)); | ||
} | ||
, _addController: function(controllerName, service) { | ||
if(this._hasController(controllerName)) throw new Error('The controller «'+controllerName+'» was already registered on the service manager. Every controller can only be registered once in the application!'); | ||
else this._controllerList[controllerName] = service; | ||
} | ||
}); | ||
}(); | ||
// legacy | ||
onLoad(callback) { | ||
this.load().then(() => { | ||
this.emit('load'); | ||
callback(); | ||
}).catch(callback); | ||
} | ||
}; | ||
})(); |
{ | ||
"name" : "ee-soa-service-manager" | ||
, "description" : "Service Manager for the ee-soa framework" | ||
, "version" : "0.1.10" | ||
, "author" : "Tobias Kneubühler <tobias@joinbox.com> (http://joinbox.com/)" | ||
, "contributors": [ | ||
"Tobias Kneubühler <tobias@joinbox.com> (http://joinbox.com/)" | ||
, "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
] | ||
, "licence" : "mit" | ||
, "os" : "!win" | ||
, "version" : "1.0.0" | ||
, "author" : "Michael van der Weg <michael@joinbox.com> (http://joinbox.com/)" | ||
, "license" : "MIT" | ||
, "repository": { | ||
@@ -17,3 +12,3 @@ "url" : "https://github.com/eventEmitter/ee-service-manager.git" | ||
, "engines": { | ||
"node" : ">= 0.10.26" | ||
"node" : ">=6" | ||
} | ||
@@ -24,18 +19,9 @@ , "bugs": { | ||
, "dependencies": { | ||
"ee-class" : "1.0.x" | ||
, "ee-log" : "0.2.x" | ||
, "ee-project" : "0.2.x" | ||
, "ee-types" : "0.1.x" | ||
, "ee-event-emitter" : "0.1.x" | ||
, "ee-argv" : "0.1.x" | ||
"ee-types" : "1.x" | ||
, "ee-log" : "0.3.x" | ||
} | ||
, "devDependencies": { | ||
"mocha" : "1.18.x" | ||
, "ee-travis" : "0.1.x" | ||
} | ||
, "devDependencies": {} | ||
, "optionalDependencies": {} | ||
, "scripts": { | ||
"test" : "./node_modules/mocha/bin/mocha --reporter spec" | ||
} | ||
, "keywords" : ["Service Oriented Architecture", "SOA", "Service", "ee-soa", "ee"] | ||
, "scripts": {} | ||
, "keywords" : ["ee-soa"] | ||
} |
Sorry, the diff of this file is not supported yet
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
0
1
6525
7
90
+ Addedee-class@1.4.0(transitive)
+ Addedee-log@0.3.11(transitive)
+ Addedee-types@1.0.02.2.1(transitive)
- Removedee-argv@0.1.x
- Removedee-class@1.0.x
- Removedee-event-emitter@0.1.x
- Removedee-project@0.2.x
- Removedee-class@0.4.01.0.9(transitive)
- Removedee-event-emitter@0.1.3(transitive)
- Removedee-log@0.2.23(transitive)
- Removedee-project@0.2.4(transitive)
- Removedee-types@0.1.3(transitive)
Updatedee-log@0.3.x
Updatedee-types@1.x