@nxtedition/nxt-undici 2.2.0 → 2.2.3
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/interceptor/cache.js +27 -18
- package/package.json +1 -1
package/lib/interceptor/cache.js
CHANGED
|
@@ -24,14 +24,18 @@ class CacheHandler extends DecoratorHandler {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
onHeaders(statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) {
|
|
27
|
-
// NOTE: Only cache 307 respones for now...
|
|
28
27
|
if (statusCode !== 307) {
|
|
29
28
|
return this.#handler.onHeaders(statusCode, rawHeaders, resume, statusMessage, headers)
|
|
30
29
|
}
|
|
31
30
|
|
|
31
|
+
// TODO (fix): Support vary header.
|
|
32
32
|
const cacheControl = parseCacheControl(headers['cache-control'])
|
|
33
33
|
|
|
34
|
+
const contentLength = headers['content-length'] ? Number(headers['content-length']) : Infinity
|
|
35
|
+
const maxEntrySize = this.#store.maxEntrySize ?? Infinity
|
|
36
|
+
|
|
34
37
|
if (
|
|
38
|
+
contentLength < maxEntrySize &&
|
|
35
39
|
cacheControl &&
|
|
36
40
|
cacheControl.public &&
|
|
37
41
|
!cacheControl.private &&
|
|
@@ -50,12 +54,17 @@ class CacheHandler extends DecoratorHandler {
|
|
|
50
54
|
|
|
51
55
|
if (ttl > 0) {
|
|
52
56
|
this.#value = {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
data: {
|
|
58
|
+
statusCode,
|
|
59
|
+
statusMessage,
|
|
60
|
+
rawHeaders,
|
|
61
|
+
rawTrailers: null,
|
|
62
|
+
body: [],
|
|
63
|
+
},
|
|
64
|
+
size:
|
|
65
|
+
(rawHeaders?.reduce((xs, x) => xs + x.length, 0) ?? 0) +
|
|
66
|
+
(statusMessage?.length ?? 0) +
|
|
67
|
+
64,
|
|
59
68
|
ttl: ttl * 1e3,
|
|
60
69
|
}
|
|
61
70
|
}
|
|
@@ -67,10 +76,12 @@ class CacheHandler extends DecoratorHandler {
|
|
|
67
76
|
onData(chunk) {
|
|
68
77
|
if (this.#value) {
|
|
69
78
|
this.#value.size += chunk.bodyLength
|
|
70
|
-
|
|
79
|
+
|
|
80
|
+
const maxEntrySize = this.#store.maxEntrySize ?? Infinity
|
|
81
|
+
if (this.#value.size > maxEntrySize) {
|
|
71
82
|
this.#value = null
|
|
72
83
|
} else {
|
|
73
|
-
this.#value.body.push(chunk)
|
|
84
|
+
this.#value.data.body.push(chunk)
|
|
74
85
|
}
|
|
75
86
|
}
|
|
76
87
|
return this.#handler.onData(chunk)
|
|
@@ -78,8 +89,9 @@ class CacheHandler extends DecoratorHandler {
|
|
|
78
89
|
|
|
79
90
|
onComplete(rawTrailers) {
|
|
80
91
|
if (this.#value) {
|
|
81
|
-
this.#value.rawTrailers = rawTrailers
|
|
82
|
-
this.#
|
|
92
|
+
this.#value.data.rawTrailers = rawTrailers
|
|
93
|
+
this.#value.size += rawTrailers?.reduce((xs, x) => xs + x.length, 0) ?? 0
|
|
94
|
+
this.#store.set(this.#key, this.#value.data, { ttl: this.#value.ttl, size: this.#value.size })
|
|
83
95
|
}
|
|
84
96
|
return this.#handler.onComplete(rawTrailers)
|
|
85
97
|
}
|
|
@@ -87,17 +99,14 @@ class CacheHandler extends DecoratorHandler {
|
|
|
87
99
|
|
|
88
100
|
// TODO (fix): Async filesystem cache.
|
|
89
101
|
class CacheStore {
|
|
90
|
-
constructor({ maxSize, maxEntrySize }) {
|
|
102
|
+
constructor({ maxSize = 1024 * 1024, maxEntrySize = 128 * 1024 }) {
|
|
91
103
|
this.maxSize = maxSize
|
|
92
104
|
this.maxEntrySize = maxEntrySize
|
|
93
|
-
this.cache = new LRUCache({
|
|
94
|
-
maxSize,
|
|
95
|
-
sizeCalculation: (value) => value.size,
|
|
96
|
-
})
|
|
105
|
+
this.cache = new LRUCache({ maxSize })
|
|
97
106
|
}
|
|
98
107
|
|
|
99
|
-
set(key, value,
|
|
100
|
-
this.cache.set(key, value,
|
|
108
|
+
set(key, value, opts) {
|
|
109
|
+
this.cache.set(key, value, opts)
|
|
101
110
|
}
|
|
102
111
|
|
|
103
112
|
get(key) {
|