-
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: port protobuf reader/writer to ts (#60)
Ports just the parts of protobufjs we use to typescript and integrates the changes from protobufjs/protobuf.js#1557 to have native BigInt support.
- Loading branch information
1 parent
d8c4e6b
commit d101804
Showing
12 changed files
with
1,173 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { reader } from './utils.js' | ||
import { createReader } from './utils/reader.js' | ||
import type { Codec } from './codec.js' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
|
||
export function decodeMessage <T> (buf: Uint8Array | Uint8ArrayList, codec: Codec<T>): T { | ||
const r = reader(buf instanceof Uint8Array ? buf : buf.subarray()) | ||
const reader = createReader(buf) | ||
|
||
return codec.decode(r) | ||
return codec.decode(reader) | ||
} |
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,109 @@ | ||
const f32 = new Float32Array([-0]) | ||
const f8b = new Uint8Array(f32.buffer) | ||
|
||
/** | ||
* Writes a 32 bit float to a buffer using little endian byte order | ||
*/ | ||
export function writeFloatLE (val: number, buf: Uint8Array, pos: number): void { | ||
f32[0] = val | ||
buf[pos] = f8b[0] | ||
buf[pos + 1] = f8b[1] | ||
buf[pos + 2] = f8b[2] | ||
buf[pos + 3] = f8b[3] | ||
} | ||
|
||
/** | ||
* Writes a 32 bit float to a buffer using big endian byte order | ||
*/ | ||
export function writeFloatBE (val: number, buf: Uint8Array, pos: number): void { | ||
f32[0] = val | ||
buf[pos] = f8b[3] | ||
buf[pos + 1] = f8b[2] | ||
buf[pos + 2] = f8b[1] | ||
buf[pos + 3] = f8b[0] | ||
} | ||
|
||
/** | ||
* Reads a 32 bit float from a buffer using little endian byte order | ||
*/ | ||
export function readFloatLE (buf: Uint8Array, pos: number): number { | ||
f8b[0] = buf[pos] | ||
f8b[1] = buf[pos + 1] | ||
f8b[2] = buf[pos + 2] | ||
f8b[3] = buf[pos + 3] | ||
return f32[0] | ||
} | ||
|
||
/** | ||
* Reads a 32 bit float from a buffer using big endian byte order | ||
*/ | ||
export function readFloatBE (buf: Uint8Array, pos: number): number { | ||
f8b[3] = buf[pos] | ||
f8b[2] = buf[pos + 1] | ||
f8b[1] = buf[pos + 2] | ||
f8b[0] = buf[pos + 3] | ||
return f32[0] | ||
} | ||
|
||
const f64 = new Float64Array([-0]) | ||
const d8b = new Uint8Array(f64.buffer) | ||
|
||
/** | ||
* Writes a 64 bit double to a buffer using little endian byte order | ||
*/ | ||
export function writeDoubleLE (val: number, buf: Uint8Array, pos: number): void { | ||
f64[0] = val | ||
buf[pos] = d8b[0] | ||
buf[pos + 1] = d8b[1] | ||
buf[pos + 2] = d8b[2] | ||
buf[pos + 3] = d8b[3] | ||
buf[pos + 4] = d8b[4] | ||
buf[pos + 5] = d8b[5] | ||
buf[pos + 6] = d8b[6] | ||
buf[pos + 7] = d8b[7] | ||
} | ||
|
||
/** | ||
* Writes a 64 bit double to a buffer using big endian byte order | ||
*/ | ||
export function writeDoubleBE (val: number, buf: Uint8Array, pos: number): void { | ||
f64[0] = val | ||
buf[pos] = d8b[7] | ||
buf[pos + 1] = d8b[6] | ||
buf[pos + 2] = d8b[5] | ||
buf[pos + 3] = d8b[4] | ||
buf[pos + 4] = d8b[3] | ||
buf[pos + 5] = d8b[2] | ||
buf[pos + 6] = d8b[1] | ||
buf[pos + 7] = d8b[0] | ||
} | ||
|
||
/** | ||
* Reads a 64 bit double from a buffer using little endian byte order | ||
*/ | ||
export function readDoubleLE (buf: Uint8Array, pos: number): number { | ||
d8b[0] = buf[pos] | ||
d8b[1] = buf[pos + 1] | ||
d8b[2] = buf[pos + 2] | ||
d8b[3] = buf[pos + 3] | ||
d8b[4] = buf[pos + 4] | ||
d8b[5] = buf[pos + 5] | ||
d8b[6] = buf[pos + 6] | ||
d8b[7] = buf[pos + 7] | ||
return f64[0] | ||
} | ||
|
||
/** | ||
* Reads a 64 bit double from a buffer using big endian byte order | ||
*/ | ||
export function readDoubleBE (buf: Uint8Array, pos: number): number { | ||
d8b[7] = buf[pos] | ||
d8b[6] = buf[pos + 1] | ||
d8b[5] = buf[pos + 2] | ||
d8b[4] = buf[pos + 3] | ||
d8b[3] = buf[pos + 4] | ||
d8b[2] = buf[pos + 5] | ||
d8b[1] = buf[pos + 6] | ||
d8b[0] = buf[pos + 7] | ||
return f64[0] | ||
} |
Oops, something went wrong.