nats-hemera
Advanced tools
Comparing version 0.3.9 to 0.3.10
@@ -1,2 +0,2 @@ | ||
// | ||
'use strict'; | ||
@@ -9,4 +9,2 @@ /*! | ||
'use strict' | ||
// Errors messages | ||
@@ -34,2 +32,3 @@ module.exports = { | ||
INVALID_EXTENSION_TYPE: 'Invalid extension type' | ||
} | ||
}; | ||
//# sourceMappingURL=constants.js.map |
@@ -0,1 +1,7 @@ | ||
"use strict"; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/*! | ||
@@ -8,11 +14,17 @@ * hemera | ||
'use strict' | ||
var Decoder = function () { | ||
function Decoder() { | ||
_classCallCheck(this, Decoder); | ||
} | ||
class Decoder { | ||
_createClass(Decoder, null, [{ | ||
key: "decode", | ||
value: function decode(msg) { | ||
static decode(msg) { | ||
return Parse(msg); | ||
} | ||
}]); | ||
return Parse(msg) | ||
} | ||
} | ||
return Decoder; | ||
}(); | ||
@@ -22,17 +34,18 @@ function Parse(data) { | ||
if (!(this instanceof Parse)) { | ||
return new Parse(data) | ||
return new Parse(data); | ||
} | ||
this.error = null | ||
this.value = null | ||
this.error = null; | ||
this.value = null; | ||
try { | ||
this.value = JSON.parse(data) | ||
this.value = JSON.parse(data); | ||
} catch (error) { | ||
this.error = error | ||
this.error = error; | ||
} | ||
} | ||
module.exports = Decoder | ||
module.exports = Decoder; | ||
//# sourceMappingURL=decoder.js.map |
@@ -1,3 +0,7 @@ | ||
// | ||
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/*! | ||
@@ -10,19 +14,24 @@ * hemera | ||
'use strict' | ||
var Encoder = function () { | ||
function Encoder() { | ||
_classCallCheck(this, Encoder); | ||
} | ||
class Encoder { | ||
_createClass(Encoder, null, [{ | ||
key: 'encode', | ||
value: function encode(msg) { | ||
static encode(msg) { | ||
return stringify(msg); | ||
} | ||
}]); | ||
return stringify(msg) | ||
} | ||
} | ||
return Encoder; | ||
}(); | ||
module.exports = Encoder | ||
module.exports = Encoder; | ||
function stringify(obj) { | ||
decirc(obj, '', [], null) | ||
return JSON.stringify(obj) | ||
decirc(obj, '', [], null); | ||
return JSON.stringify(obj); | ||
} | ||
@@ -32,6 +41,6 @@ | ||
this.val = val | ||
this.k = k | ||
this.parent = parent | ||
this.count = 1 | ||
this.val = val; | ||
this.k = k; | ||
this.parent = parent; | ||
this.count = 1; | ||
} | ||
@@ -43,10 +52,10 @@ | ||
this.parent[this.k] = this.val | ||
this.parent[this.k] = this.val; | ||
} | ||
return '[Circular]' | ||
} | ||
return '[Circular]'; | ||
}; | ||
function decirc(val, k, stack, parent) { | ||
var keys, len, i | ||
var keys, len, i; | ||
@@ -56,25 +65,26 @@ if (typeof val !== 'object' || val === null) { | ||
// not an object, nothing to do | ||
return | ||
return; | ||
} else if (val instanceof Circle) { | ||
val.count++ | ||
return | ||
val.count++; | ||
return; | ||
} else if (parent) { | ||
if (~stack.indexOf(val)) { | ||
parent[k] = new Circle(val, k, parent) | ||
return | ||
parent[k] = new Circle(val, k, parent); | ||
return; | ||
} | ||
} | ||
stack.push(val) | ||
keys = Object.keys(val) | ||
len = keys.length | ||
i = 0 | ||
stack.push(val); | ||
keys = Object.keys(val); | ||
len = keys.length; | ||
i = 0; | ||
for (; i < len; i++) { | ||
k = keys[i] | ||
decirc(val[k], k, stack, val) | ||
k = keys[i]; | ||
decirc(val[k], k, stack, val); | ||
} | ||
stack.pop() | ||
stack.pop(); | ||
} | ||
//# sourceMappingURL=encoder.js.map |
@@ -1,2 +0,2 @@ | ||
// | ||
'use strict'; | ||
@@ -9,16 +9,13 @@ /*! | ||
'use strict' | ||
var SuperError = require('super-error'); | ||
const SuperError = require('super-error') | ||
var HemeraError = SuperError.subclass('HemeraError'), | ||
ParseError = HemeraError.subclass('HemeraParseError'), | ||
TimeoutError = HemeraError.subclass('TimeoutError'), | ||
ImplementationError = HemeraError.subclass('ImplementationError'), | ||
BusinessError = HemeraError.subclass('BusinessError'), | ||
FatalError = HemeraError.subclass('FatalError'), | ||
PatternNotFound = HemeraError.subclass('PatternNotFound'), | ||
PayloadValidationError = SuperError.subclass('PayloadValidationError'); | ||
const | ||
HemeraError = SuperError.subclass('HemeraError'), | ||
ParseError = HemeraError.subclass('HemeraParseError'), | ||
TimeoutError = HemeraError.subclass('TimeoutError'), | ||
ImplementationError = HemeraError.subclass('ImplementationError'), | ||
BusinessError = HemeraError.subclass('BusinessError'), | ||
FatalError = HemeraError.subclass('FatalError'), | ||
PatternNotFound = HemeraError.subclass('PatternNotFound'), | ||
PayloadValidationError = SuperError.subclass('PayloadValidationError') | ||
module.exports = { | ||
@@ -33,2 +30,3 @@ HemeraError, | ||
PayloadValidationError | ||
} | ||
}; | ||
//# sourceMappingURL=errors.js.map |
@@ -1,3 +0,5 @@ | ||
// | ||
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
/*! | ||
@@ -9,18 +11,19 @@ * hemera | ||
'use strict' | ||
var _items = require('items'); | ||
const Items = require('items') | ||
var _items2 = _interopRequireDefault(_items); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/** | ||
* @class Ext | ||
*/ | ||
class Ext { | ||
var Ext = function () { | ||
function Ext(type) { | ||
_classCallCheck(this, Ext); | ||
constructor(type) { | ||
this._handler = [] | ||
this._type = type | ||
this._handler = []; | ||
this._type = type; | ||
} | ||
@@ -34,41 +37,52 @@ /** | ||
*/ | ||
add(handler) { | ||
this._handler.push(handler) | ||
} | ||
_createClass(Ext, [{ | ||
key: 'add', | ||
value: function add(handler) { | ||
/** | ||
* | ||
* | ||
* @param {Array<Function>} handlers | ||
* | ||
* @memberOf Ext | ||
*/ | ||
addRange(handlers) { | ||
this._handler.push(handler); | ||
} | ||
this._handler = this._handler.concat(handlers) | ||
/** | ||
* | ||
* | ||
* @param {Array<Function>} handlers | ||
* | ||
* @memberOf Ext | ||
*/ | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} cb | ||
* | ||
* @memberOf Ext | ||
*/ | ||
invoke(ctx, cb) { | ||
}, { | ||
key: 'addRange', | ||
value: function addRange(handlers) { | ||
const each = (ext, next) => { | ||
this._handler = this._handler.concat(handlers); | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} cb | ||
* | ||
* @memberOf Ext | ||
*/ | ||
const bind = ctx | ||
}, { | ||
key: 'invoke', | ||
value: function invoke(ctx, cb) { | ||
ext.call(bind, next); | ||
var each = function each(ext, next) { | ||
var bind = ctx; | ||
ext.call(bind, next); | ||
}; | ||
_items2.default.serial(this._handler, each, cb.bind(ctx)); | ||
} | ||
}]); | ||
Items.serial(this._handler, each, cb.bind(ctx)) | ||
return Ext; | ||
}(); | ||
} | ||
} | ||
module.exports = Ext | ||
module.exports = Ext; | ||
//# sourceMappingURL=ext.js.map |
@@ -1,3 +0,13 @@ | ||
// | ||
'use strict'; | ||
var _util = require('./util'); | ||
var _util2 = _interopRequireDefault(_util); | ||
var _hoek = require('hoek'); | ||
var _hoek2 = _interopRequireDefault(_hoek); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/*! | ||
@@ -9,37 +19,32 @@ * hemera | ||
'use strict' | ||
const Util = require('./util') | ||
const Hoek = require('hoek') | ||
module.exports.onClientPreRequest = [function onClientPreRequest(next) { | ||
let ctx = this | ||
var ctx = this; | ||
let pattern = this._pattern | ||
var pattern = this._pattern; | ||
let prevCtx = this._prevContext | ||
let cleanPattern = this._cleanPattern | ||
let currentTime = Util.nowHrTime() | ||
var prevCtx = this._prevContext; | ||
var cleanPattern = this._cleanPattern; | ||
var currentTime = _util2.default.nowHrTime(); | ||
// shared context | ||
ctx.context$ = pattern.context$ || prevCtx.context$ | ||
ctx.context$ = pattern.context$ || prevCtx.context$; | ||
// set metadata by passed pattern or current message context | ||
ctx.meta$ = Hoek.merge(pattern.meta$ || {}, ctx.meta$) | ||
ctx.meta$ = _hoek2.default.merge(pattern.meta$ || {}, ctx.meta$); | ||
// is only passed by msg | ||
ctx.delegate$ = pattern.delegate$ || {} | ||
ctx.delegate$ = pattern.delegate$ || {}; | ||
// tracing | ||
ctx.trace$ = pattern.trace$ || {} | ||
ctx.trace$.parentSpanId = prevCtx.trace$.spanId | ||
ctx.trace$.traceId = prevCtx.trace$.traceId || Util.randomId() | ||
ctx.trace$.spanId = pattern.trace$ ? pattern.trace$.spanId : Util.randomId() | ||
ctx.trace$.timestamp = currentTime | ||
ctx.trace$.service = pattern.topic | ||
ctx.trace$.method = Util.pattern(pattern) | ||
ctx.trace$ = pattern.trace$ || {}; | ||
ctx.trace$.parentSpanId = prevCtx.trace$.spanId; | ||
ctx.trace$.traceId = prevCtx.trace$.traceId || _util2.default.randomId(); | ||
ctx.trace$.spanId = pattern.trace$ ? pattern.trace$.spanId : _util2.default.randomId(); | ||
ctx.trace$.timestamp = currentTime; | ||
ctx.trace$.service = pattern.topic; | ||
ctx.trace$.method = _util2.default.pattern(pattern); | ||
// request | ||
let request = { | ||
id: pattern.requestId$ || Util.randomId(), | ||
var request = { | ||
id: pattern.requestId$ || _util2.default.randomId(), | ||
parentId: ctx.request$.id, | ||
@@ -49,6 +54,6 @@ timestamp: currentTime, | ||
duration: 0 | ||
} | ||
}; | ||
// build msg | ||
let message = { | ||
var message = { | ||
pattern: cleanPattern, | ||
@@ -59,71 +64,70 @@ meta: ctx.meta$, | ||
request: request | ||
} | ||
}; | ||
ctx._message = message | ||
ctx._message = message; | ||
ctx._request = ctx._encoder.encode.call(ctx, ctx._message) | ||
ctx._request = ctx._encoder.encode.call(ctx, ctx._message); | ||
ctx.log.info(pattern, `ACT_OUTBOUND - ID:${String(ctx._message.request.id)}`) | ||
ctx.log.info(pattern, `ACT_OUTBOUND - ID:${String(ctx._message.request.id)}`); | ||
ctx.emit('onClientPreRequest', ctx) | ||
ctx.emit('onClientPreRequest', ctx); | ||
next() | ||
}] | ||
next(); | ||
}]; | ||
module.exports.onClientPostRequest = [function onClientPostRequest(next) { | ||
let ctx = this | ||
let pattern = this._pattern | ||
let msg = ctx._response.value | ||
var ctx = this; | ||
var pattern = this._pattern; | ||
var msg = ctx._response.value; | ||
// pass to act context | ||
ctx.request$ = msg.request || {} | ||
ctx.request$.service = pattern.topic | ||
ctx.request$.method = Util.pattern(pattern) | ||
ctx.trace$ = msg.trace || {} | ||
ctx.meta$ = msg.meta || {} | ||
ctx.request$ = msg.request || {}; | ||
ctx.request$.service = pattern.topic; | ||
ctx.request$.method = _util2.default.pattern(pattern); | ||
ctx.trace$ = msg.trace || {}; | ||
ctx.meta$ = msg.meta || {}; | ||
ctx.log.info(`ACT_INBOUND - ID:${ctx.request$.id} (${ctx.request$.duration / 1000000}ms)`) | ||
ctx.log.info(`ACT_INBOUND - ID:${ctx.request$.id} (${ctx.request$.duration / 1000000}ms)`); | ||
ctx.emit('onClientPostRequest', ctx) | ||
ctx.emit('onClientPostRequest', ctx); | ||
next() | ||
}] | ||
next(); | ||
}]; | ||
module.exports.onServerPreRequest = [function onServerPreRequest(next) { | ||
let msg = this._request.value | ||
let ctx = this | ||
var msg = this._request.value; | ||
var ctx = this; | ||
if (msg) { | ||
ctx.meta$ = msg.meta || {} | ||
ctx.trace$ = msg.trace || {} | ||
ctx.delegate$ = msg.delegate || {} | ||
ctx.request$ = msg.request || {} | ||
ctx.meta$ = msg.meta || {}; | ||
ctx.trace$ = msg.trace || {}; | ||
ctx.delegate$ = msg.delegate || {}; | ||
ctx.request$ = msg.request || {}; | ||
} | ||
ctx.emit('onServerPreRequest', ctx) | ||
ctx.emit('onServerPreRequest', ctx); | ||
next() | ||
}] | ||
next(); | ||
}]; | ||
module.exports.onServerPreHandler = [function onServerPreHandler(next) { | ||
let ctx = this | ||
var ctx = this; | ||
ctx.emit('onServerPreHandler', ctx) | ||
ctx.emit('onServerPreHandler', ctx); | ||
next() | ||
next(); | ||
}]; | ||
}] | ||
module.exports.onServerPreResponse = [function onServerPreResponse(next) { | ||
let ctx = this | ||
var ctx = this; | ||
ctx.emit('onServerPreResponse', ctx) | ||
ctx.emit('onServerPreResponse', ctx); | ||
next() | ||
}] | ||
next(); | ||
}]; | ||
//# sourceMappingURL=extensions.js.map |
1340
build/index.js
@@ -1,3 +0,69 @@ | ||
// | ||
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _events = require('events'); | ||
var _events2 = _interopRequireDefault(_events); | ||
var _bloomrun = require('bloomrun'); | ||
var _bloomrun2 = _interopRequireDefault(_bloomrun); | ||
var _errio = require('errio'); | ||
var _errio2 = _interopRequireDefault(_errio); | ||
var _hoek = require('hoek'); | ||
var _hoek2 = _interopRequireDefault(_hoek); | ||
var _heavy = require('heavy'); | ||
var _heavy2 = _interopRequireDefault(_heavy); | ||
var _lodash = require('lodash'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _errors = require('./errors'); | ||
var _errors2 = _interopRequireDefault(_errors); | ||
var _constants = require('./constants'); | ||
var _constants2 = _interopRequireDefault(_constants); | ||
var _ext = require('./ext'); | ||
var _ext2 = _interopRequireDefault(_ext); | ||
var _util = require('./util'); | ||
var _util2 = _interopRequireDefault(_util); | ||
var _extensions = require('./extensions'); | ||
var _extensions2 = _interopRequireDefault(_extensions); | ||
var _encoder = require('./encoder'); | ||
var _encoder2 = _interopRequireDefault(_encoder); | ||
var _decoder = require('./decoder'); | ||
var _decoder2 = _interopRequireDefault(_decoder); | ||
var _logger = require('./logger'); | ||
var _logger2 = _interopRequireDefault(_logger); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
/*! | ||
@@ -9,4 +75,2 @@ * hemera | ||
'use strict' | ||
/** | ||
@@ -16,20 +80,2 @@ * Module Dependencies | ||
const | ||
EventEmitter = require('events'), | ||
Bloomrun = require('bloomrun'), | ||
Errio = require('errio'), | ||
Hoek = require('hoek'), | ||
Heavy = require('heavy'), | ||
_ = require('lodash') | ||
const | ||
Errors = require('./errors'), | ||
Constants = require('./constants'), | ||
Ext = require('./ext'), | ||
Util = require('./util'), | ||
DefaultExtensions = require('./extensions'), | ||
DefaultEncoder = require('./encoder'), | ||
DefaultDecoder = require('./decoder'), | ||
DefaultLogger = require('./logger') | ||
// config | ||
@@ -44,3 +90,3 @@ var defaultConfig = { | ||
} | ||
} | ||
}; | ||
@@ -50,51 +96,23 @@ /** | ||
*/ | ||
class Hemera extends EventEmitter { | ||
var Hemera = function (_EventEmitter) { | ||
_inherits(Hemera, _EventEmitter); | ||
function Hemera(transport, params) { | ||
_classCallCheck(this, Hemera); | ||
var _this = _possibleConstructorReturn(this, (Hemera.__proto__ || Object.getPrototypeOf(Hemera)).call(this)); | ||
_this._config = _hoek2.default.applyToDefaults(defaultConfig, params || {}); | ||
_this._catalog = (0, _bloomrun2.default)(); | ||
_this._heavy = new _heavy2.default(_this._config.load); | ||
_this._transport = transport; | ||
_this._topics = {}; | ||
_this._exposition = {}; | ||
constructor(transport, params) { | ||
super() | ||
this._config = Hoek.applyToDefaults(defaultConfig, params || {}) | ||
this._catalog = Bloomrun() | ||
this._heavy = new Heavy(this._config.load) | ||
this._transport = transport | ||
this._topics = {} | ||
this._exposition = {} | ||
// special variables for new execution context | ||
this.context$ = {} | ||
this.meta$ = {} | ||
this.delegate$ = {} | ||
this.plugin$ = { | ||
_this.context$ = {}; | ||
_this.meta$ = {}; | ||
_this.delegate$ = {}; | ||
_this.plugin$ = { | ||
options: { | ||
@@ -106,5 +124,5 @@ payloadValidator: '' | ||
} | ||
} | ||
this.trace$ = {} | ||
this.request$ = { | ||
}; | ||
_this.trace$ = {}; | ||
_this.request$ = { | ||
duration: 0, | ||
@@ -115,25 +133,25 @@ parentId: '', | ||
id: '' | ||
} | ||
}; | ||
this._plugins = { | ||
core: this.plugin$.attributes | ||
} | ||
_this._plugins = { | ||
core: _this.plugin$.attributes | ||
}; | ||
this._encoder = { | ||
encode: DefaultEncoder.encode | ||
} | ||
this._decoder = { | ||
decode: DefaultDecoder.decode | ||
} | ||
_this._encoder = { | ||
encode: _encoder2.default.encode | ||
}; | ||
_this._decoder = { | ||
decode: _decoder2.default.decode | ||
}; | ||
// define extension points | ||
this._extensions = { | ||
onClientPreRequest: new Ext('onClientPreRequest'), | ||
onClientPostRequest: new Ext('onClientPostRequest'), | ||
onServerPreHandler: new Ext('onServerPreHandler'), | ||
onServerPreRequest: new Ext('onServerPreRequest'), | ||
onServerPreResponse: new Ext('onServerPreResponse') | ||
} | ||
_this._extensions = { | ||
onClientPreRequest: new _ext2.default('onClientPreRequest'), | ||
onClientPostRequest: new _ext2.default('onClientPostRequest'), | ||
onServerPreHandler: new _ext2.default('onServerPreHandler'), | ||
onServerPreRequest: new _ext2.default('onServerPreRequest'), | ||
onServerPreResponse: new _ext2.default('onServerPreResponse') | ||
}; | ||
this._heavy.start() | ||
_this._heavy.start(); | ||
@@ -143,3 +161,3 @@ /** | ||
*/ | ||
this._extensions.onClientPreRequest.addRange(DefaultExtensions.onClientPreRequest) | ||
_this._extensions.onClientPreRequest.addRange(_extensions2.default.onClientPreRequest); | ||
@@ -149,3 +167,3 @@ /** | ||
*/ | ||
this._extensions.onClientPostRequest.addRange(DefaultExtensions.onClientPostRequest) | ||
_this._extensions.onClientPostRequest.addRange(_extensions2.default.onClientPostRequest); | ||
@@ -155,3 +173,3 @@ /** | ||
*/ | ||
this._extensions.onServerPreRequest.addRange(DefaultExtensions.onServerPreRequest) | ||
_this._extensions.onServerPreRequest.addRange(_extensions2.default.onServerPreRequest); | ||
@@ -161,3 +179,3 @@ /** | ||
*/ | ||
this._extensions.onServerPreHandler.addRange(DefaultExtensions.onServerPreHandler) | ||
_this._extensions.onServerPreHandler.addRange(_extensions2.default.onServerPreHandler); | ||
@@ -167,7 +185,8 @@ /** | ||
*/ | ||
this._extensions.onServerPreResponse.addRange(DefaultExtensions.onServerPreResponse) | ||
_this._extensions.onServerPreResponse.addRange(_extensions2.default.onServerPreResponse); | ||
this.log = this._config.logger || new DefaultLogger({ | ||
level: this._config.logLevel | ||
}) | ||
_this.log = _this._config.logger || new _logger2.default({ | ||
level: _this._config.logLevel | ||
}); | ||
return _this; | ||
} | ||
@@ -180,746 +199,811 @@ | ||
*/ | ||
get plugins() { | ||
return this._plugins | ||
} | ||
/** | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
get catalog() { | ||
_createClass(Hemera, [{ | ||
key: 'expose', | ||
return this._catalog | ||
} | ||
/** | ||
* | ||
* | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
get load() { | ||
/** | ||
* | ||
* | ||
* @param {string} key | ||
* @param {mixed} object | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
value: function expose(key, object) { | ||
return this._heavy.load | ||
} | ||
var pluginName = this.plugin$.attributes.name; | ||
/** | ||
* | ||
* | ||
* @readonly | ||
* @type {Exposition} | ||
* @memberOf Hemera | ||
*/ | ||
get exposition() { | ||
if (!this._exposition[pluginName]) { | ||
return this._exposition | ||
} | ||
this._exposition[pluginName] = {}; | ||
this._exposition[pluginName][key] = object; | ||
} else { | ||
/** | ||
* | ||
* | ||
* @param {string} key | ||
* @param {mixed} object | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
expose(key, object) { | ||
this._exposition[pluginName][key] = object; | ||
} | ||
} | ||
let pluginName = this.plugin$.attributes.name | ||
/** | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
if (!this._exposition[pluginName]) { | ||
}, { | ||
key: 'ext', | ||
this._exposition[pluginName] = {} | ||
this._exposition[pluginName][key] = object | ||
} else { | ||
/** | ||
* | ||
* | ||
* @param {any} type | ||
* @param {any} handler | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
value: function ext(type, handler) { | ||
this._exposition[pluginName][key] = object | ||
if (!this._extensions[type]) { | ||
var error = new _errors2.default.HemeraError(_constants2.default.INVALID_EXTENSION_TYPE, { | ||
type | ||
}); | ||
this.log.error(error); | ||
throw error; | ||
} | ||
this._extensions[type].add(handler); | ||
} | ||
/** | ||
* @param {any} plugin | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
} | ||
}, { | ||
key: 'use', | ||
value: function use(params) { | ||
/** | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
get transport() { | ||
if (this._plugins[params.attributes.name]) { | ||
var error = new _errors2.default.HemeraError(_constants2.default.PLUGIN_ALREADY_IN_USE, { | ||
plugin: params.attributes.name | ||
}); | ||
this.log.error(error); | ||
throw error; | ||
} | ||
return this._transport | ||
} | ||
// create new execution context | ||
var ctx = this.createContext(); | ||
ctx.plugin$ = {}; | ||
ctx.plugin$.attributes = params.attributes || {}; | ||
ctx.plugin$.attributes.dependencies = params.attributes.dependencies || []; | ||
ctx.plugin$.options = params.options || {}; | ||
ctx.plugin$.options.payloadValidator = params.options.payloadValidator || ''; | ||
/** | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
get topics() { | ||
return this._topics | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} type | ||
* @param {any} handler | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
ext(type, handler) { | ||
params.plugin.call(ctx, params.options); | ||
if (!this._extensions[type]) { | ||
let error = new Errors.HemeraError(Constants.INVALID_EXTENSION_TYPE, { | ||
type | ||
}) | ||
this.log.error(error) | ||
throw (error) | ||
this.log.info(params.attributes.name, _constants2.default.PLUGIN_ADDED); | ||
this._plugins[params.attributes.name] = ctx.plugin$.attributes; | ||
} | ||
this._extensions[type].add(handler) | ||
/** | ||
* | ||
* | ||
* @param {any} options | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
} | ||
/** | ||
* @param {any} plugin | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
use(params) { | ||
}, { | ||
key: 'setOption', | ||
value: function setOption(key, value) { | ||
if (this._plugins[params.attributes.name]) { | ||
let error = new Errors.HemeraError(Constants.PLUGIN_ALREADY_IN_USE, { | ||
plugin: params.attributes.name | ||
}) | ||
this.log.error(error) | ||
throw (error) | ||
this.plugin$.options[key] = value; | ||
} | ||
// create new execution context | ||
let ctx = this.createContext() | ||
ctx.plugin$ = {} | ||
ctx.plugin$.attributes = params.attributes || {} | ||
ctx.plugin$.attributes.dependencies = params.attributes.dependencies || [] | ||
ctx.plugin$.options = params.options || {} | ||
ctx.plugin$.options.payloadValidator = params.options.payloadValidator || '' | ||
/** | ||
* | ||
* | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
params.plugin.call(ctx, params.options) | ||
}, { | ||
key: 'setConfig', | ||
value: function setConfig(key, value) { | ||
this.log.info(params.attributes.name, Constants.PLUGIN_ADDED) | ||
this._plugins[params.attributes.name] = ctx.plugin$.attributes | ||
this._config[key] = value; | ||
} | ||
} | ||
/** | ||
* @memberOf Hemera | ||
*/ | ||
/** | ||
* | ||
* | ||
* @param {any} options | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
setOption(key, value) { | ||
}, { | ||
key: 'fatal', | ||
value: function fatal() { | ||
this.plugin$.options[key] = value | ||
} | ||
this.close(); | ||
/** | ||
* | ||
* | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
setConfig(key, value) { | ||
process.exit(1); | ||
} | ||
this._config[key] = value | ||
} | ||
/** | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
/** | ||
* @memberOf Hemera | ||
*/ | ||
fatal() { | ||
}, { | ||
key: 'ready', | ||
value: function ready(cb) { | ||
var _this2 = this; | ||
this.close() | ||
this._transport.on('connect', function () { | ||
process.exit(1) | ||
} | ||
_this2.log.info(_constants2.default.TRANSPORT_CONNECTED); | ||
cb.call(_this2); | ||
}); | ||
} | ||
/** | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
ready(cb) { | ||
/** | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
this._transport.on('connect', () => { | ||
}, { | ||
key: 'timeout', | ||
value: function timeout() { | ||
this.log.info(Constants.TRANSPORT_CONNECTED) | ||
cb.call(this) | ||
}) | ||
} | ||
return this.transport.timeout.apply(this.transport, arguments); | ||
} | ||
/** | ||
* Add response | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
/** | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
timeout() { | ||
}, { | ||
key: 'send', | ||
value: function send() { | ||
return this.transport.timeout.apply(this.transport, arguments) | ||
} | ||
/** | ||
* Add response | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
send() { | ||
return this.transport.publish.apply(this.transport, arguments); | ||
} | ||
return this.transport.publish.apply(this.transport, arguments) | ||
} | ||
/** | ||
* Act | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
/** | ||
* Act | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
sendRequest() { | ||
}, { | ||
key: 'sendRequest', | ||
value: function sendRequest() { | ||
return this.transport.request.apply(this.transport, arguments) | ||
} | ||
return this.transport.request.apply(this.transport, arguments); | ||
} | ||
/** | ||
* | ||
* | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
_buildMessage() { | ||
/** | ||
* | ||
* | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
let result = this._response | ||
}, { | ||
key: '_buildMessage', | ||
value: function _buildMessage() { | ||
let message = { | ||
meta: this.meta$ || {}, | ||
trace: this.trace$ || {}, | ||
request: this.request$, | ||
result: result instanceof Error ? null : result, | ||
error: result instanceof Error ? Errio.toObject(result) : null | ||
var result = this._response; | ||
var message = { | ||
meta: this.meta$ || {}, | ||
trace: this.trace$ || {}, | ||
request: this.request$, | ||
result: result instanceof Error ? null : result, | ||
error: result instanceof Error ? _errio2.default.toObject(result) : null | ||
}; | ||
var endTime = _util2.default.nowHrTime(); | ||
message.request.duration = endTime - message.request.timestamp; | ||
message.trace.duration = endTime - message.request.timestamp; | ||
this._message = message; | ||
} | ||
/** | ||
* | ||
* | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
let endTime = Util.nowHrTime() | ||
message.request.duration = endTime - message.request.timestamp | ||
message.trace.duration = endTime - message.request.timestamp | ||
}, { | ||
key: 'finish', | ||
value: function finish() { | ||
this._message = message | ||
var self = this; | ||
} | ||
/** | ||
* | ||
* | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
finish() { | ||
self._extensions.onServerPreResponse.invoke(self, function (err) { | ||
let self = this; | ||
// check if an error was already catched | ||
if (self._response instanceof Error) { | ||
self._extensions.onServerPreResponse.invoke(self, function (err) { | ||
self.log.error(self._response); | ||
self._buildMessage(); | ||
} | ||
// check for an extension error | ||
else if (err) { | ||
// check if an error was already catched | ||
if (self._response instanceof Error) { | ||
var error = new _errors2.default.HemeraError(_constants2.default.EXTENSION_ERROR).causedBy(err); | ||
self._response = error; | ||
self.log.error(self._response); | ||
self._buildMessage(); | ||
} else { | ||
self.log.error(self._response) | ||
self._buildMessage() | ||
} | ||
// check for an extension error | ||
else if (err) { | ||
self._buildMessage(); | ||
} | ||
let error = new Errors.HemeraError(Constants.EXTENSION_ERROR).causedBy(err) | ||
self._response = error | ||
self.log.error(self._response) | ||
self._buildMessage() | ||
} else { | ||
// indicates that an error occurs and that the program should exit | ||
if (self._shouldCrash) { | ||
self._buildMessage() | ||
} | ||
if (self._replyTo) { | ||
// indicates that an error occurs and that the program should exit | ||
if (self._shouldCrash) { | ||
var msg = self._encoder.encode.call(self, self._message); | ||
if (self._replyTo) { | ||
// send error back to callee | ||
return self.send(self._replyTo, msg, function () { | ||
const msg = self._encoder.encode.call(self, self._message) | ||
// let it crash | ||
if (self._config.crashOnFatal) { | ||
// send error back to callee | ||
return self.send(self._replyTo, msg, () => { | ||
self.fatal(); | ||
} | ||
}); | ||
} else if (self._config.crashOnFatal) { | ||
// let it crash | ||
if (self._config.crashOnFatal) { | ||
return self.fatal(); | ||
} | ||
} | ||
self.fatal() | ||
} | ||
}) | ||
if (self._replyTo) { | ||
} else if (self._config.crashOnFatal) { | ||
var _msg = self._encoder.encode.call(self, self._message); | ||
return self.fatal() | ||
return this.send(this._replyTo, _msg); | ||
} | ||
}); | ||
} | ||
} | ||
/** | ||
* @param {any} topic | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
if (self._replyTo) { | ||
}, { | ||
key: 'subscribe', | ||
value: function subscribe(topic, subToMany, maxMessages) { | ||
var _this3 = this; | ||
const msg = self._encoder.encode.call(self, self._message) | ||
var self = this; | ||
return this.send(this._replyTo, msg) | ||
// avoid duplicate subscribers of the emit stream | ||
// we use one subscriber per topic | ||
if (self._topics[topic]) { | ||
return; | ||
} | ||
}) | ||
var handler = function handler(request, replyTo) { | ||
} | ||
// create new execution context | ||
var ctx = _this3.createContext(); | ||
ctx._shouldCrash = false; | ||
ctx._replyTo = replyTo; | ||
ctx._request = self._decoder.decode.call(ctx, request); | ||
ctx._pattern = {}; | ||
ctx._actMeta = {}; | ||
/** | ||
* @param {any} topic | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
subscribe(topic, subToMany, maxMessages) { | ||
//Extension point 'onServerPreRequest' | ||
self._extensions.onServerPreRequest.invoke(ctx, function (err) { | ||
let self = this | ||
var self = this; | ||
// avoid duplicate subscribers of the emit stream | ||
// we use one subscriber per topic | ||
if (self._topics[topic]) { | ||
return | ||
} | ||
if (err) { | ||
let handler = (request, replyTo) => { | ||
var error = new _errors2.default.HemeraError(_constants2.default.EXTENSION_ERROR).causedBy(err); | ||
// create new execution context | ||
let ctx = this.createContext() | ||
ctx._shouldCrash = false | ||
ctx._replyTo = replyTo | ||
ctx._request = self._decoder.decode.call(ctx, request) | ||
ctx._pattern = {} | ||
ctx._actMeta = {} | ||
self.log.error(error); | ||
self._response = error; | ||
//Extension point 'onServerPreRequest' | ||
self._extensions.onServerPreRequest.invoke(ctx, function (err) { | ||
return self.finish(); | ||
} | ||
let self = this | ||
// invalid payload | ||
if (self._request.error) { | ||
if (err) { | ||
var _error = new _errors2.default.ParseError(_constants2.default.PAYLOAD_PARSING_ERROR, { | ||
topic | ||
}).causedBy(self._request.error); | ||
let error = new Errors.HemeraError(Constants.EXTENSION_ERROR).causedBy(err) | ||
return self.finish(replyTo, _error); | ||
} | ||
self.log.error(error) | ||
self._response = error | ||
var requestType = self._request.value.request.type; | ||
self._pattern = self._request.value.pattern; | ||
self._actMeta = self._catalog.lookup(self._pattern); | ||
return self.finish() | ||
} | ||
// check if a handler is registered with this pattern | ||
if (self._actMeta) { | ||
// invalid payload | ||
if (self._request.error) { | ||
self._extensions.onServerPreHandler.invoke(ctx, function (err) { | ||
let error = new Errors.ParseError(Constants.PAYLOAD_PARSING_ERROR, { | ||
topic | ||
}).causedBy(self._request.error) | ||
if (err) { | ||
return self.finish(replyTo, error) | ||
} | ||
self._response = new _errors2.default.HemeraError(_constants2.default.EXTENSION_ERROR).causedBy(err); | ||
let requestType = self._request.value.request.type | ||
self._pattern = self._request.value.pattern | ||
self._actMeta = self._catalog.lookup(self._pattern) | ||
self.log.error(self._response); | ||
// check if a handler is registered with this pattern | ||
if (self._actMeta) { | ||
return self.finish(); | ||
} | ||
self._extensions.onServerPreHandler.invoke(ctx, function (err) { | ||
try { | ||
if (err) { | ||
var action = self._actMeta.action.bind(self); | ||
self._response = new Errors.HemeraError(Constants.EXTENSION_ERROR).causedBy(err) | ||
// if request type is 'pubsub' we dont have to answer | ||
if (requestType === 'pubsub') { | ||
self.log.error(self._response) | ||
action(self._request.value.pattern); | ||
return self.finish() | ||
} | ||
return self.finish(); | ||
} | ||
try { | ||
// call action | ||
action(self._request.value.pattern, function (err, resp) { | ||
let action = self._actMeta.action.bind(self) | ||
if (err) { | ||
// if request type is 'pubsub' we dont have to answer | ||
if (requestType === 'pubsub') { | ||
self._response = new _errors2.default.BusinessError(_constants2.default.IMPLEMENTATION_ERROR, { | ||
pattern: self._pattern | ||
}).causedBy(err); | ||
action(self._request.value.pattern) | ||
return self.finish(); | ||
} | ||
return self.finish() | ||
self._response = resp; | ||
self.finish(); | ||
}); | ||
} catch (err) { | ||
self._response = new _errors2.default.ImplementationError(_constants2.default.IMPLEMENTATION_ERROR, { | ||
pattern: self._pattern | ||
}).causedBy(err); | ||
self._shouldCrash = true; | ||
self.finish(); | ||
} | ||
}); | ||
} else { | ||
// call action | ||
action(self._request.value.pattern, (err, resp) => { | ||
self.log.info({ | ||
topic | ||
}, _constants2.default.PATTERN_NOT_FOUND); | ||
if (err) { | ||
self._response = new _errors2.default.PatternNotFound(_constants2.default.PATTERN_NOT_FOUND, { | ||
pattern: self._pattern | ||
}); | ||
self._response = new Errors.BusinessError(Constants.IMPLEMENTATION_ERROR, { | ||
pattern: self._pattern | ||
}).causedBy(err) | ||
// send error back to callee | ||
self.finish(); | ||
} | ||
}); | ||
}; | ||
return self.finish() | ||
} | ||
// standard pubsub with optional max proceed messages | ||
if (subToMany) { | ||
self._response = resp | ||
self.transport.subscribe(topic, { | ||
max: maxMessages | ||
}, handler); | ||
} else { | ||
self.finish() | ||
}) | ||
// queue group names allow load balancing of services | ||
self.transport.subscribe(topic, { | ||
'queue': 'queue.' + topic, | ||
max: maxMessages | ||
}, handler); | ||
} | ||
} catch (err) { | ||
this._topics[topic] = true; | ||
} | ||
self._response = new Errors.ImplementationError(Constants.IMPLEMENTATION_ERROR, { | ||
pattern: self._pattern | ||
}).causedBy(err) | ||
/** | ||
* @param {any} pattern | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
self._shouldCrash = true | ||
}, { | ||
key: 'add', | ||
value: function add(pattern, cb) { | ||
self.finish() | ||
} | ||
var hasCallback = _lodash2.default.isFunction(cb); | ||
}) | ||
// topic is needed to subscribe on a subject in NATS | ||
if (!pattern.topic) { | ||
} else { | ||
var error = new _errors2.default.HemeraError(_constants2.default.NO_TOPIC_TO_SUBSCRIBE, { | ||
pattern | ||
}); | ||
self.log.info({ | ||
topic | ||
}, Constants.PATTERN_NOT_FOUND) | ||
this.log.error(error); | ||
throw error; | ||
} | ||
self._response = new Errors.PatternNotFound(Constants.PATTERN_NOT_FOUND, { | ||
pattern: self._pattern | ||
}) | ||
if (!hasCallback) { | ||
// send error back to callee | ||
self.finish() | ||
} | ||
var _error2 = new _errors2.default.HemeraError(_constants2.default.MISSING_IMPLEMENTATION, { | ||
pattern | ||
}); | ||
}) | ||
this.log.error(_error2); | ||
throw _error2; | ||
} | ||
} | ||
var origPattern = _lodash2.default.cloneDeep(pattern); | ||
// standard pubsub with optional max proceed messages | ||
if (subToMany) { | ||
var schema = {}; | ||
self.transport.subscribe(topic, { | ||
max: maxMessages | ||
}, handler) | ||
} else { | ||
// remove objects (rules) from pattern and extract scheme | ||
_lodash2.default.each(pattern, function (v, k) { | ||
// queue group names allow load balancing of services | ||
self.transport.subscribe(topic, { | ||
'queue': 'queue.' + topic, | ||
max: maxMessages | ||
}, handler) | ||
} | ||
if (_lodash2.default.isObject(v)) { | ||
schema[k] = _lodash2.default.clone(v); | ||
delete origPattern[k]; | ||
} | ||
}); | ||
this._topics[topic] = true | ||
// remove special $ variables from pattern | ||
origPattern = _util2.default.cleanPattern(origPattern); | ||
} | ||
// create message object which represent the object behind the matched pattern | ||
var actMeta = { | ||
schema: schema, | ||
pattern: origPattern, | ||
action: cb, | ||
plugin: this.plugin$ | ||
}; | ||
/** | ||
* @param {any} pattern | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
add(pattern, cb) { | ||
var handler = this._catalog.lookup(origPattern); | ||
let hasCallback = _.isFunction(cb) | ||
// check if pattern is already registered | ||
if (handler) { | ||
// topic is needed to subscribe on a subject in NATS | ||
if (!pattern.topic) { | ||
var _error3 = new _errors2.default.HemeraError(_constants2.default.PATTERN_ALREADY_IN_USE, { | ||
pattern | ||
}); | ||
let error = new Errors.HemeraError(Constants.NO_TOPIC_TO_SUBSCRIBE, { | ||
pattern | ||
}) | ||
this.log.error(_error3); | ||
throw _error3; | ||
} | ||
this.log.error(error) | ||
throw (error) | ||
} | ||
// add to bloomrun | ||
this._catalog.add(origPattern, actMeta); | ||
if (!hasCallback) { | ||
this.log.info(origPattern, _constants2.default.ADD_ADDED); | ||
let error = new Errors.HemeraError(Constants.MISSING_IMPLEMENTATION, { | ||
pattern | ||
}) | ||
this.log.error(error) | ||
throw (error) | ||
// subscribe on topic | ||
this.subscribe(pattern.topic, pattern.pubsub$, pattern.maxMessages$); | ||
} | ||
let origPattern = _.cloneDeep(pattern) | ||
/** | ||
* @param {any} pattern | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
let schema = {} | ||
}, { | ||
key: 'act', | ||
value: function act(pattern, cb) { | ||
// remove objects (rules) from pattern and extract scheme | ||
_.each(pattern, function (v, k) { | ||
// topic is needed to subscribe on a subject in NATS | ||
if (!pattern.topic) { | ||
if (_.isObject(v)) { | ||
schema[k] = _.clone(v) | ||
delete origPattern[k] | ||
var error = new _errors2.default.HemeraError(_constants2.default.NO_TOPIC_TO_REQUEST, { | ||
pattern | ||
}); | ||
this.log.error(error); | ||
throw error; | ||
} | ||
}) | ||
// remove special $ variables from pattern | ||
origPattern = Util.cleanPattern(origPattern) | ||
// create new execution context | ||
var ctx = this.createContext(); | ||
ctx._pattern = pattern; | ||
ctx._prevContext = this; | ||
ctx._cleanPattern = _util2.default.cleanPattern(pattern); | ||
ctx._response = {}; | ||
ctx._request = {}; | ||
// create message object which represent the object behind the matched pattern | ||
let actMeta = { | ||
schema: schema, | ||
pattern: origPattern, | ||
action: cb, | ||
plugin: this.plugin$ | ||
} | ||
ctx._extensions.onClientPreRequest.invoke(ctx, function onPreRequest(err) { | ||
let handler = this._catalog.lookup(origPattern) | ||
var self = this; | ||
// check if pattern is already registered | ||
if (handler) { | ||
var hasCallback = _lodash2.default.isFunction(cb); | ||
let error = new Errors.HemeraError(Constants.PATTERN_ALREADY_IN_USE, { | ||
pattern | ||
}) | ||
if (err) { | ||
this.log.error(error) | ||
throw (error) | ||
} | ||
var _error4 = new _errors2.default.HemeraError(_constants2.default.EXTENSION_ERROR).causedBy(err); | ||
// add to bloomrun | ||
this._catalog.add(origPattern, actMeta) | ||
self.log.error(_error4); | ||
this.log.info(origPattern, Constants.ADD_ADDED) | ||
if (hasCallback) { | ||
return cb.call(self, _error4); | ||
} | ||
// subscribe on topic | ||
this.subscribe(pattern.topic, pattern.pubsub$, pattern.maxMessages$) | ||
} | ||
return; | ||
} | ||
/** | ||
* @param {any} pattern | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
act(pattern, cb) { | ||
// use simple publish mechanism instead to fire a request | ||
if (pattern.pubsub$ === true) { | ||
// topic is needed to subscribe on a subject in NATS | ||
if (!pattern.topic) { | ||
if (hasCallback) { | ||
self.log.info(_constants2.default.PUB_CALLBACK_REDUNDANT); | ||
} | ||
let error = new Errors.HemeraError(Constants.NO_TOPIC_TO_REQUEST, { | ||
pattern | ||
}) | ||
self.send(pattern.topic, self._request); | ||
} else { | ||
this.log.error(error) | ||
throw (error) | ||
} | ||
// send request | ||
var sid = self.sendRequest(pattern.topic, self._request, function (response) { | ||
// create new execution context | ||
let ctx = this.createContext() | ||
ctx._pattern = pattern | ||
ctx._prevContext = this | ||
ctx._cleanPattern = Util.cleanPattern(pattern) | ||
ctx._response = {} | ||
ctx._request = {} | ||
self._response = self._decoder.decode.call(ctx, response); | ||
ctx._extensions.onClientPreRequest.invoke(ctx, function onPreRequest(err) { | ||
try { | ||
let self = this | ||
// if payload is invalid | ||
if (self._response.error) { | ||
let hasCallback = _.isFunction(cb) | ||
var _error5 = new _errors2.default.ParseError(_constants2.default.PAYLOAD_PARSING_ERROR, { | ||
pattern: self._cleanPattern | ||
}).causedBy(self._response.error); | ||
if (err) { | ||
self.log.error(_error5); | ||
let error = new Errors.HemeraError(Constants.EXTENSION_ERROR).causedBy(err) | ||
if (hasCallback) { | ||
return cb.call(self, _error5); | ||
} | ||
} | ||
self.log.error(error) | ||
self._extensions.onClientPostRequest.invoke(ctx, function (err) { | ||
if (hasCallback) { | ||
return cb.call(self, error) | ||
} | ||
if (err) { | ||
return | ||
} | ||
var _error6 = new _errors2.default.HemeraError(_constants2.default.EXTENSION_ERROR).causedBy(err); | ||
// use simple publish mechanism instead to fire a request | ||
if (pattern.pubsub$ === true) { | ||
self.log.error(_error6); | ||
if (hasCallback) { | ||
self.log.info(Constants.PUB_CALLBACK_REDUNDANT) | ||
} | ||
if (hasCallback) { | ||
return cb.call(self, _error6); | ||
} | ||
self.send(pattern.topic, self._request) | ||
} else { | ||
return; | ||
} | ||
// send request | ||
let sid = self.sendRequest(pattern.topic, self._request, (response) => { | ||
if (hasCallback) { | ||
self._response = self._decoder.decode.call(ctx, response) | ||
if (self._response.value.error) { | ||
try { | ||
var responseError = _errio2.default.fromObject(self._response.value.error); | ||
var responseErrorCause = responseError.cause; | ||
var _error7 = new _errors2.default.BusinessError(_constants2.default.BUSINESS_ERROR, { | ||
pattern: self._cleanPattern | ||
}).causedBy(responseErrorCause ? responseError.cause : responseError); | ||
// if payload is invalid | ||
if (self._response.error) { | ||
self.log.error(_error7); | ||
let error = new Errors.ParseError(Constants.PAYLOAD_PARSING_ERROR, { | ||
return cb.call(self, responseError); | ||
} | ||
cb.apply(self, [null, self._response.value.result]); | ||
} | ||
}); | ||
} catch (err) { | ||
var _error8 = new _errors2.default.FatalError(_constants2.default.FATAL_ERROR, { | ||
pattern: self._cleanPattern | ||
}).causedBy(self._response.error) | ||
}).causedBy(err); | ||
self.log.error(error) | ||
self.log.fatal(_error8); | ||
if (hasCallback) { | ||
return cb.call(self, error) | ||
// let it crash | ||
if (self._config.crashOnFatal) { | ||
self.fatal(); | ||
} | ||
} | ||
}); | ||
self._extensions.onClientPostRequest.invoke(ctx, function (err) { | ||
// handle timeout | ||
self.handleTimeout(sid, pattern, cb); | ||
} | ||
}); | ||
} | ||
if (err) { | ||
/** | ||
* @param {any} sid | ||
* @param {any} pattern | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
let error = new Errors.HemeraError(Constants.EXTENSION_ERROR).causedBy(err) | ||
}, { | ||
key: 'handleTimeout', | ||
value: function handleTimeout(sid, pattern, cb) { | ||
var _this4 = this; | ||
self.log.error(error) | ||
// handle timeout | ||
this.timeout(sid, pattern.timeout$ || this._config.timeout, 1, function () { | ||
if (hasCallback) { | ||
return cb.call(self, error) | ||
} | ||
var hasCallback = _lodash2.default.isFunction(cb); | ||
return | ||
} | ||
var error = new _errors2.default.TimeoutError(_constants2.default.ACT_TIMEOUT_ERROR, { | ||
pattern | ||
}); | ||
if (hasCallback) { | ||
_this4.log.error(error); | ||
if (self._response.value.error) { | ||
if (hasCallback) { | ||
let responseError = Errio.fromObject(self._response.value.error) | ||
let responseErrorCause = responseError.cause | ||
let error = new Errors.BusinessError(Constants.BUSINESS_ERROR, { | ||
pattern: self._cleanPattern | ||
}).causedBy(responseErrorCause ? responseError.cause : responseError) | ||
try { | ||
self.log.error(error) | ||
return cb.call(self, responseError) | ||
} | ||
cb.apply(self, [null, self._response.value.result]) | ||
} | ||
}) | ||
cb.call(_this4, error); | ||
} catch (err) { | ||
let error = new Errors.FatalError(Constants.FATAL_ERROR, { | ||
pattern: self._cleanPattern | ||
}).causedBy(err) | ||
var _error9 = new _errors2.default.FatalError(_constants2.default.FATAL_ERROR, { | ||
pattern | ||
}).causedBy(err); | ||
self.log.fatal(error) | ||
_this4.log.fatal(_error9); | ||
// let it crash | ||
if (self._config.crashOnFatal) { | ||
if (_this4._config.crashOnFatal) { | ||
self.fatal() | ||
_this4.fatal(); | ||
} | ||
} | ||
}) | ||
} | ||
}); | ||
} | ||
// handle timeout | ||
self.handleTimeout(sid, pattern, cb) | ||
} | ||
/** | ||
* @returns | ||
* OLOO (objects-linked-to-other-objects) is a code style which creates and relates objects directly without the abstraction of classes. OLOO quite naturally * implements [[Prototype]]-based behavior delegation. | ||
* More details: {@link https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch6.md} | ||
* @memberOf Hemera | ||
*/ | ||
}) | ||
}, { | ||
key: 'createContext', | ||
value: function createContext() { | ||
} | ||
var self = this; | ||
/** | ||
* @param {any} sid | ||
* @param {any} pattern | ||
* @param {any} cb | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
handleTimeout(sid, pattern, cb) { | ||
// create new instance of hemera but with pointer on the previous propertys | ||
// so we are able to create a scope per act without lossing the reference to the core api. | ||
var ctx = Object.create(self); | ||
// handle timeout | ||
this.timeout(sid, pattern.timeout$ || this._config.timeout, 1, () => { | ||
return ctx; | ||
} | ||
let hasCallback = _.isFunction(cb) | ||
/** | ||
* @memberOf Hemera | ||
*/ | ||
let error = new Errors.TimeoutError(Constants.ACT_TIMEOUT_ERROR, { | ||
pattern | ||
}) | ||
}, { | ||
key: 'list', | ||
value: function list(params) { | ||
this.log.error(error) | ||
return this._catalog.list(params); | ||
} | ||
if (hasCallback) { | ||
/** | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
try { | ||
}, { | ||
key: 'close', | ||
value: function close() { | ||
cb.call(this, error) | ||
} catch (err) { | ||
this._heavy.stop(); | ||
let error = new Errors.FatalError(Constants.FATAL_ERROR, { | ||
pattern | ||
}).causedBy(err) | ||
return this.transport.close(); | ||
} | ||
}, { | ||
key: 'plugins', | ||
get: function get() { | ||
this.log.fatal(error) | ||
return this._plugins; | ||
} | ||
// let it crash | ||
if (this._config.crashOnFatal) { | ||
/** | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
this.fatal() | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
}, { | ||
key: 'catalog', | ||
get: function get() { | ||
/** | ||
* @returns | ||
* OLOO (objects-linked-to-other-objects) is a code style which creates and relates objects directly without the abstraction of classes. OLOO quite naturally * implements [[Prototype]]-based behavior delegation. | ||
* More details: {@link https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch6.md} | ||
* @memberOf Hemera | ||
*/ | ||
createContext() { | ||
return this._catalog; | ||
} | ||
var self = this | ||
/** | ||
* | ||
* | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
// create new instance of hemera but with pointer on the previous propertys | ||
// so we are able to create a scope per act without lossing the reference to the core api. | ||
var ctx = Object.create(self) | ||
}, { | ||
key: 'load', | ||
get: function get() { | ||
return ctx | ||
} | ||
return this._heavy.load; | ||
} | ||
/** | ||
* @memberOf Hemera | ||
*/ | ||
list(params) { | ||
/** | ||
* | ||
* | ||
* @readonly | ||
* @type {Exposition} | ||
* @memberOf Hemera | ||
*/ | ||
return this._catalog.list(params) | ||
} | ||
}, { | ||
key: 'exposition', | ||
get: function get() { | ||
/** | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
close() { | ||
return this._exposition; | ||
} | ||
}, { | ||
key: 'transport', | ||
get: function get() { | ||
this._heavy.stop() | ||
return this._transport; | ||
} | ||
return this.transport.close() | ||
} | ||
} | ||
/** | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
module.exports = Hemera | ||
}, { | ||
key: 'topics', | ||
get: function get() { | ||
return this._topics; | ||
} | ||
}]); | ||
return Hemera; | ||
}(_events2.default); | ||
module.exports = Hemera; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,5 @@ | ||
// | ||
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
/*! | ||
@@ -9,14 +11,16 @@ * hemera | ||
'use strict' | ||
var _hoek = require('hoek'); | ||
/** | ||
* Module Dependencies | ||
*/ | ||
var _hoek2 = _interopRequireDefault(_hoek); | ||
const | ||
Hoek = require('hoek'), | ||
Pino = require('pino'), | ||
Pretty = Pino.pretty() | ||
var _pino = require('pino'); | ||
var _pino2 = _interopRequireDefault(_pino); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Pretty = _pino2.default.pretty(); | ||
//Config | ||
@@ -26,3 +30,3 @@ var defaultConfig = { | ||
levels: ['info', 'warn', 'debug', 'trace', 'error', 'fatal'] | ||
} | ||
}; | ||
@@ -32,6 +36,4 @@ /** | ||
*/ | ||
class Logger { | ||
var Logger = function () { | ||
/** | ||
@@ -44,31 +46,33 @@ * Creates an instance of Logger. | ||
*/ | ||
constructor(params) { | ||
function Logger(params) { | ||
var _this = this; | ||
let self = this | ||
_classCallCheck(this, Logger); | ||
self._config = Hoek.applyToDefaults(defaultConfig, params || {}) | ||
var self = this; | ||
self._config = _hoek2.default.applyToDefaults(defaultConfig, params || {}); | ||
//Leads to too much listeners in tests | ||
if (this._config.level !== 'silent') { | ||
Pretty.pipe(process.stdout) | ||
Pretty.pipe(process.stdout); | ||
} | ||
this._logger = Pino({ | ||
this._logger = (0, _pino2.default)({ | ||
name: 'app', | ||
safe: true, | ||
level: this._config.level | ||
}, Pretty) | ||
}, Pretty); | ||
//Set levels, create new prototype methods | ||
self._config.levels.forEach((level) => { | ||
self._config.levels.forEach(function (level) { | ||
let that = this | ||
var that = _this; | ||
that[level] = function () { | ||
let args = [level].concat(Array.prototype.slice.call(arguments)) | ||
self.log.apply(self, args) | ||
} | ||
}) | ||
var args = [level].concat(Array.prototype.slice.call(arguments)); | ||
self.log.apply(self, args); | ||
}; | ||
}); | ||
} | ||
@@ -79,9 +83,16 @@ | ||
*/ | ||
log() { | ||
this._logger[arguments[0]].apply(this._logger, | ||
Array.prototype.slice.call(arguments).slice(1)) | ||
} | ||
} | ||
module.exports = Logger | ||
_createClass(Logger, [{ | ||
key: 'log', | ||
value: function log() { | ||
this._logger[arguments[0]].apply(this._logger, Array.prototype.slice.call(arguments).slice(1)); | ||
} | ||
}]); | ||
return Logger; | ||
}(); | ||
module.exports = Logger; | ||
//# sourceMappingURL=logger.js.map |
@@ -1,3 +0,5 @@ | ||
// | ||
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
/*! | ||
@@ -9,79 +11,105 @@ * hemera | ||
'use strict' | ||
var _lodash = require('lodash'); | ||
const | ||
_ = require('lodash'), | ||
Crypto = require('crypto') | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _crypto = require('crypto'); | ||
var _crypto2 = _interopRequireDefault(_crypto); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/** | ||
* @class Util | ||
*/ | ||
class Util { | ||
var Util = function () { | ||
function Util() { | ||
_classCallCheck(this, Util); | ||
} | ||
/** | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
static randomId() { | ||
_createClass(Util, null, [{ | ||
key: 'randomId', | ||
return Crypto.randomBytes(16).toString('hex') | ||
} | ||
/** | ||
* Get high resolution time in nanoseconds | ||
* | ||
* @static | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
static nowHrTime() { | ||
/** | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
value: function randomId() { | ||
const hrtime = process.hrtime() | ||
return Math.floor(hrtime[0] * 1000000 + hrtime[1] / 1000) | ||
} | ||
return _crypto2.default.randomBytes(16).toString('hex'); | ||
} | ||
/** | ||
* @static | ||
* @param {any} obj | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
static cleanPattern(obj) { | ||
/** | ||
* Get high resolution time in nanoseconds | ||
* | ||
* @static | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
if (obj === null) return obj | ||
}, { | ||
key: 'nowHrTime', | ||
value: function nowHrTime() { | ||
return _.pickBy(obj, function (val, prop) { | ||
return !_.includes(prop, '$') | ||
}) | ||
} | ||
var hrtime = process.hrtime(); | ||
return Math.floor(hrtime[0] * 1000000 + hrtime[1] / 1000); | ||
} | ||
/** | ||
* @param {any} args | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
static pattern(args) { | ||
/** | ||
* @static | ||
* @param {any} obj | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
if (_.isString(args)) { | ||
return args | ||
}, { | ||
key: 'cleanPattern', | ||
value: function cleanPattern(obj) { | ||
if (obj === null) return obj; | ||
return _lodash2.default.pickBy(obj, function (val, prop) { | ||
return !_lodash2.default.includes(prop, '$'); | ||
}); | ||
} | ||
args = args || {} | ||
var sb = [] | ||
_.each(args, function (v, k) { | ||
if (!~k.indexOf('$') && !_.isFunction(v)) { | ||
sb.push(k + ':' + v) | ||
/** | ||
* @param {any} args | ||
* @returns | ||
* | ||
* @memberOf Util | ||
*/ | ||
}, { | ||
key: 'pattern', | ||
value: function pattern(args) { | ||
if (_lodash2.default.isString(args)) { | ||
return args; | ||
} | ||
}) | ||
sb.sort() | ||
args = args || {}; | ||
var sb = []; | ||
_lodash2.default.each(args, function (v, k) { | ||
if (!~k.indexOf('$') && !_lodash2.default.isFunction(v)) { | ||
sb.push(k + ':' + v); | ||
} | ||
}); | ||
return sb.join(',') | ||
} | ||
} | ||
sb.sort(); | ||
module.exports = Util | ||
return sb.join(','); | ||
} | ||
}]); | ||
return Util; | ||
}(); | ||
module.exports = Util; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "nats-hemera", | ||
"author": "Dustin Deus (https://github.com/StarpTech)", | ||
"version": "0.3.9", | ||
"main": "index.js", | ||
"version": "0.3.10", | ||
"main": "build/index.js", | ||
"homepage": "https://hemerajs.github.io/hemera/", | ||
@@ -37,6 +37,5 @@ "repository": { | ||
"scripts": { | ||
"flow": "node ./node_modules/flow-bin/cli check", | ||
"build": "node ./node_modules/flow-remove-types/flow-remove-types --pretty ./lib --out-dir ./build", | ||
"prepublish": "npm run flow && npm run flow && npm run build", | ||
"lint": "eslint --ext .js lib" | ||
"lint": "eslint ./src ./test && flow", | ||
"build": "babel ./lib --out-dir ./build --copy-files --source-maps", | ||
"prepublish": "npm run lint && npm run build" | ||
}, | ||
@@ -58,7 +57,26 @@ "engines": { | ||
"devDependencies": { | ||
"babel-cli": "^6.22.2", | ||
"babel-plugin-istanbul": "^3.1.2", | ||
"babel-plugin-syntax-flow": "^6.18.0", | ||
"babel-plugin-transform-flow-comments": "^6.22.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-preset-env": "^1.1.8", | ||
"babel-register": "^6.22.0", | ||
"eslint": "^3.13.1", | ||
"eslint-plugin-flowtype": "^2.30.0", | ||
"flow-bin": "0.38.x", | ||
"flow-remove-types": "1.1.x" | ||
"flow-bin": "^0.38.0" | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"lib/**/*.js" | ||
], | ||
"instrument": false, | ||
"reporter": [ | ||
"text-lcov" | ||
], | ||
"require": [ | ||
"babel-register" | ||
], | ||
"sourceMap": false | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
102364
27
1216
10
1