@@ -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?: 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
2330 */
2431module . exports = class SqliteCacheStore {
2532 #maxEntrySize = MAX_ENTRY_SIZE
@@ -219,36 +226,80 @@ module.exports = class SqliteCacheStore {
219226
220227 /**
221228 * @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 }
223230 */
224231 get ( key ) {
225232 assertCacheKey ( key )
226233
227234 const value = this . #findValue( key )
235+ return value
236+ ? {
237+ body : value . body ? Buffer . from ( value . body . buffer ) : undefined ,
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+ }
228252
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 )
232259
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
249266 }
250267
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+ }
252303 }
253304
254305 /**
@@ -260,7 +311,6 @@ module.exports = class SqliteCacheStore {
260311 assertCacheKey ( key )
261312 assertCacheValue ( value )
262313
263- const url = this . #makeValueUrl( key )
264314 let size = 0
265315 /**
266316 * @type {Buffer[] | null }
@@ -269,11 +319,8 @@ module.exports = class SqliteCacheStore {
269319 const store = this
270320
271321 return new Writable ( {
322+ decodeStrings : true ,
272323 write ( chunk , encoding , callback ) {
273- if ( typeof chunk === 'string' ) {
274- chunk = Buffer . from ( chunk , encoding )
275- }
276-
277324 size += chunk . byteLength
278325
279326 if ( size < store . #maxEntrySize) {
@@ -285,42 +332,7 @@ module.exports = class SqliteCacheStore {
285332 callback ( )
286333 } ,
287334 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 } )
324336 callback ( )
325337 }
326338 } )
@@ -379,7 +391,7 @@ module.exports = class SqliteCacheStore {
379391 /**
380392 * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
381393 * @param {boolean } [canBeExpired=false]
382- * @returns {( SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined }
394+ * @returns {SqliteStoreValue | undefined }
383395 */
384396 #findValue ( key , canBeExpired = false ) {
385397 const url = this . #makeValueUrl( key )
@@ -407,10 +419,10 @@ module.exports = class SqliteCacheStore {
407419 return undefined
408420 }
409421
410- value . vary = JSON . parse ( value . vary )
422+ const vary = JSON . parse ( value . vary )
411423
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 ] ) ) {
414426 matches = false
415427 break
416428 }
0 commit comments