@@ -123,8 +123,8 @@ export class GrpcChannel implements IChannel {
123123export class GrpcCommunication extends EventEmitter implements ICommunication , IMeshServiceServer {
124124 private config : MeshConfig ;
125125 private server : grpc . Server ;
126- private channelListeners : { [ service : string ] : ( channel : IChannel ) => void } = { } ;
127- private unaryListeners : { [ service : string ] : ( msg : any ) => Promise < any > } = { } ;
126+ private channelListeners : { [ service : string ] : ( channel : IChannel , metadata ?: Record < string , string > ) => void } = { } ;
127+ private unaryListeners : { [ service : string ] : ( msg : any , metadata ?: Record < string , string > ) => Promise < any > } = { } ;
128128
129129 // Note: IMeshServiceServer methods are implemented as arrow functions or bound methods usually.
130130 // Or we simply pass implementation object.
@@ -164,9 +164,18 @@ export class GrpcCommunication extends EventEmitter implements ICommunication, I
164164 const metadata = call . metadata ;
165165 const serviceName = metadata . get ( 'x-service-name' ) [ 0 ] as string ;
166166
167+ // Convert grpc Metadata to Record<string, string>
168+ const metadataRecord : Record < string , string > = { } ;
169+ for ( const key in metadata . getMap ( ) ) {
170+ const val = metadata . get ( key ) ;
171+ if ( val . length > 0 ) {
172+ metadataRecord [ key ] = val [ 0 ] as string ;
173+ }
174+ }
175+
167176 if ( serviceName && this . channelListeners [ serviceName ] ) {
168177 const channel = new GrpcChannel ( call ) ;
169- this . channelListeners [ serviceName ] ( channel ) ;
178+ this . channelListeners [ serviceName ] ( channel , metadataRecord ) ;
170179 } else {
171180 console . warn ( `[gRPC] No channel listener for service: ${ serviceName } ` ) ;
172181 call . end ( ) ;
@@ -177,14 +186,23 @@ export class GrpcCommunication extends EventEmitter implements ICommunication, I
177186 const metadata = call . metadata ;
178187 const serviceName = metadata . get ( 'x-service-name' ) [ 0 ] as string ;
179188
189+ // Convert grpc Metadata to Record<string, string>
190+ const metadataRecord : Record < string , string > = { } ;
191+ for ( const key in metadata . getMap ( ) ) {
192+ const val = metadata . get ( key ) ;
193+ if ( val . length > 0 ) {
194+ metadataRecord [ key ] = val [ 0 ] as string ;
195+ }
196+ }
197+
180198 if ( serviceName && this . unaryListeners [ serviceName ] ) {
181199 const requestMsg = call . request ;
182200 try {
183201 const payloadBytes = requestMsg . getPayload_asU8 ( ) ;
184202 const jsonString = Buffer . from ( payloadBytes ) . toString ( 'utf8' ) ;
185203 const msg = JSON . parse ( jsonString ) ;
186204
187- this . unaryListeners [ serviceName ] ( msg )
205+ this . unaryListeners [ serviceName ] ( msg , metadataRecord )
188206 . then ( response => {
189207 const payload = Buffer . from ( JSON . stringify ( response ) , 'utf8' ) ;
190208 const respMsg = new MeshMessage ( ) ;
@@ -215,38 +233,50 @@ export class GrpcCommunication extends EventEmitter implements ICommunication, I
215233 }
216234 }
217235
218- public listenChannel ( serviceName : string , callback : ( channel : IChannel ) => void ) {
236+ public listenChannel ( serviceName : string , callback : ( channel : IChannel , metadata ?: Record < string , string > ) => void ) {
219237 this . channelListeners [ serviceName ] = callback ;
220238 }
221239
222- public listenUnary ( serviceName : string , callback : ( msg : any ) => Promise < any > ) {
240+ public listenUnary ( serviceName : string , callback : ( msg : any , metadata ?: Record < string , string > ) => Promise < any > ) {
223241 this . unaryListeners [ serviceName ] = callback ;
224242 }
225243
226- public async createChannel ( node : Node ) : Promise < IChannel > {
244+ public async createChannel ( node : Node , metadata ?: Record < string , string > ) : Promise < IChannel > {
227245 const address = `${ node . host } :${ node . port } ` ;
228246 const client = new MeshServiceClient ( address , grpc . credentials . createInsecure ( ) ) ;
229247
230- const metadata = new grpc . Metadata ( ) ;
231- metadata . add ( 'x-service-name' , node . service_name ) ;
248+ const grpcMetadata = new grpc . Metadata ( ) ;
249+ grpcMetadata . add ( 'x-service-name' , node . service_name ) ;
232250
233- const call = client . send ( metadata ) ;
251+ if ( metadata ) {
252+ for ( const [ key , value ] of Object . entries ( metadata ) ) {
253+ grpcMetadata . add ( key , value ) ;
254+ }
255+ }
256+
257+ const call = client . send ( grpcMetadata ) ;
234258 return new GrpcChannel ( call ) ;
235259 }
236260
237- public async sendUnary ( node : Node , message : any ) : Promise < any > {
261+ public async sendUnary ( node : Node , message : any , metadata ?: Record < string , string > ) : Promise < any > {
238262 return new Promise ( ( resolve , reject ) => {
239263 const address = `${ node . host } :${ node . port } ` ;
240264 const client = new MeshServiceClient ( address , grpc . credentials . createInsecure ( ) ) ;
241265
242- const metadata = new grpc . Metadata ( ) ;
243- metadata . add ( 'x-service-name' , node . service_name ) ;
266+ const grpcMetadata = new grpc . Metadata ( ) ;
267+ grpcMetadata . add ( 'x-service-name' , node . service_name ) ;
268+
269+ if ( metadata ) {
270+ for ( const [ key , value ] of Object . entries ( metadata ) ) {
271+ grpcMetadata . add ( key , value ) ;
272+ }
273+ }
244274
245275 const payload = Buffer . from ( JSON . stringify ( message ) , 'utf8' ) ;
246276 const reqMsg = new MeshMessage ( ) ;
247277 reqMsg . setPayload ( payload ) ;
248278
249- client . unary ( reqMsg , metadata , ( err : grpc . ServiceError | null , response : MeshMessage ) => {
279+ client . unary ( reqMsg , grpcMetadata , ( err : grpc . ServiceError | null , response : MeshMessage ) => {
250280 if ( err ) {
251281 reject ( err ) ;
252282 } else {
0 commit comments