Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jacobmgevans/turbo-r2-archive
TurboRepo API Compliant Remote Caching w/ Cloudflare Workers using R2
https://github.com/jacobmgevans/turbo-r2-archive
Last synced: 29 days ago
JSON representation
TurboRepo API Compliant Remote Caching w/ Cloudflare Workers using R2
- Host: GitHub
- URL: https://github.com/jacobmgevans/turbo-r2-archive
- Owner: JacobMGEvans
- License: mit
- Created: 2023-08-24T19:33:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-29T02:53:01.000Z (about 1 year ago)
- Last Synced: 2023-09-29T04:28:16.762Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 300 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Turbo R2 Archive
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/jacobMGEvans/Turbo-R2-Archive)
## Overview
This is a Worker that will act as an event server for caching TurboRepo artifacts. Compliant with the TurboRepo API, for remote caching, it will store the cache artifacts in a Cloudflare R2 bucket and purge the R2 Objects on a schedule, using [R2 Object lifecycle rules](https://blog.cloudflare.com/introducing-object-lifecycle-management-for-cloudflare-r2/). This allows for all the benefits of remote caching TurboRepo artifacts on Cloudflare's edge network.
This will require a Cloudflare account, with a zone and R2.
## Environment Variables
Utilizing `.env` file to store the variables for Turborepo API & Worker. The `.env` file should be in the root of the project directory. The `.env` file should contain the following variables. Be sure to add the `.env` file to `.gitignore` to prevent it from being committed to the repository.
The commands are ran with `dotenv` cli `pnpm exec dotenv -- pnpm exec turbo ` to inject the environment variables into the command process environment.
```bash
TURBO_API= # https://something.com
TURBO_TEAM= # team_whatever it has to start with prefix team_
TURBO_TOKEN= # whatever is set in the worker secret
TURBO_REMOTE_CACHE_SIGNATURE_KEY= # needs to be the same for every one using the same cache
```## Turbo Project Config
To utilize the `TURBO_REMOTE_CACHE_SIGNATURE_KEY` which will increase the security of the remote cache, the project config will need to be updated to include the following:
```json
{
"remoteCache": { "signature": true }
}
```## Worker Configuration
The endpoints are protected by a bearer token. The token is stored as a secret in the Worker. The token can be set with the following command:
```bash
echo | wrangler secret put TURBO_TOKEN
```The `crons` array is a list of cron expressions that will trigger the purge. The default is every Sunday at 1am. The default expiration time is 6 days, expressed in `vars` `EXPIRATION_HOURS`, which allows for the cache to be completely purged once a week.
```jsonc
{
"vars": {
"EXPIRATION_HOURS": 144 // 6 days
},
"crons": ["0 1 * * 0"], // Every Sunday at 1am"r2_buckets": [
{
"binding": "R2_ARTIFACT_ARCHIVE", // The binding name for the bucket, used in the Worker i.e. env.R2_ARTIFACT_ARCHIVE.get()
"bucket_name": "turbo-cache", // bucket name when looking for objects in dashboard
"preview_bucket_name": "turbo-cache-preview"
}
]
}
```### Manual Cache Purge
The cache can also be purged manually by sending a `POST` request to the `/artifacts/manual-cache-bust` endpoint. This can be done with the following command:
```bash
https -A bearer -a POST /artifacts/manual-cache-bust expireInHours:=0
```Setting the value to `0` will purge the entire cache.
## How can I develop & test locally?
This project is primarily self-service and is meant to be forked and modified to fit your needs. The following instructions will help you get started.
### Running the Worker Locally
The Worker can be run, locally with the `package.json` script `start`:
```bash
pnpm start
```### TurboRepo Testing Local Server
Well since we are already using Cloudflare, let's keep that going. `cloudflared` allows for creating a tunnel to your `http://127.0.0.1:8787` and exposing it to the internet. This will allow others direct their TurboRepo to the local dev server by setting`TURBO_API` to ``.
The command to create a tunnel looks like:```bash
cloudflared tunnel --hostname --url http://127.0.0.1:8787/ --name r2-archive (or whatever you want to name it)
```## How can I deploy this?
Fork this repo, have a Cloudflare account, and run the following command:
```bash
pnpm deploy
```You now have a TurboRepo API compliant event server that will cache artifacts on Cloudflare's edge network.