Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Deploy

# Merging to main deploys the Worker to login.tangent.workers.dev, after the unit
# tests pass. It needs one repository secret, CLOUDFLARE_API_TOKEN (a Cloudflare
# API token made from the "Edit Cloudflare Workers" template; the same kind flare
# uses). Without it the deploy step is skipped with a note, so nothing fails
# before it is set up.
# A deploy uploads the code and wrangler.toml (vars such as ALLOWED_ORIGINS and
# the D1 binding). The Worker's secrets (ANTHROPIC_API_KEY, SESSION_SECRET, the
# Google/Apple keys) live in Cloudflare and are left as they are. The D1 schema
# is not touched either; see the README for loading it by hand.
on:
push:
branches: [main]
workflow_dispatch:

concurrency:
group: deploy
cancel-in-progress: false

jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: 41f6f507a22c7eec431dbc5e9670c73d # the tangent account, as in flare
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm test
- name: Deploy
if: env.CLOUDFLARE_API_TOKEN != ''
run: |
npm ci
npx wrangler deploy
- name: Not deployed
if: env.CLOUDFLARE_API_TOKEN == ''
run: echo "::warning::Not deployed. Add the CLOUDFLARE_API_TOKEN repository secret to deploy on merge (see README)."
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Tests

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
# No install: the tests use only Node's built-in test runner and an in-memory D1.
- run: npm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.wrangler/
.dev.vars
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ npx wrangler secret put APPLE_PRIVATE_KEY # paste the whole .p8 contents

Anything `false` there is your next job. It never echoes a secret.

### Deploying on merge

After the first setup, merging to `main` deploys automatically: the "Deploy" workflow in GitHub Actions runs the tests and then `wrangler deploy`. It needs one setting, made once:

1. **A Cloudflare API token.** In the Cloudflare dashboard: **My Profile → API Tokens → Create Token → Edit Cloudflare Workers** template, **Account Resources** set to your account, **Zone Resources** to *All zones*, then **Continue to summary → Create Token**. (The token made for flare works here too, if you kept it.)
2. **Give it to GitHub.** In this repo: **Settings → Secrets and variables → Actions → New repository secret**, named `CLOUDFLARE_API_TOKEN`.

Until it is set, merges skip the deploy with a warning. A deploy can also be started from the **Actions** tab (Deploy → Run workflow). A deploy sends the code and `wrangler.toml`; the Worker's secrets and the D1 data are left as they are.

## Using it from the app

```html
Expand Down Expand Up @@ -159,7 +168,7 @@ If the two workloads diverge further, give the expensive one its own Worker with
## Tests

```bash
npm test # 29 tests, no network, no wrangler
npm test # 30 tests, no network, no wrangler
```

The suite runs the Worker handler directly against a stubbed Anthropic and a **real SQLite** standing in for D1 (via `node:sqlite`), so the quota logic is exercised as actual SQL rather than against a fake that would accept anything. It covers session forgery and expiry, the open-redirect guard on `redirect_uri`, the full OAuth callback including wrong-audience and unverified-email rejection, popup `postMessage` targeting the app origin rather than `*`, per-user quota isolation, a **ten-way concurrent race** proving a spent allowance can't be beaten by parallel requests, per-user overrides and blocks, the burst limiter firing before any database work, and a timing assertion that SSE genuinely streams while usage is counted off a tee. The dev stub has its own set: that it contacts no provider, that `as=` really produces separate people with separate allowances, that it stays off unless `DEV_AUTH` is exactly `"true"`, that enabling it does not weaken the open-redirect guard, and that `/health` shouts while it is live.
Expand Down
Loading
Loading