I checked there is recent release on server interceptors , can you please add documentation how this can be configured
kirankumarkn opened this issue · 9 comments
Problem description
A clear and concise description of what the problem is.
Reproduction steps
Give very precise steps you've discovered to reproduce your problem. If possible and applicable, provide us with a repository we can clone that contains a reproduction case. Also if possible and applicable, please include a Dockerfile that exhibits the problem if it's specific to a certain environment. Bug reports with no reproduction steps will be closed.
Environment
- OS name, version and architecture: [e.g. Linux Ubuntu 18.04 amd64]
- Node version [e.g. 8.10.0]
- Node installation method [e.g. nvm]
- If applicable, compiler version [e.g. clang 3.8.0-2ubuntu4]
- Package name and version [e.g. gRPC@1.12.0]
Additional context
Add any other context about the problem here. If possible, attach full logs. Do not try to omit anything for brevity, but instead include absolutely everything. Please try and set your operating system's locale to English, so that logs contain error messages in the English language.
As stated in the release notes, detailed information about that feature can be found in gRFC L112. All of the information you need can be found there. If you have specific questions feel free to follow up.
Is there any document, I can referrer for configuring in Js , not typescript, please let me know Thanks
Note: interceptors will only be called when a request to a registered method is made.
This note says we have to register a request , so does this support JavaScript. If so Can you please add an example for configuration
The API for JavaScript is the same as for TypeScript, except that you don't use any types.
This example demonstrates registering a service, specifically on this line
I have followed adding a server interceptor in the below code. But when you call service from client , console log in server interceptor it is not logged. Please let me know how to configure listener , where is start method in responder to configure
// Server code
const listener = (new grpc.ServerListenerBuilder())
.withOnReceiveMetadata(function (metadata, next) {
logger.log(metadata);
next(metadata);
})
.withOnReceiveMessage(function (message, next) {
logger.log(message);
next(message);
})
.build();
const serverInterceptor = {
start :(next) => {
next(listener)
}
}
const requester = (new grpc.ResponderBuilder())
.withSendMetadata(function (metadata, next) {
logger.log(metadata);
next(metadata);
})
.withSendMessage(function (message, next) {
logger.log(message);
next(message);
})
.build();
const interceptors = function interceptor(methodDescriptor, call) {
return new grpc.ServerInterceptingCall(serverInterceptor, requester);
}
const server = new grpc.Server([interceptors])
server.addService(linksProto.LinkService.service, {
ListLinks: listLinks
})
server.bindAsync(
"localhost:30043",
grpc.ServerCredentials.createInsecure(), () => {
server.start()
console.log(`Server running at ${serverAddress}`)
})
// Client code
function main() {
let metadata = new grpc.Metadata();
metadata.add('authorization', 'test Data')
let client = new LinkService("localhost:30043", grpc.credentials.createInsecure());
client.ListLinks({"parent":"partners/partner-1"},metadata,(err,data) => {
if(!err){
console.log('data', JSON.stringify(data));
}
else{
console.log('error', err);
}
});
}
I think need a document on how to configure serverListener. Please check above server code and client code, when I call service interceptors are not called. Please let me know if any suggestions
Your server constructor call should be
const server = new grpc.Server({interceptors: [interceptors]});
Your server constructor call should be
const server = new grpc.Server({interceptors: [interceptors]});
Thank you for pointing out the issue, I am able to configure interceptors and log metadata and message in listener.
I have one query regarding handling metadata, usually metadata is handled for validating tokens, if the token is invalid. how do you handle , I don't see the call back function to throw an error. if I throw an error inside as below code. server is getting aborted here. please let me know how to handle the scenarios to stop further and throw back error to client
ex : withOnReceiveMetadata(function (metadata, next) {
console.log('meta data Received', metadata);
if(!token){
throw new Error('Invalid token authentication failed')
}
next(metadata);
})
To do that, you would need to interact with the call passed in to the interceptor function, and call call.sendStatus
. Here is an example in the tests.
Thank you @murgatroid99.