diff --git a/mod.ts b/mod.ts index 1e4b633..869d461 100644 --- a/mod.ts +++ b/mod.ts @@ -12,4 +12,3 @@ export { LocationData } from './location_data.ts' export { otp } from './otp.ts' export { h, Renderer } from './render.ts' export { sendMail } from './send_mail.ts' -export { Store } from './store.ts' diff --git a/store.ts b/store.ts deleted file mode 100644 index c7ea8c7..0000000 --- a/store.ts +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. -import { encode } from 'https://deno.land/std@0.198.0/encoding/base64.ts' -import { Context } from './context.ts' - -export class Store { - #cache: globalThis.Cache | null - #context - #maxAge - #name - - constructor(c: Context, { - maxAge = 600, - name = 'cheetah', - }: { - /** - * Duration in seconds for how long a response should be cached. - * - * @default 600 - */ - maxAge?: number - /** A unique name for your cache. */ - name?: string - } = {}) { - this.#cache = null - this.#context = c - this.#maxAge = maxAge - this.#name = name - } - - async set(key: string, data: string | Record | Uint8Array) { - if (this.#cache === null) { - this.#cache = await caches.open(this.#name) - } - - this.#context.waitUntil( - this.#cache.put( - `https://${this.#name}.com/${encode(key)}`, - new Response( - typeof data === 'string' || data instanceof Uint8Array - ? data - : JSON.stringify(data), - { - headers: { - 'cache-control': `max-age=${this.#maxAge}`, - }, - }, - ), - ), - ) - } - - get = Record>( - key: string, - type: 'json', - ): Promise - get( - key: string, - type: 'string', - ): Promise - get( - key: string, - type: 'buffer', - ): Promise - - async get | string | Uint8Array>( - key: string, - type: 'string' | 'json' | 'buffer' = 'string', - ): Promise { - if (this.#cache === null) { - this.#cache = await caches.open(this.#name) - } - - try { - const result = await this.#cache.match( - `https://${this.#name}.com/${encode(key)}`, - ) - - if (!result) { - return undefined - } - - const data = type === 'string' - ? await result.text() - : type === 'json' - ? await result.json() - : new Uint8Array(await result.arrayBuffer()) - - return data - } catch (_err) { - return undefined - } - } - - async has(key: string) { - if (this.#cache === null) { - this.#cache = await caches.open(this.#name) - } - - const result = await this.#cache.match( - `https://${this.#name}.com/${encode(key)}`, - ) - - return result !== undefined - } - - async delete(key: string) { - if (this.#cache === null) { - this.#cache = await caches.open(this.#name) - } - - return await this.#cache.delete( - `https://${this.#name}.com/${encode(key)}`, - ) - } -}