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