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: cache etag support #3758

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,25 @@ class CacheHandler extends DecoratorHandler {
cacheControlDirectives
)

this.#writeStream = this.#store.createWriteStream(this.#requestOptions, {
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheStoreValue}
*/
const value = {
statusCode,
statusMessage,
rawHeaders: strippedHeaders,
vary: varyDirectives,
cachedAt: now,
staleAt,
deleteAt
})
}

// Check for the response's etag header, ignore if there's multiple
if (typeof headers.etag === 'string') {
value.etag = headers.etag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't etag be validated here? https://httpwg.org/specs/rfc9110.html#field.etag

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also account for weak etags

}

this.#writeStream = this.#store.createWriteStream(this.#requestOptions, value)

if (this.#writeStream) {
this.#writeStream.on('drain', resume)
Expand Down
4 changes: 4 additions & 0 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ module.exports = (opts = {}) => {

opts.headers['if-modified-since'] = new Date(value.cachedAt).toUTCString()

if (value.etag) {
opts.headers['if-none-match'] = value.etag
}

// Need to revalidate the response
dispatch(
opts,
Expand Down
6 changes: 6 additions & 0 deletions test/cache-interceptor/cache-stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function cacheStoreTests (CacheStore) {
const requestValue = {
statusCode: 200,
statusMessage: '',
etag: '',
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
Expand Down Expand Up @@ -72,6 +73,7 @@ function cacheStoreTests (CacheStore) {
const anotherValue = {
statusCode: 200,
statusMessage: '',
etag: '',
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
Expand Down Expand Up @@ -111,6 +113,7 @@ function cacheStoreTests (CacheStore) {
const requestValue = {
statusCode: 200,
statusMessage: '',
etag: '',
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
cachedAt: Date.now() - 10000,
staleAt: Date.now() - 1,
Expand Down Expand Up @@ -146,6 +149,7 @@ function cacheStoreTests (CacheStore) {
const requestValue = {
statusCode: 200,
statusMessage: '',
etag: '',
cachedAt: Date.now() - 20000,
staleAt: Date.now() - 10000,
deleteAt: Date.now() - 5
Expand Down Expand Up @@ -177,6 +181,7 @@ function cacheStoreTests (CacheStore) {
const requestValue = {
statusCode: 200,
statusMessage: '',
etag: '',
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
vary: {
'some-header': 'hello world'
Expand Down Expand Up @@ -234,6 +239,7 @@ test('MemoryCacheStore locks values properly', async () => {
const requestValue = {
statusCode: 200,
statusMessage: '',
etag: '',
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
Expand Down
2 changes: 2 additions & 0 deletions test/interceptors/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('Cache Interceptor', () => {

if (requestsToOrigin > 1) {
notEqual(req.headers['if-modified-since'], undefined)
equal(req.headers['if-none-match'], 'asd123')

if (requestsToOrigin === 3) {
res.end('asd123')
Expand All @@ -176,6 +177,7 @@ describe('Cache Interceptor', () => {
res.end()
}
} else {
res.setHeader('etag', 'asd123')
res.end('asd')
}
}).listen(0)
Expand Down
1 change: 1 addition & 0 deletions types/cache-interceptor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ declare namespace CacheHandler {
export interface CacheStoreValue {
statusCode: number;
statusMessage: string;
etag?: string;
rawHeaders: (Buffer | Buffer[])[];
rawTrailers?: string[];
/**
Expand Down
Loading