{"library":"pbf","title":"PBF: Low-Level Protocol Buffers for JavaScript","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pbf"],"cli":null},"imports":["import Pbf from 'pbf';","import { readMyMessage, writeMyMessage } from './my-schema.js';","import { compile } from 'pbf/compile';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Pbf from 'pbf';\n\ninterface MyComplexData {\n  name: string;\n  version: number;\n  layer: {\n    name: string;\n    size: number;\n  };\n}\n\nfunction writeLayer(layer: MyComplexData['layer'], pbf: Pbf) {\n  pbf.writeStringField(1, layer.name);\n  pbf.writeVarintField(3, layer.size);\n}\n\nfunction writeData(data: MyComplexData, pbf: Pbf) {\n  pbf.writeStringField(1, data.name);\n  pbf.writeVarintField(2, data.version);\n  pbf.writeMessage(3, writeLayer, data.layer);\n}\n\nfunction readLayer(tag: number, layer: MyComplexData['layer'], pbf: Pbf) {\n  if (tag === 1) layer.name = pbf.readString();\n  else if (tag === 3) layer.size = pbf.readVarint();\n}\n\nfunction readData(tag: number, data: MyComplexData, pbf: Pbf) {\n  if (tag === 1) data.name = pbf.readString();\n  else if (tag === 2) data.version = pbf.readVarint();\n  else if (tag === 3) data.layer = pbf.readMessage(readLayer, {} as MyComplexData['layer']);\n}\n\nconst myData: MyComplexData = {\n  name: 'TelemetryPacket',\n  version: 42,\n  layer: {\n    name: 'SensorReadings',\n    size: 1024\n  }\n};\n\nconst pbfWriter = new Pbf();\nwriteData(myData, pbfWriter);\nconst encodedBuffer: Uint8Array = pbfWriter.finish();\n\nconsole.log('Encoded buffer (Uint8Array):', encodedBuffer);\nconsole.log('Encoded buffer length:', encodedBuffer.length, 'bytes');\n\nconst pbfReader = new Pbf(encodedBuffer);\nconst decodedData: MyComplexData = pbfReader.readFields(readData, {} as MyComplexData);\n\nconsole.log('Decoded data:', decodedData);\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}