/** * Bundles the Node.js middleware (`proxy.ts` / `middleware.ts` with the `nodejs` runtime) * into a Workers compatible `middleware/handler.mjs`. * * NOTE: Running Next.js Node.js middleware on workerd is experimental and is not supported * by the OpenNext maintainers. It re-bundles the middleware compiled by Next.js, which is an * internal output that can change between Next.js versions. * * `@opennextjs/aws` bundles the external middleware for a Node.js server: * the OpenNext config is read from the filesystem at runtime and the middleware compiled * by Next.js is loaded with `await import("./.next/server/middleware.js")`. * * workerd can not access the filesystem nor load modules at runtime, so the handler * built by `@opennextjs/aws` is replaced with a fully self-contained bundle: * * - the config manifests are inlined by `openNextEdgePlugins` (as for the edge middleware) * - the middleware compiled by Next.js is statically bundled from the traced files that * `@opennextjs/aws` copies to `middleware//.next/server/middleware.js` * * The traced files are resolved, aliased and minified as for the server, see `bundle-server.ts`. */ import { type BuildOptions } from "@opennextjs/aws/build/helper.js"; import { type Plugin } from "esbuild"; import type { ProjectOptions } from "../../project-options.js"; /** * Returns the chunks of the middleware compiled by Next.js, from its trace. * * `@opennextjs/aws` also copies the files traced for the instrumentation hook to the middleware * output. They must not be bundled: the middleware does not load the hook, the server does. * See https://github.com/opennextjs/opennextjs-cloudflare/issues/1401 * * @param tracePath Path to the `.nft.json` trace of the middleware. * @param dotNextServerDir The `.next/server` directory of the middleware output. * @returns The `.js` and `.wasm` files of the trace in `.next/server/chunks`, that exist in the output. * @throws When the trace can not be read. */ export declare function getMiddlewareChunks(tracePath: string, dotNextServerDir: string): string[]; /** * Resolves the middleware compiled by Next.js to the copy created by `copyTracedFiles`. * * `@opennextjs/aws`'s `nodeMiddlewareHandler` loads the middleware with a dynamic * `await import("./.next/server/middleware.js")` that nothing on the aws side resolves (it relies * on the adapter bundling it), so this resolves that specifier to the traced copy. */ export declare function setCompiledMiddlewarePlugin(compiledMiddlewarePath: string): Plugin; /** * Makes the Node.js builtins used by the bundled code (i.e. `require("crypto")` or * `import from "node:crypto"`) resolve to the modules workerd provides via `nodejs_compat`. * * `require` calls are converted into a virtual CommonJS module re-exporting the builtin. * The virtual module has to stay CommonJS - esbuild classifies it as such because it assigns to * `module.exports` and uses no `export` keyword - so that `require("crypto")` receives the module * workerd provides rather than an esbuild `__toCommonJS` wrapper built from its named exports, * which would drop whatever the named exports do not expose and add a synthetic `__esModule`. * * The conversion is kept in sync with `handleRequireCallsToNodeJSBuiltins` in wrangler's * `hybrid-nodejs-compat` esbuild plugin, which is the source of truth: * https://github.com/cloudflare/workers-sdk/blob/c457bfc6b5a575586354f5b0ad7a1100eff915fe/packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts#L102-L133 * * It differs on a single intentional point: the builtin is imported as a namespace and * `mod.default ?? mod` is used rather than a default import, so that a builtin without a `default` * export can not fail to link in workerd. wrangler applies the same fallback to its unenv aliases * in `handleUnenvAliasedPackages`. */ export declare function nodeBuiltinsPlugin(): Plugin; /** * Resolves `@opentelemetry/api` to the copy compiled into Next.js when the real package can not * be resolved. * * `next/dist/server/lib/trace/tracer.js` requires `@opentelemetry/api`, an optional dependency * that most apps do not install. On the edge runtime Next.js does not fall back to its compiled * copy when the require throws, so the fallback is applied here. * * See https://github.com/opennextjs/opennextjs-cloudflare/issues/969 * * @returns The esbuild plugin. */ export declare function opentelemetryFallbackPlugin(): Plugin; /** * Bundles the traced Node.js middleware for Workers. * * @param options Build options. * @param projectOpts Cloudflare project options. * @returns Resolves when the middleware handler has been written. * @throws When the compiled middleware or its trace is missing or preparing or bundling it fails. */ export declare function bundleNodeMiddleware(options: BuildOptions, projectOpts: ProjectOptions): Promise;