Skip to content

fix: Handle cloudflare-internal:workers default export#139

Closed
penalosa wants to merge 2 commits intomainfrom
fix-internal-workers-module
Closed

fix: Handle cloudflare-internal:workers default export#139
penalosa wants to merge 2 commits intomainfrom
fix-internal-workers-module

Conversation

@penalosa
Copy link
Collaborator

@penalosa penalosa commented Feb 18, 2026

When running as a workerd extension module (like miniflare's dispatch-namespace.worker.ts), only cloudflare-internal:workers is available, rather than cloudflare:workers. It's easy enough for Miniflare to rewrite the capnpweb imports to cloudflare-internal:workers when bundling it's internal workers, but the import * approach causes problems, since the internal module exports everything on default rather than as named exports:

// cloudflare:workers (regular workers)
import * as cfw from "cloudflare:workers";
cfw.RpcTarget // ✅ works

// cloudflare-internal:workers (extension modules)
import * as cfw from "cloudflare-internal:workers";
cfw.RpcTarget // ❌ undefined
cfw.default.RpcTarget // ✅ works

This change normalizes workersModule to handle both cases by checking for a default export first.

When running as a workerd extension module, cloudflare:workers is
rewritten to cloudflare-internal:workers. However, the internal module
exports everything on `default` rather than as named exports.

This change normalizes the workersModule to handle both cases:
- cloudflare:workers: named exports (workersModule.RpcTarget)
- cloudflare-internal:workers: default export (workersModule.default.RpcTarget)

This enables capnweb to work in miniflare's extension workers like
dispatch-namespace.worker.ts.
@changeset-bot
Copy link

changeset-bot bot commented Feb 18, 2026

🦋 Changeset detected

Latest commit: 29263e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
capnweb Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 18, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@139

commit: 29263e1

@penalosa penalosa marked this pull request as ready for review February 18, 2026 11:38
@penalosa
Copy link
Collaborator Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Feb 18, 2026
@penalosa penalosa requested a review from kentonv February 18, 2026 12:04

let workersModule: any = (globalThis as any)[WORKERS_MODULE_SYMBOL];
// Handle cloudflare-internal:workers which exports on `default` instead of named exports
workersModule = workersModule?.default ?? workersModule;

Choose a reason for hiding this comment

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

Is there a chance that this breaks modules that previously had a minimal default export but really wanted to share most exports via named exports?

Choose a reason for hiding this comment

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

Oh I see this is explicitly the cloudflare:workers module.
I thought this was generic code for any module.
How does that work for cloudflare:workers-internal then?

Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

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

I'm uncomfortable with this fix as it means that if cloudflare:workers would never be able to add a default export without breaking Cap'n Web users in the wild. We cannot force-update Cap'n Web so we would not be able to fix it on the Cap'n Web side.

Moreover, it's unclear that this is a lasting fix as there could be further differences between cloudflare-internal:workers and cloudflare:workers that may only reveal themselves in the future when Cap'n Web tries to use more of the cloudflare:workers interface.

IMO the problem here is a problem with workerd extension modules, they should be allowed to import cloudflare:workers. This seems like an important thing to fix in general as it is increasingly the case that you need to import cloudflare:workers to properly implement a lot of things.

@penalosa
Copy link
Collaborator Author

@kentonv fair enough—I'll close this and try and see if I can open a PR to workerd

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.

3 participants