Skip to content

Commit 83a1e4d

Browse files
committed
feat: sqlite add set and minor cleanup
1 parent d76f5cf commit 83a1e4d

File tree

3 files changed

+115
-54
lines changed

3 files changed

+115
-54
lines changed

lib/cache/sqlite-cache-store.js

Lines changed: 70 additions & 52 deletions
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
@@ -234,12 +241,12 @@ module.exports = class SqliteCacheStore {
234241
* @type {import('../../types/cache-interceptor.d.ts').default.GetResult}
235242
*/
236243
const result = {
237-
body: Buffer.from(value.body),
244+
body: value.body,
238245
statusCode: value.statusCode,
239246
statusMessage: value.statusMessage,
240247
headers: value.headers ? JSON.parse(value.headers) : undefined,
241248
etag: value.etag ? value.etag : undefined,
242-
vary: value.vary ?? undefined,
249+
vary: value.vary ? JSON.parse(value.vary) : undefined,
243250
cacheControlDirectives: value.cacheControlDirectives
244251
? JSON.parse(value.cacheControlDirectives)
245252
: undefined,
@@ -251,6 +258,56 @@ module.exports = class SqliteCacheStore {
251258
return result
252259
}
253260

261+
/**
262+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
263+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: null | Buffer | Array<Buffer>}} value
264+
*/
265+
set (key, value) {
266+
const url = this.#makeValueUrl(key)
267+
const body = Array.isArray(value.body) ? Buffer.concat(value.body) : value.body
268+
const size = body?.byteLength
269+
270+
if (size && size > this.#maxEntrySize) {
271+
return
272+
}
273+
274+
const existingValue = this.#findValue(key, true)
275+
if (existingValue) {
276+
// Updating an existing response, let's overwrite it
277+
this.#updateValueQuery.run(
278+
body,
279+
value.deleteAt,
280+
value.statusCode,
281+
value.statusMessage,
282+
value.headers ? JSON.stringify(value.headers) : null,
283+
value.etag ? value.etag : null,
284+
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
285+
value.cachedAt,
286+
value.staleAt,
287+
value.deleteAt,
288+
existingValue.id
289+
)
290+
} else {
291+
this.#prune()
292+
// New response, let's insert it
293+
this.#insertValueQuery.run(
294+
url,
295+
key.method,
296+
body,
297+
value.deleteAt,
298+
value.statusCode,
299+
value.statusMessage,
300+
value.headers ? JSON.stringify(value.headers) : null,
301+
value.etag ? value.etag : null,
302+
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
303+
value.vary ? JSON.stringify(value.vary) : null,
304+
value.cachedAt,
305+
value.staleAt,
306+
value.deleteAt
307+
)
308+
}
309+
}
310+
254311
/**
255312
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
256313
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} value
@@ -260,7 +317,6 @@ module.exports = class SqliteCacheStore {
260317
assertCacheKey(key)
261318
assertCacheValue(value)
262319

263-
const url = this.#makeValueUrl(key)
264320
let size = 0
265321
/**
266322
* @type {Buffer[] | null}
@@ -269,11 +325,8 @@ module.exports = class SqliteCacheStore {
269325
const store = this
270326

271327
return new Writable({
328+
decodeStrings: true,
272329
write (chunk, encoding, callback) {
273-
if (typeof chunk === 'string') {
274-
chunk = Buffer.from(chunk, encoding)
275-
}
276-
277330
size += chunk.byteLength
278331

279332
if (size < store.#maxEntrySize) {
@@ -285,42 +338,7 @@ module.exports = class SqliteCacheStore {
285338
callback()
286339
},
287340
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-
341+
store.set(key, { ...value, body })
324342
callback()
325343
}
326344
})
@@ -379,7 +397,7 @@ module.exports = class SqliteCacheStore {
379397
/**
380398
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
381399
* @param {boolean} [canBeExpired=false]
382-
* @returns {(SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined}
400+
* @returns {SqliteStoreValue | undefined}
383401
*/
384402
#findValue (key, canBeExpired = false) {
385403
const url = this.#makeValueUrl(key)
@@ -407,10 +425,10 @@ module.exports = class SqliteCacheStore {
407425
return undefined
408426
}
409427

410-
value.vary = JSON.parse(value.vary)
428+
const vary = JSON.parse(value.vary)
411429

412-
for (const header in value.vary) {
413-
if (!headerValueEquals(headers[header], value.vary[header])) {
430+
for (const header in vary) {
431+
if (!headerValueEquals(headers[header], vary[header])) {
414432
matches = false
415433
break
416434
}

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

Lines changed: 44 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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?: null | 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)