@tramvai/module-dns-cache 7.21.1 → 7.26.9
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 +30 -37
- package/lib/server.js +30 -37
- package/package.json +4 -4
package/lib/server.es.js
CHANGED
|
@@ -2,13 +2,12 @@ import noop from '@tinkoff/utils/function/noop';
|
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import https from 'https';
|
|
4
4
|
import dns from 'dns';
|
|
5
|
-
import
|
|
5
|
+
import 'undici';
|
|
6
6
|
import CacheableLookup from 'cacheable-lookup';
|
|
7
7
|
import { createToken, declareModule, provide, commandLineListTokens, Scope } from '@tramvai/core';
|
|
8
8
|
import { HTTP_CLIENT_AGENT_INTERCEPTORS, DEFAULT_HTTP_CLIENT_INTERCEPTORS } from '@tramvai/tokens-http-client';
|
|
9
9
|
import { ENV_MANAGER_TOKEN, CREATE_CACHE_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
|
|
10
10
|
|
|
11
|
-
const DNS_LOOKUP_LRU_CACHE_TOKEN = createToken('dnsLookupLruCache');
|
|
12
11
|
const DNS_CACHEABLE_LOOKUP_CACHE_TOKEN = createToken('dnsCacheableLookupCache');
|
|
13
12
|
const DNS_UNDICI_LOOKUP_CACHE_TOKEN = createToken('dnsUndiciLookupCache');
|
|
14
13
|
const TramvaiDnsCacheModule = declareModule({
|
|
@@ -18,22 +17,14 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
18
17
|
provide({
|
|
19
18
|
provide: HTTP_CLIENT_AGENT_INTERCEPTORS,
|
|
20
19
|
useFactory: ({ envManager, storage }) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return function
|
|
26
|
-
return
|
|
27
|
-
return dispatch(opts, handler);
|
|
28
|
-
};
|
|
20
|
+
envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
|
|
21
|
+
Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
22
|
+
Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
23
|
+
return function noopInterceptor(dispatch) {
|
|
24
|
+
return function noopInterceptorDispatch(opts, handler) {
|
|
25
|
+
return dispatch(opts, handler);
|
|
29
26
|
};
|
|
30
|
-
}
|
|
31
|
-
return interceptors.dns({
|
|
32
|
-
maxTTL,
|
|
33
|
-
maxItems,
|
|
34
|
-
// https://github.com/nodejs/undici/pull/4589
|
|
35
|
-
storage,
|
|
36
|
-
});
|
|
27
|
+
};
|
|
37
28
|
},
|
|
38
29
|
deps: {
|
|
39
30
|
envManager: ENV_MANAGER_TOKEN,
|
|
@@ -52,6 +43,11 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
52
43
|
const cacheable = new CacheableLookup({
|
|
53
44
|
cache,
|
|
54
45
|
maxTtl,
|
|
46
|
+
// cacheable-lookup captures dns.lookup at import time via destructuring,
|
|
47
|
+
// so runtime patches to dns.lookup are invisible.
|
|
48
|
+
// Passing dns.lookup from the module object at construction time fixes this.
|
|
49
|
+
// https://github.com/szmarczak/cacheable-lookup/blob/9e60c9f6e74a003692aec68f3ddad93afe613b8f/source/index.mjs#L6
|
|
50
|
+
lookup: dns.lookup,
|
|
55
51
|
});
|
|
56
52
|
const originalLookup = cacheable.lookup;
|
|
57
53
|
// workaround for https://github.com/szmarczak/cacheable-lookup/issues/68,
|
|
@@ -94,7 +90,7 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
94
90
|
}),
|
|
95
91
|
provide({
|
|
96
92
|
provide: DEFAULT_HTTP_CLIENT_INTERCEPTORS,
|
|
97
|
-
useFactory: ({ envManager, cache }) => {
|
|
93
|
+
useFactory: ({ envManager, cache, cacheHttp }) => {
|
|
98
94
|
const dnsLookupEnabled = envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
|
|
99
95
|
return (req, next) => {
|
|
100
96
|
if (dnsLookupEnabled) {
|
|
@@ -103,8 +99,10 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
103
99
|
const isExpectedError = e.code === 'ERR_HTTP_REQUEST_TIMEOUT' || e.code === 'ABORT_ERR';
|
|
104
100
|
if (!isExpectedError) {
|
|
105
101
|
if (req.baseUrl) {
|
|
102
|
+
const key = new URL(req.baseUrl).hostname;
|
|
106
103
|
// clear DNS lookup cache for all unexpected HTTP errors
|
|
107
|
-
cache.delete(
|
|
104
|
+
cache.delete(key);
|
|
105
|
+
cacheHttp.delete(key);
|
|
108
106
|
}
|
|
109
107
|
}
|
|
110
108
|
throw e;
|
|
@@ -115,27 +113,17 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
115
113
|
},
|
|
116
114
|
deps: {
|
|
117
115
|
envManager: ENV_MANAGER_TOKEN,
|
|
118
|
-
cache:
|
|
116
|
+
cache: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
117
|
+
cacheHttp: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
119
118
|
},
|
|
120
119
|
}),
|
|
121
120
|
provide({
|
|
122
|
-
provide:
|
|
121
|
+
provide: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
123
122
|
scope: Scope.SINGLETON,
|
|
124
|
-
useFactory: ({
|
|
123
|
+
useFactory: ({ envManager, createCache }) => {
|
|
125
124
|
const max = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
126
125
|
const dnsTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
127
|
-
const cache = createCache('memory', { name: 'dns-lookup', max, ttl: dnsTTL });
|
|
128
|
-
return cache;
|
|
129
|
-
},
|
|
130
|
-
deps: {
|
|
131
|
-
createCache: CREATE_CACHE_TOKEN,
|
|
132
|
-
envManager: ENV_MANAGER_TOKEN,
|
|
133
|
-
},
|
|
134
|
-
}),
|
|
135
|
-
provide({
|
|
136
|
-
provide: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
137
|
-
scope: Scope.SINGLETON,
|
|
138
|
-
useFactory: ({ cache }) => {
|
|
126
|
+
const cache = createCache('memory', { name: 'dns-lookup-http', max, ttl: dnsTTL });
|
|
139
127
|
const adapter = {
|
|
140
128
|
set: (hostname, entries, ttl) => {
|
|
141
129
|
return cache.set(hostname, entries, { ttl });
|
|
@@ -153,13 +141,17 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
153
141
|
return adapter;
|
|
154
142
|
},
|
|
155
143
|
deps: {
|
|
156
|
-
|
|
144
|
+
createCache: CREATE_CACHE_TOKEN,
|
|
145
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
157
146
|
},
|
|
158
147
|
}),
|
|
159
148
|
provide({
|
|
160
149
|
provide: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
161
150
|
scope: Scope.SINGLETON,
|
|
162
|
-
useFactory: ({
|
|
151
|
+
useFactory: ({ envManager, createCache }) => {
|
|
152
|
+
const max = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
153
|
+
const dnsTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
154
|
+
const cache = createCache('memory', { name: 'dns-lookup', max, ttl: dnsTTL });
|
|
163
155
|
const adapter = {
|
|
164
156
|
set: (hostname, records, opts) => {
|
|
165
157
|
cache.set(hostname, records, opts);
|
|
@@ -180,7 +172,8 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
180
172
|
return adapter;
|
|
181
173
|
},
|
|
182
174
|
deps: {
|
|
183
|
-
|
|
175
|
+
createCache: CREATE_CACHE_TOKEN,
|
|
176
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
184
177
|
},
|
|
185
178
|
}),
|
|
186
179
|
provide({
|
package/lib/server.js
CHANGED
|
@@ -6,7 +6,7 @@ var noop = require('@tinkoff/utils/function/noop');
|
|
|
6
6
|
var http = require('http');
|
|
7
7
|
var https = require('https');
|
|
8
8
|
var dns = require('dns');
|
|
9
|
-
|
|
9
|
+
require('undici');
|
|
10
10
|
var CacheableLookup = require('cacheable-lookup');
|
|
11
11
|
var core = require('@tramvai/core');
|
|
12
12
|
var tokensHttpClient = require('@tramvai/tokens-http-client');
|
|
@@ -20,7 +20,6 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
|
20
20
|
var dns__default = /*#__PURE__*/_interopDefaultLegacy(dns);
|
|
21
21
|
var CacheableLookup__default = /*#__PURE__*/_interopDefaultLegacy(CacheableLookup);
|
|
22
22
|
|
|
23
|
-
const DNS_LOOKUP_LRU_CACHE_TOKEN = core.createToken('dnsLookupLruCache');
|
|
24
23
|
const DNS_CACHEABLE_LOOKUP_CACHE_TOKEN = core.createToken('dnsCacheableLookupCache');
|
|
25
24
|
const DNS_UNDICI_LOOKUP_CACHE_TOKEN = core.createToken('dnsUndiciLookupCache');
|
|
26
25
|
const TramvaiDnsCacheModule = core.declareModule({
|
|
@@ -30,22 +29,14 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
30
29
|
core.provide({
|
|
31
30
|
provide: tokensHttpClient.HTTP_CLIENT_AGENT_INTERCEPTORS,
|
|
32
31
|
useFactory: ({ envManager, storage }) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return function
|
|
38
|
-
return
|
|
39
|
-
return dispatch(opts, handler);
|
|
40
|
-
};
|
|
32
|
+
envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
|
|
33
|
+
Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
34
|
+
Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
35
|
+
return function noopInterceptor(dispatch) {
|
|
36
|
+
return function noopInterceptorDispatch(opts, handler) {
|
|
37
|
+
return dispatch(opts, handler);
|
|
41
38
|
};
|
|
42
|
-
}
|
|
43
|
-
return undici.interceptors.dns({
|
|
44
|
-
maxTTL,
|
|
45
|
-
maxItems,
|
|
46
|
-
// https://github.com/nodejs/undici/pull/4589
|
|
47
|
-
storage,
|
|
48
|
-
});
|
|
39
|
+
};
|
|
49
40
|
},
|
|
50
41
|
deps: {
|
|
51
42
|
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
@@ -64,6 +55,11 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
64
55
|
const cacheable = new CacheableLookup__default["default"]({
|
|
65
56
|
cache,
|
|
66
57
|
maxTtl,
|
|
58
|
+
// cacheable-lookup captures dns.lookup at import time via destructuring,
|
|
59
|
+
// so runtime patches to dns.lookup are invisible.
|
|
60
|
+
// Passing dns.lookup from the module object at construction time fixes this.
|
|
61
|
+
// https://github.com/szmarczak/cacheable-lookup/blob/9e60c9f6e74a003692aec68f3ddad93afe613b8f/source/index.mjs#L6
|
|
62
|
+
lookup: dns__default["default"].lookup,
|
|
67
63
|
});
|
|
68
64
|
const originalLookup = cacheable.lookup;
|
|
69
65
|
// workaround for https://github.com/szmarczak/cacheable-lookup/issues/68,
|
|
@@ -106,7 +102,7 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
106
102
|
}),
|
|
107
103
|
core.provide({
|
|
108
104
|
provide: tokensHttpClient.DEFAULT_HTTP_CLIENT_INTERCEPTORS,
|
|
109
|
-
useFactory: ({ envManager, cache }) => {
|
|
105
|
+
useFactory: ({ envManager, cache, cacheHttp }) => {
|
|
110
106
|
const dnsLookupEnabled = envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
|
|
111
107
|
return (req, next) => {
|
|
112
108
|
if (dnsLookupEnabled) {
|
|
@@ -115,8 +111,10 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
115
111
|
const isExpectedError = e.code === 'ERR_HTTP_REQUEST_TIMEOUT' || e.code === 'ABORT_ERR';
|
|
116
112
|
if (!isExpectedError) {
|
|
117
113
|
if (req.baseUrl) {
|
|
114
|
+
const key = new URL(req.baseUrl).hostname;
|
|
118
115
|
// clear DNS lookup cache for all unexpected HTTP errors
|
|
119
|
-
cache.delete(
|
|
116
|
+
cache.delete(key);
|
|
117
|
+
cacheHttp.delete(key);
|
|
120
118
|
}
|
|
121
119
|
}
|
|
122
120
|
throw e;
|
|
@@ -127,27 +125,17 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
127
125
|
},
|
|
128
126
|
deps: {
|
|
129
127
|
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
130
|
-
cache:
|
|
128
|
+
cache: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
129
|
+
cacheHttp: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
131
130
|
},
|
|
132
131
|
}),
|
|
133
132
|
core.provide({
|
|
134
|
-
provide:
|
|
133
|
+
provide: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
135
134
|
scope: core.Scope.SINGLETON,
|
|
136
|
-
useFactory: ({
|
|
135
|
+
useFactory: ({ envManager, createCache }) => {
|
|
137
136
|
const max = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
138
137
|
const dnsTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
139
|
-
const cache = createCache('memory', { name: 'dns-lookup', max, ttl: dnsTTL });
|
|
140
|
-
return cache;
|
|
141
|
-
},
|
|
142
|
-
deps: {
|
|
143
|
-
createCache: tokensCommon.CREATE_CACHE_TOKEN,
|
|
144
|
-
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
145
|
-
},
|
|
146
|
-
}),
|
|
147
|
-
core.provide({
|
|
148
|
-
provide: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
149
|
-
scope: core.Scope.SINGLETON,
|
|
150
|
-
useFactory: ({ cache }) => {
|
|
138
|
+
const cache = createCache('memory', { name: 'dns-lookup-http', max, ttl: dnsTTL });
|
|
151
139
|
const adapter = {
|
|
152
140
|
set: (hostname, entries, ttl) => {
|
|
153
141
|
return cache.set(hostname, entries, { ttl });
|
|
@@ -165,13 +153,17 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
165
153
|
return adapter;
|
|
166
154
|
},
|
|
167
155
|
deps: {
|
|
168
|
-
|
|
156
|
+
createCache: tokensCommon.CREATE_CACHE_TOKEN,
|
|
157
|
+
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
169
158
|
},
|
|
170
159
|
}),
|
|
171
160
|
core.provide({
|
|
172
161
|
provide: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
173
162
|
scope: core.Scope.SINGLETON,
|
|
174
|
-
useFactory: ({
|
|
163
|
+
useFactory: ({ envManager, createCache }) => {
|
|
164
|
+
const max = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
165
|
+
const dnsTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
166
|
+
const cache = createCache('memory', { name: 'dns-lookup', max, ttl: dnsTTL });
|
|
175
167
|
const adapter = {
|
|
176
168
|
set: (hostname, records, opts) => {
|
|
177
169
|
cache.set(hostname, records, opts);
|
|
@@ -192,7 +184,8 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
192
184
|
return adapter;
|
|
193
185
|
},
|
|
194
186
|
deps: {
|
|
195
|
-
|
|
187
|
+
createCache: tokensCommon.CREATE_CACHE_TOKEN,
|
|
188
|
+
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
196
189
|
},
|
|
197
190
|
}),
|
|
198
191
|
core.provide({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-dns-cache",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.26.9",
|
|
4
4
|
"description": "DNS lookup cache",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@tinkoff/utils": "^2.1.2",
|
|
27
|
-
"@tramvai/core": "7.
|
|
28
|
-
"@tramvai/tokens-common": "7.
|
|
29
|
-
"@tramvai/tokens-http-client": "7.
|
|
27
|
+
"@tramvai/core": "7.26.9",
|
|
28
|
+
"@tramvai/tokens-common": "7.26.9",
|
|
29
|
+
"@tramvai/tokens-http-client": "7.26.9",
|
|
30
30
|
"cacheable-lookup": "^7.0.0",
|
|
31
31
|
"undici": "^7.24.4"
|
|
32
32
|
},
|