protobuffers
v1.0.0
Published
A lightweight and performant Protocol Buffers (protobuf) encoding and decoding library for JavaScript/TypeScript, providing tools to read, write, and work with protobuf messages efficiently.
Maintainers
Readme
Protobuf
A lightweight and performant Protocol Buffers (protobuf) encoding and decoding library for JavaScript/TypeScript, providing tools to read, write, and work with protobuf messages efficiently.
Features
- Encode and decode protobuf messages with full support for:
- Varint, fixed32, fixed64, length-delimited fields
- ZigZag encoding for signed integers (sint32/sint64)
- Nested messages and packed repeated fields
- Low-level wire format utilities for protobuf serialization/deserialization
ProtobufReaderfor parsing buffers into typed values with safe bounds checksProtobufWriterfor writing protobuf-encoded data to buffers- Declarative message class generation with
createMessageClassandMessagebase class - Registry to manage protobuf message descriptors and types
- Fully typed TypeScript support with detailed field and wire type definitions
Installation
npm install protobuffersor
yarn add protobuffersUsage
import { ProtobufReader, ProtobufWriter, createMessageClass, Message } from 'protobuffers';
// Writing a protobuf message
const writer = new ProtobufWriter();
writer.writeInt32(1, -1);
writer.writeString(2, "Hello, protobuf!");
const buffer = writer.finish();
// Reading a protobuf message
const reader = new ProtobufReader(buffer);
while (reader.hasMore()) {
const tag = reader.readTag();
if (!tag) break;
switch (tag.fieldNumber) {
case 1:
const intValue = reader.readInt32();
console.log('int32 field:', intValue);
break;
case 2:
const strValue = reader.readString();
console.log('string field:', strValue);
break;
default:
reader.skipField(tag.wireType);
}
}Contributing
Contributions and issues are welcome! Please open issues or pull requests.
License
MIT License
