diff --git a/src/middleware/cacheMiddleware.ts b/src/middleware/cacheMiddleware.ts deleted file mode 100644 index baa4976d..00000000 --- a/src/middleware/cacheMiddleware.ts +++ /dev/null @@ -1,28 +0,0 @@ -import Debug from '@soapbox/stickynotes/debug'; -import { type MiddlewareHandler } from 'hono'; - -import ExpiringCache from '@/utils/expiring-cache.ts'; - -const debug = Debug('ditto:middleware:cache'); - -export const cacheMiddleware = (options: { - cacheName: string; - expires?: number; -}): MiddlewareHandler => { - return async (c, next) => { - const key = c.req.url.replace('http://', 'https://'); - const cache = new ExpiringCache(await caches.open(options.cacheName)); - const response = await cache.match(key); - if (!response) { - debug('Building cache for page', c.req.url); - await next(); - const response = c.res.clone(); - if (response.status < 500) { - await cache.putExpiring(key, response, options.expires ?? 0); - } - } else { - debug('Serving page from cache', c.req.url); - return response; - } - }; -}; diff --git a/src/utils/expiring-cache.test.ts b/src/utils/expiring-cache.test.ts deleted file mode 100644 index 8c6d7b18..00000000 --- a/src/utils/expiring-cache.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { assert } from '@std/assert'; - -import ExpiringCache from './expiring-cache.ts'; - -Deno.test('ExpiringCache', async () => { - const cache = new ExpiringCache(await caches.open('test')); - - await cache.putExpiring('http://mostr.local/1', new Response('hello world'), 300); - await cache.putExpiring('http://mostr.local/2', new Response('hello world'), -1); - - // const resp1 = await cache.match('http://mostr.local/1'); - const resp2 = await cache.match('http://mostr.local/2'); - - // assert(resp1!.headers.get('Expires')); - assert(!resp2); - - // await resp1!.text(); -}); diff --git a/src/utils/expiring-cache.ts b/src/utils/expiring-cache.ts deleted file mode 100644 index ebb5d2ee..00000000 --- a/src/utils/expiring-cache.ts +++ /dev/null @@ -1,68 +0,0 @@ -class ExpiringCache implements Cache { - #cache: Cache; - - constructor(cache: Cache) { - this.#cache = cache; - } - - add(request: RequestInfo | URL): Promise { - return this.#cache.add(request); - } - - addAll(requests: RequestInfo[]): Promise { - return this.#cache.addAll(requests); - } - - keys(request?: RequestInfo | URL | undefined, options?: CacheQueryOptions | undefined): Promise { - return this.#cache.keys(request, options); - } - - matchAll( - request?: RequestInfo | URL | undefined, - options?: CacheQueryOptions | undefined, - ): Promise { - return this.#cache.matchAll(request, options); - } - - put(request: RequestInfo | URL, response: Response): Promise { - return this.#cache.put(request, response); - } - - putExpiring(request: RequestInfo | URL, response: Response, expiresIn: number): Promise { - const expires = Date.now() + expiresIn; - - const clone = new Response(response.body, { - status: response.status, - headers: { - expires: new Date(expires).toUTCString(), - ...Object.fromEntries(response.headers.entries()), - }, - }); - - return this.#cache.put(request, clone); - } - - async match(request: RequestInfo | URL, options?: CacheQueryOptions | undefined): Promise { - const response = await this.#cache.match(request, options); - const expires = response?.headers.get('Expires'); - - if (response && expires) { - if (new Date(expires).getTime() > Date.now()) { - return response; - } else { - await Promise.all([ - this.delete(request), - response.text(), // Prevent memory leaks - ]); - } - } else if (response) { - return response; - } - } - - delete(request: RequestInfo | URL, options?: CacheQueryOptions | undefined): Promise { - return this.#cache.delete(request, options); - } -} - -export default ExpiringCache;