@gravity-ui/gateway 4.11.0 → 4.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ const constants_1 = require("../constants");
11
11
  const axios_1 = require("../utils/axios");
12
12
  const common_1 = require("../utils/common");
13
13
  const parse_error_1 = require("../utils/parse-error");
14
+ const pipe_through_byte_counter_1 = require("../utils/pipe-through-byte-counter");
14
15
  const redact_sensitive_headers_1 = require("../utils/redact-sensitive-headers");
15
16
  const validate_1 = require("../utils/validate");
16
17
  function getRestResponseSize(data, ctx, ErrorConstructor) {
@@ -306,7 +307,16 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
306
307
  }
307
308
  }
308
309
  if (options === null || options === void 0 ? void 0 : options.sendStats) {
309
- options.sendStats(Object.assign(Object.assign({}, requestData), { responseSize: getRestResponseSize(response === null || response === void 0 ? void 0 : response.data, ctx, ErrorConstructor), restStatus: 200 }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
310
+ const emitStats = (responseSize, restStatus) => {
311
+ var _a;
312
+ return (_a = options.sendStats) === null || _a === void 0 ? void 0 : _a.call(options, Object.assign(Object.assign({}, requestData), { responseSize, restStatus }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
313
+ };
314
+ if (response.data && typeof response.data.pipe === 'function') {
315
+ response.data = (0, pipe_through_byte_counter_1.pipeThroughByteCounter)(response.data, (streamedBytes) => emitStats(streamedBytes, 200));
316
+ }
317
+ else {
318
+ emitStats(getRestResponseSize(response === null || response === void 0 ? void 0 : response.data, ctx, ErrorConstructor), 200);
319
+ }
310
320
  }
311
321
  else {
312
322
  ctx.stats(Object.assign(Object.assign({}, requestData), { responseStatus: 200 }));
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { Readable } from 'stream';
4
+ export declare function pipeThroughByteCounter(source: NodeJS.ReadableStream, onFlush: (streamedBytes: number) => void): Readable;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pipeThroughByteCounter = void 0;
4
+ const stream_1 = require("stream");
5
+ function pipeThroughByteCounter(source, onFlush) {
6
+ let streamedBytes = 0;
7
+ const counter = new stream_1.Transform({
8
+ transform(chunk, encoding, cb) {
9
+ if (Buffer.isBuffer(chunk) || chunk instanceof Uint8Array) {
10
+ streamedBytes += chunk.length;
11
+ }
12
+ else if (typeof chunk === 'string') {
13
+ streamedBytes += Buffer.byteLength(chunk, typeof encoding === 'string' ? encoding : undefined);
14
+ }
15
+ cb(null, chunk);
16
+ },
17
+ flush(cb) {
18
+ onFlush(streamedBytes);
19
+ cb();
20
+ },
21
+ });
22
+ return source.pipe(counter);
23
+ }
24
+ exports.pipeThroughByteCounter = pipeThroughByteCounter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/gateway",
3
- "version": "4.11.0",
3
+ "version": "4.11.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",