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): Make http cache providers respect cache-control header #33848

Open
wants to merge 7 commits 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
22 changes: 21 additions & 1 deletion lib/util/http/cache/abstract-http-cache-provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { logger } from '../../../logger';
import { regEx } from '../../regex';
import { HttpCacheStats } from '../../stats';
import type { GotOptions, HttpResponse } from '../types';
import { copyResponse } from '../util';
import { type HttpCache, HttpCacheSchema } from './schema';
import type { HttpCacheProvider } from './types';

export abstract class AbstractHttpCacheProvider implements HttpCacheProvider {
protected checkCacheControlHeader = true;

protected abstract load(url: string): Promise<unknown>;
protected abstract persist(url: string, data: HttpCache): Promise<void>;

Expand All @@ -16,7 +19,7 @@ export abstract class AbstractHttpCacheProvider implements HttpCacheProvider {
return null;
}

return httpCache as HttpCache;
return httpCache;
}

async setCacheHeaders<T extends Pick<GotOptions, 'headers'>>(
Expand All @@ -43,11 +46,28 @@ export abstract class AbstractHttpCacheProvider implements HttpCacheProvider {
return Promise.resolve(null);
}

private preventCaching<T>(resp: HttpResponse<T>): boolean {
if (this.checkCacheControlHeader === false) {
return false;
}

const isPublic = resp.headers?.['cache-control']
?.toLocaleLowerCase()
.split(regEx(/\s*,\s*/))
.includes('public');

return !isPublic;
Comment on lines +54 to +59
Copy link
Member

Choose a reason for hiding this comment

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

I think we should allow caching if the header is missing 🤔

we should first add some logging and track some stats in the hosted app. so we're sure most ecosystems behave properly. and if not we need config options to workaround.

}

async wrapServerResponse<T>(
url: string,
resp: HttpResponse<T>,
): Promise<HttpResponse<T>> {
if (resp.statusCode === 200) {
if (this.preventCaching(resp)) {
return resp;
}

const etag = resp.headers?.['etag'];
const lastModified = resp.headers?.['last-modified'];

Expand Down
30 changes: 25 additions & 5 deletions lib/util/http/cache/package-http-cache-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ describe('util/http/cache/package-http-cache-provider', () => {
expect(packageCache.set).not.toHaveBeenCalled();

mockTime('2024-06-15T00:15:00.000Z');
httpMock.scope(url).get('').reply(200, 'new response', { etag: 'foobar' });
httpMock.scope(url).get('').reply(200, 'new response', {
etag: 'foobar',
'cache-control': 'max-age=180, public',
});

const res2 = await http.get(url, { cacheProvider });
expect(res2.body).toBe('new response');
Expand All @@ -88,10 +91,10 @@ describe('util/http/cache/package-http-cache-provider', () => {
const cacheProvider = new PackageHttpCacheProvider({
namespace: '_test-namespace',
});
httpMock
.scope(url)
.get('')
.reply(200, 'fetched response', { etag: 'foobar' });
httpMock.scope(url).get('').reply(200, 'fetched response', {
etag: 'foobar',
'cache-control': 'max-age=180, public',
});

const res = await http.get(url, { cacheProvider });

Expand All @@ -109,4 +112,21 @@ describe('util/http/cache/package-http-cache-provider', () => {
},
});
});

it('prevents caching when cache-control is private', async () => {
mockTime('2024-06-15T00:00:00.000Z');

const cacheProvider = new PackageHttpCacheProvider({
namespace: '_test-namespace',
});

httpMock.scope(url).get('').reply(200, 'private response', {
'cache-control': 'max-age=180, private',
});

const res = await http.get(url, { cacheProvider });

expect(res.body).toBe('private response');
expect(packageCache.set).not.toHaveBeenCalled();
});
});
2 changes: 2 additions & 0 deletions lib/util/http/cache/repository-http-cache-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { AbstractHttpCacheProvider } from './abstract-http-cache-provider';
import type { HttpCache } from './schema';

export class RepositoryHttpCacheProvider extends AbstractHttpCacheProvider {
protected override checkCacheControlHeader = false;

override load(url: string): Promise<unknown> {
const cache = getCache();
cache.httpCache ??= {};
Expand Down
Loading