nats-hemera
Advanced tools
Comparing version 5.0.0 to 5.0.1
@@ -12,3 +12,3 @@ 'use strict' | ||
const Series = require('fastseries') | ||
const runExt = require('./extensionRunner').extRunner | ||
@@ -32,3 +32,2 @@ /** | ||
this.actMeta.middleware = addDef.middleware || [] | ||
this.series = Series() | ||
} | ||
@@ -85,11 +84,6 @@ | ||
run(request, response, cb) { | ||
this.series( | ||
{}, | ||
(fn, next) => { | ||
const result = fn(request, response, next) | ||
if (result && typeof result.then === 'function') { | ||
result.then(x => next()).catch(err => next(err)) | ||
} | ||
}, | ||
runExt( | ||
this.middleware, | ||
(fn, state, next) => fn(request, response, next), | ||
null, | ||
cb | ||
@@ -96,0 +90,0 @@ ) |
@@ -68,2 +68,3 @@ 'use strict' | ||
} | ||
/** | ||
@@ -70,0 +71,0 @@ * |
206
lib/index.js
@@ -24,4 +24,6 @@ /** | ||
const Avvio = require('avvio') | ||
const Series = require('fastseries') | ||
const runExt = require('./extensionRunner').extRunner | ||
const serverExtIterator = require('./extensionRunner').serverExtIterator | ||
const clientExtIterator = require('./extensionRunner').clientExtIterator | ||
const Errors = require('./errors') | ||
@@ -203,4 +205,2 @@ const Symbols = require('./symbols') | ||
this._series = Series() | ||
// use own logger | ||
@@ -689,18 +689,2 @@ if (this._config.logger) { | ||
* | ||
* @static | ||
* @param {any} fn | ||
* @param {any} cb | ||
* @memberof Hemera | ||
*/ | ||
_serverExtIterator(fn, cb) { | ||
const ret = fn(this, this._request, this.reply, cb) | ||
if (ret && typeof ret.then === 'function') { | ||
ret.then(cb).catch(cb) | ||
} | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} err | ||
@@ -772,6 +756,6 @@ * @returns | ||
hemera._series( | ||
runExt( | ||
hemera._extensionManager.onServerPreRequest, | ||
serverExtIterator, | ||
hemera, | ||
hemera._serverExtIterator, | ||
hemera._extensionManager['onServerPreRequest'], | ||
err => hemera._onServerPreRequestCompleted(err) | ||
@@ -832,12 +816,12 @@ ) | ||
self.emit('serverPreHandler', self) | ||
if (self._extensionManager['onServerPreHandler'].length) { | ||
self._series( | ||
if (self._extensionManager.onServerPreHandler.length) { | ||
runExt( | ||
self._extensionManager.onServerPreHandler, | ||
serverExtIterator, | ||
self, | ||
self._serverExtIterator, | ||
self._extensionManager['onServerPreHandler'], | ||
err => self._onServerPreHandlerCompleted(err) | ||
) | ||
return | ||
} else { | ||
self._onServerPreHandlerCompleted() | ||
} | ||
self._onServerPreHandlerCompleted() | ||
} else { | ||
@@ -874,40 +858,63 @@ const internalError = new Errors.PatternNotFound( | ||
let action = self.matchedAction.action.bind(self) | ||
// action middleware | ||
self.matchedAction.run(self._request, self.reply, err => | ||
self._afterMiddlewareHandler(err) | ||
) | ||
} | ||
// add middleware | ||
self.matchedAction.run(self._request, self.reply, err => { | ||
if (err) { | ||
const internalError = new Errors.HemeraError( | ||
'Action middleware', | ||
self.errorDetails | ||
).causedBy(err) | ||
self.log.error(internalError) | ||
self.emit('serverResponseError', err) | ||
self.reply.send(err) | ||
return | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} err | ||
* @memberof Hemera | ||
*/ | ||
_afterMiddlewareHandler(err) { | ||
const self = this | ||
// if request type is 'pubsub' we don't have to reply back | ||
if (self._request.payload.request.type === 'pubsub') { | ||
action(self._request.payload.pattern) | ||
self.reply.send() | ||
return | ||
} | ||
if (err) { | ||
const internalError = new Errors.HemeraError( | ||
'Action middleware', | ||
self.errorDetails | ||
).causedBy(err) | ||
self.log.error(internalError) | ||
self.emit('serverResponseError', err) | ||
self.reply.send(err) | ||
return | ||
} | ||
const result = action(self._request.payload.pattern, (err, result) => { | ||
const result = self._processServerAction() | ||
const isPromise = result && typeof result.then === 'function' | ||
if (isPromise) { | ||
result.then(x => self.reply.send(x)).catch(err => { | ||
if (err) { | ||
self._isValidError(err) | ||
} | ||
self.reply.send(err || result) | ||
self.reply.send(err) | ||
}) | ||
} | ||
} | ||
const isPromise = result && typeof result.then === 'function' | ||
if (isPromise) { | ||
result.then(x => self.reply.send(x)).catch(err => { | ||
if (err) { | ||
self._isValidError(err) | ||
} | ||
self.reply.send(err) | ||
}) | ||
/** | ||
* | ||
* | ||
* @returns | ||
* @memberof Hemera | ||
*/ | ||
_processServerAction() { | ||
const self = this | ||
let action = self.matchedAction.action.bind(self) | ||
// if request type is 'pubsub' we don't have to reply back | ||
if (self._request.payload.request.type === 'pubsub') { | ||
action(self._request.payload.pattern) | ||
self.reply.send() | ||
return | ||
} | ||
return action(self._request.payload.pattern, (err, result) => { | ||
if (err) { | ||
self._isValidError(err) | ||
} | ||
self.reply.send(err || result) | ||
}) | ||
@@ -994,3 +1001,5 @@ } | ||
if (!definition) { | ||
let error = new Errors.HemeraError('Pattern is required') | ||
let error = new Errors.HemeraError( | ||
'Pattern is required to define a server action' | ||
) | ||
this.log.error(error) | ||
@@ -1008,9 +1017,5 @@ throw error | ||
let error = new Errors.HemeraError( | ||
'Topic must not be empty and from type string', | ||
{ | ||
definition, | ||
app: this._config.name | ||
} | ||
'Topic is required and must be from type string', | ||
this.errorDetails | ||
) | ||
this.log.error(error) | ||
@@ -1097,9 +1102,10 @@ throw error | ||
_runOnAddHandler(addDefinition) { | ||
this._series( | ||
this, | ||
(fn, cb) => { | ||
runExt( | ||
this._onAddHandlers, | ||
(fn, state, next) => { | ||
fn(addDefinition) | ||
cb() | ||
next() | ||
}, | ||
this._onAddHandlers | ||
this, | ||
() => {} | ||
) | ||
@@ -1146,16 +1152,2 @@ } | ||
* | ||
* @param {any} fn | ||
* @param {any} cb | ||
* @memberof Hemera | ||
*/ | ||
_clientExtIterator(fn, cb) { | ||
const ret = fn(this, cb) | ||
if (ret && typeof ret.then === 'function') { | ||
ret.then(cb).catch(cb) | ||
} | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} response | ||
@@ -1189,6 +1181,6 @@ * | ||
self._series( | ||
runExt( | ||
self._extensionManager.onClientPostRequest, | ||
clientExtIterator, | ||
self, | ||
self._clientExtIterator, | ||
self._extensionManager['onClientPostRequest'], | ||
err => self._onClientPostRequestCompleted(err) | ||
@@ -1245,3 +1237,5 @@ ) | ||
if (!pattern) { | ||
let error = new Errors.HemeraError('Pattern is required') | ||
let error = new Errors.HemeraError( | ||
'Pattern is required to start a request' | ||
) | ||
this.log.error(error) | ||
@@ -1256,2 +1250,12 @@ throw error | ||
// topic is needed to subscribe on a subject in NATS | ||
if (!pattern.topic) { | ||
let error = new Errors.HemeraError( | ||
'Topic is required and must be from type string', | ||
this.errorDetails | ||
) | ||
this.log.error(error) | ||
throw error | ||
} | ||
// create new execution context | ||
@@ -1268,18 +1272,8 @@ let hemera = this.createContext() | ||
// topic is needed to subscribe on a subject in NATS | ||
if (!pattern.topic) { | ||
let error = new Errors.HemeraError( | ||
'Topic must not be empty and from type string', | ||
hemera.errorDetails | ||
) | ||
this.log.error(error) | ||
throw error | ||
} | ||
if (cb) { | ||
hemera._execute = cb.bind(hemera) | ||
hemera._series( | ||
runExt( | ||
hemera._extensionManager.onClientPreRequest, | ||
clientExtIterator, | ||
hemera, | ||
hemera._clientExtIterator, | ||
hemera._extensionManager['onClientPreRequest'], | ||
err => hemera._onClientPreRequestCompleted(err) | ||
@@ -1298,6 +1292,6 @@ ) | ||
hemera._series( | ||
runExt( | ||
hemera._extensionManager.onClientPreRequest, | ||
clientExtIterator, | ||
hemera, | ||
hemera._clientExtIterator, | ||
hemera._extensionManager['onClientPreRequest'], | ||
err => hemera._onClientPreRequestCompleted(err) | ||
@@ -1435,6 +1429,6 @@ ) | ||
self._series( | ||
runExt( | ||
self._extensionManager.onClientPostRequest, | ||
clientExtIterator, | ||
self, | ||
self._clientExtIterator, | ||
self._extensionManager['onClientPostRequest'], | ||
err => self._onClientTimeoutPostRequestCompleted(err) | ||
@@ -1441,0 +1435,0 @@ ) |
@@ -14,2 +14,4 @@ 'use strict' | ||
const Errio = require('errio') | ||
const runExt = require('./extensionRunner').extRunner | ||
const serverExtIterator = require('./extensionRunner').serverExtIterator | ||
@@ -119,14 +121,3 @@ /** | ||
* | ||
* @param {*} fn | ||
* @param {*} cb | ||
*/ | ||
_preResponseIterator(fn, cb) { | ||
const ret = fn(this, this._request, this.reply, cb) | ||
if (ret && typeof ret.then === 'function') { | ||
ret.then(cb).catch(cb) | ||
} | ||
} | ||
/** | ||
* | ||
*/ | ||
serverPreResponse() { | ||
@@ -138,11 +129,11 @@ const self = this | ||
if (self.hemera._extensionManager.onServerPreResponse.length) { | ||
self.hemera._series( | ||
runExt( | ||
self.hemera._extensionManager.onServerPreResponse, | ||
serverExtIterator, | ||
self.hemera, | ||
self._preResponseIterator, | ||
self.hemera._extensionManager['onServerPreResponse'], | ||
err => self._onServerPreResponseCompleted(err) | ||
) | ||
return | ||
} else { | ||
self._onServerPreResponseCompleted() | ||
} | ||
self._onServerPreResponseCompleted() | ||
} | ||
@@ -149,0 +140,0 @@ /** |
@@ -0,0 +0,0 @@ 'use strict' |
{ | ||
"name": "nats-hemera", | ||
"author": "Dustin Deus (https://github.com/StarpTech)", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"main": "lib/index.js", | ||
@@ -50,3 +50,2 @@ "homepage": "https://hemerajs.github.io/hemera/", | ||
"fast-safe-stringify": "2.0.x", | ||
"fastseries": "1.7.x", | ||
"heavy": "4.0.x", | ||
@@ -53,0 +52,0 @@ "joi": "11.1.x", |
10
22
3061
75995
- Removedfastseries@1.7.x
- Removedfastseries@1.7.2(transitive)