-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: expose
globalThis.crypto
when not available (#48941)
### What? Exposing `globalThis.crypto`, based on [Node.js' WebCrypto API](https://nodejs.org/api/globals.html#crypto_1) ### Why? Similar to `fetch`, `crypto` is a popular API that is currently not available on `globalThis` 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](https://vercel.slack.com/archives/C03KAR5DCKC/p1681821510191059)
- Loading branch information
1 parent
0918ebd
commit 88a033f
Showing
11 changed files
with
74 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,6 +315,6 @@ | |
"caniuse-lite": "1.0.30001406" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=16.8.0" | ||
} | ||
} |
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() | ||
}, | ||
}) | ||
} |
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' |
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' |
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 |