Comparing version 1.2.4 to 1.2.5
{ | ||
"name": "ugrpc", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "gRPC for microservice communications", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,6 +1,70 @@ | ||
export = GrpcClient; | ||
/** | ||
* @typedef { import('@grpc/grpc-js').Client } Client | ||
*/ | ||
/** | ||
* proto-loader configurations | ||
* @typedef {Object} ProtoConfig | ||
* @property {string} params.proto.packageName package name | ||
* @property {string} params.proto.serviceName service name | ||
* @property {string} params.proto.protoPath *.proto absolute file path | ||
*/ | ||
export type ProtoConfig = { | ||
/** | ||
* proto-loader options. Read more at: https://www.npmjs.com/package/@grpc/proto-loader | ||
* @typedef {Object} protoLoaderOptions | ||
* @property {Boolean} [params.protoLoaderOptions.keepCase=false] - `true` or `false` | ||
* @property {Function} [params.protoLoaderOptions.longs=String] `String` or `Number` | ||
* @property {Function} [params.protoLoaderOptions.enums=String] `String` | ||
* @property {Function} [params.protoLoaderOptions.bytes=Buffer] `Array` or `String` | ||
* @property {Boolean} [params.protoLoaderOptions.defaults=false] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.arrays=true] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.objects=false] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.oneofs=true] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.json=false] `true` or `false` | ||
* @property {[String]} [params.protoLoaderOptions.includeDirs=[]] A list of search paths for imported .proto files. | ||
*/ | ||
/** | ||
* gRPC Client | ||
* | ||
* @class GrpcClient | ||
*/ | ||
declare class GrpcClient { | ||
/** | ||
* Constructor - Create gRPC Client | ||
* | ||
* @param {Object} params | ||
* @param {!string} params.host gRPC server address. REQUIRED | ||
* @param {!Boolean} params.isSecureChannel should create the secure channel. Default: false | ||
* @param {!ProtoConfig} params.proto proto loader configurations | ||
* @param {!protoLoaderOptions} params.protoLoaderOptions proto loader configurations | ||
* @returns {Client} gRPC Client | ||
*/ | ||
constructor(params: { | ||
host: string; | ||
isSecureChannel: boolean; | ||
proto: ProtoConfig; | ||
protoLoaderOptions: protoLoaderOptions; | ||
}); | ||
createCallOptions(timeoutMs?: number): { | ||
deadline: Date; | ||
}; | ||
/** | ||
* return current gRPC Client | ||
* | ||
* @returns {Client} gRPC Client | ||
* @memberof GrpcClient | ||
*/ | ||
getClient(): Client; | ||
handleCallRejected(): void; | ||
#private; | ||
} | ||
declare namespace GrpcClient { | ||
export { Client, ProtoConfig, protoLoaderOptions }; | ||
} | ||
type Client = import('@grpc/grpc-js').Client; | ||
/** | ||
* proto-loader configurations | ||
*/ | ||
type ProtoConfig = { | ||
/** | ||
* package name | ||
@@ -21,3 +85,3 @@ */ | ||
*/ | ||
export type protoLoaderOptions = { | ||
type protoLoaderOptions = { | ||
/** | ||
@@ -64,42 +128,1 @@ * - `true` or `false` | ||
}; | ||
/** | ||
* proto-loader configurations | ||
* @typedef {Object} ProtoConfig | ||
* @property {string} params.proto.packageName package name | ||
* @property {string} params.proto.serviceName service name | ||
* @property {string} params.proto.protoPath *.proto absolute file path | ||
*/ | ||
/** | ||
* proto-loader options. Read more at: https://www.npmjs.com/package/@grpc/proto-loader | ||
* @typedef {Object} protoLoaderOptions | ||
* @property {Boolean} [params.protoLoaderOptions.keepCase=false] - `true` or `false` | ||
* @property {Function} [params.protoLoaderOptions.longs=String] `String` or `Number` | ||
* @property {Function} [params.protoLoaderOptions.enums=String] `String` | ||
* @property {Function} [params.protoLoaderOptions.bytes=Buffer] `Array` or `String` | ||
* @property {Boolean} [params.protoLoaderOptions.defaults=false] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.arrays=true] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.objects=false] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.oneofs=true] `true` or `false` | ||
* @property {Boolean} [params.protoLoaderOptions.json=false] `true` or `false` | ||
* @property {[String]} [params.protoLoaderOptions.includeDirs=[]] A list of search paths for imported .proto files. | ||
*/ | ||
/** | ||
* Create gRPC Client | ||
* | ||
* @param {Object} params | ||
* @param {!string} params.host gRPC server address. REQUIRED | ||
* @param {!Boolean} params.isSecureChannel should create the secure channel. Default: false | ||
* @param {!ProtoConfig} params.proto proto loader configurations | ||
* @param {!protoLoaderOptions} params.protoLoaderOptions proto loader configurations | ||
* @returns {grpc.Client} gRPC Client | ||
*/ | ||
export function createGrpcClient(params: { | ||
host: string; | ||
isSecureChannel: boolean; | ||
proto: ProtoConfig; | ||
protoLoaderOptions: protoLoaderOptions; | ||
}): grpc.Client; | ||
export function createCallOptions(timeoutMs?: number): { | ||
deadline: Date; | ||
}; | ||
import grpc = require("@grpc/grpc-js"); |
@@ -0,1 +1,3 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
const _ = require('lodash'); | ||
@@ -6,2 +8,6 @@ const grpc = require('@grpc/grpc-js'); | ||
/** | ||
* @typedef { import('@grpc/grpc-js').Client } Client | ||
*/ | ||
/** | ||
* proto-loader configurations | ||
@@ -30,64 +36,85 @@ * @typedef {Object} ProtoConfig | ||
/** | ||
* Create gRPC Client | ||
* gRPC Client | ||
* | ||
* @param {Object} params | ||
* @param {!string} params.host gRPC server address. REQUIRED | ||
* @param {!Boolean} params.isSecureChannel should create the secure channel. Default: false | ||
* @param {!ProtoConfig} params.proto proto loader configurations | ||
* @param {!protoLoaderOptions} params.protoLoaderOptions proto loader configurations | ||
* @returns {grpc.Client} gRPC Client | ||
* @class GrpcClient | ||
*/ | ||
function createGrpcClient(params) { | ||
const { host, proto = {}, isSecureChannel } = params; | ||
const { protoLoaderOptions = {} } = params; | ||
const { packageName, serviceName, protoPath } = proto; | ||
class GrpcClient { | ||
#client | ||
if (!host) { | ||
return null; | ||
/** | ||
* Constructor - Create gRPC Client | ||
* | ||
* @param {Object} params | ||
* @param {!string} params.host gRPC server address. REQUIRED | ||
* @param {!Boolean} params.isSecureChannel should create the secure channel. Default: false | ||
* @param {!ProtoConfig} params.proto proto loader configurations | ||
* @param {!protoLoaderOptions} params.protoLoaderOptions proto loader configurations | ||
* @returns {Client} gRPC Client | ||
*/ | ||
constructor(params) { | ||
const { host, proto = {}, isSecureChannel } = params; | ||
const { protoLoaderOptions = {} } = params; | ||
const { packageName, serviceName, protoPath } = proto; | ||
if (!host) { | ||
return null; | ||
} | ||
const packageDefinition = protoLoader.loadSync( | ||
protoPath, | ||
{ | ||
keepCase: false, | ||
longs: String, | ||
enums: String, | ||
defaults: false, | ||
oneofs: true, | ||
arrays: true, | ||
...protoLoaderOptions, | ||
}, | ||
); | ||
const GrpcService = _.chain(grpc.loadPackageDefinition(packageDefinition)) | ||
.get(packageName) | ||
.get(serviceName) | ||
.value(); | ||
const options = { | ||
'grpc.keepalive_time_ms': 10000, | ||
'grpc.keepalive_timeout_ms': 5000, | ||
'grpc.keepalive_permit_without_calls': 1, | ||
'grpc.http2.max_pings_without_data': 0, | ||
'grpc.http2.min_time_between_pings_ms': 10000, | ||
'grpc.http2.min_ping_interval_without_data_ms': 5000, | ||
}; | ||
const channelCredential = isSecureChannel ? grpc.credentials.createSsl() : grpc.credentials.createInsecure(); | ||
this.#client = new GrpcService(host, channelCredential, options); | ||
} | ||
const packageDefinition = protoLoader.loadSync( | ||
protoPath, | ||
{ | ||
keepCase: false, | ||
longs: String, | ||
enums: String, | ||
defaults: false, | ||
oneofs: true, | ||
arrays: true, | ||
...protoLoaderOptions, | ||
}, | ||
); | ||
const GrpcService = _.chain(grpc.loadPackageDefinition(packageDefinition)) | ||
.get(packageName) | ||
.get(serviceName) | ||
.value(); | ||
createCallOptions(timeoutMs = 60000) { | ||
const deadline = this.#createDeadline(timeoutMs); | ||
return { | ||
deadline, | ||
}; | ||
} | ||
const options = { | ||
'grpc.keepalive_time_ms': 10000, | ||
'grpc.keepalive_timeout_ms': 5000, | ||
'grpc.keepalive_permit_without_calls': 1, | ||
'grpc.http2.max_pings_without_data': 0, | ||
'grpc.http2.min_time_between_pings_ms': 10000, | ||
'grpc.http2.min_ping_interval_without_data_ms': 5000, | ||
}; | ||
const channelCredential = isSecureChannel ? grpc.credentials.createSsl() : grpc.credentials.createInsecure(); | ||
return new GrpcService(host, channelCredential, options); | ||
} | ||
#createDeadline(timeoutMs = 60000) { | ||
const deadline = new Date(Date.now() + timeoutMs); | ||
return deadline; | ||
} | ||
function createCallOptions(timeoutMs = 60000) { | ||
const deadline = createDeadline(timeoutMs); | ||
return { | ||
deadline, | ||
}; | ||
} | ||
/** | ||
* return current gRPC Client | ||
* | ||
* @returns {Client} gRPC Client | ||
* @memberof GrpcClient | ||
*/ | ||
getClient() { | ||
return this.#client; | ||
} | ||
function createDeadline(timeoutMs = 60000) { | ||
const deadline = new Date(Date.now() + timeoutMs); | ||
return deadline; | ||
handleCallRejected() { | ||
const channel = this.#client.getChannel(); | ||
channel.getConnectivityState(true); | ||
} | ||
} | ||
module.exports = { | ||
createGrpcClient, | ||
createCallOptions, | ||
}; | ||
module.exports = GrpcClient; |
30812
581