@tramvai/module-dns-cache 2.120.0 → 2.121.2

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,12 +1,11 @@
1
1
  import noop from '@tinkoff/utils/function/noop';
2
- import monkeypatch from '@tinkoff/monkeypatch';
3
2
  import http from 'http';
4
3
  import https from 'https';
5
4
  import dns from 'dns';
6
5
  import CacheableLookup from 'cacheable-lookup';
7
6
  import { createToken, declareModule, provide, commandLineListTokens, Scope } from '@tramvai/core';
7
+ import { DEFAULT_HTTP_CLIENT_INTERCEPTORS } from '@tramvai/tokens-http-client';
8
8
  import { ENV_MANAGER_TOKEN, CREATE_CACHE_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
9
- import { getUrlAndOptions } from '@tramvai/module-metrics';
10
9
 
11
10
  const DNS_LOOKUP_CACHE_TOKEN = createToken('dnsLookupCache');
12
11
  const TramvaiDnsCacheModule = declareModule({
@@ -66,37 +65,24 @@ const TramvaiDnsCacheModule = declareModule({
66
65
  },
67
66
  }),
68
67
  provide({
69
- provide: commandLineListTokens.init,
70
- multi: true,
68
+ provide: DEFAULT_HTTP_CLIENT_INTERCEPTORS,
71
69
  useFactory: ({ envManager, cache }) => {
72
- if (envManager.get('DNS_LOOKUP_CACHE_ENABLE') !== 'true') {
73
- return noop;
74
- }
75
- // if dns cache is stale, we can get any error, so it's safe to always clear dns cache on request error,
76
- // except aborted requests and timeouts - this errors shoud be expected
77
- return function addPossibleStaleDnsErrorsHandler() {
78
- function handlePossibleStaleDnsErrors(originalReq, ...args) {
79
- // @ts-expect-error
80
- const req = originalReq.apply(this, args);
81
- req.on('error', (e) => {
82
- if ((e === null || e === void 0 ? void 0 : e.type) === 'aborted' || (e === null || e === void 0 ? void 0 : e.code) === 'ETIMEDOUT') {
83
- return;
70
+ const dnsLookupEnabled = envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
71
+ return (req, next) => {
72
+ if (dnsLookupEnabled) {
73
+ return next(req).catch((e) => {
74
+ // expected HTTP errors - https://github.com/Tinkoff/tinkoff-request/blob/master/packages/plugin-protocol-http/src/errors.ts
75
+ const isExpectedError = e.code === 'ERR_HTTP_REQUEST_TIMEOUT' || e.code === 'ABORT_ERR';
76
+ if (!isExpectedError) {
77
+ if (req.baseUrl) {
78
+ // clear DNS lookup cache for all unexpected HTTP errors
79
+ cache.delete(new URL(req.baseUrl).hostname);
80
+ }
84
81
  }
85
- const [_, __, url] = getUrlAndOptions(args);
86
- cache.delete(url === null || url === void 0 ? void 0 : url.hostname);
82
+ throw e;
87
83
  });
88
- return req;
89
84
  }
90
- monkeypatch({
91
- obj: http,
92
- method: 'request',
93
- handler: handlePossibleStaleDnsErrors,
94
- });
95
- monkeypatch({
96
- obj: https,
97
- method: 'request',
98
- handler: handlePossibleStaleDnsErrors,
99
- });
85
+ return next(req);
100
86
  };
101
87
  },
102
88
  deps: {
package/lib/server.js CHANGED
@@ -3,19 +3,17 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var noop = require('@tinkoff/utils/function/noop');
6
- var monkeypatch = require('@tinkoff/monkeypatch');
7
6
  var http = require('http');
8
7
  var https = require('https');
9
8
  var dns = require('dns');
10
9
  var CacheableLookup = require('cacheable-lookup');
11
10
  var core = require('@tramvai/core');
11
+ var tokensHttpClient = require('@tramvai/tokens-http-client');
12
12
  var tokensCommon = require('@tramvai/tokens-common');
13
- var moduleMetrics = require('@tramvai/module-metrics');
14
13
 
15
14
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
16
15
 
17
16
  var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
18
- var monkeypatch__default = /*#__PURE__*/_interopDefaultLegacy(monkeypatch);
19
17
  var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
20
18
  var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
21
19
  var dns__default = /*#__PURE__*/_interopDefaultLegacy(dns);
@@ -79,37 +77,24 @@ const TramvaiDnsCacheModule = core.declareModule({
79
77
  },
80
78
  }),
81
79
  core.provide({
82
- provide: core.commandLineListTokens.init,
83
- multi: true,
80
+ provide: tokensHttpClient.DEFAULT_HTTP_CLIENT_INTERCEPTORS,
84
81
  useFactory: ({ envManager, cache }) => {
85
- if (envManager.get('DNS_LOOKUP_CACHE_ENABLE') !== 'true') {
86
- return noop__default["default"];
87
- }
88
- // if dns cache is stale, we can get any error, so it's safe to always clear dns cache on request error,
89
- // except aborted requests and timeouts - this errors shoud be expected
90
- return function addPossibleStaleDnsErrorsHandler() {
91
- function handlePossibleStaleDnsErrors(originalReq, ...args) {
92
- // @ts-expect-error
93
- const req = originalReq.apply(this, args);
94
- req.on('error', (e) => {
95
- if ((e === null || e === void 0 ? void 0 : e.type) === 'aborted' || (e === null || e === void 0 ? void 0 : e.code) === 'ETIMEDOUT') {
96
- return;
82
+ const dnsLookupEnabled = envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
83
+ return (req, next) => {
84
+ if (dnsLookupEnabled) {
85
+ return next(req).catch((e) => {
86
+ // expected HTTP errors - https://github.com/Tinkoff/tinkoff-request/blob/master/packages/plugin-protocol-http/src/errors.ts
87
+ const isExpectedError = e.code === 'ERR_HTTP_REQUEST_TIMEOUT' || e.code === 'ABORT_ERR';
88
+ if (!isExpectedError) {
89
+ if (req.baseUrl) {
90
+ // clear DNS lookup cache for all unexpected HTTP errors
91
+ cache.delete(new URL(req.baseUrl).hostname);
92
+ }
97
93
  }
98
- const [_, __, url] = moduleMetrics.getUrlAndOptions(args);
99
- cache.delete(url === null || url === void 0 ? void 0 : url.hostname);
94
+ throw e;
100
95
  });
101
- return req;
102
96
  }
103
- monkeypatch__default["default"]({
104
- obj: http__default["default"],
105
- method: 'request',
106
- handler: handlePossibleStaleDnsErrors,
107
- });
108
- monkeypatch__default["default"]({
109
- obj: https__default["default"],
110
- method: 'request',
111
- handler: handlePossibleStaleDnsErrors,
112
- });
97
+ return next(req);
113
98
  };
114
99
  },
115
100
  deps: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-dns-cache",
3
- "version": "2.120.0",
3
+ "version": "2.121.2",
4
4
  "description": "DNS lookup cache",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -24,10 +24,9 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@tinkoff/utils": "^2.1.2",
27
- "@tramvai/core": "2.120.0",
28
- "@tramvai/module-metrics": "2.120.0",
29
- "@tramvai/tokens-common": "2.120.0",
30
- "@tinkoff/monkeypatch": "2.0.5",
27
+ "@tramvai/core": "2.121.2",
28
+ "@tramvai/tokens-common": "2.121.2",
29
+ "@tramvai/tokens-http-client": "2.121.2",
31
30
  "cacheable-lookup": "^7.0.0"
32
31
  },
33
32
  "devDependencies": {},