Comments (3)
This information should already be available. The objects output by proto-loader include message definition objects for each loaded message type, and those contain the full DescriptorProrot
information, which contains all of the FieldDescriptorProto
objects for each field, and those contain the FieldOptions
. See gRFC L43 for more information about those message definition objects.
from grpc-node.
Hmm, strange. When I tested I got a protobufjs error. That's why I supposed it's not supported yet.
foobar.proto
:
syntax = "proto3";
package dev.foobar;
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional string my_field_option = 50000;
}
message Person {
string first_name = 1 [(my_field_option) = 'blablubb'];
string last_name = 2;
}
message EchoObjectRequest {
Person person = 1;
}
message EchoObjectResponse {
Person person = 1;
}
// The foo service definition.
service FooService {
rpc EchoObject (EchoObjectRequest) returns (EchoObjectResponse);
}
server.js
:
'use strict'
const path = require('node:path')
const grpc = require('@grpc/grpc-js')
const protoLoader = require('@grpc/proto-loader')
const grpcPort = 10000
const protoPath = path.resolve(__dirname, 'foobar.proto')
const packageDefinition = protoLoader.loadSync(protoPath, {
keepCase : true,
defaults : false,
enums : String,
includeDirs: [
path.resolve(__dirname, './')
]
})
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition)
const server = new grpc.Server()
const methodImplementations = {
EchoObject (call, callback) {
console.log(`EchoObject called with the following parameters:`, call.request)
callback(null, {
person: call.request.person,
})
},
}
server.addService(protoDescriptor.dev.foobar.FooService.service, methodImplementations)
console.log(`Starting Foo service`)
server.bindAsync(
`0.0.0.0:${grpcPort}`,
grpc.ServerCredentials.createInsecure(),
(err) => {
if (err) {
throw new Error(`Could not start gRPC server on port ${grpcPort}`, err)
}
server.start()
console.log(`Foo service is listening on port ${grpcPort}`)
})
$ node server.js
/home/me/git/bitbucket/proto-custom-fields/node_modules/protobufjs/ext/descriptor/index.js:488
if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
^
TypeError: Cannot read properties of undefined (reading 'indexOf')
at Field.toDescriptor (/home/me/git/bitbucket/proto-custom-fields/node_modules/protobufjs/ext/descriptor/index.js:488:62)
at Root_toDescriptorRecursive (/home/me/git/bitbucket/proto-custom-fields/node_modules/protobufjs/ext/descriptor/index.js:142:40)
at Root_toDescriptorRecursive (/home/me/git/bitbucket/proto-custom-fields/node_modules/protobufjs/ext/descriptor/index.js:146:13)
at Root_toDescriptorRecursive (/home/me/git/bitbucket/proto-custom-fields/node_modules/protobufjs/ext/descriptor/index.js:146:13)
at Root.toDescriptor (/home/me/git/bitbucket/proto-custom-fields/node_modules/protobufjs/ext/descriptor/index.js:121:5)
at createPackageDefinition (/home/me/git/bitbucket/proto-custom-fields/node_modules/@grpc/proto-loader/build/src/index.js:176:33)
at Object.loadSync (/home/me/git/bitbucket/proto-custom-fields/node_modules/@grpc/proto-loader/build/src/index.js:223:12)
at Object.<anonymous> (/home/me/git/bitbucket/proto-custom-fields/server.js:12:39)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
Node.js v20.11.1
(using @grpc/grpc-js 1.10.9 and @grpc/proto-loader 0.7.13, indirect dependency protobufjs 7.3.0)
Update:
I don't get this error when compiling a descriptor set with --include_imports
option (protoc --proto_path=./ --descriptor_set_out=./descriptor_set.pb --include_imports foobar.proto
) and loading that:
const protoDescriptorSet = fs.readFileSync(path.resolve(__dirname, 'descriptor_set.pb'))
const packageDefinition = protoLoader.loadFileDescriptorSetFromBuffer(protoDescriptorSet, {
keepCase : true,
defaults : false,
enums : String,
includeDirs: [
path.resolve(__dirname, './')
]
})
However I don't find the value for custom field option my_field_option
anywhere in packageDefinition
. Would expect to see in on dev.foobar.Person.type.field[0].options
but it's null
:
{
name: "first_name",
extendee: "",
number: 1,
label: "LABEL_OPTIONAL",
type: "TYPE_STRING",
typeName: "",
defaultValue: "",
options: null,
oneofIndex: 0,
jsonName: "",
}
Might be related to protobufjs/protobuf.js#1072.
from grpc-node.
@grpc/proto-loader
just passes along the descriptors produced by Protobuf.js. It looks like Protobuf.js isn't successfully loading field option information into the descriptors, and that is a bug that needs to be fixed in Protobuf.js itself.
from grpc-node.
Related Issues (20)
- Discussion: Why HTTP2 pseudo headers such as :authory are removed from Metadata HOT 2
- Deadline examples/docs could use clarification HOT 8
- When response stream includes messages and then an error, final message(s) can be dropped HOT 1
- Servers base64-encode “grpc-status-details-bin” header with padding, but should be unpadded HOT 1
- Server does not check the HTTP method of the request HOT 2
- Compression handling has some issues
- Cardinality violations should use error code “unimplemented” HOT 3
- Servers send grpc-message trailer even when no error HOT 1
- successful calls still emit 'cancelled' as of v1.10.x HOT 2
- `@grpc/grpc-js@latest` is pointing to an old version HOT 1
- Implement authority overrides in the DNS resolver HOT 7
- return process.dlopen(module, path.toNamespacedPath(filename));/issues/233 HOT 1
- gRPC-JS client hangs when target server restarts HOT 1
- Infinite loop in LoadBalancingCall.doPick after closing the client HOT 2
- `grpc_tools_node_protoc` generated code does not build for `--moduleResolution=nodenext` HOT 3
- Performance degradation in high throughput applications due to DNS resolution bug HOT 5
- GRPC 2 UNKNOWN error.details always empty string HOT 3
- a HOT 1
- Cannot find module 'node:dns' HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grpc-node.