@nxtedition/nxt-undici 6.2.24 → 6.2.26

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.
@@ -1,8 +1,8 @@
1
1
  import { DatabaseSync } from 'node:sqlite'
2
2
  import assert from 'node:assert'
3
- import { parseRangeHeader } from './utils.js'
3
+ import { parseRangeHeader, getFastNow } from './utils.js'
4
4
 
5
- const VERSION = 6
5
+ const VERSION = 7
6
6
 
7
7
  /**
8
8
  * @typedef {import('undici-types/cache-interceptor.d.ts').default.CacheStore} CacheStore
@@ -46,9 +46,9 @@ export class SqliteCacheStore {
46
46
  #deleteExpiredValuesQuery
47
47
 
48
48
  /**
49
- * @type {import('node:sqlite').StatementSync}
49
+ * @type {number}
50
50
  */
51
- #deleteByUrlQuery
51
+ #deleteExpiredValuesTime = getFastNow()
52
52
 
53
53
  /**
54
54
  * @param {import('undici-types/cache-interceptor.d.ts').default.SqliteCacheStoreOpts & { maxEntryCount?: number} | undefined} opts
@@ -75,10 +75,8 @@ export class SqliteCacheStore {
75
75
  staleAt INTEGER NOT NULL
76
76
  );
77
77
 
78
- CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_url ON cacheInterceptorV${VERSION}(url);
79
- CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_method ON cacheInterceptorV${VERSION}(method);
80
- CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_deleteAt ON cacheInterceptorV${VERSION}(deleteAt);
81
- CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_start ON cacheInterceptorV${VERSION}(start);
78
+ CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_getValuesQuery ON cacheInterceptorV${VERSION}(url, method, start, deleteAt);
79
+ CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_deleteExpiredValuesQuery ON cacheInterceptorV${VERSION}(deleteAt);
82
80
  `)
83
81
 
84
82
  this.#getValuesQuery = this.#db.prepare(`
@@ -122,10 +120,6 @@ export class SqliteCacheStore {
122
120
  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
123
121
  `)
124
122
 
125
- this.#deleteByUrlQuery = this.#db.prepare(
126
- `DELETE FROM cacheInterceptorV${VERSION} WHERE url = ?`,
127
- )
128
-
129
123
  this.#deleteExpiredValuesQuery = this.#db.prepare(
130
124
  `DELETE FROM cacheInterceptorV${VERSION} WHERE deleteAt <= ?`,
131
125
  )
@@ -160,7 +154,10 @@ export class SqliteCacheStore {
160
154
  assert(Number.isFinite(value.end))
161
155
  assert(!body || body?.byteLength === value.end - value.start)
162
156
 
163
- this.#deleteExpiredValuesQuery.run(Date.now())
157
+ const now = getFastNow()
158
+ if (now > this.#deleteExpiredValuesTime + 60e3) {
159
+ this.#deleteExpiredValuesQuery.run(now)
160
+ }
164
161
 
165
162
  this.#insertValueQuery.run(
166
163
  makeValueUrl(key),
@@ -180,17 +177,6 @@ export class SqliteCacheStore {
180
177
  )
181
178
  }
182
179
 
183
- /**
184
- * @param {import('undici-types/cache-interceptor.d.ts').default.CacheKey} key
185
- */
186
- delete(key) {
187
- if (typeof key !== 'object') {
188
- throw new TypeError(`expected key to be object, got ${typeof key}`)
189
- }
190
-
191
- this.#deleteByUrlQuery.run(makeValueUrl(key))
192
- }
193
-
194
180
  /**
195
181
  * @param {import('undici-types/cache-interceptor.d.ts').default.CacheKey} key
196
182
  * @param {boolean} [canBeExpired=false]
@@ -214,7 +200,7 @@ export class SqliteCacheStore {
214
200
  return undefined
215
201
  }
216
202
 
217
- const now = Date.now()
203
+ const now = getFastNow()
218
204
  for (const value of values) {
219
205
  if (now >= value.deleteAt && !canBeExpired) {
220
206
  return undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.2.24",
3
+ "version": "6.2.26",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",