@@ -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
@@ -61,7 +68,7 @@ module.exports = class SqliteCacheStore {
6168 #countEntriesQuery
6269
6370 /**
64- * @type {import('node:sqlite').StatementSync }
71+ * @type {import('node:sqlite').StatementSync | null }
6572 */
6673 #deleteOldValuesQuery
6774
@@ -163,8 +170,7 @@ module.exports = class SqliteCacheStore {
163170 etag = ?,
164171 cacheControlDirectives = ?,
165172 cachedAt = ?,
166- staleAt = ?,
167- deleteAt = ?
173+ staleAt = ?
168174 WHERE
169175 id = ?
170176 ` )
@@ -182,9 +188,8 @@ module.exports = class SqliteCacheStore {
182188 cacheControlDirectives,
183189 vary,
184190 cachedAt,
185- staleAt,
186- deleteAt
187- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
191+ staleAt
192+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
188193 ` )
189194
190195 this . #deleteByUrlQuery = this . #db. prepare (
@@ -219,36 +224,78 @@ module.exports = class SqliteCacheStore {
219224
220225 /**
221226 * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
222- * @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined }
227+ * @returns {( import('../../types/cache-interceptor.d.ts').default.GetResult & { body?: Buffer }) | undefined }
223228 */
224229 get ( key ) {
225230 assertCacheKey ( key )
226231
227232 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+ }
228250
229- if ( ! value ) {
230- return undefined
231- }
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 )
232257
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
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
249264 }
250265
251- 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+ }
252299 }
253300
254301 /**
@@ -260,7 +307,6 @@ module.exports = class SqliteCacheStore {
260307 assertCacheKey ( key )
261308 assertCacheValue ( value )
262309
263- const url = this . #makeValueUrl( key )
264310 let size = 0
265311 /**
266312 * @type {Buffer[] | null }
@@ -269,11 +315,8 @@ module.exports = class SqliteCacheStore {
269315 const store = this
270316
271317 return new Writable ( {
318+ decodeStrings : true ,
272319 write ( chunk , encoding , callback ) {
273- if ( typeof chunk === 'string' ) {
274- chunk = Buffer . from ( chunk , encoding )
275- }
276-
277320 size += chunk . byteLength
278321
279322 if ( size < store . #maxEntrySize) {
@@ -285,42 +328,7 @@ module.exports = class SqliteCacheStore {
285328 callback ( )
286329 } ,
287330 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-
331+ store . set ( key , { ...value , body } )
324332 callback ( )
325333 }
326334 } )
@@ -344,14 +352,14 @@ module.exports = class SqliteCacheStore {
344352
345353 {
346354 const removed = this . #deleteExpiredValuesQuery. run ( Date . now ( ) ) . changes
347- if ( removed > 0 ) {
355+ if ( removed ) {
348356 return removed
349357 }
350358 }
351359
352360 {
353- const removed = this . #deleteOldValuesQuery. run ( Math . max ( Math . floor ( this . #maxCount * 0.1 ) , 1 ) ) . changes
354- if ( removed > 0 ) {
361+ const removed = this . #deleteOldValuesQuery? .run ( Math . max ( Math . floor ( this . #maxCount * 0.1 ) , 1 ) ) . changes
362+ if ( removed ) {
355363 return removed
356364 }
357365 }
@@ -379,7 +387,7 @@ module.exports = class SqliteCacheStore {
379387 /**
380388 * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
381389 * @param {boolean } [canBeExpired=false]
382- * @returns {( SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined }
390+ * @returns {SqliteStoreValue | undefined }
383391 */
384392 #findValue ( key , canBeExpired = false ) {
385393 const url = this . #makeValueUrl( key )
@@ -407,10 +415,10 @@ module.exports = class SqliteCacheStore {
407415 return undefined
408416 }
409417
410- value . vary = JSON . parse ( value . vary )
418+ const vary = JSON . parse ( value . vary )
411419
412- for ( const header in value . vary ) {
413- if ( ! headerValueEquals ( headers [ header ] , value . vary [ header ] ) ) {
420+ for ( const header in vary ) {
421+ if ( ! headerValueEquals ( headers [ header ] , vary [ header ] ) ) {
414422 matches = false
415423 break
416424 }
0 commit comments