add: metadata to creds

This commit is contained in:
sanish-bruno
2025-10-30 19:14:36 +05:30
parent b982f6db16
commit 252fd386b7
3 changed files with 35 additions and 17 deletions

7
package-lock.json generated
View File

@@ -15167,6 +15167,12 @@
"csstype": "^3.0.10"
}
},
"node_modules/google-protobuf": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/google-protobuf/-/google-protobuf-4.0.0.tgz",
"integrity": "sha512-b8wmenhUMf2WNL+xIJ/slvD/hEE6V3nRnG86O2bzkBrMweM9gnqZE1dfXlDjibY3aXJXDNbAHepevYyQ7qWKsQ==",
"license": "(BSD-3-Clause AND Apache-2.0)"
},
"node_modules/gopd": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
@@ -32100,6 +32106,7 @@
"@types/qs": "^6.9.18",
"axios": "^1.9.0",
"debug": "^4.4.3",
"google-protobuf": "^4.0.0",
"grpc-js-reflection-client": "^1.3.0",
"is-ip": "^5.0.1",
"system-ca": "^2.0.1",

View File

@@ -26,11 +26,12 @@
"@types/qs": "^6.9.18",
"axios": "^1.9.0",
"debug": "^4.4.3",
"google-protobuf": "^4.0.0",
"grpc-js-reflection-client": "^1.3.0",
"is-ip": "^5.0.1",
"ws": "^8.18.3",
"system-ca": "^2.0.1",
"tough-cookie": "^6.0.0"
"tough-cookie": "^6.0.0",
"ws": "^8.18.3"
},
"devDependencies": {
"@babel/preset-env": "^7.22.0",
@@ -53,4 +54,4 @@
"overrides": {
"rollup": "3.29.5"
}
}
}

View File

@@ -1,4 +1,4 @@
import { makeGenericClientConstructor, ChannelCredentials, Metadata, status } from '@grpc/grpc-js';
import { makeGenericClientConstructor, ChannelCredentials, Metadata, status, credentials } from '@grpc/grpc-js';
import { GrpcReflection } from 'grpc-js-reflection-client';
import * as protoLoader from '@grpc/proto-loader';
import { generateGrpcSampleMessage } from './grpcMessageGenerator';
@@ -182,7 +182,7 @@ class GrpcClient {
* @param {grpc.ChannelOptions} options - channel options
* @returns {Promise<{ client: GrpcReflection, version: 'v1' | 'v1alpha' }>}
*/
async #getReflectionClient(host, credentials = ChannelCredentials.createInsecure(), options = {}) {
async #getReflectionClient(host, credentials = ChannelCredentials.createInsecure(), metadata = null, options = {}) {
const makeClient = (version) => new GrpcReflection(host, credentials, options, version);
let client;
let services;
@@ -235,8 +235,15 @@ class GrpcClient {
* @param {VerifyOptions} verifyOptions - Additional options for verifying the server certificate
* @returns {import('@grpc/grpc-js').ChannelCredentials} The gRPC channel credentials
*/
#getChannelCredentials({ url, rootCertificate, privateKey, certificateChain, passphrase, pfx, verifyOptions }) {
#getChannelCredentials({ url, headers, rootCertificate, privateKey, certificateChain, passphrase, pfx, verifyOptions }) {
const securedProtocols = ['grpcs', 'https'];
const metadata = new Metadata();
Object.entries(headers).forEach(([name, value]) => {
metadata.add(name, value);
});
const callCredentials = credentials.createFromMetadataGenerator((options, callback) => {
callback(null, metadata);
});
try {
const { protocol } = getParsedGrpcUrlObject(url);
const isSecureConnection = securedProtocols.some((sp) => protocol === sp);
@@ -266,16 +273,17 @@ class GrpcClient {
pfx: pfxBuffer,
passphrase: passphrase
});
return ChannelCredentials.createFromSecureContext(secureContext, sslOptions);
const channelCredentials = ChannelCredentials.createFromSecureContext(secureContext, sslOptions);
return credentials.combineChannelCredentials(channelCredentials, callCredentials);
}
const credentials = ChannelCredentials.createSsl(rootCertBuffer, privateKeyBuffer, clientCertBuffer, sslOptions);
return credentials;
const channelCredentials = ChannelCredentials.createSsl(rootCertBuffer, privateKeyBuffer, clientCertBuffer, sslOptions);
return credentials.combineChannelCredentials(channelCredentials, callCredentials);
} catch (error) {
console.error('Error creating channel credentials:', error);
// Default to insecure as fallback
return ChannelCredentials.createInsecure();
const channelCredentials = ChannelCredentials.createInsecure();
return credentials.combineChannelCredentials(channelCredentials, callCredentials);
}
}
@@ -460,6 +468,7 @@ class GrpcClient {
}) {
const credentials = this.#getChannelCredentials({
url: request.url,
headers: request.headers,
rootCertificate,
privateKey,
certificateChain,
@@ -594,8 +603,14 @@ class GrpcClient {
verifyOptions,
sendEvent
}) {
const { host, path } = getParsedGrpcUrlObject(request.url);
const metadata = new Metadata();
Object.entries(request.headers).forEach(([name, value]) => {
metadata.add(name, value);
});
const credentials = this.#getChannelCredentials({
url: request.url,
headers: request.headers,
rootCertificate,
privateKey,
certificateChain,
@@ -603,14 +618,9 @@ class GrpcClient {
pfx,
verifyOptions
});
const { host, path } = getParsedGrpcUrlObject(request.url);
const metadata = new Metadata();
Object.entries(request.headers).forEach(([name, value]) => {
metadata.add(name, value);
});
try {
const { client, services } = await this.#getReflectionClient(host, credentials, {});
const { client, services } = await this.#getReflectionClient(host, credentials, metadata, {});
const methods = [];
for (const service of services) {