diff --git a/package.json b/package.json index 20d1bf9..6fabbb0 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "test:firefox-webworker": "lerna run --concurrency 1 test:firefox-webworker -- --", "test:electron-main": "lerna run --concurrency 1 test:electron-main -- --", "test:electron-renderer": "lerna run --concurrency 1 test:electron-renderer -- --", + "clean": "lerna run clean", "build": "lerna run build", "lint": "lerna run lint", "dep-check": "lerna run dep-check", diff --git a/packages/protons-runtime/package.json b/packages/protons-runtime/package.json index f757ccb..295712e 100644 --- a/packages/protons-runtime/package.json +++ b/packages/protons-runtime/package.json @@ -141,12 +141,16 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", "dep-check": "aegir dep-check", "build": "aegir build", "release": "aegir release" }, "dependencies": { + "byte-access": "^1.0.1", + "longbits": "^1.1.0", + "uint8-varint": "^1.0.2", "uint8arraylist": "^2.0.0", "uint8arrays": "^3.0.0" }, diff --git a/packages/protons-runtime/src/codecs/bytes.ts b/packages/protons-runtime/src/codecs/bytes.ts index d86ce3e..e288b14 100644 --- a/packages/protons-runtime/src/codecs/bytes.ts +++ b/packages/protons-runtime/src/codecs/bytes.ts @@ -1,6 +1,6 @@ import { Uint8ArrayList } from 'uint8arraylist' -import { unsigned } from '../utils/varint.js' +import { unsigned } from 'uint8-varint' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' @@ -10,11 +10,10 @@ const encodingLength: EncodingLengthFunction = function bytesEncodin } const encode: EncodeFunction = function bytesEncode (val) { - const prefix = new Uint8Array(unsigned.encodingLength(val.byteLength)) - - unsigned.encode(val.byteLength, prefix) - - return new Uint8ArrayList(prefix, val) + return new Uint8ArrayList( + unsigned.encode(val.byteLength), + val + ) } const decode: DecodeFunction = function bytesDecode (buf, offset) { diff --git a/packages/protons-runtime/src/codecs/enum.ts b/packages/protons-runtime/src/codecs/enum.ts index a8ef222..f5648d5 100644 --- a/packages/protons-runtime/src/codecs/enum.ts +++ b/packages/protons-runtime/src/codecs/enum.ts @@ -1,7 +1,8 @@ -import { unsigned } from '../utils/varint.js' +import { unsigned } from 'uint8-varint' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction, Codec } from '../codec.js' +import { allocUnsafe } from '../utils/alloc.js' export function enumeration (v: any): Codec { function findValue (val: string | number): number { @@ -23,7 +24,7 @@ export function enumeration (v: any): Codec { const encode: EncodeFunction = function enumEncode (val) { const enumValue = findValue(val) - const buf = new Uint8Array(unsigned.encodingLength(enumValue)) + const buf = allocUnsafe(unsigned.encodingLength(enumValue)) unsigned.encode(enumValue, buf) return buf diff --git a/packages/protons-runtime/src/codecs/int32.ts b/packages/protons-runtime/src/codecs/int32.ts index ef4c636..9755e63 100644 --- a/packages/protons-runtime/src/codecs/int32.ts +++ b/packages/protons-runtime/src/codecs/int32.ts @@ -1,20 +1,23 @@ -import { signed } from '../utils/varint.js' +import { signed } from 'uint8-varint' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' const encodingLength: EncodingLengthFunction = function int32EncodingLength (val) { + if (val < 0) { + return 10 // 10 bytes per spec - https://developers.google.com/protocol-buffers/docs/encoding#signed-ints + } + return signed.encodingLength(val) } const encode: EncodeFunction = function int32Encode (val) { const buf = new Uint8Array(encodingLength(val)) - signed.encode(val, buf) - return buf + return signed.encode(val, buf) } const decode: DecodeFunction = function int32Decode (buf, offset) { - return signed.decode(buf, offset) + return signed.decode(buf, offset) | 0 } export const int32 = createCodec('int32', CODEC_TYPES.VARINT, encode, decode, encodingLength) diff --git a/packages/protons-runtime/src/codecs/int64.ts b/packages/protons-runtime/src/codecs/int64.ts index 47f571f..801bc4b 100644 --- a/packages/protons-runtime/src/codecs/int64.ts +++ b/packages/protons-runtime/src/codecs/int64.ts @@ -1,16 +1,19 @@ -import { signed } from '../utils/big-varint.js' +import { signed } from 'uint8-varint/big' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' const encodingLength: EncodingLengthFunction = function int64EncodingLength (val) { + if (val < 0n) { + return 10 // 10 bytes per spec - https://developers.google.com/protocol-buffers/docs/encoding#signed-ints + } + return signed.encodingLength(val) } const encode: EncodeFunction = function int64Encode (val) { const buf = new Uint8Array(encodingLength(val)) - signed.encode(val, buf) - return buf + return signed.encode(val, buf) } const decode: DecodeFunction = function int64Decode (buf, offset) { diff --git a/packages/protons-runtime/src/codecs/message.ts b/packages/protons-runtime/src/codecs/message.ts index 602a091..c259bef 100644 --- a/packages/protons-runtime/src/codecs/message.ts +++ b/packages/protons-runtime/src/codecs/message.ts @@ -1,8 +1,9 @@ -import { unsigned } from '../utils/varint.js' +import { unsigned } from 'uint8-varint' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction, Codec } from '../codec.js' import { Uint8ArrayList } from 'uint8arraylist' import type { FieldDefs, FieldDef } from '../index.js' +import { allocUnsafe } from '../utils/alloc.js' export interface Factory { new (obj: A): T @@ -32,7 +33,7 @@ export function message (fieldDefs: FieldDefs): Codec { } const key = (fieldNumber << 3) | fieldDef.codec.type - const prefix = new Uint8Array(unsigned.encodingLength(key)) + const prefix = allocUnsafe(unsigned.encodingLength(key)) unsigned.encode(key, prefix) const encoded = fieldDef.codec.encode(value) @@ -56,8 +57,7 @@ export function message (fieldDefs: FieldDefs): Codec { } } - const prefix = new Uint8Array(unsigned.encodingLength(bytes.length)) - unsigned.encode(bytes.length, prefix) + const prefix = unsigned.encode(bytes.length) return new Uint8ArrayList(prefix, bytes) } diff --git a/packages/protons-runtime/src/codecs/sint32.ts b/packages/protons-runtime/src/codecs/sint32.ts index 88bbb4a..469b9fe 100644 --- a/packages/protons-runtime/src/codecs/sint32.ts +++ b/packages/protons-runtime/src/codecs/sint32.ts @@ -1,4 +1,4 @@ -import { zigzag } from '../utils/varint.js' +import { zigzag } from 'uint8-varint' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' @@ -7,11 +7,7 @@ const encodingLength: EncodingLengthFunction = function sint32EncodingLe } const encode: EncodeFunction = function svarintEncode (val) { - const buf = new Uint8Array(encodingLength(val)) - - zigzag.encode(val, buf) - - return buf + return zigzag.encode(val) } const decode: DecodeFunction = function svarintDecode (buf, offset) { diff --git a/packages/protons-runtime/src/codecs/sint64.ts b/packages/protons-runtime/src/codecs/sint64.ts index ca56ce8..bd1d3f4 100644 --- a/packages/protons-runtime/src/codecs/sint64.ts +++ b/packages/protons-runtime/src/codecs/sint64.ts @@ -1,4 +1,4 @@ -import { zigzag } from '../utils/big-varint.js' +import { zigzag } from 'uint8-varint/big' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' @@ -7,10 +7,7 @@ const encodingLength: EncodingLengthFunction = function int64EncodingLen } const encode: EncodeFunction = function int64Encode (val) { - const buf = new Uint8Array(encodingLength(val)) - zigzag.encode(val, buf) - - return buf + return zigzag.encode(val) } const decode: DecodeFunction = function int64Decode (buf, offset) { diff --git a/packages/protons-runtime/src/codecs/string.ts b/packages/protons-runtime/src/codecs/string.ts index 04ebf11..58a317f 100644 --- a/packages/protons-runtime/src/codecs/string.ts +++ b/packages/protons-runtime/src/codecs/string.ts @@ -1,4 +1,4 @@ -import { unsigned } from '../utils/varint.js' +import { unsigned } from 'uint8-varint' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { createCodec, CODEC_TYPES } from '../codec.js' @@ -12,11 +12,11 @@ const encodingLength: EncodingLengthFunction = function stringEncodingLe const encode: EncodeFunction = function stringEncode (val) { const asBuf = uint8ArrayFromString(val) - const prefix = new Uint8Array(unsigned.encodingLength(asBuf.byteLength)) - unsigned.encode(asBuf.length, prefix) - - return new Uint8ArrayList(prefix, asBuf) + return new Uint8ArrayList( + unsigned.encode(asBuf.byteLength), + asBuf + ) } const decode: DecodeFunction = function stringDecode (buf, offset) { diff --git a/packages/protons-runtime/src/codecs/uint32.ts b/packages/protons-runtime/src/codecs/uint32.ts index 42d4153..9c894ca 100644 --- a/packages/protons-runtime/src/codecs/uint32.ts +++ b/packages/protons-runtime/src/codecs/uint32.ts @@ -1,4 +1,4 @@ -import { unsigned } from '../utils/varint.js' +import { unsigned } from 'uint8-varint' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' @@ -9,11 +9,7 @@ const encodingLength: EncodingLengthFunction = function uint32EncodingLe const encode: EncodeFunction = function uint32Encode (val) { // val = val < 0 ? val + 4294967296 : val - const buf = new Uint8Array(encodingLength(val)) - - unsigned.encode(val, buf) - - return buf + return unsigned.encode(val) } const decode: DecodeFunction = function uint32Decode (buf, offset) { diff --git a/packages/protons-runtime/src/codecs/uint64.ts b/packages/protons-runtime/src/codecs/uint64.ts index f8c2925..efad6ad 100644 --- a/packages/protons-runtime/src/codecs/uint64.ts +++ b/packages/protons-runtime/src/codecs/uint64.ts @@ -1,4 +1,4 @@ -import { unsigned } from '../utils/big-varint.js' +import { unsigned } from 'uint8-varint/big' import { createCodec, CODEC_TYPES } from '../codec.js' import type { DecodeFunction, EncodeFunction, EncodingLengthFunction } from '../codec.js' @@ -7,11 +7,7 @@ const encodingLength: EncodingLengthFunction = function uint64EncodingLe } const encode: EncodeFunction = function uint64Encode (val) { - const buf = new Uint8Array(unsigned.encodingLength(val)) - - unsigned.encode(val, buf) - - return buf + return unsigned.encode(val) } const decode: DecodeFunction = function uint64Decode (buf, offset) { diff --git a/packages/protons-runtime/src/decode.ts b/packages/protons-runtime/src/decode.ts index d9229e5..6216804 100644 --- a/packages/protons-runtime/src/decode.ts +++ b/packages/protons-runtime/src/decode.ts @@ -1,10 +1,11 @@ import { Uint8ArrayList } from 'uint8arraylist' -import { unsigned } from './utils/varint.js' +import { unsigned } from 'uint8-varint' import type { Codec } from './codec.js' +import { allocUnsafe } from './utils/alloc.js' export function decodeMessage (buf: Uint8Array | Uint8ArrayList, codec: Codec): T { // wrap root message - const prefix = new Uint8Array(unsigned.encodingLength(buf.byteLength)) + const prefix = allocUnsafe(unsigned.encodingLength(buf.byteLength)) unsigned.encode(buf.byteLength, prefix) return codec.decode(new Uint8ArrayList(prefix, buf), 0) diff --git a/packages/protons-runtime/src/encode.ts b/packages/protons-runtime/src/encode.ts index a92ba64..ff343e1 100644 --- a/packages/protons-runtime/src/encode.ts +++ b/packages/protons-runtime/src/encode.ts @@ -1,6 +1,6 @@ import { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from './codec.js' -import { unsigned } from './utils/varint.js' +import { unsigned } from 'uint8-varint' export function encodeMessage (message: T, codec: Codec): Uint8ArrayList { // unwrap root message diff --git a/packages/protons-runtime/src/utils/accessor.ts b/packages/protons-runtime/src/utils/accessor.ts deleted file mode 100644 index e19c387..0000000 --- a/packages/protons-runtime/src/utils/accessor.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { Uint8ArrayList } from 'uint8arraylist' - -export default function accessor (buf: Uint8Array | Uint8ArrayList) { - if (buf instanceof Uint8Array) { - return { - get (index: number) { - return buf[index] - }, - - set (index: number, value: number) { - buf[index] = value - } - } - } - - return { - get (index: number) { - return buf.get(index) - }, - - set (index: number, value: number) { - buf.set(index, value) - } - } -} diff --git a/packages/protons-runtime/src/utils/alloc.ts b/packages/protons-runtime/src/utils/alloc.ts new file mode 100644 index 0000000..e870fd8 --- /dev/null +++ b/packages/protons-runtime/src/utils/alloc.ts @@ -0,0 +1,12 @@ + +export function alloc (len: number) { + return new Uint8Array(len) +} + +export function allocUnsafe (len: number) { + if (globalThis?.Buffer?.allocUnsafe != null) { + return globalThis.Buffer.allocUnsafe(len) + } + + return new Uint8Array(len) +} diff --git a/packages/protons-runtime/src/utils/big-varint.ts b/packages/protons-runtime/src/utils/big-varint.ts deleted file mode 100644 index a899e1e..0000000 --- a/packages/protons-runtime/src/utils/big-varint.ts +++ /dev/null @@ -1,70 +0,0 @@ -import type { Uint8ArrayList } from 'uint8arraylist' -import accessor from './accessor.js' -import { LongBits } from './long-bits.js' - -const LIMIT = 0x7fn - -// https://github.com/joeltg/big-varint/blob/main/src/unsigned.ts -export const unsigned = { - encodingLength (value: bigint): number { - let i = 0 - for (; value >= 0x80n; i++) { - value >>= 7n - } - return i + 1 - }, - - encode (value: bigint, buf: Uint8ArrayList | Uint8Array) { - const access = accessor(buf) - - let offset = 0 - while (LIMIT < value) { - access.set(offset++, Number(value & LIMIT) | 0x80) - value >>= 7n - } - - access.set(offset, Number(value)) - }, - - decode (buf: Uint8ArrayList | Uint8Array, offset = 0) { - return LongBits.fromBytes(buf, offset).toBigInt(true) - } -} - -export const signed = { - encodingLength (value: bigint): number { - if (value < 0n) { - return 10 // 10 bytes per spec - } - - return unsigned.encodingLength(value) - }, - - encode (value: bigint, buf: Uint8ArrayList | Uint8Array, offset = 0) { - if (value < 0n) { - LongBits.fromBigInt(value).toBytes(buf, offset) - - return - } - - return unsigned.encode(value, buf) - }, - - decode (buf: Uint8ArrayList | Uint8Array, offset = 0) { - return LongBits.fromBytes(buf, offset).toBigInt(false) - } -} - -export const zigzag = { - encodingLength (value: bigint): number { - return unsigned.encodingLength(value >= 0 ? value * 2n : value * -2n - 1n) - }, - - encode (value: bigint, buf: Uint8ArrayList | Uint8Array, offset = 0) { - LongBits.fromBigInt(value).zzEncode().toBytes(buf, offset) - }, - - decode (buf: Uint8ArrayList | Uint8Array, offset = 0) { - return LongBits.fromBytes(buf, offset).zzDecode().toBigInt(false) - } -} diff --git a/packages/protons-runtime/src/utils/long-bits.ts b/packages/protons-runtime/src/utils/long-bits.ts deleted file mode 100644 index ad1ddcd..0000000 --- a/packages/protons-runtime/src/utils/long-bits.ts +++ /dev/null @@ -1,184 +0,0 @@ -import type { Uint8ArrayList } from 'uint8arraylist' -import accessor from './accessor.js' - -const TWO_32 = 4294967296 - -export class LongBits { - public hi: number - public lo: number - - constructor (hi: number = 0, lo: number = 0) { - this.hi = hi - this.lo = lo - } - - toBigInt (unsigned: boolean): bigint { - if (unsigned) { - return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n) - } - - if ((this.hi >>> 31) !== 0) { - const lo = ~this.lo + 1 >>> 0 - let hi = ~this.hi >>> 0 - - if (lo === 0) { - hi = hi + 1 >>> 0 - } - - return -(BigInt(lo) + (BigInt(hi) << 32n)) - } - - return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n) - } - - zzDecode () { - const mask = -(this.lo & 1) - const lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0 - const hi = (this.hi >>> 1 ^ mask) >>> 0 - - return new LongBits(hi, lo) - } - - zzEncode () { - const mask = this.hi >> 31 - const hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0 - const lo = (this.lo << 1 ^ mask) >>> 0 - - return new LongBits(hi, lo) - } - - toBytes (buf: Uint8ArrayList | Uint8Array, offset = 0) { - const access = accessor(buf) - - while (this.hi > 0) { - access.set(offset++, this.lo & 127 | 128) - this.lo = (this.lo >>> 7 | this.hi << 25) >>> 0 - this.hi >>>= 7 - } - - while (this.lo > 127) { - access.set(offset++, this.lo & 127 | 128) - this.lo = this.lo >>> 7 - } - - access.set(offset++, this.lo) - } - - static fromBigInt (value: bigint) { - if (value === 0n) { - return new LongBits() - } - - const negative = value < 0 - - if (negative) { - value = -value - } - - let hi = Number(value >> 32n) | 0 - let lo = Number(value - (BigInt(hi) << 32n)) | 0 - - if (negative) { - hi = ~hi >>> 0 - lo = ~lo >>> 0 - - if (++lo > TWO_32) { - lo = 0 - - if (++hi > TWO_32) { - hi = 0 - } - } - } - - return new LongBits(hi, lo) - } - - static fromNumber (value: number) { - if (value === 0) { - return new LongBits() - } - - const sign = value < 0 - - if (sign) { - value = -value - } - - let lo = value >>> 0 - let hi = (value - lo) / 4294967296 >>> 0 - - if (sign) { - hi = ~hi >>> 0 - lo = ~lo >>> 0 - - if (++lo > 4294967295) { - lo = 0 - - if (++hi > 4294967295) { - hi = 0 - } - } - } - - return new LongBits(hi, lo) - } - - static fromBytes (buf: Uint8ArrayList | Uint8Array, offset: number) { - const access = accessor(buf) - - // tends to deopt with local vars for octet etc. - const bits = new LongBits() - let i = 0 - - if (buf.length - offset > 4) { // fast route (lo) - for (; i < 4; ++i) { - // 1st..4th - bits.lo = (bits.lo | (access.get(offset) & 127) << i * 7) >>> 0 - if (access.get(offset++) < 128) { return bits } - } - // 5th - bits.lo = (bits.lo | (access.get(offset) & 127) << 28) >>> 0 - bits.hi = (bits.hi | (access.get(offset) & 127) >> 4) >>> 0 - if (access.get(offset++) < 128) { return bits } - i = 0 - } else { - for (; i < 3; ++i) { - /* istanbul ignore if */ - if (offset >= buf.length) { - throw RangeError(`index out of range: ${offset} > ${buf.length}`) - } - - // 1st..3th - bits.lo = (bits.lo | (access.get(offset) & 127) << i * 7) >>> 0 - if (access.get(offset++) < 128) { return bits } - } - // 4th - bits.lo = (bits.lo | (access.get(offset++) & 127) << i * 7) >>> 0 - return bits - } - if (buf.length - offset > 4) { // fast route (hi) - for (; i < 5; ++i) { - // 6th..10th - bits.hi = (bits.hi | (access.get(offset) & 127) << i * 7 + 3) >>> 0 - if (access.get(offset++) < 128) { return bits } - } - } else { - for (; i < 5; ++i) { - /* istanbul ignore if */ - if (offset >= buf.length) { - throw RangeError(`index out of range: ${offset} > ${buf.length}`) - } - - // 6th..10th - bits.hi = (bits.hi | (access.get(offset) & 127) << i * 7 + 3) >>> 0 - if (access.get(offset++) < 128) { - return bits - } - } - } - - /* istanbul ignore next */ - throw Error('invalid varint encoding') - } -} diff --git a/packages/protons-runtime/src/utils/varint.ts b/packages/protons-runtime/src/utils/varint.ts deleted file mode 100644 index f3b0738..0000000 --- a/packages/protons-runtime/src/utils/varint.ts +++ /dev/null @@ -1,173 +0,0 @@ -import type { Uint8ArrayList } from 'uint8arraylist' -import accessor from './accessor.js' -import { LongBits } from './long-bits.js' - -const MSB = 0x80 -const REST = 0x7F -const MSBALL = ~REST -const INT = Math.pow(2, 31) -const N1 = Math.pow(2, 7) -const N2 = Math.pow(2, 14) -const N3 = Math.pow(2, 21) -const N4 = Math.pow(2, 28) -const N5 = Math.pow(2, 35) -const N6 = Math.pow(2, 42) -const N7 = Math.pow(2, 49) -const N8 = Math.pow(2, 56) -const N9 = Math.pow(2, 63) - -export const unsigned = { - encodingLength (value: number): number { - if (value < N1) { - return 1 - } - - if (value < N2) { - return 2 - } - - if (value < N3) { - return 3 - } - - if (value < N4) { - return 4 - } - - if (value < N5) { - return 5 - } - - if (value < N6) { - return 6 - } - - if (value < N7) { - return 7 - } - - if (value < N8) { - return 8 - } - - if (value < N9) { - return 9 - } - - return 10 - }, - - encode (value: number, buf: Uint8ArrayList | Uint8Array) { - let offset = 0 - const access = accessor(buf) - - while (value >= INT) { - access.set(offset++, (value & 0xFF) | MSB) - value /= 128 - } - - while ((value & MSBALL) > 0) { - access.set(offset++, (value & 0xFF) | MSB) - value >>>= 7 - } - - access.set(offset, value | 0) - }, - - decode (buf: Uint8ArrayList | Uint8Array, offset: number = 0) { - const access = accessor(buf) - let value = 4294967295 // optimizer type-hint, tends to deopt otherwise (?!) - - value = (access.get(offset) & 127) >>> 0 - - if (access.get(offset++) < 128) { - return value - } - - value = (value | (access.get(offset) & 127) << 7) >>> 0 - - if (access.get(offset++) < 128) { - return value - } - - value = (value | (access.get(offset) & 127) << 14) >>> 0 - - if (access.get(offset++) < 128) { - return value - } - - value = (value | (access.get(offset) & 127) << 21) >>> 0 - - if (access.get(offset++) < 128) { - return value - } - - value = (value | (access.get(offset) & 15) << 28) >>> 0 - - if (access.get(offset++) < 128) { - return value - } - - if ((offset += 5) > buf.length) { - throw RangeError(`index out of range: ${offset} > ${buf.length}`) - } - - return value - } -} - -export const signed = { - encodingLength (value: number): number { - if (value < 0) { - return 10 // 10 bytes per spec - } - - return unsigned.encodingLength(value) - }, - - encode (value: number, buf: Uint8ArrayList | Uint8Array) { - if (value < 0) { - let offset = 0 - const access = accessor(buf) - const bits = LongBits.fromNumber(value) - - while (bits.hi > 0) { - access.set(offset++, bits.lo & 127 | 128) - bits.lo = (bits.lo >>> 7 | bits.hi << 25) >>> 0 - bits.hi >>>= 7 - } - - while (bits.lo > 127) { - access.set(offset++, bits.lo & 127 | 128) - bits.lo = bits.lo >>> 7 - } - - access.set(offset++, bits.lo) - - return - } - - unsigned.encode(value, buf) - }, - - decode (data: Uint8ArrayList | Uint8Array, offset = 0) { - return unsigned.decode(data, offset) | 0 - } -} - -export const zigzag = { - encodingLength (value: number): number { - value = (value << 1 ^ value >> 31) >>> 0 - return unsigned.encodingLength(value) - }, - - encode (value: number, buf: Uint8ArrayList | Uint8Array, offset = 0) { - value = (value << 1 ^ value >> 31) >>> 0 - return unsigned.encode(value, buf) - }, - - decode (data: Uint8ArrayList | Uint8Array, offset = 0) { - const value = unsigned.decode(data, offset) - return value >>> 1 ^ -(value & 1) | 0 - } -} diff --git a/packages/protons/package.json b/packages/protons/package.json index ea4a955..161be33 100644 --- a/packages/protons/package.json +++ b/packages/protons/package.json @@ -144,6 +144,7 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", "dep-check": "aegir dep-check", "build": "aegir build", diff --git a/packages/protons/test/fixtures/basic.proto b/packages/protons/test/fixtures/basic.proto index 7ada835..55882d4 100644 --- a/packages/protons/test/fixtures/basic.proto +++ b/packages/protons/test/fixtures/basic.proto @@ -1,3 +1,5 @@ +syntax = "proto3"; + message Basic { optional string foo = 1; required int32 num = 2; diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index 7491aed..10046fb 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -6,14 +6,14 @@ import type { Codec } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' export interface Basic { - foo: string + foo?: string num: number } export namespace Basic { export const codec = (): Codec => { return message({ - 1: { name: 'foo', codec: string }, + 1: { name: 'foo', codec: string, optional: true }, 2: { name: 'num', codec: int32 } }) }