@nxtedition/nxt-undici 6.0.22 → 6.1.0
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/index.js
CHANGED
|
@@ -59,6 +59,7 @@ function wrapDispatch(dispatcher) {
|
|
|
59
59
|
if (wrappedDispatcher == null) {
|
|
60
60
|
wrappedDispatcher = compose(
|
|
61
61
|
dispatcher,
|
|
62
|
+
interceptors.log({ bindings: { intercept: 'upstream' } }),
|
|
62
63
|
interceptors.responseError(),
|
|
63
64
|
interceptors.requestBodyFactory(),
|
|
64
65
|
interceptors.dns(),
|
|
@@ -69,7 +70,7 @@ function wrapDispatch(dispatcher) {
|
|
|
69
70
|
interceptors.proxy(),
|
|
70
71
|
interceptors.cache(),
|
|
71
72
|
interceptors.redirect(),
|
|
72
|
-
interceptors.log(),
|
|
73
|
+
interceptors.log({ bindings: { intercept: 'downstream' } }),
|
|
73
74
|
(dispatch) => (opts, handler) => {
|
|
74
75
|
const headers = parseHeaders(opts.headers)
|
|
75
76
|
|
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
|
}
|
|
@@ -53,11 +55,17 @@ class CacheHandler extends DecoratorHandler {
|
|
|
53
55
|
return super.onHeaders(statusCode, headers, resume)
|
|
54
56
|
}
|
|
55
57
|
|
|
58
|
+
if (cacheControlDirectives['must-understand']) {
|
|
59
|
+
// Do nothing. We only cache responses that we understand...
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (cacheControlDirectives['no-transform']) {
|
|
63
|
+
// Do nothing. We don't transform responses...
|
|
64
|
+
}
|
|
65
|
+
|
|
56
66
|
if (
|
|
57
|
-
cacheControlDirectives['no-transform'] ||
|
|
58
67
|
cacheControlDirectives['must-revalidate'] ||
|
|
59
68
|
cacheControlDirectives['proxy-revalidate'] ||
|
|
60
|
-
cacheControlDirectives['must-understand'] ||
|
|
61
69
|
cacheControlDirectives['stale-while-revalidate'] ||
|
|
62
70
|
cacheControlDirectives['stale-if-error'] ||
|
|
63
71
|
cacheControlDirectives['no-cache']
|
|
@@ -111,7 +119,7 @@ class CacheHandler extends DecoratorHandler {
|
|
|
111
119
|
this.#value.size += chunk.length
|
|
112
120
|
this.#value.body.push(chunk)
|
|
113
121
|
|
|
114
|
-
if (this.#value.size >
|
|
122
|
+
if (this.#value.size > this.#maxEntrySize) {
|
|
115
123
|
this.#value = null
|
|
116
124
|
this.#value.size = 0
|
|
117
125
|
}
|
|
@@ -140,12 +148,15 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
140
148
|
|
|
141
149
|
const cacheControlDirectives = parseCacheControl(opts?.headers['cache-control']) ?? {}
|
|
142
150
|
|
|
151
|
+
if (cacheControlDirectives['no-transform']) {
|
|
152
|
+
// Do nothing. We don't transform requests...
|
|
153
|
+
}
|
|
154
|
+
|
|
143
155
|
if (
|
|
144
156
|
cacheControlDirectives['max-age'] ||
|
|
145
157
|
cacheControlDirectives['max-stale'] ||
|
|
146
158
|
cacheControlDirectives['min-fresh'] ||
|
|
147
159
|
cacheControlDirectives['no-cache'] ||
|
|
148
|
-
cacheControlDirectives['no-transform'] ||
|
|
149
160
|
cacheControlDirectives['stale-if-error']
|
|
150
161
|
) {
|
|
151
162
|
// TODO (fix): Support all cache control directives...
|
|
@@ -162,7 +173,11 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
162
173
|
opts,
|
|
163
174
|
cacheControlDirectives['no-store']
|
|
164
175
|
? handler
|
|
165
|
-
: new CacheHandler(undici.util.cache.makeCacheKey(opts), {
|
|
176
|
+
: new CacheHandler(undici.util.cache.makeCacheKey(opts), {
|
|
177
|
+
maxEntrySize: opts.cache.maxEntrySize,
|
|
178
|
+
store,
|
|
179
|
+
handler,
|
|
180
|
+
}),
|
|
166
181
|
)
|
|
167
182
|
}
|
|
168
183
|
|
package/lib/interceptor/log.js
CHANGED
|
@@ -19,12 +19,16 @@ class Handler extends DecoratorHandler {
|
|
|
19
19
|
#statusCode
|
|
20
20
|
#headers
|
|
21
21
|
|
|
22
|
-
constructor(opts, { handler }) {
|
|
22
|
+
constructor(logOpts, opts, { handler }) {
|
|
23
23
|
super(handler)
|
|
24
24
|
|
|
25
25
|
this.#opts = opts
|
|
26
26
|
this.#logger = opts.logger.child({ ureq: opts })
|
|
27
27
|
|
|
28
|
+
if (logOpts?.bindings) {
|
|
29
|
+
this.#logger = this.#logger.child(logOpts?.bindings)
|
|
30
|
+
}
|
|
31
|
+
|
|
28
32
|
this.#created = performance.now()
|
|
29
33
|
}
|
|
30
34
|
|
|
@@ -128,5 +132,5 @@ class Handler extends DecoratorHandler {
|
|
|
128
132
|
}
|
|
129
133
|
}
|
|
130
134
|
|
|
131
|
-
export default () => (dispatch) => (opts, handler) =>
|
|
132
|
-
opts.logger ? dispatch(opts, new Handler(opts, { handler })) : dispatch(opts, handler)
|
|
135
|
+
export default (logOpts) => (dispatch) => (opts, handler) =>
|
|
136
|
+
opts.logger ? dispatch(opts, new Handler(logOpts, opts, { handler })) : dispatch(opts, handler)
|
|
@@ -178,7 +178,7 @@ class Handler extends DecoratorHandler {
|
|
|
178
178
|
this.#onError(this.#error)
|
|
179
179
|
} else if (!this.#headersSent) {
|
|
180
180
|
this.#retryCount++
|
|
181
|
-
this.#opts.logger?.debug({ retryCount: this.#retryCount }, 'retry response headers')
|
|
181
|
+
this.#opts.logger?.debug({ err, retryCount: this.#retryCount }, 'retry response headers')
|
|
182
182
|
this.#dispatch(this.#opts, this)
|
|
183
183
|
} else {
|
|
184
184
|
assert(Number.isFinite(this.#pos))
|
|
@@ -196,7 +196,7 @@ class Handler extends DecoratorHandler {
|
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
this.#retryCount++
|
|
199
|
-
this.#opts.logger?.debug({ retryCount: this.#retryCount }, 'retry response body')
|
|
199
|
+
this.#opts.logger?.debug({ err, retryCount: this.#retryCount }, 'retry response body')
|
|
200
200
|
this.#dispatch(this.#opts, this)
|
|
201
201
|
}
|
|
202
202
|
})
|