-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d1a9ca0
chore: bump minimum Node.js version
balazsorban44 7e5974c
fix: polyfill `globalThis.crypto`
balazsorban44 f70efc0
add tests
balazsorban44 814904b
expose crypto for build
balazsorban44 ed2c3f7
Merge branch 'canary' into chore/polyfill-crypto
balazsorban44 cabb953
set required package.json lower
balazsorban44 951c3f4
Merge branch 'chore/polyfill-crypto' of https://github.com/vercel/nex…
balazsorban44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,7 +249,7 @@ | |
"@types/react-dom": "18.0.11" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=16.8.0" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Polyfill crypto() in the Node.js environment | ||
|
||
if (!(global as any).crypto) { | ||
function getCryptoImpl() { | ||
return require('node:crypto').webcrypto | ||
} | ||
Object.defineProperty(global, 'crypto', { | ||
get() { | ||
return getCryptoImpl() | ||
}, | ||
}) | ||
timneutkens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
9 changes: 9 additions & 0 deletions
9
test/e2e/app-dir/crypto-globally-available/app/handler/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function GET() { | ||
return new Response( | ||
typeof globalThis.crypto === 'object' | ||
? 'crypto is available' | ||
: 'crypto is not available' | ||
) | ||
} | ||
|
||
export const runtime = 'nodejs' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set explicitly so the test will work indefinitely in the future. Note: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default function Page() { | ||
return ( | ||
<p> | ||
{typeof globalThis.crypto === 'object' | ||
? 'crypto is available' | ||
: 'crypto is not available'} | ||
</p> | ||
) | ||
} | ||
|
||
export const runtime = 'nodejs' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
22 changes: 22 additions & 0 deletions
22
test/e2e/app-dir/crypto-globally-available/crypto-globally-available.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createNextDescribe } from 'e2e-utils' | ||
|
||
createNextDescribe( | ||
'Web Crypto API is available globally', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next }) => { | ||
// Recommended for tests that need a full browser | ||
it('should be available in Server Components', async () => { | ||
const browser = await next.browser('/') | ||
expect(await browser.elementByCss('p').text()).toBe('crypto is available') | ||
}) | ||
|
||
// In case you need to test the response object | ||
it('should be available in Route Handlers', async () => { | ||
const res = await next.fetch('/handler') | ||
const html = await res.text() | ||
expect(html).toContain('crypto is available') | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
experimental: { appDir: true }, | ||
} | ||
|
||
module.exports = nextConfig |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 in15.0.0
https://nodejs.org/dist/latest-v18.x/docs/api/crypto.html#cryptowebcryptoBut we still want to bump this version since
undici
(what we use in App Router) requires16.8.0+
.Let me know if this should be a different PR.