Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compact-encoding

Package Overview
Dependencies
Maintainers
3
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compact-encoding - npm Package Compare versions

Comparing version 2.14.0 to 2.15.0

106

index.js

@@ -167,12 +167,62 @@ const b4a = require('b4a')

exports.int = zigZag(uint)
exports.int8 = zigZag(uint8)
exports.int16 = zigZag(uint16)
exports.int24 = zigZag(uint24)
exports.int32 = zigZag(uint32)
exports.int40 = zigZag(uint40)
exports.int48 = zigZag(uint48)
exports.int56 = zigZag(uint56)
exports.int64 = zigZag(uint64)
exports.int = zigZagInt(uint)
exports.int8 = zigZagInt(uint8)
exports.int16 = zigZagInt(uint16)
exports.int24 = zigZagInt(uint24)
exports.int32 = zigZagInt(uint32)
exports.int40 = zigZagInt(uint40)
exports.int48 = zigZagInt(uint48)
exports.int56 = zigZagInt(uint56)
exports.int64 = zigZagInt(uint64)
const biguint64 = exports.biguint64 = {
preencode (state, n) {
state.end += 8
},
encode (state, n) {
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
view.setBigUint64(0, n, true) // little endian
state.start += 8
},
decode (state) {
if (state.end - state.start < 8) throw new Error('Out of bounds')
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
const n = view.getBigUint64(0, true) // little endian
state.start += 8
return n
}
}
exports.bigint64 = zigZagBigInt(biguint64)
const biguint = exports.biguint = {
preencode (state, n) {
let len = 0
for (let m = n; m; m = m >> 64n) len++
uint.preencode(state, len)
state.end += 8 * len
},
encode (state, n) {
let len = 0
for (let m = n; m; m = m >> 64n) len++
uint.encode(state, len)
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8 * len)
for (let m = n, i = 0; m; m = m >> 64n, i += 8) {
view.setBigUint64(i, BigInt.asUintN(64, m), true) // little endian
}
state.start += 8 * len
},
decode (state) {
const len = uint.decode(state)
if (state.end - state.start < 8 * len) throw new Error('Out of bounds')
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8 * len)
let n = 0n
for (let i = len - 1; i >= 0; i--) n = (n << 64n) + view.getBigUint64(i * 8, true) // little endian
state.start += 8 * len
return n
}
}
exports.bigint = zigZagBigInt(biguint)
exports.lexint = require('./lexint')

@@ -310,2 +360,5 @@

exports.biguint64array = typedarray(BigUint64Array, b4a.swap64)
exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
exports.float32array = typedarray(Float32Array, b4a.swap32)

@@ -644,12 +697,12 @@ exports.float64array = typedarray(Float64Array, b4a.swap64)

