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

feat: node middleware #725

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions packages/open-next/src/adapters/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
loadBuildId,
loadConfig,
loadConfigHeaders,
loadFunctionsConfigManifest,
loadHtmlPages,
loadMiddlewareManifest,
loadPrerenderManifest,
Expand Down Expand Up @@ -35,3 +36,6 @@ export const MiddlewareManifest =
export const AppPathsManifest = /* @__PURE__ */ loadAppPathsManifest(NEXT_DIR);
export const AppPathRoutesManifest =
/* @__PURE__ */ loadAppPathRoutesManifest(NEXT_DIR);

export const FunctionsConfigManifest =
/* @__PURE__ */ loadFunctionsConfigManifest(NEXT_DIR);
15 changes: 15 additions & 0 deletions packages/open-next/src/adapters/config/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import type {
FunctionsConfigManifest,
MiddlewareManifest,
NextConfig,
PrerenderManifest,
Expand Down Expand Up @@ -123,3 +124,17 @@ export function loadMiddlewareManifest(nextDir: string) {
const json = fs.readFileSync(filePath, "utf-8");
return JSON.parse(json) as MiddlewareManifest;
}

export function loadFunctionsConfigManifest(nextDir: string) {
const filePath = path.join(
nextDir,
"server",
"functions-config-manifest.json",
);
try {
const json = fs.readFileSync(filePath, "utf-8");
return JSON.parse(json) as FunctionsConfigManifest;
} catch (e) {
return;
}
}
2 changes: 1 addition & 1 deletion packages/open-next/src/build/compileConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function compileOpenNextConfig(
// We need to check if the config uses the edge runtime at any point
// If it does, we need to compile it with the edge runtime
const usesEdgeRuntime =
config.middleware?.external ||
(config.middleware?.external && config.middleware.runtime !== "node") ||
Object.values(config.functions || {}).some((fn) => fn.runtime === "edge");
if (!usesEdgeRuntime) {
logger.debug(
Expand Down
19 changes: 15 additions & 4 deletions packages/open-next/src/build/copyTracedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function copyTracedFiles(
outputDir: string,
routes: string[],
bundledNextServer: boolean,
skipServerFiles = false,
) {
const tsStart = Date.now();
const dotNextDir = path.join(buildOutputPath, ".next");
Expand Down Expand Up @@ -58,10 +59,11 @@ export async function copyTracedFiles(
const filesToCopy = new Map<string, string>();

// Files necessary by the server
extractFiles(requiredServerFiles.files).forEach((f) => {
filesToCopy.set(f, f.replace(standaloneDir, outputDir));
});

if (!skipServerFiles) {
extractFiles(requiredServerFiles.files).forEach((f) => {
filesToCopy.set(f, f.replace(standaloneDir, outputDir));
});
}
// create directory for pages
if (existsSync(path.join(standaloneDir, ".next/server/pages"))) {
mkdirSync(path.join(outputNextDir, "server/pages"), {
Expand Down Expand Up @@ -141,6 +143,15 @@ File ${fullFilePath} does not exist
}
};

if (existsSync(path.join(dotNextDir, "server/middleware.js.nft.json"))) {
// We still need to copy the nft.json file so that computeCopyFilesForPage doesn't throw
copyFileSync(
path.join(dotNextDir, "server/middleware.js.nft.json"),
path.join(standaloneNextDir, "server/middleware.js.nft.json"),
);
computeCopyFilesForPage("middleware");
}

const hasPageDir = routes.some((route) => route.startsWith("pages/"));
const hasAppDir = routes.some((route) => route.startsWith("app/"));

Expand Down
33 changes: 33 additions & 0 deletions packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import fs from "node:fs";
import fsAsync from "node:fs/promises";
import path from "node:path";

import logger from "../logger.js";
import type {
FunctionsConfigManifest,
MiddlewareInfo,
MiddlewareManifest,
} from "../types/next-types.js";
import { buildEdgeBundle } from "./edge/createEdgeBundle.js";
import * as buildHelper from "./helper.js";
import { installDependencies } from "./installDeps.js";
import {
buildBundledNodeMiddleware,
buildExternalNodeMiddleware,
} from "./middleware/buildNodeMiddleware.js";

/**
* Compiles the middleware bundle.
Expand Down Expand Up @@ -36,6 +42,33 @@ export async function createMiddleware(
| MiddlewareInfo
| undefined;

if (!middlewareInfo) {
// If there is no middleware info, it might be a node middleware
const functionsConfigManifestPath = path.join(
appBuildOutputPath,
".next/server/functions-config-manifest.json",
);
const functionsConfigManifest = JSON.parse(
await fsAsync
.readFile(functionsConfigManifestPath, "utf8")
.catch(() => "{}"),
) as FunctionsConfigManifest;

// TODO: Handle external node middleware in the future
if (functionsConfigManifest?.functions["/_middleware"]) {
if (!config.middleware?.external) {
// If we are here, it means that we are using a node middleware
await buildBundledNodeMiddleware(options);
// We return early to not build the edge middleware
return;
}

// Here it means that we are using a node external middleware
await buildExternalNodeMiddleware(options);
return;
}
}

if (config.middleware?.external) {
const outputPath = path.join(outputDir, "middleware");
fs.mkdirSync(outputPath, { recursive: true });
Expand Down
9 changes: 4 additions & 5 deletions packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import type { OriginResolver } from "types/overrides.js";
import logger from "../../logger.js";
import { openNextEdgePlugins } from "../../plugins/edge.js";
import { openNextExternalMiddlewarePlugin } from "../../plugins/externalMiddleware.js";
import { openNextReplacementPlugin } from "../../plugins/replacement.js";
import { openNextResolvePlugin } from "../../plugins/resolve.js";
import { getCrossPlatformPathRegex } from "../../utils/regex.js";
Expand Down Expand Up @@ -88,14 +89,12 @@ export async function buildEdgeBundle({
target: getCrossPlatformPathRegex("adapters/middleware.js"),
deletes: includeCache ? [] : ["includeCacheInMiddleware"],
}),
openNextExternalMiddlewarePlugin(
path.join(options.openNextDistDir, "core", "edgeFunctionHandler.js"),
),
openNextEdgePlugins({
middlewareInfo,
nextDir: path.join(options.appBuildOutputPath, ".next"),
edgeFunctionHandlerPath: path.join(
options.openNextDistDir,
"core",
"edgeFunctionHandler.js",
),
isInCloudfare,
}),
],
Expand Down
138 changes: 138 additions & 0 deletions packages/open-next/src/build/middleware/buildNodeMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import fs from "node:fs";
import path from "node:path";

import type {
IncludedOriginResolver,
LazyLoadedOverride,
OverrideOptions,
} from "types/open-next.js";
import type { OriginResolver } from "types/overrides.js";
import { getCrossPlatformPathRegex } from "utils/regex.js";
import { openNextExternalMiddlewarePlugin } from "../../plugins/externalMiddleware.js";
import { openNextReplacementPlugin } from "../../plugins/replacement.js";
import { openNextResolvePlugin } from "../../plugins/resolve.js";
import { copyTracedFiles } from "../copyTracedFiles.js";
import * as buildHelper from "../helper.js";
import { installDependencies } from "../installDeps.js";

type Override = OverrideOptions & {
originResolver?: LazyLoadedOverride<OriginResolver> | IncludedOriginResolver;
};

export async function buildExternalNodeMiddleware(
options: buildHelper.BuildOptions,
) {
const { appBuildOutputPath, config, outputDir } = options;
if (!config.middleware?.external) {
throw new Error(
"This function should only be called for external middleware",
);
}
const outputPath = path.join(outputDir, "middleware");
fs.mkdirSync(outputPath, { recursive: true });

// Copy open-next.config.mjs
buildHelper.copyOpenNextConfig(
options.buildDir,
outputPath,
await buildHelper.isEdgeRuntime(config.middleware.override),
);
const overrides = {
...config.middleware.override,
originResolver: config.middleware.originResolver,
};
const includeCache = config.dangerous?.enableCacheInterception;
const packagePath = buildHelper.getPackagePath(options);

// TODO: change this so that we don't copy unnecessary files
await copyTracedFiles(
appBuildOutputPath,
packagePath,
outputPath,
[],
false,
true,
);

function override<T extends keyof Override>(target: T) {
return typeof overrides?.[target] === "string"
? overrides[target]
: undefined;
}

// Bundle middleware
await buildHelper.esbuildAsync(
{
entryPoints: [
path.join(options.openNextDistDir, "adapters", "middleware.js"),
],
outfile: path.join(outputPath, "handler.mjs"),
external: ["./.next/*"],
platform: "node",
plugins: [
openNextResolvePlugin({
overrides: {
wrapper: override("wrapper") ?? "aws-lambda",
converter: override("converter") ?? "aws-cloudfront",
...(includeCache
? {
tagCache: override("tagCache") ?? "dynamodb-lite",
incrementalCache: override("incrementalCache") ?? "s3-lite",
queue: override("queue") ?? "sqs-lite",
}
: {}),
originResolver: override("originResolver") ?? "pattern-env",
proxyExternalRequest: override("proxyExternalRequest") ?? "node",
},
fnName: "middleware",
}),
openNextReplacementPlugin({
name: "externalMiddlewareOverrides",
target: getCrossPlatformPathRegex("adapters/middleware.js"),
deletes: includeCache ? [] : ["includeCacheInMiddleware"],
}),
openNextExternalMiddlewarePlugin(
path.join(
options.openNextDistDir,
"core",
"nodeMiddlewareHandler.js",
),
),
],
banner: {
js: [
`globalThis.monorepoPackagePath = "${packagePath}";`,
"import process from 'node:process';",
"import { Buffer } from 'node:buffer';",
"import {AsyncLocalStorage} from 'node:async_hooks';",
"import { createRequire as topLevelCreateRequire } from 'module';",
"const require = topLevelCreateRequire(import.meta.url);",
"import bannerUrl from 'url';",
"const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));",
].join(""),
},
},
options,
);

// Do we need to copy or do something with env file here?

installDependencies(outputPath, config.middleware?.install);
}

export async function buildBundledNodeMiddleware(
options: buildHelper.BuildOptions,
) {
await buildHelper.esbuildAsync(
{
entryPoints: [
path.join(options.openNextDistDir, "core", "nodeMiddlewareHandler.js"),
],
external: ["./.next/*"],
outfile: path.join(options.buildDir, "middleware.mjs"),
bundle: true,
platform: "node",
},
options,
);
}
1 change: 1 addition & 0 deletions packages/open-next/src/core/edgeFunctionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default async function edgeFunctionHandler(
},
},
});
// TODO: use the global waitUntil
await result.waitUntil;
const response = result.response;
return response;
Expand Down
47 changes: 47 additions & 0 deletions packages/open-next/src/core/nodeMiddlewareHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { RequestData } from "types/global";

type EdgeRequest = Omit<RequestData, "page">;

// Do we need Buffer here?
import { Buffer } from "node:buffer";
globalThis.Buffer = Buffer;

// AsyncLocalStorage is needed to be defined globally
import { AsyncLocalStorage } from "node:async_hooks";
globalThis.AsyncLocalStorage = AsyncLocalStorage;

interface NodeMiddleware {
default: (req: {
handler: any;
request: EdgeRequest;
page: "middleware";
}) => Promise<{
response: Response;
waitUntil: Promise<void>;
}>;
middleware: any;
}

let _module: NodeMiddleware | undefined;

export default async function middlewareHandler(
request: EdgeRequest,
): Promise<Response> {
if (!_module) {
// We use await import here so that we are sure that it is loaded after AsyncLocalStorage is defined on globalThis
// TODO: We will probably need to change this at build time when used in a monorepo (or if the location changes)
//@ts-expect-error - This file should be bundled with esbuild
_module = (await import("./.next/server/middleware.js")).default;
}
const adapterFn = _module!.default || _module;
const result = await adapterFn({
handler: _module!.middleware || _module,
request: request,
page: "middleware",
});
// Not sure if we should await it here or defer to the global als
globalThis.__openNextAls
.getStore()
?.pendingPromiseRunner.add(result.waitUntil);
return result.response;
}
12 changes: 10 additions & 2 deletions packages/open-next/src/core/routing/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { ReadableStream } from "node:stream/web";

import { MiddlewareManifest, NextConfig } from "config/index.js";
import {
FunctionsConfigManifest,
MiddlewareManifest,
NextConfig,
} from "config/index.js";
import type { InternalEvent, InternalResult } from "types/open-next.js";
import { emptyReadableStream } from "utils/stream.js";

Expand All @@ -20,8 +24,12 @@ import {
} from "./util.js";

const middlewareManifest = MiddlewareManifest;
const functionsConfigManifest = FunctionsConfigManifest;

const middleMatch = getMiddlewareMatch(middlewareManifest);
const middleMatch = getMiddlewareMatch(
middlewareManifest,
functionsConfigManifest,
);

type MiddlewareEvent = InternalEvent & {
responseHeaders?: Record<string, string | string[]>;
Expand Down
Loading
Loading