-
Notifications
You must be signed in to change notification settings - Fork 218
cloudflare self-host: browser approval + a Cloudflare e2e target #1016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
| // The Cloudflare host boot recipe: the REAL worker on workerd via `wrangler dev` | ||
| // (Miniflare) with a local D1 + R2 and dev-auth on. Shared by the vitest | ||
| // globalsetup (ephemeral) and, like the other hosts, available to a dev CLI. | ||
| // | ||
| // The browser scenarios drive the console `/resume` page, which the worker | ||
| // serves as Static Assets from `dist/` — so the SPA is built first (vite build, | ||
| // a couple of seconds) before wrangler serves it. | ||
| import { execFile } from "node:child_process"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { promisify } from "node:util"; | ||
|
|
||
| import { bootProcesses, waitForHttp, type BootedProcesses } from "./boot"; | ||
|
|
||
| export const cloudflareDir = fileURLToPath(new URL("../../apps/host-cloudflare/", import.meta.url)); | ||
|
|
||
| export interface CloudflareBootOptions { | ||
| readonly port: number; | ||
| readonly logFile?: string; | ||
| /** Skip the SPA build when `dist/` is already current (fast local iteration). */ | ||
| readonly skipBuild?: boolean; | ||
| } | ||
|
|
||
| export const bootCloudflare = async (options: CloudflareBootOptions): Promise<BootedProcesses> => { | ||
| if (!options.skipBuild) { | ||
| await promisify(execFile)("bun", ["run", "build"], { cwd: cloudflareDir }); | ||
| } | ||
|
|
||
| const procs = bootProcesses( | ||
| [ | ||
| { | ||
| // bunx resolves host-cloudflare's own wrangler. `--local` is the default; | ||
| // dev-auth + the secret key arrive as `--var` overrides so the worker | ||
| // needs no Cloudflare account or real Access app. | ||
| cmd: "bunx", | ||
| args: [ | ||
| "wrangler", | ||
| "dev", | ||
| "--port", | ||
| String(options.port), | ||
| "--ip", | ||
| "127.0.0.1", | ||
| "--var", | ||
| "ENABLE_DEV_AUTH:true", | ||
| "--var", | ||
| "EXECUTOR_SECRET_KEY:e2e-secret-key-0123456789abcdef0123456789abcdef", | ||
| ], | ||
| cwd: cloudflareDir, | ||
| env: { WRANGLER_SEND_METRICS: "false", CI: "true" }, | ||
| logFile: options.logFile, | ||
| }, | ||
| ], | ||
| { label: "cloudflare" }, | ||
| ); | ||
|
|
||
| try { | ||
| // dev-auth: /api/account/me answers 200 as the dev admin once the worker is | ||
| // up (workerd boot + esbuild + D1 schema bring-up take a beat on first run). | ||
| await waitForHttp(`http://127.0.0.1:${options.port}/api/account/me`, { timeoutMs: 120_000 }); | ||
| } catch (error) { | ||
| await procs.teardown(); | ||
| throw error; | ||
| } | ||
| return procs; | ||
| }; |
This file contains hidden or 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,30 @@ | ||
| // Boot the Cloudflare target: claim this checkout's port atomically, then run | ||
| // the shared boot recipe (cloudflare.boot.ts). Set E2E_CLOUDFLARE_URL to attach | ||
| // to an already-running instance instead. | ||
| import { claimPorts } from "../src/ports"; | ||
| import { waitForHttp } from "./boot"; | ||
| import { bootCloudflare } from "./cloudflare.boot"; | ||
|
|
||
| export default async function setup(): Promise<(() => Promise<void>) | void> { | ||
| if (process.env.E2E_CLOUDFLARE_URL) { | ||
| await waitForHttp(`${process.env.E2E_CLOUDFLARE_URL}/api/account/me`); | ||
| return; | ||
| } | ||
|
|
||
| const { ports, release } = await claimPorts([ | ||
| { envVar: "E2E_CLOUDFLARE_PORT", offset: 5, label: "cloudflare wrangler dev" }, | ||
| ]); | ||
| const port = ports.E2E_CLOUDFLARE_PORT!; | ||
|
|
||
| let procs; | ||
| try { | ||
| procs = await bootCloudflare({ port }); | ||
| } catch (error) { | ||
| await release(); | ||
| throw error; | ||
| } | ||
| return async () => { | ||
| await procs.teardown(); | ||
| await release(); | ||
| }; | ||
| } |
This file contains hidden or 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,30 @@ | ||
| // The Cloudflare self-host app (apps/host-cloudflare) as a target: the REAL | ||
| // worker on workerd via Miniflare (wrangler `unstable_dev`) with a local D1 + | ||
| // R2, booted in setup/cloudflare.globalsetup.ts. Dev-auth is on, so every | ||
| // request is the fixed dev admin — no per-identity login and no MCP OAuth (the | ||
| // /mcp endpoint accepts the dev principal directly). Single-tenant, like | ||
| // self-host; per-test isolation is the next step here. | ||
| import { Effect } from "effect"; | ||
|
|
||
| import { e2ePort } from "../src/ports"; | ||
| import type { Identity, Target } from "../src/target"; | ||
|
|
||
| // Offsets 0-4 are taken by cloud (0-3) and self-host (4); Cloudflare claims 5. | ||
| export const CLOUDFLARE_PORT = e2ePort("E2E_CLOUDFLARE_PORT", 5); | ||
| export const CLOUDFLARE_BASE_URL = | ||
| process.env.E2E_CLOUDFLARE_URL ?? `http://127.0.0.1:${CLOUDFLARE_PORT}`; | ||
|
|
||
| export const cloudflareTarget = (): Target => ({ | ||
| name: "cloudflare", | ||
| baseUrl: CLOUDFLARE_BASE_URL, | ||
| mcpUrl: `${CLOUDFLARE_BASE_URL}/mcp`, | ||
| // No "billing" and no setAccessTokenTtl (Cloudflare Access is the IdP; not | ||
| // test-adjustable). "mcp-oauth" advertises that the MCP surface exists — but | ||
| // dev-auth means it needs no consent flow, so `mcpConsent` is omitted and the | ||
| // MCP client connects as the dev admin directly. | ||
| capabilities: new Set(["api", "browser", "mcp-oauth"]), | ||
| // Dev-auth: one fixed admin. Empty `headers` makes the API surface send no | ||
| // auth (and skip the Better Auth sign-in path) — the worker resolves every | ||
| // request to the dev admin. No cookie is needed for the browser either. | ||
| newIdentity: () => Effect.succeed({ label: "dev-admin", headers: {} } satisfies Identity), | ||
| }); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.