A low-level, lightweight protocol buffers implementation for JavaScript with high-performance binary serialization and deserialization capabilities.
npx @tessl/cli install tessl/npm-pbf@4.0.0A low-level, lightweight protocol buffers implementation for JavaScript with high-performance binary serialization and deserialization capabilities. PBF provides ultra-fast encoding and decoding of protocol buffer messages, supporting both Node.js and browser environments with lazy decoding and custom reading/writing functions.
npm install pbfESM (ES6 modules):
import Pbf from "pbf";
import { compile, compileRaw } from "pbf/compile";CommonJS:
const Pbf = require("pbf");
const { compile, compileRaw } = require("pbf/compile");import Pbf from "pbf";
// Read from buffer
const pbf = new Pbf(buffer);
const data = pbf.readFields(readDataField, {});
function readDataField(tag, data, pbf) {
if (tag === 1) data.name = pbf.readString();
else if (tag === 2) data.version = pbf.readVarint();
}import Pbf from "pbf";
// Write to buffer
const pbf = new Pbf();
writeData(data, pbf);
const buffer = pbf.finish();
function writeData(data, pbf) {
pbf.writeStringField(1, data.name);
pbf.writeVarintField(2, data.version);
}PBF provides three main interfaces:
Core protocol buffer reading and writing functionality with support for all protocol buffer data types and packed fields.
class Pbf {
constructor(buf?: Uint8Array | ArrayBuffer);
// Buffer state and data
buf: Uint8Array;
dataView: DataView;
pos: number;
type: number;
length: number;
// Field-level reading
readFields<T>(readField: (tag: number, result: T, pbf: Pbf) => void, result: T, end?: number): T;
readMessage<T>(readField: (tag: number, result: T, pbf: Pbf) => void, result: T): T;
// Basic data type reading
readVarint(isSigned?: boolean): number;
readString(): string;
readBytes(): Uint8Array;
readBoolean(): boolean;
// Buffer management
realloc(min: number): void;
finish(): Uint8Array;
// Basic data type writing
writeVarint(val: number): void;
writeString(str: string): void;
writeBytes(buffer: Uint8Array): void;
writeBoolean(val: boolean): void;
}Compile protocol buffer schema files (.proto) into optimized JavaScript read/write functions.
interface CompileOptions {
dev?: boolean;
legacy?: boolean;
noRead?: boolean;
noWrite?: boolean;
}
// Compile schema to executable functions
function compile(proto: object): Record<string, Function>;
// Compile schema to raw JavaScript code
function compileRaw(proto: object, options?: CompileOptions): string;Command-line interface for compiling .proto files to JavaScript modules.
# Basic compilation
pbf example.proto > example.js
# Options
pbf example.proto --no-read --legacy// Field reading callback function
interface FieldReader<T> {
(tag: number, result: T, pbf: Pbf): void;
}
// Compilation options
interface CompileOptions {
dev?: boolean; // Include development mode features
legacy?: boolean; // Generate CommonJS instead of ESM
noRead?: boolean; // Skip generating read functions
noWrite?: boolean; // Skip generating write functions
}
// Protocol buffer field types
type PBFType =
| 'string'
| 'float' | 'double'
| 'bool'
| 'uint32' | 'uint64'
| 'int32' | 'int64'
| 'sint32' | 'sint64'
| 'fixed32' | 'fixed64'
| 'sfixed32' | 'sfixed64'
| 'bytes';
// Wire format constants
declare const PBF_VARINT: 0;
declare const PBF_FIXED64: 1;
declare const PBF_BYTES: 2;
declare const PBF_FIXED32: 5;