nats-hemera
Advanced tools
Comparing version 0.1.30 to 0.1.31
214
lib/index.js
@@ -26,2 +26,3 @@ /*! | ||
SuperError = require('super-error'), | ||
NUID = require('nuid'), | ||
Pretty = Pino.pretty() | ||
@@ -47,5 +48,5 @@ | ||
TRANSPORT_CONNECTED = 'Connected!', | ||
PLUGIN_ADDED = 'PLUGIN ADDED!', | ||
PLUGIN_ADDED = 'PLUGIN - ADDED!', | ||
PAYLOAD_VALIDATION_ERROR = 'Invalid payload', | ||
ADD_ADDED = 'ADD ADDED' | ||
ADD_ADDED = 'ADD - ADDED' | ||
@@ -171,2 +172,79 @@ //Errors | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
timeout() { | ||
return this.transport.timeout.apply(this.transport, arguments) | ||
} | ||
/** | ||
* Add response | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
send() { | ||
return this.transport.publish.apply(this.transport, arguments) | ||
} | ||
/** | ||
* Act | ||
* | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
sendRequest() { | ||
return this.transport.request.apply(this.transport, arguments) | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} error | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
buildError(error, additionalData) { | ||
this.log().error(error) | ||
//After action is proceed | ||
additionalData.response$.endTime = Date.now() | ||
let msg = this.stringifyJSON(Hoek.merge({ | ||
result: null, | ||
error: Errio.stringify(error) | ||
}, additionalData)) | ||
return msg | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} data | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
buildSuccess(data, additionalData) { | ||
//After action is proceed | ||
additionalData.response$.endTime = Date.now() | ||
let msg = this.stringifyJSON(Hoek.merge({ | ||
result: data, | ||
error: null | ||
}, additionalData)) | ||
return msg | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} topic | ||
@@ -192,2 +270,9 @@ * @returns | ||
let additionalResponseData = { | ||
//Log time before request is proceed | ||
response$: { | ||
startTime: Date.now() | ||
} | ||
} | ||
//Parse response as JSON | ||
@@ -200,11 +285,4 @@ let result = this.parseJSON(request) | ||
let error = new ParseError(PAYLOAD_PARSING_ERROR).causedBy(result.error) | ||
this.log().error(error) | ||
//Encode msg as JSON | ||
let msg = this.stringifyJSON({ | ||
result: null, | ||
error: Errio.stringify(error) | ||
}) | ||
return this.transport.publish(replyTo, msg) | ||
return this.send(replyTo, this.buildError(error, additionalResponseData)) | ||
} | ||
@@ -219,9 +297,13 @@ | ||
//Pass metadata | ||
result = Hoek.merge(result.value.pattern, { | ||
meta$: result.value.meta$, | ||
request$: result.value.request$ | ||
}) | ||
additionalResponseData.meta$ = result.meta$ | ||
additionalResponseData.request$ = result.request$ | ||
try { | ||
//Pass metadata | ||
result = Hoek.merge(result.value.pattern, { | ||
meta$: result.value.meta$ | ||
}) | ||
let paramcheck = Parambulator(actMeta.patternRules) | ||
@@ -235,11 +317,5 @@ | ||
let payloadError = new PayloadValidationError(PAYLOAD_VALIDATION_ERROR).causedBy(err) | ||
this.log().error(payloadError) | ||
let msg = this.stringifyJSON({ | ||
result: null, | ||
error: Errio.stringify(payloadError) | ||
}) | ||
//Send message | ||
return this.transport.publish(replyTo, msg) | ||
return this.send(replyTo, this.buildError(payloadError, additionalResponseData)) | ||
} | ||
@@ -250,25 +326,11 @@ | ||
let msg = null | ||
if (err) { | ||
let businessError = new BusinessError(IMPLEMENTATION_ERROR).causedBy(err) | ||
this.log().error(businessError) | ||
msg = this.stringifyJSON({ | ||
result: null, | ||
error: Errio.stringify(businessError) | ||
}) | ||
return this.transport.publish(replyTo, msg) | ||
return this.send(replyTo, this.buildError(businessError, additionalResponseData)) | ||
} | ||
//Encode msg as JSON | ||
msg = this.stringifyJSON({ | ||
result: resp, | ||
error: null | ||
}) | ||
//Send message | ||
this.transport.publish(replyTo, msg) | ||
this.send(replyTo, this.buildSuccess(resp, additionalResponseData)) | ||
}) | ||
@@ -281,12 +343,5 @@ | ||
let error = new ImplementationError(IMPLEMENTATION_ERROR).causedBy(err) | ||
this.log().fatal(error) | ||
//Encode msg as JSON | ||
let msg = this.stringifyJSON({ | ||
result: null, | ||
error: Errio.stringify(error) | ||
}) | ||
//Send error back to callee | ||
this.transport.publish(replyTo, msg, () => { | ||
this.send(replyTo, this.buildError(error, additionalResponseData), () => { | ||
@@ -308,10 +363,6 @@ //let it crash | ||
//Encode msg as JSON | ||
let msg = this.stringifyJSON({ | ||
result: null, | ||
error: Errio.stringify(new PatternNotFound(PATTERN_NOT_FOUND)) | ||
}) | ||
let error = new PatternNotFound(PATTERN_NOT_FOUND) | ||
//Send error back to callee | ||
this.transport.publish(replyTo, msg) | ||
this.send(replyTo, this.buildError(error, additionalResponseData)) | ||
} | ||
@@ -327,2 +378,14 @@ | ||
* | ||
* @param {any} pattern | ||
* @returns | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
createRequestId(pattern) { | ||
return NUID.next() | ||
} | ||
/** | ||
* | ||
* | ||
* @param {any} msg | ||
@@ -453,20 +516,25 @@ * @returns | ||
delegate.meta$ = pattern.meta$ || delegate.meta$ | ||
//Create unique reqeuest id | ||
delegate.requestId$ = this.createRequestId() | ||
//clean special $ variables | ||
let cleanPattern = this.cleanPattern(pattern) | ||
//Parse msg as JSON | ||
let msg = this.stringifyJSON({ | ||
pattern: this.cleanPattern(pattern), //clean special $ variables | ||
meta$: delegate.meta$ | ||
}) | ||
let msg = { | ||
pattern: cleanPattern, | ||
meta$: delegate.meta$, | ||
request$: { | ||
id: delegate.requestId$, | ||
startTime: Date.now() | ||
} | ||
} | ||
this.log().info(pattern, 'ACT') | ||
let t1 = new Date() | ||
this.log().info(pattern, 'ACT - OUTBOUND') | ||
//Emit event | ||
this.emit('outbound', msg) | ||
//Request to topic | ||
let sid = this.transport.request(pattern.topic, msg, (response) => { | ||
let sid = this.sendRequest(pattern.topic, this.stringifyJSON(msg), (response) => { | ||
//Measure time | ||
this.log().info(Hoek.merge(pattern, { | ||
time$: new Date() - t1 | ||
}), 'ACT_RESP') | ||
//Parse response as JSON | ||
@@ -488,2 +556,18 @@ let msg = this.parseJSON(response) | ||
//Check if request$ was successfully transfered | ||
if (!msg.value.request$) { | ||
msg.value.request$ = { | ||
endTime: Date.now() | ||
} | ||
} else { | ||
msg.value.request$.endTime = Date.now() | ||
} | ||
//Emit event | ||
this.emit('inbound', msg.value) | ||
this.log().info(pattern, 'ACT - INBOUND') | ||
if (typeof cb === 'function') { | ||
@@ -518,3 +602,3 @@ | ||
//Handle timeout | ||
this.transport.timeout(sid, pattern.timeout$ || this.config.timeout, 1, () => { | ||
this.timeout(sid, pattern.timeout$ || this.config.timeout, 1, () => { | ||
@@ -521,0 +605,0 @@ let error = new TimeoutError(ACT_TIMEOUT_ERROR) |
{ | ||
"name": "nats-hemera", | ||
"author": "Dustin Deus (https://github.com/StarpTech)", | ||
"version": "0.1.30", | ||
"version": "0.1.31", | ||
"main": "index.js", | ||
@@ -54,2 +54,3 @@ "homepage": "https://starptech.github.io/hemera/", | ||
"lodash": "4.17.x", | ||
"nuid": "0.6.x", | ||
"parambulator": "1.5.x", | ||
@@ -56,0 +57,0 @@ "pino": "3.0.x", |
@@ -202,4 +202,2 @@ # Hemera | ||
hemera.act({ topic: 'math', cmd: 'add', a: 1, b: 1 }, function (err, resp) { | ||
//or | ||
this.context$.a = 'foobar'; | ||
@@ -287,2 +285,14 @@ this.act({ topic: 'math', cmd: 'add', a: 1, b: 5 }, function (err, resp) { | ||
### Tracing | ||
```js | ||
hemera.on('outbound', (msg) => { | ||
console.log('Outbound', msg) | ||
}) | ||
hemera.on('inbound', (msg) => { | ||
console.log('Inbound', msg) | ||
}) | ||
``` | ||
### Protocol | ||
@@ -292,14 +302,17 @@ | ||
#### Response | ||
#### Request | ||
```JSON | ||
{ | ||
"result": "<msg>", | ||
"error": "<serialized_error>" | ||
"pattern": "<msg>", | ||
"meta$": "<msg>", | ||
"request$": "<msg>" | ||
} | ||
``` | ||
#### Request | ||
#### Response | ||
```JSON | ||
{ | ||
"pattern": "<msg>", | ||
"meta$": "<msg>" | ||
"result": "<msg>", | ||
"error": "<serialized_error>", | ||
"meta$": "<msg>", | ||
"response$": "<msg>" | ||
} | ||
@@ -306,0 +319,0 @@ ``` |
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
32825
518
409
11
+ Addednuid@0.6.x
+ Addednuid@0.6.14(transitive)