@nxtedition/nxt-undici 2.2.0 → 2.2.1

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