What is resolve-protobuf-schema?
The resolve-protobuf-schema npm package is used to parse and resolve Protocol Buffers (protobuf) schema definitions. It helps in converting .proto files into JavaScript objects that can be used for further processing or validation.
What are resolve-protobuf-schema's main functionalities?
Parsing .proto files
This feature allows you to read and parse a .proto file into a JavaScript object. The parsed schema can then be used for various purposes such as validation or code generation.
const fs = require('fs');
const resolve = require('resolve-protobuf-schema');
const schema = resolve(fs.readFileSync('path/to/your.proto', 'utf-8'));
console.log(schema);
Resolving imports in .proto files
This feature allows you to resolve imports within .proto files. If your .proto file imports other .proto files, you can provide a custom resolveImport function to handle these imports.
const fs = require('fs');
const resolve = require('resolve-protobuf-schema');
const schema = resolve(fs.readFileSync('path/to/your.proto', 'utf-8'), {
resolveImport: (importPath) => fs.readFileSync(importPath, 'utf-8')
});
console.log(schema);
Other packages similar to resolve-protobuf-schema
protobufjs
protobufjs is a comprehensive library for working with Protocol Buffers in JavaScript. It provides functionality for parsing .proto files, generating static code, and runtime message encoding/decoding. Compared to resolve-protobuf-schema, protobufjs offers a broader range of features including message serialization and deserialization.
proto-loader
proto-loader is a package that works with grpc and allows you to load .proto files into JavaScript objects. It supports resolving imports and is often used in conjunction with grpc libraries. While it offers similar schema resolution capabilities, it is more focused on gRPC service definitions and usage.
resolve-protobuf-schema
Read a protobuf schema from the disk, parse it and resolve all imports
npm install resolve-protobuf-schema
Usage
Store the following example protobuf schema in test.proto
message Test {
optional string test = 1;
}
Then run
var resolve = require('resolve-protobuf-schema')
console.log(resolve.sync('test.proto'))
Schema imports will resolved as well
import "./test.proto"
message AnotherTest {
optional string test = 1;
}
console.log(resolve.sync('./another-test.proto'))
API
resolve(path, cb)
read and resolve a schemaresolve.sync()
sync version of resolve
License
MIT