-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from dcSpark/feature/cache-invalidation-r2
Implements R2 cache invalidation for tools.
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Make this a module | ||
export {}; | ||
|
||
interface DirectoryEntry { | ||
file: string; | ||
} | ||
|
||
const CLOUDFLARE_ZONE = Deno.env.get("CLOUDFLARE_ZONE"); | ||
const CLOUDFLARE_TOKEN = Deno.env.get("CLOUDFLARE_TOKEN"); | ||
|
||
if (!CLOUDFLARE_TOKEN) { | ||
console.error("CLOUDFLARE_TOKEN environment variable is required"); | ||
Deno.exit(1); | ||
} | ||
|
||
try { | ||
const directoryContent = await Deno.readTextFile("packages/directory.json"); | ||
const directory: DirectoryEntry[] = JSON.parse(directoryContent); | ||
|
||
const files = directory.map(entry => entry.file); | ||
console.log(`URLs to invalidate: ${JSON.stringify(files, null, 2)}`); | ||
|
||
const response = await fetch( | ||
`https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Authorization": `Bearer ${CLOUDFLARE_TOKEN}`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ files }), | ||
} | ||
); | ||
|
||
const result = await response.json(); | ||
console.log(`Cache invalidation response (${response.status}):`, JSON.stringify(result, null, 2)); | ||
|
||
if (!result.success) { | ||
console.error("Failed to invalidate cache"); | ||
Deno.exit(1); | ||
} | ||
} catch (error) { | ||
console.error("Error:", error); | ||
Deno.exit(1); | ||
} |