Repository navigation
[No QA] Run the Cloudflare Worker during local web development #104525
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
+1,619
−52
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
dee3db5
docs: plan local Worker development
roryabraham 780e8d3
feat(web): run local sessions through the Worker
roryabraham 456facc
refactor(web): use Bun-native launcher APIs
roryabraham a76a0b7
Merge remote-tracking branch 'origin/main' into plan/104387-local-clo…
roryabraham f732268
Merge commit 'f2978e44948dc8cebfa51f17e59c9d85c478d261' into plan/104…
roryabraham 0e6c505
Merge commit '9279ac926016302cc4d4d7368295983730dff7df' into plan/104…
roryabraham 67ac29d
style(web): prefix boolean variable names
roryabraham bee309f
Merge commit 'b88098c5d58e596fd40e875074a1e8e65416832f' into plan/104…
roryabraham fd2a2f1
fix(web): wait for Rsbuild before starting Worker
roryabraham 7ab263d
docs: keep implementation plan local
roryabraham 97588de
refactor(web): simplify local Worker setup
roryabraham a9999d9
Merge main into plan/104387-local-cloudflare-worker
roryabraham 727fe41
fix(web): allow local backend media and frames in dev CSP
roryabraham 26aa6d5
refactor(web): reuse createNotFoundResponse for dev 404s
roryabraham cf6b80f
fix(web): stop requiring mkcert to start the dev server
roryabraham 9d524e6
test(web): release the pending handler before stopping the test server
roryabraham fec2179
fix(web): don't stop the dev session on a slow first compile
roryabraham 8cc1d51
fix(web): only install the Worker tooling when its lockfile changes
roryabraham 893355b
Merge main into plan/104387-local-cloudflare-worker
roryabraham 3b51c62
test(web): fail fast when a reload test edit misses its target
roryabraham ce6c4df
fix(web): drop redundant hardcoded dev CSP origins
roryabraham 49ab611
fix(web): validate live Worker vars once with named errors
roryabraham 428600f
refactor(web): share the nonce placeholder with the Rsbuild config
roryabraham 320f270
refactor(web): define the API proxy paths and port once
roryabraham 95ac108
refactor(web): define the local backend env keys once
roryabraham 0cd9db1
refactor(web): share one readiness loop between the launcher and Rsbu…
roryabraham ed8a288
refactor(web): keep getWebDevPorts free of port probes
roryabraham 222c691
fix(web): pass launcher settings to Rsbuild explicitly
roryabraham c3194a6
style(web): list Wrangler arguments one per line
roryabraham bec3811
refactor(web): launch Rsbuild and Wrangler with npx
roryabraham 26dc149
chore(deps): bump @types/node to match the Node 26 runtime
roryabraham 1d87d41
refactor(web): move getWebCommands into its own module
roryabraham 2782299
fix(web): allow the QA backend and its Cloudflare Access issuer in de…
roryabraham 5763cd7
fix(web): serve Rsbuild directly when the Worker tooling is missing
roryabraham 97c8032
fix(web): warn instead of failing npm install when the Worker install…
roryabraham c46878f
ci: only run the Worker tests for changes they cover
roryabraham f4a652c
fix(web): let a second session pick free ports and share the API proxy
roryabraham bda7aac
Merge main into plan/104387-local-cloudflare-worker
roryabraham 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** Local backends from `.env`, which the app calls and the launcher adds to the live Worker's CSP. */ | ||
| const LOCAL_BACKEND_URL_KEYS = [ | ||
| 'EXPENSIFY_URL', | ||
| 'SECURE_EXPENSIFY_URL', | ||
| 'STAGING_EXPENSIFY_URL', | ||
| 'STAGING_SECURE_EXPENSIFY_URL', | ||
| 'QA_EXPENSIFY_URL', | ||
| 'QA_SECURE_EXPENSIFY_URL', | ||
| 'NGROK_URL', | ||
| 'SECURE_NGROK_URL', | ||
| ]; | ||
|
|
||
| /** The Cloudflare Access team for the QA backend: a bare hostname whose token endpoint the app calls over HTTPS. */ | ||
| const QA_ACCESS_TEAM_DOMAIN_KEY = 'QA_CF_TEAM_DOMAIN'; | ||
|
|
||
| /** Public development settings that shell variables can override in the app config. Never forward arbitrary shell variables, which may contain secrets. */ | ||
| const FORWARDED_DEVELOPMENT_ENV_KEYS = [ | ||
| ...LOCAL_BACKEND_URL_KEYS, | ||
| QA_ACCESS_TEAM_DOMAIN_KEY, | ||
| 'USE_WEB_PROXY', | ||
| 'USE_NGROK', | ||
| 'NEW_EXPENSIFY_URL', | ||
| 'SKIP_ONBOARDING', | ||
| 'PUSHER_APP_KEY', | ||
| 'PUSHER_DEV_SUFFIX', | ||
| ]; | ||
|
|
||
| export {FORWARDED_DEVELOPMENT_ENV_KEYS, LOCAL_BACKEND_URL_KEYS, QA_ACCESS_TEAM_DOMAIN_KEY}; | ||
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,29 @@ | ||
| type DevServerSettings = { | ||
| isWorkerEnabled: boolean; | ||
|
|
||
| /** The browser-facing port, which links built by the app use. */ | ||
| publicPort: number; | ||
|
|
||
| /** The port Rsbuild binds: private behind the Worker, otherwise the public port. */ | ||
| port: number; | ||
| strictPort: boolean; | ||
| open: string | false; | ||
| }; | ||
|
|
||
| /** | ||
| * `npm run web` owns the ports and opening the browser, and says so with WEB_LAUNCHER. Without it, Rsbuild runs on its | ||
| * own, so launcher settings that `.env` may contain do not apply. | ||
| */ | ||
| async function getDevServerSettings(env: Record<string, string | undefined>, findPort: () => Promise<number>): Promise<DevServerSettings> { | ||
| if (env.WEB_LAUNCHER !== 'true') { | ||
| const port = await findPort(); | ||
| // Rsbuild's --open flag defaults to the first entry's route, e.g. "/main" for our "main" entry key, which 404s | ||
| // since the app has no route by that name. Open the actual app root instead. | ||
| return {isWorkerEnabled: false, publicPort: port, port, strictPort: false, open: '/'}; | ||
| } | ||
| const isWorkerEnabled = env.USE_CLOUDFLARE_WORKER === 'true'; | ||
| const publicPort = Number(env.WEB_PORT); | ||
| return {isWorkerEnabled, publicPort, port: isWorkerEnabled ? Number(env.WEB_INTERNAL_PORT) : publicPort, strictPort: true, open: false}; | ||
| } | ||
|
|
||
| export default getDevServerSettings; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
| /** | ||
| * Install the independently locked Worker tooling during local App setup, so `npm run web` can run it. CI jobs that need | ||
| * the Worker install it themselves, and local installs skip it until its lockfile changes. | ||
| */ | ||
| import {$} from 'bun'; | ||
| import path from 'node:path'; | ||
|
|
||
| /** Kept inside node_modules, so deleting the Worker dependencies also invalidates it. */ | ||
| const INSTALLED_LOCKFILE_HASH_PATH = 'node_modules/.app-installed-lockfile-hash'; | ||
|
|
||
| async function runNpmCI(workerDir: string): Promise<void> { | ||
| await $`npm ci --include=dev`.cwd(workerDir); | ||
| } | ||
|
|
||
| async function installCloudflareWorker(workerDir: string, env: Record<string, string | undefined> = Bun.env, install = runNpmCI): Promise<boolean> { | ||
| if (env.CI) { | ||
| return false; | ||
| } | ||
| const lockfileHash = new Bun.CryptoHasher('sha256').update(await Bun.file(path.join(workerDir, 'package-lock.json')).text()).digest('hex'); | ||
| const installedLockfileHash = Bun.file(path.join(workerDir, INSTALLED_LOCKFILE_HASH_PATH)); | ||
| if ((await installedLockfileHash.exists()) && (await installedLockfileHash.text()) === lockfileHash) { | ||
| return false; | ||
| } | ||
| console.log('Installing local Cloudflare Worker tooling'); | ||
| try { | ||
| await install(workerDir); | ||
| } catch (error) { | ||
| // Only `npm run web` uses the Worker, and it serves Rsbuild directly without it, so this must not fail the App install. | ||
| console.warn('Could not install the Cloudflare Worker tooling. npm run web will serve Rsbuild directly until the next npm install installs it.', error); | ||
| return false; | ||
| } | ||
| await Bun.write(installedLockfileHash, lockfileHash); | ||
| return true; | ||
| } | ||
|
|
||
| if (import.meta.main) { | ||
| await installCloudflareWorker(path.resolve(import.meta.dir, '../web/cloudflare-worker')); | ||
| } | ||
|
|
||
| export default installCloudflareWorker; |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nab Seems kind of annoying to maintain, is there a better way to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I have a better suggestion - fortunately it just references keys in a
.env, which makes it easy to configure per-workspace (such as if you're trying to set up parallel Docker stacks).