{"id":26500083,"url":"https://github.com/mfydev/ghost-bunnycdn-cache-clearer","last_synced_at":"2025-03-20T15:28:41.543Z","repository":{"id":247029370,"uuid":"821077587","full_name":"MFYDev/Ghost-BunnyCDN-Cache-Clearer","owner":"MFYDev","description":"Cloudflare Worker that automatically purges BunnyCDN pull zone cache and cleans up Perma-Cache folders upon receiving Ghost CMS webhooks, optimizing storage usage and reducing costs.","archived":false,"fork":false,"pushed_at":"2024-08-28T20:24:47.000Z","size":15,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T00:38:19.703Z","etag":null,"topics":["bunnycdn","bunnycdn-adapter","bunnycdn-api","bunnycdn-storage","cloudflare","cloudflare-worker","cloudflare-workers","ghost","ghost-blog","ghost-cms"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MFYDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-27T19:06:46.000Z","updated_at":"2024-10-17T18:06:41.000Z","dependencies_parsed_at":"2024-07-06T07:20:18.365Z","dependency_job_id":"361b60ee-1234-4d02-bbf3-fdbd85a18c2a","html_url":"https://github.com/MFYDev/Ghost-BunnyCDN-Cache-Clearer","commit_stats":null,"previous_names":["mfydev/ghost-bunnycdn-cache-clearer"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MFYDev%2FGhost-BunnyCDN-Cache-Clearer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MFYDev%2FGhost-BunnyCDN-Cache-Clearer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MFYDev%2FGhost-BunnyCDN-Cache-Clearer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MFYDev%2FGhost-BunnyCDN-Cache-Clearer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MFYDev","download_url":"https://codeload.github.com/MFYDev/Ghost-BunnyCDN-Cache-Clearer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244639404,"owners_count":20485870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bunnycdn","bunnycdn-adapter","bunnycdn-api","bunnycdn-storage","cloudflare","cloudflare-worker","cloudflare-workers","ghost","ghost-blog","ghost-cms"],"created_at":"2025-03-20T15:28:40.836Z","updated_at":"2025-03-20T15:28:41.537Z","avatar_url":"https://github.com/MFYDev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ghost BunnyCDN Cache Purger\n\n## Motivation\n\nBunnyCDN's Perma-Cache feature retains files in a new directory structure after a full pull zone purge. While this can improve performance in some scenarios, it may lead to unnecessary storage costs over time. This webhook-based solution automates the clearing of old cache folders, preventing excessive space usage and associated expenses.\n\n## Features\n\n- Automatically purges BunnyCDN pull zone cache upon receiving a webhook from Ghost CMS\n- Clears old Perma-Cache folders to prevent unnecessary storage usage\n- Verifies webhook signatures for security\n- Logs detailed information for easy debugging and monitoring\n- Designed to run as a Cloudflare Worker for scalability and low latency\n\n## Installation\n\n1. Clone this repository:\n   ```\n   git clone https://github.com/yourusername/bunnycdn-cache-purger.git\n   ```\n\n2. Set up a Cloudflare Worker:\n   - Log in to your Cloudflare account and navigate to the Workers section\n   - Create a new Worker and paste the contents of `index.js` into the script editor\n\n3. Set up environment variables in your Cloudflare Worker:\n   - GHOST_WEBHOOK_SECRET: Your Ghost webhook secret\n   - BUNNY_PULLZONE_ID: Your BunnyCDN pull zone ID\n   - BUNNY_API_KEY: Your BunnyCDN account API key\n   - BUNNY_STORAGE_ZONE_HOSTNAME: Your storage zone hostname (e.g., \"ny.storage.bunnycdn.com\")\n   - BUNNY_STORAGE_ZONE_NAME: Your storage zone name\n   - BUNNY_STORAGE_ZONE_PASSWORD: Your storage zone password\n\n4. Configure your Ghost CMS to send webhooks to your Cloudflare Worker URL when content is published or updated\n\n## Code Overview\n\n### Main Request Handler\n\nThe `handleRequest` function is the main entry point for processing requests:\n\n1. It first checks if all required environment variables are set.\n2. It then examines the request path.\n3. If the path is '/purge-full-cache', it proceeds with the cache purging process.\n\n### Authentication\n\nThe code supports two types of authentication:\n\n1. **Manual Trigger**: It checks for a 'manualtriggertoken' header. If this matches the `MANUAL_TRIGGER_TOKEN` environment variable, it bypasses further authentication.\n\n2. **Ghost Webhook Verification**: If it's not a manual trigger, it verifies the request signature using the `verifySignature` function. This function:\n   - Extracts the signature and timestamp from the 'X-Ghost-Signature' header.\n   - Checks if the timestamp is within 5 minutes of the current time.\n   - Computes a hash using the request body, timestamp, and the `GHOST_WEBHOOK_SECRET`.\n   - Compares the computed hash with the provided signature.\n\n### Cache Purging\n\nIf authentication is successful, the code proceeds to purge the BunnyCDN cache:\n\n1. It sends a POST request to the BunnyCDN API to purge the entire pull zone cache.\n2. The pull zone ID and API key are taken from environment variables.\n\n### Perma-Cache Cleanup\n\nAfter successfully purging the cache, it calls the `cleanupPermaCacheFolders` function:\n\n1. This function first fetches a list of all folders in the '__bcdn_perma_cache__' directory of the BunnyCDN storage zone.\n2. It then iterates through each folder and sends a DELETE request to remove it.\n3. It keeps track of successful deletions and failures.\n\n### Error Handling and Logging\n\nThroughout the code, there are numerous `console.log` statements for debugging and error tracking. Each major function also has try-catch blocks to handle and log errors.\n\n### Response\n\nThe worker responds with different HTTP status codes based on the outcome:\n- 200: Successful cache purge and cleanup\n- 403: Invalid signature\n- 404: Invalid path\n- 500: Any other errors\n\n## License\n\nThis project is licensed under the MIT License. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfydev%2Fghost-bunnycdn-cache-clearer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmfydev%2Fghost-bunnycdn-cache-clearer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfydev%2Fghost-bunnycdn-cache-clearer/lists"}