@nxtedition/nxt-undici 6.0.22 → 6.0.23
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 +11 -5
- package/package.json +1 -1
package/lib/interceptor/cache.js
CHANGED
|
@@ -2,7 +2,7 @@ import undici from '@nxtedition/undici'
|
|
|
2
2
|
import { DecoratorHandler, parseCacheControl } from '../utils.js'
|
|
3
3
|
|
|
4
4
|
const DEFAULT_STORE = new undici.cacheStores.SqliteCacheStore({ location: ':memory:' })
|
|
5
|
-
const
|
|
5
|
+
const DEFAULT_MAX_ENTRY_SIZE = 128 * 1024
|
|
6
6
|
const EMPTY_BUFFER = Buffer.alloc(0)
|
|
7
7
|
const NOOP = () => {}
|
|
8
8
|
|
|
@@ -10,8 +10,9 @@ class CacheHandler extends DecoratorHandler {
|
|
|
10
10
|
#key
|
|
11
11
|
#value
|
|
12
12
|
#store
|
|
13
|
+
#maxEntrySize
|
|
13
14
|
|
|
14
|
-
constructor(key, { store, handler }) {
|
|
15
|
+
constructor(key, { store, handler, maxEntrySize }) {
|
|
15
16
|
undici.util.cache.assertCacheKey(key)
|
|
16
17
|
|
|
17
18
|
super(handler)
|
|
@@ -19,6 +20,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
19
20
|
this.#key = key
|
|
20
21
|
this.#value = null
|
|
21
22
|
this.#store = store
|
|
23
|
+
this.#maxEntrySize = maxEntrySize ?? store.maxEntrySize ?? DEFAULT_MAX_ENTRY_SIZE
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
onConnect(abort) {
|
|
@@ -38,7 +40,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
const contentLength = headers['content-length'] ? Number(headers['content-length']) : Infinity
|
|
41
|
-
if (Number.isFinite(contentLength) && contentLength >
|
|
43
|
+
if (Number.isFinite(contentLength) && contentLength > DEFAULT_MAX_ENTRY_SIZE) {
|
|
42
44
|
// We don't support caching responses with body...
|
|
43
45
|
return super.onHeaders(statusCode, headers, resume)
|
|
44
46
|
}
|
|
@@ -111,7 +113,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
111
113
|
this.#value.size += chunk.length
|
|
112
114
|
this.#value.body.push(chunk)
|
|
113
115
|
|
|
114
|
-
if (this.#value.size >
|
|
116
|
+
if (this.#value.size > this.#maxEntrySize) {
|
|
115
117
|
this.#value = null
|
|
116
118
|
this.#value.size = 0
|
|
117
119
|
}
|
|
@@ -162,7 +164,11 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
162
164
|
opts,
|
|
163
165
|
cacheControlDirectives['no-store']
|
|
164
166
|
? handler
|
|
165
|
-
: new CacheHandler(undici.util.cache.makeCacheKey(opts), {
|
|
167
|
+
: new CacheHandler(undici.util.cache.makeCacheKey(opts), {
|
|
168
|
+
maxEntrySize: opts.cache.maxEntrySize,
|
|
169
|
+
store,
|
|
170
|
+
handler,
|
|
171
|
+
}),
|
|
166
172
|
)
|
|
167
173
|
}
|
|
168
174
|
|