2.0.41 • Published 10 days ago
@3-/proto v2.0.41
protobuf-codec
Minimal Protocol Buffers wire encoding/decoding
Example
Writer
import Writer from 'protobuf-codec/encoding/writer'
import { uint64, string } from 'protobuf-codec/encoding/types'
const w = new Writer()
w.varint(1, 1024n, uint64) // myInt
w.varint(2, 'Hello world', string) // myString
const buf = w.concat()
Reader
import Reader from 'protobuf-codec/decoding/reader'
import { uint64, string } from 'protobuf-codec/decoding/types'
const buf = new Uint8Array([ /* ... */ ])
const myInt = 0n
const myString = ''
for (const [fieldNumber, { data }] of reader(buf)) {
switch (fieldNumber) {
case 1:
myInt = uint64(data); break
case 2:
myString = string(data); break
}
}
API
Encoding
const writer = new Writer(prealloc = 256)
writer.varint(fieldNumber, value, [codec])
writer.bytes(fieldNumber, value, [codec])
writer.fixed64(fieldNumber, value, [codec])
writer.fixed32(fieldNumber, value, [codec])
const buf = writer.concat([buf], [byteOffset])
Decoding
const iter = reader(buf, [byteOffset], [byteLength])
const msg = new Uint8Array([ /* ... */ ])
for (const [fieldNumber, field] of reader(msg)) {
const {
tagOffset,
tagLen,
// The tag is essentially `fieldNumber << 3 | wireType`
fieldNumber,
wireType,
// Total length of the data, eg. including a length prefix or extra bytes for varints
len,
// The actually value of the field decoded based on the wireType:
//
// - varint: bigint
// - fixed32: Uint8Array subarray
// - fixed64: Uint8Array subarray
// - bytes: Uint8Array subarray
//
// The fixed size integers are represented as Uint8Arrays since there's no easy way
// to cast a integer to a double/float without using DataView's, which in turn expect
// a TypedArray
data
} = field
// ...
}
Install
npm install protobuf-codec
License
2.0.41
10 days ago
2.0.37
1 month ago
2.0.38
1 month ago
2.0.39
1 month ago
2.0.40
1 month ago
2.0.35
4 months ago
2.0.36
4 months ago
2.0.33
4 months ago
2.0.34
4 months ago
2.0.28
5 months ago
2.0.29
5 months ago
2.0.31
5 months ago
2.0.32
5 months ago
2.0.30
5 months ago
2.0.26
5 months ago
2.0.27
5 months ago
2.0.24
6 months ago
2.0.25
6 months ago
2.0.22
6 months ago
2.0.23
6 months ago
2.0.20
6 months ago
2.0.21
6 months ago
2.0.19
6 months ago
2.0.18
6 months ago
2.0.17
6 months ago
2.0.16
6 months ago
2.0.14
6 months ago
2.0.13
6 months ago
2.0.12
6 months ago
2.0.11
6 months ago
2.0.10
6 months ago
2.0.9
6 months ago
2.0.8
6 months ago
2.0.7
6 months ago
2.0.6
6 months ago
2.0.5
6 months ago