@@ -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 */
2431module . 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 }
0 commit comments