Protocol Buffers Schema Parser
protocol-buffers-schema is a minimalist JavaScript library designed for parsing Protocol Buffers (.proto) schema files into a structured JavaScript object and stringifying such objects back into the .proto format. It explicitly focuses on schema definition parsing rather than binary serialization or deserialization of actual Protobuf messages. The current stable version is 3.6.1, though the last npm publication (for 3.6.0) was approximately four years ago, suggesting a maintenance mode with an infrequent release cadence, if any. Its primary differentiator is its straightforward API for schema manipulation, contrasting with more comprehensive Protobuf libraries that include code generation, runtime encoding/decoding, and gRPC support. This package serves as a foundational component for tools that need to inspect or modify Protobuf definitions programmatically.
Common errors
-
TypeError: schema.encode is not a function
cause Attempting to use `protocol-buffers-schema` for binary encoding/decoding, which it does not support.fixThis library only parses and stringifies Protobuf schema definitions. To encode or decode binary Protobuf messages, use a different library such as `protobuf.js` or `google-protobuf`. -
TypeError: Cannot read properties of undefined (reading 'parse')
cause Incorrectly importing the `parse` function as a named export instead of accessing it from the default module object.fixFor CommonJS, use `const schema = require('protocol-buffers-schema'); schema.parse(...)`. For ESM, use `import schema from 'protocol-buffers-schema'; schema.parse(...)`. -
SyntaxError: Unexpected token 'required' / 'optional'
cause Parsing a `proto3` file that uses `required` or `optional` keywords for field rules without explicitly setting `syntax = "proto2"`. While `protocol-buffers-schema` supports both, `proto3` has different default field behaviors and `required`/`optional` are not standard field rules (except with Editions).fixEnsure `syntax = "proto2";` is present in your `.proto` file if you intend to use explicit `required`/`optional` field rules. If using `proto3` without Editions, these keywords are not valid for field rules.
Warnings
- gotcha This package is a schema parser only. It does NOT provide functionality for encoding or decoding actual Protocol Buffers binary messages. For binary serialization/deserialization, consider libraries like `protobuf.js`, `google-protobuf`, or `protobuf-es`.
- gotcha The package (v3.6.1, last published ~4 years ago for 3.6.0) might not fully support all modern Protocol Buffers language features introduced in newer `proto3` specifications or `Protobuf Editions`. Ensure compatibility if working with very recent .proto syntax.
- breaking Older versions of `protocol-buffers-schema` may have handled certain Protobuf syntax nuances differently. While no specific major breaking changes for schema parsing are widely documented for this library, the broader Protobuf ecosystem has had breaking changes, especially concerning generated code and runtime compatibility.
Install
-
npm install protocol-buffers-schema -
yarn add protocol-buffers-schema -
pnpm add protocol-buffers-schema
Imports
- schema
const { parse } = require('protocol-buffers-schema')import schema from 'protocol-buffers-schema'
- parse
import { parse } from 'protocol-buffers-schema'import schema from 'protocol-buffers-schema'; schema.parse(protoString);
- stringify
require('protocol-buffers-schema').stringify(schemaObject)const schema = require('protocol-buffers-schema'); schema.stringify(schemaObject);
Quickstart
import { readFileSync } from 'fs';
import schema from 'protocol-buffers-schema';
// Create a dummy .proto file for demonstration
const protoContent = `
syntax = "proto2";
message Point {
required int32 x = 1;
required int32 y = 2;
optional string label = 3;
}
message Line {
required Point start = 1;
required Point end = 2;
optional string label = 3;
}
`;
// In a real scenario, this would be fs.writeFileSync('example.proto', protoContent);
// For this quickstart, we'll parse the string directly.
// Parse the .proto schema
const parsedSchema = schema.parse(protoContent);
// Log the parsed JavaScript object representation of the schema
console.log('Parsed Protobuf Schema:');
console.log(JSON.stringify(parsedSchema, null, 2));
// You can also stringify it back to .proto format
const stringifiedSchema = schema.stringify(parsedSchema);
console.log('\nStringified Protobuf Schema (reconstructed):');
console.log(stringifiedSchema);
// Example of accessing elements
console.log('\nFirst message name:', parsedSchema.messages[0].name);
console.log('First field of first message:', parsedSchema.messages[0].fields[0].name);