Comparing version 2.0.0 to 2.1.0
{ | ||
"name": "compactr", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Schema based serialization made easy", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -63,6 +63,6 @@ <h1 align="center"> | ||
// Decoding (full) | ||
const content = userSchema.decode(buffer); | ||
const content = userSchema.read(buffer); | ||
// Decoding (partial) | ||
const content = userSchema.decode(header, partial); | ||
const content = userSchema.readContent(partial); | ||
``` | ||
@@ -74,25 +74,19 @@ | ||
``` | ||
[Array] JSON x 84.04 ops/sec ±0.77% (64 runs sampled) | ||
[Array] Compactr x 88.12 ops/sec ±0.99% (66 runs sampled) | ||
[Array] JSON x 188 ops/sec ±2.47% (73 runs sampled) | ||
[Array] Compactr x 248 ops/sec ±3.16% (72 runs sampled) | ||
[Boolean] JSON x 99.29 ops/sec ±0.88% (64 runs sampled) | ||
[Boolean] Compactr x 190 ops/sec ±1.17% (75 runs sampled) | ||
[Boolean] JSON x 220 ops/sec ±5.04% (71 runs sampled) | ||
[Boolean] Compactr x 731 ops/sec ±7.57% (74 runs sampled) | ||
[Float] JSON x 66.76 ops/sec ±1.10% (61 runs sampled) | ||
[Float] Compactr x 112 ops/sec ±1.67% (70 runs sampled) | ||
[Float] JSON x 159 ops/sec ±3.41% (70 runs sampled) | ||
[Float] Compactr x 476 ops/sec ±1.58% (85 runs sampled) | ||
[Integer] JSON x 103 ops/sec ±1.41% (66 runs sampled) | ||
[Integer] Compactr x 202 ops/sec ±1.74% (74 runs sampled) | ||
[Integer] JSON x 264 ops/sec ±1.79% (79 runs sampled) | ||
[Integer] Compactr x 885 ops/sec ±1.36% (84 runs sampled) | ||
[Integer (negative)] JSON x 109 ops/sec ±1.24% (69 runs sampled) | ||
[Integer (negative)] Compactr x 203 ops/sec ±1.55% (74 runs sampled) | ||
[Object] JSON x 139 ops/sec ±1.89% (76 runs sampled) | ||
[Object] Compactr x 169 ops/sec ±1.52% (80 runs sampled) | ||
[Object] JSON x 61.91 ops/sec ±1.32% (58 runs sampled) | ||
[Object] Compactr x 44.78 ops/sec ±2.19% (54 runs sampled) | ||
[String] JSON x 68.89 ops/sec ±1.48% (63 runs sampled) | ||
[String] Compactr x 79.16 ops/sec ±2.04% (61 runs sampled) | ||
[String (special characters)] JSON x 71.65 ops/sec ±1.15% (65 runs sampled) | ||
[String (special characters)] Compactr x 144 ops/sec ±1.37% (71 runs sampled) | ||
[String] JSON x 107 ops/sec ±6.86% (64 runs sampled) | ||
[String] Compactr x 167 ops/sec ±4.86% (72 runs sampled) | ||
``` | ||
@@ -99,0 +93,0 @@ |
@@ -1,2 +0,2 @@ | ||
/** Data writer component */ | ||
/** Data reader component */ | ||
@@ -14,8 +14,8 @@ 'use strict'; | ||
function read(bytes) { | ||
readMap(bytes); | ||
return readData(bytes); | ||
readHeader(bytes); | ||
return readContent(bytes, scope.contentBegins); | ||
} | ||
function readMap(bytes) { | ||
scope.header.length = 0; | ||
function readHeader(bytes) { | ||
scope.header = []; | ||
let caret = 1; | ||
@@ -47,4 +47,4 @@ const keys = bytes[0]; | ||
function readData(bytes) { | ||
let caret = scope.contentBegins; | ||
function readContent(bytes, caret) { | ||
caret = caret || 0; | ||
const ret = {}; | ||
@@ -58,3 +58,3 @@ for (let i = 0; i < scope.header.length; i++) { | ||
return { read, readMap, readData }; | ||
return { read, readHeader, readContent }; | ||
} | ||
@@ -64,2 +64,2 @@ | ||
module.exports = Reader; | ||
module.exports = Reader; |
@@ -16,3 +16,21 @@ /** Schema parsing component */ | ||
function Schema(schema) { | ||
const sizeRef = { | ||
boolean: 1, | ||
number: 8, | ||
int8: 1, | ||
int16: 2, | ||
int32: 4, | ||
double: 8, | ||
string: 2, | ||
char8: 1, | ||
char16: 2, | ||
char32: 4, | ||
array: 2, | ||
object: 1, | ||
unsigned: 8, | ||
unsigned8: 1, | ||
unsigned16: 2, | ||
unsigned32: 4 | ||
}; | ||
const scope = { | ||
@@ -27,5 +45,8 @@ schema, | ||
}; | ||
scope.indices = preformat(schema); | ||
const writer = Writer(scope); | ||
const reader = Reader(scope); | ||
applyBlank(); // Pre-load header for easy streaming | ||
function preformat(schema) { | ||
@@ -43,2 +64,3 @@ const ret = {}; | ||
index, | ||
type: keyType, | ||
transformIn: (childSchema !== undefined) ? Encoder[keyType].bind(null, childSchema) : Encoder[keyType], | ||
@@ -49,3 +71,4 @@ transformOut: (childSchema !== undefined) ? Decoder[keyType].bind(null, childSchema) : Decoder[keyType], | ||
size: schema[key].size || null, | ||
count | ||
count, | ||
nested: childSchema | ||
}; | ||
@@ -57,2 +80,11 @@ }); | ||
function applyBlank() { | ||
for (let key in scope.schema) { | ||
scope.header.push({ | ||
key: scope.indices[key], | ||
size: scope.indices[key].size || sizeRef[scope.indices[key].type] | ||
}); | ||
} | ||
} | ||
function computeNested(schema, key) { | ||
@@ -81,3 +113,3 @@ const keyType = schema[key].type; | ||
return Object.assign({}, Writer(scope), Reader(scope)); | ||
return Object.assign({}, writer, reader); | ||
} | ||
@@ -84,0 +116,0 @@ |
@@ -37,6 +37,19 @@ /** Data writer component */ | ||
function clear() { | ||
scope.headerBytes.length = 0; | ||
scope.contentBytes.length = 0; | ||
scope.headerBytes = [0]; | ||
scope.contentBytes = []; | ||
} | ||
function sizes(data) { | ||
const s = {}; | ||
for (let key in data) { | ||
if (data[key] instanceof Object) { | ||
s[key] = scope.indices[key].nested.sizes(data[key]); | ||
s.size = scope.indices[key].transformIn(data[key]).length; | ||
} | ||
else s[key] = scope.indices[key].transformIn(data[key]).length; | ||
} | ||
return s; | ||
} | ||
function filterKeys(data) { | ||
@@ -55,3 +68,3 @@ const res = []; | ||
return res; | ||
} | ||
} | ||
@@ -82,3 +95,3 @@ function headerBuffer() { | ||
return { write, headerBuffer, headerArray, contentBuffer, contentArray, buffer, array }; | ||
return { write, headerBuffer, headerArray, contentBuffer, contentArray, buffer, array, sizes }; | ||
} | ||
@@ -85,0 +98,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
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
18100
462
0
115