@tramvai/module-dns-cache 6.78.1 → 6.79.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 +69 -7
- package/lib/server.js +68 -6
- package/package.json +6 -5
package/lib/server.es.js
CHANGED
|
@@ -2,16 +2,44 @@ 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 { interceptors } from 'undici';
|
|
5
6
|
import CacheableLookup from 'cacheable-lookup';
|
|
6
7
|
import { createToken, declareModule, provide, commandLineListTokens, Scope } from '@tramvai/core';
|
|
7
|
-
import { DEFAULT_HTTP_CLIENT_INTERCEPTORS } from '@tramvai/tokens-http-client';
|
|
8
|
+
import { HTTP_CLIENT_AGENT_INTERCEPTORS, DEFAULT_HTTP_CLIENT_INTERCEPTORS } from '@tramvai/tokens-http-client';
|
|
8
9
|
import { ENV_MANAGER_TOKEN, CREATE_CACHE_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
|
|
9
10
|
|
|
10
|
-
const
|
|
11
|
+
const DNS_LOOKUP_LRU_CACHE_TOKEN = createToken('dnsLookupLruCache');
|
|
12
|
+
const DNS_CACHEABLE_LOOKUP_CACHE_TOKEN = createToken('dnsCacheableLookupCache');
|
|
13
|
+
const DNS_UNDICI_LOOKUP_CACHE_TOKEN = createToken('dnsUndiciLookupCache');
|
|
11
14
|
const TramvaiDnsCacheModule = declareModule({
|
|
12
15
|
name: 'TramvaiDnsCacheModule',
|
|
13
16
|
imports: [],
|
|
14
17
|
providers: [
|
|
18
|
+
provide({
|
|
19
|
+
provide: HTTP_CLIENT_AGENT_INTERCEPTORS,
|
|
20
|
+
useFactory: ({ envManager, storage }) => {
|
|
21
|
+
const dnsLookupEnabled = envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
|
|
22
|
+
const maxTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
23
|
+
const maxItems = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
24
|
+
if (!dnsLookupEnabled) {
|
|
25
|
+
return function noopInterceptor(dispatch) {
|
|
26
|
+
return function noopInterceptorDispatch(opts, handler) {
|
|
27
|
+
return dispatch(opts, handler);
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return interceptors.dns({
|
|
32
|
+
maxTTL,
|
|
33
|
+
maxItems,
|
|
34
|
+
// TODO: wait for https://github.com/nodejs/undici/pull/4589 release
|
|
35
|
+
// storage,
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
deps: {
|
|
39
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
40
|
+
storage: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
15
43
|
provide({
|
|
16
44
|
provide: commandLineListTokens.init,
|
|
17
45
|
multi: true,
|
|
@@ -61,7 +89,7 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
61
89
|
},
|
|
62
90
|
deps: {
|
|
63
91
|
envManager: ENV_MANAGER_TOKEN,
|
|
64
|
-
cache:
|
|
92
|
+
cache: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
65
93
|
},
|
|
66
94
|
}),
|
|
67
95
|
provide({
|
|
@@ -87,16 +115,27 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
87
115
|
},
|
|
88
116
|
deps: {
|
|
89
117
|
envManager: ENV_MANAGER_TOKEN,
|
|
90
|
-
cache:
|
|
118
|
+
cache: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
91
119
|
},
|
|
92
120
|
}),
|
|
93
121
|
provide({
|
|
94
|
-
provide:
|
|
122
|
+
provide: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
95
123
|
scope: Scope.SINGLETON,
|
|
96
124
|
useFactory: ({ createCache, envManager }) => {
|
|
97
125
|
const max = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
98
126
|
const dnsTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
99
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 }) => {
|
|
100
139
|
const adapter = {
|
|
101
140
|
set: (hostname, entries, ttl) => {
|
|
102
141
|
return cache.set(hostname, entries, { ttl });
|
|
@@ -114,8 +153,31 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
114
153
|
return adapter;
|
|
115
154
|
},
|
|
116
155
|
deps: {
|
|
117
|
-
|
|
118
|
-
|
|
156
|
+
cache: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
provide({
|
|
160
|
+
provide: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
161
|
+
scope: Scope.SINGLETON,
|
|
162
|
+
useFactory: ({ cache }) => {
|
|
163
|
+
const adapter = {
|
|
164
|
+
set: (hostname, records, opts) => {
|
|
165
|
+
cache.set(hostname, records, opts);
|
|
166
|
+
},
|
|
167
|
+
get: (hostname) => {
|
|
168
|
+
return cache.get(hostname);
|
|
169
|
+
},
|
|
170
|
+
delete: (hostname) => {
|
|
171
|
+
cache.delete(hostname);
|
|
172
|
+
},
|
|
173
|
+
full: () => {
|
|
174
|
+
return false;
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
return adapter;
|
|
178
|
+
},
|
|
179
|
+
deps: {
|
|
180
|
+
cache: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
119
181
|
},
|
|
120
182
|
}),
|
|
121
183
|
provide({
|
package/lib/server.js
CHANGED
|
@@ -6,6 +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
|
+
var undici = require('undici');
|
|
9
10
|
var CacheableLookup = require('cacheable-lookup');
|
|
10
11
|
var core = require('@tramvai/core');
|
|
11
12
|
var tokensHttpClient = require('@tramvai/tokens-http-client');
|
|
@@ -19,11 +20,38 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
|
19
20
|
var dns__default = /*#__PURE__*/_interopDefaultLegacy(dns);
|
|
20
21
|
var CacheableLookup__default = /*#__PURE__*/_interopDefaultLegacy(CacheableLookup);
|
|
21
22
|
|
|
22
|
-
const
|
|
23
|
+
const DNS_LOOKUP_LRU_CACHE_TOKEN = core.createToken('dnsLookupLruCache');
|
|
24
|
+
const DNS_CACHEABLE_LOOKUP_CACHE_TOKEN = core.createToken('dnsCacheableLookupCache');
|
|
25
|
+
const DNS_UNDICI_LOOKUP_CACHE_TOKEN = core.createToken('dnsUndiciLookupCache');
|
|
23
26
|
const TramvaiDnsCacheModule = core.declareModule({
|
|
24
27
|
name: 'TramvaiDnsCacheModule',
|
|
25
28
|
imports: [],
|
|
26
29
|
providers: [
|
|
30
|
+
core.provide({
|
|
31
|
+
provide: tokensHttpClient.HTTP_CLIENT_AGENT_INTERCEPTORS,
|
|
32
|
+
useFactory: ({ envManager, storage }) => {
|
|
33
|
+
const dnsLookupEnabled = envManager.get('DNS_LOOKUP_CACHE_ENABLE') === 'true';
|
|
34
|
+
const maxTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
35
|
+
const maxItems = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
36
|
+
if (!dnsLookupEnabled) {
|
|
37
|
+
return function noopInterceptor(dispatch) {
|
|
38
|
+
return function noopInterceptorDispatch(opts, handler) {
|
|
39
|
+
return dispatch(opts, handler);
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return undici.interceptors.dns({
|
|
44
|
+
maxTTL,
|
|
45
|
+
maxItems,
|
|
46
|
+
// TODO: wait for https://github.com/nodejs/undici/pull/4589 release
|
|
47
|
+
// storage,
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
deps: {
|
|
51
|
+
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
52
|
+
storage: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
27
55
|
core.provide({
|
|
28
56
|
provide: core.commandLineListTokens.init,
|
|
29
57
|
multi: true,
|
|
@@ -73,7 +101,7 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
73
101
|
},
|
|
74
102
|
deps: {
|
|
75
103
|
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
76
|
-
cache:
|
|
104
|
+
cache: DNS_CACHEABLE_LOOKUP_CACHE_TOKEN,
|
|
77
105
|
},
|
|
78
106
|
}),
|
|
79
107
|
core.provide({
|
|
@@ -99,16 +127,27 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
99
127
|
},
|
|
100
128
|
deps: {
|
|
101
129
|
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
102
|
-
cache:
|
|
130
|
+
cache: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
103
131
|
},
|
|
104
132
|
}),
|
|
105
133
|
core.provide({
|
|
106
|
-
provide:
|
|
134
|
+
provide: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
107
135
|
scope: core.Scope.SINGLETON,
|
|
108
136
|
useFactory: ({ createCache, envManager }) => {
|
|
109
137
|
const max = Number(envManager.get('DNS_LOOKUP_CACHE_LIMIT'));
|
|
110
138
|
const dnsTTL = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
111
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 }) => {
|
|
112
151
|
const adapter = {
|
|
113
152
|
set: (hostname, entries, ttl) => {
|
|
114
153
|
return cache.set(hostname, entries, { ttl });
|
|
@@ -126,8 +165,31 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
126
165
|
return adapter;
|
|
127
166
|
},
|
|
128
167
|
deps: {
|
|
129
|
-
|
|
130
|
-
|
|
168
|
+
cache: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
169
|
+
},
|
|
170
|
+
}),
|
|
171
|
+
core.provide({
|
|
172
|
+
provide: DNS_UNDICI_LOOKUP_CACHE_TOKEN,
|
|
173
|
+
scope: core.Scope.SINGLETON,
|
|
174
|
+
useFactory: ({ cache }) => {
|
|
175
|
+
const adapter = {
|
|
176
|
+
set: (hostname, records, opts) => {
|
|
177
|
+
cache.set(hostname, records, opts);
|
|
178
|
+
},
|
|
179
|
+
get: (hostname) => {
|
|
180
|
+
return cache.get(hostname);
|
|
181
|
+
},
|
|
182
|
+
delete: (hostname) => {
|
|
183
|
+
cache.delete(hostname);
|
|
184
|
+
},
|
|
185
|
+
full: () => {
|
|
186
|
+
return false;
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
return adapter;
|
|
190
|
+
},
|
|
191
|
+
deps: {
|
|
192
|
+
cache: DNS_LOOKUP_LRU_CACHE_TOKEN,
|
|
131
193
|
},
|
|
132
194
|
}),
|
|
133
195
|
core.provide({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-dns-cache",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.79.1",
|
|
4
4
|
"description": "DNS lookup cache",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -24,10 +24,11 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@tinkoff/utils": "^2.1.2",
|
|
27
|
-
"@tramvai/core": "6.
|
|
28
|
-
"@tramvai/tokens-common": "6.
|
|
29
|
-
"@tramvai/tokens-http-client": "6.
|
|
30
|
-
"cacheable-lookup": "^7.0.0"
|
|
27
|
+
"@tramvai/core": "6.79.1",
|
|
28
|
+
"@tramvai/tokens-common": "6.79.1",
|
|
29
|
+
"@tramvai/tokens-http-client": "6.79.1",
|
|
30
|
+
"cacheable-lookup": "^7.0.0",
|
|
31
|
+
"undici": "^7.16.0"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
33
34
|
"tslib": "^2.4.0"
|