@gravity-ui/gateway 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -218,12 +218,17 @@ async function refreshCache(actionEndpoint, config, endpointData, grpcOptions, c
218
218
  return;
219
219
  }
220
220
  lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], Object.assign(Object.assign({}, cacheStatus), { isRefresh: true }));
221
- const res = await getServiceInstanceReflect(config, endpointData, grpcOptions, credentials, true);
222
- lodash_1.default.set(reflectionServiceInstancesMap, [config.protoKey, actionEndpoint], Promise.resolve(res));
223
- lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], {
224
- time: Date.now() / 1000,
225
- isRefresh: false,
226
- });
221
+ try {
222
+ const res = await getServiceInstanceReflect(config, endpointData, grpcOptions, credentials, true);
223
+ lodash_1.default.set(reflectionServiceInstancesMap, [config.protoKey, actionEndpoint], Promise.resolve(res));
224
+ lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], {
225
+ time: Date.now() / 1000,
226
+ isRefresh: false,
227
+ });
228
+ }
229
+ catch (e) {
230
+ lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], Object.assign(Object.assign({}, cacheStatus), { isRefresh: false }));
231
+ }
227
232
  }
228
233
  function getServiceInstanceReflectCached(config, endpointData, grpcOptions, credentials) {
229
234
  if (config.reflection === common_1.GrpcReflection.OnEveryRequest) {
@@ -241,6 +246,9 @@ function getServiceInstanceReflectCached(config, endpointData, grpcOptions, cred
241
246
  }
242
247
  const service = getServiceInstanceReflect(config, endpointData, grpcOptions, credentials);
243
248
  lodash_1.default.set(reflectionServiceInstancesMap, cacheKey, service);
249
+ service.catch(() => {
250
+ lodash_1.default.set(reflectionServiceInstancesMap, cacheKey, undefined);
251
+ });
244
252
  return service;
245
253
  }
246
254
  async function getServiceInstanceReflect(config, endpointData, grpcOptions, credentials, isRefreshCache) {
@@ -250,21 +258,22 @@ async function getServiceInstanceReflect(config, endpointData, grpcOptions, cred
250
258
  : undefined;
251
259
  const isInsecure = config.insecure || endpointInsecure;
252
260
  const creds = isInsecure ? credentials.insecure : credentials.secure;
261
+ const endpointGrpcOptions = (0, common_2.isExtendedGrpcActionEndpoint)(endpointData)
262
+ ? endpointData.grpcOptions || {}
263
+ : {};
264
+ const combinedGrpcOptions = Object.assign(Object.assign(Object.assign({}, constants_1.DEFAULT_GRPC_OPTIONS), grpcOptions), endpointGrpcOptions);
253
265
  let loadedRoot;
254
266
  if (config.reflection === common_1.GrpcReflection.OnEveryRequest || isRefreshCache) {
255
- loadedRoot = await (0, grpc_reflection_1.getReflectionRoot)(actionEndpoint, config.protoKey, creds, isRefreshCache);
267
+ loadedRoot = await (0, grpc_reflection_1.getReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions, isRefreshCache);
256
268
  }
257
269
  else {
258
- loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds);
270
+ loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions);
259
271
  }
260
272
  const descriptor = loadedRoot.toDescriptor('proto3');
261
273
  const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor, reflectLoaderOptions);
262
274
  const packageObject = grpc.loadPackageDefinition(definition);
263
275
  const Service = lodash_1.default.get(packageObject, config.protoKey);
264
- const endpointGrpcOptions = (0, common_2.isExtendedGrpcActionEndpoint)(endpointData)
265
- ? endpointData.grpcOptions || {}
266
- : {};
267
- const serviceInstance = new Service(actionEndpoint, creds, Object.assign(Object.assign(Object.assign({}, constants_1.DEFAULT_GRPC_OPTIONS), grpcOptions), endpointGrpcOptions));
276
+ const serviceInstance = new Service(actionEndpoint, creds, combinedGrpcOptions);
268
277
  return serviceInstance;
269
278
  }
270
279
  function loadAndCachePackageObject(root, protoPath) {
@@ -1,5 +1,4 @@
1
1
  import type { ChannelCredentials } from '@grpc/grpc-js';
2
- import * as protobufjs from 'protobufjs';
3
2
  type DescriptorExtensionProto = string[] | {
4
3
  includeProtoRoots: string[];
5
4
  filenames: string[];
@@ -8,20 +7,22 @@ type DescriptorExtensionProto = string[] | {
8
7
  * @param actionEndpoint
9
8
  * @param protoKey
10
9
  * @param credentials
10
+ * @param grpcOptions
11
11
  * @param descriptorExtensionProto
12
12
  * @returns Promise<protobufjs.Root>.
13
13
  * use toDescriptor for use with protoLoader.
14
14
  * use toJSON for get a JSON descriptor.
15
15
  */
16
- export declare function getCachedReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, descriptorExtensionProto?: DescriptorExtensionProto): Promise<protobufjs.Root>;
16
+ export declare function getCachedReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, grpcOptions?: object, descriptorExtensionProto?: DescriptorExtensionProto): Promise<any>;
17
17
  /**
18
18
  * @param actionEndpoint
19
19
  * @param protoKey
20
20
  * @param credentials
21
+ * @param grpcOptions
21
22
  * @param addToCache
22
23
  * @returns Promise<protobufjs.Root>.
23
24
  * use toDescriptor for use with protoLoader.
24
25
  * use toJSON for get a JSON descriptor.
25
26
  */
26
- export declare function getReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, addToCache?: boolean): Promise<protobufjs.Root>;
27
+ export declare function getReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, grpcOptions?: object, addToCache?: boolean): Promise<any>;
27
28
  export {};
