{"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/delete.ts","../src/get.ts","../src/set.ts","../src/index.ts"],"sourcesContent":["export function buildCacheKey(givenKey: string, keyPrefix?: string): string {\n  return keyPrefix ? `${keyPrefix}:${givenKey}` : givenKey;\n}\n","import { KVNamespace } from \"@cloudflare/workers-types/2023-07-01\";\nimport { buildCacheKey } from \"./utils\";\n\nexport async function deleteOperation(\n  kv: KVNamespace,\n  key: string,\n  keyPrefix?: string,\n): Promise<void> {\n  const cacheKey = buildCacheKey(key, keyPrefix);\n  await kv.delete(cacheKey);\n}\n","import { KVNamespace } from \"@cloudflare/workers-types/2023-07-01\";\nimport { type CacheEntry, type CacheMetadata } from \"@epic-web/cachified\";\nimport { buildCacheKey } from \"./utils\";\n\nexport async function getOperation<Value = unknown>(\n  kv: KVNamespace,\n  key: string,\n  keyPrefix?: string,\n): Promise<CacheEntry<Value> | null> {\n  const cacheKey = buildCacheKey(key, keyPrefix);\n  const { value, metadata } = await kv.getWithMetadata<CacheMetadata>(cacheKey, { type: \"text\" });\n  if (value === null) {\n    return value;\n  }\n  const jsonValue = JSON.parse(value) as Value;\n  return {\n    value: jsonValue,\n    metadata: metadata!,\n  };\n}\n","import { KVNamespace } from \"@cloudflare/workers-types/2023-07-01\";\nimport { type CacheEntry, totalTtl } from \"@epic-web/cachified\";\nimport { buildCacheKey } from \"./utils\";\n\nexport async function setOperation<Value = unknown>(\n  kv: KVNamespace,\n  key: string,\n  value: CacheEntry<Value>,\n  keyPrefix?: string,\n): Promise<unknown> {\n  const cacheKey = buildCacheKey(key, keyPrefix);\n\n  let expirationTtl: number | undefined = totalTtl(value.metadata);\n  if (expirationTtl === Infinity) {\n    expirationTtl = undefined;\n  } else {\n    expirationTtl = Math.max(Math.ceil(expirationTtl / 1000), 60);\n  }\n\n  await kv.put(cacheKey, JSON.stringify(value.value), {\n    expirationTtl: expirationTtl,\n    metadata: value.metadata,\n  });\n  return value.value;\n}\n","import type { KVNamespace } from \"@cloudflare/workers-types/2023-07-01\";\nimport { type Cache } from \"@epic-web/cachified\";\nimport { deleteOperation } from \"./delete\";\nimport { getOperation } from \"./get\";\nimport { setOperation } from \"./set\";\n\n/**\n * Configuration options for the Cloudflare KV cache adapter.\n * @property {KVNamespace} kv - The KVNamespace instance to interact with Cloudflare KV.\n * @property {string} [keyPrefix] - Optional prefix for all keys managed by this adapter.\n * @property {string} [name] - Optional name for the cache, defaults to \"CloudflareKV\".\n */\nexport type CloudflareKvCacheConfig = {\n  kv: KVNamespace;\n  keyPrefix?: string;\n  name?: string;\n};\n\n/**\n * Creates a cache adapter for Cloudflare's KV storage.\n * @param {CloudflareKvCacheConfig} config - Configuration options for the cache adapter.\n * @returns {Cache} A cache adapter instance for Cloudflare KV.\n */\nexport function cloudflareKvCacheAdapter<Value = unknown>(\n  config: CloudflareKvCacheConfig,\n): Cache<Value> {\n  return {\n    name: config.name ?? \"CloudflareKV\",\n    get: async (key) => {\n      return getOperation<Value>(config.kv, key, config.keyPrefix);\n    },\n    set: async (key, value) => {\n      return await setOperation<Value>(config.kv, key, value, config.keyPrefix);\n    },\n    delete: async (key) => {\n      await deleteOperation(config.kv, key, config.keyPrefix);\n    },\n  };\n}\n"],"names":["totalTtl"],"mappings":";;;AAAgB,SAAA,cAAc,UAAkB,WAA4B;AAC1E,SAAO,YAAY,GAAG,SAAS,IAAI,QAAQ,KAAK;AAClD;ACCsB,eAAA,gBACpB,IACA,KACA,WACe;AACT,QAAA,WAAW,cAAc,KAAK,SAAS;AACvC,QAAA,GAAG,OAAO,QAAQ;AAC1B;ACNsB,eAAA,aACpB,IACA,KACA,WACmC;AAC7B,QAAA,WAAW,cAAc,KAAK,SAAS;AACvC,QAAA,EAAE,OAAO,SAAa,IAAA,MAAM,GAAG,gBAA+B,UAAU,EAAE,MAAM,OAAA,CAAQ;AAC9F,MAAI,UAAU,MAAM;AACX,WAAA;AAAA,EACT;AACM,QAAA,YAAY,KAAK,MAAM,KAAK;AAC3B,SAAA;AAAA,IACL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