@gravity-ui/gateway 2.6.0 → 3.0.0
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.
- package/build/components/grpc.js +4 -16
- package/build/constants.d.ts +6 -0
- package/build/constants.js +7 -1
- package/build/utils/grpc.d.ts +1 -11
- package/build/utils/grpc.js +16 -26
- package/build/utils/parse-error.js +1 -1
- package/package.json +1 -1
package/build/components/grpc.js
CHANGED
|
@@ -45,13 +45,7 @@ const parse_error_1 = require("../utils/parse-error");
|
|
|
45
45
|
const proto_path_resolver_1 = require("../utils/proto-path-resolver");
|
|
46
46
|
const redact_sensitive_headers_1 = require("../utils/redact-sensitive-headers");
|
|
47
47
|
const validate_1 = require("../utils/validate");
|
|
48
|
-
const
|
|
49
|
-
longs: String,
|
|
50
|
-
enums: String,
|
|
51
|
-
defaults: true,
|
|
52
|
-
oneofs: true,
|
|
53
|
-
};
|
|
54
|
-
const grpcLoaderOptions = Object.assign(Object.assign({}, reflectLoaderOptions), { includeDirs: [path_1.default.join(__dirname, '../../proto')] });
|
|
48
|
+
const grpcLoaderOptions = Object.assign(Object.assign({}, constants_1.DEFAULT_PROTO_LOADER_OPTIONS), { includeDirs: [path_1.default.join(__dirname, '../../proto')] });
|
|
55
49
|
function createRoot(includeGrpcPaths) {
|
|
56
50
|
const root = new protobufjs.Root();
|
|
57
51
|
root.loadSync(path_1.default.resolve(__dirname, '../../proto/google/rpc/code.proto'));
|
|
@@ -81,16 +75,10 @@ function decodeResponse(response, packageRoot, ctx, encodedFields = [], ErrorCon
|
|
|
81
75
|
const systemFields = ['metadata', 'response', 'error.details'];
|
|
82
76
|
[...systemFields, ...encodedFields].forEach((fieldName) => {
|
|
83
77
|
try {
|
|
84
|
-
const
|
|
85
|
-
const needTraverse = traverseRegExp.test(fieldName);
|
|
86
|
-
const parsedFieldName = needTraverse
|
|
87
|
-
? fieldName.replace(traverseRegExp, '')
|
|
88
|
-
: fieldName;
|
|
78
|
+
const parsedFieldName = fieldName.replace(/\.\*$/, '');
|
|
89
79
|
const fieldValue = lodash_1.default.get(response, parsedFieldName);
|
|
90
80
|
if (fieldValue) {
|
|
91
|
-
lodash_1.default.set(response, parsedFieldName,
|
|
92
|
-
? (0, grpc_1.traverseAnyMessage)(packageRoot, fieldValue)
|
|
93
|
-
: (0, grpc_1.decodeAnyMessageRecursively)(packageRoot, fieldValue));
|
|
81
|
+
lodash_1.default.set(response, parsedFieldName, (0, grpc_1.decodeAnyMessageRecursively)(packageRoot, fieldValue));
|
|
94
82
|
}
|
|
95
83
|
}
|
|
96
84
|
catch (error) {
|
|
@@ -270,7 +258,7 @@ async function getServiceInstanceReflect(config, endpointData, grpcOptions, cred
|
|
|
270
258
|
loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions);
|
|
271
259
|
}
|
|
272
260
|
const descriptor = loadedRoot.toDescriptor('proto3');
|
|
273
|
-
const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor,
|
|
261
|
+
const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor, constants_1.DEFAULT_PROTO_LOADER_OPTIONS);
|
|
274
262
|
const packageObject = grpc.loadPackageDefinition(definition);
|
|
275
263
|
const Service = lodash_1.default.get(packageObject, config.protoKey);
|
|
276
264
|
const serviceInstance = new Service(actionEndpoint, creds, combinedGrpcOptions);
|
package/build/constants.d.ts
CHANGED
|
@@ -20,6 +20,12 @@ export declare const DEFAULT_GRPC_OPTIONS: {
|
|
|
20
20
|
'grpc.keepalive_timeout_ms': number;
|
|
21
21
|
'grpc.keepalive_permit_without_calls': number;
|
|
22
22
|
};
|
|
23
|
+
export declare const DEFAULT_PROTO_LOADER_OPTIONS: {
|
|
24
|
+
longs: StringConstructor;
|
|
25
|
+
enums: StringConstructor;
|
|
26
|
+
defaults: boolean;
|
|
27
|
+
oneofs: boolean;
|
|
28
|
+
};
|
|
23
29
|
/**
|
|
24
30
|
* Byte sizes are taken from ECMAScript Language Specification
|
|
25
31
|
* http://www.ecma-international.org/ecma-262/5.1/
|
package/build/constants.js
CHANGED
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.AXIOS_RETRY_NAMESPACE = exports.DEFAULT_VALIDATION_SCHEMA = exports.RECREATE_SERVICE_CODES = exports.RETRYABLE_STATUS_CODES = exports.ANY_ACTION_SYMBOL = exports.ECMA_STRING_SIZE = exports.DEFAULT_GRPC_OPTIONS = exports.DEFAULT_AXIOS_OPTIONS = exports.DEFAULT_PROXY_HEADERS = exports.DEFAULT_LANG_HEADER = exports.DEFAULT_TIMEOUT = exports.Lang = exports.VERSION = void 0;
|
|
29
|
+
exports.AXIOS_RETRY_NAMESPACE = exports.DEFAULT_VALIDATION_SCHEMA = exports.RECREATE_SERVICE_CODES = exports.RETRYABLE_STATUS_CODES = exports.ANY_ACTION_SYMBOL = exports.ECMA_STRING_SIZE = exports.DEFAULT_PROTO_LOADER_OPTIONS = exports.DEFAULT_GRPC_OPTIONS = exports.DEFAULT_AXIOS_OPTIONS = exports.DEFAULT_PROXY_HEADERS = exports.DEFAULT_LANG_HEADER = exports.DEFAULT_TIMEOUT = exports.Lang = exports.VERSION = void 0;
|
|
30
30
|
const http_1 = __importDefault(require("http"));
|
|
31
31
|
const https_1 = __importDefault(require("https"));
|
|
32
32
|
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
@@ -63,6 +63,12 @@ exports.DEFAULT_GRPC_OPTIONS = {
|
|
|
63
63
|
'grpc.keepalive_timeout_ms': 1000,
|
|
64
64
|
'grpc.keepalive_permit_without_calls': 1,
|
|
65
65
|
};
|
|
66
|
+
exports.DEFAULT_PROTO_LOADER_OPTIONS = {
|
|
67
|
+
longs: String,
|
|
68
|
+
enums: String,
|
|
69
|
+
defaults: true,
|
|
70
|
+
oneofs: true,
|
|
71
|
+
};
|
|
66
72
|
/**
|
|
67
73
|
* Byte sizes are taken from ECMAScript Language Specification
|
|
68
74
|
* http://www.ecma-international.org/ecma-262/5.1/
|
package/build/utils/grpc.d.ts
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import * as grpc from '@grpc/grpc-js';
|
|
3
2
|
import * as protobufjs from 'protobufjs';
|
|
4
|
-
export declare function decodeAnyMessageRecursively(root: protobufjs.Root, message?:
|
|
5
|
-
type_url?: string;
|
|
6
|
-
value?: Buffer;
|
|
7
|
-
}): {
|
|
8
|
-
type_url?: string | undefined;
|
|
9
|
-
value?: Buffer | undefined;
|
|
10
|
-
} | {
|
|
11
|
-
[k: string]: any;
|
|
12
|
-
} | undefined;
|
|
13
|
-
export declare function traverseAnyMessage(root: protobufjs.Root, message?: any): any;
|
|
3
|
+
export declare function decodeAnyMessageRecursively(root: protobufjs.Root, message?: unknown): unknown;
|
|
14
4
|
export declare function isRetryableError(error?: grpc.ServiceError): boolean;
|
|
15
5
|
export declare function isRecreateServiceError(error?: grpc.ServiceError): boolean;
|
package/build/utils/grpc.js
CHANGED
|
@@ -1,44 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* eslint-disable camelcase */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.isRecreateServiceError = exports.isRetryableError = exports.
|
|
4
|
+
exports.isRecreateServiceError = exports.isRetryableError = exports.decodeAnyMessageRecursively = void 0;
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
|
+
function isEncodedMessage(message) {
|
|
7
|
+
return Boolean(message.type_url && message.value);
|
|
8
|
+
}
|
|
6
9
|
function decodeAnyMessageRecursively(root, message) {
|
|
7
|
-
if (!message ||
|
|
10
|
+
if (!message || typeof message !== 'object') {
|
|
8
11
|
return message;
|
|
9
12
|
}
|
|
13
|
+
if (Array.isArray(message)) {
|
|
14
|
+
return message.map((innerMessage) => decodeAnyMessageRecursively(root, innerMessage));
|
|
15
|
+
}
|
|
16
|
+
if (typeof message === 'object' && !isEncodedMessage(message)) {
|
|
17
|
+
return Object.entries(message).reduce((res, [key, value]) => {
|
|
18
|
+
res[key] = decodeAnyMessageRecursively(root, value);
|
|
19
|
+
return res;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
10
22
|
const lastSlashIndex = message.type_url.lastIndexOf('/');
|
|
11
23
|
if (lastSlashIndex < 0) {
|
|
12
24
|
return message;
|
|
13
25
|
}
|
|
14
26
|
const typeName = message.type_url.substring(lastSlashIndex + 1);
|
|
15
27
|
const type = root.lookupType(typeName);
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
data[key] = decodeAnyMessageRecursively(root, data[key]);
|
|
19
|
-
});
|
|
20
|
-
return data;
|
|
28
|
+
const decodedMessage = type.toObject(type.decode(message.value), constants_1.DEFAULT_PROTO_LOADER_OPTIONS);
|
|
29
|
+
return decodeAnyMessageRecursively(root, decodedMessage);
|
|
21
30
|
}
|
|
22
31
|
exports.decodeAnyMessageRecursively = decodeAnyMessageRecursively;
|
|
23
|
-
function traverseAnyMessage(root, message) {
|
|
24
|
-
if (!message) {
|
|
25
|
-
return message;
|
|
26
|
-
}
|
|
27
|
-
if (Array.isArray(message)) {
|
|
28
|
-
return message.map((item) => {
|
|
29
|
-
return traverseAnyMessage(root, item);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
if (typeof message === 'object') {
|
|
33
|
-
Object.keys(message).forEach((key) => {
|
|
34
|
-
if (message[key]) {
|
|
35
|
-
message[key] = traverseAnyMessage(root, message[key]);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
return decodeAnyMessageRecursively(root, message);
|
|
40
|
-
}
|
|
41
|
-
exports.traverseAnyMessage = traverseAnyMessage;
|
|
42
32
|
function isRetryableError(error) {
|
|
43
33
|
if (!error) {
|
|
44
34
|
return false;
|
|
@@ -182,7 +182,7 @@ function parseGrpcError(error, packageRoot, lang = constants_1.Lang.Ru) {
|
|
|
182
182
|
message: String(description || DEFAULT_GATEWAY_MESSAGE),
|
|
183
183
|
code: DEFAULT_GATEWAY_CODE,
|
|
184
184
|
details: Object.assign({ title,
|
|
185
|
-
description, grpcCode: code }, details),
|
|
185
|
+
description, grpcCode: code }, (typeof details === 'object' && !Array.isArray(details) ? details : { details })),
|
|
186
186
|
};
|
|
187
187
|
}
|
|
188
188
|
exports.parseGrpcError = parseGrpcError;
|