{"version":3,"file":"cache-inspect.cjs","names":[],"sources":["../../src/sw/cache-inspect.ts"],"sourcesContent":["/**\n * Main-thread helpers to inspect and clear the Cache Storage buckets a service\n * worker fills — for a \"X MB cacheado\" readout and a \"limpar cache\" action\n * (e.g. on logout). All guard `caches` so they no-op safely under SSR / older\n * browsers.\n */\n\n/** Per-cache usage summary returned by {@link inspectCaches}. */\nexport interface CacheReport {\n    /** The Cache Storage bucket name. */\n    name: string;\n    /** Number of cached responses. */\n    entries: number;\n    /** Approximate total bytes, or `null` when byte measurement was skipped. */\n    bytes: number | null;\n}\n\n/** A cache-name matcher: a prefix string, a `RegExp`, or a predicate. */\nexport type CacheFilter = string | RegExp | ((name: string) => boolean);\n\nfunction nameMatches(filter: CacheFilter | undefined, name: string): boolean {\n    if (filter === undefined) return true;\n    if (typeof filter === \"string\") return name.startsWith(filter);\n    if (filter instanceof RegExp) return filter.test(name);\n    return filter(name);\n}\n\nfunction cacheStorageAvailable(): boolean {\n    return typeof caches !== \"undefined\";\n}\n\n/** Best-effort byte size of one cached response (Content-Length, else the blob). */\nasync function responseBytes(response: Response): Promise<number> {\n    const header = response.headers.get(\"content-length\");\n    if (header) {\n        const parsed = Number(header);\n        if (!Number.isNaN(parsed)) return parsed;\n    }\n    try {\n        const blob = await response.clone().blob();\n        return blob.size;\n    } catch {\n        return 0;\n    }\n}\n\n/**\n * Report entry counts (and optionally byte sizes) for the Cache Storage\n * buckets whose name passes `filter`.\n *\n * @param options - `filter` narrows which caches to include; `measureBytes`\n *   (default `true`) reads each response to sum sizes — set `false` for a fast,\n *   count-only report.\n * @returns One {@link CacheReport} per matching cache. Empty when unsupported.\n *\n * @example\n * const reports = await inspectCaches({ filter: \"tempest-\" });\n * const totalMb = reports.reduce((n, r) => n + (r.bytes ?? 0), 0) / 1e6;\n */\nexport async function inspectCaches(\n    options: { filter?: CacheFilter; measureBytes?: boolean } = {},\n): Promise<CacheReport[]> {\n    if (!cacheStorageAvailable()) return [];\n    const { filter, measureBytes = true } = options;\n\n    const names = (await caches.keys()).filter((name) => nameMatches(filter, name));\n    const reports: CacheReport[] = [];\n    for (const name of names) {\n        const cache = await caches.open(name);\n        const requests = await cache.keys();\n        let bytes: number | null = null;\n        if (measureBytes) {\n            bytes = 0;\n            for (const request of requests) {\n                const response = await cache.match(request);\n                if (response) bytes += await responseBytes(response);\n            }\n        }\n        reports.push({ name, entries: requests.length, bytes });\n    }\n    return reports;\n}\n\n/**\n * Delete Cache Storage buckets whose name passes `filter` (all of them when no\n * filter is given).\n *\n * @param filter - Which caches to delete.\n * @returns The names of the caches that were deleted.\n *\n * @example\n * await clearCaches(\"tempest-\"); // drop every SDK-managed cache on logout\n */\nexport async function clearCaches(filter?: CacheFilter): Promise<string[]> {\n    if (!cacheStorageAvailable()) return [];\n    const names = (await caches.keys()).filter((name) => nameMatches(filter, name));\n    const deleted: string[] = [];\n    for (const name of names) {\n        if (await caches.delete(name)) deleted.push(name);\n    }\n    return deleted;\n}\n"],"mappings":"AAoBA,SAAS,EAAY,EAAiC,EAAuB,CAIzE,OAHI,IAAW,IAAA,GAAkB,GAC7B,OAAO,GAAW,SAAiB,EAAK,WAAW,CAAM,EACzD,aAAkB,OAAe,EAAO,KAAK,CAAI,EAC9C,EAAO,CAAI,CACtB,CAEA,SAAS,GAAiC,CACtC,OAAO,OAAO,OAAW,GAC7B,CAGA,eAAe,EAAc,EAAqC,CAC9D,IAAM,EAAS,EAAS,QAAQ,IAAI,gBAAgB,EACpD,GAAI,EAAQ,CACR,IAAM,EAAS,OAAO,CAAM,EAC5B,GAAI,CAAC,OAAO,MAAM,CAAM,EAAG,OAAO,CACtC,CACA,GAAI,CAEA,OAAO,MADY,EAAS,MAAM,CAAC,CAAC,KAAK,EAAA,CAC7B,IAChB,MAAQ,CACJ,MAAO,EACX,CACJ,CAeA,eAAsB,EAClB,EAA4D,CAAC,EACvC,CACtB,GAAI,CAAC,EAAsB,EAAG,MAAO,CAAC,EACtC,GAAM,CAAE,SAAQ,eAAe,I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