@aws-sdk/endpoint-cache 3.186.0 → 3.188.0
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/CHANGELOG.md +8 -0
- package/dist-es/EndpointCache.js +26 -31
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.188.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.187.0...v3.188.0) (2022-10-13)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @aws-sdk/endpoint-cache
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [3.186.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.185.0...v3.186.0) (2022-10-06)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @aws-sdk/endpoint-cache
|
package/dist-es/EndpointCache.js
CHANGED
|
@@ -1,58 +1,53 @@
|
|
|
1
1
|
import LRUCache from "mnemonist/lru-cache";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
export class EndpointCache {
|
|
3
|
+
constructor(capacity) {
|
|
4
4
|
this.cache = new LRUCache(capacity);
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
getEndpoint(key) {
|
|
7
|
+
const endpointsWithExpiry = this.get(key);
|
|
8
8
|
if (!endpointsWithExpiry || endpointsWithExpiry.length === 0) {
|
|
9
9
|
return undefined;
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
const endpoints = endpointsWithExpiry.map((endpoint) => endpoint.Address);
|
|
12
12
|
return endpoints[Math.floor(Math.random() * endpoints.length)];
|
|
13
|
-
}
|
|
14
|
-
|
|
13
|
+
}
|
|
14
|
+
get(key) {
|
|
15
15
|
if (!this.has(key)) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
const value = this.cache.get(key);
|
|
19
19
|
if (!value) {
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
const now = Date.now();
|
|
23
|
+
const endpointsWithExpiry = value.filter((endpoint) => now < endpoint.Expires);
|
|
24
24
|
if (endpointsWithExpiry.length === 0) {
|
|
25
25
|
this.delete(key);
|
|
26
26
|
return undefined;
|
|
27
27
|
}
|
|
28
28
|
return endpointsWithExpiry;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.cache.set(key, endpoints.map(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}));
|
|
39
|
-
};
|
|
40
|
-
EndpointCache.prototype.delete = function (key) {
|
|
29
|
+
}
|
|
30
|
+
set(key, endpoints) {
|
|
31
|
+
const now = Date.now();
|
|
32
|
+
this.cache.set(key, endpoints.map(({ Address, CachePeriodInMinutes }) => ({
|
|
33
|
+
Address,
|
|
34
|
+
Expires: now + CachePeriodInMinutes * 60 * 1000,
|
|
35
|
+
})));
|
|
36
|
+
}
|
|
37
|
+
delete(key) {
|
|
41
38
|
this.cache.set(key, []);
|
|
42
|
-
}
|
|
43
|
-
|
|
39
|
+
}
|
|
40
|
+
has(key) {
|
|
44
41
|
if (!this.cache.has(key)) {
|
|
45
42
|
return false;
|
|
46
43
|
}
|
|
47
|
-
|
|
44
|
+
const endpoints = this.cache.peek(key);
|
|
48
45
|
if (!endpoints) {
|
|
49
46
|
return false;
|
|
50
47
|
}
|
|
51
48
|
return endpoints.length > 0;
|
|
52
|
-
}
|
|
53
|
-
|
|
49
|
+
}
|
|
50
|
+
clear() {
|
|
54
51
|
this.cache.clear();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
}());
|
|
58
|
-
export { EndpointCache };
|
|
52
|
+
}
|
|
53
|
+
}
|