PBF is a low-level, fast, and ultra-lightweight JavaScript library (3KB gzipped) for efficiently decoding and encoding Protocol Buffers (protobuf), a compact binary format for structured data serialization. Version 4.0.1 is the current stable release, which includes minor fixes building upon the significant 4.0.0 overhaul. The library is actively maintained, with a recent major release focusing on modernizing the codebase and developer experience. A key differentiator of PBF is its exceptional performance, consistently outperforming `JSON.parse`/`JSON.stringify` and other protobuf implementations like `protocol-buffers` in benchmarks. It supports lazy decoding and offers extensive customization options for reading and writing data, making it suitable for both Node.js and browser environments where performance and bundle size are critical considerations.
npm install pbfVerified import paths — ran on the pinned version, not inferred.
Demonstrates manual encoding and decoding of a custom data structure using PBF's low-level API without a .proto schema, showcasing its `writeField`, `readField`, and `readMessage` methods.
Refactor your codebase to use ES module `import` and `export` syntax. If targeting environments that do not support ESM natively, use a transpiler like Babel or webpack.
Ensure your target runtime environment supports modern ES syntax or integrate a transpilation step (e.g., Babel) into your build process to downlevel the code.
Update your build scripts to handle ES module output (e.g., change `require` to `import` in generated files, or use bundlers). If CommonJS output is strictly required for generated code, use the `--legacy` flag when running the `pbf` CLI tool: `pbf --legacy example.proto > example.js`.
Adjust your import statements and code logic to use the new flat export structure for generated read and write functions. For example, change `read.MyMessage(pbf)` to `readMyMessage(pbf)`.
Update to PBF v4.0.1 or higher to get the corrected typings, which mark the `buf` parameter as optional.
Change `const Pbf = require('pbf');` to `import Pbf from 'pbf';`. For generated code, change `const { readMyMessage } = require('./my-schema');` to `import { readMyMessage } from './my-schema.js';`.Ensure your project is configured for ES modules and use `import Pbf from 'pbf';`. If using TypeScript, check `tsconfig.json` for `"esModuleInterop": true` and `"module": "NodeNext"` or `"ES2020"`.
Review your import statements for generated code. Instead of `import { read } from './my-schema.js'; read.MyMessage(pbf);`, use `import { readMyMessage } from './my-schema.js'; readMyMessage(pbf);`.No dependency data recorded yet.