@nxtedition/nxt-undici 2.2.0 → 2.2.3

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.
@@ -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,17 @@ class CacheHandler extends DecoratorHandler {
50
54
 
51
55
  if (ttl > 0) {
52
56
  this.#value = {
53
- statusCode,
54
- statusMessage,
55
- rawHeaders,
56
- rawTrailers: null,
57
- body: [],
58
- size: 0,
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) ?? 0) +
66
+ (statusMessage?.length ?? 0) +
67
+ 64,
59
68
  ttl: ttl * 1e3,
60
69
  }
61
70
  }
@@ -67,10 +76,12 @@ class CacheHandler extends DecoratorHandler {
67
76
  onData(chunk) {
68
77
  if (this.#value) {
69
78
  this.#value.size += chunk.bodyLength
70
- if (this.#value.size > this.#store.maxEntrySize) {
79
+
80
+ const maxEntrySize = this.#store.maxEntrySize ?? Infinity
81
+ if (this.#value.size > maxEntrySize) {
71
82
  this.#value = null
72
83
  } else {
73
- this.#value.body.push(chunk)
84
+ this.#value.data.body.push(chunk)
74
85
  }
75
86
  }
76
87
  return this.#handler.onData(chunk)
@@ -78,8 +89,9 @@ class CacheHandler extends DecoratorHandler {
78
89
 
79
90
  onComplete(rawTrailers) {
80
91
  if (this.#value) {
81
- this.#value.rawTrailers = rawTrailers
82
- this.#store.set(this.#key, this.#value, this.#value.ttl)
92
+ this.#value.data.rawTrailers = rawTrailers
93
+ this.#value.size += rawTrailers?.reduce((xs, x) => xs + x.length, 0) ?? 0
94
+ this.#store.set(this.#key, this.#value.data, { ttl: this.#value.ttl, size: this.#value.size })
83
95
  }
84
96
  return this.#handler.onComplete(rawTrailers)
85
97
  }
@@ -87,17 +99,14 @@ class CacheHandler extends DecoratorHandler {
87
99
 
88
100
  // TODO (fix): Async filesystem cache.
89
101
  class CacheStore {
90
- constructor({ maxSize, maxEntrySize }) {
102
+ constructor({ maxSize = 1024 * 1024, maxEntrySize = 128 * 1024 }) {
91
103
  this.maxSize = maxSize
92
104
  this.maxEntrySize = maxEntrySize
93
- this.cache = new LRUCache({
94
- maxSize,
95
- sizeCalculation: (value) => value.size,
96
- })
105
+ this.cache = new LRUCache({ maxSize })
97
106
  }
98
107
 
99
- set(key, value, ttl) {
100
- this.cache.set(key, value, ttl)
108
+ set(key, value, opts) {
109
+ this.cache.set(key, value, opts)
101
110
  }
102
111
 
103
112
  get(key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "2.2.0",
3
+ "version": "2.2.3",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",