Skip to content

Add platform bindings to the environment of every request - #254

Merged
tnkuehne merged 1 commit into
mainfrom
fix/request-env-bindings
Nov 20, 2026
Merged

tnkuehne merged 1 commit into
mainfrom
fix/request-env-bindings

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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 Give app entrypoints the platform's bindings on every environment #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.

RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

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.

  • Every app request gets its platform bindings.

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"| E
Loading

Reviews (1) · Last reviewed commit: "Add platform bindings to the environment..." · Reviewed by Greptile

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.
@tnkuehne
tnkuehne merged commit b6e6839 into main Nov 20, 2026
16 checks passed
@tnkuehne
tnkuehne deleted the fix/request-env-bindings branch November 20, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant