Protocol Buffers for JavaScript with TypeScript support, providing pure JavaScript implementation for serializing and deserializing structured data using Google's Protocol Buffer format.
npx @tessl/cli install tessl/npm-protobufjs@7.5.0protobufjs is a pure JavaScript implementation of Protocol Buffers for JavaScript and TypeScript environments. It provides comprehensive functionality for parsing .proto files, runtime reflection, and efficient binary serialization/deserialization, supporting both Node.js and browser environments with zero native dependencies.
npm install protobufjs// Full build - includes parser and reflection
const protobuf = require("protobufjs");// Light build - reflection without parser
const protobuf = require("protobufjs/light");// Minimal build - core serialization only
const protobuf = require("protobufjs/minimal");ES Module imports:
import protobuf from "protobufjs";
import * as protobuf from "protobufjs";TypeScript:
import * as protobuf from "protobufjs";
import { Root, Type, Field, Message } from "protobufjs";const protobuf = require("protobufjs");
// Load a .proto file
protobuf.load("awesome.proto", function(err, root) {
if (err) throw err;
// Obtain a message type
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
// Create a message instance
const payload = { awesomeField: "AwesomeString" };
// Verify the payload if necessary
const errMsg = AwesomeMessage.verify(payload);
if (errMsg) throw Error(errMsg);
// Create and encode a message
const message = AwesomeMessage.create(payload);
const buffer = AwesomeMessage.encode(message).finish();
// Decode the message
const decoded = AwesomeMessage.decode(buffer);
console.log(decoded);
});protobufjs is built around several key architectural components:
| Feature | Minimal | Light | Full |
|---|---|---|---|
| Bundle Size | ~6KB | ~16KB | ~26KB |
| Writer/Reader | ✅ | ✅ | ✅ |
| Message Encoding/Decoding | ✅ | ✅ | ✅ |
| Root/Type/Field Classes | ❌ | ✅ | ✅ |
| Runtime Reflection | ❌ | ✅ | ✅ |
| Dynamic Loading (.proto) | ❌ | ✅ | ✅ |
| Parser (.proto to JSON) | ❌ | ❌ | ✅ |
| Tokenizer | ❌ | ❌ | ✅ |
| Common Google Types | ❌ | ❌ | ✅ |
| Use Case | Pre-compiled | Runtime Reflection | Full Development |
Choosing a Build:
Dynamic loading and parsing of .proto files with full reflection support. Enables runtime schema loading and type discovery.
function load(filename: string | string[], callback: LoadCallback): void;
function load(filename: string | string[], root: Root, callback: LoadCallback): void;
function loadSync(filename: string | string[], root?: Root): Root;
interface LoadCallback {
(error: Error | null, root?: Root): void;
}Complete runtime reflection API for working with protobuf types, services, and schemas. Provides programmatic access to all protobuf constructs.
class Root extends Namespace {
static fromJSON(json: object): Root;
load(filename: string | string[], callback: LoadCallback): Root;
loadSync(filename: string | string[]): Root;
}
class Type extends Namespace {
static fromJSON(name: string, json: object): Type;
create(properties?: object): Message;
encode(message: Message | object): Writer;
decode(reader: Reader | Uint8Array): Message;
verify(message: object): string | null;
}High-performance binary serialization and deserialization with full type safety and validation.
class Message {
static create(properties?: object): Message;
static encode(message: Message | object): Writer;
static encodeDelimited(message: Message | object): Writer;
static decode(reader: Reader | Uint8Array): Message;
static decodeDelimited(reader: Reader | Uint8Array): Message;
static verify(message: object): string | null;
static fromObject(object: object): Message;
static toObject(message: Message, options?: IConversionOptions): object;
}Low-level binary reading and writing operations for custom serialization needs and performance optimization.
class Writer {
static create(): Writer;
uint32(value: number): Writer;
int32(value: number): Writer;
string(value: string): Writer;
bytes(value: Uint8Array): Writer;
finish(): Uint8Array;
}
class Reader {
static create(buffer: Uint8Array): Reader;
uint32(): number;
int32(): number;
string(): string;
bytes(): Uint8Array;
}Static code generation and compilation utilities for creating optimized protobuf implementations.
function encoder(mtype: Type): Codegen;
function decoder(mtype: Type): Codegen;
function verifier(mtype: Type): Codegen;
namespace converter {
function fromObject(mtype: Type): Codegen;
function toObject(mtype: Type): Codegen;
}Built-in RPC service framework for implementing and consuming protobuf-based services.
class Service extends Namespace {
static fromJSON(name: string, json: object): Service;
create(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean): rpc.Service;
}
interface RPCImpl {
(method: Method, requestData: Uint8Array, callback: RPCImplCallback): void;
}Low-level tokenization of .proto source files for advanced parsing and analysis use cases. Available in full build only.
function tokenize(source: string, alternateCommentMode: boolean): ITokenizerHandle;
namespace tokenize {
function unescape(str: string): string;
}
interface ITokenizerHandle {
next(): string | null;
peek(): string | null;
push(token: string): void;
skip(expected?: string, optional?: boolean): boolean;
cmnt(trailingLine?: number): string | null;
line: number;
}Built-in support for Google's well-known types with automatic handling and type definitions. Available in full build only.
function common(name: string, json: { [k: string]: any }): void;
namespace common {
function get(file: string): object | null;
interface IAny {
typeUrl?: string;
bytes?: Uint8Array;
}
interface IDuration {
seconds?: (number | Long);
nanos?: number;
}
interface ITimestamp {
seconds?: (number | Long);
nanos?: number;
}
interface IEmpty {}
interface IStruct {
fields?: { [k: string]: IValue };
}
interface IValue {
kind?: string;
nullValue?: 0;
numberValue?: number;
stringValue?: string;
boolValue?: boolean;
structValue?: IStruct;
listValue?: IListValue;
}
interface IListValue {
values?: IValue[];
}
}Command-line utilities for static code generation and TypeScript definition generation.
# Generate static JavaScript code
pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
# Generate TypeScript definitions
pbts -o compiled.d.ts compiled.jsCommon utility functions for type checking, string manipulation, and low-level operations.
namespace util {
// Type checking utilities
function isInteger(value: any): boolean;
function isString(value: any): boolean;
function isObject(value: any): boolean;
function isSet(obj: object, prop: string): boolean;
// String manipulation
function lcFirst(str: string): string;
// Buffer operations
function newBuffer(sizeOrArray: number | number[]): Uint8Array;
// Long integer utilities
function longToHash(value: Long | number): string;
function longFromHash(hash: string, unsigned?: boolean): Long;
// OneOf field helpers
function oneOfGetter(fieldNames: string[]): () => string | undefined;
function oneOfSetter(fieldNames: string[]): (name?: string) => void;
// Low-level bit manipulation
class LongBits {
constructor(lo: number, hi: number);
lo: number;
hi: number;
static zero: LongBits;
static fromNumber(value: number): LongBits;
static from(value: Long | number | string): LongBits;
toNumber(unsigned?: boolean): number;
toLong(unsigned?: boolean): Long;
zzEncode(): LongBits;
zzDecode(): LongBits;
}
}interface IConversionOptions {
longs?: typeof String | typeof Number | typeof Long;
enums?: typeof String;
bytes?: typeof Array | typeof String;
defaults?: boolean;
arrays?: boolean;
objects?: boolean;
oneofs?: boolean;
}
interface Long {
low: number;
high: number;
unsigned: boolean;
toNumber(): number;
toString(): string;
}
interface LoadCallback {
(error: Error | null, root?: Root): void;
}
interface RPCImplCallback {
(error: Error | null, response?: Uint8Array): void;
}
interface ITokenizerHandle {
next(): string | null;
peek(): string | null;
push(token: string): void;
skip(expected?: string, optional?: boolean): boolean;
cmnt(trailingLine?: number): string | null;
line: number;
}
interface IFetchOptions {
binary?: boolean;
xhr?: boolean;
}
type FetchCallback = (error: Error, contents?: string) => void;
type PoolAllocator = (size: number) => Uint8Array;
type PoolSlicer = (start: number, end: number) => Uint8Array;