Skip to content
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 7 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"@types/react-dom": "18.0.11"
},
"engines": {
"node": ">=16"
"node": ">=16.15.0"
},
"packageManager": "[email protected]"
}
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,6 @@
"caniuse-lite": "1.0.30001406"
},
"engines": {
"node": ">=16"
"node": ">=16.15.0"
}
}
1 change: 1 addition & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/server/node-polyfill-crypto.ts
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
}
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'
Copy link
Member Author

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.

7 changes: 7 additions & 0 deletions test/e2e/app-dir/crypto-globally-available/app/layout.tsx
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>
)
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/crypto-globally-available/app/page.tsx
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'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')
})
}
)
8 changes: 8 additions & 0 deletions test/e2e/app-dir/crypto-globally-available/next.config.js
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