-
Notifications
You must be signed in to change notification settings - Fork 70
Fix #112 #115
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
base: main
Are you sure you want to change the base?
Fix #112 #115
Conversation
…ning cuid2 on Cloudflare Workers Having this initialization in the top level of the ESM module causes cuid2 to fail to be imported at runtime from a Cloudflare Worker, even if using dynamic import and even if using it only inside the fetch handler. CF Workers forbids (https://developers.cloudflare.com/workers/runtime-apis/handlers) usage of crypto randomness sources at the top level of any ESM module, even if evaluation of such top level code happens at a time later than startup.
|
@cursor please /review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR addresses issue #112 by deferring the initialization of createId until first usage, enabling the cuid2 library to run on Cloudflare Workers. Cloudflare Workers prohibit the use of crypto randomness sources at the top level of ESM modules, so the initialization must be lazy.
Key Changes:
- Replaced immediate initialization of
createIdwith a lazy initialization pattern - Added a
lazyhelper function to wrapinit()and defer execution until first use
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (!initialized) { | ||
| initialized = fn(); | ||
| } | ||
| return initialized(); |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lazy initialization has a logic error. The variable initialized stores the result of fn() (which is the cuid2() function), but then tries to call initialized() again on line 141. This creates a double function call where the returned cuid2 function is invoked without arguments, then its return value (a string ID) is invoked as a function, which will cause a runtime error.
The fix should be to return initialized directly without the extra call:
return initialized;
|
@cursoragent please /review - please also validate copilot's comment. |
|
@ericelliott Apparently Cursor will not show up and Copilot's comment is wrong. |
|
Thanks for the heads up. I'll investigate in depth as soon as I have a moment. In the meantime, feel free to use your own fork while I sort this out and push a new release with a fix. I appreciate you! 🙏 |
|
No worries. Do reach out if you need any more detail about what I investigated. |
|
Yes, if you can share how to easily test your solution and ensure that the
AI is wrong about the bug report, that would be useful to help get this
merged!
…On Tue, Dec 16, 2025 at 4:08 PM Ángel Sanz ***@***.***> wrote:
*angelsanzn* left a comment (paralleldrive/cuid2#115)
<#115 (comment)>
No worries. Do reach out if you need any more detail about what I
investigated.
—
Reply to this email directly, view it on GitHub
<#115 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACZBNYQNLZA44SOV57EYRD4CCNI7AVCNFSM6AAAAACOHGOFSSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMNRSHE4DOMBZGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Eric Elliott
|
|
For that, a slightly deeper form of static analysis (done just by looking) should suffice: lazy is only called once, getting the function init as its only arg. This function "lazy" is called, not at the time the ESM module initializes but at a later time. It's called only once, its return value being stored in a closure. The return value of the init function is the createId function, as shown by the previous diff (the red part). So it can be called too. Both cuid2 test scripts pass all the same with or without this patch, I ran them on my machine. And I have successfully tested this patched version against a live CF Workers deployment. |
createId: defer initialization until first usage, to support running cuid2 on Cloudflare Workers. Fixes #112
Having just the initialization for this function in the top level of the cuid2 ESM module causes the whole cuid2 library to fail to be imported at runtime from a Cloudflare Worker, even if using dynamic import and even if using dynamic import only inside the fetch handler. CF Workers forbids usage of crypto randomness sources at the top level of any ESM module, even if evaluation of such top level code happens at a time later than startup.
Also: