What is @bufbuild/protobuf?
@bufbuild/protobuf is an npm package that provides tools for working with Protocol Buffers (protobufs) in JavaScript and TypeScript. It allows you to define, serialize, and deserialize structured data efficiently.
What are @bufbuild/protobuf's main functionalities?
Define Protobuf Messages
This feature allows you to define protobuf messages using JavaScript classes. The `@Field.d` decorator is used to specify the field number and type.
const { Message, Field } = require('@bufbuild/protobuf');
class Person extends Message {
@Field.d(1, 'string')
name = '';
@Field.d(2, 'int32')
age = 0;
}
const person = new Person({ name: 'John Doe', age: 30 });
console.log(person);
Serialize Protobuf Messages
This feature allows you to serialize a protobuf message into a binary format. The `encode` method is used to convert the message into a buffer.
const { Message, Field } = require('@bufbuild/protobuf');
class Person extends Message {
@Field.d(1, 'string')
name = '';
@Field.d(2, 'int32')
age = 0;
}
const person = new Person({ name: 'John Doe', age: 30 });
const buffer = Person.encode(person).finish();
console.log(buffer);
Deserialize Protobuf Messages
This feature allows you to deserialize a binary buffer back into a protobuf message. The `decode` method is used to convert the buffer back into a message object.
const { Message, Field } = require('@bufbuild/protobuf');
class Person extends Message {
@Field.d(1, 'string')
name = '';
@Field.d(2, 'int32')
age = 0;
}
const buffer = new Uint8Array([10, 8, 74, 111, 104, 110, 32, 68, 111, 101, 16, 30]);
const person = Person.decode(buffer);
console.log(person);
Other packages similar to @bufbuild/protobuf
protobufjs
protobufjs is a popular library for working with Protocol Buffers in JavaScript. It provides similar functionalities to @bufbuild/protobuf, such as defining, serializing, and deserializing protobuf messages. However, protobufjs has been around longer and has a larger user base.
google-protobuf
google-protobuf is the official Protocol Buffers library for JavaScript provided by Google. It offers core functionalities for working with protobufs, including message definition, serialization, and deserialization. Compared to @bufbuild/protobuf, google-protobuf is more closely aligned with Google's protobuf ecosystem.
@bufbuild/protobuf
A complete implementation of protocol buffers in TypeScript,
suitable for web browsers and Node.js.
Features
- small code size
- no dependencies
- implements all proto3 features, including the canonical JSON format
- implements all proto2 features, except for extensions and the text format
- passes the protocol buffers conformance tests
- provides all well-known types with their specialized JSON representation
- uses and algebraic data type to represent
oneof
groups - unboxes fields using google/protobuf/wrappers.proto to optional primitives
- represents 64-bit integers with BigInt, and falls back to
string
if unavailable - uses standard TypeScript enums for protocol buffer
enum
- provides
equals()
and clone()
on each message for convenience - fields are plain properties, and support the object spread operator
- messages can be constructed from partial plain objects
- can dynamically create types at run time, for example from a set of
google.protobuf.FileDescriptorProto
- provides field information to traverse types programmatically
Copyright
The code to encode and decode varint is Copyright 2008 Google Inc., licensed under BSD-3-Clause.