From d726f57dfed62742dbc9752f08fdc7057b24b74b Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Thu, 2 May 2024 12:21:22 +0100 Subject: [PATCH 1/4] Remove the node protocol from the require statement This should improve the situation for certain JavaScript bundlers in the ecosystem which do not like the `node:crypto` require statement. --- lib/util.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 22a14c89..99c6e12f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -90,7 +90,9 @@ async function createHMACSHA256(secret, data) { // In Node 18 the `crypto` global is behind a --no-experimental-global-webcrypto flag if (typeof crypto === "undefined" && typeof require === "function") { - crypto = require("node:crypto").webcrypto; + // NOTE: We explicitly do not include the "node:crypto" namespace here + // as this seems to create issues with bundlers like pnpm. + crypto = require("crypto").webcrypto; } const key = await crypto.subtle.importKey( From 346bde6f26533ffcb0e5a89526adb1f636be71d5 Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Thu, 2 May 2024 13:01:40 +0100 Subject: [PATCH 2/4] Update browser tests to mark "crypto" module as external --- integration/browser/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/browser/index.test.js b/integration/browser/index.test.js index 380d8138..b960f3ce 100644 --- a/integration/browser/index.test.js +++ b/integration/browser/index.test.js @@ -6,7 +6,7 @@ const result = await build({ entryPoints: ["index.js"], bundle: true, platform: "browser", - external: ["node:crypto"], + external: ["crypto"], write: false, }); const source = new TextDecoder().decode(result.outputFiles[0].contents); From 7ee0aa5edadd6b636e08eff2945a5af94b2fc647 Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Thu, 2 May 2024 14:10:57 +0100 Subject: [PATCH 3/4] Make indirect request to `require("node:crypto")` --- integration/browser/index.test.js | 2 +- lib/util.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/integration/browser/index.test.js b/integration/browser/index.test.js index b960f3ce..380d8138 100644 --- a/integration/browser/index.test.js +++ b/integration/browser/index.test.js @@ -6,7 +6,7 @@ const result = await build({ entryPoints: ["index.js"], bundle: true, platform: "browser", - external: ["crypto"], + external: ["node:crypto"], write: false, }); const source = new TextDecoder().decode(result.outputFiles[0].contents); diff --git a/lib/util.js b/lib/util.js index 99c6e12f..68b1d9de 100644 --- a/lib/util.js +++ b/lib/util.js @@ -90,9 +90,18 @@ async function createHMACSHA256(secret, data) { // In Node 18 the `crypto` global is behind a --no-experimental-global-webcrypto flag if (typeof crypto === "undefined" && typeof require === "function") { - // NOTE: We explicitly do not include the "node:crypto" namespace here - // as this seems to create issues with bundlers like pnpm. - crypto = require("crypto").webcrypto; + // NOTE: Webpack (primarily as it's used by Next.js) and perhaps some + // other bundlers do not currently support the `node` protocol and will + // error if it's found in the source. Other platforms like CloudFlare + // will only support requires when using the node protocol. + // + // As this line is purely to support Node 18.x we make an indirect request + // to the require function which fools Webpack... + // + // We may be able to remove this in future as it looks like Webpack is getting + // support for requiring using the `node:` protocol. + // See: https://github.com/webpack/webpack/issues/18277 + crypto = require.call(null, "node:crypto").webcrypto; } const key = await crypto.subtle.importKey( From 6ae67a24362267d444228d3aef7333ceafead9d7 Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Thu, 2 May 2024 14:29:09 +0100 Subject: [PATCH 4/4] Add nextjs integration test --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ integration/next/.npmrc | 3 +++ integration/next/middleware.ts | 17 +++++++++++++++++ integration/next/package.json | 14 ++++++++++++++ integration/next/pages/index.js | 5 +++++ 5 files changed, 61 insertions(+) create mode 100644 integration/next/.npmrc create mode 100644 integration/next/middleware.ts create mode 100644 integration/next/package.json create mode 100644 integration/next/pages/index.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed2cab8e..e0a90e49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -171,3 +171,25 @@ jobs: for ((i=0; i ( +
+

Welcome to Next.js

+
+)