Feat: implement HTTP 304 with SQLite Cache (#42)

This commit is contained in:
Sukka
2024-10-09 09:25:25 +08:00
committed by GitHub
parent abf924c977
commit 07d3fdf05b
9 changed files with 144 additions and 48 deletions

View File

@@ -1,18 +1,14 @@
import { TTL, deserializeArray, fsFetchCache, serializeArray, createCacheKey } from './cache-filesystem';
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
import { deserializeArray, fsFetchCache, getFileContentHash, serializeArray } from './cache-filesystem';
import { createMemoizedPromise } from './memo-promise';
const cacheKey = createCacheKey(__filename);
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => fsFetchCache.apply(
cacheKey('https://publicsuffix.org/list/public_suffix_list.dat'),
() => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit)
.then(r => r.text()).then(text => text.split('\n')),
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => fsFetchCache.applyWithHttp304<string[]>(
'https://publicsuffix.org/list/public_suffix_list.dat',
getFileContentHash(__filename),
(r) => r.text().then(text => text.split('\n')),
{
// https://github.com/publicsuffix/list/blob/master/.github/workflows/tld-update.yml
// Though the action runs every 24 hours, the IANA list is updated every 7 days.
// So a 3 day TTL should be enough.
ttl: TTL.THREE_DAYS(),
serializer: serializeArray,
deserializer: deserializeArray
}