@gravity-ui/gateway 2.5.3-alpha.0 → 2.5.3

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.
@@ -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 reflectLoaderOptions = {
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'));
@@ -270,7 +264,7 @@ async function getServiceInstanceReflect(config, endpointData, grpcOptions, cred
270
264
  loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions);
271
265
  }
272
266
  const descriptor = loadedRoot.toDescriptor('proto3');
273
- const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor, reflectLoaderOptions);
267
+ const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor, constants_1.DEFAULT_PROTO_LOADER_OPTIONS);
274
268
  const packageObject = grpc.loadPackageDefinition(definition);
275
269
  const Service = lodash_1.default.get(packageObject, config.protoKey);
276
270
  const serviceInstance = new Service(actionEndpoint, creds, combinedGrpcOptions);
@@ -396,8 +390,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
396
390
  }
397
391
  }
398
392
  return async function action(actionConfig) {
399
- var _a;
400
- const { args, requestId, headers, ctx: parentCtx, userId } = actionConfig;
393
+ const { args, requestId, headers, ctx: parentCtx } = actionConfig;
401
394
  const { action } = config;
402
395
  const lang = headers[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru; // header might be empty string
403
396
  const ctx = parentCtx.create(`Gateway ${serviceName} ${actionName} [grpc]`, {
@@ -416,8 +409,6 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
416
409
  requestId: actionConfig.requestId,
417
410
  requestMethod: action,
418
411
  requestUrl: config.protoKey,
419
- traceId: ((_a = ctx.getTraceId) === null || _a === void 0 ? void 0 : _a.call(ctx)) || '',
420
- userId: userId || '',
421
412
  };
422
413
  const debugHeaders = {
423
414
  'x-api-request-action': action,
@@ -39,8 +39,8 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
39
39
  const defaultAxiosClient = (0, axios_1.getAxiosClient)(timeout, config === null || config === void 0 ? void 0 : config.retries, options === null || options === void 0 ? void 0 : options.axiosConfig);
40
40
  /* eslint-disable complexity */
41
41
  return async function action(actionConfig) {
42
- var _a, _b, _c, _d, _e, _f, _g, _h;
43
- const { args, requestId, headers: requestHeaders, ctx: parentCtx, authArgs, userId, } = actionConfig;
42
+ var _a, _b, _c, _d, _e, _f, _g;
43
+ const { args, requestId, headers: requestHeaders, ctx: parentCtx, authArgs } = actionConfig;
44
44
  const debugHeaders = {};
45
45
  const lang = requestHeaders[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru; // header might be empty string
46
46
  const serviceName = (options === null || options === void 0 ? void 0 : options.serviceName) || serviceKey;
@@ -210,8 +210,6 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
210
210
  requestId,
211
211
  requestMethod: config.method,
212
212
  requestUrl: actionURL,
213
- traceId: ((_f = ctx.getTraceId) === null || _f === void 0 ? void 0 : _f.call(ctx)) || '',
214
- userId: userId || '',
215
213
  };
216
214
  const requestConfig = {
217
215
  url: actionURL,
@@ -247,7 +245,7 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
247
245
  const responseHeaders = {};
248
246
  const endRequestTime = Date.now();
249
247
  requestData.requestTime = endRequestTime - startRequestTime;
250
- const actualResponseContentType = (_g = response.headers) === null || _g === void 0 ? void 0 : _g['Content-Type'];
248
+ const actualResponseContentType = (_f = response.headers) === null || _f === void 0 ? void 0 : _f['Content-Type'];
251
249
  const expectedResponseContentType = config.expectedResponseContentType || options.expectedResponseContentType;
252
250
  if (actualResponseContentType && expectedResponseContentType) {
253
251
  let isInvalidResponseContentType;
@@ -344,7 +342,7 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
344
342
  }
345
343
  const responseStatus = lodash_1.default.get(parsedError, 'status') || lodash_1.default.get(error, 'status', 500);
346
344
  if (options === null || options === void 0 ? void 0 : options.sendStats) {
347
- options.sendStats(Object.assign(Object.assign({}, requestData), { responseSize: getRestResponseSize((_h = error === null || error === void 0 ? void 0 : error.response) === null || _h === void 0 ? void 0 : _h.data, ctx, ErrorConstructor), restStatus: responseStatus, userId }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
345
+ options.sendStats(Object.assign(Object.assign({}, requestData), { responseSize: getRestResponseSize((_g = error === null || error === void 0 ? void 0 : error.response) === null || _g === void 0 ? void 0 : _g.data, ctx, ErrorConstructor), restStatus: responseStatus }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
348
346
  }
349
347
  else {
350
348
  ctx.stats(Object.assign(Object.assign({}, requestData), { responseStatus }));
@@ -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/
@@ -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/index.js CHANGED
@@ -112,7 +112,6 @@ function generateGatewayApiController(schemasByScope, Api, config, controllerAct
112
112
  // eslint-disable-next-line complexity
113
113
  return async function gateway(req, res) {
114
114
  var _a, _b, _c;
115
- const { userId } = res.locals || {};
116
115
  const { service, action, scope = 'root' } = req.params;
117
116
  const withDebugHeaders = typeof config.withDebugHeaders === 'function'
118
117
  ? config.withDebugHeaders(req, res)
@@ -187,7 +186,6 @@ function generateGatewayApiController(schemasByScope, Api, config, controllerAct
187
186
  ctx: req.ctx,
188
187
  args,
189
188
  authArgs: config.getAuthArgs(req, res),
190
- userId,
191
189
  });
192
190
  if (withDebugHeaders) {
193
191
  res.set(debugHeaders);
@@ -29,7 +29,6 @@ export interface ApiActionConfig<Context extends GatewayContext, TRequestData, T
29
29
  timeout?: number;
30
30
  callback?: (response: TResponseData) => void;
31
31
  authArgs?: Record<string, unknown>;
32
- userId?: string;
33
32
  }
34
33
  export interface GRPCActionData {
35
34
  [key: string]: unknown;
@@ -48,8 +47,6 @@ export interface Stats {
48
47
  requestMethod: string;
49
48
  requestUrl: string;
50
49
  timestamp: number;
51
- userId?: string;
52
- traceId: string;
53
50
  }
54
51
  export type ControllerType = 'rest' | 'grpc';
55
52
  export interface GatewayError {
@@ -19,5 +19,4 @@ export interface GatewayContext {
19
19
  redactSensitiveKeys?: (headers: Dict) => Dict;
20
20
  };
21
21
  getMetadata: () => IncomingHttpHeaders;
22
- getTraceId?: () => string;
23
22
  }
@@ -13,9 +13,14 @@ function decodeAnyMessageRecursively(root, message) {
13
13
  }
14
14
  const typeName = message.type_url.substring(lastSlashIndex + 1);
15
15
  const type = root.lookupType(typeName);
16
- const data = type.decode(message.value).toJSON();
16
+ const data = type.toObject(type.decode(message.value), constants_1.DEFAULT_PROTO_LOADER_OPTIONS);
17
17
  Object.keys(data).forEach((key) => {
18
- data[key] = decodeAnyMessageRecursively(root, data[key]);
18
+ if (Array.isArray(data[key])) {
19
+ data[key] = data[key].map((item) => decodeAnyMessageRecursively(root, item));
20
+ }
21
+ else {
22
+ data[key] = decodeAnyMessageRecursively(root, data[key]);
23
+ }
19
24
  });
20
25
  return data;
21
26
  }
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/gateway",
3
- "version": "2.5.3-alpha.0",
3
+ "version": "2.5.3",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",