Coder Social home page Coder Social logo

grpc-loader: Expose field options about grpc-node HOT 3 OPEN

n0v1 avatar n0v1 commented on July 17, 2024
grpc-loader: Expose field options

from grpc-node.

Comments (3)

murgatroid99 avatar murgatroid99 commented on July 17, 2024

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.

n0v1 avatar n0v1 commented on July 17, 2024

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.

murgatroid99 avatar murgatroid99 commented on July 17, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.