@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.
@@ -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 MAX_ENTRY_SIZE = 128 * 1024
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 > MAX_ENTRY_SIZE) {
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 > MAX_ENTRY_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), { store, handler }),
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
- let state = 0
179
-
180
- const resume = () => {
181
- try {
182
- let paused = false
183
- while (!paused && !aborted) {
184
- state += 1
185
- if (state === 1) {
186
- handler.onConnect(abort)
187
- } else if (state === 2) {
188
- paused = handler.onHeaders(statusCode, headers, resume) === false
189
- } else if (state === 3) {
190
- paused = handler.onData(body ?? EMPTY_BUFFER) === false
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
- resume()
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(...args) {
279
+ onUpgrade(statusCode, headers, socket) {
280
280
  if (!this.#aborted && !this.#errored) {
281
- return this.#handler.onUpgrade?.(...args)
281
+ return this.#handler.onUpgrade?.(statusCode, headers, socket)
282
282
  }
283
283
  }
284
284
 
285
- onHeaders(...args) {
285
+ onHeaders(statusCode, headers, resume) {
286
286
  if (!this.#aborted && !this.#errored) {
287
- return this.#handler.onHeaders?.(...args)
287
+ return this.#handler.onHeaders?.(statusCode, headers, resume)
288
288
  }
289
289
  }
290
290
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.0.21",
3
+ "version": "6.0.23",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",