Unverified Commit 88a033fa authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

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)
parent 0918ebd6
......@@ -249,7 +249,7 @@
"@types/react-dom": "18.0.11"
},
"engines": {
"node": ">=16"
"node": ">=16.8.0"
},
"packageManager": "pnpm@7.24.3"
}
......@@ -315,6 +315,6 @@
"caniuse-lite": "1.0.30001406"
},
"engines": {
"node": ">=16"
"node": ">=16.8.0"
}
}
......@@ -16,6 +16,7 @@ import type { StaticGenerationAsyncStorage } from '../client/components/static-g
import '../server/require-hook'
import '../server/node-polyfill-fetch'
import '../server/node-polyfill-crypto'
import chalk from 'next/dist/compiled/chalk'
import getGzipSize from 'next/dist/compiled/gzip-size'
import textTable from 'next/dist/compiled/text-table'
......
......@@ -3,6 +3,7 @@ import './require-hook'
import './node-polyfill-fetch'
import './node-polyfill-form'
import './node-polyfill-web-streams'
import './node-polyfill-crypto'
import type { TLSSocket } from 'tls'
import type { Route, RouterOptions } from './router'
......
......@@ -5,6 +5,7 @@ import type { NextConfigComplete } from './config-shared'
import './require-hook'
import './node-polyfill-fetch'
import './node-polyfill-crypto'
import { default as Server } from './next-server'
import * as log from '../build/output/log'
import loadConfig from './config'
......
// 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()
},
})
}
export function GET() {
return new Response(
typeof globalThis.crypto === 'object'
? 'crypto is available'
: 'crypto is not available'
)
}
export const runtime = 'nodejs'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<p>
{typeof globalThis.crypto === 'object'
? 'crypto is available'
: 'crypto is not available'}
</p>
)
}
export const runtime = 'nodejs'
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')
})
}
)
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: { appDir: true },
}
module.exports = nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment