nats-hemera
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,3 +0,1 @@ | ||
// | ||
/*! | ||
@@ -7,2 +5,3 @@ * hemera | ||
* MIT Licensed | ||
* Based on https://github.com/mcollina/fast-json-parse | ||
*/ | ||
@@ -12,12 +11,28 @@ | ||
const Util = require('./util') | ||
class Decoder { | ||
decode(msg) { | ||
static decode(msg) { | ||
return Util.parseJSON(msg) | ||
return Parse(msg) | ||
} | ||
} | ||
function Parse(data) { | ||
if (!(this instanceof Parse)) { | ||
return new Parse(data) | ||
} | ||
this.error = null | ||
this.value = null | ||
try { | ||
this.value = JSON.parse(data) | ||
} catch (error) { | ||
this.error = error | ||
} | ||
} | ||
module.exports = Decoder |
@@ -7,2 +7,3 @@ // | ||
* MIT Licensed | ||
* Based on https://github.com/davidmarkclements/fast-safe-stringify | ||
*/ | ||
@@ -12,9 +13,7 @@ | ||
const Util = require('./util') | ||
class Encoder { | ||
encode(msg) { | ||
static encode(msg) { | ||
return Util.stringifyJSON(msg) | ||
return stringify(msg) | ||
} | ||
@@ -24,1 +23,57 @@ } | ||
module.exports = Encoder | ||
function stringify(obj) { | ||
decirc(obj, '', [], null) | ||
return JSON.stringify(obj) | ||
} | ||
function Circle(val, k, parent) { | ||
this.val = val | ||
this.k = k | ||
this.parent = parent | ||
this.count = 1 | ||
} | ||
Circle.prototype.toJSON = function toJSON() { | ||
if (--this.count === 0) { | ||
this.parent[this.k] = this.val | ||
} | ||
return '[Circular]' | ||
} | ||
function decirc(val, k, stack, parent) { | ||
var keys, len, i | ||
if (typeof val !== 'object' || val === null) { | ||
// not an object, nothing to do | ||
return | ||
} else if (val instanceof Circle) { | ||
val.count++ | ||
return | ||
} else if (parent) { | ||
if (~stack.indexOf(val)) { | ||
parent[k] = new Circle(val, k, parent) | ||
return | ||
} | ||
} | ||
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) | ||
} | ||
stack.pop() | ||
} |
@@ -98,4 +98,4 @@ // | ||
this._encoder = new DefaultEncoder | ||
this._decoder = new DefaultDecoder | ||
this._encoder = { encode: DefaultEncoder.encode } | ||
this._decoder = { decode: DefaultDecoder.decode } | ||
@@ -241,24 +241,2 @@ // define extension points | ||
/** | ||
* | ||
* | ||
* @type {*} | ||
* @memberOf Hemera | ||
*/ | ||
set decoder(decoder) { | ||
this._decoder = decoder | ||
} | ||
/** | ||
* | ||
* | ||
* @type {*} | ||
* @memberOf Hemera | ||
*/ | ||
set encoder(encoder) { | ||
this._encoder = encoder | ||
} | ||
/** | ||
* @readonly | ||
@@ -265,0 +243,0 @@ * |
{ | ||
"name": "nats-hemera", | ||
"author": "Dustin Deus (https://github.com/StarpTech)", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"main": "index.js", | ||
@@ -49,4 +49,2 @@ "homepage": "https://hemerajs.github.io/hemera/", | ||
"errio": "1.2.x", | ||
"fast-json-parse": "1.0.x", | ||
"fast-safe-stringify": "1.1.x", | ||
"hoek": "4.1.x", | ||
@@ -53,0 +51,0 @@ "items": "2.1.x", |
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
82764
7
20
984
+ Addedfast-safe-stringify@1.2.3(transitive)
- Removedfast-json-parse@1.0.x
- Removedfast-safe-stringify@1.1.x
- Removedfast-safe-stringify@1.1.13(transitive)