Skip to content

Commit

Permalink
Optimize the performance, and logic etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
MFYDev committed Aug 28, 2024
1 parent bf65154 commit b4a641e
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 170 deletions.
51 changes: 42 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,52 @@ BunnyCDN's Perma-Cache feature retains files in a new directory structure after

## Code Overview

The main components of the script are:
### Main Request Handler

- `handleRequest`: The main function that processes incoming requests
- `verifySignature`: Verifies the Ghost webhook signature for security
- `cleanupPermaCacheFolders`: Clears old Perma-Cache folders in BunnyCDN storage
The `handleRequest` function is the main entry point for processing requests:

The script listens for POST requests to the `/purge-full-cache` endpoint. When a valid request is received, it:
1. It first checks if all required environment variables are set.
2. It then examines the request path.
3. If the path is '/purge-full-cache', it proceeds with the cache purging process.

1. Verifies the Ghost webhook signature
2. Purges the BunnyCDN pull zone cache
3. Cleans up old Perma-Cache folders
### Authentication

Detailed logs are provided at each step for easy monitoring and debugging.
The code supports two types of authentication:

1. **Manual Trigger**: It checks for a 'manualtriggertoken' header. If this matches the `MANUAL_TRIGGER_TOKEN` environment variable, it bypasses further authentication.

2. **Ghost Webhook Verification**: If it's not a manual trigger, it verifies the request signature using the `verifySignature` function. This function:
- Extracts the signature and timestamp from the 'X-Ghost-Signature' header.
- Checks if the timestamp is within 5 minutes of the current time.
- Computes a hash using the request body, timestamp, and the `GHOST_WEBHOOK_SECRET`.
- Compares the computed hash with the provided signature.

### Cache Purging

If authentication is successful, the code proceeds to purge the BunnyCDN cache:

1. It sends a POST request to the BunnyCDN API to purge the entire pull zone cache.
2. The pull zone ID and API key are taken from environment variables.

### Perma-Cache Cleanup

After successfully purging the cache, it calls the `cleanupPermaCacheFolders` function:

1. This function first fetches a list of all folders in the '__bcdn_perma_cache__' directory of the BunnyCDN storage zone.
2. It then iterates through each folder and sends a DELETE request to remove it.
3. It keeps track of successful deletions and failures.

### Error Handling and Logging

Throughout 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.

### Response

The worker responds with different HTTP status codes based on the outcome:
- 200: Successful cache purge and cleanup
- 403: Invalid signature
- 404: Invalid path
- 500: Any other errors

## License

Expand Down
Loading

0 comments on commit b4a641e

Please sign in to comment.