Protocol Buffers Schema Parser

3.6.1 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates parsing a Protobuf schema string into a JavaScript object and then stringifying it back, also showing how to access parsed schema elements.

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);

view raw JSON →