revert: Revert "fix(fetcher): remove 200 minimum cache"

This reverts commit 80e4f4edc4.
This commit is contained in:
WebStreamr 2025-06-27 19:18:56 +00:00
parent 70df4c2697
commit aee0fd8373
No known key found for this signature in database
2 changed files with 13 additions and 4 deletions

View file

@ -16,8 +16,8 @@ describe('fetch', () => {
fetchMock.clearHistory();
});
test('text passes successful response through setting headers and utilising cache', async () => {
fetchMock.get('https://some-url.test/', { body: 'some text', status: 200, headers: { 'Cache-Control': 'max-age=123' } });
test('text passes successful response through setting headers', async () => {
fetchMock.get('https://some-url.test/', 'some text');
const responseText1 = await fetcher.text(ctx, new URL('https://some-url.test/'));
const responseText2 = await fetcher.text(ctx, new URL('https://some-url.test/'), { headers: { 'User-Agent': 'jest' } });

View file

@ -51,6 +51,7 @@ export type CustomRequestInit = RequestInit & {
};
export class Fetcher {
private readonly MIN_CACHE_TTL = 900000; // 15m
private readonly DEFAULT_TIMEOUT = 10000;
private readonly DEFAULT_QUEUE_LIMIT = 5;
private readonly DEFAULT_QUEUE_TIMEOUT = 5000;
@ -163,7 +164,7 @@ export class Fetcher {
httpCacheItem.body = challengeResult.solution.response;
const ttl = httpCacheItem.policy.timeToLive();
const ttl = this.determineCacheTtl(httpCacheItem);
/* istanbul ignore next */
if (ttl > 0) {
this.httpCache.set(this.determineCacheKey(url, init), httpCacheItem, { ttl });
@ -196,6 +197,14 @@ export class Fetcher {
return `${url.href}_${init?.body?.toString()}`;
}
private determineCacheTtl(httpCacheItem: HttpCacheItem): number {
if (httpCacheItem.status === 200) {
return Math.max(httpCacheItem.policy.timeToLive(), this.MIN_CACHE_TTL);
}
return httpCacheItem.policy.timeToLive();
};
private headersToObject(headers: Headers): Record<string, string> {
const obj: Record<string, string> = {};
@ -225,7 +234,7 @@ export class Fetcher {
httpCacheItem = { policy, status: response.status, statusText: response.statusText, body };
const ttl = httpCacheItem.policy.timeToLive();
const ttl = this.determineCacheTtl(httpCacheItem);
if (ttl > 0) {
this.httpCache.set(cacheKey, httpCacheItem, { ttl });
}