@nxtedition/nxt-undici 6.0.6 → 6.0.7
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 +37 -28
- 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
|
@@ -42,20 +42,26 @@ class CacheHandler extends DecoratorHandler {
|
|
|
42
42
|
return super.onHeaders(statusCode, headers, resume)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
const
|
|
45
|
+
const cacheControlDirectives = parseCacheControl(headers['cache-control']) ?? {}
|
|
46
|
+
|
|
47
|
+
if (this.#key.headers.authorization && !cacheControlDirectives.public) {
|
|
48
|
+
return super.onHeaders(statusCode, headers, resume)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (cacheControlDirectives.private || cacheControlDirectives['no-store']) {
|
|
52
|
+
return super.onHeaders(statusCode, headers, resume)
|
|
53
|
+
}
|
|
54
|
+
|
|
46
55
|
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
cacheControl['must-understand'] ||
|
|
55
|
-
cacheControl['must-revalidate'] ||
|
|
56
|
-
cacheControl['proxy-revalidate']
|
|
56
|
+
cacheControlDirectives['no-transform'] ||
|
|
57
|
+
cacheControlDirectives['must-revalidate'] ||
|
|
58
|
+
cacheControlDirectives['proxy-revalidate'] ||
|
|
59
|
+
cacheControlDirectives['must-understand'] ||
|
|
60
|
+
cacheControlDirectives['stale-while-revalidate'] ||
|
|
61
|
+
cacheControlDirectives['stale-if-error'] ||
|
|
62
|
+
cacheControlDirectives['no-cache']
|
|
57
63
|
) {
|
|
58
|
-
//
|
|
64
|
+
// TODO (fix): Support all cache control directives...
|
|
59
65
|
return super.onHeaders(statusCode, headers, resume)
|
|
60
66
|
}
|
|
61
67
|
|
|
@@ -76,9 +82,9 @@ class CacheHandler extends DecoratorHandler {
|
|
|
76
82
|
return super.onHeaders(statusCode, headers, resume)
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
const ttl =
|
|
85
|
+
const ttl = cacheControlDirectives.immutable
|
|
80
86
|
? 31556952
|
|
81
|
-
: Number(
|
|
87
|
+
: Number(cacheControlDirectives['s-max-age'] ?? cacheControlDirectives['max-age'])
|
|
82
88
|
if (!ttl || !Number.isFinite(ttl) || ttl <= 0) {
|
|
83
89
|
return super.onHeaders(statusCode, headers, resume)
|
|
84
90
|
}
|
|
@@ -92,8 +98,8 @@ class CacheHandler extends DecoratorHandler {
|
|
|
92
98
|
statusCode,
|
|
93
99
|
statusMessage: '',
|
|
94
100
|
headers,
|
|
95
|
-
cacheControlDirectives
|
|
96
|
-
etag:
|
|
101
|
+
cacheControlDirectives,
|
|
102
|
+
etag: headers.etag,
|
|
97
103
|
vary,
|
|
98
104
|
cachedAt,
|
|
99
105
|
staleAt: 0,
|
|
@@ -134,16 +140,17 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
134
140
|
return dispatch(opts, handler)
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
|
|
143
|
+
const cacheControlDirectives = parseCacheControl(opts?.headers['cache-control']) ?? {}
|
|
144
|
+
|
|
145
|
+
if (
|
|
146
|
+
cacheControlDirectives['max-age'] ||
|
|
147
|
+
cacheControlDirectives['max-stale'] ||
|
|
148
|
+
cacheControlDirectives['min-fresh'] ||
|
|
149
|
+
cacheControlDirectives['no-cache'] ||
|
|
150
|
+
cacheControlDirectives['no-transform'] ||
|
|
151
|
+
cacheControlDirectives['stale-if-error']
|
|
152
|
+
) {
|
|
138
153
|
// 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
154
|
return dispatch(opts, handler)
|
|
148
155
|
}
|
|
149
156
|
|
|
@@ -152,10 +159,12 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
152
159
|
|
|
153
160
|
const store = opts.cache.store ?? DEFAULT_STORE
|
|
154
161
|
const entry = store.get(opts)
|
|
155
|
-
if (!entry) {
|
|
162
|
+
if (!entry && !cacheControlDirectives['only-if-cached']) {
|
|
156
163
|
return dispatch(
|
|
157
164
|
opts,
|
|
158
|
-
|
|
165
|
+
cacheControlDirectives['no-store']
|
|
166
|
+
? handler
|
|
167
|
+
: new CacheHandler(undici.util.cache.makeCacheKey(opts), { store, handler }),
|
|
159
168
|
)
|
|
160
169
|
}
|
|
161
170
|
|
|
@@ -174,7 +183,7 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
174
183
|
}
|
|
175
184
|
}
|
|
176
185
|
|
|
177
|
-
const { statusCode, headers } = entry
|
|
186
|
+
const { statusCode, headers } = entry ?? { statusCode: 504, headers: {} }
|
|
178
187
|
try {
|
|
179
188
|
handler.onConnect(abort)
|
|
180
189
|
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
|
/**
|