What is pbf?
The pbf npm package is a low-level library for reading and writing Protocol Buffers (protobuf), a language-neutral, platform-neutral, extensible mechanism for serializing structured data. It is designed to be both efficient in performance and small in size, making it ideal for applications like map data processing in browsers or on servers.
What are pbf's main functionalities?
Reading Protocol Buffers
This code sample demonstrates how to read data from a Protocol Buffers file. It uses the Pbf instance to parse the buffer based on predefined tags and assigns the parsed data to a JavaScript object.
const Pbf = require('pbf');
const { readFileSync } = require('fs');
const buffer = readFileSync('data.pbf');
const pbf = new Pbf(buffer);
const data = pbf.readFields(readData, {});
function readData(tag, data, pbf) {
if (tag === 1) data.name = pbf.readString();
else if (tag === 2) data.version = pbf.readVarint();
}
Writing Protocol Buffers
This code sample shows how to write data to a Protocol Buffers file. It creates a new Pbf instance, writes data into it by specifying tags, and then writes the serialized data to a file.
const Pbf = require('pbf');
const { writeFileSync } = require('fs');
const data = { name: 'Example', version: 1 };
const pbf = new Pbf();
writeData(data, pbf);
pbf.finish();
writeFileSync('output.pbf', Buffer.from(pbf.buffer));
function writeData(obj, pbf) {
pbf.writeStringField(1, obj.name);
pbf.writeVarintField(2, obj.version);
}
Other packages similar to pbf
protobufjs
protobufjs is a more feature-rich library for working with Protocol Buffers in JavaScript. Unlike pbf, which focuses on minimalism and speed, protobufjs offers a comprehensive suite of tools including a CLI for generating static code and a more extensive API for manipulating protobuf schemas dynamically. This makes protobufjs better suited for applications requiring complex protobuf handling.
google-protobuf
google-protobuf is the official Protocol Buffers library for JavaScript, developed by Google. It provides robust support for the full range of protobuf features, including reflection and extensions, which are not supported by pbf. This package is ideal for users looking for official support and the most complete implementation of the Protocol Buffers specification.
pbf
A protocol buffers implementation in
JavaScript for node and browsers.
install
npm install pbf
api
Protobuf(buf, pos)
var Pbf = require('pbf');
var protobuffer = new Pbf(buffer);
var varInt = protobuffer.readVarInt();
Reading
- readUInt32
- readUInt32LE
- readUInt64
- readUInt64LE
- readDouble
- readDoubleLE
- readVarint
- readSVarint
- readString
- readPacked