unenv icon indicating copy to clipboard operation
unenv copied to clipboard

Improve `node:crypto` polyfills

Open pi0 opened this issue 10 months ago • 9 comments

Describe the feature

Some node:crypto exports in unenv v2 had been improved (src) however many were also unimplemented and some commonly requested, we should investigate implementation (PRs more than welcome)

  • [ ] crypto.createHmac (https://github.com/unjs/unenv/issues/291)
  • [ ] crypto. createHash (https://github.com/unjs/unenv/issues/177)

[!NOTE] For cloudflare users:** Since unenv v2, crypto is implemented as hybrid. Cloudflare might have more native implementations. Both createHash and createHmac are implemented natively in workerd and soon will be available for Nitro and Nuxt in cloudflare preset as they migrate to unenv2.

pi0 avatar Feb 06 '25 19:02 pi0

When I deployed my blog app created with Nuxt3 to NuxtHub, the deployment was successful and the top page was displayed, but when displaying the blog page, an internal error occurred on the /api side. "[unenv] crypto.createHmac is not implemented yet!" It works fine with Azure hosting.

I wonder if this problem has already been reflected in NuxtHub? Sorry, I don't really understand the relationship there.

oasisniikawa avatar Feb 18 '25 02:02 oasisniikawa

@oasisniikawa nodejs compat handling with nuxt hub is different. i suggest you report it there.

pi0 avatar Feb 18 '25 07:02 pi0

Hi @pi0,

Thank you for addressing this issue.

I wanted to let you know that this would be very beneficial for a website I launched today, www.planttattoosai.com. Unfortunately, I wasn't aware that it wasn't compatible with NuxtHub on Cloudflare, so I'll need to find an alternative host or an alternative crypto function.

Additionally, createHmac is the method used to verify webhooks from leading AI model supplier, Replicate. You can find more details here: Replicate Webhook Verification.

I'm happy to test your fix on Cloudflare via NuxtHub once it's ready.

Image

layerzzzio avatar Mar 07 '25 21:03 layerzzzio

Hi @layerzzzio. The latest version of nuxthub/core should leverage nitro 2.11 hybrid polyfills. So if you are still having this issue, would you mind to open an issue in hub repo with reproduction/version and ping me? 🙏🏼 would be happy to help investigating from there.

pi0 avatar Mar 07 '25 21:03 pi0

Thank you for your help.

What I did so far :

  • I updated nuxthub/core to the latest version (^0.8.18) and redeployed, but I'm still encountering the same error. Is this commit intended to utilize the polyfills you mentioned? https://github.com/nuxt-hub/core/commit/0c9eb8b

  • Additionally, I attempted to upgrade to Nuxt 3.16 due to the polyfills in Nitro 2.11.0, which are necessary for edge platforms that have partially implemented Node.js APIs. However, I've faced issues with the latest version of @nuxtjs/i18n (v9.3.0) not working properly in my local environment (this upgrade is necessary for the compatibility between i18n and Nuxt 3.16). Once I resolve this and successfully complete the upgrade, I'll update you on the issue.

Thanks again for your assistance.

layerzzzio avatar Mar 07 '25 23:03 layerzzzio

Hi @pi0 Upgrading to Nuxt 3.16, including Nitro 2.11.0, fixes the error 🎉 Thank you so much for your help. Best

layerzzzio avatar Mar 10 '25 09:03 layerzzzio

Hi @pi0 , Thanks for your kindly tips.

I am trying to deploy my Nuxt app to cloudflare, but encountered problems. I am not very clear about the backgrounds, but seems it's now cloudflare team's turn to solve this problem. I just want to know, where can I track the progress of cloudflare's work? Thank you very much.

TooBug avatar Mar 11 '25 07:03 TooBug

You can check Nitro 2.11 release notes for Enabling Node.js compatibility.

// Nitro
export default defineNitroConfig({
  compatibilityDate: "2025-03-01",
  cloudflare: { nodeCompat: true, deployConfig: true }
});

// Nuxt
export default defineNuxtConfig({
  compatibilityDate: "2025-03-01",
  nitro: { cloudflare: { nodeCompat: true, deployConfig: true } }
});

pi0 avatar Mar 11 '25 07:03 pi0

Hi,

cypto.createHash is still not working on Vercel, even with the latest Nuxt version and using Fluid compute.

[unenv] crypto.createHash is not implemented yet!

Is there a similar option to nodeCompat for Vercel?

tnld avatar Mar 22 '25 09:03 tnld