Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class MemoryCacheStore extends EventEmitter {

entry.size += chunk.byteLength

if (entry.size >= store.#maxEntrySize) {
if (entry.size > store.#maxEntrySize) {
this.destroy()
} else {
entry.body.push(chunk)
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ module.exports = class SqliteCacheStore {
write (chunk, encoding, callback) {
size += chunk.byteLength

if (size < store.#maxEntrySize) {
if (size <= store.#maxEntrySize) {
body.push(chunk)
} else {
this.destroy()
Expand Down
39 changes: 39 additions & 0 deletions test/cache-interceptor/cache-store-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ function cacheStoreTests (CacheStore, options) {
}
})

test('caches streamed body at maxEntrySize boundary', options, async () => {
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
*/
const key = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: {}
}

/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
*/
const value = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'bar' },
cacheControlDirectives: {},
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
deleteAt: Date.now() + 20000
}

const body = [Buffer.from('asd'), Buffer.from('123')]
const store = new CacheStore({ maxEntrySize: 6 })
const writable = store.createWriteStream(key, value)

notEqual(writable, undefined)

const finished = once(writable, 'finish')
writeBody(writable, body)
await finished

const result = await store.get(structuredClone(key))
notEqual(result, undefined)
await compareGetResults(result, value, body)
})

test('returns stale response before deleteAt', options, async () => {
const clock = FakeTimers.install({
shouldClearNativeTimers: true
Expand Down
Loading