@nxtedition/nxt-undici 6.0.6 → 6.0.8
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 +43 -36
- package/lib/interceptor/log.js +1 -1
- package/lib/interceptor/redirect.js +2 -2
- package/lib/utils.js +4 -4
- package/package.json +1 -1
package/lib/interceptor/cache.js
CHANGED
|
@@ -26,8 +26,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
onHeaders(statusCode, headers, resume) {
|
|
29
|
-
if (statusCode !== 307) {
|
|
30
|
-
// Only cache redirects...
|
|
29
|
+
if (statusCode !== 307 && statusCode !== 200) {
|
|
31
30
|
return super.onHeaders(statusCode, headers, resume)
|
|
32
31
|
}
|
|
33
32
|
|
|
@@ -42,28 +41,36 @@ class CacheHandler extends DecoratorHandler {
|
|
|
42
41
|
return super.onHeaders(statusCode, headers, resume)
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
const
|
|
44
|
+
const cacheControlDirectives = parseCacheControl(headers['cache-control']) ?? {}
|
|
45
|
+
|
|
46
|
+
if (this.#key.headers.authorization && !cacheControlDirectives.public) {
|
|
47
|
+
return super.onHeaders(statusCode, headers, resume)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (cacheControlDirectives.private || cacheControlDirectives['no-store']) {
|
|
51
|
+
return super.onHeaders(statusCode, headers, resume)
|
|
52
|
+
}
|
|
53
|
+
|
|
46
54
|
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
cacheControl['must-understand'] ||
|
|
55
|
-
cacheControl['must-revalidate'] ||
|
|
56
|
-
cacheControl['proxy-revalidate']
|
|
55
|
+
cacheControlDirectives['no-transform'] ||
|
|
56
|
+
cacheControlDirectives['must-revalidate'] ||
|
|
57
|
+
cacheControlDirectives['proxy-revalidate'] ||
|
|
58
|
+
cacheControlDirectives['must-understand'] ||
|
|
59
|
+
cacheControlDirectives['stale-while-revalidate'] ||
|
|
60
|
+
cacheControlDirectives['stale-if-error'] ||
|
|
61
|
+
cacheControlDirectives['no-cache']
|
|
57
62
|
) {
|
|
58
|
-
//
|
|
63
|
+
// TODO (fix): Support all cache control directives...
|
|
59
64
|
return super.onHeaders(statusCode, headers, resume)
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
const vary = {}
|
|
63
68
|
if (headers.vary) {
|
|
64
|
-
|
|
65
|
-
.
|
|
66
|
-
|
|
69
|
+
if (typeof headers.vary !== 'string') {
|
|
70
|
+
return super.onHeaders(statusCode, headers, resume)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const key of headers.varyvary.split(',').map((key) => key.trim().toLowerCase())) {
|
|
67
74
|
const val = this.#key.headers[key]
|
|
68
75
|
if (!val) {
|
|
69
76
|
// Expect vary headers to be present...
|
|
@@ -71,14 +78,11 @@ class CacheHandler extends DecoratorHandler {
|
|
|
71
78
|
}
|
|
72
79
|
vary[key] = val
|
|
73
80
|
}
|
|
74
|
-
|
|
75
|
-
// Unexpected vary header type...
|
|
76
|
-
return super.onHeaders(statusCode, headers, resume)
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
const ttl =
|
|
83
|
+
const ttl = cacheControlDirectives.immutable
|
|
80
84
|
? 31556952
|
|
81
|
-
: Number(
|
|
85
|
+
: Number(cacheControlDirectives['s-max-age'] ?? cacheControlDirectives['max-age'])
|
|
82
86
|
if (!ttl || !Number.isFinite(ttl) || ttl <= 0) {
|
|
83
87
|
return super.onHeaders(statusCode, headers, resume)
|
|
84
88
|
}
|
|
@@ -92,8 +96,8 @@ class CacheHandler extends DecoratorHandler {
|
|
|
92
96
|
statusCode,
|
|
93
97
|
statusMessage: '',
|
|
94
98
|
headers,
|
|
95
|
-
cacheControlDirectives
|
|
96
|
-
etag:
|
|
99
|
+
cacheControlDirectives,
|
|
100
|
+
etag: headers.etag,
|
|
97
101
|
vary,
|
|
98
102
|
cachedAt,
|
|
99
103
|
staleAt: 0,
|
|
@@ -134,16 +138,17 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
134
138
|
return dispatch(opts, handler)
|
|
135
139
|
}
|
|
136
140
|
|
|
137
|
-
|
|
141
|
+
const cacheControlDirectives = parseCacheControl(opts?.headers['cache-control']) ?? {}
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
cacheControlDirectives['max-age'] ||
|
|
145
|
+
cacheControlDirectives['max-stale'] ||
|
|
146
|
+
cacheControlDirectives['min-fresh'] ||
|
|
147
|
+
cacheControlDirectives['no-cache'] ||
|
|
148
|
+
cacheControlDirectives['no-transform'] ||
|
|
149
|
+
cacheControlDirectives['stale-if-error']
|
|
150
|
+
) {
|
|
138
151
|
// TODO (fix): Support all cache control directives...
|
|
139
|
-
// const cacheControl = cacheControlParser.parse(opts.headers['cache-control'])
|
|
140
|
-
// cacheControl['no-cache']
|
|
141
|
-
// cacheControl['no-store']
|
|
142
|
-
// cacheControl['max-age']
|
|
143
|
-
// cacheControl['max-stale']
|
|
144
|
-
// cacheControl['min-fresh']
|
|
145
|
-
// cacheControl['no-transform']
|
|
146
|
-
// cacheControl['only-if-cached']
|
|
147
152
|
return dispatch(opts, handler)
|
|
148
153
|
}
|
|
149
154
|
|
|
@@ -152,10 +157,12 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
152
157
|
|
|
153
158
|
const store = opts.cache.store ?? DEFAULT_STORE
|
|
154
159
|
const entry = store.get(opts)
|
|
155
|
-
if (!entry) {
|
|
160
|
+
if (!entry && !cacheControlDirectives['only-if-cached']) {
|
|
156
161
|
return dispatch(
|
|
157
162
|
opts,
|
|
158
|
-
|
|
163
|
+
cacheControlDirectives['no-store']
|
|
164
|
+
? handler
|
|
165
|
+
: new CacheHandler(undici.util.cache.makeCacheKey(opts), { store, handler }),
|
|
159
166
|
)
|
|
160
167
|
}
|
|
161
168
|
|
|
@@ -174,7 +181,7 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
174
181
|
}
|
|
175
182
|
}
|
|
176
183
|
|
|
177
|
-
const { statusCode, headers } = entry
|
|
184
|
+
const { statusCode, headers } = entry ?? { statusCode: 504, headers: {} }
|
|
178
185
|
try {
|
|
179
186
|
handler.onConnect(abort)
|
|
180
187
|
if (aborted) {
|
package/lib/interceptor/log.js
CHANGED
|
@@ -41,8 +41,8 @@ class Handler extends DecoratorHandler {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
onUpgrade(statusCode,
|
|
45
|
-
super.onUpgrade(statusCode,
|
|
44
|
+
onUpgrade(statusCode, headers, socket) {
|
|
45
|
+
super.onUpgrade(statusCode, headers, socket)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
onHeaders(statusCode, headers, resume) {
|
package/lib/utils.js
CHANGED
|
@@ -265,10 +265,6 @@ export class DecoratorHandler {
|
|
|
265
265
|
return this.#handler.onConnect?.(...args)
|
|
266
266
|
}
|
|
267
267
|
|
|
268
|
-
onError(...args) {
|
|
269
|
-
return this.#handler.onError?.(...args)
|
|
270
|
-
}
|
|
271
|
-
|
|
272
268
|
onUpgrade(...args) {
|
|
273
269
|
return this.#handler.onUpgrade?.(...args)
|
|
274
270
|
}
|
|
@@ -284,6 +280,10 @@ export class DecoratorHandler {
|
|
|
284
280
|
onComplete(...args) {
|
|
285
281
|
return this.#handler.onComplete?.(...args)
|
|
286
282
|
}
|
|
283
|
+
|
|
284
|
+
onError(...args) {
|
|
285
|
+
return this.#handler.onError?.(...args)
|
|
286
|
+
}
|
|
287
287
|
}
|
|
288
288
|
|
|
289
289
|
/**
|