-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use uint8-varint, byte-accesor and longbits modules (#56)
To reduce code duplication, pull shared code out into separate modules
- Loading branch information
1 parent
a74ff6a
commit 66d72f5
Showing
22 changed files
with
64 additions
and
504 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.