or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

buffer-operations.mdcli-tool.mdindex.mdschema-compilation.md
tile.json

tessl/npm-pbf

A low-level, lightweight protocol buffers implementation for JavaScript with high-performance binary serialization and deserialization capabilities.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/pbf@4.0.x

To install, run

npx @tessl/cli install tessl/npm-pbf@4.0.0

index.mddocs/

PBF

A 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.

Package Information

  • Package Name: pbf
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install pbf
  • Size: 3KB gzipped
  • Environments: Node.js, Browser

Core Imports

ESM (ES6 modules):

import Pbf from "pbf";
import { compile, compileRaw } from "pbf/compile";

CommonJS:

const Pbf = require("pbf");
const { compile, compileRaw } = require("pbf/compile");

Basic Usage

Reading Protocol Buffer Data

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();
}

Writing Protocol Buffer Data

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);
}

Architecture

PBF provides three main interfaces:

  1. Low-level API - Direct buffer manipulation for custom protocols
  2. Schema Compilation - Generate optimized read/write functions from .proto files
  3. CLI Tool - Command-line proto-to-JavaScript compilation

Capabilities

Buffer Reading and Writing

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;
}

Buffer Reading and Writing

Schema Compilation

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;

Schema Compilation

CLI Tool

Command-line interface for compiling .proto files to JavaScript modules.

# Basic compilation
pbf example.proto > example.js

# Options
pbf example.proto --no-read --legacy

CLI Tool

Types

// 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;