@@ -15,11 +15,18 @@ const MAX_ENTRY_SIZE = 2 * 1000 * 1000 * 1000
15
15
* @implements {CacheStore}
16
16
*
17
17
* @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
23
30
*/
24
31
module . exports = class SqliteCacheStore {
25
32
#maxEntrySize = MAX_ENTRY_SIZE
@@ -219,36 +226,80 @@ module.exports = class SqliteCacheStore {
219
226
220
227
/**
221
228
* @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 }
223
230
*/
224
231
get ( key ) {
225
232
assertCacheKey ( key )
226
233
227
234
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
+ }
228
252
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 )
232
259
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
249
266
}
250
267
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
+ }
252
303
}
253
304
254
305
/**
@@ -260,7 +311,6 @@ module.exports = class SqliteCacheStore {
260
311
assertCacheKey ( key )
261
312
assertCacheValue ( value )
262
313
263
- const url = this . #makeValueUrl( key )
264
314
let size = 0
265
315
/**
266
316
* @type {Buffer[] | null }
@@ -269,11 +319,8 @@ module.exports = class SqliteCacheStore {
269
319
const store = this
270
320
271
321
return new Writable ( {
322
+ decodeStrings : true ,
272
323
write ( chunk , encoding , callback ) {
273
- if ( typeof chunk === 'string' ) {
274
- chunk = Buffer . from ( chunk , encoding )
275
- }
276
-
277
324
size += chunk . byteLength
278
325
279
326
if ( size < store . #maxEntrySize) {
@@ -285,42 +332,7 @@ module.exports = class SqliteCacheStore {
285
332
callback ( )
286
333
} ,
287
334
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 } )
324
336
callback ( )
325
337
}
326
338
} )
@@ -379,7 +391,7 @@ module.exports = class SqliteCacheStore {
379
391
/**
380
392
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
381
393
* @param {boolean } [canBeExpired=false]
382
- * @returns {( SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined }
394
+ * @returns {SqliteStoreValue | undefined }
383
395
*/
384
396
#findValue ( key , canBeExpired = false ) {
385
397
const url = this . #makeValueUrl( key )
@@ -407,10 +419,10 @@ module.exports = class SqliteCacheStore {
407
419
return undefined
408
420
}
409
421
410
- value . vary = JSON . parse ( value . vary )
422
+ const vary = JSON . parse ( value . vary )
411
423
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 ] ) ) {
414
426
matches = false
415
427
break
416
428
}
0 commit comments