diff --git a/packages/grpc-js/src/server-interceptors.ts b/packages/grpc-js/src/server-interceptors.ts index 9dfbbb2ff..c9cfe416b 100644 --- a/packages/grpc-js/src/server-interceptors.ts +++ b/packages/grpc-js/src/server-interceptors.ts @@ -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() { @@ -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, diff --git a/packages/grpc-js/test/test-server-interceptors.ts b/packages/grpc-js/test/test-server-interceptors.ts index 5e93d32d2..e94169721 100644 --- a/packages/grpc-js/test/test-server-interceptors.ts +++ b/packages/grpc-js/test/test-server-interceptors.ts @@ -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 = {