fix: Handle cloudflare-internal:workers default export#139
fix: Handle cloudflare-internal:workers default export#139
Conversation
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 detectedLatest commit: 29263e1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
All contributors have signed the CLA ✍️ ✅ |
commit: |
|
I have read the CLA Document and I hereby sign the CLA |
|
|
||
| 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
kentonv
left a comment
There was a problem hiding this comment.
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.
|
@kentonv fair enough—I'll close this and try and see if I can open a PR to workerd |
When running as a workerd extension module (like miniflare's
dispatch-namespace.worker.ts), onlycloudflare-internal:workersis available, rather thancloudflare:workers. It's easy enough for Miniflare to rewrite the capnpweb imports tocloudflare-internal:workerswhen bundling it's internal workers, but theimport *approach causes problems, since the internal module exports everything ondefaultrather than as named exports:This change normalizes
workersModuleto handle both cases by checking for adefaultexport first.