An open API service indexing awesome lists of open source software.

https://github.com/pwmkin/protobuffers

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.
https://github.com/pwmkin/protobuffers

javascript library protobuf protobufjs protocol-buffers serialization typescript

Last synced: 2 months ago
JSON representation

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.

Awesome Lists containing this project

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
* `ProtobufReader` for parsing buffers into typed values with safe bounds checks
* `ProtobufWriter` for writing protobuf-encoded data to buffers
* Declarative message class generation with `createMessageClass` and `Message` base class
* Registry to manage protobuf message descriptors and types
* Fully typed TypeScript support with detailed field and wire type definitions

## Installation

```bash
npm install protobuffers
```

or

```bash
yarn add protobuffers
```

## Usage

```ts
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