Gathering detailed insights and metrics for @bemoje/serializer
Gathering detailed insights and metrics for @bemoje/serializer
Gathering detailed insights and metrics for @bemoje/serializer
Gathering detailed insights and metrics for @bemoje/serializer
npm install @bemoje/serializer
Typescript
Module System
Node Version
NPM Version
68.5
Supply Chain
98.9
Quality
75
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last Day
100%
2
Compared to previous day
Last Week
75%
7
Compared to previous week
Last Month
-6.7%
14
Compared to previous month
Last Year
-8.5%
97
Compared to previous year
2
Buffer, string + circular references in a single serialization module that basically just merges three different solutions: v8's Buffer serializer, the native JSON stringifier and the drop-in replacer for JSON.stringify: 'safe-stable-stringify', which supplies the support for circular references.
1npm install --save @bemoje/serializer
1export default { toBufferV8, toJson, toJsonPretty, deserialize } 2 3/** 4 * Serializes with v8 engine. 5 * If that fails for any reason, JSON.stringify will be used, after which that resulting stringwill be 6 * converted to a Buffer as to not change types. 7 * @param {*} data - What to serialize 8 * @returns {Buffer} 9 */ 10export function toBufferV8(data: any): Buffer 11 12/** 13 * Serializes with JSON.stringify. 14 * If that fails for any reason, 'safe-stable-stringify', as it supports circular references. If that 15 * fails, the v8 engine is tried. It's returned Buffer will be converted to a string before it is 16 * returned as to not change types, suddenly. 17 * @param {*} data - What to serialize 18 * @param {function} [replacer] - See the JSON.stringify() documentation 19 * @param {integer} [space=0] - See the JSON.stringify() documentation 20 * @returns {string} 21 */ 22export function toJson(data: any, replacer?: Function, space?: Number): String 23 24/** 25 * Serializes with safe-stable-stringify, since that supports circular references. That circular 26 * references error from 27 * JSON is not pretty :) 28 * @param {*} data - What to serialize 29 * @param {function} [replacer] - See the JSON.stringify() documentation 30 * @param {integer} [space=3] - See the JSON.stringify() documentation 31 * @returns {string} 32 */ 33export function toJsonPretty(data: any, replacer?: Function, space?: Number = 3): String 34 35/** 36 * Deserializes with v8 engine if the input is a Buffer and with JSON.parse if it's a string. 37 * @param {Buffer|string} serialized - What to deserialize 38 * @returns {*} 39 */ 40export function deserialize(serialized: Buffer|String, reviver?: Function): any
1const data = { a: 3, b: { a: 32 } } 2 3const _toBufferV8 = toBufferV8(data) 4const _toJson = toJson(data) 5const _toJsonPretty = toJsonPretty(data) 6 7const __toBufferV8 = deserialize(_toBufferV8) 8const __toJson = deserialize(_toJson) 9const __toJsonPretty = deserialize(_toJsonPretty) 10 11console.log({ 12 data, 13 _toBufferV8, 14 _toJson, 15 _toJsonPretty, 16 __toBufferV8, 17 __toJson, 18 __toJsonPretty, 19}) 20 21/*{ 22 data: { a: 3, b: { a: 32 } }, 23 _toBufferV8: <Buffer ff 0d 6f 22 01 61 49 06 22 01 62 6f 22 01 61 49 40 7b 01 7b 02>, 24 _toJson: '{"a":3,"b":{"a":32}}', 25 _toJsonPretty: '{\n "a": 3,\n "b": {\n "a": 32\n }\n}', 26 __toBufferV8: { a: 3, b: { a: 32 } }, 27 __toJson: { a: 3, b: { a: 32 } }, 28 __toJsonPretty: { a: 3, b: { a: 32 } } 29}*/ 30 31const circular = { a: 3, b: { a: 32 } } 32circular.b.b = circular.b 33 34const c_toBufferV8 = toBufferV8(circular) 35const c_toJson = toJson(circular) 36const c_toJsonPretty = toJsonPretty(circular) 37 38const c__toBufferV8 = deserialize(c_toBufferV8) 39const c__toJson = deserialize(c_toJson) 40const c__toJsonPretty = deserialize(c_toJsonPretty) 41 42console.log({ 43 circular, 44 c_toBufferV8, 45 c_toJson, 46 c_toJsonPretty, 47 c__toBufferV8, 48 c__toJson, 49 c__toJsonPretty, 50}) 51 52/*{ 53 circular: { a: 3, b: { a: 32, b: [Circular] } }, 54 c_toBufferV8: <Buffer ff 0d 6f 22 01 61 49 06 22 01 62 6f 22 01 61 49 40 22 01 62 5e 01 7b 02 7b 02>, 55 c_toJson: '{"a":3,"b":{"a":32,"b":"[Circular]"}}', 56 c_toJsonPretty: '{\n "a": 3,\n "b": {\n "a": 32,\n "b": "[Circular]"\n }\n}', 57 c__toBufferV8: { a: 3, b: { a: 32, b: [Circular] } }, 58 c__toJson: { a: 3, b: { a: 32, b: '[Circular]' } }, 59 c__toJsonPretty: { a: 3, b: { a: 32, b: '[Circular]' } } 60}*/
No vulnerabilities found.
No security vulnerabilities found.