Skip to content

Commit

Permalink
grpc-js: Ensure server interceptors work with builder utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
murgatroid99 committed Mar 21, 2024
1 parent 649412f commit 9948aea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
20 changes: 17 additions & 3 deletions packages/grpc-js/src/server-interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ export class ServerInterceptingCall implements ServerInterceptingCallInterface {
private nextCall: ServerInterceptingCallInterface,
responder?: Responder
) {
this.responder = { ...defaultResponder, ...responder };
this.responder = {
start: responder?.start ?? defaultResponder.start,
sendMetadata: responder?.sendMetadata ?? defaultResponder.sendMetadata,
sendMessage: responder?.sendMessage ?? defaultResponder.sendMessage,
sendStatus: responder?.sendStatus ?? defaultResponder.sendStatus,
};
}

private processPendingMessage() {
Expand All @@ -369,8 +374,17 @@ export class ServerInterceptingCall implements ServerInterceptingCallInterface {
start(listener: InterceptingServerListener): void {
this.responder.start(interceptedListener => {
const fullInterceptedListener: FullServerListener = {
...defaultServerListener,
...interceptedListener,
onReceiveMetadata:
interceptedListener?.onReceiveMetadata ??
defaultServerListener.onReceiveMetadata,
onReceiveMessage:
interceptedListener?.onReceiveMessage ??
defaultServerListener.onReceiveMessage,
onReceiveHalfClose:
interceptedListener?.onReceiveHalfClose ??
defaultServerListener.onReceiveHalfClose,
onCancel:
interceptedListener?.onCancel ?? defaultServerListener.onCancel,
};
const finalInterceptingListener = new InterceptingServerListenerImpl(
fullInterceptedListener,
Expand Down
35 changes: 16 additions & 19 deletions packages/grpc-js/test/test-server-interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,22 @@ const testAuthInterceptor: grpc.ServerInterceptor = (
methodDescriptor,
call
) => {
return new grpc.ServerInterceptingCall(call, {
start: next => {
const authListener: grpc.ServerListener = {
onReceiveMetadata: (metadata, mdNext) => {
if (
metadata.get(AUTH_HEADER_KEY)?.[0] !== AUTH_HEADER_ALLOWED_VALUE
) {
call.sendStatus({
code: grpc.status.UNAUTHENTICATED,
details: 'Auth metadata not correct',
});
} else {
mdNext(metadata);
}
},
};
next(authListener);
},
});
const authListener = (new grpc.ServerListenerBuilder())
.withOnReceiveMetadata((metadata, mdNext) => {
if (
metadata.get(AUTH_HEADER_KEY)?.[0] !== AUTH_HEADER_ALLOWED_VALUE
) {
call.sendStatus({
code: grpc.status.UNAUTHENTICATED,
details: 'Auth metadata not correct',
});
} else {
mdNext(metadata);
}
}).build();
const responder = (new grpc.ResponderBuilder())
.withStart(next => next(authListener)).build();
return new grpc.ServerInterceptingCall(call, responder);
};

let eventCounts = {
Expand Down

0 comments on commit 9948aea

Please sign in to comment.