Skip to content

Commit 587d445

Browse files
authored
Remove the node protocol from the require statement (#250)
* 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. * Update browser tests to mark "crypto" module as external * Make indirect request to `require("node:crypto")` * Add nextjs integration test
1 parent a8c4927 commit 587d445

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,25 @@ jobs:
171171
for ((i=0; i<retries; i++)); do
172172
bun test && break || echo "Test failed, retrying..."
173173
done
174+
175+
integration-nextjs:
176+
needs: [test, build]
177+
runs-on: ubuntu-latest
178+
179+
env:
180+
REPLICATE_API_TOKEN: ${{ secrets.REPLICATE_API_TOKEN }}
181+
182+
steps:
183+
- uses: actions/checkout@v4
184+
- uses: actions/download-artifact@v3
185+
with:
186+
name: package-tarball
187+
- name: Use Node.js
188+
uses: actions/setup-node@v4
189+
with:
190+
node-version: 20.x
191+
cache: "npm"
192+
- run: |
193+
npm --prefix integration/next install
194+
npm --prefix integration/next install "./${{ needs.build.outputs.tarball-name }}"
195+
npm --prefix integration/next run build

integration/next/.npmrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package-lock=false
2+
audit=false
3+
fund=false

integration/next/middleware.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// NOTE: This file currently doesn't do anything other than
2+
// validate that `next build` works as expected. We can
3+
// extend it in future to support actual middleware tests.
4+
import { NextRequest } from "next/server";
5+
import Replicate from "replicate";
6+
7+
// Limit the middleware to paths starting with `/api/`
8+
export const config = {
9+
matcher: "/api/:function*",
10+
};
11+
12+
const replicate = new Replicate();
13+
14+
export function middleware(request: NextRequest) {
15+
const output = replicate.run("foo/bar");
16+
return Response.json({ output }, 200);
17+
}

integration/next/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "replicate-next",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next",
7+
"build": "rm -rf .next && next build",
8+
"start": "next start"
9+
},
10+
"dependencies": {
11+
"next": "^14.2.3",
12+
"replicate": "../../"
13+
}
14+
}

integration/next/pages/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default () => (
2+
<main>
3+
<h1>Welcome to Next.js</h1>
4+
</main>
5+
)

lib/util.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ async function createHMACSHA256(secret, data) {
9090

9191
// In Node 18 the `crypto` global is behind a --no-experimental-global-webcrypto flag
9292
if (typeof crypto === "undefined" && typeof require === "function") {
93-
crypto = require("node:crypto").webcrypto;
93+
// NOTE: Webpack (primarily as it's used by Next.js) and perhaps some
94+
// other bundlers do not currently support the `node` protocol and will
95+
// error if it's found in the source. Other platforms like CloudFlare
96+
// will only support requires when using the node protocol.
97+
//
98+
// As this line is purely to support Node 18.x we make an indirect request
99+
// to the require function which fools Webpack...
100+
//
101+
// We may be able to remove this in future as it looks like Webpack is getting
102+
// support for requiring using the `node:` protocol.
103+
// See: https://github.com/webpack/webpack/issues/18277
104+
crypto = require.call(null, "node:crypto").webcrypto;
94105
}
95106

96107
const key = await crypto.subtle.importKey(

0 commit comments

Comments
 (0)