protobuf-codec
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -26,7 +26,7 @@ export function tag (bigint) { | ||
export function sint64 (bigint) { | ||
return ((bigint >> 1n) ^ (bigint) << 63n) >> 63n | ||
return ((bigint >> 1n) ^ (-1n * (bigint & 1n))) | ||
} | ||
export function sint32 (bigint) { | ||
return Number(((bigint >> 1n) ^ (bigint) << 31n) >> 31n) | ||
return Number((bigint >> 1n) ^ (-1n * (bigint & 1n))) | ||
} | ||
@@ -38,2 +38,6 @@ | ||
export function enumerable (uint) { | ||
return Number(uint) | 0 // trick to cast uint to int | ||
} | ||
export function bytes (bytes) { | ||
@@ -40,0 +44,0 @@ return bytes |
@@ -16,3 +16,4 @@ import assert from 'nanoassert' | ||
) { | ||
assert(int <= varint.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
let o = byteOffset | ||
@@ -32,3 +33,4 @@ let n = BigInt(int) | ||
encodeOversize (int, len, buf, byteOffset = 0) { | ||
assert(int <= varint.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
assert(len >= this.encodingLength(int), 'len does not fit int') | ||
@@ -52,3 +54,4 @@ assert(buf.byteLength - byteOffset >= len, 'buf does not fit len') | ||
encodingLength (int) { | ||
assert(int <= varint.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
if (int <= 0xffff_ffff) return (9 * (32 - Math.clz32(Number(int))) + 64) / 64 | 0 | ||
@@ -58,2 +61,3 @@ const high = Number(BigInt(int) >> 32n) | ||
}, | ||
MIN_VALUE: 0n, | ||
MAX_VALUE: (1n << 64n) - 1n | ||
@@ -97,2 +101,3 @@ } | ||
}, | ||
MIN_VALUE: 1n, | ||
MAX_VALUE: (1n << 29n) - 1n | ||
@@ -117,11 +122,17 @@ } | ||
export const uint64 = { | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
const bigint = BigInt(int) | ||
varint.encode(BigInt.asUintN(64, bigint), buf, byteOffset) | ||
encode (uint, buf = alloc(this, uint), byteOffset = 0) { | ||
assert(uint >= this.MIN_VALUE, 'uint exceeds MIN_VALUE') | ||
assert(uint <= this.MAX_VALUE, 'uint exceeds MAX_VALUE') | ||
const biguint = BigInt(uint) | ||
varint.encode(BigInt.asUintN(64, biguint), buf, byteOffset) | ||
this.encode.bytes = varint.encode.bytes | ||
return buf.subarray(byteOffset, byteOffset + this.encode.bytes) | ||
}, | ||
encodingLength (int) { | ||
return varint.encodingLength(int) | ||
} | ||
encodingLength (uint) { | ||
assert(uint >= this.MIN_VALUE, 'uint exceeds MIN_VALUE') | ||
assert(uint <= this.MAX_VALUE, 'uint exceeds MAX_VALUE') | ||
return varint.encodingLength(uint) | ||
}, | ||
MIN_VALUE: varint.MIN_VALUE, | ||
MAX_VALUE: varint.MAX_VALUE | ||
} | ||
@@ -131,2 +142,4 @@ | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
const bigint = BigInt(int) | ||
@@ -138,4 +151,8 @@ varint.encode(BigInt.asIntN(64, bigint), buf, byteOffset) | ||
encodingLength (int) { | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
return varint.encodingLength(int) | ||
} | ||
}, | ||
MIN_VALUE: -(1n << 63n), | ||
MAX_VALUE: (1n << 63n) - 1n | ||
} | ||
@@ -145,4 +162,6 @@ | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
const bigint = BigInt(int) | ||
varint.encode(BigInt.asIntN(64, (bigint << 1n) ^ (bigint >> 63n)), buf, byteOffset) | ||
varint.encode(BigInt.asUintN(64, (bigint << 1n) ^ (bigint >> 63n)), buf, byteOffset) | ||
this.encode.bytes = varint.encode.bytes | ||
@@ -152,9 +171,16 @@ return buf.subarray(byteOffset, byteOffset + this.encode.bytes) | ||
encodingLength (int) { | ||
return varint.encodingLength(int) | ||
} | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
const bigint = BigInt(int) | ||
return varint.encodingLength((bigint << 1n) ^ (bigint >> 63n)) | ||
}, | ||
MIN_VALUE: -(1n << 63n), | ||
MAX_VALUE: (1n << 63n) - 1n | ||
} | ||
export const uint32 = { | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
const bigint = BigInt(int) | ||
encode (uint, buf = alloc(this, uint), byteOffset = 0) { | ||
assert(uint >= this.MIN_VALUE, 'uint exceeds MIN_VALUE') | ||
assert(uint <= this.MAX_VALUE, 'uint exceeds MAX_VALUE') | ||
const bigint = BigInt(uint) | ||
varint.encode(BigInt.asUintN(32, bigint), buf, byteOffset) | ||
@@ -164,5 +190,9 @@ this.encode.bytes = varint.encode.bytes | ||
}, | ||
encodingLength (int) { | ||
return varint.encodingLength(int) | ||
} | ||
encodingLength (uint) { | ||
assert(uint >= this.MIN_VALUE, 'uint exceeds MIN_VALUE') | ||
assert(uint <= this.MAX_VALUE, 'uint exceeds MAX_VALUE') | ||
return varint.encodingLength(uint) | ||
}, | ||
MIN_VALUE: 0, | ||
MAX_VALUE: (1n << 32n) - 1n | ||
} | ||
@@ -172,2 +202,4 @@ | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
const bigint = BigInt(int) | ||
@@ -179,4 +211,8 @@ varint.encode(BigInt.asIntN(32, bigint), buf, byteOffset) | ||
encodingLength (int) { | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
return varint.encodingLength(int) | ||
} | ||
}, | ||
MIN_VALUE: -(1n << 31n), | ||
MAX_VALUE: (1n << 31n) - 1n | ||
} | ||
@@ -186,4 +222,6 @@ | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
const bigint = BigInt(int) | ||
varint.encode(BigInt.asIntN(32, (bigint << 1n) ^ (bigint >> 31n)), buf, byteOffset) | ||
varint.encode((bigint << 1n) ^ (bigint >> 31n), buf, byteOffset) | ||
this.encode.bytes = varint.encode.bytes | ||
@@ -193,4 +231,9 @@ return buf.subarray(byteOffset, byteOffset + this.encode.bytes) | ||
encodingLength (int) { | ||
return varint.encodingLength(int) | ||
} | ||
assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
assert(int <= this.MAX_VALUE, 'int exceeds MAX_VALUE') | ||
const bigint = BigInt(int) | ||
return varint.encodingLength((bigint << 1n) ^ (bigint >> 31n)) | ||
}, | ||
MIN_VALUE: -(1n << 31n), | ||
MAX_VALUE: (1n << 31n) - 1n | ||
} | ||
@@ -237,3 +280,6 @@ | ||
encode (en, buf = alloc(this, en), byteOffset = 0) { | ||
assert(en >= enumerable.MIN_VALUE, 'enum value exceeds MIN_VALUE') | ||
assert(en <= enumerable.MAX_VALUE, 'enum value exceeds MAX_VALUE') | ||
en = en >>> 0 // cast to uint32 for varint encoding | ||
varint.encode(en, buf, byteOffset) | ||
@@ -244,5 +290,6 @@ this.encode.bytes = varint.encode.bytes | ||
encodingLength (en) { | ||
return varint.encodingLength(en) | ||
return varint.encodingLength(en >>> 0) | ||
}, | ||
MAX_VALUE: (1n << 32n) - 1n | ||
MIN_VALUE: -(1n << 31n), | ||
MAX_VALUE: (1n << 31n) - 1n | ||
} | ||
@@ -249,0 +296,0 @@ |
{ | ||
"name": "protobuf-codec", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Minimal Protocol Buffers wire encoding/decoding", | ||
@@ -5,0 +5,0 @@ "type": "module", |
25125
13
608