@gravity-ui/gateway 4.1.2-alpha.0 → 4.2.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/{dist/commonjs → build}/components/grpc.d.ts +4 -4
- package/{dist/commonjs → build}/components/grpc.js +78 -85
- package/{dist/esm → build}/components/mixed.d.ts +4 -4
- package/{dist/commonjs → build}/components/mixed.js +12 -11
- package/{dist/commonjs → build}/components/rest.d.ts +5 -5
- package/{dist/commonjs → build}/components/rest.js +32 -32
- package/{dist/esm → build}/constants.d.ts +2 -2
- package/{dist/commonjs → build}/constants.js +19 -29
- package/{dist/commonjs → build}/index.d.ts +7 -8
- package/{dist/commonjs → build}/index.js +46 -32
- package/{dist/esm → build}/models/common.d.ts +14 -12
- package/{dist/esm → build}/models/context.d.ts +1 -0
- package/build/utils/axios.d.ts +4 -0
- package/{dist/commonjs → build}/utils/axios.js +6 -4
- package/{dist/esm → build}/utils/common.d.ts +3 -3
- package/{dist/commonjs → build}/utils/common.js +8 -7
- package/{dist/commonjs → build}/utils/create-context-api.d.ts +2 -2
- package/{dist/commonjs → build}/utils/create-context-api.js +6 -5
- package/{dist/commonjs → build}/utils/grpc-reflection.js +18 -31
- package/{dist/commonjs → build}/utils/grpc.js +8 -7
- package/build/utils/overrideEndpoints/index.d.ts +2 -0
- package/build/utils/overrideEndpoints/index.js +4 -0
- package/{dist/commonjs → build}/utils/overrideEndpoints/overrideEndpoints.d.ts +1 -1
- package/{dist/commonjs → build}/utils/overrideEndpoints/overrideEndpoints.js +2 -1
- package/{dist/esm → build}/utils/parse-error.d.ts +4 -4
- package/{dist/commonjs → build}/utils/parse-error.js +18 -18
- package/{dist/commonjs → build}/utils/proto-path-resolver.js +17 -23
- package/{dist/commonjs → build}/utils/redact-sensitive-headers.d.ts +2 -1
- package/{dist/commonjs → build}/utils/redact-sensitive-headers.js +2 -1
- package/{dist/commonjs → build}/utils/typed-api.d.ts +1 -1
- package/{dist/commonjs → build}/utils/typed-api.js +2 -1
- package/{dist/commonjs → build}/utils/validate.js +10 -6
- package/package.json +16 -41
- package/dist/commonjs/components/mixed.d.ts +0 -11
- package/dist/commonjs/constants.d.ts +0 -53
- package/dist/commonjs/models/common.d.ts +0 -272
- package/dist/commonjs/models/context.d.ts +0 -22
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/utils/axios.d.ts +0 -3
- package/dist/commonjs/utils/common.d.ts +0 -15
- package/dist/commonjs/utils/overrideEndpoints/index.d.ts +0 -2
- package/dist/commonjs/utils/overrideEndpoints/index.js +0 -4
- package/dist/commonjs/utils/package-root.d.ts +0 -1
- package/dist/commonjs/utils/package-root.js +0 -44
- package/dist/commonjs/utils/parse-error.d.ts +0 -30
- package/dist/commonjs/utils/source-dir.d.ts +0 -1
- package/dist/commonjs/utils/source-dir.js +0 -41
- package/dist/esm/components/grpc.d.ts +0 -24
- package/dist/esm/components/grpc.js +0 -641
- package/dist/esm/components/mixed.js +0 -62
- package/dist/esm/components/rest.d.ts +0 -8
- package/dist/esm/components/rest.js +0 -349
- package/dist/esm/constants.js +0 -82
- package/dist/esm/index.d.ts +0 -12
- package/dist/esm/index.js +0 -264
- package/dist/esm/models/common.js +0 -5
- package/dist/esm/models/context.js +0 -1
- package/dist/esm/models/error.d.ts +0 -12
- package/dist/esm/models/error.js +0 -1
- package/dist/esm/package.json +0 -3
- package/dist/esm/utils/axios.d.ts +0 -3
- package/dist/esm/utils/axios.js +0 -23
- package/dist/esm/utils/common.js +0 -38
- package/dist/esm/utils/create-context-api.d.ts +0 -4
- package/dist/esm/utils/create-context-api.js +0 -38
- package/dist/esm/utils/grpc-reflection.d.ts +0 -28
- package/dist/esm/utils/grpc-reflection.js +0 -72
- package/dist/esm/utils/grpc.d.ts +0 -5
- package/dist/esm/utils/grpc.js +0 -39
- package/dist/esm/utils/overrideEndpoints/index.d.ts +0 -2
- package/dist/esm/utils/overrideEndpoints/index.js +0 -2
- package/dist/esm/utils/overrideEndpoints/overrideEndpoints.d.ts +0 -17
- package/dist/esm/utils/overrideEndpoints/overrideEndpoints.js +0 -96
- package/dist/esm/utils/package-root.d.ts +0 -1
- package/dist/esm/utils/package-root.js +0 -8
- package/dist/esm/utils/parse-error.js +0 -210
- package/dist/esm/utils/proto-path-resolver.d.ts +0 -2
- package/dist/esm/utils/proto-path-resolver.js +0 -23
- package/dist/esm/utils/redact-sensitive-headers.d.ts +0 -3
- package/dist/esm/utils/redact-sensitive-headers.js +0 -12
- package/dist/esm/utils/source-dir.d.ts +0 -1
- package/dist/esm/utils/source-dir.js +0 -4
- package/dist/esm/utils/typed-api.d.ts +0 -2
- package/dist/esm/utils/typed-api.js +0 -3
- package/dist/esm/utils/validate.d.ts +0 -4
- package/dist/esm/utils/validate.js +0 -47
- /package/bin/{patch.cjs → patch.js} +0 -0
- /package/{dist/commonjs → build}/models/common.js +0 -0
- /package/{dist/commonjs → build}/models/context.js +0 -0
- /package/{dist/commonjs → build}/models/error.d.ts +0 -0
- /package/{dist/commonjs → build}/models/error.js +0 -0
- /package/{dist/commonjs → build}/utils/grpc-reflection.d.ts +0 -0
- /package/{dist/commonjs → build}/utils/grpc.d.ts +0 -0
- /package/{dist/commonjs → build}/utils/proto-path-resolver.d.ts +0 -0
- /package/{dist/commonjs → build}/utils/validate.d.ts +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as grpc from '@grpc/grpc-js';
|
|
2
2
|
import * as protobufjs from 'protobufjs';
|
|
3
3
|
import type * as descriptor from 'protobufjs/ext/descriptor';
|
|
4
|
-
import { ApiActionConfig, ApiServiceGrpcActionConfig, EndpointsConfig, GatewayApiOptions } from '../models/common
|
|
5
|
-
import { GatewayContext } from '../models/context
|
|
6
|
-
import { AppErrorConstructor } from '../models/error
|
|
4
|
+
import { ApiActionConfig, ApiServiceGrpcActionConfig, EndpointsConfig, GatewayApiOptions } from '../models/common';
|
|
5
|
+
import { GatewayContext } from '../models/context';
|
|
6
|
+
import { AppErrorConstructor } from '../models/error';
|
|
7
7
|
declare module 'protobufjs' {
|
|
8
8
|
interface Root {
|
|
9
9
|
toDescriptor(protoVersion: string): protobufjs.Message<descriptor.IFileDescriptorSet> & descriptor.IFileDescriptorSet;
|
|
@@ -20,5 +20,5 @@ export interface GrpcContext {
|
|
|
20
20
|
}
|
|
21
21
|
export declare function createRoot(includeGrpcPaths?: string[]): protobufjs.Root;
|
|
22
22
|
export declare function getCredentialsMap(caCertificatePath?: string | null): CredentialsMap;
|
|
23
|
-
export
|
|
23
|
+
export default function createGrpcAction<Context extends GatewayContext>({ root, credentials }: GrpcContext, endpoints: EndpointsConfig | undefined, config: ApiServiceGrpcActionConfig<Context, any, any>, serviceKey: string, actionName: string, options: GatewayApiOptions<Context>, ErrorConstructor: AppErrorConstructor): (actionConfig: ApiActionConfig<Context, any, any>) => Promise<import("../models/common").GatewayActionClientStreamResponse<any> | import("../models/common").GatewayActionServerStreamResponse<any> | import("../models/common").GatewayActionDuplexStreamResponse<any> | import("../models/common").GatewayActionUnaryResponse<any>>;
|
|
24
24
|
export {};
|
|
@@ -16,30 +16,18 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
16
16
|
}) : function(o, v) {
|
|
17
17
|
o["default"] = v;
|
|
18
18
|
});
|
|
19
|
-
var __importStar = (this && this.__importStar) ||
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return ownKeys(o);
|
|
27
|
-
};
|
|
28
|
-
return function (mod) {
|
|
29
|
-
if (mod && mod.__esModule) return mod;
|
|
30
|
-
var result = {};
|
|
31
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
-
__setModuleDefault(result, mod);
|
|
33
|
-
return result;
|
|
34
|
-
};
|
|
35
|
-
})();
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
36
26
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
27
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
28
|
};
|
|
39
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.createRoot =
|
|
41
|
-
exports.getCredentialsMap = getCredentialsMap;
|
|
42
|
-
exports.createGrpcAction = createGrpcAction;
|
|
30
|
+
exports.getCredentialsMap = exports.createRoot = void 0;
|
|
43
31
|
const fs_1 = __importDefault(require("fs"));
|
|
44
32
|
const path_1 = __importDefault(require("path"));
|
|
45
33
|
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
@@ -48,29 +36,29 @@ const lodash_1 = __importDefault(require("lodash"));
|
|
|
48
36
|
const object_sizeof_1 = __importDefault(require("object-sizeof"));
|
|
49
37
|
const protobufjs = __importStar(require("protobufjs"));
|
|
50
38
|
const uuid_1 = require("uuid");
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const grpcLoaderOptions = Object.assign(Object.assign({}, constants_js_1.DEFAULT_PROTO_LOADER_OPTIONS), { includeDirs: [path_1.default.join(package_root_js_1.packageRoot, 'proto')] });
|
|
39
|
+
const constants_1 = require("../constants");
|
|
40
|
+
const common_1 = require("../models/common");
|
|
41
|
+
const common_2 = require("../utils/common");
|
|
42
|
+
const grpc_1 = require("../utils/grpc");
|
|
43
|
+
const grpc_reflection_1 = require("../utils/grpc-reflection");
|
|
44
|
+
const parse_error_1 = require("../utils/parse-error");
|
|
45
|
+
const proto_path_resolver_1 = require("../utils/proto-path-resolver");
|
|
46
|
+
const redact_sensitive_headers_1 = require("../utils/redact-sensitive-headers");
|
|
47
|
+
const validate_1 = require("../utils/validate");
|
|
48
|
+
const grpcLoaderOptions = Object.assign(Object.assign({}, constants_1.DEFAULT_PROTO_LOADER_OPTIONS), { includeDirs: [path_1.default.join(__dirname, '../../proto')] });
|
|
62
49
|
function createRoot(includeGrpcPaths) {
|
|
63
50
|
const root = new protobufjs.Root();
|
|
64
|
-
root.loadSync(path_1.default.resolve(
|
|
65
|
-
root.loadSync(path_1.default.resolve(
|
|
66
|
-
root.loadSync(path_1.default.resolve(
|
|
51
|
+
root.loadSync(path_1.default.resolve(__dirname, '../../proto/google/rpc/code.proto'));
|
|
52
|
+
root.loadSync(path_1.default.resolve(__dirname, '../../proto/google/rpc/error_details.proto'));
|
|
53
|
+
root.loadSync(path_1.default.resolve(__dirname, '../../proto/google/rpc/status.proto'));
|
|
67
54
|
// Load well-known internal protobufjs types
|
|
68
55
|
root.loadSync('google/protobuf/struct.proto');
|
|
69
56
|
root.loadSync('google/protobuf/wrappers.proto');
|
|
70
57
|
grpcLoaderOptions.includeDirs = [...grpcLoaderOptions.includeDirs, ...(includeGrpcPaths !== null && includeGrpcPaths !== void 0 ? includeGrpcPaths : [])];
|
|
71
|
-
(0,
|
|
58
|
+
(0, proto_path_resolver_1.patchProtoPathResolver)(root, grpcLoaderOptions.includeDirs);
|
|
72
59
|
return root;
|
|
73
60
|
}
|
|
61
|
+
exports.createRoot = createRoot;
|
|
74
62
|
function getCredentialsMap(caCertificatePath) {
|
|
75
63
|
let certificate;
|
|
76
64
|
if (caCertificatePath && fs_1.default.existsSync(caCertificatePath)) {
|
|
@@ -82,6 +70,7 @@ function getCredentialsMap(caCertificatePath) {
|
|
|
82
70
|
insecure: grpc.ChannelCredentials.createInsecure(),
|
|
83
71
|
};
|
|
84
72
|
}
|
|
73
|
+
exports.getCredentialsMap = getCredentialsMap;
|
|
85
74
|
function decodeResponse(response, packageRoot, ctx, encodedFields = [], ErrorConstructor, decodeAnyMessageProtoLoaderOptions) {
|
|
86
75
|
const systemFields = ['metadata', 'response', 'error.details'];
|
|
87
76
|
[...systemFields, ...encodedFields].forEach((fieldName) => {
|
|
@@ -89,21 +78,21 @@ function decodeResponse(response, packageRoot, ctx, encodedFields = [], ErrorCon
|
|
|
89
78
|
const parsedFieldName = fieldName.replace(/\.\*$/, '');
|
|
90
79
|
const fieldValue = lodash_1.default.get(response, parsedFieldName);
|
|
91
80
|
if (fieldValue) {
|
|
92
|
-
lodash_1.default.set(response, parsedFieldName, (0,
|
|
81
|
+
lodash_1.default.set(response, parsedFieldName, (0, grpc_1.decodeAnyMessageRecursively)(packageRoot, fieldValue, decodeAnyMessageProtoLoaderOptions));
|
|
93
82
|
}
|
|
94
83
|
}
|
|
95
84
|
catch (error) {
|
|
96
|
-
(0,
|
|
85
|
+
(0, common_2.handleError)(ErrorConstructor, error, ctx, 'Message decoding failed', { fieldName });
|
|
97
86
|
}
|
|
98
87
|
});
|
|
99
88
|
}
|
|
100
89
|
function createMetadata({ options, actionConfig, config, params, serviceName, ctx, }) {
|
|
101
90
|
var _a;
|
|
102
91
|
const { headers, requestId, authArgs } = actionConfig;
|
|
103
|
-
const proxyHeaders = [...
|
|
92
|
+
const proxyHeaders = [...constants_1.DEFAULT_PROXY_HEADERS];
|
|
104
93
|
let metadata = {
|
|
105
94
|
'x-request-id': requestId,
|
|
106
|
-
'accept-language': headers[
|
|
95
|
+
'accept-language': headers[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru,
|
|
107
96
|
};
|
|
108
97
|
if (typeof options.proxyHeaders === 'function') {
|
|
109
98
|
Object.assign(metadata, options.proxyHeaders(Object.assign({}, headers), 'grpc'));
|
|
@@ -149,7 +138,7 @@ function createMetadata({ options, actionConfig, config, params, serviceName, ct
|
|
|
149
138
|
return serviceMetadata;
|
|
150
139
|
}
|
|
151
140
|
function createActionEndpoint(endpointData) {
|
|
152
|
-
return (0,
|
|
141
|
+
return (0, common_2.isExtendedActionEndpoint)(endpointData) ? endpointData.path : endpointData;
|
|
153
142
|
}
|
|
154
143
|
const packageObjectsMap = new Map();
|
|
155
144
|
const serviceInstancesMap = {};
|
|
@@ -181,7 +170,7 @@ function clearInstancesCache(service, instancesMap, cachePath, closeTimeout, ctx
|
|
|
181
170
|
function getChannelCredential(config, endpointData, credentials) {
|
|
182
171
|
let endpointInsecure;
|
|
183
172
|
let endpointSecureWithoutRootCert;
|
|
184
|
-
if ((0,
|
|
173
|
+
if ((0, common_2.isExtendedGrpcActionEndpoint)(endpointData)) {
|
|
185
174
|
endpointInsecure = endpointData === null || endpointData === void 0 ? void 0 : endpointData.insecure;
|
|
186
175
|
endpointSecureWithoutRootCert = endpointData === null || endpointData === void 0 ? void 0 : endpointData.secureWithoutRootCert;
|
|
187
176
|
}
|
|
@@ -230,7 +219,7 @@ async function refreshCache(actionEndpoint, config, endpointData, grpcOptions, c
|
|
|
230
219
|
}
|
|
231
220
|
}
|
|
232
221
|
function getServiceInstanceReflectCached(config, endpointData, grpcOptions, credentials) {
|
|
233
|
-
if (config.reflection ===
|
|
222
|
+
if (config.reflection === common_1.GrpcReflection.OnEveryRequest) {
|
|
234
223
|
return getServiceInstanceReflect(config, endpointData, grpcOptions, credentials);
|
|
235
224
|
}
|
|
236
225
|
const actionEndpoint = createActionEndpoint(endpointData);
|
|
@@ -252,24 +241,24 @@ function getServiceInstanceReflectCached(config, endpointData, grpcOptions, cred
|
|
|
252
241
|
}
|
|
253
242
|
async function getServiceInstanceReflect(config, endpointData, grpcOptions, credentials, isRefreshCache) {
|
|
254
243
|
const actionEndpoint = createActionEndpoint(endpointData);
|
|
255
|
-
const endpointInsecure = (0,
|
|
244
|
+
const endpointInsecure = (0, common_2.isExtendedGrpcActionEndpoint)(endpointData)
|
|
256
245
|
? endpointData === null || endpointData === void 0 ? void 0 : endpointData.insecure
|
|
257
246
|
: undefined;
|
|
258
247
|
const isInsecure = config.insecure || endpointInsecure;
|
|
259
248
|
const creds = isInsecure ? credentials.insecure : credentials.secure;
|
|
260
|
-
const endpointGrpcOptions = (0,
|
|
249
|
+
const endpointGrpcOptions = (0, common_2.isExtendedGrpcActionEndpoint)(endpointData)
|
|
261
250
|
? endpointData.grpcOptions || {}
|
|
262
251
|
: {};
|
|
263
|
-
const combinedGrpcOptions = Object.assign(Object.assign(Object.assign({},
|
|
252
|
+
const combinedGrpcOptions = Object.assign(Object.assign(Object.assign({}, constants_1.DEFAULT_GRPC_OPTIONS), grpcOptions), endpointGrpcOptions);
|
|
264
253
|
let loadedRoot;
|
|
265
|
-
if (config.reflection ===
|
|
266
|
-
loadedRoot = await (0,
|
|
254
|
+
if (config.reflection === common_1.GrpcReflection.OnEveryRequest || isRefreshCache) {
|
|
255
|
+
loadedRoot = await (0, grpc_reflection_1.getReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions, isRefreshCache);
|
|
267
256
|
}
|
|
268
257
|
else {
|
|
269
|
-
loadedRoot = await (0,
|
|
258
|
+
loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions);
|
|
270
259
|
}
|
|
271
260
|
const descriptor = loadedRoot.toDescriptor('proto3');
|
|
272
|
-
const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor,
|
|
261
|
+
const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor, constants_1.DEFAULT_PROTO_LOADER_OPTIONS);
|
|
273
262
|
const packageObject = grpc.loadPackageDefinition(definition);
|
|
274
263
|
const Service = lodash_1.default.get(packageObject, config.protoKey);
|
|
275
264
|
const serviceInstance = new Service(actionEndpoint, creds, combinedGrpcOptions);
|
|
@@ -303,10 +292,10 @@ async function getServiceInstance(root, config, endpointData, grpcOptions, crede
|
|
|
303
292
|
const packageObject = loadAndCachePackageObject(root, config.protoPath);
|
|
304
293
|
const Service = lodash_1.default.get(packageObject, config.protoKey);
|
|
305
294
|
const creds = getChannelCredential(config, endpointData, credentials);
|
|
306
|
-
const endpointGrpcOptions = (0,
|
|
295
|
+
const endpointGrpcOptions = (0, common_2.isExtendedGrpcActionEndpoint)(endpointData)
|
|
307
296
|
? endpointData.grpcOptions || {}
|
|
308
297
|
: {};
|
|
309
|
-
serviceInstance = new Service(actionEndpoint, creds, Object.assign(Object.assign(Object.assign({},
|
|
298
|
+
serviceInstance = new Service(actionEndpoint, creds, Object.assign(Object.assign(Object.assign({}, constants_1.DEFAULT_GRPC_OPTIONS), grpcOptions), endpointGrpcOptions));
|
|
310
299
|
// Save pointer to service in cache
|
|
311
300
|
lodash_1.default.set(serviceInstancesMap, cacheKey, serviceInstance);
|
|
312
301
|
}
|
|
@@ -333,7 +322,7 @@ async function getResponseData({ config, response, ctx, packageRoot, args, Error
|
|
|
333
322
|
ctx.log('Transformed response data');
|
|
334
323
|
}
|
|
335
324
|
catch (error) {
|
|
336
|
-
(0,
|
|
325
|
+
(0, common_2.handleError)(ErrorConstructor, error, ctx, 'Transform response data failed');
|
|
337
326
|
}
|
|
338
327
|
}
|
|
339
328
|
return responseData;
|
|
@@ -373,8 +362,8 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
373
362
|
if (endpointData) {
|
|
374
363
|
const actionEndpoint = createActionEndpoint(endpointData);
|
|
375
364
|
if ('reflection' in config &&
|
|
376
|
-
(config.reflection ===
|
|
377
|
-
config.reflection ===
|
|
365
|
+
(config.reflection === common_1.GrpcReflection.OnEveryRequest ||
|
|
366
|
+
config.reflection === common_1.GrpcReflection.OnFirstRequest)) {
|
|
378
367
|
getService = () => getServiceInstanceReflectCached(config, endpointData, grpcOptions, credentials);
|
|
379
368
|
recreateService = (service, closeTimeout, ctx) => {
|
|
380
369
|
const cachePath = [config.protoKey, actionEndpoint];
|
|
@@ -398,7 +387,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
398
387
|
var _a;
|
|
399
388
|
const { args, requestId, headers, ctx: parentCtx, userId } = actionConfig;
|
|
400
389
|
const { action } = config;
|
|
401
|
-
const lang = headers[
|
|
390
|
+
const lang = headers[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru; // header might be empty string
|
|
402
391
|
const ctx = parentCtx.create(`Gateway ${serviceName} ${actionName} [grpc]`, {
|
|
403
392
|
tags: {
|
|
404
393
|
action: actionName,
|
|
@@ -423,7 +412,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
423
412
|
'x-api-request-protokey': config.protoKey,
|
|
424
413
|
'x-api-request-lang': lang,
|
|
425
414
|
'x-request-id': requestId,
|
|
426
|
-
'x-gateway-version':
|
|
415
|
+
'x-gateway-version': constants_1.VERSION,
|
|
427
416
|
};
|
|
428
417
|
if ('protoPath' in config) {
|
|
429
418
|
debugHeaders['x-api-request-protopath'] = config.protoPath;
|
|
@@ -438,10 +427,10 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
438
427
|
}
|
|
439
428
|
}
|
|
440
429
|
}
|
|
441
|
-
ctx.log('Initiating request', { debugHeaders: (0,
|
|
430
|
+
ctx.log('Initiating request', { debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders) });
|
|
442
431
|
const sendStats = (status, data) => {
|
|
443
432
|
if (options === null || options === void 0 ? void 0 : options.sendStats) {
|
|
444
|
-
options.sendStats(Object.assign(Object.assign({}, data), { restStatus: status }), (0,
|
|
433
|
+
options.sendStats(Object.assign(Object.assign({}, data), { restStatus: status }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders) });
|
|
445
434
|
}
|
|
446
435
|
else {
|
|
447
436
|
ctx.stats(Object.assign(Object.assign({}, requestData), { responseStatus: status }));
|
|
@@ -452,7 +441,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
452
441
|
const restStatus = lodash_1.default.get(grpcError.getGatewayError(), 'status', 500);
|
|
453
442
|
sendStats(restStatus, Object.assign(Object.assign({}, requestData), { responseSize: (_a = grpcError.getRawError()) === null || _a === void 0 ? void 0 : _a.metadata, grpcStatus: grpcError.getGatewayError().code }));
|
|
454
443
|
ctx.logError('Request failed', ErrorConstructor.wrap(grpcError.getAppError(ErrorConstructor)), Object.assign({ serviceName,
|
|
455
|
-
actionName, debugHeaders: (0,
|
|
444
|
+
actionName, debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders) }, grpcError.getGatewayError()));
|
|
456
445
|
ctx.end();
|
|
457
446
|
}
|
|
458
447
|
let params;
|
|
@@ -461,7 +450,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
461
450
|
params = await config.params(args, headers, { ctx });
|
|
462
451
|
}
|
|
463
452
|
catch (error) {
|
|
464
|
-
(0,
|
|
453
|
+
(0, common_2.handleError)(ErrorConstructor, error, ctx, 'Getting config params failed');
|
|
465
454
|
}
|
|
466
455
|
}
|
|
467
456
|
let service;
|
|
@@ -469,7 +458,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
469
458
|
service = await getService(args);
|
|
470
459
|
}
|
|
471
460
|
catch (error) {
|
|
472
|
-
(0,
|
|
461
|
+
(0, common_2.handleError)(ErrorConstructor, error, ctx, 'getService failed');
|
|
473
462
|
throw error;
|
|
474
463
|
}
|
|
475
464
|
// eslint-disable-next-line complexity
|
|
@@ -484,7 +473,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
484
473
|
}
|
|
485
474
|
if (!endpointData) {
|
|
486
475
|
const errorText = `Gateway config error. Endpoint has been not found in service "${serviceKey}"`;
|
|
487
|
-
throw new
|
|
476
|
+
throw new parse_error_1.GrpcError(errorText, {
|
|
488
477
|
status: 400,
|
|
489
478
|
code: 'ENDPOINT_NOT_FOUND',
|
|
490
479
|
message: errorText,
|
|
@@ -493,9 +482,9 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
493
482
|
const actionEndpoint = getActionEndpoint(args);
|
|
494
483
|
debugHeaders['x-api-request-endpoint'] = actionEndpoint;
|
|
495
484
|
const validationSchema = config.validationSchema || options.validationSchema;
|
|
496
|
-
const invalidParams = validationSchema ? (0,
|
|
485
|
+
const invalidParams = validationSchema ? (0, validate_1.validateArgs)(args, validationSchema) : false;
|
|
497
486
|
if (invalidParams) {
|
|
498
|
-
throw new
|
|
487
|
+
throw new parse_error_1.GrpcError('Invalid params', {
|
|
499
488
|
status: 400,
|
|
500
489
|
code: 'INVALID_PARAMS',
|
|
501
490
|
message: 'Validation failed',
|
|
@@ -505,7 +494,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
505
494
|
},
|
|
506
495
|
});
|
|
507
496
|
}
|
|
508
|
-
const timeout = (_c = (_b = (_a = actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.timeout) !== null && _a !== void 0 ? _a : config === null || config === void 0 ? void 0 : config.timeout) !== null && _b !== void 0 ? _b : options === null || options === void 0 ? void 0 : options.timeout) !== null && _c !== void 0 ? _c :
|
|
497
|
+
const timeout = (_c = (_b = (_a = actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.timeout) !== null && _a !== void 0 ? _a : config === null || config === void 0 ? void 0 : config.timeout) !== null && _b !== void 0 ? _b : options === null || options === void 0 ? void 0 : options.timeout) !== null && _c !== void 0 ? _c : constants_1.DEFAULT_TIMEOUT;
|
|
509
498
|
const serviceOptions = {
|
|
510
499
|
deadline: Date.now() + timeout,
|
|
511
500
|
};
|
|
@@ -519,7 +508,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
519
508
|
ctx,
|
|
520
509
|
});
|
|
521
510
|
if (!service[action]) {
|
|
522
|
-
reject(new
|
|
511
|
+
reject(new parse_error_1.GrpcError('Not found action', {
|
|
523
512
|
status: 400,
|
|
524
513
|
code: 'GRPC_ACTION_NOT_FOUND',
|
|
525
514
|
message: `Not found action ${action} in ${serviceKey}`,
|
|
@@ -529,22 +518,22 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
529
518
|
switch (config.type) {
|
|
530
519
|
case 'serverStream': {
|
|
531
520
|
ctx.log('Creating serverStream request', {
|
|
532
|
-
debugHeaders: (0,
|
|
521
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
533
522
|
});
|
|
534
523
|
const actionCall = service[action].bind(service);
|
|
535
524
|
const stream = actionCall(body, serviceMetadata, serviceOptions);
|
|
536
525
|
stream.on('error', (error) => {
|
|
537
526
|
ctx.log('ServerStream error', {
|
|
538
|
-
debugHeaders: (0,
|
|
527
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
539
528
|
});
|
|
540
|
-
processError(new
|
|
529
|
+
processError(new parse_error_1.GrpcError('ClientReadableStream error', (0, parse_error_1.parseGrpcError)(error, root, lang, config.decodeAnyMessageProtoLoaderOptions), error));
|
|
541
530
|
});
|
|
542
531
|
stream.on('status', (status) => {
|
|
543
532
|
ctx.log('ServerStream status changed', status);
|
|
544
533
|
});
|
|
545
534
|
stream.on('end', () => {
|
|
546
535
|
ctx.log('ServerStream request completed', {
|
|
547
|
-
debugHeaders: (0,
|
|
536
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
548
537
|
});
|
|
549
538
|
ctx.end();
|
|
550
539
|
});
|
|
@@ -553,10 +542,10 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
553
542
|
}
|
|
554
543
|
case 'clientStream': {
|
|
555
544
|
ctx.log('Creating clientStream request', {
|
|
556
|
-
debugHeaders: (0,
|
|
545
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
557
546
|
});
|
|
558
547
|
if (!actionConfig.callback) {
|
|
559
|
-
throw new
|
|
548
|
+
throw new parse_error_1.GrpcError('Invalid action type', {
|
|
560
549
|
status: 400,
|
|
561
550
|
code: 'ACTION_CALLBACK_REQUIRED',
|
|
562
551
|
message: `Client stream actions require callback function`,
|
|
@@ -569,22 +558,22 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
569
558
|
}
|
|
570
559
|
case 'bidi': {
|
|
571
560
|
ctx.log('Creating serverStream request', {
|
|
572
|
-
debugHeaders: (0,
|
|
561
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
573
562
|
});
|
|
574
563
|
const actionCall = service[action].bind(service);
|
|
575
564
|
const stream = actionCall(serviceMetadata, serviceOptions);
|
|
576
565
|
stream.on('error', (error) => {
|
|
577
566
|
ctx.log('BidiStream error', {
|
|
578
|
-
debugHeaders: (0,
|
|
567
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
579
568
|
});
|
|
580
|
-
processError(new
|
|
569
|
+
processError(new parse_error_1.GrpcError('BidiStream error', (0, parse_error_1.parseGrpcError)(error, root, lang, config.decodeAnyMessageProtoLoaderOptions), error));
|
|
581
570
|
});
|
|
582
571
|
stream.on('status', (status) => {
|
|
583
572
|
ctx.log('BidiStream status changed', status);
|
|
584
573
|
});
|
|
585
574
|
stream.on('end', () => {
|
|
586
575
|
ctx.log('BidiStream request completed', {
|
|
587
|
-
debugHeaders: (0,
|
|
576
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
588
577
|
});
|
|
589
578
|
ctx.end();
|
|
590
579
|
});
|
|
@@ -596,19 +585,22 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
596
585
|
}
|
|
597
586
|
default: {
|
|
598
587
|
ctx.log('Starting unary request', {
|
|
599
|
-
debugHeaders: (0,
|
|
588
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
600
589
|
});
|
|
601
590
|
let retries = (_d = config.retries) !== null && _d !== void 0 ? _d : 0;
|
|
602
591
|
let actionCall = service[action].bind(service);
|
|
603
592
|
const callAction = () => {
|
|
604
593
|
let trailingMetadata = {};
|
|
605
594
|
const call = actionCall(body, serviceMetadata, serviceOptions, async (error, response) => {
|
|
595
|
+
var _a, _b;
|
|
606
596
|
const endRequestTime = Date.now();
|
|
607
597
|
requestData.requestTime = endRequestTime - startRequestTime;
|
|
608
598
|
const shouldRecreateService = error &&
|
|
609
599
|
options.grpcRecreateService &&
|
|
610
|
-
(0,
|
|
611
|
-
const shouldRetry = error &&
|
|
600
|
+
(0, grpc_1.isRecreateServiceError)(error);
|
|
601
|
+
const shouldRetry = error &&
|
|
602
|
+
retries &&
|
|
603
|
+
((_b = (_a = options.grpcRetryCondition) === null || _a === void 0 ? void 0 : _a.call(options, error)) !== null && _b !== void 0 ? _b : (0, grpc_1.isRetryableError)(error));
|
|
612
604
|
if (shouldRecreateService) {
|
|
613
605
|
ctx.log(`Service client for ${config.protoKey} is going to be re-created`);
|
|
614
606
|
recreateService(service, timeout * 1.5, ctx, args);
|
|
@@ -620,7 +612,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
620
612
|
service = await getService(args);
|
|
621
613
|
}
|
|
622
614
|
catch (error) {
|
|
623
|
-
(0,
|
|
615
|
+
(0, common_2.handleError)(ErrorConstructor, error, ctx, 'getService failed');
|
|
624
616
|
throw error;
|
|
625
617
|
}
|
|
626
618
|
// Update service
|
|
@@ -629,7 +621,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
629
621
|
return;
|
|
630
622
|
}
|
|
631
623
|
if (error) {
|
|
632
|
-
reject(new
|
|
624
|
+
reject(new parse_error_1.GrpcError('gRPC request error', (0, parse_error_1.parseGrpcError)(error, root, lang, config.decodeAnyMessageProtoLoaderOptions), error));
|
|
633
625
|
return;
|
|
634
626
|
}
|
|
635
627
|
const responseData = await getResponseData({
|
|
@@ -644,7 +636,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
644
636
|
const responseHeaders = {};
|
|
645
637
|
if (config.proxyResponseHeaders) {
|
|
646
638
|
const proxyResponseHeaders = [];
|
|
647
|
-
const headersFromMetadata = (0,
|
|
639
|
+
const headersFromMetadata = (0, common_2.getHeadersFromMetadata)(trailingMetadata);
|
|
648
640
|
if (typeof config.proxyResponseHeaders === 'function') {
|
|
649
641
|
Object.assign(responseHeaders, config.proxyResponseHeaders(headersFromMetadata, 'grpc'));
|
|
650
642
|
}
|
|
@@ -658,10 +650,10 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
658
650
|
}
|
|
659
651
|
}
|
|
660
652
|
}
|
|
661
|
-
Object.assign(debugHeaders, (0,
|
|
653
|
+
Object.assign(debugHeaders, (0, common_2.getHeadersFromMetadata)(trailingMetadata, 'x-metadata-'));
|
|
662
654
|
sendStats(200, Object.assign(Object.assign({}, requestData), { responseSize: (0, object_sizeof_1.default)(response), grpcStatus: 0 }));
|
|
663
655
|
ctx.log('Request completed', {
|
|
664
|
-
debugHeaders: (0,
|
|
656
|
+
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
665
657
|
});
|
|
666
658
|
ctx.end();
|
|
667
659
|
return resolve({ responseData, responseHeaders, debugHeaders });
|
|
@@ -674,9 +666,10 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
674
666
|
}
|
|
675
667
|
}
|
|
676
668
|
}).catch((error) => {
|
|
677
|
-
const grpcError = (0,
|
|
669
|
+
const grpcError = (0, parse_error_1.isGrpcError)(error) ? error : (0, parse_error_1.grpcErrorFactory)(error);
|
|
678
670
|
processError(grpcError);
|
|
679
671
|
return Promise.reject({ error: grpcError.getGatewayError(), debugHeaders });
|
|
680
672
|
});
|
|
681
673
|
};
|
|
682
674
|
}
|
|
675
|
+
exports.default = createGrpcAction;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ApiActionConfig, ApiByScope, ApiServiceMixedActionConfig, GatewayConfig, GatewayRequest, GatewayResponse, SchemasByScope } from '../models/common
|
|
2
|
-
import { GatewayContext } from '../models/context
|
|
3
|
-
import { AppErrorConstructor } from '../models/error
|
|
4
|
-
import type { GrpcContext } from './grpc
|
|
1
|
+
import { ApiActionConfig, ApiByScope, ApiServiceMixedActionConfig, GatewayConfig, GatewayRequest, GatewayResponse, SchemasByScope } from '../models/common';
|
|
2
|
+
import { GatewayContext } from '../models/context';
|
|
3
|
+
import { AppErrorConstructor } from '../models/error';
|
|
4
|
+
import type { GrpcContext } from './grpc';
|
|
5
5
|
export declare function createMixedAction<TSchema extends SchemasByScope, Context extends GatewayContext, Req extends GatewayRequest<Context>, Res extends GatewayResponse>(config: ApiServiceMixedActionConfig<Context, Req, Res, any, any, any>, api: ApiByScope<TSchema, Context, Req, Res>, serviceName: string, actionName: string, extra: {
|
|
6
6
|
config: GatewayConfig<Context, Req, Res>;
|
|
7
7
|
grpcContext: GrpcContext;
|
|
@@ -11,11 +11,11 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.createMixedAction =
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
14
|
+
exports.createMixedAction = void 0;
|
|
15
|
+
const constants_1 = require("../constants");
|
|
16
|
+
const common_1 = require("../utils/common");
|
|
17
|
+
const create_context_api_1 = require("../utils/create-context-api");
|
|
18
|
+
const parse_error_1 = require("../utils/parse-error");
|
|
19
19
|
function createMixedAction(config, api, serviceName, actionName, extra, ErrorConstructor) {
|
|
20
20
|
return async (actionConfig) => {
|
|
21
21
|
const { args } = actionConfig, context = __rest(actionConfig, ["args"]);
|
|
@@ -26,9 +26,9 @@ function createMixedAction(config, api, serviceName, actionName, extra, ErrorCon
|
|
|
26
26
|
type: 'mixed',
|
|
27
27
|
},
|
|
28
28
|
});
|
|
29
|
-
const contextApi = (0,
|
|
29
|
+
const contextApi = (0, create_context_api_1.generateContextApi)(api, Object.assign(Object.assign({}, context), { ctx }));
|
|
30
30
|
try {
|
|
31
|
-
const responseData = await config(contextApi, args, Object.assign({ headers: actionConfig.headers, lang: actionConfig.headers[
|
|
31
|
+
const responseData = await config(contextApi, args, Object.assign({ headers: actionConfig.headers, lang: actionConfig.headers[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru, ctx }, extra));
|
|
32
32
|
ctx.log('Request completed');
|
|
33
33
|
return {
|
|
34
34
|
responseData,
|
|
@@ -37,15 +37,15 @@ function createMixedAction(config, api, serviceName, actionName, extra, ErrorCon
|
|
|
37
37
|
}
|
|
38
38
|
catch (e) {
|
|
39
39
|
if (e instanceof Object && 'error' in e) {
|
|
40
|
-
(0,
|
|
40
|
+
(0, common_1.handleError)(ErrorConstructor, e, ctx, 'Request failed', {
|
|
41
41
|
actionName,
|
|
42
42
|
serviceName,
|
|
43
43
|
});
|
|
44
44
|
throw e;
|
|
45
45
|
}
|
|
46
46
|
if (e instanceof Error) {
|
|
47
|
-
const parsedError = (0,
|
|
48
|
-
(0,
|
|
47
|
+
const parsedError = (0, parse_error_1.parseMixedError)(e);
|
|
48
|
+
(0, common_1.handleError)(ErrorConstructor, e, ctx, 'Request failed', {
|
|
49
49
|
actionName,
|
|
50
50
|
serviceName,
|
|
51
51
|
});
|
|
@@ -53,7 +53,7 @@ function createMixedAction(config, api, serviceName, actionName, extra, ErrorCon
|
|
|
53
53
|
error: parsedError,
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
|
-
(0,
|
|
56
|
+
(0, common_1.handleError)(ErrorConstructor, e, ctx, 'Request failed');
|
|
57
57
|
throw {
|
|
58
58
|
error: e,
|
|
59
59
|
};
|
|
@@ -63,3 +63,4 @@ function createMixedAction(config, api, serviceName, actionName, extra, ErrorCon
|
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
|
+
exports.createMixedAction = createMixedAction;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ApiActionConfig, ApiServiceRestActionConfig, EndpointsConfig, GatewayApiOptions, Headers } from '../models/common
|
|
2
|
-
import { GatewayContext } from '../models/context
|
|
3
|
-
import { AppErrorConstructor } from '../models/error
|
|
4
|
-
export
|
|
1
|
+
import { ApiActionConfig, ApiServiceRestActionConfig, EndpointsConfig, GatewayApiOptions, Headers } from '../models/common';
|
|
2
|
+
import { GatewayContext } from '../models/context';
|
|
3
|
+
import { AppErrorConstructor } from '../models/error';
|
|
4
|
+
export default function createRestAction<Context extends GatewayContext>(endpoints: EndpointsConfig | undefined, config: ApiServiceRestActionConfig<Context, any, any>, serviceKey: string, actionName: string, options: GatewayApiOptions<Context>, ErrorConstructor: AppErrorConstructor): (actionConfig: ApiActionConfig<Context, any>) => Promise<{
|
|
5
5
|
responseData: unknown;
|
|
6
|
-
responseHeaders?: Headers;
|
|
6
|
+
responseHeaders?: Headers | undefined;
|
|
7
7
|
debugHeaders: Headers;
|
|
8
8
|
}>;
|