function zigZag (enc) {
function zigZagInt (enc) {
return {
preencode (state, n) {
enc.preencode(state, zigZagEncode(n))
enc.preencode(state, zigZagEncodeInt(n))
},
encode (state, n) {
enc.encode(state, zigZagEncode(n))
enc.encode(state, zigZagEncodeInt(n))
},
decode (state) {
return zigZagDecode(enc.decode(state))
return zigZagDecodeInt(enc.decode(state))
}

@@ -659,9 +712,32 @@ }

function zigZagDecode (n) {
function zigZagDecodeInt (n) {
return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2
}
function zigZagEncode (n) {
function zigZagEncodeInt (n) {
// 0, -1, 1, -2, 2, ...
return n < 0 ? (2 * -n) - 1 : n === 0 ? 0 : 2 * n
}
function zigZagBigInt (enc) {
return {
preencode (state, n) {
enc.preencode(state, zigZagEncodeBigInt(n))
},
encode (state, n) {
enc.encode(state, zigZagEncodeBigInt(n))
},
decode (state) {
return zigZagDecodeBigInt(enc.decode(state))
}
}
}
function zigZagDecodeBigInt (n) {
return n === 0n ? n : (n & 1n) === 0n ? n / 2n : -(n + 1n) / 2n
}
function zigZagEncodeBigInt (n) {
// 0, -1, 1, -2, 2, ...
return n < 0n ? (2n * -n) - 1n : n === 0n ? 0n : 2n * n
}

2

package.json
{
"name": "compact-encoding",
"version": "2.14.0",
"version": "2.15.0",
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -107,2 +107,5 @@ const b4a = require('b4a')

exports.biguint64array = typedarray(BigUint64Array, b4a.swap64)
exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
exports.float32array = typedarray(Float32Array, b4a.swap32)

@@ -109,0 +112,0 @@ exports.float64array = typedarray(Float64Array, b4a.swap64)

@@ -97,3 +97,6 @@ # compact-encoding

* `cenc.int64` - Encodes a fixed size int64 using `cenc.uint64` with ZigZag encoding.
* `cenc.lexint` - Encodes an int using [lexicographic-integer](https://github.com/substack/lexicographic-integer) encoding so that encoded values are lexicographically sorted in ascending numerical order.
* `cenc.biguint64` - Encodes a fixed size biguint64.
* `cenc.bigint64` - Encodes a fixed size bigint64 using `cenc.biguint64` with ZigZag encoding.
* `cenc.biguint` - Encodes a biguint with its word count uint prefixed.
* `cenc.bigint` - Encodes a bigint using `cenc.biguint` with ZigZag encoding.
* `cenc.float32` - Encodes a fixed size float32.

@@ -117,2 +120,6 @@ * `cenc.float64` - Encodes a fixed size float64.

* `cenc.raw.int32array` - Encodes a int32array without a length prefixed.
* `cenc.biguint64array` - Encodes a biguint64array with its element length uint prefixed.
* `cenc.raw.biguint64array` - Encodes a biguint64array without a length prefixed.
* `cenc.bigint64array` - Encodes a bigint64array with its element length uint prefixed.
* `cenc.raw.bigint64array` - Encodes a bigint64array without a length prefixed.
* `cenc.float32array` - Encodes a float32array with its element length uint prefixed.

@@ -149,2 +156,3 @@ * `cenc.raw.float32array` - Encodes a float32array without a length prefixed.

* `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
* `cenc.none` - Helper for when you want to just express nothing

@@ -151,0 +159,0 @@ ## License

@@ -149,2 +149,78 @@ const enc = require('./')

test('biguint64', function (t) {
const state = enc.state()
const n = 0x0102030405060708n
enc.biguint64.preencode(state, n)
t.alike(state, enc.state(0, 8))
state.buffer = b4a.alloc(state.end)
enc.biguint64.encode(state, n)
t.alike(state, enc.state(8, 8, b4a.from([0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1])))
state.start = 0
t.is(enc.biguint64.decode(state), n)
t.is(state.start, state.end)
t.exception(() => enc.biguint64.decode(state))
})
test('bigint64', function (t) {
const state = enc.state()
const n = -0x0102030405060708n
enc.bigint64.preencode(state, n)
t.alike(state, enc.state(0, 8))
state.buffer = b4a.alloc(state.end)
enc.bigint64.encode(state, n)
t.alike(state, enc.state(8, 8, b4a.from([0xf, 0xe, 0xc, 0xa, 0x8, 0x6, 0x4, 0x2])))
state.start = 0
t.is(enc.bigint64.decode(state), n)
t.is(state.start, state.end)
t.exception(() => enc.bigint64.decode(state))
})
test('biguint', function (t) {
const state = enc.state()
const n = 0x0102030405060708090a0b0cn
enc.biguint.preencode(state, n)
t.alike(state, enc.state(0, 17))
state.buffer = b4a.alloc(state.end)
enc.biguint.encode(state, n)
t.alike(state, enc.state(17, 17, b4a.from([2, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0])))
state.start = 0
t.is(enc.biguint.decode(state), n)
t.is(state.start, state.end)
t.exception(() => enc.biguint.decode(state))
})
test('bigint', function (t) {
const state = enc.state()
const n = -0x0102030405060708090a0b0cn
enc.bigint.preencode(state, n)
t.alike(state, enc.state(0, 17))
state.buffer = b4a.alloc(state.end)
enc.bigint.encode(state, n)
t.alike(state, enc.state(17, 17, b4a.from([2, 0x17, 0x16, 0x14, 0x12, 0x10, 0xe, 0xc, 0xa, 0x8, 0x6, 0x4, 0x2, 0x0, 0x0, 0x0, 0x0])))
state.start = 0
t.is(enc.bigint.decode(state), n)
t.is(state.start, state.end)
t.exception(() => enc.bigint.decode(state))
})
test('buffer', function (t) {

@@ -312,2 +388,40 @@ const state = enc.state()

test('biguint64array', function (t) {
const state = enc.state()
const arr = new BigUint64Array([0x01020304n, 0x05060708n, 0x090a0b0cn])
enc.biguint64array.preencode(state, arr)
t.alike(state, enc.state(0, 25))
state.buffer = b4a.alloc(state.end)
enc.biguint64array.encode(state, arr)
t.alike(state, enc.state(25, 25, b4a.from([3, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0, 0x8, 0x7, 0x6, 0x5, 0x0, 0x0, 0x0, 0x0, 0xc, 0xb, 0xa, 0x9, 0x0, 0x0, 0x0, 0x0])))
state.start = 0
t.alike(enc.biguint64array.decode(state), arr)
t.is(state.start, state.end)
t.exception(() => enc.biguint64array.decode(state))
})
test('bigint64array', function (t) {
const state = enc.state()
const arr = new BigInt64Array([-0x01020304n, 0x05060708n, -0x090a0b0cn])
enc.bigint64array.preencode(state, arr)
t.alike(state, enc.state(0, 25))
state.buffer = b4a.alloc(state.end)
enc.bigint64array.encode(state, arr)
t.alike(state, enc.state(25, 25, b4a.from([3, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x8, 0x7, 0x6, 0x5, 0x0, 0x0, 0x0, 0x0, 0xf4, 0xf4, 0xf5, 0xf6, 0xff, 0xff, 0xff, 0xff])))
state.start = 0
t.alike(enc.bigint64array.decode(state), arr)
t.is(state.start, state.end)
t.exception(() => enc.bigint64array.decode(state))
})
test('float32array', function (t) {

@@ -314,0 +428,0 @@ const state = enc.state()

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc