@@ -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?: Uint8Array
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
@@ -217,36 +224,78 @@ module.exports = class SqliteCacheStore {
217
224
218
225
/**
219
226
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
220
- * @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined }
227
+ * @returns {( import('../../types/cache-interceptor.d.ts').default.GetResult & { body?: Buffer }) | undefined }
221
228
*/
222
229
get ( key ) {
223
230
assertCacheKey ( key )
224
231
225
232
const value = this . #findValue( key )
233
+ return value
234
+ ? {
235
+ body : value . body ? Buffer . from ( value . body . buffer ) : undefined ,
236
+ statusCode : value . statusCode ,
237
+ statusMessage : value . statusMessage ,
238
+ headers : value . headers ? JSON . parse ( value . headers ) : undefined ,
239
+ etag : value . etag ? value . etag : undefined ,
240
+ vary : value . vary ? JSON . parse ( value . vary ) : undefined ,
241
+ cacheControlDirectives : value . cacheControlDirectives
242
+ ? JSON . parse ( value . cacheControlDirectives )
243
+ : undefined ,
244
+ cachedAt : value . cachedAt ,
245
+ staleAt : value . staleAt ,
246
+ deleteAt : value . deleteAt
247
+ }
248
+ : undefined
249
+ }
226
250
227
- if ( ! value ) {
228
- return undefined
229
- }
251
+ /**
252
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
253
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: null | Buffer | Array<Buffer>} } value
254
+ */
255
+ set ( key , value ) {
256
+ assertCacheKey ( key )
230
257
231
- /**
232
- * @type {import('../../types/cache-interceptor.d.ts').default.GetResult }
233
- */
234
- const result = {
235
- body : Buffer . from ( value . body ) ,
236
- statusCode : value . statusCode ,
237
- statusMessage : value . statusMessage ,
238
- headers : value . headers ? JSON . parse ( value . headers ) : undefined ,
239
- etag : value . etag ? value . etag : undefined ,
240
- vary : value . vary ?? undefined ,
241
- cacheControlDirectives : value . cacheControlDirectives
242
- ? JSON . parse ( value . cacheControlDirectives )
243
- : undefined ,
244
- cachedAt : value . cachedAt ,
245
- staleAt : value . staleAt ,
246
- deleteAt : value . deleteAt
258
+ const url = this . #makeValueUrl( key )
259
+ const body = Array . isArray ( value . body ) ? Buffer . concat ( value . body ) : value . body
260
+ const size = body ?. byteLength
261
+
262
+ if ( size && size > this . #maxEntrySize) {
263
+ return
247
264
}
248
265
249
- return result
266
+ const existingValue = this . #findValue( key , true )
267
+ if ( existingValue ) {
268
+ // Updating an existing response, let's overwrite it
269
+ this . #updateValueQuery. run (
270
+ body ,
271
+ value . deleteAt ,
272
+ value . statusCode ,
273
+ value . statusMessage ,
274
+ value . headers ? JSON . stringify ( value . headers ) : null ,
275
+ value . etag ? value . etag : null ,
276
+ value . cacheControlDirectives ? JSON . stringify ( value . cacheControlDirectives ) : null ,
277
+ value . cachedAt ,
278
+ value . staleAt ,
279
+ existingValue . id
280
+ )
281
+ } else {
282
+ this . #prune( )
283
+ // New response, let's insert it
284
+ this . #insertValueQuery. run (
285
+ url ,
286
+ key . method ,
287
+ body ,
288
+ value . deleteAt ,
289
+ value . statusCode ,
290
+ value . statusMessage ,
291
+ value . headers ? JSON . stringify ( value . headers ) : null ,
292
+ value . etag ? value . etag : null ,
293
+ value . cacheControlDirectives ? JSON . stringify ( value . cacheControlDirectives ) : null ,
294
+ value . vary ? JSON . stringify ( value . vary ) : null ,
295
+ value . cachedAt ,
296
+ value . staleAt
297
+ )
298
+ }
250
299
}
251
300
252
301
/**
@@ -258,7 +307,6 @@ module.exports = class SqliteCacheStore {
258
307
assertCacheKey ( key )
259
308
assertCacheValue ( value )
260
309
261
- const url = this . #makeValueUrl( key )
262
310
let size = 0
263
311
/**
264
312
* @type {Buffer[] | null }
@@ -267,11 +315,8 @@ module.exports = class SqliteCacheStore {
267
315
const store = this
268
316
269
317
return new Writable ( {
318
+ decodeStrings : true ,
270
319
write ( chunk , encoding , callback ) {
271
- if ( typeof chunk === 'string' ) {
272
- chunk = Buffer . from ( chunk , encoding )
273
- }
274
-
275
320
size += chunk . byteLength
276
321
277
322
if ( size < store . #maxEntrySize) {
@@ -283,40 +328,7 @@ module.exports = class SqliteCacheStore {
283
328
callback ( )
284
329
} ,
285
330
final ( callback ) {
286
- const existingValue = store . #findValue( key , true )
287
- if ( existingValue ) {
288
- // Updating an existing response, let's overwrite it
289
- store . #updateValueQuery. run (
290
- Buffer . concat ( 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 . cachedAt ,
298
- value . staleAt ,
299
- existingValue . id
300
- )
301
- } else {
302
- store . #prune( )
303
- // New response, let's insert it
304
- store . #insertValueQuery. run (
305
- url ,
306
- key . method ,
307
- Buffer . concat ( body ) ,
308
- value . deleteAt ,
309
- value . statusCode ,
310
- value . statusMessage ,
311
- value . headers ? JSON . stringify ( value . headers ) : null ,
312
- value . etag ? value . etag : null ,
313
- value . cacheControlDirectives ? JSON . stringify ( value . cacheControlDirectives ) : null ,
314
- value . vary ? JSON . stringify ( value . vary ) : null ,
315
- value . cachedAt ,
316
- value . staleAt
317
- )
318
- }
319
-
331
+ store . set ( key , { ...value , body } )
320
332
callback ( )
321
333
}
322
334
} )
@@ -375,7 +387,7 @@ module.exports = class SqliteCacheStore {
375
387
/**
376
388
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
377
389
* @param {boolean } [canBeExpired=false]
378
- * @returns {( SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined }
390
+ * @returns {SqliteStoreValue | undefined }
379
391
*/
380
392
#findValue ( key , canBeExpired = false ) {
381
393
const url = this . #makeValueUrl( key )
@@ -403,10 +415,10 @@ module.exports = class SqliteCacheStore {
403
415
return undefined
404
416
}
405
417
406
- value . vary = JSON . parse ( value . vary )
418
+ const vary = JSON . parse ( value . vary )
407
419
408
- for ( const header in value . vary ) {
409
- if ( ! headerValueEquals ( headers [ header ] , value . vary [ header ] ) ) {
420
+ for ( const header in vary ) {
421
+ if ( ! headerValueEquals ( headers [ header ] , vary [ header ] ) ) {
410
422
matches = false
411
423
break
412
424
}
0 commit comments