-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for hybrid (node) module polyfills #181
Comments
It seems an interesting idea. If workerd could provide an alternative way to access node polyfills like An alternative would involved unenv + specific bundler (rollup/vite/nitro) plugin that preserves external imports from |
ok, thanks for the confirmation @pi0. I'll discuss with the team, and get back to you. |
We discussed this and it appears that the easiest way for us to support this is via a newly proposed (and almost landed) process.getBuiltinModule API. It would be fairly easy for us to implement it and return the native module which could then be used by unenv to compose a hybrid polyfill. If user code uses process.getBuiltinModule to access the native module then they'll see only the workerd version of the API, which is fine as we expect only very specialized code and not general npm packages to use this newly added node API. |
One observation that came out of cloudflare/workerd#2129 is that if we want the polyfills to be truly hybrid, we'll need to update internal module and symbol references to be absolute so that we don't end up with multiple implementations of an API due to internal references not respecting the presets overrides. I don't believe that this is a show stopper, it's just something that will take special consideration. |
Thanks for the pointer. I think in platforms such as worked that can actually implement |
Describe the feature
Unenv's presets allow us to mark certain modules as natively supported by a given host environment. For example Cloudflare's workerd natively supports several APIs in
node:buffer
, and the cloudflare preset specifies this module as natively supported:unenv/src/presets/cloudflare.ts
Line 8 in c6dca1d
The issue I see is that workerd supports only some
node:buffer
APIs but not all. Specifically it supportsBuffer
andSlowBuffer
APIs, but notBlob
orFile
, which are part of Node'sbuffer
module.What I'd really like is if I could modify the Cloudflare preset in a way that would create a hybrid module polyfill for
node:buffer
which would provide the natively supported APIs of workerd'snode:module
while, using the unenv mock (or polyfills` for the remaining APIs.I gave this a shot locally, but I wonder if this is actually possible at all with the current way unenv works. The problem I see is that we'd need a way to preserve access to the "native"
node:buffer
while also defining analias
for it. And since all aliases are flattened, I don't see a way how we can have both. Am I missing something? Is there some creative solution to this?Or is the only way to achieve this to modify workerd to alias
node:*
modules via an alternative module prefix (e.g.node-unenv:*
) so that unenv can internally use these modules while overridingnode:*
specifiers using module aliases?Additional information
The text was updated successfully, but these errors were encountered: