Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nats-hemera

Package Overview
Dependencies
Maintainers
1
Versions
279
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nats-hemera - npm Package Compare versions

Comparing version 0.1.63 to 0.1.64

README.md

9

.eslintrc.js
module.exports = {
"parser": "babel-eslint",
"env": {

@@ -6,3 +7,9 @@ "es6": true,

},
"extends": "eslint:recommended",
"plugins": [
"flowtype"
],
"extends": [
"plugin:flowtype/recommended",
"eslint:recommended",
],
"parserOptions": {

@@ -9,0 +16,0 @@ "sourceType": "module"

5

build/constants.js

@@ -1,2 +0,2 @@

//
//

@@ -30,3 +30,4 @@ /*!

FATAL_ERROR: 'Fatal error',
EXTENSION_ERROR: 'Extension error'
EXTENSION_ERROR: 'Extension error',
EXPOSITION_OVERWRITE: 'Your overwrite a exposition'
}

@@ -1,2 +0,2 @@

//
//

@@ -3,0 +3,0 @@ /*!

@@ -1,2 +0,2 @@

//
//

@@ -18,6 +18,6 @@ /*!

constructor(type ) {
constructor(type) {

@@ -34,3 +34,3 @@ this._handler = []

*/
subscribe(handler ) {
subscribe(handler) {

@@ -47,3 +47,3 @@ this._handler.push(handler)

*/
invoke(ctx , cb ) {
invoke(ctx, cb) {

@@ -50,0 +50,0 @@ const each = (ext, next) => {

@@ -1,2 +0,2 @@

//
//

@@ -29,4 +29,4 @@ /*!

//Config
var defaultConfig = {
// config
var defaultConfig = {
timeout: 2000,

@@ -43,29 +43,30 @@ debug: false,

constructor(transport , params ) {
constructor(transport, params) {

@@ -79,4 +80,5 @@ super()

this._plugins = {}
this._exposition = {}
//Special variables for act and add
// special variables for new execution context
this.context$ = {}

@@ -94,3 +96,3 @@ this.meta$ = {}

//Define extension points
// define extension points
this._extensions = {

@@ -107,19 +109,19 @@ onClientPreRequest: new Ext('onClientPreRequest'),

*/
this._extensions.onClientPreRequest.subscribe(function (next ) {
this._extensions.onClientPreRequest.subscribe(function (next) {
let pattern = this._pattern
let pattern = this._pattern
let prevCtx = this._prevContext
let cleanPattern = this._cleanPattern
let ctx = this
let ctx = this
//Shared context
// shared context
ctx.context$ = pattern.context$ || prevCtx.context$
//Set metadata by passed pattern or current message context
// set metadata by passed pattern or current message context
ctx.meta$ = Hoek.merge(pattern.meta$ || {}, ctx.meta$)
//Is only passed by msg
// is only passed by msg
ctx.delegate$ = pattern.delegate$ || {}
//Tracing
// tracing
ctx.trace$ = pattern.trace$ || {}

@@ -133,4 +135,4 @@ ctx.trace$.parentSpanId = prevCtx.trace$.spanId

//Request
let request = {
// request
let request = {
id: pattern.requestId$ || Util.randomId(),

@@ -142,4 +144,4 @@ parentId: ctx.request$.id,

//Build msg
let message = {
// build msg
let message = {
pattern: cleanPattern,

@@ -161,9 +163,9 @@ meta$: ctx.meta$,

this._extensions.onClientPostRequest.subscribe(function (next ) {
this._extensions.onClientPostRequest.subscribe(function (next) {
let ctx = this
let pattern = this._pattern
let ctx = this
let pattern = this._pattern
let msg = ctx._response.value
//Pass to act context
// pass to act context
ctx.request$ = msg.request$ || {}

@@ -185,6 +187,6 @@ ctx.request$.service = pattern.topic

*/
this._extensions.onServerPreRequest.subscribe(function (next ) {
this._extensions.onServerPreRequest.subscribe(function (next) {
let msg = this._request.value
let ctx = this
let ctx = this

@@ -204,4 +206,8 @@ if (msg) {

this._extensions.onServerPreRequest.subscribe(function (next ) {
this._extensions.onServerPreRequest.subscribe(function (next) {
let ctx = this
ctx.emit('onServerPreRequest', ctx)
next()

@@ -211,23 +217,7 @@

this._extensions.onServerPreResponse.subscribe(function (next ) {
this._extensions.onServerPreResponse.subscribe(function (next) {
let ctx = this
let result = this._response
let ctx = this
let message = {
meta$: ctx.meta$ || {},
trace$: ctx.trace$ || {},
request$: ctx.request$,
result: result instanceof Error ? null : result,
error: result instanceof Error ? Errio.stringify(result) : null
}
let endTime = Util.nowHrTime()
message.request$.duration = endTime - message.request$.timestamp
message.trace$.duration = endTime - message.request$.timestamp
ctx._message = message
ctx.emit('onServerPreResponse', ctx)
next()

@@ -247,3 +237,3 @@

*/
get plugins() {
get plugins() {

@@ -258,3 +248,3 @@ return this._plugins

*/
get catalog() {
get catalog() {

@@ -265,8 +255,41 @@ return this._catalog

/**
*
*
* @readonly
* @type {Exposition}
* @memberOf Hemera
*/
get exposition() {
return this._exposition
}
/**
*
*
* @param {string} key
* @param {mixed} object
*
* @memberOf Hemera
*/
get transport() {
expose(key, object) {
if (!this.exposition[key]) {
this.exposition[key] = object
} else {
this.log.warn(Constants.EXPOSITION_OVERWRITE)
this.exposition[key] = object
}
}
/**
* @readonly
*
* @memberOf Hemera
*/
get transport() {
return this._transport

@@ -280,16 +303,15 @@ }

*/
get topics() {
get topics() {
return this._topics
}
/**
*
*
* @param {any} type
* @param {any} handler
*
* @memberOf Hemera
*/
ext(type, handler) {
return this._topics
}
/**
*
*
* @param {any} type
* @param {any} handler
*
* @memberOf Hemera
*/
ext(type , handler ) {
this._extensions[type].subscribe(handler)

@@ -303,3 +325,3 @@

*/
use(params ) {
use(params) {

@@ -314,9 +336,9 @@ if (this._plugins[params.attributes.name]) {

//Create new execution context
// create new execution context
let ctx = this.createContext()
ctx.plugin$ = params.attributes
ctx.plugin$.attributes = params.attributes
params.plugin.call(ctx, params.options)
this.log.info(params.attributes.name, Constants.PLUGIN_ADDED)
this._plugins[params.attributes.name] = ctx.plugin$
this._plugins[params.attributes.name] = ctx.plugin$.attributes

@@ -338,3 +360,3 @@ }

*/
ready(cb ) {
ready(cb) {

@@ -388,19 +410,49 @@ this._transport.on('connect', () => {

*/
reply() {
_buildMessage() {
let self = this;
let result = this._response
if (self._response instanceof Error) {
self.log.error(self._response)
let message = {
meta$: this.meta$ || {},
trace$: this.trace$ || {},
request$: this.request$,
result: result instanceof Error ? null : result,
error: result instanceof Error ? Errio.stringify(result) : null
}
let endTime = Util.nowHrTime()
message.request$.duration = endTime - message.request$.timestamp
message.trace$.duration = endTime - message.request$.timestamp
this._message = message
}
/**
*
*
*
* @memberOf Hemera
*/
reply() {
let self = this;
self._extensions.onServerPreResponse.invoke(self, function (err) {
if (err) {
// check if an error was already catched
if (self._response instanceof Error) {
self.log.error(self._response)
self._buildMessage()
}
// check for an extension error
else if (err) {
let error = new Errors.HemeraError(Constants.EXTENSION_ERROR).causedBy(err)
self._response = error
self.log.error(self._response)
self._buildMessage()
} else {
self.log.error(error)
throw (error)
self._buildMessage()
}

@@ -410,8 +462,9 @@

// indicate that an error occurs and that the program should exit
if (self._shouldCrash) {
//Send error back to callee
// send error back to callee
return self.send(self._replyTo, msg, () => {
//let it crash
// let it crash
if (self._config.crashOnFatal) {

@@ -437,8 +490,8 @@

*/
subscribe(topic ) {
subscribe(topic) {
let self = this
let self = this
//Avoid duplicate subscribers of the emit stream
//We use one subscriber per topic
// avoid duplicate subscribers of the emit stream
// we use one subscriber per topic
if (self._topics[topic]) {

@@ -448,8 +501,8 @@ return

//Queue group names allow load balancing of services
// queue group names allow load balancing of services
self.transport.subscribe(topic, {
'queue': 'queue.' + topic
}, (request , replyTo ) => {
}, (request, replyTo) => {
//Create new execution context
// create new execution context
let ctx = this.createContext()

@@ -465,3 +518,3 @@ ctx._shouldCrash = false

let self = this
let self = this

@@ -473,6 +526,9 @@ if (err) {

self.log.error(error)
throw (error)
self._response = error
// send message
return self.reply()
}
//Invalid payload
// invalid payload
if (self._request.error) {

@@ -490,7 +546,7 @@

//Check if a handler is registered with this pattern
// check if a handler is registered with this pattern
if (self._actMeta) {
//Extension point 'onServerPreHandler'
self._extensions.onServerPreHandler.invoke(ctx, function (err ) {
// extension point 'onServerPreHandler'
self._extensions.onServerPreHandler.invoke(ctx, function (err) {

@@ -503,3 +559,3 @@ if (err) {

//Send message
// send message
return self.reply()

@@ -512,4 +568,4 @@ }

//Call action
action(self._request.value.pattern, (err , resp) => {
// call action
action(self._request.value.pattern, (err, resp) => {

@@ -527,3 +583,3 @@ if (err) {

//Send message
// send message
self.reply()

@@ -555,3 +611,3 @@ })

//Send error back to callee
// send error back to callee
self.reply()

@@ -574,5 +630,5 @@ }

*/
add(pattern , cb ) {
add(pattern, cb) {
//Topic is needed to subscribe on a subject in NATS
// topic is needed to subscribe on a subject in NATS
if (!pattern.topic) {

@@ -602,4 +658,4 @@

//Remove objects (rules) from pattern
_.each(pattern, function (v , k ) {
// remove objects (rules) from pattern
_.each(pattern, function (v, k) {

@@ -612,4 +668,4 @@ if (_.isObject(v)) {

//Create message object which represent the object behind the matched pattern
let actMeta = {
// create message object which represent the object behind the matched pattern
let actMeta = {
schema: schema,

@@ -622,3 +678,3 @@ pattern: origPattern,

//Check if pattern is already registered
// check if pattern is already registered
if (handler) {

@@ -634,3 +690,3 @@

//Add to bloomrun
// add to bloomrun
this._catalog.add(origPattern, actMeta)

@@ -640,3 +696,3 @@

//Subscribe on topic
// subscribe on topic
this.subscribe(pattern.topic)

@@ -651,5 +707,5 @@ }

*/
act(pattern , cb ) {
act(pattern, cb) {
//Topic is needed to subscribe on a subject in NATS
// topic is needed to subscribe on a subject in NATS
if (!pattern.topic) {

@@ -665,3 +721,3 @@

//Create new execution context
// create new execution context
let ctx = this.createContext()

@@ -674,5 +730,5 @@ ctx._pattern = pattern

ctx._extensions.onClientPreRequest.invoke(ctx, function onPreRequest(err ) {
ctx._extensions.onClientPreRequest.invoke(ctx, function onPreRequest(err) {
let self = this
let self = this

@@ -684,10 +740,15 @@ if (err) {

self.log.error(error)
throw (error)
if (typeof cb === 'function') {
return cb.call(self, error)
}
return
}
//Encode msg to JSON
// encode msg to JSON
self._request = Util.stringifyJSON(self._message)
//Send request
let sid = self.sendRequest(pattern.topic, self._request, (response ) => {
// send request
let sid = self.sendRequest(pattern.topic, self._request, (response) => {

@@ -698,3 +759,3 @@ self._response = Util.parseJSON(response)

//If payload is invalid
// if payload is invalid
if (self._response.error) {

@@ -709,3 +770,2 @@

if (typeof cb === 'function') {
return cb.call(self, error)

@@ -715,4 +775,4 @@ }

//Extension point 'onClientPostRequest'
self._extensions.onClientPostRequest.invoke(ctx, function (err ) {
// extension point 'onClientPostRequest'
self._extensions.onClientPostRequest.invoke(ctx, function (err) {

@@ -724,3 +784,8 @@ if (err) {

self.log.error(error)
throw (error)
if (typeof cb === 'function') {
return cb.call(self, error)
}
return
}

@@ -738,3 +803,3 @@

//Error is already wrapped
// error is already wrapped
return cb.call(self, Errio.parse(self._response.value.error))

@@ -756,3 +821,3 @@ }

//Let it crash
// let it crash
if (self._config.crashOnFatal) {

@@ -765,3 +830,3 @@

//Handle timeout
// handle timeout
self.handleTimeout(sid, pattern, cb)

@@ -780,5 +845,5 @@

*/
handleTimeout(sid , pattern , cb ) {
handleTimeout(sid, pattern, cb) {
//Handle timeout
// handle timeout
this.timeout(sid, pattern.timeout$ || this._config.timeout, 1, () => {

@@ -805,3 +870,3 @@

//Let it crash
// let it crash
if (this._config.crashOnFatal) {

@@ -818,3 +883,4 @@

* @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

@@ -826,5 +892,5 @@ */

//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)
// 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)

@@ -837,3 +903,3 @@ return ctx

*/
list(params ) {
list(params) {

@@ -840,0 +906,0 @@ return this._catalog.list(params)

@@ -1,2 +0,2 @@

//
//

@@ -32,4 +32,4 @@ /*!

/**

@@ -42,3 +42,3 @@ * Creates an instance of Logger.

*/
constructor(params ) {
constructor(params) {

@@ -63,3 +63,3 @@ let self = this

let that = this
let that = this

@@ -66,0 +66,0 @@ that[level] = function () {

@@ -1,2 +0,2 @@

//
//

@@ -57,3 +57,3 @@ /*!

return _.pickBy(obj, function (val, prop ) {
return _.pickBy(obj, function (val, prop) {
return !_.includes(prop, '$')

@@ -60,0 +60,0 @@ })

{
"name": "nats-hemera",
"author": "Dustin Deus (https://github.com/StarpTech)",
"version": "0.1.63",
"version": "0.1.64",
"main": "index.js",

@@ -38,4 +38,5 @@ "homepage": "https://hemerajs.github.io/hemera/",

"flow": "flow check",
"build": "flow-remove-types lib/ --out-dir build/",
"prepublish": "npm run flow && npm run build"
"build": "flow-remove-types --pretty lib/ --out-dir build/",
"prepublish": "npm run flow && npm run build",
"lint": "eslint --ext .js lib"
},

@@ -42,0 +43,0 @@ "engines": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc