Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setter to sqlite cache store #4012

Closed
wants to merge 1 commit into from
Closed
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
99 changes: 58 additions & 41 deletions lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,62 @@ module.exports = class SqliteCacheStore {
return result
}

/**
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: Buffer | Array<Buffer> | null }} value
*/
set (key, value) {
assertCacheKey(key)
assertCacheValue(value)

const body = Array.isArray(value.body) ? Buffer.concat(value.body) : value.body
const size = body ? body.byteLength : 0

if (size > this.#maxEntrySize) {
return false
}

const url = this.#makeValueUrl(key)

const existingValue = this.#findValue(key, true)
if (existingValue) {
// Updating an existing response, let's overwrite it
this.#updateValueQuery.run(
body,
value.deleteAt,
value.statusCode,
value.statusMessage,
value.headers ? JSON.stringify(value.headers) : null,
value.etag ? value.etag : null,
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
value.cachedAt,
value.staleAt,
value.deleteAt,
existingValue.id
)
} else {
this.#prune()
// New response, let's insert it
this.#insertValueQuery.run(
url,
key.method,
body,
value.deleteAt,
value.statusCode,
value.statusMessage,
value.headers ? JSON.stringify(value.headers) : null,
value.etag ? value.etag : null,
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
value.vary ? JSON.stringify(value.vary) : null,
value.cachedAt,
value.staleAt,
value.deleteAt
)
}

return true
}

/**
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} value
Expand All @@ -260,7 +316,6 @@ module.exports = class SqliteCacheStore {
assertCacheKey(key)
assertCacheValue(value)

const url = this.#makeValueUrl(key)
let size = 0
/**
* @type {Buffer[] | null}
Expand All @@ -269,11 +324,8 @@ module.exports = class SqliteCacheStore {
const store = this

return new Writable({
decodeStrings: true,
write (chunk, encoding, callback) {
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, encoding)
}

size += chunk.byteLength

if (size < store.#maxEntrySize) {
Expand All @@ -285,42 +337,7 @@ module.exports = class SqliteCacheStore {
callback()
},
final (callback) {
const existingValue = store.#findValue(key, true)
if (existingValue) {
// Updating an existing response, let's overwrite it
store.#updateValueQuery.run(
Buffer.concat(body),
value.deleteAt,
value.statusCode,
value.statusMessage,
value.headers ? JSON.stringify(value.headers) : null,
value.etag ? value.etag : null,
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
value.cachedAt,
value.staleAt,
value.deleteAt,
existingValue.id
)
} else {
store.#prune()
// New response, let's insert it
store.#insertValueQuery.run(
url,
key.method,
Buffer.concat(body),
value.deleteAt,
value.statusCode,
value.statusMessage,
value.headers ? JSON.stringify(value.headers) : null,
value.etag ? value.etag : null,
value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null,
value.vary ? JSON.stringify(value.vary) : null,
value.cachedAt,
value.staleAt,
value.deleteAt
)
}

store.set(key, { ...value, body })
callback()
}
})
Expand Down
4 changes: 4 additions & 0 deletions lib/util/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ function assertCacheValue (value) {
if (value.etag !== undefined && typeof value.etag !== 'string') {
throw new TypeError(`expected value.etag to be string, got ${typeof value.etag}`)
}

if (value.body !== undefined && !Buffer.isBuffer(value.body)) {
throw new TypeError(`expected value.body to be Buffer, got ${typeof value.body}`)
}
}

/**
Expand Down
Loading