Skip to content

Commit 316c7a4

Browse files
committed
feat: sqlite add set and minor cleanup
1 parent d76f5cf commit 316c7a4

File tree

3 files changed

+128
-73
lines changed

3 files changed

+128
-73
lines changed

lib/cache/sqlite-cache-store.js

+83-71
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ const MAX_ENTRY_SIZE = 2 * 1000 * 1000 * 1000
1515
* @implements {CacheStore}
1616
*
1717
* @typedef {{
18-
* id: Readonly<number>
19-
* headers?: Record<string, string | string[]>
20-
* vary?: string | object
21-
* body: string
22-
* } & import('../../types/cache-interceptor.d.ts').default.CacheValue} SqliteStoreValue
18+
* id: Readonly<number>,
19+
* body?: Buffer
20+
* statusCode: number
21+
* statusMessage: string
22+
* headers?: string
23+
* vary?: string
24+
* etag?: string
25+
* cacheControlDirectives?: string
26+
* cachedAt: number
27+
* staleAt: number
28+
* deleteAt: number
29+
* }} SqliteStoreValue
2330
*/
2431
module.exports = class SqliteCacheStore {
2532
#maxEntrySize = MAX_ENTRY_SIZE
@@ -219,36 +226,80 @@ module.exports = class SqliteCacheStore {
219226

220227
/**
221228
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
222-
* @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined}
229+
* @returns {(import('../../types/cache-interceptor.d.ts').default.GetResult & { body?: Buffer }) | undefined}
223230
*/
224231
get (key) {
225232
assertCacheKey(key)
226233

227234
const value = this.#findValue(key)
235+
return value
236+
? {
237+
body: Buffer.from(value.body),
238+
statusCode: value.statusCode,
239+
statusMessage: value.statusMessage,
240+
headers: value.headers ? JSON.parse(value.headers) : undefined,
241+
etag: value.etag ? value.etag : undefined,
242+
vary: value.vary ? JSON.parse(value.vary) : undefined,
243+
cacheControlDirectives: value.cacheControlDirectives
244+
? JSON.parse(value.cacheControlDirectives)
245+
: undefined,
246+
cachedAt: value.cachedAt,
247+
staleAt: value.staleAt,
248+
deleteAt: value.deleteAt
249+
}
250+
: undefined
251+
}
228252

229-
if (!value) {
230-
return undefined
231-
}
253+
/**
254+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
255+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: null | Buffer | Array<Buffer>}} value
256+
*/
257+
set (key, value) {
258+
assertCacheKey(key)
232259

233-
/**
234-
* @type {import('../../types/cache-interceptor.d.ts').default.GetResult}
235-
*/
236-
const result = {
237-
body: Buffer.from(value.body),
238-
statusCode: value.statusCode,
239-
statusMessage: value.statusMessage,
240-
headers: value.headers ? JSON.parse(value.headers) : undefined,
241-
etag: value.etag ? value.etag : undefined,
242-
vary: value.vary ?? undefined,
243-
cacheControlDirectives: value.cacheControlDirectives
244-
? JSON.parse(value.cacheControlDirectives)
245-
: undefined,
246-
cachedAt: value.cachedAt,
247-
staleAt: value.staleAt,
248-
deleteAt: value.deleteAt
260+
const url = this.#makeValueUrl(key)
261+
const body = Array.isArray(value.body) ? Buffer.concat(value.body) : value.body
262+
const size = body?.byteLength
263+
264+
if (size && size > this.#maxEntrySize) {
265+
return
249266
}
250267

251-
return result
268+
const existingValue = this.#findValue(key, true)
269+
if (existingValue) {
270+
// Updating an existing response, let's overwrite it
271+
this.#updateValueQuery.run(
272+
body,
273+
value.deleteAt,
274+
value.statusCode,
275+
value.statusMessage,
276+
value.headers ? JSON.stringify(value.headers) : null,
277+
value.etag ? value.etag : null,
278+
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
279+
value.cachedAt,
280+
value.staleAt,
281+
value.deleteAt,
282+
existingValue.id
283+
)
284+
} else {
285+
this.#prune()
286+
// New response, let's insert it
287+
this.#insertValueQuery.run(
288+
url,
289+
key.method,
290+
body,
291+
value.deleteAt,
292+
value.statusCode,
293+
value.statusMessage,
294+
value.headers ? JSON.stringify(value.headers) : null,
295+
value.etag ? value.etag : null,
296+
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
297+
value.vary ? JSON.stringify(value.vary) : null,
298+
value.cachedAt,
299+
value.staleAt,
300+
value.deleteAt
301+
)
302+
}
252303
}
253304

254305
/**
@@ -260,7 +311,6 @@ module.exports = class SqliteCacheStore {
260311
assertCacheKey(key)
261312
assertCacheValue(value)
262313

263-
const url = this.#makeValueUrl(key)
264314
let size = 0
265315
/**
266316
* @type {Buffer[] | null}
@@ -269,11 +319,8 @@ module.exports = class SqliteCacheStore {
269319
const store = this
270320

271321
return new Writable({
322+
decodeStrings: true,
272323
write (chunk, encoding, callback) {
273-
if (typeof chunk === 'string') {
274-
chunk = Buffer.from(chunk, encoding)
275-
}
276-
277324
size += chunk.byteLength
278325

279326
if (size < store.#maxEntrySize) {
@@ -285,42 +332,7 @@ module.exports = class SqliteCacheStore {
285332
callback()
286333
},
287334
final (callback) {
288-
const existingValue = store.#findValue(key, true)
289-
if (existingValue) {
290-
// Updating an existing response, let's overwrite it
291-
store.#updateValueQuery.run(
292-
Buffer.concat(body),
293-
value.deleteAt,
294-
value.statusCode,
295-
value.statusMessage,
296-
value.headers ? JSON.stringify(value.headers) : null,
297-
value.etag ? value.etag : null,
298-
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
299-
value.cachedAt,
300-
value.staleAt,
301-
value.deleteAt,
302-
existingValue.id
303-
)
304-
} else {
305-
store.#prune()
306-
// New response, let's insert it
307-
store.#insertValueQuery.run(
308-
url,
309-
key.method,
310-
Buffer.concat(body),
311-
value.deleteAt,
312-
value.statusCode,
313-
value.statusMessage,
314-
value.headers ? JSON.stringify(value.headers) : null,
315-
value.etag ? value.etag : null,
316-
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
317-
value.vary ? JSON.stringify(value.vary) : null,
318-
value.cachedAt,
319-
value.staleAt,
320-
value.deleteAt
321-
)
322-
}
323-
335+
store.set(key, { ...value, body })
324336
callback()
325337
}
326338
})
@@ -379,7 +391,7 @@ module.exports = class SqliteCacheStore {
379391
/**
380392
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
381393
* @param {boolean} [canBeExpired=false]
382-
* @returns {(SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined}
394+
* @returns {SqliteStoreValue | undefined}
383395
*/
384396
#findValue (key, canBeExpired = false) {
385397
const url = this.#makeValueUrl(key)
@@ -407,10 +419,10 @@ module.exports = class SqliteCacheStore {
407419
return undefined
408420
}
409421

410-
value.vary = JSON.parse(value.vary)
422+
const vary = JSON.parse(value.vary)
411423

412-
for (const header in value.vary) {
413-
if (!headerValueEquals(headers[header], value.vary[header])) {
424+
for (const header in vary) {
425+
if (!headerValueEquals(headers[header], vary[header])) {
414426
matches = false
415427
break
416428
}

test/cache-interceptor/sqlite-cache-store-tests.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { test, skip } = require('node:test')
4-
const { notEqual, strictEqual } = require('node:assert')
4+
const { notEqual, strictEqual, deepStrictEqual } = require('node:assert')
55
const { rm } = require('node:fs/promises')
66
const { cacheStoreTests, writeBody, compareGetResults } = require('./cache-store-test-utils.js')
77

@@ -179,3 +179,46 @@ test('SqliteCacheStore two writes', async (t) => {
179179
writeBody(writable, body)
180180
}
181181
})
182+
183+
test('SqliteCacheStore write & read', async (t) => {
184+
if (!hasSqlite) {
185+
t.skip()
186+
return
187+
}
188+
189+
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
190+
191+
const store = new SqliteCacheStore({
192+
maxCount: 10
193+
})
194+
195+
/**
196+
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
197+
*/
198+
const key = {
199+
origin: 'localhost',
200+
path: '/',
201+
method: 'GET',
202+
headers: {}
203+
}
204+
205+
/**
206+
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: Buffer }}
207+
*/
208+
const value = {
209+
statusCode: 200,
210+
statusMessage: '',
211+
headers: { foo: 'bar' },
212+
cacheControlDirectives: { 'max-stale': 0 },
213+
cachedAt: Date.now(),
214+
staleAt: Date.now() + 10000,
215+
deleteAt: Date.now() + 20000,
216+
body: Buffer.from('asd'),
217+
etag: undefined,
218+
vary: undefined
219+
}
220+
221+
store.set(key, value)
222+
223+
deepStrictEqual(store.get(key), value)
224+
})

types/cache-interceptor.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ declare namespace CacheHandler {
9090
headers: Record<string, string | string[]>
9191
vary?: Record<string, string | string[]>
9292
etag?: string
93-
body: null | Readable | Iterable<Buffer> | AsyncIterable<Buffer> | Buffer | Iterable<string> | AsyncIterable<string> | string
93+
body?: Readable | Iterable<Buffer> | AsyncIterable<Buffer> | Buffer | Iterable<string> | AsyncIterable<string> | string
9494
cacheControlDirectives: CacheControlDirectives,
9595
cachedAt: number
9696
staleAt: number

0 commit comments

Comments
 (0)