Repository navigation
Add platform bindings to the environment of every request - #254
Merged
Merged
Conversation
The runtime defined D1, R2, KV and Workflow facades, managed OAuth and app connections once, on the importable env while the app's modules load. In production, Cloudflare can serve later requests in the same isolate with a new environment object, and the importable env follows the current request. Those requests saw the raw bindings but not the facades, so `env.DB` was undefined, for example when a connector page followed a status check in the same isolate. The facades are now kept in a registry. Entrypoints, object handlers, cron, queue and workflow runners add them to the environment they pass to the app and to the importable env on each invocation. This replaces copying them from the importable env, which lacked them as well.
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
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_OAUTHand app connections once, on the importableenv, 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/startafter a My connections status check:this.envin the requestenvin the requestDB,WIDEFLEET_OAUTHWIDEFLEET_D1_DBand other raw bindingsCloudflare hands that request a new environment object. The importable
envis 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
bindingRegistrykeeps the facades. They resolve the raw bindings from the current environment when called, so the same objects serve every request.withBindingsadds missing properties to the environment an invocation hands the app, and to the importableenv. Class entrypoints, object handlers, cron, queue and both workflow runners use it.withBindingsfrom./__widefleet_bindings.jsthrough thewidefleet:bindingsalias.env-merge.tsandenv-properties.tsfrom Give app entrypoints the platform's bindings on every environment #253.Tests
binding-registry.test.tsreproduces the production sequence with a workerd-style proxy: install at load, switch to a new environment object, then install again on the next request.pnpm check, unit, dynamic, capture, services and Cloudflare runtime suites.noindextest fails onmainas well.

Confidence Score: 5/5 
The PR appears safe to merge; no actionable issues were found.Summary
The PR keeps platform bindings in a shared registry and restores missing bindings before app handlers run.
No actionable issues were found. No tests were run during this review.
Diagram
%%{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"| EReviews (1) · Last reviewed commit: "Add platform bindings to the environment..." · Reviewed by Greptile