@@ -239,7 +239,7 @@ module.exports = class SqliteCacheStore {
239
239
statusMessage : value . statusMessage ,
240
240
headers : value . headers ? JSON . parse ( value . headers ) : undefined ,
241
241
etag : value . etag ? value . etag : undefined ,
242
- vary : value . vary ?? undefined ,
242
+ vary : value . vary ? JSON . parse ( value . vary ) : undefined ,
243
243
cacheControlDirectives : value . cacheControlDirectives
244
244
? JSON . parse ( value . cacheControlDirectives )
245
245
: undefined ,
@@ -251,6 +251,56 @@ module.exports = class SqliteCacheStore {
251
251
return result
252
252
}
253
253
254
+ /**
255
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
256
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: null | Buffer | Array<Buffer>} } value
257
+ */
258
+ set ( key , value ) {
259
+ const url = this . #makeValueUrl( key )
260
+ const body = Array . isArray ( value . body ) ? Buffer . concat ( value . body ) : value . body
261
+ const size = body ?. byteLength
262
+
263
+ if ( size && size > this . #maxEntrySize) {
264
+ return
265
+ }
266
+
267
+ const existingValue = this . #findValue( key , true )
268
+ if ( existingValue ) {
269
+ // Updating an existing response, let's overwrite it
270
+ this . #updateValueQuery. run (
271
+ body ,
272
+ value . deleteAt ,
273
+ value . statusCode ,
274
+ value . statusMessage ,
275
+ value . headers ? JSON . stringify ( value . headers ) : null ,
276
+ value . etag ? value . etag : null ,
277
+ value . cacheControlDirectives ? JSON . stringify ( value . cacheControlDirectives ) : null ,
278
+ value . cachedAt ,
279
+ value . staleAt ,
280
+ value . deleteAt ,
281
+ existingValue . id
282
+ )
283
+ } else {
284
+ this . #prune( )
285
+ // New response, let's insert it
286
+ this . #insertValueQuery. run (
287
+ url ,
288
+ key . method ,
289
+ body ?? null ,
290
+ value . deleteAt ,
291
+ value . statusCode ,
292
+ value . statusMessage ,
293
+ value . headers ? JSON . stringify ( value . headers ) : null ,
294
+ value . etag ? value . etag : null ,
295
+ value . cacheControlDirectives ? JSON . stringify ( value . cacheControlDirectives ) : null ,
296
+ value . vary ? JSON . stringify ( value . vary ) : null ,
297
+ value . cachedAt ,
298
+ value . staleAt ,
299
+ value . deleteAt
300
+ )
301
+ }
302
+ }
303
+
254
304
/**
255
305
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
256
306
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue } value
@@ -260,7 +310,6 @@ module.exports = class SqliteCacheStore {
260
310
assertCacheKey ( key )
261
311
assertCacheValue ( value )
262
312
263
- const url = this . #makeValueUrl( key )
264
313
let size = 0
265
314
/**
266
315
* @type {Buffer[] | null }
@@ -269,11 +318,8 @@ module.exports = class SqliteCacheStore {
269
318
const store = this
270
319
271
320
return new Writable ( {
321
+ decodeStrings : true ,
272
322
write ( chunk , encoding , callback ) {
273
- if ( typeof chunk === 'string' ) {
274
- chunk = Buffer . from ( chunk , encoding )
275
- }
276
-
277
323
size += chunk . byteLength
278
324
279
325
if ( size < store . #maxEntrySize) {
@@ -285,42 +331,7 @@ module.exports = class SqliteCacheStore {
285
331
callback ( )
286
332
} ,
287
333
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
-
334
+ store . set ( key , { ...value , body } )
324
335
callback ( )
325
336
}
326
337
} )
@@ -407,10 +418,10 @@ module.exports = class SqliteCacheStore {
407
418
return undefined
408
419
}
409
420
410
- value . vary = JSON . parse ( value . vary )
421
+ const vary = JSON . parse ( value . vary )
411
422
412
- for ( const header in value . vary ) {
413
- if ( ! headerValueEquals ( headers [ header ] , value . vary [ header ] ) ) {
423
+ for ( const header in vary ) {
424
+ if ( ! headerValueEquals ( headers [ header ] , vary [ header ] ) ) {
414
425
matches = false
415
426
break
416
427
}
0 commit comments