Skip to content

Use from workers import env instead of importing from cloudflare:workers#32

Merged
G4brym merged 4 commits intomainfrom
fix/use-workers-env-import
Mar 9, 2026
Merged

Use from workers import env instead of importing from cloudflare:workers#32
G4brym merged 4 commits intomainfrom
fix/use-workers-env-import

Conversation

@G4brym
Copy link
Copy Markdown
Owner

@G4brym G4brym commented Mar 7, 2026

Summary

  • Replaces import_from_javascript("cloudflare:workers") + cf_workers.env pattern with direct from workers import env in the D1 database backend and R2 storage backend
  • Removes the now-unnecessary import_from_javascript import and caching from both backends
  • Uses the newer Workers Python API added in cloudflare/workerd#5682

Fixes #28

Changes

django_cf/db/backends/d1/base.py:

  • Removed from workers import import_from_javascript and self.import_from_javascript from __init__
  • Replaced self.import_from_javascript("cloudflare:workers") + getattr(cf_workers.env, ...) with from workers import env + getattr(env, ...)

django_cf/storage/r2.py:

  • Removed self._import_from_javascript instance variable
  • Changed lazy init guard from _import_from_javascript to _run_sync
  • Replaced the same cloudflare:workers pattern with direct from workers import env

Test plan

  • Verify D1 database queries work with the new import pattern
  • Verify R2 storage operations (read, write, delete, list) work correctly

🤖 Generated with Claude Code

Copy link
Copy Markdown
Owner Author

@G4brym G4brym left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review — APPROVED ✅

Review Scores: 5/5 reviewers approved

Summary

Clean migration from the JS interop import_from_javascript("cloudflare:workers") pattern to the native Python from workers import env API in both the D1 database backend and R2 storage backend. The change is semantically equivalent, reduces code by 5 lines, removes unnecessary instance state, and uses the newer Workers Python API (cloudflare/workerd#5682).

Review Perspectives

  1. Correctness: ✅ Semantically equivalent — getattr(env, self.binding) produces the same result as getattr(cf_workers.env, self.binding). Lazy init guard correctly updated from _import_from_javascript to _run_sync.
  2. Security: ✅ No security-relevant changes. Binding names come from Django settings (trusted input).
  3. Performance: ✅ Python's import cache makes repeated from workers import env calls negligible. Removing JS interop is a slight improvement.
  4. Code Quality: ✅ More Pythonic, less state, consistent pattern between both files.
  5. Testing: ✅ Straightforward API migration with equivalent behavior.

Non-blocking notes

  • In r2.py, from workers import env is now outside the try/except ImportError block, so a missing workers module would give a raw ImportError instead of Exception("Code not running inside a worker!"). This is fine in practice since workers is always available when pyodide.ffi is, and the ImportError message is clear on its own.

🤖 Automated review by prodboard

G4brym and others added 2 commits March 9, 2026 10:11
…udflare:workers")`

Replace the indirect `import_from_javascript("cloudflare:workers")` pattern
with the direct `from workers import env` import in both the D1 database
backend and R2 storage backend. This uses the newer Workers Python API
(cloudflare/workerd#5682) which is cleaner and avoids the JavaScript
interop layer for accessing environment bindings.

Fixes #28

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…compat date

- Remove all _import_from_javascript references from test_storage_errors.py
  and test_d1_backend.py since this attribute was removed in the workers env
  import migration
- Bump wrangler compatibility_date from 2025-11-25 to 2026-03-08 in all
  wrangler.jsonc files (tests/servers/r2, templates/d1, templates/durable-objects)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@G4brym G4brym force-pushed the fix/use-workers-env-import branch from bf73690 to add36d4 Compare March 9, 2026 10:12
@G4brym
Copy link
Copy Markdown
Owner Author

G4brym commented Mar 9, 2026

Changes in this update

Rebased from main and fixed CI failures:

  1. Removed _import_from_javascript references from teststests/r2/test_storage_errors.py had 10 references to the now-removed _import_from_javascript attribute (set via storage._import_from_javascript = MagicMock() and patch.object(storage, '_import_from_javascript', None)). These have been cleaned up since the attribute no longer exists after the from workers import env migration.

  2. Removed import_from_javascript from D1 test mocktests/db/test_d1_backend.py was still setting up mock_import_from_javascript and self.import_from_javascript in the mock wrapper. Removed since the actual DatabaseWrapper.__init__ no longer imports or stores this.

  3. Bumped wrangler compatibility_date to 2026-03-08 in all three wrangler.jsonc files:

    • tests/servers/r2/wrangler.jsonc
    • templates/d1/wrangler.jsonc
    • templates/durable-objects/wrangler.jsonc

The newer compat date is required for from workers import env to work properly in the Workers Python runtime.

G4brym and others added 2 commits March 9, 2026 10:18
Wrangler 4.51.0's bundled workerd runtime only supports compat dates up
to 2025-11-25, which predates the `from workers import env` API.
Upgrading to wrangler 4.71.0 brings a newer workerd that supports the
2026-03-08 compat date and the direct env import.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@G4brym G4brym merged commit cb30a81 into main Mar 9, 2026
1 check passed
@G4brym G4brym deleted the fix/use-workers-env-import branch March 9, 2026 10:25
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.

Add from workers import env instead of importing from cloudflare:workers

1 participant