@tramvai/module-request-limiter 1.94.0 → 1.94.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.
package/lib/server.es.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { Module, provide, Scope } from '@tramvai/core';
3
- import { WEB_FASTIFY_APP_LIMITER_TOKEN, WEB_FASTIFY_APP_TOKEN } from '@tramvai/tokens-server-private';
3
+ import { WEB_FASTIFY_APP_LIMITER_TOKEN } from '@tramvai/tokens-server-private';
4
4
  import fp from 'fastify-plugin';
5
5
  import { HttpError } from '@tinkoff/errors';
6
+ import onFinished from 'on-finished';
6
7
  import { monitorEventLoopDelay } from 'perf_hooks';
7
8
  import { createToken } from '@tinkoff/dippy';
8
9
 
@@ -126,8 +127,15 @@ class RequestLimiter {
126
127
  this.run(this.queue.pop());
127
128
  }
128
129
  }
129
- run({ next }) {
130
+ run({ next, res }) {
130
131
  this.currentActive++;
132
+ // onFinished doesn't work OK in DEV mode. Just stuck with high load without any reasons
133
+ // fastify: it only works this way with on-finished, as using hook `onRequest` is not enough to handle all of the requests
134
+ // and some of the failed requests are getting disappeared
135
+ // and other hooks useless as well. [related issue](https://github.com/fastify/fastify/issues/1352)
136
+ onFinished(res.raw, () => {
137
+ this.onResponse();
138
+ });
131
139
  next();
132
140
  }
133
141
  }
@@ -135,9 +143,6 @@ const fastifyRequestsLimiter = fp(async (fastify, { requestsLimiter }) => {
135
143
  fastify.addHook('onRequest', (req, res, next) => {
136
144
  requestsLimiter.add({ req, res, next });
137
145
  });
138
- fastify.addHook('onResponse', async () => {
139
- requestsLimiter.onResponse();
140
- });
141
146
  });
142
147
 
143
148
  const REQUESTS_LIMITER_TOKEN = createToken('requestsLimiterToken');
@@ -166,16 +171,15 @@ RequestLimiterModule = __decorate([
166
171
  provide({
167
172
  provide: WEB_FASTIFY_APP_LIMITER_TOKEN,
168
173
  multi: true,
169
- useFactory: ({ app, requestsLimiter, featureEnable }) => {
170
- return async () => {
174
+ useFactory: ({ requestsLimiter, featureEnable, }) => {
175
+ return async (app) => {
171
176
  if (featureEnable !== true) {
172
177
  return;
173
178
  }
174
- await app.register(fastifyRequestsLimiter, { requestsLimiter });
179
+ await fastifyRequestsLimiter(app, { requestsLimiter });
175
180
  };
176
181
  },
177
182
  deps: {
178
- app: WEB_FASTIFY_APP_TOKEN,
179
183
  requestsLimiter: REQUESTS_LIMITER_TOKEN,
180
184
  featureEnable: { token: REQUESTS_LIMITER_ACTIVATE_TOKEN, optional: true },
181
185
  },
package/lib/server.js CHANGED
@@ -7,12 +7,14 @@ var core = require('@tramvai/core');
7
7
  var tokensServerPrivate = require('@tramvai/tokens-server-private');
8
8
  var fp = require('fastify-plugin');
9
9
  var errors = require('@tinkoff/errors');
10
+ var onFinished = require('on-finished');
10
11
  var perf_hooks = require('perf_hooks');
11
12
  var dippy = require('@tinkoff/dippy');
12
13
 
13
14
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
15
 
15
16
  var fp__default = /*#__PURE__*/_interopDefaultLegacy(fp);
17
+ var onFinished__default = /*#__PURE__*/_interopDefaultLegacy(onFinished);
16
18
 
17
19
  class DoubleLinkedList {
18
20
  constructor() {
@@ -134,8 +136,15 @@ class RequestLimiter {
134
136
  this.run(this.queue.pop());
135
137
  }
136
138
  }
137
- run({ next }) {
139
+ run({ next, res }) {
138
140
  this.currentActive++;
141
+ // onFinished doesn't work OK in DEV mode. Just stuck with high load without any reasons
142
+ // fastify: it only works this way with on-finished, as using hook `onRequest` is not enough to handle all of the requests
143
+ // and some of the failed requests are getting disappeared
144
+ // and other hooks useless as well. [related issue](https://github.com/fastify/fastify/issues/1352)
145
+ onFinished__default["default"](res.raw, () => {
146
+ this.onResponse();
147
+ });
139
148
  next();
140
149
  }
141
150
  }
@@ -143,9 +152,6 @@ const fastifyRequestsLimiter = fp__default["default"](async (fastify, { requests
143
152
  fastify.addHook('onRequest', (req, res, next) => {
144
153
  requestsLimiter.add({ req, res, next });
145
154
  });
146
- fastify.addHook('onResponse', async () => {
147
- requestsLimiter.onResponse();
148
- });
149
155
  });
150
156
 
151
157
  const REQUESTS_LIMITER_TOKEN = dippy.createToken('requestsLimiterToken');
@@ -174,16 +180,15 @@ exports.RequestLimiterModule = tslib.__decorate([
174
180
  core.provide({
175
181
  provide: tokensServerPrivate.WEB_FASTIFY_APP_LIMITER_TOKEN,
176
182
  multi: true,
177
- useFactory: ({ app, requestsLimiter, featureEnable }) => {
178
- return async () => {
183
+ useFactory: ({ requestsLimiter, featureEnable, }) => {
184
+ return async (app) => {
179
185
  if (featureEnable !== true) {
180
186
  return;
181
187
  }
182
- await app.register(fastifyRequestsLimiter, { requestsLimiter });
188
+ await fastifyRequestsLimiter(app, { requestsLimiter });
183
189
  };
184
190
  },
185
191
  deps: {
186
- app: tokensServerPrivate.WEB_FASTIFY_APP_TOKEN,
187
192
  requestsLimiter: REQUESTS_LIMITER_TOKEN,
188
193
  featureEnable: { token: REQUESTS_LIMITER_ACTIVATE_TOKEN, optional: true },
189
194
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-request-limiter",
3
- "version": "1.94.0",
3
+ "version": "1.94.1",
4
4
  "description": "Enable different rendering modes for pages",
5
5
  "main": "lib/server.js",
6
6
  "module": "lib/server.es.js",
@@ -25,13 +25,14 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@tinkoff/errors": "0.2.20",
28
- "fastify-plugin": "^3.0.1"
28
+ "fastify-plugin": "^3.0.1",
29
+ "on-finished": "^2.3.0"
29
30
  },
30
31
  "devDependencies": {},
31
32
  "peerDependencies": {
32
33
  "@tinkoff/dippy": "0.7.39",
33
- "@tramvai/core": "1.94.0",
34
- "@tramvai/tokens-server-private": "1.94.0",
34
+ "@tramvai/core": "1.94.1",
35
+ "@tramvai/tokens-server-private": "1.94.1",
35
36
  "fastify": "^3.0.0",
36
37
  "tslib": "^2.0.3"
37
38
  }