-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
chore: expose globalThis.crypto
when not available
#48941
Conversation
) | ||
} | ||
|
||
export const runtime = 'nodejs' |
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.
Set explicitly so the test will work indefinitely in the future. Note: "edge"
already exposes globalThis.crypto
for us.
) | ||
} | ||
|
||
export const runtime = 'nodejs' |
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.
Failing test suitesCommit: f70efc0
Expand output● Web Crypto API is available globally › should be available in Server Components
● Web Crypto API is available globally › should be available in Route Handlers
Read more about building and testing Next.js in contributing.md. |
…t.js into chore/polyfill-crypto
@@ -249,7 +249,7 @@ | |||
"@types/react-dom": "18.0.11" | |||
}, | |||
"engines": { | |||
"node": ">=16" | |||
"node": ">=16.8.0" |
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.
I realized that webcrypto
was actually added back in 15.0.0
https://nodejs.org/dist/latest-v18.x/docs/api/crypto.html#cryptowebcrypto
But we still want to bump this version since undici
(what we use in App Router) requires 16.8.0+
.
Let me know if this should be a different PR.
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.
Looks good to me, thanks Balazs 💯
We bumped `undici` fetch which has a minimum version of 16.8.0 so we need to make sure `next` and `create-next-app` also have the same minimum version. Since 14.x reaches End-of-Life on [2023-04-30](https://github.com/nodejs/Release), we can drop support for 14 in the next release. See also: - Related to #48870 - Related #48941
Wait this unbreaks a ton of stuff for me thank you so much |
### What? Allow overwriting the `global.crypto` property when polyfilling it. ### Why? #48941 introduced `global.crypto` polyfill. The problem is that if some library (e.g. [xksuid](https://github.com/ValeriaVG/xksuid/blob/main/src/index.node.mjs)) tries to do the same thing, it breaks as `global.crypto` is defined as non-writable[^1]. Arguably libraries should check for `global.crypto` presence before overwriting it BUT I think polyfill should match the actual implementation[^2]. ### How? Make `global.crypto` `enumerable` and `configurable`, as well as define `set` implementation[^3]. [^1]: ![image](https://user-images.githubusercontent.com/7079786/236440322-7bcf1b18-8fcc-4bb9-b9b4-0f2eb032f5ba.png) [^2]: ![image](https://user-images.githubusercontent.com/7079786/236437260-d3abdb0c-134f-4c9d-aab8-de7bf4d7c831.png) [^3]: ![image](https://user-images.githubusercontent.com/7079786/236440393-1c469035-a9f1-4fbe-9ce7-c0308e980510.png) --------- Co-authored-by: Tim Neutkens <[email protected]>
What?
Exposing
globalThis.crypto
, based on Node.js' WebCrypto APIWhy?
Similar to
fetch
,crypto
is a popular API that is currently not available onglobalThis
in all active Node.js versions yet.This can help library authors to create runtime-agnostic packages.
How?
Node.js already has the WebCrypto API that can be imported, we just expose it on
globalThis
in Node.js versions where this is not available.Closes NEXT-1063
Slack thread