compact-encoding
Advanced tools
Comparing version 2.8.0 to 2.9.0
@@ -6,4 +6,4 @@ const b4a = require('b4a') | ||
exports.state = function () { | ||
return { start: 0, end: 0, buffer: null } | ||
exports.state = function (start = 0, end = 0, buffer = null) { | ||
return { start, end, buffer, cache: null } | ||
} | ||
@@ -449,3 +449,3 @@ | ||
exports.encode = function encode (enc, m) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = exports.state() | ||
enc.preencode(state, m) | ||
@@ -458,3 +458,3 @@ state.buffer = b4a.allocUnsafe(state.end) | ||
exports.decode = function decode (enc, buffer) { | ||
return enc.decode({ start: 0, end: buffer.byteLength, buffer }) | ||
return enc.decode(exports.state(0, buffer.byteLength, buffer)) | ||
} | ||
@@ -461,0 +461,0 @@ |
{ | ||
"name": "compact-encoding", | ||
"version": "2.8.0", | ||
"version": "2.9.0", | ||
"description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,3 @@ # compact-encoding | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = cenc.state() | ||
@@ -21,3 +21,3 @@ // use preencode to figure out how big a buffer is needed | ||
console.log(state) // { start: 0, end: 4, buffer: null } | ||
console.log(state) // { start: 0, end: 4, buffer: null, cache: null } | ||
@@ -41,3 +41,3 @@ state.buffer = Buffer.allocUnsafe(state.end) | ||
Should be an object that looks like this `{ start, end, buffer }`. | ||
Should be an object that looks like this `{ start, end, buffer, cache }`. | ||
@@ -49,2 +49,3 @@ You can also get a blank state object using `cenc.state()`. | ||
* `buffer` should be either a Node.js Buffer or Uint8Array. | ||
* `cache` is used internally be codecs, starts out as `null`. | ||
@@ -51,0 +52,0 @@ #### `enc.preencode(state, val)` |
146
test.js
@@ -8,15 +8,15 @@ const enc = require('./') | ||
enc.uint.preencode(state, 42) | ||
t.alike(state, { start: 0, end: 1, buffer: null }) | ||
t.alike(state, enc.state(0, 1)) | ||
enc.uint.preencode(state, 4200) | ||
t.alike(state, { start: 0, end: 4, buffer: null }) | ||
t.alike(state, enc.state(0, 4)) | ||
enc.uint.preencode(state, Number.MAX_SAFE_INTEGER) | ||
t.alike(state, { start: 0, end: 13, buffer: null }) | ||
t.alike(state, enc.state(0, 13)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.uint.encode(state, 42) | ||
t.alike(state, { start: 1, end: 13, buffer: Buffer.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(1, 13, Buffer.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) | ||
enc.uint.encode(state, 4200) | ||
t.alike(state, { start: 4, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(4, 13, Buffer.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) | ||
enc.uint.encode(state, Number.MAX_SAFE_INTEGER) | ||
t.alike(state, { start: 13, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0]) }) | ||
t.alike(state, enc.state(13, 13, Buffer.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0]))) | ||
@@ -36,11 +36,11 @@ state.start = 0 | ||
enc.int.preencode(state, 42) | ||
t.alike(state, { start: 0, end: 1, buffer: null }) | ||
t.alike(state, enc.state(0, 1)) | ||
enc.int.preencode(state, -4200) | ||
t.alike(state, { start: 0, end: 4, buffer: null }) | ||
t.alike(state, enc.state(0, 4)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.int.encode(state, 42) | ||
t.alike(state, { start: 1, end: 4, buffer: Buffer.from([84, 0, 0, 0]) }) | ||
t.alike(state, enc.state(1, 4, Buffer.from([84, 0, 0, 0]))) | ||
enc.int.encode(state, -4200) | ||
t.alike(state, { start: 4, end: 4, buffer: Buffer.from([84, 0xfd, 207, 32]) }) | ||
t.alike(state, enc.state(4, 4, Buffer.from([84, 0xfd, 207, 32]))) | ||
@@ -59,8 +59,8 @@ state.start = 0 | ||
enc.float64.preencode(state, 162.2377294) | ||
t.alike(state, { start: 0, end: 8, buffer: null }) | ||
t.alike(state, enc.state(0, 8)) | ||
state.buffer = Buffer.alloc(state.end) | ||
t.alike(state, { start: 0, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(0, 8, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]))) | ||
enc.float64.encode(state, 162.2377294) | ||
t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) }) | ||
t.alike(state, enc.state(8, 8, Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))) | ||
@@ -80,9 +80,9 @@ state.start = 0 | ||
enc.float64.preencode(state, 162.2377294) | ||
t.alike(state, { start: 0, end: 9, buffer: null }) | ||
t.alike(state, enc.state(0, 9)) | ||
state.buffer = Buffer.alloc(state.end) | ||
t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(0, 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]))) | ||
enc.int.encode(state, 0) | ||
enc.float64.encode(state, 162.2377294) | ||
t.alike(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) }) | ||
t.alike(state, enc.state(9, 9, Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))) | ||
@@ -98,6 +98,6 @@ state.start = 0 | ||
state.buffer = buf.subarray(1) | ||
t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(0, 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]))) | ||
enc.int.encode(state, 0) | ||
enc.float64.encode(state, 162.2377294) | ||
t.alike(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) }) | ||
t.alike(state, enc.state(9, 9, Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))) | ||
t.alike(buf, Buffer.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])) | ||
@@ -118,3 +118,3 @@ | ||
enc.float64.encode(state, 0) | ||
t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(8, 8, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]))) | ||
@@ -133,3 +133,3 @@ state.start = 0 | ||
enc.float64.encode(state, Infinity) | ||
t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]) }) | ||
t.alike(state, enc.state(8, 8, Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]))) | ||
@@ -148,3 +148,3 @@ state.start = 0 | ||
enc.float64.encode(state, 0.1 + 0.2) | ||
t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]) }) | ||
t.alike(state, enc.state(8, 8, Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]))) | ||
@@ -160,15 +160,15 @@ state.start = 0 | ||
enc.buffer.preencode(state, Buffer.from('hi')) | ||
t.alike(state, { start: 0, end: 3, buffer: null }) | ||
t.alike(state, enc.state(0, 3)) | ||
enc.buffer.preencode(state, Buffer.from('hello')) | ||
t.alike(state, { start: 0, end: 9, buffer: null }) | ||
t.alike(state, enc.state(0, 9)) | ||
enc.buffer.preencode(state, null) | ||
t.alike(state, { start: 0, end: 10, buffer: null }) | ||
t.alike(state, enc.state(0, 10)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.buffer.encode(state, Buffer.from('hi')) | ||
t.alike(state, { start: 3, end: 10, buffer: Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00') }) | ||
t.alike(state, enc.state(3, 10, Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00'))) | ||
enc.buffer.encode(state, Buffer.from('hello')) | ||
t.alike(state, { start: 9, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') }) | ||
t.alike(state, enc.state(9, 10, Buffer.from('\x02hi\x05hello\x00'))) | ||
enc.buffer.encode(state, null) | ||
t.alike(state, { start: 10, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') }) | ||
t.alike(state, enc.state(10, 10, Buffer.from('\x02hi\x05hello\x00'))) | ||
@@ -188,7 +188,7 @@ state.start = 0 | ||
enc.raw.preencode(state, Buffer.from('hi')) | ||
t.alike(state, { start: 0, end: 2, buffer: null }) | ||
t.alike(state, enc.state(0, 2)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.raw.encode(state, Buffer.from('hi')) | ||
t.alike(state, { start: 2, end: 2, buffer: Buffer.from('hi') }) | ||
t.alike(state, enc.state(2, 2, Buffer.from('hi'))) | ||
@@ -204,7 +204,7 @@ state.start = 0 | ||
enc.uint16array.preencode(state, new Uint16Array([1, 2, 3])) | ||
t.alike(state, { start: 0, end: 7, buffer: null }) | ||
t.alike(state, enc.state(0, 7)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.uint16array.encode(state, new Uint16Array([1, 2, 3])) | ||
t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 2, 0, 3, 0]) }) | ||
t.alike(state, enc.state(7, 7, Buffer.from([3, 1, 0, 2, 0, 3, 0]))) | ||
@@ -222,11 +222,11 @@ state.start = 0 | ||
enc.uint32array.preencode(state, new Uint32Array([1])) | ||
t.alike(state, { start: 0, end: 5, buffer: null }) | ||
t.alike(state, enc.state(0, 5)) | ||
enc.uint32array.preencode(state, new Uint32Array([42, 43])) | ||
t.alike(state, { start: 0, end: 14, buffer: null }) | ||
t.alike(state, enc.state(0, 14)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.uint32array.encode(state, new Uint32Array([1])) | ||
t.alike(state, { start: 5, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(5, 14, Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) | ||
enc.uint32array.encode(state, new Uint32Array([42, 43])) | ||
t.alike(state, { start: 14, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]) }) | ||
t.alike(state, enc.state(14, 14, Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]))) | ||
@@ -245,7 +245,7 @@ state.start = 0 | ||
enc.int16array.preencode(state, new Int16Array([1, -2, 3])) | ||
t.alike(state, { start: 0, end: 7, buffer: null }) | ||
t.alike(state, enc.state(0, 7)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.int16array.encode(state, new Int16Array([1, -2, 3])) | ||
t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 0xfe, 0xff, 3, 0]) }) | ||
t.alike(state, enc.state(7, 7, Buffer.from([3, 1, 0, 0xfe, 0xff, 3, 0]))) | ||
@@ -263,7 +263,7 @@ state.start = 0 | ||
enc.int32array.preencode(state, new Int32Array([1, -2, 3])) | ||
t.alike(state, { start: 0, end: 13, buffer: null }) | ||
t.alike(state, enc.state(0, 13)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.int32array.encode(state, new Int32Array([1, -2, 3])) | ||
t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0]) }) | ||
t.alike(state, enc.state(13, 13, Buffer.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0]))) | ||
@@ -281,7 +281,7 @@ state.start = 0 | ||
enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3])) | ||
t.alike(state, { start: 0, end: 13, buffer: null }) | ||
t.alike(state, enc.state(0, 13)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3])) | ||
t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40]) }) | ||
t.alike(state, enc.state(13, 13, Buffer.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40]))) | ||
@@ -299,7 +299,7 @@ state.start = 0 | ||
enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3])) | ||
t.alike(state, { start: 0, end: 25, buffer: null }) | ||
t.alike(state, enc.state(0, 25)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3])) | ||
t.alike(state, { start: 25, end: 25, buffer: Buffer.from([3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40]) }) | ||
t.alike(state, enc.state(25, 25, Buffer.from([3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40]))) | ||
@@ -317,11 +317,11 @@ state.start = 0 | ||
enc.string.preencode(state, '🌾') | ||
t.alike(state, { start: 0, end: 5, buffer: null }) | ||
t.alike(state, enc.state(0, 5)) | ||
enc.string.preencode(state, 'høsten er fin') | ||
t.alike(state, { start: 0, end: 20, buffer: null }) | ||
t.alike(state, enc.state(0, 20)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.string.encode(state, '🌾') | ||
t.alike(state, { start: 5, end: 20, buffer: Buffer.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') }) | ||
t.alike(state, enc.state(5, 20, Buffer.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))) | ||
enc.string.encode(state, 'høsten er fin') | ||
t.alike(state, { start: 20, end: 20, buffer: Buffer.from('\x04🌾\x0ehøsten er fin') }) | ||
t.alike(state, enc.state(20, 20, Buffer.from('\x04🌾\x0ehøsten er fin'))) | ||
@@ -340,11 +340,11 @@ state.start = 0 | ||
enc.fixed32.preencode(state, Buffer.alloc(32).fill('a')) | ||
t.alike(state, { start: 0, end: 32, buffer: null }) | ||
t.alike(state, enc.state(0, 32)) | ||
enc.fixed32.preencode(state, Buffer.alloc(32).fill('b')) | ||
t.alike(state, { start: 0, end: 64, buffer: null }) | ||
t.alike(state, enc.state(0, 64)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.fixed32.encode(state, Buffer.alloc(32).fill('a')) | ||
t.alike(state, { start: 32, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32) }) | ||
t.alike(state, enc.state(32, 64, Buffer.alloc(64).fill('a', 0, 32))) | ||
enc.fixed32.encode(state, Buffer.alloc(32).fill('b')) | ||
t.alike(state, { start: 64, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64) }) | ||
t.alike(state, enc.state(64, 64, Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64))) | ||
@@ -363,11 +363,11 @@ state.start = 0 | ||
enc.fixed64.preencode(state, Buffer.alloc(64).fill('a')) | ||
t.alike(state, { start: 0, end: 64, buffer: null }) | ||
t.alike(state, enc.state(0, 64)) | ||
enc.fixed64.preencode(state, Buffer.alloc(64).fill('b')) | ||
t.alike(state, { start: 0, end: 128, buffer: null }) | ||
t.alike(state, enc.state(0, 128)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.fixed64.encode(state, Buffer.alloc(64).fill('a')) | ||
t.alike(state, { start: 64, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64) }) | ||
t.alike(state, enc.state(64, 128, Buffer.alloc(128).fill('a', 0, 64))) | ||
enc.fixed64.encode(state, Buffer.alloc(64).fill('b')) | ||
t.alike(state, { start: 128, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128) }) | ||
t.alike(state, enc.state(128, 128, Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128))) | ||
@@ -387,11 +387,11 @@ state.start = 0 | ||
fixed.preencode(state, Buffer.alloc(3).fill('a')) | ||
t.alike(state, { start: 0, end: 3, buffer: null }) | ||
t.alike(state, enc.state(0, 3)) | ||
fixed.preencode(state, Buffer.alloc(3).fill('b')) | ||
t.alike(state, { start: 0, end: 6, buffer: null }) | ||
t.alike(state, enc.state(0, 6)) | ||
state.buffer = Buffer.alloc(state.end) | ||
fixed.encode(state, Buffer.alloc(3).fill('a')) | ||
t.alike(state, { start: 3, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3) }) | ||
t.alike(state, enc.state(3, 6, Buffer.alloc(6).fill('a', 0, 3))) | ||
fixed.encode(state, Buffer.alloc(3).fill('b')) | ||
t.alike(state, { start: 6, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6) }) | ||
t.alike(state, enc.state(6, 6, Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6))) | ||
@@ -413,11 +413,11 @@ state.start = 0 | ||
arr.preencode(state, [true, false, true]) | ||
t.alike(state, { start: 0, end: 4, buffer: null }) | ||
t.alike(state, enc.state(0, 4)) | ||
arr.preencode(state, [false, false, true, true]) | ||
t.alike(state, { start: 0, end: 9, buffer: null }) | ||
t.alike(state, enc.state(0, 9)) | ||
state.buffer = Buffer.alloc(state.end) | ||
arr.encode(state, [true, false, true]) | ||
t.alike(state, { start: 4, end: 9, buffer: Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0]) }) | ||
t.alike(state, enc.state(4, 9, Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0]))) | ||
arr.encode(state, [false, false, true, true]) | ||
t.alike(state, { start: 9, end: 9, buffer: Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1]) }) | ||
t.alike(state, enc.state(9, 9, Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1]))) | ||
@@ -436,14 +436,10 @@ state.start = 0 | ||
enc.json.preencode(state, { a: 1, b: 2 }) | ||
t.alike(state, { start: 0, end: 14, buffer: null }) | ||
t.alike(state, enc.state(0, 14)) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.json.encode(state, { a: 1, b: 2 }) | ||
t.alike(state, { | ||
start: 14, | ||
end: 14, | ||
buffer: Buffer.concat([ | ||
Buffer.from([13]), | ||
Buffer.from('{"a":1,"b":2}') | ||
]) | ||
}) | ||
t.alike(state, enc.state(14, 14, Buffer.concat([ | ||
Buffer.from([13]), | ||
Buffer.from('{"a":1,"b":2}') | ||
]))) | ||
@@ -513,7 +509,3 @@ state.start = 0 | ||
const state = { | ||
start: 0, | ||
end: 0, | ||
buffer: null | ||
} | ||
const state = enc.state() | ||
@@ -520,0 +512,0 @@ enc.lexint.preencode(state, num) |
126
39705
981