@@ -31,10 +31,10 @@ const lodash_1 = __importDefault(require("lodash"));
31
31
  const protobufjs = __importStar(require("protobufjs"));
32
32
  const proto_path_resolver_1 = require("./proto-path-resolver");
33
33
  const reflectionClientsMap = {};
34
- const reflectionRootPromiseMap = {};
35
- function getCachedClient(actionEndpoint, credentials, descriptorExtensionProto) {
36
- let client = reflectionClientsMap[actionEndpoint];
37
- if (!client) {
34
+ function getCachedClient(actionEndpoint, credentials, grpcOptions, descriptorExtensionProto) {
35
+ const cacheKey = [actionEndpoint, JSON.stringify([grpcOptions, descriptorExtensionProto])];
36
+ let clientWithCache = lodash_1.default.get(reflectionClientsMap, cacheKey);
37
+ if (!clientWithCache) {
38
38
  const grpcReflection = require('grpc-reflection-js');
39
39
  let descriptorRoot;
40
40
  if (descriptorExtensionProto) {
@@ -47,26 +47,32 @@ function getCachedClient(actionEndpoint, credentials, descriptorExtensionProto)
47
47
  descriptorRoot.loadSync(descriptorExtensionProto.filenames);
48
48
  }
49
49
  }
50
- client = new grpcReflection.Client(actionEndpoint, credentials, undefined, undefined, descriptorRoot);
51
- reflectionClientsMap[actionEndpoint] = client;
50
+ const client = new grpcReflection.Client(actionEndpoint, credentials, grpcOptions, undefined, descriptorRoot);
51
+ clientWithCache = { client, reflectionRootPromiseMap: {} };
52
+ lodash_1.default.set(reflectionClientsMap, cacheKey, clientWithCache);
52
53
  }
53
- return client;
54
+ return clientWithCache;
54
55
  }
55
56
  /**
56
57
  * @param actionEndpoint
57
58
  * @param protoKey
58
59
  * @param credentials
60
+ * @param grpcOptions
59
61
  * @param descriptorExtensionProto
60
62
  * @returns Promise<protobufjs.Root>.
61
63
  * use toDescriptor for use with protoLoader.
62
64
  * use toJSON for get a JSON descriptor.
63
65
  */
64
- async function getCachedReflectionRoot(actionEndpoint, protoKey, credentials, descriptorExtensionProto) {
65
- const client = getCachedClient(actionEndpoint, credentials, descriptorExtensionProto);
66
- let cachedRootPromise = lodash_1.default.get(reflectionRootPromiseMap, [actionEndpoint, protoKey]);
66
+ async function getCachedReflectionRoot(actionEndpoint, protoKey, credentials, grpcOptions, descriptorExtensionProto) {
67
+ const { client, reflectionRootPromiseMap } = getCachedClient(actionEndpoint, credentials, grpcOptions, descriptorExtensionProto);
68
+ const cacheKey = [actionEndpoint, protoKey];
69
+ let cachedRootPromise = lodash_1.default.get(reflectionRootPromiseMap, cacheKey);
67
70
  if (!cachedRootPromise) {
68
71
  cachedRootPromise = client.fileContainingSymbol(protoKey);
69
- lodash_1.default.set(reflectionRootPromiseMap, [actionEndpoint, protoKey], cachedRootPromise);
72
+ lodash_1.default.set(reflectionRootPromiseMap, cacheKey, cachedRootPromise);
73
+ cachedRootPromise.catch(() => {
74
+ lodash_1.default.set(reflectionRootPromiseMap, cacheKey, undefined);
75
+ });
70
76
  }
71
77
  const loadedRoot = await cachedRootPromise;
72
78
  return loadedRoot;
@@ -76,13 +82,14 @@ exports.getCachedReflectionRoot = getCachedReflectionRoot;
76
82
  * @param actionEndpoint
77
83
  * @param protoKey
78
84
  * @param credentials
85
+ * @param grpcOptions
79
86
  * @param addToCache
80
87
  * @returns Promise<protobufjs.Root>.
81
88
  * use toDescriptor for use with protoLoader.
82
89
  * use toJSON for get a JSON descriptor.
83
90
  */
84
- async function getReflectionRoot(actionEndpoint, protoKey, credentials, addToCache) {
85
- const client = getCachedClient(actionEndpoint, credentials);
91
+ async function getReflectionRoot(actionEndpoint, protoKey, credentials, grpcOptions, addToCache) {
92
+ const { client, reflectionRootPromiseMap } = getCachedClient(actionEndpoint, credentials, grpcOptions);
86
93
  const loadedRoot = await client.fileContainingSymbol(protoKey);
87
94
  if (addToCache) {
88
95
  lodash_1.default.set(reflectionRootPromiseMap, [actionEndpoint, protoKey], Promise.resolve(loadedRoot));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/gateway",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",