How to add metadata to nodejs grpc call

前端 未结 3 1557
醉梦人生
醉梦人生 2020-12-17 15:06

I\'d like to know how to add metadata to a nodejs grpc function call. I can use channel credentials when making the client with

var client = new proto.Docume         


        
3条回答
  •  太阳男子
    2020-12-17 15:52

    I eventually worked it out through introspecting the grpc credentials code and modifying the implementation to expose an inner function. In the grpc module in the node_modules, file grpc/src/node/src/credentials.js add the line

    exports.CallCredentials = CallCredentials;
    

    after CallCredentials is imported. Then, in your code, you can write something like

    var meta = grpc.Metadata();
    meta.add('key', 'value');
    var extra_creds = grpc.credentials.CallCredentials.createFromPlugin(
      function (url, callback) {
        callback(null, meta);
      }
    )
    

    Then use extra_creds in the client builder

    var creds = grpc.credentials.combineChannelCredentials(
      grpc.credentials.createSsl(),
      extra_creds,
    )
    

    Now you can make your client

    var client = new proto.Document(
      'some.address:8000',
      creds,
    )
    

提交回复
热议问题