Skip to content

Remove the node protocol from the require statement #250

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

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,25 @@ jobs:
for ((i=0; i<retries; i++)); do
bun test && break || echo "Test failed, retrying..."
done

integration-nextjs:
needs: [test, build]
runs-on: ubuntu-latest

env:
REPLICATE_API_TOKEN: ${{ secrets.REPLICATE_API_TOKEN }}

steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v3
with:
name: package-tarball
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20.x
cache: "npm"
- run: |
npm --prefix integration/next install
npm --prefix integration/next install "./${{ needs.build.outputs.tarball-name }}"
npm --prefix integration/next run build
3 changes: 3 additions & 0 deletions integration/next/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package-lock=false
audit=false
fund=false
17 changes: 17 additions & 0 deletions integration/next/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// NOTE: This file currently doesn't do anything other than
// validate that `next build` works as expected. We can
// extend it in future to support actual middleware tests.
import { NextRequest } from "next/server";
import Replicate from "replicate";

// Limit the middleware to paths starting with `/api/`
export const config = {
matcher: "/api/:function*",
};

const replicate = new Replicate();

export function middleware(request: NextRequest) {
const output = replicate.run("foo/bar");
return Response.json({ output }, 200);
}
14 changes: 14 additions & 0 deletions integration/next/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "replicate-next",
Copy link
Contributor

Choose a reason for hiding this comment

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

@aron Thanks for taking the time to add an integration test for Next.js!

"version": "0.0.0",
"private": true,
"scripts": {
"dev": "next",
"build": "rm -rf .next && next build",
"start": "next start"
},
"dependencies": {
"next": "^14.2.3",
"replicate": "../../"
}
}
5 changes: 5 additions & 0 deletions integration/next/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => (
<main>
<h1>Welcome to Next.js</h1>
</main>
)
13 changes: 12 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +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") {
crypto = require("node: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(
Expand Down
Loading