|
| 1 | +globalThis.openNextDebug = false;globalThis.openNextVersion = "3.6.5"; |
| 2 | + |
| 3 | +// ../../node_modules/@opennextjs/cloudflare/dist/api/durable-objects/bucket-cache-purge.js |
| 4 | +import { DurableObject } from "cloudflare:workers"; |
| 5 | + |
| 6 | +// ../../node_modules/@opennextjs/cloudflare/dist/api/cloudflare-context.js |
| 7 | +var cloudflareContextSymbol = Symbol.for("__cloudflare-context__"); |
| 8 | + |
| 9 | +// ../../node_modules/@opennextjs/cloudflare/dist/api/overrides/internal.js |
| 10 | +var debugCache = (name, ...args) => { |
| 11 | + if (process.env.NEXT_PRIVATE_DEBUG_CACHE) { |
| 12 | + console.log(`[${name}] `, ...args); |
| 13 | + } |
| 14 | +}; |
| 15 | +async function internalPurgeCacheByTags(env, tags) { |
| 16 | + if (!env.CACHE_PURGE_ZONE_ID && !env.CACHE_PURGE_API_TOKEN) { |
| 17 | + debugCache("purgeCacheByTags", "No cache zone ID or API token provided. Skipping cache purge."); |
| 18 | + return "missing-credentials"; |
| 19 | + } |
| 20 | + try { |
| 21 | + const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${env.CACHE_PURGE_ZONE_ID}/purge_cache`, { |
| 22 | + headers: { |
| 23 | + Authorization: `Bearer ${env.CACHE_PURGE_API_TOKEN}`, |
| 24 | + "Content-Type": "application/json" |
| 25 | + }, |
| 26 | + method: "POST", |
| 27 | + body: JSON.stringify({ |
| 28 | + tags |
| 29 | + }) |
| 30 | + }); |
| 31 | + if (response.status === 429) { |
| 32 | + debugCache("purgeCacheByTags", "Rate limit exceeded. Skipping cache purge."); |
| 33 | + return "rate-limit-exceeded"; |
| 34 | + } |
| 35 | + const bodyResponse = await response.json(); |
| 36 | + if (!bodyResponse.success) { |
| 37 | + debugCache("purgeCacheByTags", "Cache purge failed. Errors:", bodyResponse.errors.map((error) => `${error.code}: ${error.message}`)); |
| 38 | + return "purge-failed"; |
| 39 | + } |
| 40 | + debugCache("purgeCacheByTags", "Cache purged successfully for tags:", tags); |
| 41 | + return "purge-success"; |
| 42 | + } catch (error) { |
| 43 | + console.error("Error purging cache by tags:", error); |
| 44 | + return "purge-failed"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// ../../node_modules/@opennextjs/cloudflare/dist/api/durable-objects/bucket-cache-purge.js |
| 49 | +var DEFAULT_BUFFER_TIME_IN_SECONDS = 5; |
| 50 | +var MAX_NUMBER_OF_TAGS_PER_PURGE = 100; |
| 51 | +var BucketCachePurge = class extends DurableObject { |
| 52 | + bufferTimeInSeconds; |
| 53 | + constructor(state, env) { |
| 54 | + super(state, env); |
| 55 | + this.bufferTimeInSeconds = env.NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS ? parseInt(env.NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS) : DEFAULT_BUFFER_TIME_IN_SECONDS; |
| 56 | + state.blockConcurrencyWhile(async () => { |
| 57 | + state.storage.sql.exec(` |
| 58 | + CREATE TABLE IF NOT EXISTS cache_purge ( |
| 59 | + tag TEXT NOT NULL |
| 60 | + ); |
| 61 | + CREATE UNIQUE INDEX IF NOT EXISTS tag_index ON cache_purge (tag); |
| 62 | + `); |
| 63 | + }); |
| 64 | + } |
| 65 | + async purgeCacheByTags(tags) { |
| 66 | + for (const tag of tags) { |
| 67 | + this.ctx.storage.sql.exec(` |
| 68 | + INSERT OR REPLACE INTO cache_purge (tag) |
| 69 | + VALUES (?)`, [tag]); |
| 70 | + } |
| 71 | + const nextAlarm = await this.ctx.storage.getAlarm(); |
| 72 | + if (!nextAlarm) { |
| 73 | + this.ctx.storage.setAlarm(Date.now() + this.bufferTimeInSeconds * 1e3); |
| 74 | + } |
| 75 | + } |
| 76 | + async alarm() { |
| 77 | + let tags = this.ctx.storage.sql.exec(` |
| 78 | + SELECT * FROM cache_purge LIMIT ${MAX_NUMBER_OF_TAGS_PER_PURGE} |
| 79 | + `).toArray(); |
| 80 | + do { |
| 81 | + if (tags.length === 0) { |
| 82 | + return; |
| 83 | + } |
| 84 | + const result = await internalPurgeCacheByTags(this.env, tags.map((row) => row.tag)); |
| 85 | + if (result === "rate-limit-exceeded") { |
| 86 | + throw new Error("Rate limit exceeded"); |
| 87 | + } |
| 88 | + this.ctx.storage.sql.exec(` |
| 89 | + DELETE FROM cache_purge |
| 90 | + WHERE tag IN (${tags.map(() => "?").join(",")}) |
| 91 | + `, tags.map((row) => row.tag)); |
| 92 | + if (tags.length < MAX_NUMBER_OF_TAGS_PER_PURGE) { |
| 93 | + tags = []; |
| 94 | + } else { |
| 95 | + tags = this.ctx.storage.sql.exec(` |
| 96 | + SELECT * FROM cache_purge LIMIT ${MAX_NUMBER_OF_TAGS_PER_PURGE} |
| 97 | + `).toArray(); |
| 98 | + } |
| 99 | + } while (tags.length >= 0); |
| 100 | + } |
| 101 | +}; |
| 102 | +export { |
| 103 | + BucketCachePurge |
| 104 | +}; |
0 commit comments