Repository navigation
Give app entrypoints the platform's bindings on every environment - #253
Merged
Merged
Conversation
The runtime installs D1, R2 and KV facades, managed OAuth and app connections on the importable env when an app loads. That env is a proxy for the Worker's base environment. In production, Cloudflare's Worker Loader hands the default HTTP entrypoint another environment object, so apps reading `this.env` or a handler's `env` found `DB` undefined there, while named RPC entrypoints and the importable env worked. Entrypoints, object handlers, cron, queue and workflow runners now pass apps an environment that carries every property of the importable env. An environment that cannot be extended is copied. The importable env is read only by keys and values, since reading descriptors through its proxy breaks invariants for non-configurable bindings.
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
widefleet-docs | 9c25bee | Nov 20 2026, 08:19 PM |
tnkuehne
added a commit
that referenced
this pull request
Nov 20, 2026
## Problem Connector pages still fail with `Cannot read properties of undefined (reading 'batch')` on 0.7.20. #253 did not fix it. The runtime defines the D1/R2/KV/Workflow facades, `WIDEFLEET_OAUTH` and app connections once, on the importable `env`, while the app's modules load. A diagnostic app in production logged what happens next. When the same isolate serves a later request, such as `/connect/start` after a My connections status check: | | at module load | `this.env` in the request | importable `env` in the request | |---|---|---|---| | `DB`, `WIDEFLEET_OAUTH` | present | missing | missing | | `WIDEFLEET_D1_DB` and other raw bindings | present | present | present | Cloudflare hands that request a new environment object. The importable `env` is a proxy for the current environment, so it follows the new object. Everything defined at module load is gone. Requests in a fresh isolate work, which is why single test calls succeeded. #253 copied the facades from the importable `env`. It lacked them too, so the copy changed nothing. ## Change - `bindingRegistry` keeps the facades. They resolve the raw bindings from the current environment when called, so the same objects serve every request. - `withBindings` adds missing properties to the environment an invocation hands the app, and to the importable `env`. Class entrypoints, object handlers, cron, queue and both workflow runners use it. - Child bundles that are compiled separately import `withBindings` from `./__widefleet_bindings.js` through the `widefleet:bindings` alias. - Module load still installs the facades, for apps that read bindings at the top level. - Every property is configurable, so reading it through the importable env's proxy no longer breaks proxy invariants. - Removes `env-merge.ts` and `env-properties.ts` from #253. ## Tests - `binding-registry.test.ts` reproduces the production sequence with a workerd-style proxy: install at load, switch to a new environment object, then install again on the next request. - Ran `pnpm check`, unit, dynamic, capture, services and Cloudflare runtime suites. - Locally, the control plane `noindex` test fails on `main` as well. <!-- greptile_comment --> <!-- greptile_summary --> <h2><a href="https://app.greptile.com/api/retrigger?id=114626572&sig=2db3c64dab16b102ede7edb841ce761c7a5bf5b9321ad276bcfe09166dd32c9c"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=mendra&next=%2Fwidefleet%2Fwidefleet%2Fpull%2F254&utm_source=greptile_review&utm_medium=github&utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier: plus" title="Review tier: plus" src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-plus.svg?v=3" height="12"></sup></h2> <!-- greptile-impact-verdict --> The PR appears safe to merge; no actionable issues were found. <details open><summary><h3>Summary</h3></summary> The PR keeps platform bindings in a shared registry and restores missing bindings before app handlers run. - Every app request gets its platform bindings. No actionable issues were found. No tests were run during this review. </details> <details open><summary><h3>Diagram</h3></summary> ```mermaid %%{init: {'theme': 'neutral'}}%% flowchart TD A["Define platform bindings"] --> B["Shared binding registry"] B --> C["Install during module load"] D["App invocation"] --> E["withBindings"] B --> E E --> F["Complete importable env"] E --> G["Complete invocation env"] G --> H["Call app code"] I["Event and workflow modules"] -->|"widefleet:bindings"| E ``` </details> <!-- greptile_confidence_score:5 --> <sub>Reviews (1) · Last reviewed commit: ["Add platform bindings to the environment..."](b450d8a) · [Reviewed by Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub> <!-- /greptile_comment -->
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Hosted apps that read bindings through
this.envor a handler'senvlose the platform's facades on the HTTP path. The GitHub template's browser routes (/,/connect/start) fail withCannot read properties of undefined (reading 'batch'):initialize(this.env.DB)getsundefined. The same app'sUserConnectionRPC calls read the database without problems, and apps that useimport { env } from "cloudflare:workers"are unaffected.The runtime installs the D1/R2/KV facades,
WIDEFLEET_OAUTHand app connections on the importableenvwhen the app loads. In workerd, that export is a proxy for the Worker's base environment (getWorkerEnv()). Locally every entrypoint receives the same object, so all tests passed. In production, Cloudflare's Worker Loader hands the default HTTP entrypoint a different environment object that doesn't have them.Change
withEnvPropertiespasses apps an environment that carries every property of the importableenv.ScopedEntrypoint), object handlers, the cron and queue runners, and both workflow runners.envis read by keys and values only. Reading descriptors through its proxy throws for non-configurable bindings such asWIDEFLEET_OAUTH; the managed OAuth runtime test caught that.Tests
env-properties.test.tscovers merging, frozen environments and a workerd-style proxy with a non-configurable binding.pnpm check, all unit tests, and the dynamic, workflow, capture, services, managed OAuth and Cloudflare runtime suites.noindextest fails onmainas well.deployment.test.tsneeds a Docker network, which this machine could not create.

Confidence Score: 5/5
The PR appears safe to merge; the previous binding-name issue is fixed.Summary
App entrypoints now receive missing platform bindings from the importable
env.Reviews (2) · Last reviewed commit: "Keep a binding named __proto__ when merg..." · Reviewed by Greptile