Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use uint8-varint, byte-accesor and longbits modules #56

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions packages/protons-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 5 additions & 6 deletions packages/protons-runtime/src/codecs/bytes.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -10,11 +10,10 @@ const encodingLength: EncodingLengthFunction<Uint8Array> = function bytesEncodin
}

const encode: EncodeFunction<Uint8Array> = 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<Uint8Array> = function bytesDecode (buf, offset) {
Expand Down
5 changes: 3 additions & 2 deletions packages/protons-runtime/src/codecs/enum.ts
Original file line number Diff line number Diff line change
@@ -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 <T> (v: any): Codec<T> {
function findValue (val: string | number): number {
Expand All @@ -23,7 +24,7 @@ export function enumeration <T> (v: any): Codec<T> {
const encode: EncodeFunction<number | string> = 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
Expand Down
11 changes: 7 additions & 4 deletions packages/protons-runtime/src/codecs/int32.ts
Original file line number Diff line number Diff line change
@@ -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<number> = 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<number> = function int32Encode (val) {
const buf = new Uint8Array(encodingLength(val))
signed.encode(val, buf)

return buf
return signed.encode(val, buf)
}

const decode: DecodeFunction<number> = 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)
9 changes: 6 additions & 3 deletions packages/protons-runtime/src/codecs/int64.ts
Original file line number Diff line number Diff line change
@@ -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<bigint> = 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<bigint> = function int64Encode (val) {
const buf = new Uint8Array(encodingLength(val))
signed.encode(val, buf)

return buf
return signed.encode(val, buf)
}

const decode: DecodeFunction<bigint> = function int64Decode (buf, offset) {
Expand Down
8 changes: 4 additions & 4 deletions packages/protons-runtime/src/codecs/message.ts
Original file line number Diff line number Diff line change
@@ -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<A, T> {
new (obj: A): T
Expand Down Expand Up @@ -32,7 +33,7 @@ export function message <T> (fieldDefs: FieldDefs): Codec<T> {
}

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)

Expand All @@ -56,8 +57,7 @@ export function message <T> (fieldDefs: FieldDefs): Codec<T> {
}
}

const prefix = new Uint8Array(unsigned.encodingLength(bytes.length))
unsigned.encode(bytes.length, prefix)
const prefix = unsigned.encode(bytes.length)

return new Uint8ArrayList(prefix, bytes)
}
Expand Down
8 changes: 2 additions & 6 deletions packages/protons-runtime/src/codecs/sint32.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -7,11 +7,7 @@ const encodingLength: EncodingLengthFunction<number> = function sint32EncodingLe
}

const encode: EncodeFunction<number> = function svarintEncode (val) {
const buf = new Uint8Array(encodingLength(val))

zigzag.encode(val, buf)

return buf
return zigzag.encode(val)
}

const decode: DecodeFunction<number> = function svarintDecode (buf, offset) {
Expand Down
7 changes: 2 additions & 5 deletions packages/protons-runtime/src/codecs/sint64.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -7,10 +7,7 @@ const encodingLength: EncodingLengthFunction<bigint> = function int64EncodingLen
}

const encode: EncodeFunction<bigint> = function int64Encode (val) {
const buf = new Uint8Array(encodingLength(val))
zigzag.encode(val, buf)

return buf
return zigzag.encode(val)
}

const decode: DecodeFunction<bigint> = function int64Decode (buf, offset) {
Expand Down
10 changes: 5 additions & 5 deletions packages/protons-runtime/src/codecs/string.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -12,11 +12,11 @@ const encodingLength: EncodingLengthFunction<string> = function stringEncodingLe

const encode: EncodeFunction<string> = 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<string> = function stringDecode (buf, offset) {
Expand Down
8 changes: 2 additions & 6 deletions packages/protons-runtime/src/codecs/uint32.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -9,11 +9,7 @@ const encodingLength: EncodingLengthFunction<number> = function uint32EncodingLe
const encode: EncodeFunction<number> = 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<number> = function uint32Decode (buf, offset) {
Expand Down
8 changes: 2 additions & 6 deletions packages/protons-runtime/src/codecs/uint64.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -7,11 +7,7 @@ const encodingLength: EncodingLengthFunction<bigint> = function uint64EncodingLe
}

const encode: EncodeFunction<bigint> = function uint64Encode (val) {
const buf = new Uint8Array(unsigned.encodingLength(val))

unsigned.encode(val, buf)

return buf
return unsigned.encode(val)
}

const decode: DecodeFunction<bigint> = function uint64Decode (buf, offset) {
Expand Down
5 changes: 3 additions & 2 deletions packages/protons-runtime/src/decode.ts
Original file line number Diff line number Diff line change
@@ -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 <T> (buf: Uint8Array | Uint8ArrayList, codec: Codec<T>): 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)
Expand Down
2 changes: 1 addition & 1 deletion packages/protons-runtime/src/encode.ts
Original file line number Diff line number Diff line change
@@ -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 <T> (message: T, codec: Codec<T>): Uint8ArrayList {
// unwrap root message
Expand Down
25 changes: 0 additions & 25 deletions packages/protons-runtime/src/utils/accessor.ts

This file was deleted.

12 changes: 12 additions & 0 deletions packages/protons-runtime/src/utils/alloc.ts
Original file line number Diff line number Diff line change
@@ -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)
}
70 changes: 0 additions & 70 deletions packages/protons-runtime/src/utils/big-varint.ts

This file was deleted.

Loading