{"library":"protobufjs","title":"Protobuf.js","description":"Protobuf.js is a comprehensive JavaScript and TypeScript implementation of Protocol Buffers, providing tools for encoding and decoding structured data efficiently. It supports both dynamic (reflection-based) parsing of .proto schemas and static code generation for optimized performance and type safety. The library is actively maintained, with the current stable version being 8.0.1 as of early 2026, and frequent updates addressing bugs and security concerns, alongside less frequent major releases that introduce new features and breaking changes. Protobuf.js aims to be versatile, usable in both Node.js environments and browsers, making it a robust choice for projects requiring high-performance data serialization across different JavaScript runtimes.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install protobufjs"],"cli":null},"imports":["import { Root } from 'protobufjs';","import { load } from 'protobufjs';","import { Type, Field } from 'protobufjs';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Root, Type, Field } from 'protobufjs';\n\n// 1. Define a .proto schema programmatically\nconst root = new Root('my_schema_root');\nconst MyMessage = new Type('MyMessage')\n  .add(new Field('name', 1, 'string'))\n  .add(new Field('id', 2, 'int32'))\n  .add(new Field('isActive', 3, 'bool'));\n\nroot.add(MyMessage);\n\n// In a typical application, you'd load from a .proto file:\n// import { loadSync } from 'protobufjs';\n// import { join } from 'node:path';\n// const rootFromFile = loadSync(join(__dirname, 'my_service.proto'));\n// const MyMessageFromRoot = rootFromFile.lookupType('MyMessage');\n\n// 2. Prepare data to be encoded\nconst payload = {\n  name: 'Jane Doe',\n  id: 123,\n  isActive: true\n};\n\n// 3. Verify the payload against the schema (optional, but good for debugging)\nconst errMsg = MyMessage.verify(payload);\nif (errMsg) {\n  throw new Error(`Invalid payload: ${errMsg}`);\n}\n\n// 4. Create a message instance and encode it to a buffer\nconst message = MyMessage.create(payload);\nconst buffer = MyMessage.encode(message).finish();\n\nconsole.log('Encoded buffer (hex):', buffer.toString('hex'));\n\n// 5. Decode the buffer back into a message object\nconst decodedMessage = MyMessage.decode(buffer);\n\nconsole.log('Decoded message:', JSON.stringify(decodedMessage.toJSON()));\n\n// Example: decoding a message with missing fields (will use default values)\nconst partialBuffer = MyMessage.encode(MyMessage.create({ name: 'Partial Test' })).finish();\nconst decodedPartial = MyMessage.decode(partialBuffer);\nconsole.log('Decoded partial message (id and isActive default):', JSON.stringify(decodedPartial.toJSON()));","lang":"typescript","description":"This quickstart demonstrates how to programmatically define a simple Protobuf schema using `Root`, `Type`, and `Field`. It then shows the complete flow of creating a message payload, verifying it, encoding it into a binary buffer, and subsequently decoding the buffer back into a JavaScript object, illustrating core serialization capabilities.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}