@nxtedition/nxt-undici 2.1.1 → 2.2.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/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,15 @@ 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) + (statusMessage?.length ?? 0) + 64,
|
|
59
66
|
ttl: ttl * 1e3,
|
|
60
67
|
}
|
|
61
68
|
}
|
|
@@ -67,10 +74,12 @@ class CacheHandler extends DecoratorHandler {
|
|
|
67
74
|
onData(chunk) {
|
|
68
75
|
if (this.#value) {
|
|
69
76
|
this.#value.size += chunk.bodyLength
|
|
70
|
-
|
|
77
|
+
|
|
78
|
+
const maxEntrySize = this.#store.maxEntrySize ?? Infinity
|
|
79
|
+
if (this.#value.size > maxEntrySize) {
|
|
71
80
|
this.#value = null
|
|
72
81
|
} else {
|
|
73
|
-
this.#value.body.push(chunk)
|
|
82
|
+
this.#value.data.body.push(chunk)
|
|
74
83
|
}
|
|
75
84
|
}
|
|
76
85
|
return this.#handler.onData(chunk)
|
|
@@ -78,8 +87,9 @@ class CacheHandler extends DecoratorHandler {
|
|
|
78
87
|
|
|
79
88
|
onComplete(rawTrailers) {
|
|
80
89
|
if (this.#value) {
|
|
81
|
-
this.#value.rawTrailers
|
|
82
|
-
this.#
|
|
90
|
+
this.#value.size += rawTrailers?.reduce((xs, x) => xs + x.length) ?? 0
|
|
91
|
+
this.#value.data.rawTrailers = rawTrailers
|
|
92
|
+
this.#store.set(this.#key, this.#value.data, { ttl: this.#value.ttl, size: this.#value.size })
|
|
83
93
|
}
|
|
84
94
|
return this.#handler.onComplete(rawTrailers)
|
|
85
95
|
}
|
|
@@ -87,17 +97,14 @@ class CacheHandler extends DecoratorHandler {
|
|
|
87
97
|
|
|
88
98
|
// TODO (fix): Async filesystem cache.
|
|
89
99
|
class CacheStore {
|
|
90
|
-
constructor({ maxSize, maxEntrySize }) {
|
|
100
|
+
constructor({ maxSize = 1024 * 1024, maxEntrySize = 128 * 1024 }) {
|
|
91
101
|
this.maxSize = maxSize
|
|
92
102
|
this.maxEntrySize = maxEntrySize
|
|
93
|
-
this.cache = new LRUCache({
|
|
94
|
-
maxSize,
|
|
95
|
-
sizeCalculation: (value) => value.size,
|
|
96
|
-
})
|
|
103
|
+
this.cache = new LRUCache({ maxSize })
|
|
97
104
|
}
|
|
98
105
|
|
|
99
|
-
set(key, value,
|
|
100
|
-
this.cache.set(key, value,
|
|
106
|
+
set(key, value, opts) {
|
|
107
|
+
this.cache.set(key, value, opts)
|
|
101
108
|
}
|
|
102
109
|
|
|
103
110
|
get(key) {
|
|
@@ -153,7 +153,7 @@ class Handler extends DecoratorHandler {
|
|
|
153
153
|
this.#error = err
|
|
154
154
|
|
|
155
155
|
retryPromise
|
|
156
|
-
.then(() => {
|
|
156
|
+
.then((opts) => {
|
|
157
157
|
if (this.#aborted) {
|
|
158
158
|
this.#onError(this.#reason)
|
|
159
159
|
} else if (isDisturbed(this.#opts.body)) {
|
|
@@ -167,8 +167,10 @@ class Handler extends DecoratorHandler {
|
|
|
167
167
|
|
|
168
168
|
this.#opts = {
|
|
169
169
|
...this.#opts,
|
|
170
|
+
...opts,
|
|
170
171
|
headers: {
|
|
171
172
|
...this.#opts.headers,
|
|
173
|
+
...opts?.headers,
|
|
172
174
|
'if-match': this.#etag,
|
|
173
175
|
range: `bytes=${this.#pos}-${this.#end ?? ''}`,
|
|
174
176
|
},
|
|
@@ -204,25 +206,3 @@ export default (dispatch) => (opts, handler) => {
|
|
|
204
206
|
? dispatch(opts, new Handler(opts, { handler, dispatch }))
|
|
205
207
|
: dispatch(opts, handler)
|
|
206
208
|
}
|
|
207
|
-
|
|
208
|
-
export function isConnectionError(err) {
|
|
209
|
-
// AWS compat.
|
|
210
|
-
const statusCode = err?.statusCode ?? err?.$metadata?.httpStatusCode
|
|
211
|
-
return err
|
|
212
|
-
? err.code === 'ECONNRESET' ||
|
|
213
|
-
err.code === 'ECONNREFUSED' ||
|
|
214
|
-
err.code === 'ENOTFOUND' ||
|
|
215
|
-
err.code === 'ENETDOWN' ||
|
|
216
|
-
err.code === 'ENETUNREACH' ||
|
|
217
|
-
err.code === 'EHOSTDOWN' ||
|
|
218
|
-
err.code === 'EHOSTUNREACH' ||
|
|
219
|
-
err.code === 'EPIPE' ||
|
|
220
|
-
err.code === 'ETIMEDOUT' ||
|
|
221
|
-
err.message === 'other side closed' ||
|
|
222
|
-
statusCode === 420 ||
|
|
223
|
-
statusCode === 429 ||
|
|
224
|
-
statusCode === 502 ||
|
|
225
|
-
statusCode === 503 ||
|
|
226
|
-
statusCode === 504
|
|
227
|
-
: false
|
|
228
|
-
}
|