@nxtedition/nxt-undici 6.0.21 → 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 +26 -26
- package/lib/utils.js +4 -4
- package/package.json +1 -1
package/lib/interceptor/cache.js
CHANGED
|
@@ -2,15 +2,17 @@ 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
|
+
const NOOP = () => {}
|
|
7
8
|
|
|
8
9
|
class CacheHandler extends DecoratorHandler {
|
|
9
10
|
#key
|
|
10
11
|
#value
|
|
11
12
|
#store
|
|
13
|
+
#maxEntrySize
|
|
12
14
|
|
|
13
|
-
constructor(key, { store, handler }) {
|
|
15
|
+
constructor(key, { store, handler, maxEntrySize }) {
|
|
14
16
|
undici.util.cache.assertCacheKey(key)
|
|
15
17
|
|
|
16
18
|
super(handler)
|
|
@@ -18,6 +20,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
18
20
|
this.#key = key
|
|
19
21
|
this.#value = null
|
|
20
22
|
this.#store = store
|
|
23
|
+
this.#maxEntrySize = maxEntrySize ?? store.maxEntrySize ?? DEFAULT_MAX_ENTRY_SIZE
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
onConnect(abort) {
|
|
@@ -37,7 +40,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
const contentLength = headers['content-length'] ? Number(headers['content-length']) : Infinity
|
|
40
|
-
if (Number.isFinite(contentLength) && contentLength >
|
|
43
|
+
if (Number.isFinite(contentLength) && contentLength > DEFAULT_MAX_ENTRY_SIZE) {
|
|
41
44
|
// We don't support caching responses with body...
|
|
42
45
|
return super.onHeaders(statusCode, headers, resume)
|
|
43
46
|
}
|
|
@@ -110,7 +113,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
110
113
|
this.#value.size += chunk.length
|
|
111
114
|
this.#value.body.push(chunk)
|
|
112
115
|
|
|
113
|
-
if (this.#value.size >
|
|
116
|
+
if (this.#value.size > this.#maxEntrySize) {
|
|
114
117
|
this.#value = null
|
|
115
118
|
this.#value.size = 0
|
|
116
119
|
}
|
|
@@ -161,7 +164,11 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
161
164
|
opts,
|
|
162
165
|
cacheControlDirectives['no-store']
|
|
163
166
|
? handler
|
|
164
|
-
: 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
|
+
}),
|
|
165
172
|
)
|
|
166
173
|
}
|
|
167
174
|
|
|
@@ -175,27 +182,20 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
175
182
|
}
|
|
176
183
|
}
|
|
177
184
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
} else if (state === 4) {
|
|
192
|
-
handler.onComplete({})
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
} catch (err) {
|
|
196
|
-
abort(err)
|
|
197
|
-
}
|
|
185
|
+
handler.onConnect(abort)
|
|
186
|
+
if (aborted) {
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
handler.onHeaders(statusCode, headers, NOOP)
|
|
191
|
+
if (aborted) {
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
handler.onData(body ?? EMPTY_BUFFER)
|
|
196
|
+
if (aborted) {
|
|
197
|
+
return
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
|
|
200
|
+
handler.onComplete({})
|
|
201
201
|
}
|
package/lib/utils.js
CHANGED
|
@@ -276,15 +276,15 @@ export class DecoratorHandler {
|
|
|
276
276
|
})
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
-
onUpgrade(
|
|
279
|
+
onUpgrade(statusCode, headers, socket) {
|
|
280
280
|
if (!this.#aborted && !this.#errored) {
|
|
281
|
-
return this.#handler.onUpgrade?.(
|
|
281
|
+
return this.#handler.onUpgrade?.(statusCode, headers, socket)
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
284
|
|
|
285
|
-
onHeaders(
|
|
285
|
+
onHeaders(statusCode, headers, resume) {
|
|
286
286
|
if (!this.#aborted && !this.#errored) {
|
|
287
|
-
return this.#handler.onHeaders?.(
|
|
287
|
+
return this.#handler.onHeaders?.(statusCode, headers, resume)
|
|
288
288
|
}
|
|
289
289
|
}
|
|
290
290
|
|