From a420d9ae4c8ee1e014236384adc2cce62f6f4804 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 31 Oct 2023 15:10:49 +0100
Subject: [PATCH 001/102] created basic config file

---
 .../open-next/src/adapters/types/open-next.ts | 152 ++++++++++++++++++
 packages/open-next/src/build.ts               |  82 ++--------
 packages/open-next/src/index.ts               |  20 +--
 3 files changed, 173 insertions(+), 81 deletions(-)
 create mode 100644 packages/open-next/src/adapters/types/open-next.ts

diff --git a/packages/open-next/src/adapters/types/open-next.ts b/packages/open-next/src/adapters/types/open-next.ts
new file mode 100644
index 000000000..2ff2b6755
--- /dev/null
+++ b/packages/open-next/src/adapters/types/open-next.ts
@@ -0,0 +1,152 @@
+import { InternalEvent } from "../event-mapper";
+
+export interface DangerousOptions {
+  /**
+   * The dynamo db cache is used for revalidateTags and revalidatePath.
+   * @default false
+   */
+  disableDynamoDBCache?: boolean;
+  /**
+   * The incremental cache is used for ISR and SSG.
+   * Disable this only if you use only SSR
+   * @default false
+   */
+  disableIncrementalCache?: boolean;
+}
+
+type LazyLoadedOverride<T> = () => Promise<T>;
+
+type Adapter = {
+  convertFrom: (event: any) => InternalEvent;
+  convertTo: (result: any) => any;
+};
+
+type Handler = <Return>(...args: any[]) => Return;
+
+//TODO: properly type this
+type IncrementalCache = {
+  get(key: string): Promise<any>;
+  set(key: string, value: any): Promise<void>;
+};
+
+type TagCache = {
+  getByTag(tag: string): Promise<string[]>;
+  getByPath(path: string): Promise<string[]>;
+  getLastModified(path: string, lastModified?: number): Promise<number>;
+  writeTags(tags: { tag: string; path: string }): Promise<void>;
+};
+
+type Queue = {
+  send(message: any): Promise<void>;
+};
+
+interface OverrideOptions {
+  /**
+   * This is the main entrypoint of your app.
+   * @default "aws-lambda"
+   */
+  handler?:
+    | "aws-lambda"
+    | "aws-lambda-streaming"
+    | "docker"
+    | LazyLoadedOverride<Handler>;
+
+  /**
+   * This code convert the event to InternalEvent and InternalResult to the expected output.
+   * @default "aws-apigw-v2"
+   */
+  adapter?:
+    | "aws-apigw-v2"
+    | "aws-apigw-v1"
+    | "aws-cloudfront"
+    | "docker"
+    | LazyLoadedOverride<Adapter>;
+
+  /**
+   * Add possibility to override the default s3 cache. Used for fetch cache and html/rsc/json cache.
+   * @default "s3"
+   */
+  incrementalCache?: "s3" | LazyLoadedOverride<IncrementalCache>;
+
+  /**
+   * Add possibility to override the default tag cache. Used for revalidateTags and revalidatePath.
+   * @default "dynamodb"
+   */
+  tagCache?: "dynamodb" | LazyLoadedOverride<TagCache>;
+
+  /**
+   * Add possibility to override the default queue. Used for isr.
+   * @default "sqs"
+   */
+  queue?: "sqs" | LazyLoadedOverride<Queue>;
+}
+
+interface FunctionOptions {
+  /**
+   * TODO: implement edge runtime
+   * @default "node"
+   */
+  runtime?: "node" | "edge";
+  /**
+   * Here you should specify all the routes you want to use.
+   * If not provided, all the routes will be used.
+   * @default []
+   */
+  routes?: string[];
+  /**
+   * Minify the server bundle.
+   * @default false
+   */
+  minify?: boolean;
+  /**
+   * Print debug information.
+   * @default false
+   */
+  debug?: boolean;
+  /**
+   * Enable streaming mode.
+   * @default false
+   */
+  streaming?: boolean;
+  /**
+   * Enable overriding the default lambda.
+   */
+  override?: OverrideOptions;
+}
+
+export interface BuildOptions {
+  functions: {
+    default: Omit<FunctionOptions, "routes">;
+    [key: string]: FunctionOptions;
+  };
+  /**
+   * The command to build the Next.js app.
+   * @default `npm run build`, `yarn build`, or `pnpm build` based on the lock file found in the app's directory or any of its parent directories.
+   * @example
+   * ```ts
+   * build({
+   *   buildCommand: "pnpm custom:build",
+   * });
+   * ```
+   */
+  /**
+   * Dangerous options. This break some functionnality but can be useful in some cases.
+   */
+  dangerous?: DangerousOptions;
+  buildCommand?: string;
+  /**
+   * The path to the target folder of build output from the `buildCommand` option (the path which will contain the `.next` and `.open-next` folders). This path is relative from the current process.cwd().
+   * @default "."
+   */
+  buildOutputPath?: string;
+  /**
+   * The path to the root of the Next.js app's source code. This path is relative from the current process.cwd().
+   * @default "."
+   */
+  appPath?: string;
+  /**
+   * The path to the package.json file of the Next.js app. This path is relative from the current process.cwd().
+   * @default "."
+   */
+  packageJsonPath?: string;
+}
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index a184e56d5..f3521e9cd 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -10,71 +10,11 @@ import {
   buildSync,
 } from "esbuild";
 
+import { BuildOptions, DangerousOptions } from "./adapters/types/open-next.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import openNextPlugin from "./plugin.js";
 
-interface DangerousOptions {
-  /**
-   * The dynamo db cache is used for revalidateTags and revalidatePath.
-   * @default false
-   */
-  disableDynamoDBCache?: boolean;
-  /**
-   * The incremental cache is used for ISR and SSG.
-   * Disable this only if you use only SSR
-   * @default false
-   */
-  disableIncrementalCache?: boolean;
-}
-interface BuildOptions {
-  /**
-   * Minify the server bundle.
-   * @default false
-   */
-  minify?: boolean;
-  /**
-   * Print debug information.
-   * @default false
-   */
-  debug?: boolean;
-  /**
-   * Enable streaming mode.
-   * @default false
-   */
-  streaming?: boolean;
-  /**
-   * The command to build the Next.js app.
-   * @default `npm run build`, `yarn build`, or `pnpm build` based on the lock file found in the app's directory or any of its parent directories.
-   * @example
-   * ```ts
-   * build({
-   *   buildCommand: "pnpm custom:build",
-   * });
-   * ```
-   */
-  /**
-   * Dangerous options. This break some functionnality but can be useful in some cases.
-   */
-  dangerous?: DangerousOptions;
-  buildCommand?: string;
-  /**
-   * The path to the target folder of build output from the `buildCommand` option (the path which will contain the `.next` and `.open-next` folders). This path is relative from the current process.cwd().
-   * @default "."
-   */
-  buildOutputPath?: string;
-  /**
-   * The path to the root of the Next.js app's source code. This path is relative from the current process.cwd().
-   * @default "."
-   */
-  appPath?: string;
-
-  /**
-   * The path to the package.json file. This path is relative from the current process.cwd().
-   */
-  packageJsonPath?: string;
-}
-
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 let options: ReturnType<typeof normalizeOptions>;
@@ -83,7 +23,13 @@ export type PublicFiles = {
   files: string[];
 };
 
-export async function build(opts: BuildOptions = {}) {
+export async function build(
+  opts: BuildOptions = {
+    functions: {
+      default: {},
+    },
+  },
+) {
   const { root: monorepoRoot, packager } = findMonorepoRoot(
     path.join(process.cwd(), opts.appPath || "."),
   );
@@ -141,11 +87,17 @@ function normalizeOptions(opts: BuildOptions, root: string) {
     appPublicPath: path.join(appPath, "public"),
     outputDir,
     tempDir: path.join(outputDir, ".build"),
-    minify: opts.minify ?? Boolean(process.env.OPEN_NEXT_MINIFY) ?? false,
-    debug: opts.debug ?? Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
+    minify:
+      opts.functions.default.minify ??
+      Boolean(process.env.OPEN_NEXT_MINIFY) ??
+      false,
+    debug:
+      opts.functions.default.debug ??
+      Boolean(process.env.OPEN_NEXT_DEBUG) ??
+      false,
     buildCommand: opts.buildCommand,
     dangerous: opts.dangerous,
-    streaming: opts.streaming ?? false,
+    streaming: opts.functions.default.streaming ?? false,
   };
 }
 
diff --git a/packages/open-next/src/index.ts b/packages/open-next/src/index.ts
index eb66a7192..65341fa49 100755
--- a/packages/open-next/src/index.ts
+++ b/packages/open-next/src/index.ts
@@ -8,22 +8,10 @@ if (command !== "build") printHelp();
 const args = parseArgs();
 if (Object.keys(args).includes("--help")) printHelp();
 
-build({
-  buildCommand: args["--build-command"],
-  buildOutputPath: args["--build-output-path"],
-  appPath: args["--app-path"],
-  packageJsonPath: args["--package-json"],
-  minify: Object.keys(args).includes("--minify"),
-  streaming: Object.keys(args).includes("--streaming"),
-  dangerous: {
-    disableDynamoDBCache: Object.keys(args).includes(
-      "--dangerously-disable-dynamodb-cache",
-    ),
-    disableIncrementalCache: Object.keys(args).includes(
-      "--dangerously-disable-incremental-cache",
-    ),
-  },
-});
+//TODO: validate config file
+const config = await import(process.cwd() + "/open-next.config.js");
+
+build(config.default);
 
 function parseArgs() {
   return process.argv.slice(2).reduce(

From c2f29a82de6e60468c6f55d57607292794311ef9 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 2 Nov 2023 17:23:54 +0100
Subject: [PATCH 002/102] basic wrapper and converter implementation

---
 .../src/adapters/http/responseStreaming.ts    | 69 ++++++--------
 .../src/adapters/plugins/routing/util.ts      | 37 ++++++++
 .../adapters/plugins/streaming.replacement.ts |  1 +
 .../open-next/src/adapters/server-adapter.ts  |  6 +-
 .../open-next/src/adapters/types/open-next.ts | 33 ++++---
 .../open-next/src/converters/aws-apigw-v2.ts  | 91 ++++++++++++++++++
 packages/open-next/src/converters/docker.ts   | 33 +++++++
 packages/open-next/src/converters/utils.ts    | 11 +++
 .../open-next/src/core/createMainHandler.ts   | 54 +++++++++++
 packages/open-next/src/core/requestHandler.ts | 94 +++++++++++++++++++
 .../src/wrappers/aws-lambda-streaming.ts      | 51 ++++++++++
 packages/open-next/src/wrappers/aws-lambda.ts | 34 +++++++
 packages/open-next/src/wrappers/docker.ts     | 27 ++++++
 13 files changed, 489 insertions(+), 52 deletions(-)
 create mode 100644 packages/open-next/src/converters/aws-apigw-v2.ts
 create mode 100644 packages/open-next/src/converters/docker.ts
 create mode 100644 packages/open-next/src/converters/utils.ts
 create mode 100644 packages/open-next/src/core/createMainHandler.ts
 create mode 100644 packages/open-next/src/core/requestHandler.ts
 create mode 100644 packages/open-next/src/wrappers/aws-lambda-streaming.ts
 create mode 100644 packages/open-next/src/wrappers/aws-lambda.ts
 create mode 100644 packages/open-next/src/wrappers/docker.ts

diff --git a/packages/open-next/src/adapters/http/responseStreaming.ts b/packages/open-next/src/adapters/http/responseStreaming.ts
index 233b27587..d36937dc2 100644
--- a/packages/open-next/src/adapters/http/responseStreaming.ts
+++ b/packages/open-next/src/adapters/http/responseStreaming.ts
@@ -2,13 +2,26 @@ import http from "node:http";
 import { Socket } from "node:net";
 import zlib from "node:zlib";
 
+import { Writable } from "stream";
+
 import { debug, error } from "../logger.js";
-import type { ResponseStream } from "../types/aws-lambda.js";
+// import type { ResponseStream } from "../types/aws-lambda.js";
 import { parseCookies } from "../util.js";
 import { convertHeader, getString, NO_OP, parseHeaders } from "./util.js";
 
 const HEADERS = Symbol();
 
+export interface ResponseStream extends Writable {
+  writeHeaders(
+    prelude: {
+      statusCode: number;
+      cookies: string[];
+      headers: Record<string, string>;
+    },
+    onFinish: () => void,
+  ): void;
+}
+
 export interface StreamingServerResponseProps {
   method?: string;
   headers?: Record<string, string | string[] | undefined>;
@@ -153,44 +166,24 @@ export class StreamingServerResponse extends http.ServerResponse {
       debug("writeHead", this[HEADERS]);
 
       this._wroteHeader = true;
-      // FIXME: This is extracted from the docker lambda node 18 runtime
-      // https://gist.github.com/conico974/13afd708af20711b97df439b910ceb53#file-index-mjs-L921-L932
-      // We replace their write with ours which are inside a setImmediate
-      // This way it seems to work all the time
-      // I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
-      this.responseStream.setContentType(
-        "application/vnd.awslambda.http-integration-response",
+      this.statusCode = statusCode;
+      this.responseStream.writeHeaders(
+        {
+          statusCode,
+          cookies: this._cookies,
+          headers: this[HEADERS],
+        },
+        () => {
+          if (this._compressed) {
+            const br = zlib.createBrotliCompress({
+              flush: zlib.constants.BROTLI_OPERATION_FLUSH,
+            });
+            br.setMaxListeners(100);
+            br.pipe(this.responseStream);
+            this.responseStream = br as unknown as ResponseStream;
+          }
+        },
       );
-      const prelude = JSON.stringify({
-        statusCode: statusCode as number,
-        cookies: this._cookies,
-        headers: this[HEADERS],
-      });
-
-      // Try to flush the buffer to the client to invoke
-      // the streaming. This does not work 100% of the time.
-      setImmediate(() => {
-        this.responseStream.write("\n\n");
-        this.responseStream.uncork();
-      });
-      setImmediate(() => {
-        this.responseStream.write(prelude);
-      });
-
-      setImmediate(() => {
-        this.responseStream.write(new Uint8Array(8));
-
-        // After headers are written, compress all writes
-        // using Brotli
-        if (this._compressed) {
-          const br = zlib.createBrotliCompress({
-            flush: zlib.constants.BROTLI_OPERATION_FLUSH,
-          });
-          br.setMaxListeners(100);
-          br.pipe(this.responseStream);
-          this.responseStream = br as unknown as ResponseStream;
-        }
-      });
 
       debug("writeHead", this[HEADERS]);
     } catch (e) {
diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index 08b158994..d7610f516 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -3,8 +3,13 @@ import crypto from "crypto";
 import { ServerResponse } from "http";
 
 import { BuildId, HtmlPages } from "../../config/index.js";
+import { InternalEvent } from "../../event-mapper.js";
 import { IncomingMessage } from "../../http/request.js";
 import { ServerlessResponse } from "../../http/response.js";
+import {
+  ResponseStream,
+  StreamingServerResponse,
+} from "../../http/responseStreaming.js";
 import { awsLogger, debug } from "../../logger.js";
 
 declare global {
@@ -236,3 +241,35 @@ export function fixISRHeaders(headers: Record<string, string | undefined>) {
   headers[CommonHeaders.CACHE_CONTROL] =
     "s-maxage=2, stale-while-revalidate=2592000";
 }
+
+export function createServerResponse(
+  internalEvent: InternalEvent,
+  headers: Record<string, string | string[] | undefined>,
+  responseStream?: ResponseStream,
+) {
+  const { method, rawPath } = internalEvent;
+  headers["accept-encoding"] = internalEvent.headers["accept-encoding"];
+  return responseStream
+    ? new StreamingServerResponse({
+        method,
+        headers,
+        responseStream,
+        fixHeaders(headers) {
+          fixCacheHeaderForHtmlPages(rawPath, headers);
+          fixSWRCacheHeader(headers);
+          addOpenNextHeader(headers);
+          fixISRHeaders(headers);
+        },
+        async onEnd(headers) {
+          await revalidateIfRequired(
+            internalEvent.headers.host,
+            rawPath,
+            headers,
+          );
+        },
+      })
+    : new ServerlessResponse({
+        method,
+        headers,
+      });
+}
diff --git a/packages/open-next/src/adapters/plugins/streaming.replacement.ts b/packages/open-next/src/adapters/plugins/streaming.replacement.ts
index 7a3190247..c435b51a8 100644
--- a/packages/open-next/src/adapters/plugins/streaming.replacement.ts
+++ b/packages/open-next/src/adapters/plugins/streaming.replacement.ts
@@ -57,6 +57,7 @@ export const lambdaHandler = awslambda.streamifyResponse(async function (
     return new StreamingServerResponse({
       method,
       headers,
+      //@ts-ignore - we will remove this file
       responseStream,
       // We need to fix the cache header before sending any response
       fixHeaders: (headers) => {
diff --git a/packages/open-next/src/adapters/server-adapter.ts b/packages/open-next/src/adapters/server-adapter.ts
index 6c26cf541..23ea34bb5 100644
--- a/packages/open-next/src/adapters/server-adapter.ts
+++ b/packages/open-next/src/adapters/server-adapter.ts
@@ -1,9 +1,11 @@
 import { DynamoDBClient, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
 import { S3Client, S3ClientConfig } from "@aws-sdk/client-s3";
 
+import { createMainHandler } from "../core/createMainHandler.js";
+// We load every config here so that they are only loaded once
+// and during cold starts
 import { BuildId } from "./config/index.js";
 import { awsLogger } from "./logger.js";
-import { lambdaHandler } from "./plugins/lambdaHandler.js";
 import { setNodeEnv } from "./util.js";
 
 // We load every config here so that they are only loaded once
@@ -62,7 +64,7 @@ globalThis.dynamoClient = new DynamoDBClient(parseDynamoClientConfigFromEnv());
 // Handler //
 /////////////
 
-export const handler = lambdaHandler;
+export const handler = await createMainHandler();
 
 //////////////////////
 // Helper functions //
diff --git a/packages/open-next/src/adapters/types/open-next.ts b/packages/open-next/src/adapters/types/open-next.ts
index 2ff2b6755..46096ca1a 100644
--- a/packages/open-next/src/adapters/types/open-next.ts
+++ b/packages/open-next/src/adapters/types/open-next.ts
@@ -1,4 +1,5 @@
-import { InternalEvent } from "../event-mapper";
+import { InternalEvent, InternalResult } from "../event-mapper";
+import { ResponseStream } from "../http";
 
 export interface DangerousOptions {
   /**
@@ -14,53 +15,61 @@ export interface DangerousOptions {
   disableIncrementalCache?: boolean;
 }
 
-type LazyLoadedOverride<T> = () => Promise<T>;
+export type LazyLoadedOverride<T> = () => Promise<T>;
 
-type Adapter = {
+export type OpenNextHandler = (
+  event: InternalEvent,
+  responseStream?: ResponseStream,
+) => Promise<InternalResult | void>;
+
+export type Converter = {
   convertFrom: (event: any) => InternalEvent;
   convertTo: (result: any) => any;
 };
 
-type Handler = <Return>(...args: any[]) => Return;
+export type Wrapper = (
+  handler: OpenNextHandler,
+  converter: Converter,
+) => Promise<(...args: any[]) => any>;
 
 //TODO: properly type this
-type IncrementalCache = {
+export type IncrementalCache = {
   get(key: string): Promise<any>;
   set(key: string, value: any): Promise<void>;
 };
 
-type TagCache = {
+export type TagCache = {
   getByTag(tag: string): Promise<string[]>;
   getByPath(path: string): Promise<string[]>;
   getLastModified(path: string, lastModified?: number): Promise<number>;
   writeTags(tags: { tag: string; path: string }): Promise<void>;
 };
 
-type Queue = {
+export type Queue = {
   send(message: any): Promise<void>;
 };
 
-interface OverrideOptions {
+export interface OverrideOptions {
   /**
    * This is the main entrypoint of your app.
    * @default "aws-lambda"
    */
-  handler?:
+  wrapper?:
     | "aws-lambda"
     | "aws-lambda-streaming"
     | "docker"
-    | LazyLoadedOverride<Handler>;
+    | LazyLoadedOverride<Wrapper>;
 
   /**
    * This code convert the event to InternalEvent and InternalResult to the expected output.
    * @default "aws-apigw-v2"
    */
-  adapter?:
+  converter?:
     | "aws-apigw-v2"
     | "aws-apigw-v1"
     | "aws-cloudfront"
     | "docker"
-    | LazyLoadedOverride<Adapter>;
+    | LazyLoadedOverride<Converter>;
 
   /**
    * Add possibility to override the default s3 cache. Used for fetch cache and html/rsc/json cache.
diff --git a/packages/open-next/src/converters/aws-apigw-v2.ts b/packages/open-next/src/converters/aws-apigw-v2.ts
new file mode 100644
index 000000000..65e014e1f
--- /dev/null
+++ b/packages/open-next/src/converters/aws-apigw-v2.ts
@@ -0,0 +1,91 @@
+import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda";
+
+import { InternalEvent, InternalResult } from "../adapters/event-mapper";
+import { debug } from "../adapters/logger";
+import { Converter } from "../adapters/types/open-next";
+import { parseCookies } from "../adapters/util";
+import { removeUndefinedFromQuery } from "./utils";
+
+function normalizeAPIGatewayProxyEventV2Body(
+  event: APIGatewayProxyEventV2,
+): Buffer {
+  const { body, isBase64Encoded } = event;
+  if (Buffer.isBuffer(body)) {
+    return body;
+  } else if (typeof body === "string") {
+    return Buffer.from(body, isBase64Encoded ? "base64" : "utf8");
+  } else if (typeof body === "object") {
+    return Buffer.from(JSON.stringify(body));
+  } else {
+    return Buffer.from("", "utf8");
+  }
+}
+
+function normalizeAPIGatewayProxyEventV2Headers(
+  event: APIGatewayProxyEventV2,
+): Record<string, string> {
+  const { headers: rawHeaders, cookies } = event;
+
+  const headers: Record<string, string> = {};
+
+  if (Array.isArray(cookies)) {
+    headers["cookie"] = cookies.join("; ");
+  }
+
+  for (const [key, value] of Object.entries(rawHeaders || {})) {
+    headers[key.toLowerCase()] = value!;
+  }
+
+  return headers;
+}
+
+function convertFromAPIGatewayProxyEventV2(
+  event: APIGatewayProxyEventV2,
+): InternalEvent {
+  const { rawPath, rawQueryString, requestContext } = event;
+  return {
+    type: "v2",
+    method: requestContext.http.method,
+    rawPath,
+    url: rawPath + (rawQueryString ? `?${rawQueryString}` : ""),
+    body: normalizeAPIGatewayProxyEventV2Body(event),
+    headers: normalizeAPIGatewayProxyEventV2Headers(event),
+    remoteAddress: requestContext.http.sourceIp,
+    query: removeUndefinedFromQuery(event.queryStringParameters ?? {}),
+    cookies:
+      event.cookies?.reduce((acc, cur) => {
+        const [key, value] = cur.split("=");
+        return { ...acc, [key]: value };
+      }, {}) ?? {},
+  };
+}
+
+function convertToApiGatewayProxyResultV2(
+  result: InternalResult,
+): APIGatewayProxyResultV2 {
+  const headers: Record<string, string> = {};
+  Object.entries(result.headers)
+    .filter(([key]) => key.toLowerCase() !== "set-cookie")
+    .forEach(([key, value]) => {
+      if (value === null) {
+        headers[key] = "";
+        return;
+      }
+      headers[key] = Array.isArray(value) ? value.join(", ") : value.toString();
+    });
+
+  const response: APIGatewayProxyResultV2 = {
+    statusCode: result.statusCode,
+    headers,
+    cookies: parseCookies(result.headers["set-cookie"]),
+    body: result.body,
+    isBase64Encoded: result.isBase64Encoded,
+  };
+  debug(response);
+  return response;
+}
+
+export default {
+  convertFrom: convertFromAPIGatewayProxyEventV2,
+  convertTo: convertToApiGatewayProxyResultV2,
+} as Converter;
diff --git a/packages/open-next/src/converters/docker.ts b/packages/open-next/src/converters/docker.ts
new file mode 100644
index 000000000..32782445b
--- /dev/null
+++ b/packages/open-next/src/converters/docker.ts
@@ -0,0 +1,33 @@
+import { IncomingMessage } from "http";
+
+import { InternalResult } from "../adapters/event-mapper";
+import { Converter } from "../adapters/types/open-next";
+
+const converter: Converter = {
+  convertFrom: (req: IncomingMessage) => ({
+    type: "v2",
+    method: req.method ?? "GET",
+    rawPath: req.url!,
+    url: req.url!,
+    body: Buffer.from(""),
+    headers: Object.fromEntries(
+      Object.entries(req.headers ?? {})
+        .map(([key, value]) => [
+          key.toLowerCase(),
+          Array.isArray(value) ? value.join(",") : value,
+        ])
+        .filter(([key]) => key),
+    ),
+    remoteAddress: "",
+    query: {},
+    cookies: {},
+  }),
+  // Do nothing here, it's streaming
+  convertTo: (internalResult: InternalResult) => ({
+    body: internalResult.body,
+    headers: internalResult.headers,
+    statusCode: internalResult.statusCode,
+  }),
+};
+
+export default converter;
diff --git a/packages/open-next/src/converters/utils.ts b/packages/open-next/src/converters/utils.ts
new file mode 100644
index 000000000..13b7e8102
--- /dev/null
+++ b/packages/open-next/src/converters/utils.ts
@@ -0,0 +1,11 @@
+export function removeUndefinedFromQuery(
+  query: Record<string, string | string[] | undefined>,
+) {
+  const newQuery: Record<string, string | string[]> = {};
+  for (const [key, value] of Object.entries(query)) {
+    if (value !== undefined) {
+      newQuery[key] = value;
+    }
+  }
+  return newQuery;
+}
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
new file mode 100644
index 000000000..d64ad7385
--- /dev/null
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -0,0 +1,54 @@
+import {
+  BuildOptions,
+  Converter,
+  OverrideOptions,
+  Wrapper,
+} from "../adapters/types/open-next";
+import { openNextHandler } from "./requestHandler";
+
+async function resolveConverter(
+  converter: OverrideOptions["converter"],
+): Promise<Converter> {
+  if (typeof converter === "string") {
+    const m = await import(`../converters/${converter}.js`);
+    return m.default;
+  } else if (typeof converter === "function") {
+    return converter();
+  } else {
+    const m_1 = await import("../converters/aws-apigw-v2.js");
+    return m_1.default;
+  }
+}
+
+async function resolveWrapper(
+  wrapper: OverrideOptions["wrapper"],
+): Promise<Wrapper> {
+  if (typeof wrapper === "string") {
+    const m = await import(`../wrappers/${wrapper}.js`);
+    return m.default;
+  } else if (typeof wrapper === "function") {
+    return wrapper();
+  } else {
+    const m_1 = await import("../wrappers/aws-lambda.js");
+    return m_1.default;
+  }
+}
+
+export async function createMainHandler() {
+  //First we load the config
+  const config: BuildOptions = await import(
+    process.cwd() + "/open-next.config.js"
+  ).then((m) => m.default);
+
+  // From the config, we create the adapter
+  const adapter = await resolveConverter(
+    config.functions.default.override?.converter,
+  );
+
+  // Then we create the handler
+  const wrapper = await resolveWrapper(
+    config.functions.default.override?.wrapper,
+  );
+
+  return wrapper(openNextHandler, adapter);
+}
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
new file mode 100644
index 000000000..4e8087f91
--- /dev/null
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -0,0 +1,94 @@
+import { BuildId } from "../adapters/config";
+import { InternalEvent, InternalResult } from "../adapters/event-mapper";
+import {
+  IncomingMessage,
+  ResponseStream,
+  ServerlessResponse,
+} from "../adapters/http";
+import { error } from "../adapters/logger";
+import {
+  postProcessResponse,
+  processInternalEvent,
+} from "../adapters/plugins/routing/default";
+import { createServerResponse } from "../adapters/plugins/routing/util";
+import { handler as serverHandler } from "../adapters/plugins/serverHandler";
+
+export async function openNextHandler(
+  internalEvent: InternalEvent,
+  responseStreaming?: ResponseStream,
+): Promise<InternalResult | void> {
+  if (internalEvent.headers["x-forwarded-host"]) {
+    internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
+  }
+  const preprocessResult = await processInternalEvent(
+    internalEvent,
+    (method, headers) =>
+      createServerResponse(
+        internalEvent,
+        {
+          ...headers,
+          "accept-encoding": internalEvent.headers["accept-encoding"],
+        },
+        responseStreaming,
+      ),
+  );
+
+  if ("type" in preprocessResult) {
+    return preprocessResult;
+  } else {
+    const {
+      req,
+      res,
+      isExternalRewrite,
+      internalEvent: overwrittenInternalEvent,
+    } = preprocessResult;
+
+    // @ts-ignore
+    await processRequest(req, res, overwrittenInternalEvent, isExternalRewrite);
+
+    const internalResult = await postProcessResponse({
+      internalEvent: overwrittenInternalEvent,
+      req,
+      res,
+      isExternalRewrite,
+    });
+
+    return internalResult;
+  }
+}
+
+async function processRequest(
+  req: IncomingMessage,
+  res: ServerlessResponse,
+  internalEvent: InternalEvent,
+  isExternalRewrite?: boolean,
+) {
+  // @ts-ignore
+  // Next.js doesn't parse body if the property exists
+  // https://github.com/dougmoscrop/serverless-http/issues/227
+  delete req.body;
+
+  try {
+    // `serverHandler` is replaced at build time depending on user's
+    // nextjs version to patch Nextjs 13.4.x and future breaking changes.
+    await serverHandler(req, res, {
+      internalEvent,
+      buildId: BuildId,
+      isExternalRewrite,
+    });
+  } catch (e: any) {
+    error("NextJS request failed.", e);
+
+    res.setHeader("Content-Type", "application/json");
+    res.end(
+      JSON.stringify(
+        {
+          message: "Server failed to respond.",
+          details: e,
+        },
+        null,
+        2,
+      ),
+    );
+  }
+}
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
new file mode 100644
index 000000000..974430591
--- /dev/null
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -0,0 +1,51 @@
+import { APIGatewayProxyEventV2 } from "aws-lambda";
+
+import { ResponseStream } from "../adapters/http";
+import { Wrapper } from "../adapters/types/open-next";
+import { WarmerEvent } from "../adapters/warmer-function";
+
+type AwsLambdaEvent = APIGatewayProxyEventV2 | WarmerEvent;
+
+type AwsLambdaReturn = void;
+const handler: Wrapper = async (handler, converter) =>
+  awslambda.streamifyResponse(
+    async (event: AwsLambdaEvent, responseStream): Promise<AwsLambdaReturn> => {
+      const internalEvent = converter.convertFrom(event);
+
+      const res = responseStream as any as ResponseStream;
+
+      res["writeHeaders"] = (_prelude, onFinish) => {
+        // FIXME: This is extracted from the docker lambda node 18 runtime
+        // https://gist.github.com/conico974/13afd708af20711b97df439b910ceb53#file-index-mjs-L921-L932
+        // We replace their write with ours which are inside a setImmediate
+        // This way it seems to work all the time
+        // I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
+        responseStream.setContentType(
+          "application/vnd.awslambda.http-integration-response",
+        );
+        const prelude = JSON.stringify(_prelude);
+
+        // Try to flush the buffer to the client to invoke
+        // the streaming. This does not work 100% of the time.
+        setImmediate(() => {
+          responseStream.write("\n\n");
+          responseStream.uncork();
+        });
+        setImmediate(() => {
+          responseStream.write(prelude);
+        });
+
+        setImmediate(() => {
+          responseStream.write(new Uint8Array(8));
+
+          onFinish();
+        });
+      };
+
+      const response = await handler(internalEvent);
+
+      return converter.convertTo(response);
+    },
+  );
+
+export default handler;
diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
new file mode 100644
index 000000000..869bde44f
--- /dev/null
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -0,0 +1,34 @@
+import {
+  APIGatewayProxyEvent,
+  APIGatewayProxyEventV2,
+  APIGatewayProxyResult,
+  APIGatewayProxyResultV2,
+  CloudFrontRequestEvent,
+  CloudFrontRequestResult,
+} from "aws-lambda";
+
+import { Wrapper } from "../adapters/types/open-next";
+import { WarmerEvent } from "../adapters/warmer-function";
+
+type AwsLambdaEvent =
+  | APIGatewayProxyEventV2
+  | CloudFrontRequestEvent
+  | APIGatewayProxyEvent
+  | WarmerEvent;
+
+type AwsLambdaReturn =
+  | APIGatewayProxyResultV2
+  | APIGatewayProxyResult
+  | CloudFrontRequestResult;
+
+const handler: Wrapper =
+  async (handler, converter) =>
+  async (event: AwsLambdaEvent): Promise<AwsLambdaReturn> => {
+    const internalEvent = converter.convertFrom(event);
+
+    const response = await handler(internalEvent);
+
+    return converter.convertTo(response);
+  };
+
+export default handler;
diff --git a/packages/open-next/src/wrappers/docker.ts b/packages/open-next/src/wrappers/docker.ts
new file mode 100644
index 000000000..2549f0e55
--- /dev/null
+++ b/packages/open-next/src/wrappers/docker.ts
@@ -0,0 +1,27 @@
+import { createServer } from "http";
+
+import { ResponseStream } from "../adapters/http";
+import { Wrapper } from "../adapters/types/open-next";
+
+const wrapper: Wrapper = async (handler, converter) => {
+  const server = createServer(async (req, res) => {
+    const internalEvent = converter.convertFrom(req);
+    const _res = res as any as ResponseStream;
+
+    _res["writeHeaders"] = (prelude, onFinish) => {
+      res.writeHead(prelude.statusCode, prelude.headers);
+      res.uncork();
+      onFinish();
+    };
+
+    await handler(internalEvent, _res);
+  });
+
+  server.listen(3000);
+
+  return () => {
+    server.close();
+  };
+};
+
+export default wrapper;

From 33794593d2c974514596167da51642d2c3398abd Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 3 Nov 2023 13:19:29 +0100
Subject: [PATCH 003/102] Minimal response writable

---
 .../src/adapters/http/openNextResponse.ts     | 127 ++++++++++++++++
 .../src/adapters/plugins/lambdaHandler.ts     | 139 ------------------
 .../plugins/routing/default.replacement.ts    |   5 +-
 .../src/adapters/plugins/routing/default.ts   |   9 +-
 .../src/adapters/plugins/routing/util.ts      |  67 ++++-----
 .../adapters/plugins/streaming.replacement.ts | 105 -------------
 .../src/adapters/routing/middleware.ts        |  10 +-
 .../open-next/src/adapters/routing/util.ts    |   9 +-
 .../open-next/src/adapters/types/plugin.ts    |  13 +-
 packages/open-next/src/build.ts               |  20 +--
 packages/open-next/src/core/requestHandler.ts |   7 +
 11 files changed, 194 insertions(+), 317 deletions(-)
 create mode 100644 packages/open-next/src/adapters/http/openNextResponse.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/lambdaHandler.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/streaming.replacement.ts

diff --git a/packages/open-next/src/adapters/http/openNextResponse.ts b/packages/open-next/src/adapters/http/openNextResponse.ts
new file mode 100644
index 000000000..f00439060
--- /dev/null
+++ b/packages/open-next/src/adapters/http/openNextResponse.ts
@@ -0,0 +1,127 @@
+import { OutgoingHttpHeader, OutgoingHttpHeaders } from "http";
+import { Writable } from "stream";
+
+import { ResponseStream } from "./responseStreaming";
+import { parseHeaders } from "./util";
+
+// We only need to implement the methods that are used by next.js
+export class OpenNextNodeResponse extends Writable {
+  statusCode: number | undefined;
+  statusMessage: string | undefined;
+  headers: OutgoingHttpHeaders = {};
+  headersSent: boolean = false;
+  _chunks: Buffer[] = [];
+
+  constructor(
+    private fixHeaders: (headers: OutgoingHttpHeaders) => void,
+    private onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
+    private responseStream?: ResponseStream,
+    private initialHeaders?: OutgoingHttpHeaders,
+  ) {
+    super();
+  }
+
+  get finished() {
+    return this.writableFinished && this.responseStream?.writableFinished;
+  }
+
+  setHeader(name: string, value: string | string[]): this {
+    this.headers[name.toLowerCase()] = value;
+    return this;
+  }
+
+  removeHeader(name: string): this {
+    delete this.headers[name.toLowerCase()];
+    return this;
+  }
+
+  hasHeader(name: string): boolean {
+    return this.headers[name.toLowerCase()] !== undefined;
+  }
+
+  getHeaders(): OutgoingHttpHeaders {
+    return this.headers;
+  }
+
+  getHeader(name: string): OutgoingHttpHeader | undefined {
+    return this.headers[name.toLowerCase()];
+  }
+
+  // Only used directly in next@14+
+  flushHeaders() {
+    this.headersSent = true;
+    if (this.initialHeaders) {
+      this.headers = { ...this.headers, ...this.initialHeaders };
+    }
+    this.fixHeaders(this.headers);
+    this.responseStream?.writeHeaders(
+      {
+        statusCode: this.statusCode ?? 200,
+        cookies: [],
+        headers: parseHeaders(this.headers),
+      },
+      () => {},
+    );
+  }
+
+  // Might be used in next page api routes
+  writeHead(statusCode: number, headers?: OutgoingHttpHeaders): this {
+    this.statusCode = statusCode;
+    if (headers) {
+      this.headers = headers;
+    }
+    this.flushHeaders();
+    return this;
+  }
+
+  get body() {
+    return Buffer.concat(this._chunks);
+  }
+
+  private _internalWrite(chunk: any, encoding: BufferEncoding) {
+    this._chunks.push(Buffer.from(chunk, encoding));
+    return this.responseStream?.write(chunk, encoding);
+  }
+
+  _write(
+    chunk: any,
+    encoding: BufferEncoding,
+    callback: (error?: Error | null | undefined) => void,
+  ): void {
+    if (!this.headersSent) {
+      this.flushHeaders();
+    }
+    this._internalWrite(chunk, encoding);
+    callback();
+  }
+
+  end(cb?: (() => void) | undefined): this;
+  end(chunk: any, cb?: (() => void) | undefined): this;
+  end(
+    chunk: any,
+    encoding: BufferEncoding,
+    cb?: (() => void) | undefined,
+  ): this;
+  end(chunk?: unknown, encoding?: unknown, cb?: unknown): this {
+    this.onEnd(parseHeaders(this.headers));
+    if (!this.headersSent) {
+      this.flushHeaders();
+    }
+    if (!chunk) {
+      this.responseStream?.end();
+      return this;
+    }
+    if (typeof chunk === "function") {
+      chunk();
+    } else if (typeof encoding === "function") {
+      this._internalWrite(chunk, "utf8");
+      encoding();
+    } else {
+      this._internalWrite(chunk, encoding as BufferEncoding);
+      //@ts-expect-error - typescript doesn't infer that cb is a function
+      cb?.();
+    }
+    this.responseStream?.end();
+    return this;
+  }
+}
diff --git a/packages/open-next/src/adapters/plugins/lambdaHandler.ts b/packages/open-next/src/adapters/plugins/lambdaHandler.ts
deleted file mode 100644
index 7d7a614e9..000000000
--- a/packages/open-next/src/adapters/plugins/lambdaHandler.ts
+++ /dev/null
@@ -1,139 +0,0 @@
-import {
-  APIGatewayProxyEvent,
-  APIGatewayProxyEventV2,
-  CloudFrontRequestEvent,
-} from "aws-lambda";
-
-import { BuildId, PublicAssets } from "../config";
-import { convertFrom, convertTo, InternalEvent } from "../event-mapper";
-import { type IncomingMessage, ServerlessResponse } from "../http";
-import { debug, error } from "../logger";
-import { CreateResponse } from "../types/plugin";
-import { generateUniqueId } from "../util";
-import { WarmerEvent, WarmerResponse } from "../warmer-function";
-//#override imports
-import {
-  postProcessResponse,
-  processInternalEvent,
-} from "./routing/default.js";
-//#endOverride
-import { handler as serverHandler } from "./serverHandler";
-
-const serverId = `server-${generateUniqueId()}`;
-
-//#override lambdaHandler
-export async function lambdaHandler(
-  event:
-    | APIGatewayProxyEventV2
-    | CloudFrontRequestEvent
-    | APIGatewayProxyEvent
-    | WarmerEvent,
-) {
-  debug("event", event);
-  // Handler warmer
-  if ("type" in event) {
-    return formatWarmerResponse(event);
-  }
-
-  // Parse Lambda event and create Next.js request
-  const internalEvent = convertFrom(event);
-
-  // WORKAROUND: Set `x-forwarded-host` header (AWS specific) — https://github.com/serverless-stack/open-next#workaround-set-x-forwarded-host-header-aws-specific
-  if (internalEvent.headers["x-forwarded-host"]) {
-    internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
-  }
-
-  // WORKAROUND: public/ static files served by the server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-public-static-files-served-by-the-server-function-aws-specific
-  // TODO: This is no longer required if each top-level file and folder in "/public"
-  //       is handled by a separate cache behavior. Leaving here for backward compatibility.
-  //       Remove this on next major release.
-  if (PublicAssets.files.includes(internalEvent.rawPath)) {
-    return internalEvent.type === "cf"
-      ? formatCloudFrontFailoverResponse(event as CloudFrontRequestEvent)
-      : formatAPIGatewayFailoverResponse();
-  }
-
-  const createServerResponse: CreateResponse<ServerlessResponse> = (
-    method,
-    headers,
-  ) => new ServerlessResponse({ method, headers });
-
-  const preprocessResult = await processInternalEvent(
-    internalEvent,
-    createServerResponse,
-  );
-  if ("type" in preprocessResult) {
-    return convertTo(preprocessResult);
-  } else {
-    const {
-      req,
-      res,
-      isExternalRewrite,
-      internalEvent: overwrittenInternalEvent,
-    } = preprocessResult;
-
-    await processRequest(req, res, overwrittenInternalEvent, isExternalRewrite);
-
-    const internalResult = await postProcessResponse({
-      internalEvent: overwrittenInternalEvent,
-      req,
-      res,
-      isExternalRewrite,
-    });
-
-    return convertTo(internalResult);
-  }
-}
-//#endOverride
-
-async function processRequest(
-  req: IncomingMessage,
-  res: ServerlessResponse,
-  internalEvent: InternalEvent,
-  isExternalRewrite?: boolean,
-) {
-  // @ts-ignore
-  // Next.js doesn't parse body if the property exists
-  // https://github.com/dougmoscrop/serverless-http/issues/227
-  delete req.body;
-
-  try {
-    // `serverHandler` is replaced at build time depending on user's
-    // nextjs version to patch Nextjs 13.4.x and future breaking changes.
-    await serverHandler(req, res, {
-      internalEvent,
-      buildId: BuildId,
-      isExternalRewrite,
-    });
-  } catch (e: any) {
-    error("NextJS request failed.", e);
-
-    res.setHeader("Content-Type", "application/json");
-    res.end(
-      JSON.stringify(
-        {
-          message: "Server failed to respond.",
-          details: e,
-        },
-        null,
-        2,
-      ),
-    );
-  }
-}
-
-function formatAPIGatewayFailoverResponse() {
-  return { statusCode: 503 };
-}
-
-function formatCloudFrontFailoverResponse(event: CloudFrontRequestEvent) {
-  return event.Records[0].cf.request;
-}
-
-function formatWarmerResponse(event: WarmerEvent) {
-  return new Promise<WarmerResponse>((resolve) => {
-    setTimeout(() => {
-      resolve({ serverId } satisfies WarmerResponse);
-    }, event.delay);
-  });
-}
diff --git a/packages/open-next/src/adapters/plugins/routing/default.replacement.ts b/packages/open-next/src/adapters/plugins/routing/default.replacement.ts
index 3388d1f08..a15e21473 100644
--- a/packages/open-next/src/adapters/plugins/routing/default.replacement.ts
+++ b/packages/open-next/src/adapters/plugins/routing/default.replacement.ts
@@ -24,7 +24,6 @@ import {
 } from "./util";
 import { convertRes } from "../../routing/util";
 import { handleMiddleware } from "../../routing/middleware";
-import { ServerlessResponse } from "../../http";
 import {
   BuildId,
   ConfigHeaders,
@@ -126,9 +125,7 @@ export async function postProcessResponse({
   res,
   isExternalRewrite,
 }: PostProcessOptions): Promise<InternalResult> {
-  const { statusCode, headers, isBase64Encoded, body } = convertRes(
-    res as ServerlessResponse,
-  );
+  const { statusCode, headers, isBase64Encoded, body } = convertRes(res);
 
   debug("ServerResponse data", { statusCode, headers, isBase64Encoded, body });
 
diff --git a/packages/open-next/src/adapters/plugins/routing/default.ts b/packages/open-next/src/adapters/plugins/routing/default.ts
index 5f2694dad..1b1c9e65e 100644
--- a/packages/open-next/src/adapters/plugins/routing/default.ts
+++ b/packages/open-next/src/adapters/plugins/routing/default.ts
@@ -16,12 +16,13 @@ import {
   revalidateIfRequired,
 } from "./util";
 import { convertRes } from "../../routing/util";
-import { ServerlessResponse } from "../../http";
-import { ServerResponse } from "http";
+import { OpenNextNodeResponse } from "../../http/openNextResponse";
 //#endOverride
 
 //#override processInternalEvent
-export async function processInternalEvent<Response extends ServerResponse>(
+export async function processInternalEvent<
+  Response extends OpenNextNodeResponse,
+>(
   internalEvent: InternalEvent,
   createResponse: CreateResponse<Response>,
 ): Promise<ProcessInternalEventResult<Response>> {
@@ -51,7 +52,7 @@ export async function postProcessResponse({
   isExternalRewrite,
 }: PostProcessOptions): Promise<InternalResult> {
   const { statusCode, headers, isBase64Encoded, body } = convertRes(
-    res as ServerlessResponse,
+    res as OpenNextNodeResponse,
   );
 
   debug("ServerResponse data", { statusCode, headers, isBase64Encoded, body });
diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index d7610f516..fcba24145 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -1,15 +1,13 @@
 import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
 import crypto from "crypto";
-import { ServerResponse } from "http";
+import { OutgoingHttpHeaders, ServerResponse } from "http";
 
 import { BuildId, HtmlPages } from "../../config/index.js";
 import { InternalEvent } from "../../event-mapper.js";
+import { OpenNextNodeResponse } from "../../http/openNextResponse.js";
 import { IncomingMessage } from "../../http/request.js";
 import { ServerlessResponse } from "../../http/response.js";
-import {
-  ResponseStream,
-  StreamingServerResponse,
-} from "../../http/responseStreaming.js";
+import { ResponseStream } from "../../http/responseStreaming.js";
 import { awsLogger, debug } from "../../logger.js";
 
 declare global {
@@ -76,7 +74,7 @@ export async function proxyRequest(
 
 export function fixCacheHeaderForHtmlPages(
   rawPath: string,
-  headers: Record<string, string | undefined>,
+  headers: OutgoingHttpHeaders,
 ) {
   // WORKAROUND: `NextServer` does not set cache headers for HTML pages — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-cache-headers-for-html-pages
   if (HtmlPages.includes(rawPath) && headers[CommonHeaders.CACHE_CONTROL]) {
@@ -85,22 +83,21 @@ export function fixCacheHeaderForHtmlPages(
   }
 }
 
-export function fixSWRCacheHeader(
-  headers: Record<string, string | string[] | undefined>,
-) {
+export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
   // WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
   let cacheControl = headers[CommonHeaders.CACHE_CONTROL];
   if (!cacheControl) return;
   if (Array.isArray(cacheControl)) {
     cacheControl = cacheControl.join(",");
   }
+  if (typeof cacheControl !== "string") return;
   headers[CommonHeaders.CACHE_CONTROL] = cacheControl.replace(
     /\bstale-while-revalidate(?!=)/,
     "stale-while-revalidate=2592000", // 30 days
   );
 }
 
-export function addOpenNextHeader(headers: Record<string, string | undefined>) {
+export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
   headers["X-OpenNext"] = "1";
   if (globalThis.openNextDebug) {
     headers["X-OpenNext-Version"] = globalThis.openNextVersion;
@@ -110,7 +107,7 @@ export function addOpenNextHeader(headers: Record<string, string | undefined>) {
 export async function revalidateIfRequired(
   host: string,
   rawPath: string,
-  headers: Record<string, string | undefined>,
+  headers: OutgoingHttpHeaders,
   req?: IncomingMessage,
 ) {
   fixISRHeaders(headers);
@@ -204,7 +201,7 @@ function cyrb128(str: string) {
   return h1 >>> 0;
 }
 
-export function fixISRHeaders(headers: Record<string, string | undefined>) {
+export function fixISRHeaders(headers: OutgoingHttpHeaders) {
   if (headers[CommonHeaders.NEXT_CACHE] === "REVALIDATED") {
     headers[CommonHeaders.CACHE_CONTROL] =
       "private, no-cache, no-store, max-age=0, must-revalidate";
@@ -218,7 +215,9 @@ export function fixISRHeaders(headers: Record<string, string | undefined>) {
     const age = Math.round((Date.now() - globalThis.lastModified) / 1000);
     // extract s-maxage from cache-control
     const regex = /s-maxage=(\d+)/;
-    const match = headers[CommonHeaders.CACHE_CONTROL]?.match(regex);
+    const cacheControl = headers[CommonHeaders.CACHE_CONTROL];
+    if (!cacheControl || typeof cacheControl !== "string") return;
+    const match = cacheControl.match(regex);
     const sMaxAge = match ? parseInt(match[1]) : undefined;
 
     // 31536000 is the default s-maxage value for SSG pages
@@ -247,29 +246,21 @@ export function createServerResponse(
   headers: Record<string, string | string[] | undefined>,
   responseStream?: ResponseStream,
 ) {
-  const { method, rawPath } = internalEvent;
-  headers["accept-encoding"] = internalEvent.headers["accept-encoding"];
-  return responseStream
-    ? new StreamingServerResponse({
-        method,
-        headers,
-        responseStream,
-        fixHeaders(headers) {
-          fixCacheHeaderForHtmlPages(rawPath, headers);
-          fixSWRCacheHeader(headers);
-          addOpenNextHeader(headers);
-          fixISRHeaders(headers);
-        },
-        async onEnd(headers) {
-          await revalidateIfRequired(
-            internalEvent.headers.host,
-            rawPath,
-            headers,
-          );
-        },
-      })
-    : new ServerlessResponse({
-        method,
-        headers,
-      });
+  return new OpenNextNodeResponse(
+    (_headers) => {
+      fixCacheHeaderForHtmlPages(internalEvent.rawPath, _headers);
+      fixSWRCacheHeader(_headers);
+      addOpenNextHeader(_headers);
+      fixISRHeaders(_headers);
+    },
+    async (_headers) => {
+      await revalidateIfRequired(
+        internalEvent.headers.host,
+        internalEvent.rawPath,
+        _headers,
+      );
+    },
+    responseStream,
+    headers,
+  );
 }
diff --git a/packages/open-next/src/adapters/plugins/streaming.replacement.ts b/packages/open-next/src/adapters/plugins/streaming.replacement.ts
deleted file mode 100644
index c435b51a8..000000000
--- a/packages/open-next/src/adapters/plugins/streaming.replacement.ts
+++ /dev/null
@@ -1,105 +0,0 @@
-/*eslint-disable simple-import-sort/imports */
-import type {
-  APIGatewayProxyEvent,
-  APIGatewayProxyEventV2,
-  CloudFrontRequestEvent,
-} from "aws-lambda";
-
-import { convertFrom } from "../event-mapper";
-import { debug } from "../logger";
-import type { ResponseStream } from "../types/aws-lambda";
-import type { WarmerEvent } from "../warmer-function";
-//#override imports
-import { StreamingServerResponse } from "../http/responseStreaming";
-import { processInternalEvent } from "./routing/default.js";
-import {
-  addOpenNextHeader,
-  fixCacheHeaderForHtmlPages,
-  fixISRHeaders,
-  fixSWRCacheHeader,
-  revalidateIfRequired,
-} from "./routing/util";
-import { CreateResponse } from "../types/plugin";
-//#endOverride
-
-//#override lambdaHandler
-export const lambdaHandler = awslambda.streamifyResponse(async function (
-  event:
-    | APIGatewayProxyEventV2
-    | CloudFrontRequestEvent
-    | APIGatewayProxyEvent
-    | WarmerEvent,
-  responseStream: ResponseStream,
-) {
-  debug("event", event);
-
-  // Handler warmer
-  if ("type" in event) {
-    // @ts-ignore formatWarmerResponse defined in lambdaHandler
-    const result = await formatWarmerResponse(event);
-    responseStream.end(Buffer.from(JSON.stringify(result)), "utf-8");
-    return;
-  }
-  // Parse Lambda event and create Next.js request
-  const internalEvent = convertFrom(event);
-
-  // WORKAROUND: Set `x-forwarded-host` header (AWS specific) — https://github.com/serverless-stack/open-next#workaround-set-x-forwarded-host-header-aws-specific
-  if (internalEvent.headers["x-forwarded-host"]) {
-    internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
-  }
-
-  const createServerResponse: CreateResponse<StreamingServerResponse> = (
-    method: string,
-    headers: Record<string, string | string[] | undefined>,
-  ) => {
-    // sets the accept-encoding for responseStreaming.ts to set "content-encoding"
-    headers["accept-encoding"] = internalEvent.headers["accept-encoding"];
-    return new StreamingServerResponse({
-      method,
-      headers,
-      //@ts-ignore - we will remove this file
-      responseStream,
-      // We need to fix the cache header before sending any response
-      fixHeaders: (headers) => {
-        fixCacheHeaderForHtmlPages(internalEvent.rawPath, headers);
-        fixSWRCacheHeader(headers);
-        addOpenNextHeader(headers);
-        fixISRHeaders(headers);
-      },
-      // This run in the callback of the response stream end
-      onEnd: async (headers) => {
-        await revalidateIfRequired(
-          internalEvent.headers.host,
-          internalEvent.rawPath,
-          headers,
-        );
-      },
-    });
-  };
-
-  const preprocessResult = await processInternalEvent(
-    internalEvent,
-    createServerResponse,
-  );
-  if ("type" in preprocessResult) {
-    const headers = preprocessResult.headers;
-    const res = createServerResponse("GET", headers);
-
-    setImmediate(() => {
-      res.writeHead(preprocessResult.statusCode, headers);
-      res.write(preprocessResult.body);
-      res.end();
-    });
-  } else {
-    const {
-      req,
-      res,
-      isExternalRewrite,
-      internalEvent: overwrittenInternalEvent,
-    } = preprocessResult;
-
-    //@ts-expect-error - processRequest is already defined in serverHandler.ts
-    await processRequest(req, res, overwrittenInternalEvent, isExternalRewrite);
-  }
-});
-//#endOverride
diff --git a/packages/open-next/src/adapters/routing/middleware.ts b/packages/open-next/src/adapters/routing/middleware.ts
index 86f825af4..3cfff8186 100644
--- a/packages/open-next/src/adapters/routing/middleware.ts
+++ b/packages/open-next/src/adapters/routing/middleware.ts
@@ -2,8 +2,8 @@ import path from "node:path";
 
 import { NEXT_DIR, NextConfig } from "../config/index.js";
 import { InternalEvent, InternalResult } from "../event-mapper.js";
+import { OpenNextNodeResponse } from "../http/openNextResponse.js";
 import { IncomingMessage } from "../http/request.js";
-import { ServerlessResponse } from "../http/response.js";
 import {
   convertRes,
   convertToQueryString,
@@ -49,10 +49,10 @@ export async function handleMiddleware(
   if (internalEvent.headers["x-isr"]) return internalEvent;
 
   const req = new IncomingMessage(internalEvent);
-  const res = new ServerlessResponse({
-    method: req.method ?? "GET",
-    headers: {},
-  });
+  const res = new OpenNextNodeResponse(
+    () => void 0,
+    () => Promise.resolve(),
+  );
 
   // NOTE: Next middleware was originally developed to support nested middlewares
   // but that was discarded for simplicity. The MiddlewareInfo type still has the original
diff --git a/packages/open-next/src/adapters/routing/util.ts b/packages/open-next/src/adapters/routing/util.ts
index ce3ff879f..5c8bd5d0b 100644
--- a/packages/open-next/src/adapters/routing/util.ts
+++ b/packages/open-next/src/adapters/routing/util.ts
@@ -2,7 +2,8 @@ import fs from "node:fs";
 import path from "node:path";
 
 import { isBinaryContentType } from "../binary";
-import { ServerlessResponse } from "../http/response";
+import { OpenNextNodeResponse } from "../http/openNextResponse";
+import { parseHeaders } from "../http/util";
 import { MiddlewareManifest } from "../types/next-types";
 
 export function isExternal(url?: string, host?: string) {
@@ -35,17 +36,17 @@ export function getUrlParts(url: string, isExternal: boolean) {
   };
 }
 
-export function convertRes(res: ServerlessResponse) {
+export function convertRes(res: OpenNextNodeResponse) {
   // Format Next.js response to Lambda response
   const statusCode = res.statusCode || 200;
-  const headers = ServerlessResponse.headers(res);
+  const headers = parseHeaders(res.headers);
   const isBase64Encoded = isBinaryContentType(
     Array.isArray(headers["content-type"])
       ? headers["content-type"][0]
       : headers["content-type"],
   );
   const encoding = isBase64Encoded ? "base64" : "utf8";
-  const body = ServerlessResponse.body(res).toString(encoding);
+  const body = res.body.toString(encoding);
   return {
     statusCode,
     headers,
diff --git a/packages/open-next/src/adapters/types/plugin.ts b/packages/open-next/src/adapters/types/plugin.ts
index 47c7ed3a0..a2ece3e91 100644
--- a/packages/open-next/src/adapters/types/plugin.ts
+++ b/packages/open-next/src/adapters/types/plugin.ts
@@ -1,11 +1,8 @@
-import type { ServerResponse } from "http";
-
 import type { InternalEvent, InternalResult } from "../event-mapper";
+import { OpenNextNodeResponse } from "../http/openNextResponse";
 import type { IncomingMessage } from "../http/request";
 
-export type ProcessInternalEventResult<
-  Response extends ServerResponse = ServerResponse,
-> =
+export type ProcessInternalEventResult<Response extends OpenNextNodeResponse> =
   | {
       internalEvent: InternalEvent;
       req: IncomingMessage;
@@ -15,14 +12,14 @@ export type ProcessInternalEventResult<
   | InternalResult;
 
 export type ProcessInternalEvent<
-  Response extends ServerResponse = ServerResponse,
+  Response extends OpenNextNodeResponse = OpenNextNodeResponse,
 > = (
   internalEvent: InternalEvent,
   createResponse: CreateResponse<Response>,
 ) => Promise<ProcessInternalEventResult<Response>>;
 
 export interface PostProcessOptions<
-  Response extends ServerResponse = ServerResponse,
+  Response extends OpenNextNodeResponse = OpenNextNodeResponse,
 > {
   internalEvent: InternalEvent;
   req: IncomingMessage;
@@ -30,7 +27,7 @@ export interface PostProcessOptions<
   isExternalRewrite?: boolean;
 }
 
-export type CreateResponse<Response extends ServerResponse = ServerResponse> = (
+export type CreateResponse<Response extends OpenNextNodeResponse> = (
   method: string,
   headers: Record<string, string | string[] | undefined>,
 ) => Response;
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index f3521e9cd..ab4755b44 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -632,16 +632,16 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   }
 
   if (streaming) {
-    const streamingPlugin = openNextPlugin({
-      name: "opennext-streaming",
-      target: /plugins\/lambdaHandler\.js/g,
-      replacements: ["./streaming.replacement.js"],
-    });
-    if (plugins) {
-      plugins.push(streamingPlugin);
-    } else {
-      plugins = [streamingPlugin];
-    }
+    // const streamingPlugin = openNextPlugin({
+    //   name: "opennext-streaming",
+    //   target: /plugins\/lambdaHandler\.js/g,
+    //   replacements: ["./streaming.replacement.js"],
+    // });
+    // if (plugins) {
+    //   plugins.push(streamingPlugin);
+    // } else {
+    //   plugins = [streamingPlugin];
+    // }
   }
 
   if (plugins && plugins.length > 0) {
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 4e8087f91..557ce75df 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -34,6 +34,13 @@ export async function openNextHandler(
   );
 
   if ("type" in preprocessResult) {
+    // res is used only in the streaming case
+    const headers = preprocessResult.headers;
+    const res = createServerResponse(internalEvent, headers, responseStreaming);
+    res.statusCode = preprocessResult.statusCode;
+    res.flushHeaders();
+    res.write(preprocessResult.body);
+    res.end();
     return preprocessResult;
   } else {
     const {

From 788913e9c3f8a61ba2bd7b71ef181c76cca39d9d Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 3 Nov 2023 14:41:13 +0100
Subject: [PATCH 004/102] build config

---
 .../src/adapters/plugins/routing/util.ts        |  3 ++-
 packages/open-next/src/build.ts                 | 10 ++++++++++
 packages/open-next/src/index.ts                 | 17 ++++++++++++++++-
 .../src/wrappers/aws-lambda-streaming.ts        |  2 +-
 4 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index fcba24145..173f81ffc 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -216,7 +216,8 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
     // extract s-maxage from cache-control
     const regex = /s-maxage=(\d+)/;
     const cacheControl = headers[CommonHeaders.CACHE_CONTROL];
-    if (!cacheControl || typeof cacheControl !== "string") return;
+    debug("cache-control", cacheControl, globalThis.lastModified, Date.now());
+    if (typeof cacheControl !== "string") return;
     const match = cacheControl.match(regex);
     const sMaxAge = match ? parseInt(match[1]) : undefined;
 
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index ab4755b44..853fd5e39 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -565,6 +565,16 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   const outputPath = path.join(outputDir, "server-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
+  // Copy open-next.config.js
+  // We should reuse the one we created at the beginning of the build
+  esbuildSync({
+    entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
+    outfile: path.join(outputPath, "open-next.config.js"),
+    bundle: true,
+    format: "cjs",
+    target: ["node18"],
+  });
+
   // Resolve path to the Next.js app if inside the monorepo
   // note: if user's app is inside a monorepo, standalone mode places
   //       `node_modules` inside `.next/standalone`, and others inside
diff --git a/packages/open-next/src/index.ts b/packages/open-next/src/index.ts
index 65341fa49..9fb8f375f 100755
--- a/packages/open-next/src/index.ts
+++ b/packages/open-next/src/index.ts
@@ -1,5 +1,8 @@
 #!/usr/bin/env node
 
+import * as esbuild from "esbuild";
+import path from "path";
+
 import { build } from "./build.js";
 
 const command = process.argv[2];
@@ -9,7 +12,19 @@ const args = parseArgs();
 if (Object.keys(args).includes("--help")) printHelp();
 
 //TODO: validate config file
-const config = await import(process.cwd() + "/open-next.config.js");
+
+const outputTmpPath = path.join(process.cwd(), ".open-next", ".build");
+
+// Compile open-next.config.ts
+esbuild.buildSync({
+  entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
+  outfile: path.join(outputTmpPath, "open-next.config.js"),
+  bundle: true,
+  format: "cjs",
+  target: ["node18"],
+});
+
+const config = await import(outputTmpPath + "/open-next.config.js");
 
 build(config.default);
 
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 974430591..0902cde14 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -42,7 +42,7 @@ const handler: Wrapper = async (handler, converter) =>
         });
       };
 
-      const response = await handler(internalEvent);
+      const response = await handler(internalEvent, res);
 
       return converter.convertTo(response);
     },

From b20f0b5614c2dee2f8ecd6f3714a2f29484fad13 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 3 Nov 2023 20:14:44 +0100
Subject: [PATCH 005/102] change response to transform to allow to use pipeline

---
 .../src/adapters/http/openNextResponse.ts     | 65 ++++++-------------
 .../src/wrappers/aws-lambda-streaming.ts      | 24 ++++---
 2 files changed, 32 insertions(+), 57 deletions(-)

diff --git a/packages/open-next/src/adapters/http/openNextResponse.ts b/packages/open-next/src/adapters/http/openNextResponse.ts
index f00439060..bc376c8ad 100644
--- a/packages/open-next/src/adapters/http/openNextResponse.ts
+++ b/packages/open-next/src/adapters/http/openNextResponse.ts
@@ -1,11 +1,11 @@
 import { OutgoingHttpHeader, OutgoingHttpHeaders } from "http";
-import { Writable } from "stream";
+import { Transform, TransformCallback } from "stream";
 
 import { ResponseStream } from "./responseStreaming";
 import { parseHeaders } from "./util";
 
 // We only need to implement the methods that are used by next.js
-export class OpenNextNodeResponse extends Writable {
+export class OpenNextNodeResponse extends Transform {
   statusCode: number | undefined;
   statusMessage: string | undefined;
   headers: OutgoingHttpHeaders = {};
@@ -14,11 +14,14 @@ export class OpenNextNodeResponse extends Writable {
 
   constructor(
     private fixHeaders: (headers: OutgoingHttpHeaders) => void,
-    private onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
+    onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
     private responseStream?: ResponseStream,
     private initialHeaders?: OutgoingHttpHeaders,
   ) {
     super();
+    this.once("finish", () => {
+      onEnd(this.headers);
+    });
   }
 
   get finished() {
@@ -54,14 +57,18 @@ export class OpenNextNodeResponse extends Writable {
       this.headers = { ...this.headers, ...this.initialHeaders };
     }
     this.fixHeaders(this.headers);
-    this.responseStream?.writeHeaders(
-      {
-        statusCode: this.statusCode ?? 200,
-        cookies: [],
-        headers: parseHeaders(this.headers),
-      },
-      () => {},
-    );
+
+    if (this.responseStream) {
+      this.responseStream?.writeHeaders(
+        {
+          statusCode: this.statusCode ?? 200,
+          cookies: [],
+          headers: parseHeaders(this.headers),
+        },
+        () => {},
+      );
+      this.pipe(this.responseStream);
+    }
   }
 
   // Might be used in next page api routes
@@ -80,13 +87,13 @@ export class OpenNextNodeResponse extends Writable {
 
   private _internalWrite(chunk: any, encoding: BufferEncoding) {
     this._chunks.push(Buffer.from(chunk, encoding));
-    return this.responseStream?.write(chunk, encoding);
+    this.push(chunk, encoding);
   }
 
-  _write(
+  _transform(
     chunk: any,
     encoding: BufferEncoding,
-    callback: (error?: Error | null | undefined) => void,
+    callback: TransformCallback,
   ): void {
     if (!this.headersSent) {
       this.flushHeaders();
@@ -94,34 +101,4 @@ export class OpenNextNodeResponse extends Writable {
     this._internalWrite(chunk, encoding);
     callback();
   }
-
-  end(cb?: (() => void) | undefined): this;
-  end(chunk: any, cb?: (() => void) | undefined): this;
-  end(
-    chunk: any,
-    encoding: BufferEncoding,
-    cb?: (() => void) | undefined,
-  ): this;
-  end(chunk?: unknown, encoding?: unknown, cb?: unknown): this {
-    this.onEnd(parseHeaders(this.headers));
-    if (!this.headersSent) {
-      this.flushHeaders();
-    }
-    if (!chunk) {
-      this.responseStream?.end();
-      return this;
-    }
-    if (typeof chunk === "function") {
-      chunk();
-    } else if (typeof encoding === "function") {
-      this._internalWrite(chunk, "utf8");
-      encoding();
-    } else {
-      this._internalWrite(chunk, encoding as BufferEncoding);
-      //@ts-expect-error - typescript doesn't infer that cb is a function
-      cb?.();
-    }
-    this.responseStream?.end();
-    return this;
-  }
 }
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 0902cde14..e98d2e2cd 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -27,19 +27,17 @@ const handler: Wrapper = async (handler, converter) =>
 
         // Try to flush the buffer to the client to invoke
         // the streaming. This does not work 100% of the time.
-        setImmediate(() => {
-          responseStream.write("\n\n");
-          responseStream.uncork();
-        });
-        setImmediate(() => {
-          responseStream.write(prelude);
-        });
-
-        setImmediate(() => {
-          responseStream.write(new Uint8Array(8));
-
-          onFinish();
-        });
+
+        responseStream.write("\n\n");
+
+        responseStream.write(prelude);
+
+        responseStream.write(new Uint8Array(8));
+        responseStream.uncork();
+
+        // responseStream.write("\r\n\r\n");
+
+        onFinish();
       };
 
       const response = await handler(internalEvent, res);

From a874fd786b7966dd17d4121d51d391a8406cf4e7 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 7 Nov 2023 23:20:34 +0100
Subject: [PATCH 006/102] fix streaming for v3

---
 examples/app-pages-router/open-next.config.ts |  8 +++
 examples/app-router/open-next.config.ts       | 12 ++++
 examples/pages-router/open-next.config.ts     |  8 +++
 .../src/adapters/http/openNextResponse.ts     | 70 +++++++++++++-----
 .../src/adapters/http/responseStreaming.ts    |  2 +
 .../src/adapters/plugins/routing/util.ts      |  8 ++-
 .../open-next/src/adapters/types/open-next.ts |  4 +-
 packages/open-next/src/build.ts               | 18 ++---
 packages/open-next/src/core/requestHandler.ts |  9 +--
 .../src/wrappers/aws-lambda-streaming.ts      | 72 +++++++++++++------
 packages/open-next/src/wrappers/docker.ts     | 14 ++--
 11 files changed, 160 insertions(+), 65 deletions(-)
 create mode 100644 examples/app-pages-router/open-next.config.ts
 create mode 100644 examples/app-router/open-next.config.ts
 create mode 100644 examples/pages-router/open-next.config.ts

diff --git a/examples/app-pages-router/open-next.config.ts b/examples/app-pages-router/open-next.config.ts
new file mode 100644
index 000000000..6b4345ecf
--- /dev/null
+++ b/examples/app-pages-router/open-next.config.ts
@@ -0,0 +1,8 @@
+const config = {
+  functions: {
+    default: {},
+  },
+  buildCommand: "npx turbo build",
+};
+
+module.exports = config;
diff --git a/examples/app-router/open-next.config.ts b/examples/app-router/open-next.config.ts
new file mode 100644
index 000000000..423ca55ef
--- /dev/null
+++ b/examples/app-router/open-next.config.ts
@@ -0,0 +1,12 @@
+const config = {
+  functions: {
+    default: {
+      override: {
+        wrapper: "aws-lambda-streaming",
+      },
+    },
+  },
+  buildCommand: "npx turbo build",
+};
+
+module.exports = config;
diff --git a/examples/pages-router/open-next.config.ts b/examples/pages-router/open-next.config.ts
new file mode 100644
index 000000000..6b4345ecf
--- /dev/null
+++ b/examples/pages-router/open-next.config.ts
@@ -0,0 +1,8 @@
+const config = {
+  functions: {
+    default: {},
+  },
+  buildCommand: "npx turbo build",
+};
+
+module.exports = config;
diff --git a/packages/open-next/src/adapters/http/openNextResponse.ts b/packages/open-next/src/adapters/http/openNextResponse.ts
index bc376c8ad..b986735f3 100644
--- a/packages/open-next/src/adapters/http/openNextResponse.ts
+++ b/packages/open-next/src/adapters/http/openNextResponse.ts
@@ -1,24 +1,43 @@
 import { OutgoingHttpHeader, OutgoingHttpHeaders } from "http";
-import { Transform, TransformCallback } from "stream";
+import { Transform, TransformCallback, Writable } from "stream";
 
-import { ResponseStream } from "./responseStreaming";
-import { parseHeaders } from "./util";
+import { parseCookies } from "../util";
+import { convertHeader, parseHeaders } from "./util";
+
+const SET_COOKIE_HEADER = "set-cookie";
+
+export interface StreamCreator {
+  writeHeaders(prelude: {
+    statusCode: number;
+    cookies: string[];
+    headers: Record<string, string>;
+  }): Writable;
+  // Just to fix an issue with aws lambda streaming with empty body
+  onWrite?: () => void;
+}
 
 // We only need to implement the methods that are used by next.js
 export class OpenNextNodeResponse extends Transform {
   statusCode: number | undefined;
   statusMessage: string | undefined;
   headers: OutgoingHttpHeaders = {};
+  private _cookies: string[] = [];
+  private responseStream?: Writable;
   headersSent: boolean = false;
   _chunks: Buffer[] = [];
 
   constructor(
     private fixHeaders: (headers: OutgoingHttpHeaders) => void,
     onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
-    private responseStream?: ResponseStream,
+    private streamCreator?: StreamCreator,
     private initialHeaders?: OutgoingHttpHeaders,
   ) {
     super();
+    if (initialHeaders && initialHeaders[SET_COOKIE_HEADER]) {
+      this._cookies = parseCookies(
+        initialHeaders[SET_COOKIE_HEADER] as string | string[],
+      ) as string[];
+    }
     this.once("finish", () => {
       onEnd(this.headers);
     });
@@ -29,17 +48,33 @@ export class OpenNextNodeResponse extends Transform {
   }
 
   setHeader(name: string, value: string | string[]): this {
-    this.headers[name.toLowerCase()] = value;
+    const key = name.toLowerCase();
+    if (key === SET_COOKIE_HEADER) {
+      this._cookies.push(convertHeader(value));
+      this.headers[key] = this._cookies;
+    } else {
+      this.headers[key] = value;
+    }
+
     return this;
   }
 
   removeHeader(name: string): this {
-    delete this.headers[name.toLowerCase()];
+    const key = name.toLowerCase();
+    if (key === SET_COOKIE_HEADER) {
+      this._cookies = [];
+    } else {
+      delete this.headers[key];
+    }
     return this;
   }
 
   hasHeader(name: string): boolean {
-    return this.headers[name.toLowerCase()] !== undefined;
+    const key = name.toLowerCase();
+    if (key === SET_COOKIE_HEADER) {
+      return this._cookies.length > 0;
+    }
+    return this.headers[key] !== undefined;
   }
 
   getHeaders(): OutgoingHttpHeaders {
@@ -54,19 +89,19 @@ export class OpenNextNodeResponse extends Transform {
   flushHeaders() {
     this.headersSent = true;
     if (this.initialHeaders) {
-      this.headers = { ...this.headers, ...this.initialHeaders };
+      this.headers = {
+        ...this.headers,
+        ...this.initialHeaders,
+      };
     }
     this.fixHeaders(this.headers);
 
-    if (this.responseStream) {
-      this.responseStream?.writeHeaders(
-        {
-          statusCode: this.statusCode ?? 200,
-          cookies: [],
-          headers: parseHeaders(this.headers),
-        },
-        () => {},
-      );
+    if (this.streamCreator) {
+      this.responseStream = this.streamCreator?.writeHeaders({
+        statusCode: this.statusCode ?? 200,
+        cookies: this._cookies,
+        headers: parseHeaders(this.headers),
+      });
       this.pipe(this.responseStream);
     }
   }
@@ -88,6 +123,7 @@ export class OpenNextNodeResponse extends Transform {
   private _internalWrite(chunk: any, encoding: BufferEncoding) {
     this._chunks.push(Buffer.from(chunk, encoding));
     this.push(chunk, encoding);
+    this.streamCreator?.onWrite?.();
   }
 
   _transform(
diff --git a/packages/open-next/src/adapters/http/responseStreaming.ts b/packages/open-next/src/adapters/http/responseStreaming.ts
index d36937dc2..d41eefb2f 100644
--- a/packages/open-next/src/adapters/http/responseStreaming.ts
+++ b/packages/open-next/src/adapters/http/responseStreaming.ts
@@ -20,6 +20,8 @@ export interface ResponseStream extends Writable {
     },
     onFinish: () => void,
   ): void;
+  // Just to fix an issue with aws lambda streaming with empty body
+  onFirstWrite?: () => void;
 }
 
 export interface StreamingServerResponseProps {
diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index 173f81ffc..83cf84146 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -4,10 +4,12 @@ import { OutgoingHttpHeaders, ServerResponse } from "http";
 
 import { BuildId, HtmlPages } from "../../config/index.js";
 import { InternalEvent } from "../../event-mapper.js";
-import { OpenNextNodeResponse } from "../../http/openNextResponse.js";
+import {
+  OpenNextNodeResponse,
+  StreamCreator,
+} from "../../http/openNextResponse.js";
 import { IncomingMessage } from "../../http/request.js";
 import { ServerlessResponse } from "../../http/response.js";
-import { ResponseStream } from "../../http/responseStreaming.js";
 import { awsLogger, debug } from "../../logger.js";
 
 declare global {
@@ -245,7 +247,7 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
 export function createServerResponse(
   internalEvent: InternalEvent,
   headers: Record<string, string | string[] | undefined>,
-  responseStream?: ResponseStream,
+  responseStream?: StreamCreator,
 ) {
   return new OpenNextNodeResponse(
     (_headers) => {
diff --git a/packages/open-next/src/adapters/types/open-next.ts b/packages/open-next/src/adapters/types/open-next.ts
index 46096ca1a..47fa00d1d 100644
--- a/packages/open-next/src/adapters/types/open-next.ts
+++ b/packages/open-next/src/adapters/types/open-next.ts
@@ -1,5 +1,5 @@
 import { InternalEvent, InternalResult } from "../event-mapper";
-import { ResponseStream } from "../http";
+import { StreamCreator } from "../http/openNextResponse";
 
 export interface DangerousOptions {
   /**
@@ -19,7 +19,7 @@ export type LazyLoadedOverride<T> = () => Promise<T>;
 
 export type OpenNextHandler = (
   event: InternalEvent,
-  responseStream?: ResponseStream,
+  responseStream?: StreamCreator,
 ) => Promise<InternalResult | void>;
 
 export type Converter = {
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 853fd5e39..d52e33aa5 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -565,24 +565,24 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   const outputPath = path.join(outputDir, "server-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
+  // Resolve path to the Next.js app if inside the monorepo
+  // note: if user's app is inside a monorepo, standalone mode places
+  //       `node_modules` inside `.next/standalone`, and others inside
+  //       `.next/standalone/package/path` (ie. `.next`, `server.js`).
+  //       We need to output the handler file inside the package path.
+  const isMonorepo = monorepoRoot !== appPath;
+  const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
+
   // Copy open-next.config.js
   // We should reuse the one we created at the beginning of the build
   esbuildSync({
     entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
-    outfile: path.join(outputPath, "open-next.config.js"),
+    outfile: path.join(outputPath, packagePath, "open-next.config.js"),
     bundle: true,
     format: "cjs",
     target: ["node18"],
   });
 
-  // Resolve path to the Next.js app if inside the monorepo
-  // note: if user's app is inside a monorepo, standalone mode places
-  //       `node_modules` inside `.next/standalone`, and others inside
-  //       `.next/standalone/package/path` (ie. `.next`, `server.js`).
-  //       We need to output the handler file inside the package path.
-  const isMonorepo = monorepoRoot !== appPath;
-  const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
-
   // Copy over standalone output files
   // note: if user uses pnpm as the package manager, node_modules contain
   //       symlinks. We don't want to resolve the symlinks when copying.
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 557ce75df..67fe92afa 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -1,10 +1,7 @@
 import { BuildId } from "../adapters/config";
 import { InternalEvent, InternalResult } from "../adapters/event-mapper";
-import {
-  IncomingMessage,
-  ResponseStream,
-  ServerlessResponse,
-} from "../adapters/http";
+import { IncomingMessage, ServerlessResponse } from "../adapters/http";
+import { StreamCreator } from "../adapters/http/openNextResponse";
 import { error } from "../adapters/logger";
 import {
   postProcessResponse,
@@ -15,7 +12,7 @@ import { handler as serverHandler } from "../adapters/plugins/serverHandler";
 
 export async function openNextHandler(
   internalEvent: InternalEvent,
-  responseStreaming?: ResponseStream,
+  responseStreaming?: StreamCreator,
 ): Promise<InternalResult | void> {
   if (internalEvent.headers["x-forwarded-host"]) {
     internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index e98d2e2cd..f6fb92742 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -1,7 +1,9 @@
 import { APIGatewayProxyEventV2 } from "aws-lambda";
 
-import { ResponseStream } from "../adapters/http";
+import { StreamCreator } from "../adapters/http/openNextResponse";
+import { parseHeaders } from "../adapters/http/util";
 import { Wrapper } from "../adapters/types/open-next";
+import { parseCookies } from "../adapters/util";
 import { WarmerEvent } from "../adapters/warmer-function";
 
 type AwsLambdaEvent = APIGatewayProxyEventV2 | WarmerEvent;
@@ -11,36 +13,64 @@ const handler: Wrapper = async (handler, converter) =>
   awslambda.streamifyResponse(
     async (event: AwsLambdaEvent, responseStream): Promise<AwsLambdaReturn> => {
       const internalEvent = converter.convertFrom(event);
+      let _hasWriten = false;
+      let _headersSent = false;
 
-      const res = responseStream as any as ResponseStream;
+      const streamCreator: StreamCreator = {
+        writeHeaders: (_prelude) => {
+          responseStream.cork();
+          // FIXME: This is extracted from the docker lambda node 18 runtime
+          // https://gist.github.com/conico974/13afd708af20711b97df439b910ceb53#file-index-mjs-L921-L932
+          // We replace their write with ours which are inside a setImmediate
+          // This way it seems to work all the time
+          // I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
+          responseStream.setContentType(
+            "application/vnd.awslambda.http-integration-response",
+          );
+          const prelude = JSON.stringify(_prelude);
 
-      res["writeHeaders"] = (_prelude, onFinish) => {
-        // FIXME: This is extracted from the docker lambda node 18 runtime
-        // https://gist.github.com/conico974/13afd708af20711b97df439b910ceb53#file-index-mjs-L921-L932
-        // We replace their write with ours which are inside a setImmediate
-        // This way it seems to work all the time
-        // I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
-        responseStream.setContentType(
-          "application/vnd.awslambda.http-integration-response",
-        );
-        const prelude = JSON.stringify(_prelude);
+          // Try to flush the buffer to the client to invoke
+          // the streaming. This does not work 100% of the time.
 
-        // Try to flush the buffer to the client to invoke
-        // the streaming. This does not work 100% of the time.
+          responseStream.write("\n\n");
 
-        responseStream.write("\n\n");
+          responseStream.write(prelude);
 
-        responseStream.write(prelude);
+          responseStream.write(new Uint8Array(8));
 
-        responseStream.write(new Uint8Array(8));
-        responseStream.uncork();
+          if (responseStream.writableCorked) {
+            for (let i = 0; i < responseStream.writableCorked; i++) {
+              responseStream.uncork();
+            }
+          }
 
-        // responseStream.write("\r\n\r\n");
+          _headersSent = true;
 
-        onFinish();
+          return responseStream;
+        },
+        onWrite: () => {
+          _hasWriten = true;
+          // Force flushing data, seems necessary for aws lambda streaming to work reliably
+          // We need to reevaluate this if it causes issues on other platforms
+          if (responseStream?.writableCorked) {
+            responseStream?.uncork();
+          }
+        },
       };
 
-      const response = await handler(internalEvent, res);
+      const response = await handler(internalEvent, streamCreator);
+
+      if (!responseStream.writableFinished) {
+        // If the headers are not sent, we need to send them
+        if (!_headersSent) {
+          streamCreator.writeHeaders({
+            statusCode: response?.statusCode ?? 500,
+            cookies: parseCookies(response?.headers["set-cookie"]) ?? [],
+            headers: parseHeaders(response?.headers),
+          });
+        }
+        responseStream.end(_hasWriten ? undefined : new Uint8Array(8));
+      }
 
       return converter.convertTo(response);
     },
diff --git a/packages/open-next/src/wrappers/docker.ts b/packages/open-next/src/wrappers/docker.ts
index 2549f0e55..a3bd933ff 100644
--- a/packages/open-next/src/wrappers/docker.ts
+++ b/packages/open-next/src/wrappers/docker.ts
@@ -1,17 +1,17 @@
 import { createServer } from "http";
 
-import { ResponseStream } from "../adapters/http";
+import { StreamCreator } from "../adapters/http/openNextResponse";
 import { Wrapper } from "../adapters/types/open-next";
 
 const wrapper: Wrapper = async (handler, converter) => {
   const server = createServer(async (req, res) => {
     const internalEvent = converter.convertFrom(req);
-    const _res = res as any as ResponseStream;
-
-    _res["writeHeaders"] = (prelude, onFinish) => {
-      res.writeHead(prelude.statusCode, prelude.headers);
-      res.uncork();
-      onFinish();
+    const _res: StreamCreator = {
+      writeHeaders: (prelude) => {
+        res.writeHead(prelude.statusCode, prelude.headers);
+        res.uncork();
+        return res;
+      },
     };
 
     await handler(internalEvent, _res);

From e7fe51545bac9ae1637b66f635e2551ac91738ea Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 8 Nov 2023 10:52:00 +0100
Subject: [PATCH 007/102] compression support

---
 .../src/wrappers/aws-lambda-streaming.ts      | 44 ++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)

diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index f6fb92742..c7b78a1c3 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -1,3 +1,6 @@
+import { Writable } from "node:stream";
+import zlib from "node:zlib";
+
 import { APIGatewayProxyEventV2 } from "aws-lambda";
 
 import { StreamCreator } from "../adapters/http/openNextResponse";
@@ -16,6 +19,35 @@ const handler: Wrapper = async (handler, converter) =>
       let _hasWriten = false;
       let _headersSent = false;
 
+      //Handle compression
+      const acceptEncoding = internalEvent.headers["accept-encoding"] ?? "";
+      let contentEncoding: string;
+      let compressedStream: Writable | undefined;
+
+      if (acceptEncoding.includes("br")) {
+        contentEncoding = "br";
+        compressedStream = zlib.createBrotliCompress({
+          flush: zlib.constants.BROTLI_OPERATION_FLUSH,
+          finishFlush: zlib.constants.BROTLI_OPERATION_FINISH,
+        });
+        compressedStream.pipe(responseStream);
+      } else if (acceptEncoding.includes("gzip")) {
+        contentEncoding = "gzip";
+        compressedStream = zlib.createGzip({
+          flush: zlib.constants.Z_SYNC_FLUSH,
+        });
+        compressedStream.pipe(responseStream);
+      } else if (acceptEncoding.includes("deflate")) {
+        contentEncoding = "deflate";
+        compressedStream = zlib.createDeflate({
+          flush: zlib.constants.Z_SYNC_FLUSH,
+        });
+        compressedStream.pipe(responseStream);
+      } else {
+        contentEncoding = "identity";
+        compressedStream = responseStream;
+      }
+
       const streamCreator: StreamCreator = {
         writeHeaders: (_prelude) => {
           responseStream.cork();
@@ -24,9 +56,11 @@ const handler: Wrapper = async (handler, converter) =>
           // We replace their write with ours which are inside a setImmediate
           // This way it seems to work all the time
           // I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
+
           responseStream.setContentType(
             "application/vnd.awslambda.http-integration-response",
           );
+          _prelude.headers["content-encoding"] = contentEncoding;
           const prelude = JSON.stringify(_prelude);
 
           // Try to flush the buffer to the client to invoke
@@ -46,21 +80,21 @@ const handler: Wrapper = async (handler, converter) =>
 
           _headersSent = true;
 
-          return responseStream;
+          return compressedStream ?? responseStream;
         },
         onWrite: () => {
           _hasWriten = true;
           // Force flushing data, seems necessary for aws lambda streaming to work reliably
           // We need to reevaluate this if it causes issues on other platforms
-          if (responseStream?.writableCorked) {
-            responseStream?.uncork();
+          if (compressedStream?.writableCorked) {
+            compressedStream?.uncork();
           }
         },
       };
 
       const response = await handler(internalEvent, streamCreator);
 
-      if (!responseStream.writableFinished) {
+      if (!compressedStream.writableFinished) {
         // If the headers are not sent, we need to send them
         if (!_headersSent) {
           streamCreator.writeHeaders({
@@ -69,7 +103,7 @@ const handler: Wrapper = async (handler, converter) =>
             headers: parseHeaders(response?.headers),
           });
         }
-        responseStream.end(_hasWriten ? undefined : new Uint8Array(8));
+        compressedStream.end(_hasWriten ? undefined : new Uint8Array(8));
       }
 
       return converter.convertTo(response);

From a5622de7e3766cbd59ff0f9ddc480747dfb3a3e0 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 8 Nov 2023 12:42:58 +0100
Subject: [PATCH 008/102] better docker handler

---
 .../open-next/src/adapters/types/open-next.ts |  2 +-
 .../open-next/src/converters/aws-apigw-v2.ts  |  4 +-
 packages/open-next/src/converters/docker.ts   | 58 +++++++++++++------
 .../src/wrappers/aws-lambda-streaming.ts      |  2 +-
 packages/open-next/src/wrappers/aws-lambda.ts |  2 +-
 packages/open-next/src/wrappers/docker.ts     | 31 +++++++++-
 6 files changed, 73 insertions(+), 26 deletions(-)

diff --git a/packages/open-next/src/adapters/types/open-next.ts b/packages/open-next/src/adapters/types/open-next.ts
index 47fa00d1d..3838373a8 100644
--- a/packages/open-next/src/adapters/types/open-next.ts
+++ b/packages/open-next/src/adapters/types/open-next.ts
@@ -23,7 +23,7 @@ export type OpenNextHandler = (
 ) => Promise<InternalResult | void>;
 
 export type Converter = {
-  convertFrom: (event: any) => InternalEvent;
+  convertFrom: (event: any) => Promise<InternalEvent>;
   convertTo: (result: any) => any;
 };
 
diff --git a/packages/open-next/src/converters/aws-apigw-v2.ts b/packages/open-next/src/converters/aws-apigw-v2.ts
index 65e014e1f..52a9c8152 100644
--- a/packages/open-next/src/converters/aws-apigw-v2.ts
+++ b/packages/open-next/src/converters/aws-apigw-v2.ts
@@ -39,9 +39,9 @@ function normalizeAPIGatewayProxyEventV2Headers(
   return headers;
 }
 
-function convertFromAPIGatewayProxyEventV2(
+async function convertFromAPIGatewayProxyEventV2(
   event: APIGatewayProxyEventV2,
-): InternalEvent {
+): Promise<InternalEvent> {
   const { rawPath, rawQueryString, requestContext } = event;
   return {
     type: "v2",
diff --git a/packages/open-next/src/converters/docker.ts b/packages/open-next/src/converters/docker.ts
index 32782445b..e433ddf6b 100644
--- a/packages/open-next/src/converters/docker.ts
+++ b/packages/open-next/src/converters/docker.ts
@@ -2,27 +2,47 @@ import { IncomingMessage } from "http";
 
 import { InternalResult } from "../adapters/event-mapper";
 import { Converter } from "../adapters/types/open-next";
+import { parseCookies } from "../adapters/util";
 
 const converter: Converter = {
-  convertFrom: (req: IncomingMessage) => ({
-    type: "v2",
-    method: req.method ?? "GET",
-    rawPath: req.url!,
-    url: req.url!,
-    body: Buffer.from(""),
-    headers: Object.fromEntries(
-      Object.entries(req.headers ?? {})
-        .map(([key, value]) => [
-          key.toLowerCase(),
-          Array.isArray(value) ? value.join(",") : value,
-        ])
-        .filter(([key]) => key),
-    ),
-    remoteAddress: "",
-    query: {},
-    cookies: {},
-  }),
-  // Do nothing here, it's streaming
+  convertFrom: async (req: IncomingMessage) => {
+    const body = await new Promise<Buffer>((resolve) => {
+      const chunks: Uint8Array[] = [];
+      req.on("data", (chunk) => {
+        chunks.push(chunk);
+      });
+      req.on("end", () => {
+        resolve(Buffer.concat(chunks));
+      });
+    });
+
+    const url = new URL(req.url!, `http://${req.headers.host}`);
+    const query = Object.fromEntries(url.searchParams.entries());
+    return {
+      type: "v2",
+      method: req.method ?? "GET",
+      rawPath: url.pathname,
+      url: url.toString(),
+      body,
+      headers: Object.fromEntries(
+        Object.entries(req.headers ?? {})
+          .map(([key, value]) => [
+            key.toLowerCase(),
+            Array.isArray(value) ? value.join(",") : value,
+          ])
+          .filter(([key]) => key),
+      ),
+      remoteAddress: req.headers["x-forwarded-for"] as string,
+      query,
+      cookies: Object.fromEntries(
+        parseCookies(req.headers["cookie"])?.map((cookie) => {
+          const [key, value] = cookie.split("=");
+          return [key, value];
+        }) ?? [],
+      ),
+    };
+  },
+  // Nothing to do here, it's streaming
   convertTo: (internalResult: InternalResult) => ({
     body: internalResult.body,
     headers: internalResult.headers,
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index c7b78a1c3..a1a61e36a 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -15,7 +15,7 @@ type AwsLambdaReturn = void;
 const handler: Wrapper = async (handler, converter) =>
   awslambda.streamifyResponse(
     async (event: AwsLambdaEvent, responseStream): Promise<AwsLambdaReturn> => {
-      const internalEvent = converter.convertFrom(event);
+      const internalEvent = await converter.convertFrom(event);
       let _hasWriten = false;
       let _headersSent = false;
 
diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index 869bde44f..709843ec5 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -24,7 +24,7 @@ type AwsLambdaReturn =
 const handler: Wrapper =
   async (handler, converter) =>
   async (event: AwsLambdaEvent): Promise<AwsLambdaReturn> => {
-    const internalEvent = converter.convertFrom(event);
+    const internalEvent = await converter.convertFrom(event);
 
     const response = await handler(internalEvent);
 
diff --git a/packages/open-next/src/wrappers/docker.ts b/packages/open-next/src/wrappers/docker.ts
index a3bd933ff..cb36c66ab 100644
--- a/packages/open-next/src/wrappers/docker.ts
+++ b/packages/open-next/src/wrappers/docker.ts
@@ -1,11 +1,13 @@
+import debug from "debug";
 import { createServer } from "http";
 
 import { StreamCreator } from "../adapters/http/openNextResponse";
+import { error } from "../adapters/logger";
 import { Wrapper } from "../adapters/types/open-next";
 
 const wrapper: Wrapper = async (handler, converter) => {
   const server = createServer(async (req, res) => {
-    const internalEvent = converter.convertFrom(req);
+    const internalEvent = await converter.convertFrom(req);
     const _res: StreamCreator = {
       writeHeaders: (prelude) => {
         res.writeHead(prelude.statusCode, prelude.headers);
@@ -17,7 +19,32 @@ const wrapper: Wrapper = async (handler, converter) => {
     await handler(internalEvent, _res);
   });
 
-  server.listen(3000);
+  await new Promise<void>((resolve) => {
+    server.on("listening", () => {
+      const cleanup = (code: number) => {
+        debug(`Closing server`);
+        server.close(() => {
+          debug(`Server closed`);
+          process.exit(code);
+        });
+      };
+      console.log(`Listening on port ${process.env.PORT ?? "3000"}`);
+      debug(`Open Next version: ${process.env.OPEN_NEXT_VERSION}`);
+
+      process.on("exit", (code) => cleanup(code));
+
+      process.on("SIGINT", () => cleanup(0));
+      process.on("SIGTERM", () => cleanup(0));
+
+      resolve();
+    });
+
+    server.listen(parseInt(process.env.PORT ?? "3000", 10));
+  });
+
+  server.on("error", (err) => {
+    error(err);
+  });
 
   return () => {
     server.close();

From 26b338687e014ae82400210c420996e6788b49a3 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 8 Nov 2023 13:00:12 +0100
Subject: [PATCH 009/102] add converter for apigw-v1 & cloudfront

---
 .../open-next/src/converters/aws-apigw-v1.ts  | 109 ++++++++++++++++++
 .../src/converters/aws-cloudfront.ts          |  87 ++++++++++++++
 2 files changed, 196 insertions(+)
 create mode 100644 packages/open-next/src/converters/aws-apigw-v1.ts
 create mode 100644 packages/open-next/src/converters/aws-cloudfront.ts

diff --git a/packages/open-next/src/converters/aws-apigw-v1.ts b/packages/open-next/src/converters/aws-apigw-v1.ts
new file mode 100644
index 000000000..19411ef8d
--- /dev/null
+++ b/packages/open-next/src/converters/aws-apigw-v1.ts
@@ -0,0 +1,109 @@
+import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
+
+import { InternalEvent, InternalResult } from "../adapters/event-mapper";
+import { debug } from "../adapters/logger";
+import { Converter } from "../adapters/types/open-next";
+import { removeUndefinedFromQuery } from "./utils";
+
+function normalizeAPIGatewayProxyEventHeaders(
+  event: APIGatewayProxyEvent,
+): Record<string, string> {
+  event.multiValueHeaders;
+  const headers: Record<string, string> = {};
+
+  for (const [key, values] of Object.entries(event.multiValueHeaders || {})) {
+    if (values) {
+      headers[key.toLowerCase()] = values.join(",");
+    }
+  }
+  for (const [key, value] of Object.entries(event.headers || {})) {
+    if (value) {
+      headers[key.toLowerCase()] = value;
+    }
+  }
+  return headers;
+}
+
+function normalizeAPIGatewayProxyEventQueryParams(
+  event: APIGatewayProxyEvent,
+): string {
+  // Note that the same query string values are returned in both
+  // "multiValueQueryStringParameters" and "queryStringParameters".
+  // We only need to use one of them.
+  // For example:
+  //   "?name=foo" appears in the event object as
+  //   {
+  //     ...
+  //     queryStringParameters: { name: 'foo' },
+  //     multiValueQueryStringParameters: { name: [ 'foo' ] },
+  //     ...
+  //   }
+  const params = new URLSearchParams();
+  for (const [key, value] of Object.entries(
+    event.multiValueQueryStringParameters || {},
+  )) {
+    if (value !== undefined) {
+      for (const v of value) {
+        params.append(key, v);
+      }
+    }
+  }
+  const value = params.toString();
+  return value ? `?${value}` : "";
+}
+
+async function convertFromAPIGatewayProxyEvent(
+  event: APIGatewayProxyEvent,
+): Promise<InternalEvent> {
+  const { path, body, httpMethod, requestContext, isBase64Encoded } = event;
+  return {
+    type: "v1",
+    method: httpMethod,
+    rawPath: path,
+    url: path + normalizeAPIGatewayProxyEventQueryParams(event),
+    body: Buffer.from(body ?? "", isBase64Encoded ? "base64" : "utf8"),
+    headers: normalizeAPIGatewayProxyEventHeaders(event),
+    remoteAddress: requestContext.identity.sourceIp,
+    query: removeUndefinedFromQuery(
+      event.multiValueQueryStringParameters ?? {},
+    ),
+    cookies:
+      event.multiValueHeaders?.cookie?.reduce((acc, cur) => {
+        const [key, value] = cur.split("=");
+        return { ...acc, [key]: value };
+      }, {}) ?? {},
+  };
+}
+
+function convertToApiGatewayProxyResult(
+  result: InternalResult,
+): APIGatewayProxyResult {
+  const headers: Record<string, string> = {};
+  const multiValueHeaders: Record<string, string[]> = {};
+  Object.entries(result.headers).forEach(([key, value]) => {
+    if (Array.isArray(value)) {
+      multiValueHeaders[key] = value;
+    } else {
+      if (value === null) {
+        headers[key] = "";
+        return;
+      }
+      headers[key] = value;
+    }
+  });
+
+  const response: APIGatewayProxyResult = {
+    statusCode: result.statusCode,
+    headers,
+    body: result.body,
+    isBase64Encoded: result.isBase64Encoded,
+    multiValueHeaders,
+  };
+  debug(response);
+  return response;
+}
+
+export default {
+  convertFrom: convertFromAPIGatewayProxyEvent,
+  convertTo: convertToApiGatewayProxyResult,
+} as Converter;
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
new file mode 100644
index 000000000..f2560466b
--- /dev/null
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -0,0 +1,87 @@
+import {
+  CloudFrontHeaders,
+  CloudFrontRequestEvent,
+  CloudFrontRequestResult,
+} from "aws-lambda";
+
+import { InternalEvent, InternalResult } from "../adapters/event-mapper";
+import { debug } from "../adapters/logger";
+import { Converter } from "../adapters/types/open-next";
+
+function normalizeCloudFrontRequestEventHeaders(
+  rawHeaders: CloudFrontHeaders,
+): Record<string, string> {
+  const headers: Record<string, string> = {};
+
+  for (const [key, values] of Object.entries(rawHeaders)) {
+    for (const { value } of values) {
+      if (value) {
+        headers[key.toLowerCase()] = value;
+      }
+    }
+  }
+
+  return headers;
+}
+
+async function convertFromCloudFrontRequestEvent(
+  event: CloudFrontRequestEvent,
+): Promise<InternalEvent> {
+  const { method, uri, querystring, body, headers, clientIp } =
+    event.Records[0].cf.request;
+  return {
+    type: "cf",
+    method,
+    rawPath: uri,
+    url: uri + (querystring ? `?${querystring}` : ""),
+    body: Buffer.from(
+      body?.data ?? "",
+      body?.encoding === "base64" ? "base64" : "utf8",
+    ),
+    headers: normalizeCloudFrontRequestEventHeaders(headers),
+    remoteAddress: clientIp,
+    query: querystring.split("&").reduce(
+      (acc, cur) => ({
+        ...acc,
+        [cur.split("=")[0]]: cur.split("=")[1],
+      }),
+      {},
+    ),
+    cookies:
+      headers.cookie?.reduce((acc, cur) => {
+        const { key, value } = cur;
+        return { ...acc, [key ?? ""]: value };
+      }, {}) ?? {},
+  };
+}
+
+function convertToCloudFrontRequestResult(
+  result: InternalResult,
+): CloudFrontRequestResult {
+  const headers: CloudFrontHeaders = {};
+  Object.entries(result.headers)
+    .filter(([key]) => key.toLowerCase() !== "content-length")
+    .forEach(([key, value]) => {
+      headers[key] = [
+        ...(headers[key] || []),
+        ...(Array.isArray(value)
+          ? value.map((v) => ({ key, value: v }))
+          : [{ key, value: value.toString() }]),
+      ];
+    });
+
+  const response: CloudFrontRequestResult = {
+    status: result.statusCode.toString(),
+    statusDescription: "OK",
+    headers,
+    bodyEncoding: result.isBase64Encoded ? "base64" : "text",
+    body: result.body,
+  };
+  debug(response);
+  return response;
+}
+
+export default {
+  convertFrom: convertFromCloudFrontRequestEvent,
+  convertTo: convertToCloudFrontRequestResult,
+} as Converter;

From a9375ecb6cbaf33903067f98644af2a2145e6a71 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 8 Nov 2023 13:40:39 +0100
Subject: [PATCH 010/102] overridable queue

---
 .../src/adapters/plugins/routing/util.ts      | 33 +++++++++----------
 .../open-next/src/adapters/server-adapter.ts  |  2 ++
 .../open-next/src/adapters/types/open-next.ts |  5 +--
 .../open-next/src/core/createMainHandler.ts   | 17 ++++++++++
 packages/open-next/src/queue/sqs.ts           | 27 +++++++++++++++
 packages/open-next/src/queue/types.ts         | 12 +++++++
 6 files changed, 74 insertions(+), 22 deletions(-)
 create mode 100644 packages/open-next/src/queue/sqs.ts
 create mode 100644 packages/open-next/src/queue/types.ts

diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index 83cf84146..054cfb009 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -1,4 +1,3 @@
-import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
 import crypto from "crypto";
 import { OutgoingHttpHeaders, ServerResponse } from "http";
 
@@ -10,7 +9,7 @@ import {
 } from "../../http/openNextResponse.js";
 import { IncomingMessage } from "../../http/request.js";
 import { ServerlessResponse } from "../../http/response.js";
-import { awsLogger, debug } from "../../logger.js";
+import { debug } from "../../logger.js";
 
 declare global {
   var openNextDebug: boolean;
@@ -23,14 +22,6 @@ enum CommonHeaders {
   NEXT_CACHE = "x-nextjs-cache",
 }
 
-// Expected environment variables
-const { REVALIDATION_QUEUE_REGION, REVALIDATION_QUEUE_URL } = process.env;
-
-const sqsClient = new SQSClient({
-  region: REVALIDATION_QUEUE_REGION,
-  logger: awsLogger,
-});
-
 export async function proxyRequest(
   req: IncomingMessage,
   res: ServerlessResponse,
@@ -144,14 +135,20 @@ export async function revalidateIfRequired(
       const lastModified =
         globalThis.lastModified > 0 ? globalThis.lastModified : "";
 
-      await sqsClient.send(
-        new SendMessageCommand({
-          QueueUrl: REVALIDATION_QUEUE_URL,
-          MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
-          MessageBody: JSON.stringify({ host, url: revalidateUrl }),
-          MessageGroupId: generateMessageGroupId(rawPath),
-        }),
-      );
+      // await sqsClient.send(
+      //   new SendMessageCommand({
+      //     QueueUrl: REVALIDATION_QUEUE_URL,
+      //     MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
+      //     MessageBody: JSON.stringify({ host, url: revalidateUrl }),
+      //     MessageGroupId: generateMessageGroupId(rawPath),
+      //   }),
+      // );
+
+      await globalThis.queue.send({
+        MessageBody: { host, url: revalidateUrl },
+        MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
+        MessageGroupId: generateMessageGroupId(rawPath),
+      });
     } catch (e) {
       debug(`Failed to revalidate stale page ${rawPath}`);
       debug(e);
diff --git a/packages/open-next/src/adapters/server-adapter.ts b/packages/open-next/src/adapters/server-adapter.ts
index 23ea34bb5..fc9dc28ec 100644
--- a/packages/open-next/src/adapters/server-adapter.ts
+++ b/packages/open-next/src/adapters/server-adapter.ts
@@ -2,6 +2,7 @@ import { DynamoDBClient, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
 import { S3Client, S3ClientConfig } from "@aws-sdk/client-s3";
 
 import { createMainHandler } from "../core/createMainHandler.js";
+import { Queue } from "../queue/types.js";
 // We load every config here so that they are only loaded once
 // and during cold starts
 import { BuildId } from "./config/index.js";
@@ -21,6 +22,7 @@ setNextjsServerWorkingDirectory();
 declare global {
   var S3Client: S3Client;
   var dynamoClient: DynamoDBClient;
+  var queue: Queue;
 }
 
 const CACHE_BUCKET_REGION = process.env.CACHE_BUCKET_REGION;
diff --git a/packages/open-next/src/adapters/types/open-next.ts b/packages/open-next/src/adapters/types/open-next.ts
index 3838373a8..b07ddb359 100644
--- a/packages/open-next/src/adapters/types/open-next.ts
+++ b/packages/open-next/src/adapters/types/open-next.ts
@@ -1,3 +1,4 @@
+import { Queue } from "../../queue/types";
 import { InternalEvent, InternalResult } from "../event-mapper";
 import { StreamCreator } from "../http/openNextResponse";
 
@@ -45,10 +46,6 @@ export type TagCache = {
   writeTags(tags: { tag: string; path: string }): Promise<void>;
 };
 
-export type Queue = {
-  send(message: any): Promise<void>;
-};
-
 export interface OverrideOptions {
   /**
    * This is the main entrypoint of your app.
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index d64ad7385..13b26a572 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -34,12 +34,29 @@ async function resolveWrapper(
   }
 }
 
+async function resolveQueue(queue: OverrideOptions["queue"]) {
+  if (typeof queue === "string") {
+    const m = await import(`../queue/${queue}.js`);
+    return m.default;
+  } else if (typeof queue === "function") {
+    return queue();
+  } else {
+    const m_1 = await import("../queue/sqs.js");
+    return m_1.default;
+  }
+}
+
 export async function createMainHandler() {
   //First we load the config
   const config: BuildOptions = await import(
     process.cwd() + "/open-next.config.js"
   ).then((m) => m.default);
 
+  // Default queue
+  globalThis.queue = await resolveQueue(
+    config.functions.default.override?.queue,
+  );
+
   // From the config, we create the adapter
   const adapter = await resolveConverter(
     config.functions.default.override?.converter,
diff --git a/packages/open-next/src/queue/sqs.ts b/packages/open-next/src/queue/sqs.ts
new file mode 100644
index 000000000..cd3c90524
--- /dev/null
+++ b/packages/open-next/src/queue/sqs.ts
@@ -0,0 +1,27 @@
+import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
+
+import { awsLogger } from "../adapters/logger";
+import { Queue } from "./types";
+
+// Expected environment variables
+const { REVALIDATION_QUEUE_REGION, REVALIDATION_QUEUE_URL } = process.env;
+
+const sqsClient = new SQSClient({
+  region: REVALIDATION_QUEUE_REGION,
+  logger: awsLogger,
+});
+
+const queue: Queue = {
+  send: async ({ MessageBody, MessageDeduplicationId, MessageGroupId }) => {
+    await sqsClient.send(
+      new SendMessageCommand({
+        QueueUrl: REVALIDATION_QUEUE_URL,
+        MessageBody: JSON.stringify(MessageBody),
+        MessageDeduplicationId,
+        MessageGroupId,
+      }),
+    );
+  },
+};
+
+export default queue;
diff --git a/packages/open-next/src/queue/types.ts b/packages/open-next/src/queue/types.ts
new file mode 100644
index 000000000..4916b7f89
--- /dev/null
+++ b/packages/open-next/src/queue/types.ts
@@ -0,0 +1,12 @@
+export interface QueueMessage {
+  MessageDeduplicationId: string;
+  MessageBody: {
+    host: string;
+    url: string;
+  };
+  MessageGroupId: string;
+}
+
+export interface Queue {
+  send(message: QueueMessage): Promise<void>;
+}

From 423831a5dedc4f5f0e33f206a09770956821349e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 8 Nov 2023 15:05:27 +0100
Subject: [PATCH 011/102] overridable s3 cache

---
 packages/open-next/src/adapters/cache.ts      | 236 +++++-------------
 .../open-next/src/adapters/server-adapter.ts  |  21 +-
 .../open-next/src/cache/incremental/s3.ts     |  70 ++++++
 .../open-next/src/cache/incremental/types.ts  |  49 ++++
 packages/open-next/src/cache/next-types.ts    |  76 ++++++
 .../open-next/src/core/createMainHandler.ts   |  37 ++-
 6 files changed, 292 insertions(+), 197 deletions(-)
 create mode 100644 packages/open-next/src/cache/incremental/s3.ts
 create mode 100644 packages/open-next/src/cache/incremental/types.ts
 create mode 100644 packages/open-next/src/cache/next-types.ts

diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts
index 48e52d9a9..c73a481c7 100644
--- a/packages/open-next/src/adapters/cache.ts
+++ b/packages/open-next/src/adapters/cache.ts
@@ -3,18 +3,11 @@ import {
   DynamoDBClient,
   QueryCommand,
 } from "@aws-sdk/client-dynamodb";
-import {
-  DeleteObjectsCommand,
-  GetObjectCommand,
-  ListObjectsV2Command,
-  PutObjectCommand,
-  PutObjectCommandInput,
-  S3Client,
-} from "@aws-sdk/client-s3";
 import path from "path";
 
+import { IncrementalCache } from "../cache/incremental/types.js";
 import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "./constants.js";
-import { debug, error, warn } from "./logger.js";
+import { debug, error } from "./logger.js";
 import { chunk } from "./util.js";
 
 interface CachedFetchValue {
@@ -95,36 +88,6 @@ interface CacheHandlerValue {
   value: IncrementalCacheValue | null;
 }
 
-type Extension = "cache" | "fetch";
-
-interface Meta {
-  status?: number;
-  headers?: Record<string, undefined | string | string[]>;
-}
-type S3CachedFile =
-  | {
-      type: "redirect";
-      props?: Object;
-      meta?: Meta;
-    }
-  | {
-      type: "page";
-      html: string;
-      json: Object;
-      meta?: Meta;
-    }
-  | {
-      type: "app";
-      html: string;
-      rsc: string;
-      meta?: Meta;
-    }
-  | {
-      type: "route";
-      body: string;
-      meta?: Meta;
-    };
-
 /** Beginning single backslash is intentional, to look for the dot + the extension. Do not escape it again. */
 const CACHE_EXTENSION_REGEX = /\.(cache|fetch)$/;
 
@@ -133,15 +96,10 @@ export function hasCacheExtension(key: string) {
 }
 
 // Expected environment variables
-const {
-  CACHE_BUCKET_NAME,
-  CACHE_BUCKET_KEY_PREFIX,
-  CACHE_DYNAMO_TABLE,
-  NEXT_BUILD_ID,
-} = process.env;
+const { CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
 
 declare global {
-  var S3Client: S3Client;
+  var incrementalCache: IncrementalCache;
   var dynamoClient: DynamoDBClient;
   var disableDynamoDBCache: boolean;
   var disableIncrementalCache: boolean;
@@ -149,12 +107,12 @@ declare global {
 }
 
 export default class S3Cache {
-  private client: S3Client;
+  private client: IncrementalCache;
   private dynamoClient: DynamoDBClient;
   private buildId: string;
 
   constructor(_ctx: CacheHandlerContext) {
-    this.client = globalThis.S3Client;
+    this.client = globalThis.incrementalCache;
     this.dynamoClient = globalThis.dynamoClient;
     this.buildId = NEXT_BUILD_ID!;
   }
@@ -183,21 +141,19 @@ export default class S3Cache {
   async getFetchCache(key: string) {
     debug("get fetch cache", { key });
     try {
-      const { Body, LastModified } = await this.getS3Object(key, "fetch");
-      const lastModified = await this.getHasRevalidatedTags(
-        key,
-        LastModified?.getTime(),
-      );
-      if (lastModified === -1) {
+      const { value, lastModified } = await this.client.get(key, true);
+      // const { Body, LastModified } = await this.getS3Object(key, "fetch");
+      const _lastModified = await this.getHasRevalidatedTags(key, lastModified);
+      if (_lastModified === -1) {
         // If some tags are stale we need to force revalidation
         return null;
       }
 
-      if (Body === null) return null;
+      if (value === undefined) return null;
 
       return {
-        lastModified,
-        value: JSON.parse((await Body?.transformToString()) ?? "{}"),
+        lastModified: _lastModified,
+        value: value,
       } as CacheHandlerValue;
     } catch (e) {
       error("Failed to get fetch cache", e);
@@ -207,23 +163,24 @@ export default class S3Cache {
 
   async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> {
     try {
-      const { Body, LastModified } = await this.getS3Object(key, "cache");
-      const cacheData = JSON.parse(
-        (await Body?.transformToString()) ?? "{}",
-      ) as S3CachedFile;
-      const meta = cacheData.meta;
-      const lastModified = await this.getHasRevalidatedTags(
+      const { value: cacheData, lastModified } = await this.client.get(
         key,
-        LastModified?.getTime(),
+        false,
       );
-      if (lastModified === -1) {
+      // const { Body, LastModified } = await this.getS3Object(key, "cache");
+      // const cacheData = JSON.parse(
+      //   (await Body?.transformToString()) ?? "{}",
+      // ) as S3CachedFile;
+      const meta = cacheData?.meta;
+      const _lastModified = await this.getHasRevalidatedTags(key, lastModified);
+      if (_lastModified === -1) {
         // If some tags are stale we need to force revalidation
         return null;
       }
-      globalThis.lastModified = lastModified;
-      if (cacheData.type === "route") {
+      globalThis.lastModified = _lastModified;
+      if (cacheData?.type === "route") {
         return {
-          lastModified: LastModified?.getTime(),
+          lastModified: _lastModified,
           value: {
             kind: "ROUTE",
             body: Buffer.from(cacheData.body ?? Buffer.alloc(0)),
@@ -231,9 +188,9 @@ export default class S3Cache {
             headers: meta?.headers,
           },
         } as CacheHandlerValue;
-      } else if (cacheData.type === "page" || cacheData.type === "app") {
+      } else if (cacheData?.type === "page" || cacheData?.type === "app") {
         return {
-          lastModified: LastModified?.getTime(),
+          lastModified: _lastModified,
           value: {
             kind: "PAGE",
             html: cacheData.html,
@@ -243,9 +200,9 @@ export default class S3Cache {
             headers: meta?.headers,
           },
         } as CacheHandlerValue;
-      } else if (cacheData.type === "redirect") {
+      } else if (cacheData?.type === "redirect") {
         return {
-          lastModified: LastModified?.getTime(),
+          lastModified: _lastModified,
           value: {
             kind: "REDIRECT",
             props: cacheData.props,
@@ -271,46 +228,55 @@ export default class S3Cache {
     }
     if (data?.kind === "ROUTE") {
       const { body, status, headers } = data;
-      this.putS3Object(
+      await this.client.set(
         key,
-        "cache",
-        JSON.stringify({
+        {
           type: "route",
           body: body.toString("utf8"),
           meta: {
             status,
             headers,
           },
-        } as S3CachedFile),
+        },
+        false,
       );
     } else if (data?.kind === "PAGE") {
       const { html, pageData } = data;
       const isAppPath = typeof pageData === "string";
-      this.putS3Object(
-        key,
-        "cache",
-        JSON.stringify({
-          type: isAppPath ? "app" : "page",
-          html,
-          rsc: isAppPath ? pageData : undefined,
-          json: isAppPath ? undefined : pageData,
-          meta: { status: data.status, headers: data.headers },
-        } as S3CachedFile),
-      );
+      if (isAppPath) {
+        this.client.set(
+          key,
+          {
+            type: "app",
+            html,
+            rsc: pageData,
+          },
+          false,
+        );
+      } else {
+        this.client.set(
+          key,
+          {
+            type: "page",
+            html,
+            json: pageData,
+          },
+          false,
+        );
+      }
     } else if (data?.kind === "FETCH") {
-      await this.putS3Object(key, "fetch", JSON.stringify(data));
+      await this.client.set<true>(key, data, true);
     } else if (data?.kind === "REDIRECT") {
-      // // delete potential page data if we're redirecting
-      await this.putS3Object(
+      await this.client.set(
         key,
-        "cache",
-        JSON.stringify({
+        {
           type: "redirect",
           props: data.props,
-        } as S3CachedFile),
+        },
+        false,
       );
     } else if (data === null || data === undefined) {
-      await this.deleteS3Objects(key);
+      await this.client.delete(key);
     }
     // Write derivedTags to dynamodb
     // If we use an in house version of getDerivedTags in build we should use it here instead of next's one
@@ -474,86 +440,4 @@ export default class S3Cache {
       revalidatedAt: { N: `${Date.now()}` },
     };
   }
-
-  // S3 handling
-
-  private buildS3Key(key: string, extension: Extension) {
-    return path.posix.join(
-      CACHE_BUCKET_KEY_PREFIX ?? "",
-      extension === "fetch" ? "__fetch" : "",
-      this.buildId,
-      extension === "fetch" ? key : `${key}.${extension}`,
-    );
-  }
-
-  private buildS3KeyPrefix(key: string) {
-    return path.posix.join(CACHE_BUCKET_KEY_PREFIX ?? "", this.buildId, key);
-  }
-
-  private async listS3Object(key: string) {
-    const { Contents } = await this.client.send(
-      new ListObjectsV2Command({
-        Bucket: CACHE_BUCKET_NAME,
-        // add a point to the key so that it only matches the key and
-        // not other keys starting with the same string
-        Prefix: `${this.buildS3KeyPrefix(key)}.`,
-      }),
-    );
-    return (Contents ?? []).map(({ Key }) => Key) as string[];
-  }
-
-  private async getS3Object(key: string, extension: Extension) {
-    try {
-      const result = await this.client.send(
-        new GetObjectCommand({
-          Bucket: CACHE_BUCKET_NAME,
-          Key: this.buildS3Key(key, extension),
-        }),
-      );
-      return result;
-    } catch (e) {
-      warn("This error can usually be ignored : ", e);
-      return { Body: null, LastModified: null };
-    }
-  }
-
-  private putS3Object(
-    key: string,
-    extension: Extension,
-    value: PutObjectCommandInput["Body"],
-  ) {
-    return this.client.send(
-      new PutObjectCommand({
-        Bucket: CACHE_BUCKET_NAME,
-        Key: this.buildS3Key(key, extension),
-        Body: value,
-      }),
-    );
-  }
-
-  private async deleteS3Objects(key: string) {
-    try {
-      const s3Keys = (await this.listS3Object(key)).filter(
-        (key) => key && hasCacheExtension(key),
-      );
-
-      if (s3Keys.length === 0) {
-        warn(
-          `No s3 keys with a valid cache extension found for ${key}, see type CacheExtension in OpenNext for details`,
-        );
-        return;
-      }
-
-      await this.client.send(
-        new DeleteObjectsCommand({
-          Bucket: CACHE_BUCKET_NAME,
-          Delete: {
-            Objects: s3Keys.map((Key) => ({ Key })),
-          },
-        }),
-      );
-    } catch (e) {
-      error("Failed to delete cache", e);
-    }
-  }
 }
diff --git a/packages/open-next/src/adapters/server-adapter.ts b/packages/open-next/src/adapters/server-adapter.ts
index fc9dc28ec..3c13d3272 100644
--- a/packages/open-next/src/adapters/server-adapter.ts
+++ b/packages/open-next/src/adapters/server-adapter.ts
@@ -1,8 +1,7 @@
 import { DynamoDBClient, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
-import { S3Client, S3ClientConfig } from "@aws-sdk/client-s3";
+import { S3ClientConfig } from "@aws-sdk/client-s3";
 
 import { createMainHandler } from "../core/createMainHandler.js";
-import { Queue } from "../queue/types.js";
 // We load every config here so that they are only loaded once
 // and during cold starts
 import { BuildId } from "./config/index.js";
@@ -15,15 +14,9 @@ setNodeEnv();
 setBuildIdEnv();
 setNextjsServerWorkingDirectory();
 
-////////////////////////
-// AWS global clients //
-////////////////////////
-
-declare global {
-  var S3Client: S3Client;
-  var dynamoClient: DynamoDBClient;
-  var queue: Queue;
-}
+///////////////////////
+// AWS global client //
+///////////////////////
 
 const CACHE_BUCKET_REGION = process.env.CACHE_BUCKET_REGION;
 
@@ -59,8 +52,10 @@ function parseNumberFromEnv(envValue: string | undefined): number | undefined {
 //       cleared). It was causing some file to stay open which after enough time
 //       would cause the function to crash with error "EMFILE too many open". It
 //       was also making the memory grow out of control.
-globalThis.S3Client = new S3Client(parseS3ClientConfigFromEnv());
-globalThis.dynamoClient = new DynamoDBClient(parseDynamoClientConfigFromEnv());
+globalThis.dynamoClient = new DynamoDBClient({
+  region: CACHE_BUCKET_REGION,
+  logger: awsLogger,
+});
 
 /////////////
 // Handler //
diff --git a/packages/open-next/src/cache/incremental/s3.ts b/packages/open-next/src/cache/incremental/s3.ts
new file mode 100644
index 000000000..18855f0a1
--- /dev/null
+++ b/packages/open-next/src/cache/incremental/s3.ts
@@ -0,0 +1,70 @@
+import {
+  DeleteObjectCommand,
+  GetObjectCommand,
+  PutObjectCommand,
+  S3Client,
+} from "@aws-sdk/client-s3";
+import path from "path";
+
+import { awsLogger } from "../../adapters/logger";
+import { Extension } from "../next-types";
+import { IncrementalCache } from "./types";
+
+const {
+  CACHE_BUCKET_REGION,
+  CACHE_BUCKET_KEY_PREFIX,
+  NEXT_BUILD_ID,
+  CACHE_BUCKET_NAME,
+} = process.env;
+
+const s3Client = new S3Client({
+  region: CACHE_BUCKET_REGION,
+  logger: awsLogger,
+});
+
+function buildS3Key(key: string, extension: Extension) {
+  return path.posix.join(
+    CACHE_BUCKET_KEY_PREFIX ?? "",
+    extension === "fetch" ? "__fetch" : "",
+    NEXT_BUILD_ID ?? "",
+    extension === "fetch" ? key : `${key}.${extension}`,
+  );
+}
+
+const incrementalCache: IncrementalCache = {
+  async get(key, isFetch) {
+    const result = await s3Client.send(
+      new GetObjectCommand({
+        Bucket: CACHE_BUCKET_NAME,
+        Key: buildS3Key(key, isFetch ? "fetch" : "cache"),
+      }),
+    );
+
+    const cacheData = JSON.parse(
+      (await result.Body?.transformToString()) ?? "{}",
+    );
+    return {
+      value: cacheData,
+      lastModified: result.LastModified?.getTime(),
+    };
+  },
+  async set(key, value, isFetch): Promise<void> {
+    await s3Client.send(
+      new PutObjectCommand({
+        Bucket: CACHE_BUCKET_NAME,
+        Key: buildS3Key(key, isFetch ? "fetch" : "cache"),
+        Body: JSON.stringify(value),
+      }),
+    );
+  },
+  async delete(key): Promise<void> {
+    await s3Client.send(
+      new DeleteObjectCommand({
+        Bucket: CACHE_BUCKET_NAME,
+        Key: buildS3Key(key, "cache"),
+      }),
+    );
+  },
+};
+
+export default incrementalCache;
diff --git a/packages/open-next/src/cache/incremental/types.ts b/packages/open-next/src/cache/incremental/types.ts
new file mode 100644
index 000000000..c2a4468de
--- /dev/null
+++ b/packages/open-next/src/cache/incremental/types.ts
@@ -0,0 +1,49 @@
+import { Meta } from "../next-types";
+
+export type S3CachedFile =
+  | {
+      type: "redirect";
+      props?: Object;
+      meta?: Meta;
+    }
+  | {
+      type: "page";
+      html: string;
+      json: Object;
+      meta?: Meta;
+    }
+  | {
+      type: "app";
+      html: string;
+      rsc: string;
+      meta?: Meta;
+    }
+  | {
+      type: "route";
+      body: string;
+      meta?: Meta;
+    };
+
+export type S3FetchCache = Object;
+
+export type WithLastModified<T> = {
+  lastModified?: number;
+  value?: T;
+};
+
+export type CacheValue<IsFetch extends boolean> = IsFetch extends true
+  ? S3FetchCache
+  : S3CachedFile;
+
+export type IncrementalCache = {
+  get<IsFetch extends boolean = false>(
+    key: string,
+    isFetch?: IsFetch,
+  ): Promise<WithLastModified<CacheValue<IsFetch>>>;
+  set<IsFetch extends boolean = false>(
+    key: string,
+    value: CacheValue<IsFetch>,
+    isFetch?: IsFetch,
+  ): Promise<void>;
+  delete(key: string): Promise<void>;
+};
diff --git a/packages/open-next/src/cache/next-types.ts b/packages/open-next/src/cache/next-types.ts
new file mode 100644
index 000000000..58fe95652
--- /dev/null
+++ b/packages/open-next/src/cache/next-types.ts
@@ -0,0 +1,76 @@
+interface CachedFetchValue {
+  kind: "FETCH";
+  data: {
+    headers: { [k: string]: string };
+    body: string;
+    url: string;
+    status?: number;
+    tags?: string[];
+  };
+  revalidate: number;
+}
+
+interface CachedRedirectValue {
+  kind: "REDIRECT";
+  props: Object;
+}
+
+interface CachedRouteValue {
+  kind: "ROUTE";
+  // this needs to be a RenderResult so since renderResponse
+  // expects that type instead of a string
+  body: Buffer;
+  status: number;
+  headers: Record<string, undefined | string | string[]>;
+}
+
+interface CachedImageValue {
+  kind: "IMAGE";
+  etag: string;
+  buffer: Buffer;
+  extension: string;
+  isMiss?: boolean;
+  isStale?: boolean;
+}
+
+interface IncrementalCachedPageValue {
+  kind: "PAGE";
+  // this needs to be a string since the cache expects to store
+  // the string value
+  html: string;
+  pageData: Object;
+  status?: number;
+  headers?: Record<string, undefined | string>;
+}
+
+type IncrementalCacheValue =
+  | CachedRedirectValue
+  | IncrementalCachedPageValue
+  | CachedImageValue
+  | CachedFetchValue
+  | CachedRouteValue;
+
+export interface CacheHandlerContext {
+  fs?: never;
+  dev?: boolean;
+  flushToDisk?: boolean;
+  serverDistDir?: string;
+  maxMemoryCacheSize?: number;
+  _appDir: boolean;
+  _requestHeaders: never;
+  fetchCacheKeyPrefix?: string;
+}
+
+export interface CacheHandlerValue {
+  lastModified?: number;
+  age?: number;
+  cacheState?: string;
+  value: IncrementalCacheValue | null;
+}
+
+export type Extension = "cache" | "fetch";
+
+export interface Meta {
+  status?: number;
+  headers?: Record<string, undefined | string | string[]>;
+}
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 13b26a572..6852c6822 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -4,8 +4,15 @@ import {
   OverrideOptions,
   Wrapper,
 } from "../adapters/types/open-next";
+import type { IncrementalCache } from "../cache/incremental/types";
+import type { Queue } from "../queue/types";
 import { openNextHandler } from "./requestHandler";
 
+declare global {
+  var queue: Queue;
+  var incrementalCache: IncrementalCache;
+}
+
 async function resolveConverter(
   converter: OverrideOptions["converter"],
 ): Promise<Converter> {
@@ -46,26 +53,40 @@ async function resolveQueue(queue: OverrideOptions["queue"]) {
   }
 }
 
+async function resolveIncrementalCache(
+  incrementalCache: OverrideOptions["incrementalCache"],
+) {
+  if (typeof incrementalCache === "string") {
+    const m = await import(`../cache/incremental/${incrementalCache}.js`);
+    return m.default;
+  } else if (typeof incrementalCache === "function") {
+    return incrementalCache();
+  } else {
+    const m_1 = await import("../cache/incremental/s3.js");
+    return m_1.default;
+  }
+}
+
 export async function createMainHandler() {
   //First we load the config
   const config: BuildOptions = await import(
     process.cwd() + "/open-next.config.js"
   ).then((m) => m.default);
 
+  const thisFunction = config.functions.default;
+
   // Default queue
-  globalThis.queue = await resolveQueue(
-    config.functions.default.override?.queue,
+  globalThis.queue = await resolveQueue(thisFunction.override?.queue);
+
+  globalThis.incrementalCache = await resolveIncrementalCache(
+    thisFunction.override?.incrementalCache,
   );
 
   // From the config, we create the adapter
-  const adapter = await resolveConverter(
-    config.functions.default.override?.converter,
-  );
+  const adapter = await resolveConverter(thisFunction.override?.converter);
 
   // Then we create the handler
-  const wrapper = await resolveWrapper(
-    config.functions.default.override?.wrapper,
-  );
+  const wrapper = await resolveWrapper(thisFunction.override?.wrapper);
 
   return wrapper(openNextHandler, adapter);
 }

From 70d3d644a5a04a3efda2f405ac696fb52bd64957 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 8 Nov 2023 15:31:21 +0100
Subject: [PATCH 012/102] overridable tag cache

---
 packages/open-next/src/adapters/cache.ts      | 164 ++----------------
 .../open-next/src/adapters/server-adapter.ts  |  47 -----
 packages/open-next/src/adapters/util.ts       |  12 ++
 .../open-next/src/cache/incremental/s3.ts     |  15 +-
 packages/open-next/src/cache/tag/dynamoDb.ts  | 145 ++++++++++++++++
 packages/open-next/src/cache/tag/types.ts     |   6 +
 .../open-next/src/core/createMainHandler.ts   |  14 ++
 7 files changed, 205 insertions(+), 198 deletions(-)
 create mode 100644 packages/open-next/src/cache/tag/dynamoDb.ts
 create mode 100644 packages/open-next/src/cache/tag/types.ts

diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts
index c73a481c7..be1421c47 100644
--- a/packages/open-next/src/adapters/cache.ts
+++ b/packages/open-next/src/adapters/cache.ts
@@ -1,14 +1,6 @@
-import {
-  BatchWriteItemCommand,
-  DynamoDBClient,
-  QueryCommand,
-} from "@aws-sdk/client-dynamodb";
-import path from "path";
-
 import { IncrementalCache } from "../cache/incremental/types.js";
-import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "./constants.js";
-import { debug, error } from "./logger.js";
-import { chunk } from "./util.js";
+import { TagCache } from "../cache/tag/types.js";
+import { debug, error, warn } from "./logger.js";
 
 interface CachedFetchValue {
   kind: "FETCH";
@@ -95,12 +87,9 @@ export function hasCacheExtension(key: string) {
   return CACHE_EXTENSION_REGEX.test(key);
 }
 
-// Expected environment variables
-const { CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
-
 declare global {
   var incrementalCache: IncrementalCache;
-  var dynamoClient: DynamoDBClient;
+  var tagCache: TagCache;
   var disableDynamoDBCache: boolean;
   var disableIncrementalCache: boolean;
   var lastModified: number;
@@ -108,13 +97,11 @@ declare global {
 
 export default class S3Cache {
   private client: IncrementalCache;
-  private dynamoClient: DynamoDBClient;
-  private buildId: string;
+  private tagClient: TagCache;
 
   constructor(_ctx: CacheHandlerContext) {
     this.client = globalThis.incrementalCache;
-    this.dynamoClient = globalThis.dynamoClient;
-    this.buildId = NEXT_BUILD_ID!;
+    this.tagClient = globalThis.tagCache;
   }
 
   public async get(
@@ -143,7 +130,10 @@ export default class S3Cache {
     try {
       const { value, lastModified } = await this.client.get(key, true);
       // const { Body, LastModified } = await this.getS3Object(key, "fetch");
-      const _lastModified = await this.getHasRevalidatedTags(key, lastModified);
+      const _lastModified = await this.tagClient.getLastModified(
+        key,
+        lastModified,
+      );
       if (_lastModified === -1) {
         // If some tags are stale we need to force revalidation
         return null;
@@ -172,7 +162,10 @@ export default class S3Cache {
       //   (await Body?.transformToString()) ?? "{}",
       // ) as S3CachedFile;
       const meta = cacheData?.meta;
-      const _lastModified = await this.getHasRevalidatedTags(key, lastModified);
+      const _lastModified = await this.tagClient.getLastModified(
+        key,
+        lastModified,
+      );
       if (_lastModified === -1) {
         // If some tags are stale we need to force revalidation
         return null;
@@ -289,10 +282,10 @@ export default class S3Cache {
     debug("derivedTags", derivedTags);
     // Get all tags stored in dynamodb for the given key
     // If any of the derived tags are not stored in dynamodb for the given key, write them
-    const storedTags = await this.getTagsByPath(key);
+    const storedTags = await this.tagClient.getByPath(key);
     const tagsToWrite = derivedTags.filter((tag) => !storedTags.includes(tag));
     if (tagsToWrite.length > 0) {
-      await this.batchWriteDynamoItem(
+      await this.tagClient.writeTags(
         tagsToWrite.map((tag) => ({
           path: key,
           tag: tag,
@@ -307,137 +300,14 @@ export default class S3Cache {
     }
     debug("revalidateTag", tag);
     // Find all keys with the given tag
-    const paths = await this.getByTag(tag);
+    const paths = await this.tagClient.getByTag(tag);
     debug("Items", paths);
     // Update all keys with the given tag with revalidatedAt set to now
-    await this.batchWriteDynamoItem(
+    await this.tagClient.writeTags(
       paths?.map((path) => ({
         path: path,
         tag: tag,
       })) ?? [],
     );
   }
-
-  // DynamoDB handling
-
-  private async getTagsByPath(path: string) {
-    try {
-      if (disableDynamoDBCache) return [];
-      const result = await this.dynamoClient.send(
-        new QueryCommand({
-          TableName: CACHE_DYNAMO_TABLE,
-          IndexName: "revalidate",
-          KeyConditionExpression: "#key = :key",
-          ExpressionAttributeNames: {
-            "#key": "path",
-          },
-          ExpressionAttributeValues: {
-            ":key": { S: this.buildDynamoKey(path) },
-          },
-        }),
-      );
-      const tags = result.Items?.map((item) => item.tag.S ?? "") ?? [];
-      debug("tags for path", path, tags);
-      return tags;
-    } catch (e) {
-      error("Failed to get tags by path", e);
-      return [];
-    }
-  }
-
-  //TODO: Figure out a better name for this function since it returns the lastModified
-  private async getHasRevalidatedTags(key: string, lastModified?: number) {
-    try {
-      if (disableDynamoDBCache) return lastModified ?? Date.now();
-      const result = await this.dynamoClient.send(
-        new QueryCommand({
-          TableName: CACHE_DYNAMO_TABLE,
-          IndexName: "revalidate",
-          KeyConditionExpression:
-            "#key = :key AND #revalidatedAt > :lastModified",
-          ExpressionAttributeNames: {
-            "#key": "path",
-            "#revalidatedAt": "revalidatedAt",
-          },
-          ExpressionAttributeValues: {
-            ":key": { S: this.buildDynamoKey(key) },
-            ":lastModified": { N: String(lastModified ?? 0) },
-          },
-        }),
-      );
-      const revalidatedTags = result.Items ?? [];
-      debug("revalidatedTags", revalidatedTags);
-      // If we have revalidated tags we return -1 to force revalidation
-      return revalidatedTags.length > 0 ? -1 : lastModified ?? Date.now();
-    } catch (e) {
-      error("Failed to get revalidated tags", e);
-      return lastModified ?? Date.now();
-    }
-  }
-
-  private async getByTag(tag: string) {
-    try {
-      if (disableDynamoDBCache) return [];
-      const { Items } = await this.dynamoClient.send(
-        new QueryCommand({
-          TableName: CACHE_DYNAMO_TABLE,
-          KeyConditionExpression: "#tag = :tag",
-          ExpressionAttributeNames: {
-            "#tag": "tag",
-          },
-          ExpressionAttributeValues: {
-            ":tag": { S: this.buildDynamoKey(tag) },
-          },
-        }),
-      );
-      return (
-        // We need to remove the buildId from the path
-        Items?.map(
-          ({ path: { S: key } }) => key?.replace(`${this.buildId}/`, "") ?? "",
-        ) ?? []
-      );
-    } catch (e) {
-      error("Failed to get by tag", e);
-      return [];
-    }
-  }
-
-  private async batchWriteDynamoItem(req: { path: string; tag: string }[]) {
-    try {
-      if (disableDynamoDBCache) return;
-      await Promise.all(
-        chunk(req, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map((Items) => {
-          return this.dynamoClient.send(
-            new BatchWriteItemCommand({
-              RequestItems: {
-                [CACHE_DYNAMO_TABLE ?? ""]: Items.map((Item) => ({
-                  PutRequest: {
-                    Item: {
-                      ...this.buildDynamoObject(Item.path, Item.tag),
-                    },
-                  },
-                })),
-              },
-            }),
-          );
-        }),
-      );
-    } catch (e) {
-      error("Failed to batch write dynamo item", e);
-    }
-  }
-
-  private buildDynamoKey(key: string) {
-    // FIXME: We should probably use something else than path.join here
-    // this could transform some fetch cache key into a valid path
-    return path.posix.join(this.buildId, key);
-  }
-
-  private buildDynamoObject(path: string, tags: string) {
-    return {
-      path: { S: this.buildDynamoKey(path) },
-      tag: { S: this.buildDynamoKey(tags) },
-      revalidatedAt: { N: `${Date.now()}` },
-    };
-  }
 }
diff --git a/packages/open-next/src/adapters/server-adapter.ts b/packages/open-next/src/adapters/server-adapter.ts
index 3c13d3272..cd9108bd1 100644
--- a/packages/open-next/src/adapters/server-adapter.ts
+++ b/packages/open-next/src/adapters/server-adapter.ts
@@ -1,11 +1,7 @@
-import { DynamoDBClient, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
-import { S3ClientConfig } from "@aws-sdk/client-s3";
-
 import { createMainHandler } from "../core/createMainHandler.js";
 // We load every config here so that they are only loaded once
 // and during cold starts
 import { BuildId } from "./config/index.js";
-import { awsLogger } from "./logger.js";
 import { setNodeEnv } from "./util.js";
 
 // We load every config here so that they are only loaded once
@@ -14,49 +10,6 @@ setNodeEnv();
 setBuildIdEnv();
 setNextjsServerWorkingDirectory();
 
-///////////////////////
-// AWS global client //
-///////////////////////
-
-const CACHE_BUCKET_REGION = process.env.CACHE_BUCKET_REGION;
-
-function parseS3ClientConfigFromEnv(): S3ClientConfig {
-  return {
-    region: CACHE_BUCKET_REGION,
-    logger: awsLogger,
-    maxAttempts: parseNumberFromEnv(process.env.AWS_SDK_S3_MAX_ATTEMPTS),
-  };
-}
-
-function parseDynamoClientConfigFromEnv(): DynamoDBClientConfig {
-  return {
-    region: CACHE_BUCKET_REGION,
-    logger: awsLogger,
-    maxAttempts: parseNumberFromEnv(process.env.AWS_SDK_DYNAMODB_MAX_ATTEMPTS),
-  };
-}
-
-function parseNumberFromEnv(envValue: string | undefined): number | undefined {
-  if (typeof envValue !== "string") {
-    return envValue;
-  }
-
-  const parsedValue = parseInt(envValue);
-
-  return isNaN(parsedValue) ? undefined : parsedValue;
-}
-
-// Cache clients using global variables
-// Note: The clients are used in `cache.ts`. The incremental cache is recreated on
-//       every request and required on every request (And the require cache is also
-//       cleared). It was causing some file to stay open which after enough time
-//       would cause the function to crash with error "EMFILE too many open". It
-//       was also making the memory grow out of control.
-globalThis.dynamoClient = new DynamoDBClient({
-  region: CACHE_BUCKET_REGION,
-  logger: awsLogger,
-});
-
 /////////////
 // Handler //
 /////////////
diff --git a/packages/open-next/src/adapters/util.ts b/packages/open-next/src/adapters/util.ts
index aca13a2c8..62cd2d798 100644
--- a/packages/open-next/src/adapters/util.ts
+++ b/packages/open-next/src/adapters/util.ts
@@ -54,3 +54,15 @@ export function chunk<T>(items: T[], chunkSize: number): T[][] {
 
   return chunked;
 }
+
+export function parseNumberFromEnv(
+  envValue: string | undefined,
+): number | undefined {
+  if (typeof envValue !== "string") {
+    return envValue;
+  }
+
+  const parsedValue = parseInt(envValue);
+
+  return isNaN(parsedValue) ? undefined : parsedValue;
+}
diff --git a/packages/open-next/src/cache/incremental/s3.ts b/packages/open-next/src/cache/incremental/s3.ts
index 18855f0a1..592d4fead 100644
--- a/packages/open-next/src/cache/incremental/s3.ts
+++ b/packages/open-next/src/cache/incremental/s3.ts
@@ -3,10 +3,12 @@ import {
   GetObjectCommand,
   PutObjectCommand,
   S3Client,
+  S3ClientConfig,
 } from "@aws-sdk/client-s3";
 import path from "path";
 
 import { awsLogger } from "../../adapters/logger";
+import { parseNumberFromEnv } from "../../adapters/util";
 import { Extension } from "../next-types";
 import { IncrementalCache } from "./types";
 
@@ -17,10 +19,15 @@ const {
   CACHE_BUCKET_NAME,
 } = process.env;
 
-const s3Client = new S3Client({
-  region: CACHE_BUCKET_REGION,
-  logger: awsLogger,
-});
+function parseS3ClientConfigFromEnv(): S3ClientConfig {
+  return {
+    region: CACHE_BUCKET_REGION,
+    logger: awsLogger,
+    maxAttempts: parseNumberFromEnv(process.env.AWS_SDK_S3_MAX_ATTEMPTS),
+  };
+}
+
+const s3Client = new S3Client(parseS3ClientConfigFromEnv());
 
 function buildS3Key(key: string, extension: Extension) {
   return path.posix.join(
diff --git a/packages/open-next/src/cache/tag/dynamoDb.ts b/packages/open-next/src/cache/tag/dynamoDb.ts
new file mode 100644
index 000000000..27f24d9f0
--- /dev/null
+++ b/packages/open-next/src/cache/tag/dynamoDb.ts
@@ -0,0 +1,145 @@
+import {
+  BatchWriteItemCommand,
+  DynamoDBClient,
+  DynamoDBClientConfig,
+  QueryCommand,
+} from "@aws-sdk/client-dynamodb";
+import path from "path";
+
+import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "../../adapters/constants";
+import { awsLogger, debug, error } from "../../adapters/logger";
+import { chunk, parseNumberFromEnv } from "../../adapters/util";
+import { TagCache } from "./types";
+
+const { CACHE_BUCKET_REGION, CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
+
+function parseDynamoClientConfigFromEnv(): DynamoDBClientConfig {
+  return {
+    region: CACHE_BUCKET_REGION,
+    logger: awsLogger,
+    maxAttempts: parseNumberFromEnv(process.env.AWS_SDK_DYNAMODB_MAX_ATTEMPTS),
+  };
+}
+
+const dynamoClient = new DynamoDBClient(parseDynamoClientConfigFromEnv());
+
+function buildDynamoKey(key: string) {
+  // FIXME: We should probably use something else than path.join here
+  // this could transform some fetch cache key into a valid path
+  return path.posix.join(NEXT_BUILD_ID ?? "", key);
+}
+
+function buildDynamoObject(path: string, tags: string) {
+  return {
+    path: { S: buildDynamoKey(path) },
+    tag: { S: buildDynamoKey(tags) },
+    revalidatedAt: { N: `${Date.now()}` },
+  };
+}
+
+const tagCache: TagCache = {
+  async getByPath(path) {
+    try {
+      if (globalThis.disableDynamoDBCache) return [];
+      const result = await dynamoClient.send(
+        new QueryCommand({
+          TableName: CACHE_DYNAMO_TABLE,
+          IndexName: "revalidate",
+          KeyConditionExpression: "#key = :key",
+          ExpressionAttributeNames: {
+            "#key": "path",
+          },
+          ExpressionAttributeValues: {
+            ":key": { S: buildDynamoKey(path) },
+          },
+        }),
+      );
+      const tags = result.Items?.map((item) => item.tag.S ?? "") ?? [];
+      debug("tags for path", path, tags);
+      return tags;
+    } catch (e) {
+      error("Failed to get tags by path", e);
+      return [];
+    }
+  },
+  async getByTag(tag) {
+    try {
+      if (globalThis.disableDynamoDBCache) return [];
+      const { Items } = await dynamoClient.send(
+        new QueryCommand({
+          TableName: CACHE_DYNAMO_TABLE,
+          KeyConditionExpression: "#tag = :tag",
+          ExpressionAttributeNames: {
+            "#tag": "tag",
+          },
+          ExpressionAttributeValues: {
+            ":tag": { S: buildDynamoKey(tag) },
+          },
+        }),
+      );
+      return (
+        // We need to remove the buildId from the path
+        Items?.map(
+          ({ path: { S: key } }) => key?.replace(`${NEXT_BUILD_ID}/`, "") ?? "",
+        ) ?? []
+      );
+    } catch (e) {
+      error("Failed to get by tag", e);
+      return [];
+    }
+  },
+  async getLastModified(key, lastModified) {
+    try {
+      if (globalThis.disableDynamoDBCache) return lastModified ?? Date.now();
+      const result = await dynamoClient.send(
+        new QueryCommand({
+          TableName: CACHE_DYNAMO_TABLE,
+          IndexName: "revalidate",
+          KeyConditionExpression:
+            "#key = :key AND #revalidatedAt > :lastModified",
+          ExpressionAttributeNames: {
+            "#key": "path",
+            "#revalidatedAt": "revalidatedAt",
+          },
+          ExpressionAttributeValues: {
+            ":key": { S: buildDynamoKey(key) },
+            ":lastModified": { N: String(lastModified ?? 0) },
+          },
+        }),
+      );
+      const revalidatedTags = result.Items ?? [];
+      debug("revalidatedTags", revalidatedTags);
+      // If we have revalidated tags we return -1 to force revalidation
+      return revalidatedTags.length > 0 ? -1 : lastModified ?? Date.now();
+    } catch (e) {
+      error("Failed to get revalidated tags", e);
+      return lastModified ?? Date.now();
+    }
+  },
+  async writeTags(tags) {
+    try {
+      if (globalThis.disableDynamoDBCache) return;
+      await Promise.all(
+        chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map((Items) => {
+          return dynamoClient.send(
+            new BatchWriteItemCommand({
+              RequestItems: {
+                [CACHE_DYNAMO_TABLE ?? ""]: Items.map((Item) => ({
+                  PutRequest: {
+                    Item: {
+                      ...buildDynamoObject(Item.path, Item.tag),
+                    },
+                  },
+                })),
+              },
+            }),
+          );
+        }),
+      );
+    } catch (e) {
+      error("Failed to batch write dynamo item", e);
+    }
+  },
+};
+
+export default tagCache;
diff --git a/packages/open-next/src/cache/tag/types.ts b/packages/open-next/src/cache/tag/types.ts
new file mode 100644
index 000000000..564d17813
--- /dev/null
+++ b/packages/open-next/src/cache/tag/types.ts
@@ -0,0 +1,6 @@
+export type TagCache = {
+  getByTag(tag: string): Promise<string[]>;
+  getByPath(path: string): Promise<string[]>;
+  getLastModified(path: string, lastModified?: number): Promise<number>;
+  writeTags(tags: { tag: string; path: string }[]): Promise<void>;
+};
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 6852c6822..2986fd9dd 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -67,6 +67,18 @@ async function resolveIncrementalCache(
   }
 }
 
+async function resolveTagCache(tagCache: OverrideOptions["tagCache"]) {
+  if (typeof tagCache === "string") {
+    const m = await import(`../cache/tag/${tagCache}.js`);
+    return m.default;
+  } else if (typeof tagCache === "function") {
+    return tagCache();
+  } else {
+    const m_1 = await import("../cache/tag/dynamoDb.js");
+    return m_1.default;
+  }
+}
+
 export async function createMainHandler() {
   //First we load the config
   const config: BuildOptions = await import(
@@ -82,6 +94,8 @@ export async function createMainHandler() {
     thisFunction.override?.incrementalCache,
   );
 
+  globalThis.tagCache = await resolveTagCache(thisFunction.override?.tagCache);
+
   // From the config, we create the adapter
   const adapter = await resolveConverter(thisFunction.override?.converter);
 

From 6c10eedfc162efd5e49fc8ba9df908a0d2b27114 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 17 Nov 2023 11:45:48 +0100
Subject: [PATCH 013/102] prebuild middleware

---
 .../src/adapters/routing/middleware.ts        | 132 +++++++++++-------
 packages/open-next/src/build.ts               |  63 ++++++++-
 .../open-next/src/core/edgeFunctionHandler.ts | 110 +++++++++++++++
 3 files changed, 255 insertions(+), 50 deletions(-)
 create mode 100644 packages/open-next/src/core/edgeFunctionHandler.ts

diff --git a/packages/open-next/src/adapters/routing/middleware.ts b/packages/open-next/src/adapters/routing/middleware.ts
index 3cfff8186..4673eec75 100644
--- a/packages/open-next/src/adapters/routing/middleware.ts
+++ b/packages/open-next/src/adapters/routing/middleware.ts
@@ -2,10 +2,16 @@ import path from "node:path";
 
 import { NEXT_DIR, NextConfig } from "../config/index.js";
 import { InternalEvent, InternalResult } from "../event-mapper.js";
-import { OpenNextNodeResponse } from "../http/openNextResponse.js";
-import { IncomingMessage } from "../http/request.js";
+//NOTE: we should try to avoid importing stuff from next as much as possible
+// every release of next could break this
+// const { run } = require("next/dist/server/web/sandbox");
+// const { getCloneableBody } = require("next/dist/server/body-streams");
+// const {
+//   signalFromNodeResponse,
+// } = require("next/dist/server/web/spec-extension/adapters/next-request");
+// @ts-expect-error - This is bundled
+import middleware from "./middleware.mjs";
 import {
-  convertRes,
   convertToQueryString,
   getMiddlewareMatch,
   isExternal,
@@ -14,14 +20,6 @@ import {
 
 const middlewareManifest = loadMiddlewareManifest(NEXT_DIR);
 
-//NOTE: we should try to avoid importing stuff from next as much as possible
-// every release of next could break this
-const { run } = require("next/dist/server/web/sandbox");
-const { getCloneableBody } = require("next/dist/server/body-streams");
-const {
-  signalFromNodeResponse,
-} = require("next/dist/server/web/spec-extension/adapters/next-request");
-
 const middleMatch = getMiddlewareMatch(middlewareManifest);
 
 type MiddlewareOutputEvent = InternalEvent & {
@@ -48,11 +46,11 @@ export async function handleMiddleware(
   // We bypass the middleware if the request is internal
   if (internalEvent.headers["x-isr"]) return internalEvent;
 
-  const req = new IncomingMessage(internalEvent);
-  const res = new OpenNextNodeResponse(
-    () => void 0,
-    () => Promise.resolve(),
-  );
+  // const req = new IncomingMessage(internalEvent);
+  // const res = new OpenNextNodeResponse(
+  //   () => void 0,
+  //   () => Promise.resolve(),
+  // );
 
   // NOTE: Next middleware was originally developed to support nested middlewares
   // but that was discarded for simplicity. The MiddlewareInfo type still has the original
@@ -62,34 +60,65 @@ export async function handleMiddleware(
     path.join(NEXT_DIR, file),
   );
 
-  const host = req.headers.host
-    ? `https://${req.headers.host}`
+  const host = internalEvent.headers.host
+    ? `https://${internalEvent.headers.host}`
     : "http://localhost:3000";
   const initialUrl = new URL(rawPath, host);
   initialUrl.search = convertToQueryString(query);
   const url = initialUrl.toString();
 
-  const result: MiddlewareResult = await run({
-    distDir: NEXT_DIR,
-    name: middlewareInfo.name || "/",
-    paths: middlewareInfo.paths || [],
-    edgeFunctionEntry: middlewareInfo,
-    request: {
-      headers: req.headers,
-      method: req.method || "GET",
+  // const result: MiddlewareResult = await run({
+  //   distDir: NEXT_DIR,
+  //   name: middlewareInfo.name || "/",
+  //   paths: middlewareInfo.paths || [],
+  //   edgeFunctionEntry: middlewareInfo,
+  //   request: {
+  //     headers: req.headers,
+  //     method: req.method || "GET",
+  //     nextConfig: {
+  //       basePath: NextConfig.basePath,
+  //       i18n: NextConfig.i18n,
+  //       trailingSlash: NextConfig.trailingSlash,
+  //     },
+  //     url,
+  //     body: getCloneableBody(req),
+  //     signal: signalFromNodeResponse(res),
+  //   },
+  //   useCache: true,
+  //   onWarning: console.warn,
+  // });
+
+  const convertBodyToReadableStream = (body: string | Buffer) => {
+    const readable = new ReadableStream({
+      start(controller) {
+        controller.enqueue(body);
+        controller.close();
+      },
+    });
+    return readable;
+  };
+
+  const result: Response = await middleware(
+    [
+      {
+        name: middlewareInfo.name || "/",
+        page: middlewareInfo.page,
+        regex: middlewareInfo.matchers[0].regexp,
+      },
+    ],
+    {
+      headers: internalEvent.headers,
+      method: internalEvent.method || "GET",
       nextConfig: {
         basePath: NextConfig.basePath,
         i18n: NextConfig.i18n,
         trailingSlash: NextConfig.trailingSlash,
       },
       url,
-      body: getCloneableBody(req),
-      signal: signalFromNodeResponse(res),
+      body: convertBodyToReadableStream(internalEvent.body),
     },
-    useCache: true,
-    onWarning: console.warn,
-  });
-  res.statusCode = result.response.status;
+  );
+  const statusCode = result.status;
 
   /* Apply override headers from middleware
     NextResponse.next({
@@ -103,7 +132,7 @@ export async function handleMiddleware(
     We can delete `x-middleware-override-headers` and check if the key starts with
     x-middleware-request- to set the req headers
   */
-  const responseHeaders = result.response.headers as Headers;
+  const responseHeaders = result.headers as Headers;
   const reqHeaders: Record<string, string> = {};
   const resHeaders: Record<string, string> = {};
 
@@ -113,24 +142,27 @@ export async function handleMiddleware(
     if (key.startsWith(xMiddlewareKey)) {
       const k = key.substring(xMiddlewareKey.length);
       reqHeaders[k] = value;
-      req.headers[k] = value;
+      // req.headers[k] = value;
     } else {
       resHeaders[key] = value;
-      res.setHeader(key, value);
+      // res.setHeader(key, value);
     }
   });
 
   // If the middleware returned a Redirect, we set the `Location` header with
   // the redirected url and end the response.
-  if (res.statusCode >= 300 && res.statusCode < 400) {
-    const location = result.response.headers
+  if (statusCode >= 300 && statusCode < 400) {
+    const location = result.headers
       .get("location")
-      ?.replace("http://localhost:3000", `https://${req.headers.host}`);
+      ?.replace(
+        "http://localhost:3000",
+        `https://${internalEvent.headers.host}`,
+      );
     // res.setHeader("Location", location);
     return {
       body: "",
       type: internalEvent.type,
-      statusCode: res.statusCode,
+      statusCode: statusCode,
       headers: {
         ...resHeaders,
         Location: location ?? "",
@@ -145,14 +177,15 @@ export async function handleMiddleware(
   let rewritten = false;
   let externalRewrite = false;
   let middlewareQueryString = internalEvent.query;
+  let newUrl = internalEvent.url;
   if (rewriteUrl) {
-    if (isExternal(rewriteUrl, req.headers.host)) {
-      req.url = rewriteUrl;
+    if (isExternal(rewriteUrl, internalEvent.headers.host)) {
+      newUrl = rewriteUrl;
       rewritten = true;
       externalRewrite = true;
     } else {
       const rewriteUrlObject = new URL(rewriteUrl);
-      req.url = rewriteUrlObject.pathname;
+      newUrl = rewriteUrlObject.pathname;
       //reset qs
       middlewareQueryString = {};
       rewriteUrlObject.searchParams.forEach((v: string, k: string) => {
@@ -164,24 +197,27 @@ export async function handleMiddleware(
 
   // If the middleware returned a `NextResponse`, pipe the body to res. This will return
   // the body immediately to the client.
-  if (result.response.body) {
+  if (result.body) {
     // transfer response body to res
-    const arrayBuffer = await result.response.arrayBuffer();
+    const arrayBuffer = await result.arrayBuffer();
     const buffer = Buffer.from(arrayBuffer);
-    res.end(buffer);
+    // res.end(buffer);
 
     // await pipeReadable(result.response.body, res);
     return {
       type: internalEvent.type,
-      ...convertRes(res),
+      statusCode: statusCode,
+      headers: resHeaders,
+      body: buffer.toString(),
+      isBase64Encoded: false,
     };
   }
 
   return {
     responseHeaders: resHeaders,
-    url: req.url ?? internalEvent.url,
+    url: newUrl,
     rawPath: rewritten
-      ? req.url ?? internalEvent.rawPath
+      ? newUrl ?? internalEvent.rawPath
       : internalEvent.rawPath,
     type: internalEvent.type,
     headers: { ...internalEvent.headers, ...reqHeaders },
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index d52e33aa5..352b9a08d 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -1,5 +1,5 @@
 import cp from "node:child_process";
-import fs from "node:fs";
+import fs, { readFileSync } from "node:fs";
 import { createRequire as topLevelCreateRequire } from "node:module";
 import path from "node:path";
 import url from "node:url";
@@ -583,6 +583,9 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
     target: ["node18"],
   });
 
+  // Bundle middleware
+  createMiddleware();
+
   // Copy over standalone output files
   // note: if user uses pnpm as the package manager, node_modules contain
   //       symlinks. We don't want to resolve the symlinks when copying.
@@ -663,7 +666,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   }
   await esbuildAsync({
     entryPoints: [path.join(__dirname, "adapters", "server-adapter.js")],
-    external: ["next"],
+    external: ["next", "./middleware.mjs"],
     outfile: path.join(outputPath, packagePath, "index.mjs"),
     banner: {
       js: [
@@ -686,6 +689,62 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   addCacheHandler(outputPath, options.dangerous);
 }
 
+function createMiddleware() {
+  console.info(`Bundling middleware function...`);
+
+  const { appBuildOutputPath, outputDir } = options;
+
+  // Get middleware manifest
+  const middlewareManifest = JSON.parse(
+    readFileSync(
+      path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
+      "utf8",
+    ),
+  );
+
+  const entry = middlewareManifest.middleware["/"];
+
+  // Build edge function
+  buildEdgeFunction(
+    entry,
+    path.join(__dirname, "core", "edgeFunctionHandler.js"),
+    path.join(outputDir, "server-function", "middleware.mjs"),
+    appBuildOutputPath,
+  );
+}
+
+function buildEdgeFunction(
+  entry: any,
+  entrypoint: string,
+  outfile: string,
+  appBuildOutputPath: string,
+) {
+  esbuildSync({
+    entryPoints: [entrypoint],
+    inject: entry.files.map((file: string) =>
+      path.join(appBuildOutputPath, ".next", file),
+    ),
+    bundle: true,
+    outfile,
+    external: ["node:*"],
+    target: "es2022",
+    platform: "neutral",
+    banner: {
+      js: `
+globalThis._ENTRIES = {};
+globalThis.self = globalThis;
+
+import {Buffer} from "node:buffer";
+globalThis.Buffer = Buffer;
+
+import {AsyncLocalStorage} from "node:async_hooks";
+globalThis.AsyncLocalStorage = AsyncLocalStorage;
+
+`,
+    },
+  });
+}
+
 function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
   // Note: in the monorepo case, the handler file is output to
   //       `.next/standalone/package/path/index.mjs`, but we want
diff --git a/packages/open-next/src/core/edgeFunctionHandler.ts b/packages/open-next/src/core/edgeFunctionHandler.ts
new file mode 100644
index 000000000..4a5962f4d
--- /dev/null
+++ b/packages/open-next/src/core/edgeFunctionHandler.ts
@@ -0,0 +1,110 @@
+// Necessary files will be imported here with banner in esbuild
+
+import type { OutgoingHttpHeaders } from "http";
+
+interface RequestData {
+  geo?: {
+    city?: string;
+    country?: string;
+    region?: string;
+    latitude?: string;
+    longitude?: string;
+  };
+  headers: OutgoingHttpHeaders;
+  ip?: string;
+  method: string;
+  nextConfig?: {
+    basePath?: string;
+    i18n?: any;
+    trailingSlash?: boolean;
+  };
+  page?: {
+    name?: string;
+    params?: { [key: string]: string | string[] };
+  };
+  url: string;
+  body?: ReadableStream<Uint8Array>;
+  signal: AbortSignal;
+}
+
+interface Entries {
+  [k: string]: {
+    default: (props: { page: string; request: RequestData }) => Promise<{
+      response: Response;
+      waitUntil: Promise<void>;
+    }>;
+  };
+}
+declare global {
+  var _ENTRIES: Entries;
+  var __storage__: Map<unknown, unknown>;
+  var AsyncContext: any;
+  //@ts-ignore
+  var AsyncLocalStorage: any;
+}
+
+interface Route {
+  name: string;
+  page: string;
+  regex: string;
+}
+
+export default async function edgeFunctionHandler(
+  routes: Route[],
+  request: RequestData,
+): Promise<Response> {
+  const path = new URL(request.url).pathname;
+  const correspondingRoute = routes.find((route) =>
+    new RegExp(route.regex).test(path),
+  );
+
+  if (!correspondingRoute) {
+    throw new Error(`No route found for ${request.url}`);
+  }
+
+  const result = await self._ENTRIES[
+    `middleware_${correspondingRoute.name}`
+  ].default({
+    page: correspondingRoute.page,
+    request: {
+      headers: request.headers,
+      method: request.method,
+      url: request.url,
+      signal: request.signal,
+      page: {
+        name: correspondingRoute.name,
+      },
+    },
+  });
+  await result.waitUntil;
+  const response = result.response;
+  return response;
+}
+
+// const route = "/ssr/page";
+
+// const toExport = {
+//   async fetch(req: Request, env: any, context: any) {
+//     const headers: Record<string, string> = {};
+//     req.headers.forEach((value, key) => {
+//       headers[key] = value;
+//     });
+//     const result = await self._ENTRIES[`middleware_app${route}`].default({
+//       page: route,
+//       request: {
+//         headers: headers,
+//         method: req.method,
+//         url: req.url,
+//         signal: req.signal,
+//         page: {
+//           name: route,
+//         },
+//       },
+//     });
+//     const response = result.response;
+//     context.waitUntil(result.waitUntil);
+//     return response;
+//   },
+// };
+
+// export default toExport;

From 14dbaac5232fe39c97258ce57ce409590c5f721e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 18 Nov 2023 14:48:28 +0100
Subject: [PATCH 014/102] refactor routing and middleware

---
 .../adapters/plugins/13.5/serverHandler.ts    |   4 +-
 .../plugins/routing/default.replacement.ts    | 154 ------------------
 .../src/adapters/plugins/routing/default.ts   |  82 ----------
 .../src/adapters/plugins/routing/util.ts      |   3 +-
 .../plugins/serverHandler.replacement.ts      |   4 +-
 .../src/adapters/plugins/serverHandler.ts     |   4 +-
 .../src/adapters/types/next-types.ts          |   3 +-
 packages/open-next/src/converters/docker.ts   |   5 +-
 packages/open-next/src/core/requestHandler.ts |  74 +++++----
 .../src/{adapters => core}/routing/matcher.ts |  84 +++++-----
 .../{adapters => core}/routing/middleware.ts  |   8 +-
 .../src/{adapters => core}/routing/util.ts    |   8 +-
 packages/open-next/src/core/routingHandler.ts |  96 +++++++++++
 13 files changed, 199 insertions(+), 330 deletions(-)
 delete mode 100644 packages/open-next/src/adapters/plugins/routing/default.replacement.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/routing/default.ts
 rename packages/open-next/src/{adapters => core}/routing/matcher.ts (82%)
 rename packages/open-next/src/{adapters => core}/routing/middleware.ts (97%)
 rename packages/open-next/src/{adapters => core}/routing/util.ts (91%)
 create mode 100644 packages/open-next/src/core/routingHandler.ts

diff --git a/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts b/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
index dc7f98647..7ad3a75c2 100644
--- a/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
+++ b/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
@@ -1,18 +1,18 @@
 /*eslint-disable simple-import-sort/imports */
 import type { Options, PluginHandler } from "../../types/next-types.js";
 import type { IncomingMessage } from "../../http/request.js";
-import type { ServerlessResponse } from "../../http/response.js";
 //#override imports
 //@ts-ignore
 import { requestHandler } from "./util.js";
 //@ts-ignore
 import { proxyRequest } from "./routing/util.js";
+import { OpenNextNodeResponse } from "../../http/openNextResponse.js";
 //#endOverride
 
 //#override handler
 export const handler: PluginHandler = async (
   req: IncomingMessage,
-  res: ServerlessResponse,
+  res: OpenNextNodeResponse,
   options: Options,
 ) => {
   if (options.isExternalRewrite) {
diff --git a/packages/open-next/src/adapters/plugins/routing/default.replacement.ts b/packages/open-next/src/adapters/plugins/routing/default.replacement.ts
deleted file mode 100644
index a15e21473..000000000
--- a/packages/open-next/src/adapters/plugins/routing/default.replacement.ts
+++ /dev/null
@@ -1,154 +0,0 @@
-/* eslint-disable simple-import-sort/imports */
-import type {
-  PostProcessOptions,
-  ProcessInternalEvent,
-} from "../../types/plugin";
-import type { InternalResult } from "../../event-mapper";
-//#override imports
-
-import { debug } from "../../logger";
-import { IncomingMessage } from "../../http/request";
-import {
-  addNextConfigHeaders,
-  fixDataPage,
-  handleFallbackFalse,
-  handleRedirects,
-  handleRewrites,
-} from "../../routing/matcher";
-import {
-  addOpenNextHeader,
-  fixCacheHeaderForHtmlPages,
-  fixISRHeaders,
-  fixSWRCacheHeader,
-  revalidateIfRequired,
-} from "./util";
-import { convertRes } from "../../routing/util";
-import { handleMiddleware } from "../../routing/middleware";
-import {
-  BuildId,
-  ConfigHeaders,
-  PrerenderManifest,
-  RoutesManifest,
-} from "../../config";
-
-//#endOverride
-
-//#override processInternalEvent
-export const processInternalEvent: ProcessInternalEvent = async (
-  event,
-  createResponse,
-) => {
-  const nextHeaders = addNextConfigHeaders(event, ConfigHeaders) ?? {};
-
-  let internalEvent = fixDataPage(event, BuildId);
-
-  internalEvent = handleFallbackFalse(internalEvent, PrerenderManifest);
-
-  const redirect = handleRedirects(internalEvent, RoutesManifest.redirects);
-  if (redirect) {
-    return redirect;
-  }
-
-  const middleware = await handleMiddleware(internalEvent);
-  let middlewareResponseHeaders: Record<string, string | string[]> = {};
-  if ("statusCode" in middleware) {
-    return middleware;
-  } else {
-    middlewareResponseHeaders = middleware.responseHeaders || {};
-    internalEvent = middleware;
-  }
-
-  let isExternalRewrite = middleware.externalRewrite ?? false;
-  if (!isExternalRewrite) {
-    // First rewrite to be applied
-    const beforeRewrites = handleRewrites(
-      internalEvent,
-      RoutesManifest.rewrites.beforeFiles,
-    );
-    internalEvent = beforeRewrites.internalEvent;
-    isExternalRewrite = beforeRewrites.isExternalRewrite;
-  }
-  const isStaticRoute = RoutesManifest.routes.static.some((route) =>
-    new RegExp(route.regex).test(event.rawPath),
-  );
-
-  if (!isStaticRoute && !isExternalRewrite) {
-    // Second rewrite to be applied
-    const afterRewrites = handleRewrites(
-      internalEvent,
-      RoutesManifest.rewrites.afterFiles,
-    );
-    internalEvent = afterRewrites.internalEvent;
-    isExternalRewrite = afterRewrites.isExternalRewrite;
-  }
-
-  const isDynamicRoute = RoutesManifest.routes.dynamic.some((route) =>
-    new RegExp(route.regex).test(event.rawPath),
-  );
-  if (!isDynamicRoute && !isStaticRoute && !isExternalRewrite) {
-    // Fallback rewrite to be applied
-    const fallbackRewrites = handleRewrites(
-      internalEvent,
-      RoutesManifest.rewrites.fallback,
-    );
-    internalEvent = fallbackRewrites.internalEvent;
-    isExternalRewrite = fallbackRewrites.isExternalRewrite;
-  }
-
-  const reqProps = {
-    method: internalEvent.method,
-    url: internalEvent.url,
-    //WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
-    // There is 3 way we can handle revalidation:
-    // 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
-    // 2. We could alter the lastModified time of our cache to make next believe that the cache is fresh, but this could cause issues with stale data since the cdn will cache the stale data as if it was fresh
-    // 3. OUR CHOICE: We could pass a purpose prefetch header to the serverless function to make next believe that the request is a prefetch request and not trigger revalidation (This could potentially break in the future if next changes the behavior of prefetch requests)
-    headers: { ...internalEvent.headers, purpose: "prefetch" },
-    body: internalEvent.body,
-    remoteAddress: internalEvent.remoteAddress,
-  };
-  debug("IncomingMessage constructor props", reqProps);
-  const req = new IncomingMessage(reqProps);
-  const res = createResponse(reqProps.method, {
-    ...nextHeaders,
-    ...middlewareResponseHeaders,
-  });
-
-  return { internalEvent: internalEvent, req, res, isExternalRewrite };
-};
-//#endOverride
-
-//#override postProcessResponse
-export async function postProcessResponse({
-  internalEvent,
-  req,
-  res,
-  isExternalRewrite,
-}: PostProcessOptions): Promise<InternalResult> {
-  const { statusCode, headers, isBase64Encoded, body } = convertRes(res);
-
-  debug("ServerResponse data", { statusCode, headers, isBase64Encoded, body });
-
-  if (!isExternalRewrite) {
-    fixCacheHeaderForHtmlPages(internalEvent.rawPath, headers);
-    fixSWRCacheHeader(headers);
-    addOpenNextHeader(headers);
-    fixISRHeaders(headers);
-
-    await revalidateIfRequired(
-      internalEvent.headers.host,
-      internalEvent.rawPath,
-      headers,
-      req,
-    );
-  }
-
-  return {
-    type: internalEvent.type,
-    statusCode,
-    headers,
-    body,
-    isBase64Encoded,
-  };
-}
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/routing/default.ts b/packages/open-next/src/adapters/plugins/routing/default.ts
deleted file mode 100644
index 1b1c9e65e..000000000
--- a/packages/open-next/src/adapters/plugins/routing/default.ts
+++ /dev/null
@@ -1,82 +0,0 @@
-/* eslint-disable simple-import-sort/imports */
-import type {
-  CreateResponse,
-  PostProcessOptions,
-  ProcessInternalEventResult,
-} from "../../types/plugin";
-import type { InternalEvent, InternalResult } from "../../event-mapper";
-//#override imports
-import { debug } from "../../logger";
-import { IncomingMessage } from "../../http/request";
-import {
-  addOpenNextHeader,
-  fixCacheHeaderForHtmlPages,
-  fixISRHeaders,
-  fixSWRCacheHeader,
-  revalidateIfRequired,
-} from "./util";
-import { convertRes } from "../../routing/util";
-import { OpenNextNodeResponse } from "../../http/openNextResponse";
-//#endOverride
-
-//#override processInternalEvent
-export async function processInternalEvent<
-  Response extends OpenNextNodeResponse,
->(
-  internalEvent: InternalEvent,
-  createResponse: CreateResponse<Response>,
-): Promise<ProcessInternalEventResult<Response>> {
-  const reqProps = {
-    method: internalEvent.method,
-    url: internalEvent.url,
-    //WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
-    // There is 3 way we can handle revalidation:
-    // 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
-    // 2. We could alter the lastModified time of our cache to make next believe that the cache is fresh, but this could cause issues with stale data since the cdn will cache the stale data as if it was fresh
-    // 3. OUR CHOICE: We could pass a purpose prefetch header to the serverless function to make next believe that the request is a prefetch request and not trigger revalidation (This could potentially break in the future if next changes the behavior of prefetch requests)
-    headers: { ...internalEvent.headers, purpose: "prefetch" },
-    body: internalEvent.body,
-    remoteAddress: internalEvent.remoteAddress,
-  };
-  const req = new IncomingMessage(reqProps);
-  const res = createResponse(reqProps.method, {});
-  return { internalEvent, req, res, isExternalRewrite: false };
-}
-//#endOverride
-
-//#override postProcessResponse
-export async function postProcessResponse({
-  internalEvent,
-  req,
-  res,
-  isExternalRewrite,
-}: PostProcessOptions): Promise<InternalResult> {
-  const { statusCode, headers, isBase64Encoded, body } = convertRes(
-    res as OpenNextNodeResponse,
-  );
-
-  debug("ServerResponse data", { statusCode, headers, isBase64Encoded, body });
-
-  if (!isExternalRewrite) {
-    fixCacheHeaderForHtmlPages(internalEvent.rawPath, headers);
-    fixSWRCacheHeader(headers);
-    addOpenNextHeader(headers);
-    fixISRHeaders(headers);
-
-    await revalidateIfRequired(
-      internalEvent.headers.host,
-      internalEvent.rawPath,
-      headers,
-      req,
-    );
-  }
-
-  return {
-    type: internalEvent.type,
-    statusCode,
-    headers,
-    body,
-    isBase64Encoded,
-  };
-}
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index 054cfb009..7cf9cd7c3 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -8,7 +8,6 @@ import {
   StreamCreator,
 } from "../../http/openNextResponse.js";
 import { IncomingMessage } from "../../http/request.js";
-import { ServerlessResponse } from "../../http/response.js";
 import { debug } from "../../logger.js";
 
 declare global {
@@ -24,7 +23,7 @@ enum CommonHeaders {
 
 export async function proxyRequest(
   req: IncomingMessage,
-  res: ServerlessResponse,
+  res: OpenNextNodeResponse,
 ) {
   const HttpProxy = require("next/dist/compiled/http-proxy") as any;
 
diff --git a/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts b/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
index 1b311b0c1..a110609ee 100644
--- a/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
+++ b/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
@@ -1,17 +1,17 @@
 /*eslint-disable simple-import-sort/imports */
 import type { Options, PluginHandler } from "../types/next-types.js";
 import type { IncomingMessage } from "../http/request.js";
-import type { ServerlessResponse } from "../http/response.js";
 //#override imports
 
 import { proxyRequest } from "./routing/util.js";
 import { requestHandler, setNextjsPrebundledReact } from "./util.js";
+import { OpenNextNodeResponse } from "../http/openNextResponse.js";
 //#endOverride
 
 //#override handler
 export const handler: PluginHandler = async (
   req: IncomingMessage,
-  res: ServerlessResponse,
+  res: OpenNextNodeResponse,
   options: Options,
 ) => {
   let { internalEvent } = options;
diff --git a/packages/open-next/src/adapters/plugins/serverHandler.ts b/packages/open-next/src/adapters/plugins/serverHandler.ts
index 620e1e90a..62faf3326 100644
--- a/packages/open-next/src/adapters/plugins/serverHandler.ts
+++ b/packages/open-next/src/adapters/plugins/serverHandler.ts
@@ -1,5 +1,5 @@
+import type { OpenNextNodeResponse } from "../http/openNextResponse.js";
 import type { IncomingMessage } from "../http/request.js";
-import { ServerlessResponse } from "../http/response.js";
 import type { Options, PluginHandler } from "../types/next-types.js";
 //#override imports
 import { requestHandler, setNextjsPrebundledReact } from "./util.js";
@@ -8,7 +8,7 @@ import { requestHandler, setNextjsPrebundledReact } from "./util.js";
 //#override handler
 export const handler: PluginHandler = async (
   req: IncomingMessage,
-  res: ServerlessResponse,
+  res: OpenNextNodeResponse,
   options: Options,
 ) => {
   setNextjsPrebundledReact(options.internalEvent.rawPath);
diff --git a/packages/open-next/src/adapters/types/next-types.ts b/packages/open-next/src/adapters/types/next-types.ts
index 99312216f..a05cabcdb 100644
--- a/packages/open-next/src/adapters/types/next-types.ts
+++ b/packages/open-next/src/adapters/types/next-types.ts
@@ -1,6 +1,7 @@
 // NOTE: add more next config typings as they become relevant
 
 import { InternalEvent } from "../event-mapper.js";
+import { OpenNextNodeResponse } from "../http/openNextResponse.js";
 import { IncomingMessage } from "../http/request.js";
 import { ServerlessResponse } from "../http/response.js";
 
@@ -152,7 +153,7 @@ export type Options = {
 export interface PluginHandler {
   (
     req: IncomingMessage,
-    res: ServerlessResponse,
+    res: OpenNextNodeResponse,
     options: Options,
   ): Promise<ServerlessResponse | undefined>;
 }
diff --git a/packages/open-next/src/converters/docker.ts b/packages/open-next/src/converters/docker.ts
index e433ddf6b..c2278673a 100644
--- a/packages/open-next/src/converters/docker.ts
+++ b/packages/open-next/src/converters/docker.ts
@@ -32,7 +32,10 @@ const converter: Converter = {
           ])
           .filter(([key]) => key),
       ),
-      remoteAddress: req.headers["x-forwarded-for"] as string,
+      remoteAddress:
+        (req.headers["x-forwarded-for"] as string) ??
+        req.socket.remoteAddress ??
+        "::1",
       query,
       cookies: Object.fromEntries(
         parseCookies(req.headers["cookie"])?.map((cookie) => {
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 67fe92afa..7d9556804 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -1,14 +1,15 @@
 import { BuildId } from "../adapters/config";
 import { InternalEvent, InternalResult } from "../adapters/event-mapper";
-import { IncomingMessage, ServerlessResponse } from "../adapters/http";
-import { StreamCreator } from "../adapters/http/openNextResponse";
-import { error } from "../adapters/logger";
+import { IncomingMessage } from "../adapters/http";
 import {
-  postProcessResponse,
-  processInternalEvent,
-} from "../adapters/plugins/routing/default";
+  OpenNextNodeResponse,
+  StreamCreator,
+} from "../adapters/http/openNextResponse";
+import { error } from "../adapters/logger";
 import { createServerResponse } from "../adapters/plugins/routing/util";
 import { handler as serverHandler } from "../adapters/plugins/serverHandler";
+import { convertRes } from "./routing/util";
+import routingHandler from "./routingHandler";
 
 export async function openNextHandler(
   internalEvent: InternalEvent,
@@ -17,18 +18,9 @@ export async function openNextHandler(
   if (internalEvent.headers["x-forwarded-host"]) {
     internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
   }
-  const preprocessResult = await processInternalEvent(
-    internalEvent,
-    (method, headers) =>
-      createServerResponse(
-        internalEvent,
-        {
-          ...headers,
-          "accept-encoding": internalEvent.headers["accept-encoding"],
-        },
-        responseStreaming,
-      ),
-  );
+
+  //TODO: replace this line for next <= 13.4.12
+  const preprocessResult = await routingHandler(internalEvent);
 
   if ("type" in preprocessResult) {
     // res is used only in the streaming case
@@ -40,22 +32,42 @@ export async function openNextHandler(
     res.end();
     return preprocessResult;
   } else {
-    const {
+    const preprocessedEvent = preprocessResult.internalEvent;
+    const reqProps = {
+      method: preprocessedEvent.method,
+      url: preprocessedEvent.url,
+      //WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
+      // There is 3 way we can handle revalidation:
+      // 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
+      // 2. We could alter the lastModified time of our cache to make next believe that the cache is fresh, but this could cause issues with stale data since the cdn will cache the stale data as if it was fresh
+      // 3. OUR CHOICE: We could pass a purpose prefetch header to the serverless function to make next believe that the request is a prefetch request and not trigger revalidation (This could potentially break in the future if next changes the behavior of prefetch requests)
+      headers: { ...preprocessedEvent.headers, purpose: "prefetch" },
+      body: preprocessedEvent.body,
+      remoteAddress: preprocessedEvent.remoteAddress,
+    };
+    const req = new IncomingMessage(reqProps);
+    const res = createServerResponse(
+      preprocessedEvent,
+      preprocessResult.headers as any,
+      responseStreaming,
+    );
+
+    await processRequest(
       req,
       res,
-      isExternalRewrite,
-      internalEvent: overwrittenInternalEvent,
-    } = preprocessResult;
+      preprocessedEvent,
+      preprocessResult.isExternalRewrite,
+    );
 
-    // @ts-ignore
-    await processRequest(req, res, overwrittenInternalEvent, isExternalRewrite);
+    const { statusCode, headers, isBase64Encoded, body } = convertRes(res);
 
-    const internalResult = await postProcessResponse({
-      internalEvent: overwrittenInternalEvent,
-      req,
-      res,
-      isExternalRewrite,
-    });
+    const internalResult = {
+      type: internalEvent.type,
+      statusCode,
+      headers,
+      body,
+      isBase64Encoded,
+    };
 
     return internalResult;
   }
@@ -63,7 +75,7 @@ export async function openNextHandler(
 
 async function processRequest(
   req: IncomingMessage,
-  res: ServerlessResponse,
+  res: OpenNextNodeResponse,
   internalEvent: InternalEvent,
   isExternalRewrite?: boolean,
 ) {
diff --git a/packages/open-next/src/adapters/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
similarity index 82%
rename from packages/open-next/src/adapters/routing/matcher.ts
rename to packages/open-next/src/core/routing/matcher.ts
index 32a910d92..3eedbb8cf 100644
--- a/packages/open-next/src/adapters/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -1,16 +1,16 @@
 import { compile, Match, match, PathFunction } from "path-to-regexp";
 
-import { NextConfig } from "../config";
-import { InternalEvent, InternalResult } from "../event-mapper";
-import { debug } from "../logger";
+import { NextConfig } from "../../adapters/config";
+import { InternalEvent, InternalResult } from "../../adapters/event-mapper";
+import { debug } from "../../adapters/logger";
 import {
   Header,
   PrerenderManifest,
   RedirectDefinition,
   RewriteDefinition,
   RouteHas,
-} from "../types/next-types";
-import { escapeRegex, unescapeRegex } from "../util";
+} from "../../adapters/types/next-types";
+import { escapeRegex, unescapeRegex } from "../../adapters/util";
 import { convertToQueryString, getUrlParts, isExternal } from "./util";
 
 const routeHasMatcher =
@@ -168,44 +168,42 @@ export function handleRedirects(
   event: InternalEvent,
   redirects: RedirectDefinition[],
 ): InternalResult | undefined {
-  if (!NextConfig.skipTrailingSlashRedirect) {
-    if (
-      NextConfig.trailingSlash &&
-      !event.headers["x-nextjs-data"] &&
-      !event.rawPath.endsWith("/") &&
-      !event.rawPath.match(/[\w-]+\.[\w]+$/g)
-    ) {
-      const headersLocation = event.url.split("?");
-      return {
-        type: event.type,
-        statusCode: 308,
-        headers: {
-          Location: `${headersLocation[0]}/${
-            headersLocation[1] ? `?${headersLocation[1]}` : ""
-          }`,
-        },
-        body: "",
-        isBase64Encoded: false,
-      };
-      // eslint-disable-next-line sonarjs/elseif-without-else
-    } else if (
-      !NextConfig.trailingSlash &&
-      event.rawPath.endsWith("/") &&
-      event.rawPath !== "/"
-    ) {
-      const headersLocation = event.url.split("?");
-      return {
-        type: event.type,
-        statusCode: 308,
-        headers: {
-          Location: `${headersLocation[0].replace(/\/$/, "")}${
-            headersLocation[1] ? `?${headersLocation[1]}` : ""
-          }`,
-        },
-        body: "",
-        isBase64Encoded: false,
-      };
-    }
+  if (
+    NextConfig.trailingSlash &&
+    !event.headers["x-nextjs-data"] &&
+    !event.rawPath.endsWith("/") &&
+    !event.rawPath.match(/[\w-]+\.[\w]+$/g)
+  ) {
+    const headersLocation = event.url.split("?");
+    return {
+      type: event.type,
+      statusCode: 308,
+      headers: {
+        Location: `${headersLocation[0]}/${
+          headersLocation[1] ? `?${headersLocation[1]}` : ""
+        }`,
+      },
+      body: "",
+      isBase64Encoded: false,
+    };
+    // eslint-disable-next-line sonarjs/elseif-without-else
+  } else if (
+    !NextConfig.trailingSlash &&
+    event.rawPath.endsWith("/") &&
+    event.rawPath !== "/"
+  ) {
+    const headersLocation = event.url.split("?");
+    return {
+      type: event.type,
+      statusCode: 308,
+      headers: {
+        Location: `${headersLocation[0].replace(/\/$/, "")}${
+          headersLocation[1] ? `?${headersLocation[1]}` : ""
+        }`,
+      },
+      body: "",
+      isBase64Encoded: false,
+    };
   }
   const { internalEvent, __rewrite } = handleRewrites(
     event,
diff --git a/packages/open-next/src/adapters/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
similarity index 97%
rename from packages/open-next/src/adapters/routing/middleware.ts
rename to packages/open-next/src/core/routing/middleware.ts
index 4673eec75..e2fc1cd3c 100644
--- a/packages/open-next/src/adapters/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -1,7 +1,7 @@
 import path from "node:path";
 
-import { NEXT_DIR, NextConfig } from "../config/index.js";
-import { InternalEvent, InternalResult } from "../event-mapper.js";
+import { NEXT_DIR, NextConfig } from "../../adapters/config/index.js";
+import { InternalEvent, InternalResult } from "../../adapters/event-mapper.js";
 //NOTE: we should try to avoid importing stuff from next as much as possible
 // every release of next could break this
 // const { run } = require("next/dist/server/web/sandbox");
@@ -32,10 +32,6 @@ type MiddlewareOutputEvent = InternalEvent & {
 // and res.body prior to processing the next-server.
 // @returns undefined | res.end()
 
-interface MiddlewareResult {
-  response: Response;
-}
-
 //    if res.end() is return, the parent needs to return and not process next server
 export async function handleMiddleware(
   internalEvent: InternalEvent,
diff --git a/packages/open-next/src/adapters/routing/util.ts b/packages/open-next/src/core/routing/util.ts
similarity index 91%
rename from packages/open-next/src/adapters/routing/util.ts
rename to packages/open-next/src/core/routing/util.ts
index 5c8bd5d0b..bc7afe5e3 100644
--- a/packages/open-next/src/adapters/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -1,10 +1,10 @@
 import fs from "node:fs";
 import path from "node:path";
 
-import { isBinaryContentType } from "../binary";
-import { OpenNextNodeResponse } from "../http/openNextResponse";
-import { parseHeaders } from "../http/util";
-import { MiddlewareManifest } from "../types/next-types";
+import { isBinaryContentType } from "../../adapters/binary";
+import { OpenNextNodeResponse } from "../../adapters/http/openNextResponse";
+import { parseHeaders } from "../../adapters/http/util";
+import { MiddlewareManifest } from "../../adapters/types/next-types";
 
 export function isExternal(url?: string, host?: string) {
   if (!url) return false;
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
new file mode 100644
index 000000000..fc0d0a981
--- /dev/null
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -0,0 +1,96 @@
+import type { OutgoingHttpHeaders } from "http";
+
+//TODO: Replace this with injected values in case of external routing
+// We could use open-next plugins to replace these values at build time
+import {
+  BuildId,
+  ConfigHeaders,
+  PrerenderManifest,
+  RoutesManifest,
+} from "../adapters/config";
+import { InternalEvent, InternalResult } from "../adapters/event-mapper";
+import {
+  addNextConfigHeaders,
+  fixDataPage,
+  handleFallbackFalse,
+  handleRedirects,
+  handleRewrites,
+} from "./routing/matcher";
+import { handleMiddleware } from "./routing/middleware";
+
+export interface MiddlewareOutputEvent {
+  internalEvent: InternalEvent;
+  headers: OutgoingHttpHeaders;
+  isExternalRewrite: boolean;
+}
+
+export default async function routingHandler(
+  event: InternalEvent,
+): Promise<InternalResult | MiddlewareOutputEvent> {
+  const nextHeaders = addNextConfigHeaders(event, ConfigHeaders) ?? {};
+
+  let internalEvent = fixDataPage(event, BuildId);
+
+  internalEvent = handleFallbackFalse(internalEvent, PrerenderManifest);
+
+  const redirect = handleRedirects(internalEvent, RoutesManifest.redirects);
+  if (redirect) {
+    console.log("redirect", redirect);
+    return redirect;
+  }
+
+  const middleware = await handleMiddleware(internalEvent);
+  let middlewareResponseHeaders: Record<string, string | string[]> = {};
+  if ("statusCode" in middleware) {
+    return middleware;
+  } else {
+    middlewareResponseHeaders = middleware.responseHeaders || {};
+    internalEvent = middleware;
+  }
+
+  let isExternalRewrite = middleware.externalRewrite ?? false;
+  if (!isExternalRewrite) {
+    // First rewrite to be applied
+    const beforeRewrites = handleRewrites(
+      internalEvent,
+      RoutesManifest.rewrites.beforeFiles,
+    );
+    internalEvent = beforeRewrites.internalEvent;
+    isExternalRewrite = beforeRewrites.isExternalRewrite;
+  }
+  const isStaticRoute = RoutesManifest.routes.static.some((route) =>
+    new RegExp(route.regex).test(event.rawPath),
+  );
+
+  if (!isStaticRoute && !isExternalRewrite) {
+    // Second rewrite to be applied
+    const afterRewrites = handleRewrites(
+      internalEvent,
+      RoutesManifest.rewrites.afterFiles,
+    );
+    internalEvent = afterRewrites.internalEvent;
+    isExternalRewrite = afterRewrites.isExternalRewrite;
+  }
+
+  const isDynamicRoute = RoutesManifest.routes.dynamic.some((route) =>
+    new RegExp(route.regex).test(event.rawPath),
+  );
+  if (!isDynamicRoute && !isStaticRoute && !isExternalRewrite) {
+    // Fallback rewrite to be applied
+    const fallbackRewrites = handleRewrites(
+      internalEvent,
+      RoutesManifest.rewrites.fallback,
+    );
+    internalEvent = fallbackRewrites.internalEvent;
+    isExternalRewrite = fallbackRewrites.isExternalRewrite;
+  }
+
+  return {
+    internalEvent,
+    headers: {
+      ...nextHeaders,
+      ...middlewareResponseHeaders,
+    },
+    isExternalRewrite,
+  };
+}

From 5199eb9d0fc24eb9666574196c2c10b84137b708 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Mon, 20 Nov 2023 16:50:37 +0100
Subject: [PATCH 015/102] big refactoring

moved files around so that it makes more sense
deleted a bunch of useless files
added todo to remind myself of what i still need to do
---
 .../open-next/src/adapters/config/util.ts     |   6 +-
 .../open-next/src/adapters/event-mapper.ts    | 333 ------------------
 packages/open-next/src/adapters/http/index.ts |   3 -
 .../open-next/src/adapters/http/response.ts   | 150 --------
 .../src/adapters/http/responseStreaming.ts    | 263 --------------
 packages/open-next/src/adapters/http/util.ts  |  50 ---
 .../adapters/image-optimization-adapter.ts    |   2 +-
 .../adapters/plugins/13.5/serverHandler.ts    |   5 +-
 .../src/adapters/plugins/routing/util.ts      |  11 +-
 .../plugins/serverHandler.replacement.ts      |   5 +-
 .../src/adapters/plugins/serverHandler.ts     |   9 +-
 .../src/adapters/plugins/util.replacement.ts  |   2 +-
 .../open-next/src/adapters/plugins/util.ts    |  10 +-
 .../open-next/src/adapters/require-hooks.ts   |   3 +-
 .../open-next/src/adapters/server-adapter.ts  |   5 +-
 packages/open-next/src/adapters/util.ts       |  35 +-
 packages/open-next/src/build.ts               |   3 +-
 .../open-next/src/converters/aws-apigw-v1.ts  |   3 +-
 .../open-next/src/converters/aws-apigw-v2.ts  |   8 +-
 .../src/converters/aws-cloudfront.ts          |  12 +-
 packages/open-next/src/converters/docker.ts   |   6 +-
 .../open-next/src/core/createMainHandler.ts   |   5 +-
 packages/open-next/src/core/requestHandler.ts |  11 +-
 .../open-next/src/core/routing/matcher.ts     |  21 +-
 .../open-next/src/core/routing/middleware.ts  |   5 +-
 packages/open-next/src/core/routing/util.ts   |  29 +-
 packages/open-next/src/core/routingHandler.ts |   8 +-
 packages/open-next/src/http/index.ts          |   2 +
 .../{adapters => }/http/openNextResponse.ts   |   3 +-
 .../src/{adapters => }/http/request.ts        |   0
 packages/open-next/src/http/util.ts           |  42 +++
 .../src/{adapters => }/types/aws-lambda.ts    |   0
 .../src/{adapters => }/types/next-types.ts    |   9 +-
 .../src/{adapters => }/types/open-next.ts     |  26 +-
 .../src/{adapters => }/types/plugin.ts        |   6 +-
 .../src/wrappers/aws-lambda-streaming.ts      |   7 +-
 packages/open-next/src/wrappers/aws-lambda.ts |   4 +-
 packages/open-next/src/wrappers/docker.ts     |   4 +-
 packages/open-next/tsconfig.json              |   7 +-
 39 files changed, 186 insertions(+), 927 deletions(-)
 delete mode 100644 packages/open-next/src/adapters/event-mapper.ts
 delete mode 100644 packages/open-next/src/adapters/http/index.ts
 delete mode 100644 packages/open-next/src/adapters/http/response.ts
 delete mode 100644 packages/open-next/src/adapters/http/responseStreaming.ts
 delete mode 100644 packages/open-next/src/adapters/http/util.ts
 create mode 100644 packages/open-next/src/http/index.ts
 rename packages/open-next/src/{adapters => }/http/openNextResponse.ts (97%)
 rename packages/open-next/src/{adapters => }/http/request.ts (100%)
 create mode 100644 packages/open-next/src/http/util.ts
 rename packages/open-next/src/{adapters => }/types/aws-lambda.ts (100%)
 rename packages/open-next/src/{adapters => }/types/next-types.ts (91%)
 rename packages/open-next/src/{adapters => }/types/open-next.ts (86%)
 rename packages/open-next/src/{adapters => }/types/plugin.ts (81%)

diff --git a/packages/open-next/src/adapters/config/util.ts b/packages/open-next/src/adapters/config/util.ts
index b0455ed53..309328119 100644
--- a/packages/open-next/src/adapters/config/util.ts
+++ b/packages/open-next/src/adapters/config/util.ts
@@ -1,12 +1,12 @@
 import fs from "fs";
 import path from "path";
-
-import { PublicFiles } from "../../build";
 import {
   NextConfig,
   PrerenderManifest,
   RoutesManifest,
-} from "../types/next-types";
+} from "types/next-types";
+
+import { PublicFiles } from "../../build";
 
 export function loadConfig(nextDir: string) {
   const filePath = path.join(nextDir, "required-server-files.json");
diff --git a/packages/open-next/src/adapters/event-mapper.ts b/packages/open-next/src/adapters/event-mapper.ts
deleted file mode 100644
index 15c632d79..000000000
--- a/packages/open-next/src/adapters/event-mapper.ts
+++ /dev/null
@@ -1,333 +0,0 @@
-import type {
-  APIGatewayProxyEvent,
-  APIGatewayProxyEventV2,
-  APIGatewayProxyResult,
-  APIGatewayProxyResultV2,
-  CloudFrontHeaders,
-  CloudFrontRequestEvent,
-  CloudFrontRequestResult,
-} from "aws-lambda";
-
-import { debug } from "./logger.js";
-import { convertToQuery } from "./routing/util.js";
-import { parseCookies } from "./util.js";
-
-export type InternalEvent = {
-  readonly type: "v1" | "v2" | "cf";
-  readonly method: string;
-  readonly rawPath: string;
-  readonly url: string;
-  readonly body: Buffer;
-  readonly headers: Record<string, string>;
-  readonly query: Record<string, string | string[]>;
-  readonly cookies: Record<string, string>;
-  readonly remoteAddress: string;
-};
-
-export type InternalResult = {
-  readonly type: "v1" | "v2" | "cf";
-  statusCode: number;
-  headers: Record<string, string | string[]>;
-  body: string;
-  isBase64Encoded: boolean;
-};
-
-export function isAPIGatewayProxyEventV2(
-  event: any,
-): event is APIGatewayProxyEventV2 {
-  return event.version === "2.0";
-}
-
-export function isAPIGatewayProxyEvent(
-  event: any,
-): event is APIGatewayProxyEvent {
-  return event.version === undefined && !isCloudFrontRequestEvent(event);
-}
-
-export function isCloudFrontRequestEvent(
-  event: any,
-): event is CloudFrontRequestEvent {
-  return event.Records !== undefined;
-}
-
-export function convertFrom(
-  event: APIGatewayProxyEventV2 | APIGatewayProxyEvent | CloudFrontRequestEvent,
-): InternalEvent {
-  let internalEvent: InternalEvent;
-  if (isCloudFrontRequestEvent(event)) {
-    internalEvent = convertFromCloudFrontRequestEvent(event);
-  } else if (isAPIGatewayProxyEventV2(event)) {
-    internalEvent = convertFromAPIGatewayProxyEventV2(event);
-  } else if (isAPIGatewayProxyEvent(event)) {
-    internalEvent = convertFromAPIGatewayProxyEvent(event);
-  } else throw new Error("Unsupported event type");
-
-  return internalEvent;
-}
-
-export function convertTo(
-  result: InternalResult,
-): APIGatewayProxyResultV2 | APIGatewayProxyResult | CloudFrontRequestResult {
-  if (result.type === "v2") {
-    return convertToApiGatewayProxyResultV2(result);
-  } else if (result.type === "v1") {
-    return convertToApiGatewayProxyResult(result);
-  } else if (result.type === "cf") {
-    return convertToCloudFrontRequestResult(result);
-  }
-  throw new Error("Unsupported event type");
-}
-
-function removeUndefinedFromQuery(
-  query: Record<string, string | string[] | undefined>,
-) {
-  const newQuery: Record<string, string | string[]> = {};
-  for (const [key, value] of Object.entries(query)) {
-    if (value !== undefined) {
-      newQuery[key] = value;
-    }
-  }
-  return newQuery;
-}
-function convertFromAPIGatewayProxyEvent(
-  event: APIGatewayProxyEvent,
-): InternalEvent {
-  const { path, body, httpMethod, requestContext, isBase64Encoded } = event;
-  return {
-    type: "v1",
-    method: httpMethod,
-    rawPath: path,
-    url: path + normalizeAPIGatewayProxyEventQueryParams(event),
-    body: Buffer.from(body ?? "", isBase64Encoded ? "base64" : "utf8"),
-    headers: normalizeAPIGatewayProxyEventHeaders(event),
-    remoteAddress: requestContext.identity.sourceIp,
-    query: removeUndefinedFromQuery(
-      event.multiValueQueryStringParameters ?? {},
-    ),
-    cookies:
-      event.multiValueHeaders?.cookie?.reduce((acc, cur) => {
-        const [key, value] = cur.split("=");
-        return { ...acc, [key]: value };
-      }, {}) ?? {},
-  };
-}
-
-function convertFromAPIGatewayProxyEventV2(
-  event: APIGatewayProxyEventV2,
-): InternalEvent {
-  const { rawPath, rawQueryString, requestContext } = event;
-  return {
-    type: "v2",
-    method: requestContext.http.method,
-    rawPath,
-    url: rawPath + (rawQueryString ? `?${rawQueryString}` : ""),
-    body: normalizeAPIGatewayProxyEventV2Body(event),
-    headers: normalizeAPIGatewayProxyEventV2Headers(event),
-    remoteAddress: requestContext.http.sourceIp,
-    query: removeUndefinedFromQuery(convertToQuery(rawQueryString)),
-    cookies:
-      event.cookies?.reduce((acc, cur) => {
-        const [key, value] = cur.split("=");
-        return { ...acc, [key]: value };
-      }, {}) ?? {},
-  };
-}
-
-function convertFromCloudFrontRequestEvent(
-  event: CloudFrontRequestEvent,
-): InternalEvent {
-  const { method, uri, querystring, body, headers, clientIp } =
-    event.Records[0].cf.request;
-  return {
-    type: "cf",
-    method,
-    rawPath: uri,
-    url: uri + (querystring ? `?${querystring}` : ""),
-    body: Buffer.from(
-      body?.data ?? "",
-      body?.encoding === "base64" ? "base64" : "utf8",
-    ),
-    headers: normalizeCloudFrontRequestEventHeaders(headers),
-    remoteAddress: clientIp,
-    query: convertToQuery(querystring),
-    cookies:
-      headers.cookie?.reduce((acc, cur) => {
-        const { key, value } = cur;
-        return { ...acc, [key ?? ""]: value };
-      }, {}) ?? {},
-  };
-}
-
-function convertToApiGatewayProxyResult(
-  result: InternalResult,
-): APIGatewayProxyResult {
-  const headers: Record<string, string> = {};
-  const multiValueHeaders: Record<string, string[]> = {};
-  Object.entries(result.headers).forEach(([key, value]) => {
-    if (Array.isArray(value)) {
-      multiValueHeaders[key] = value;
-    } else {
-      if (value === null) {
-        headers[key] = "";
-        return;
-      }
-      headers[key] = value;
-    }
-  });
-
-  const response: APIGatewayProxyResult = {
-    statusCode: result.statusCode,
-    headers,
-    body: result.body,
-    isBase64Encoded: result.isBase64Encoded,
-    multiValueHeaders,
-  };
-  debug(response);
-  return response;
-}
-
-function convertToApiGatewayProxyResultV2(
-  result: InternalResult,
-): APIGatewayProxyResultV2 {
-  const headers: Record<string, string> = {};
-  Object.entries(result.headers)
-    .filter(([key]) => key.toLowerCase() !== "set-cookie")
-    .forEach(([key, value]) => {
-      if (value === null) {
-        headers[key] = "";
-        return;
-      }
-      headers[key] = Array.isArray(value) ? value.join(", ") : value.toString();
-    });
-
-  const response: APIGatewayProxyResultV2 = {
-    statusCode: result.statusCode,
-    headers,
-    cookies: parseCookies(result.headers["set-cookie"]),
-    body: result.body,
-    isBase64Encoded: result.isBase64Encoded,
-  };
-  debug(response);
-  return response;
-}
-
-function convertToCloudFrontRequestResult(
-  result: InternalResult,
-): CloudFrontRequestResult {
-  const headers: CloudFrontHeaders = {};
-  Object.entries(result.headers)
-    .filter(([key]) => key.toLowerCase() !== "content-length")
-    .forEach(([key, value]) => {
-      headers[key] = [
-        ...(headers[key] || []),
-        ...(Array.isArray(value)
-          ? value.map((v) => ({ key, value: v }))
-          : [{ key, value: value.toString() }]),
-      ];
-    });
-
-  const response: CloudFrontRequestResult = {
-    status: result.statusCode.toString(),
-    statusDescription: "OK",
-    headers,
-    bodyEncoding: result.isBase64Encoded ? "base64" : "text",
-    body: result.body,
-  };
-  debug(response);
-  return response;
-}
-
-function normalizeAPIGatewayProxyEventV2Headers(
-  event: APIGatewayProxyEventV2,
-): Record<string, string> {
-  const { headers: rawHeaders, cookies } = event;
-
-  const headers: Record<string, string> = {};
-
-  if (Array.isArray(cookies)) {
-    headers["cookie"] = cookies.join("; ");
-  }
-
-  for (const [key, value] of Object.entries(rawHeaders || {})) {
-    headers[key.toLowerCase()] = value!;
-  }
-
-  return headers;
-}
-
-function normalizeAPIGatewayProxyEventV2Body(
-  event: APIGatewayProxyEventV2,
-): Buffer {
-  const { body, isBase64Encoded } = event;
-  if (Buffer.isBuffer(body)) {
-    return body;
-  } else if (typeof body === "string") {
-    return Buffer.from(body, isBase64Encoded ? "base64" : "utf8");
-  } else if (typeof body === "object") {
-    return Buffer.from(JSON.stringify(body));
-  }
-  return Buffer.from("", "utf8");
-}
-
-function normalizeAPIGatewayProxyEventQueryParams(
-  event: APIGatewayProxyEvent,
-): string {
-  // Note that the same query string values are returned in both
-  // "multiValueQueryStringParameters" and "queryStringParameters".
-  // We only need to use one of them.
-  // For example:
-  //   "?name=foo" appears in the event object as
-  //   {
-  //     ...
-  //     queryStringParameters: { name: 'foo' },
-  //     multiValueQueryStringParameters: { name: [ 'foo' ] },
-  //     ...
-  //   }
-  const params = new URLSearchParams();
-  for (const [key, value] of Object.entries(
-    event.multiValueQueryStringParameters || {},
-  )) {
-    if (value !== undefined) {
-      for (const v of value) {
-        params.append(key, v);
-      }
-    }
-  }
-  const value = params.toString();
-  return value ? `?${value}` : "";
-}
-
-function normalizeAPIGatewayProxyEventHeaders(
-  event: APIGatewayProxyEvent,
-): Record<string, string> {
-  event.multiValueHeaders;
-  const headers: Record<string, string> = {};
-
-  for (const [key, values] of Object.entries(event.multiValueHeaders || {})) {
-    if (values) {
-      headers[key.toLowerCase()] = values.join(",");
-    }
-  }
-  for (const [key, value] of Object.entries(event.headers || {})) {
-    if (value) {
-      headers[key.toLowerCase()] = value;
-    }
-  }
-  return headers;
-}
-
-function normalizeCloudFrontRequestEventHeaders(
-  rawHeaders: CloudFrontHeaders,
-): Record<string, string> {
-  const headers: Record<string, string> = {};
-
-  for (const [key, values] of Object.entries(rawHeaders)) {
-    for (const { value } of values) {
-      if (value) {
-        headers[key.toLowerCase()] = value;
-      }
-    }
-  }
-
-  return headers;
-}
diff --git a/packages/open-next/src/adapters/http/index.ts b/packages/open-next/src/adapters/http/index.ts
deleted file mode 100644
index 82e9e9411..000000000
--- a/packages/open-next/src/adapters/http/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from "./request.js";
-export * from "./response.js";
-export * from "./responseStreaming.js";
diff --git a/packages/open-next/src/adapters/http/response.ts b/packages/open-next/src/adapters/http/response.ts
deleted file mode 100644
index 9731f55d5..000000000
--- a/packages/open-next/src/adapters/http/response.ts
+++ /dev/null
@@ -1,150 +0,0 @@
-// Copied and modified from serverless-http by Doug Moscrop
-// https://github.com/dougmoscrop/serverless-http/blob/master/lib/response.js
-// Licensed under the MIT License
-
-import http from "node:http";
-import { Socket } from "node:net";
-
-import {
-  convertHeader,
-  getString,
-  headerEnd,
-  NO_OP,
-  parseHeaders,
-} from "./util.js";
-
-const BODY = Symbol();
-const HEADERS = Symbol();
-
-function addData(stream: ServerlessResponse, data: Uint8Array | string) {
-  if (
-    Buffer.isBuffer(data) ||
-    ArrayBuffer.isView(data) ||
-    typeof data === "string"
-  ) {
-    stream[BODY].push(Buffer.from(data));
-  } else {
-    throw new Error(`response.addData() of unexpected type: ${typeof data}`);
-  }
-}
-
-export interface ServerlessResponseProps {
-  method: string;
-  headers: Record<string, string | string[] | undefined>;
-}
-
-export class ServerlessResponse extends http.ServerResponse {
-  [BODY]: Buffer[];
-  [HEADERS]: Record<string, string>;
-  private _wroteHeader = false;
-  private _header = "";
-  private _initialHeaders: Record<string, string> = {};
-
-  constructor({ method, headers }: ServerlessResponseProps) {
-    super({ method, headers } as any);
-
-    this[BODY] = [];
-    this[HEADERS] = parseHeaders(headers) || {};
-    this._initialHeaders = this[HEADERS];
-
-    this.useChunkedEncodingByDefault = false;
-    this.chunkedEncoding = false;
-    this._header = "";
-
-    const socket: Partial<Socket> & { _writableState: any } = {
-      _writableState: {},
-      writable: true,
-      on: NO_OP,
-      removeListener: NO_OP,
-      destroy: NO_OP,
-      cork: NO_OP,
-      uncork: NO_OP,
-      write: (
-        data: Uint8Array | string,
-        encoding?: string | null | (() => void),
-        cb?: () => void,
-      ) => {
-        if (typeof encoding === "function") {
-          cb = encoding;
-          encoding = null;
-        }
-
-        if (this._header === "" || this._wroteHeader) {
-          addData(this, data);
-        } else {
-          const string = getString(data);
-          const index = string.indexOf(headerEnd);
-
-          if (index !== -1) {
-            const remainder = string.slice(index + headerEnd.length);
-
-            if (remainder) {
-              addData(this, remainder);
-            }
-
-            this._wroteHeader = true;
-          }
-        }
-
-        if (typeof cb === "function") {
-          cb();
-        }
-        return true;
-      },
-    };
-
-    this.assignSocket(socket as Socket);
-
-    this.once("finish", () => {
-      this.emit("close");
-    });
-  }
-
-  static body(res: ServerlessResponse) {
-    return Buffer.concat(res[BODY]);
-  }
-
-  static headers(res: ServerlessResponse) {
-    const headers =
-      typeof res.getHeaders === "function" ? res.getHeaders() : res[HEADERS];
-
-    return {
-      ...parseHeaders(headers),
-      ...res[HEADERS],
-      ...res._initialHeaders,
-    };
-  }
-
-  get headers() {
-    return this[HEADERS];
-  }
-
-  setHeader(key: string, value: string | number | string[]): this {
-    if (this._wroteHeader) {
-      this[HEADERS][key] = convertHeader(value);
-    } else {
-      super.setHeader(key, value);
-    }
-    return this;
-  }
-
-  writeHead(
-    statusCode: number,
-    reason?: string | any | any[],
-    obj?: any | any[],
-  ) {
-    const headers = typeof reason === "string" ? obj : reason;
-
-    for (const name in headers) {
-      this.setHeader(name, headers[name]);
-
-      if (!this._wroteHeader) {
-        // we only need to initiate super.headers once
-        // writeHead will add the other headers itself
-        break;
-      }
-    }
-
-    return super.writeHead(statusCode, reason, obj);
-  }
-}
diff --git a/packages/open-next/src/adapters/http/responseStreaming.ts b/packages/open-next/src/adapters/http/responseStreaming.ts
deleted file mode 100644
index d41eefb2f..000000000
--- a/packages/open-next/src/adapters/http/responseStreaming.ts
+++ /dev/null
@@ -1,263 +0,0 @@
-import http from "node:http";
-import { Socket } from "node:net";
-import zlib from "node:zlib";
-
-import { Writable } from "stream";
-
-import { debug, error } from "../logger.js";
-// import type { ResponseStream } from "../types/aws-lambda.js";
-import { parseCookies } from "../util.js";
-import { convertHeader, getString, NO_OP, parseHeaders } from "./util.js";
-
-const HEADERS = Symbol();
-
-export interface ResponseStream extends Writable {
-  writeHeaders(
-    prelude: {
-      statusCode: number;
-      cookies: string[];
-      headers: Record<string, string>;
-    },
-    onFinish: () => void,
-  ): void;
-  // Just to fix an issue with aws lambda streaming with empty body
-  onFirstWrite?: () => void;
-}
-
-export interface StreamingServerResponseProps {
-  method?: string;
-  headers?: Record<string, string | string[] | undefined>;
-  responseStream: ResponseStream;
-  fixHeaders: (headers: Record<string, string>) => void;
-  onEnd: (headers: Record<string, string>) => Promise<void>;
-}
-export class StreamingServerResponse extends http.ServerResponse {
-  [HEADERS]: Record<string, string> = {};
-  responseStream: ResponseStream;
-  fixHeaders: (headers: Record<string, string>) => void;
-  onEnd: (headers: Record<string, string>) => Promise<void>;
-  private _wroteHeader = false;
-  private _hasWritten = false;
-  private _initialHeaders: Record<string, string> = {};
-  private _cookies: string[] = [];
-  private _compressed = false;
-
-  constructor({
-    method,
-    headers,
-    responseStream,
-    fixHeaders,
-    onEnd,
-  }: StreamingServerResponseProps) {
-    super({ method } as any);
-    if (headers && headers["set-cookie"]) {
-      this._cookies = parseCookies(headers["set-cookie"]) as string[];
-      delete headers["set-cookie"];
-    }
-    this[HEADERS] = parseHeaders(headers) || {};
-    this._initialHeaders = { ...this[HEADERS] };
-
-    this.fixHeaders = fixHeaders;
-    this.onEnd = onEnd;
-    this.responseStream = responseStream;
-
-    this.useChunkedEncodingByDefault = false;
-    this.chunkedEncoding = false;
-
-    this.responseStream.cork();
-
-    const socket: Partial<Socket> & { _writableState: any } = {
-      _writableState: {},
-      writable: true,
-      on: NO_OP,
-      removeListener: NO_OP,
-      destroy: NO_OP,
-      cork: NO_OP,
-      uncork: NO_OP,
-      write: (
-        data: Uint8Array | string,
-        encoding?: string | null | (() => void),
-        cb?: () => void,
-      ) => {
-        if (typeof encoding === "function") {
-          cb = encoding;
-          encoding = undefined;
-        }
-        const d = getString(data);
-        const isSse = d.endsWith("\n\n");
-        this.internalWrite(data, isSse, cb);
-
-        return !this.responseStream.writableNeedDrain;
-      },
-    };
-
-    this.assignSocket(socket as Socket);
-
-    this.responseStream.on("close", this.cancel.bind(this));
-    this.responseStream.on("error", this.cancel.bind(this));
-
-    this.on("close", this.cancel.bind(this));
-    this.on("error", this.cancel.bind(this));
-    this.once("finish", () => {
-      this.emit("close");
-    });
-  }
-
-  get headers() {
-    return this[HEADERS];
-  }
-
-  setHeader(key: string, value: string | number | string[]): this {
-    key = key.toLowerCase();
-    // There can be multiple set-cookie response headers
-    // They need to be returned as a special "cookies" array, eg:
-    // {statusCode: xxx, cookies: ['Cookie=Yum'], ...}
-    if (key === "set-cookie") {
-      this._cookies.push(convertHeader(value));
-    } else {
-      this[HEADERS][key] = convertHeader(value);
-    }
-    return this;
-  }
-
-  removeHeader(key: string): this {
-    key = key.toLowerCase();
-    if (key === "set-cookie") {
-      this._cookies.length = 0;
-    } else {
-      delete this[HEADERS][key];
-    }
-    return this;
-  }
-
-  writeHead(
-    statusCode: number,
-    _statusMessage?:
-      | string
-      | http.OutgoingHttpHeaders
-      | http.OutgoingHttpHeader[],
-    _headers?: http.OutgoingHttpHeaders | http.OutgoingHttpHeader[],
-  ): this {
-    const headers =
-      typeof _statusMessage === "string" ? _headers : _statusMessage;
-    const statusMessage =
-      typeof _statusMessage === "string" ? _statusMessage : undefined;
-    if (this._wroteHeader) {
-      return this;
-    }
-    try {
-      debug("writeHead", statusCode, statusMessage, headers);
-      const parsedHeaders = parseHeaders(headers);
-      this[HEADERS] = {
-        ...this[HEADERS],
-        ...parsedHeaders,
-      };
-
-      this.fixHeaders(this[HEADERS]);
-      this[HEADERS] = {
-        ...this[HEADERS],
-        ...this._initialHeaders,
-      };
-
-      this._compressed = this[HEADERS]["accept-encoding"]?.includes("br");
-      if (this._compressed) {
-        this[HEADERS]["content-encoding"] = "br";
-      }
-      delete this[HEADERS]["accept-encoding"];
-
-      debug("writeHead", this[HEADERS]);
-
-      this._wroteHeader = true;
-      this.statusCode = statusCode;
-      this.responseStream.writeHeaders(
-        {
-          statusCode,
-          cookies: this._cookies,
-          headers: this[HEADERS],
-        },
-        () => {
-          if (this._compressed) {
-            const br = zlib.createBrotliCompress({
-              flush: zlib.constants.BROTLI_OPERATION_FLUSH,
-            });
-            br.setMaxListeners(100);
-            br.pipe(this.responseStream);
-            this.responseStream = br as unknown as ResponseStream;
-          }
-        },
-      );
-
-      debug("writeHead", this[HEADERS]);
-    } catch (e) {
-      this.responseStream.end();
-      error(e);
-    }
-
-    return this;
-  }
-
-  end(
-    _chunk?: Uint8Array | string | (() => void),
-    _encoding?: BufferEncoding | (() => void),
-    _cb?: (() => void) | undefined,
-  ): this {
-    const chunk = typeof _chunk === "function" ? undefined : _chunk;
-    const cb = typeof _cb === "function" ? _cb : undefined;
-
-    if (!this._wroteHeader) {
-      // When next directly returns with end, the writeHead is not called,
-      // so we need to call it here
-      this.writeHead(this.statusCode ?? 200);
-    }
-
-    if (!this._hasWritten && !chunk) {
-      // We need to send data here if there is none, otherwise the stream will not end at all
-      this.internalWrite(new Uint8Array(8), false, cb);
-    }
-
-    const _end = () => {
-      setImmediate(() => {
-        this.responseStream.end(_chunk, async () => {
-          if (this._compressed) {
-            (this.responseStream as unknown as zlib.BrotliCompress).flush(
-              zlib.constants.BROTLI_OPERATION_FINISH,
-            );
-          }
-          await this.onEnd(this[HEADERS]);
-          cb?.();
-        });
-      });
-    };
-
-    if (this.responseStream.writableNeedDrain) {
-      this.responseStream.once("drain", _end);
-    } else {
-      _end();
-    }
-    return this;
-  }
-
-  private internalWrite(chunk: any, isSse: boolean = false, cb?: () => void) {
-    this._hasWritten = true;
-    setImmediate(() => {
-      this.responseStream.write(chunk, cb);
-
-      // SSE need to flush to send to client ASAP
-      if (isSse) {
-        setImmediate(() => {
-          this.responseStream.write("\n\n");
-          this.responseStream.uncork();
-        });
-      }
-    });
-  }
-
-  cancel(error?: Error) {
-    this.responseStream.off("close", this.cancel.bind(this));
-    this.responseStream.off("error", this.cancel.bind(this));
-
-    if (error) {
-      this.responseStream.destroy(error);
-    }
-  }
-}
diff --git a/packages/open-next/src/adapters/http/util.ts b/packages/open-next/src/adapters/http/util.ts
deleted file mode 100644
index 132f6798b..000000000
--- a/packages/open-next/src/adapters/http/util.ts
+++ /dev/null
@@ -1,50 +0,0 @@
-import http from "node:http";
-
-export function getString(data: any) {
-  // Note: use `ArrayBuffer.isView()` to check for Uint8Array. Using
-  //       `instanceof Uint8Array` returns false in some cases. For example,
-  //       when the buffer is created in middleware and passed to NextServer.
-  if (Buffer.isBuffer(data)) {
-    return data.toString("utf8");
-  } else if (ArrayBuffer.isView(data)) {
-    //@ts-ignore
-    return Buffer.from(data).toString("utf8");
-  } else if (typeof data === "string") {
-    return data;
-  } else {
-    throw new Error(`response.getString() of unexpected type: ${typeof data}`);
-  }
-}
-
-export const headerEnd = "\r\n\r\n";
-
-export const NO_OP: (...args: any[]) => any = () => void 0;
-
-export const parseHeaders = (
-  headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
-) => {
-  const result: Record<string, string> = {};
-  if (!headers) {
-    return result;
-  }
-
-  for (const [key, value] of Object.entries(headers)) {
-    if (value === undefined) {
-      continue;
-    } else {
-      result[key] = convertHeader(value);
-    }
-  }
-
-  return result;
-};
-
-export const convertHeader = (header: http.OutgoingHttpHeader) => {
-  if (typeof header === "string") {
-    return header;
-  } else if (Array.isArray(header)) {
-    return header.join(",");
-  } else {
-    return String(header);
-  }
-};
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index f5fbb26bc..19f2f94e0 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -11,6 +11,7 @@ import type {
   APIGatewayProxyEventV2,
   APIGatewayProxyResultV2,
 } from "aws-lambda";
+import { loadConfig } from "config/util.js";
 // @ts-ignore
 import { defaultConfig } from "next/dist/server/config-shared";
 import {
@@ -21,7 +22,6 @@ import {
 // @ts-ignore
 import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
 
-import { loadConfig } from "./config/util.js";
 import { awsLogger, debug, error } from "./logger.js";
 import { setNodeEnv } from "./util.js";
 
diff --git a/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts b/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
index 7ad3a75c2..099a7e3ea 100644
--- a/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
+++ b/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
@@ -1,12 +1,11 @@
 /*eslint-disable simple-import-sort/imports */
-import type { Options, PluginHandler } from "../../types/next-types.js";
-import type { IncomingMessage } from "../../http/request.js";
+import type { Options, PluginHandler } from "types/next-types.js";
+import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
 //#override imports
 //@ts-ignore
 import { requestHandler } from "./util.js";
 //@ts-ignore
 import { proxyRequest } from "./routing/util.js";
-import { OpenNextNodeResponse } from "../../http/openNextResponse.js";
 //#endOverride
 
 //#override handler
diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
index 7cf9cd7c3..1bb6c6d18 100644
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ b/packages/open-next/src/adapters/plugins/routing/util.ts
@@ -1,13 +1,13 @@
+import { BuildId, HtmlPages } from "config/index.js";
 import crypto from "crypto";
 import { OutgoingHttpHeaders, ServerResponse } from "http";
-
-import { BuildId, HtmlPages } from "../../config/index.js";
-import { InternalEvent } from "../../event-mapper.js";
 import {
+  IncomingMessage,
   OpenNextNodeResponse,
   StreamCreator,
-} from "../../http/openNextResponse.js";
-import { IncomingMessage } from "../../http/request.js";
+} from "http/index.js";
+import { InternalEvent } from "types/open-next";
+
 import { debug } from "../../logger.js";
 
 declare global {
@@ -25,6 +25,7 @@ export async function proxyRequest(
   req: IncomingMessage,
   res: OpenNextNodeResponse,
 ) {
+  // TODO: we should use our own version instead of the one bundled with Next.js
   const HttpProxy = require("next/dist/compiled/http-proxy") as any;
 
   const proxy = new HttpProxy({
diff --git a/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts b/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
index a110609ee..1059affce 100644
--- a/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
+++ b/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
@@ -1,11 +1,10 @@
 /*eslint-disable simple-import-sort/imports */
-import type { Options, PluginHandler } from "../types/next-types.js";
-import type { IncomingMessage } from "../http/request.js";
+import type { Options, PluginHandler } from "types/next-types.js";
+import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
 //#override imports
 
 import { proxyRequest } from "./routing/util.js";
 import { requestHandler, setNextjsPrebundledReact } from "./util.js";
-import { OpenNextNodeResponse } from "../http/openNextResponse.js";
 //#endOverride
 
 //#override handler
diff --git a/packages/open-next/src/adapters/plugins/serverHandler.ts b/packages/open-next/src/adapters/plugins/serverHandler.ts
index 62faf3326..050ce2871 100644
--- a/packages/open-next/src/adapters/plugins/serverHandler.ts
+++ b/packages/open-next/src/adapters/plugins/serverHandler.ts
@@ -1,10 +1,13 @@
-import type { OpenNextNodeResponse } from "../http/openNextResponse.js";
-import type { IncomingMessage } from "../http/request.js";
-import type { Options, PluginHandler } from "../types/next-types.js";
+import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
+import type { Options, PluginHandler } from "types/next-types.js";
+
 //#override imports
 import { requestHandler, setNextjsPrebundledReact } from "./util.js";
 //#endOverride
 
+//TODO: refactor this, we don't need to override this anymore, we could use the replacement
+// and remove setNextjsPrebundledReact where we need to
+// It would be handy to change the plugin to allow delete without having to create a replacement file
 //#override handler
 export const handler: PluginHandler = async (
   req: IncomingMessage,
diff --git a/packages/open-next/src/adapters/plugins/util.replacement.ts b/packages/open-next/src/adapters/plugins/util.replacement.ts
index 6d44b404a..585b9617c 100644
--- a/packages/open-next/src/adapters/plugins/util.replacement.ts
+++ b/packages/open-next/src/adapters/plugins/util.replacement.ts
@@ -1,4 +1,4 @@
-import { NextConfig } from "../config";
+import { NextConfig } from "config/index";
 
 //#override requestHandler
 // @ts-ignore
diff --git a/packages/open-next/src/adapters/plugins/util.ts b/packages/open-next/src/adapters/plugins/util.ts
index 5f8468978..373cc48c3 100644
--- a/packages/open-next/src/adapters/plugins/util.ts
+++ b/packages/open-next/src/adapters/plugins/util.ts
@@ -1,20 +1,20 @@
 import fs from "node:fs";
 import path from "node:path";
 
-// @ts-ignore
-import NextServer from "next/dist/server/next-server.js";
-
 import {
   AppPathsManifestKeys,
   NextConfig,
   RoutesManifest,
-} from "../config/index.js";
+} from "config/index.js";
+// @ts-ignore
+import NextServer from "next/dist/server/next-server.js";
+import type { MiddlewareManifest } from "types/next-types.js";
+
 import { debug } from "../logger.js";
 import {
   applyOverride as applyNextjsRequireHooksOverride,
   overrideHooks as overrideNextjsRequireHooks,
 } from "../require-hooks.js";
-import { MiddlewareManifest } from "../types/next-types.js";
 
 // WORKAROUND: Set `__NEXT_PRIVATE_PREBUNDLED_REACT` to use prebundled React — https://github.com/serverless-stack/open-next#workaround-set-__next_private_prebundled_react-to-use-prebundled-react
 // Step 1: Need to override the require hooks for React before Next.js server
diff --git a/packages/open-next/src/adapters/require-hooks.ts b/packages/open-next/src/adapters/require-hooks.ts
index 255fe2f0f..2dfb27dcc 100644
--- a/packages/open-next/src/adapters/require-hooks.ts
+++ b/packages/open-next/src/adapters/require-hooks.ts
@@ -2,8 +2,9 @@
 // This is needed for userland plugins to attach to the same webpack instance as Next.js'.
 // Individually compiled modules are as defined for the compilation in bundles/webpack/packages/*.
 
+import type { NextConfig } from "types/next-types.js";
+
 import { error } from "./logger.js";
-import type { NextConfig } from "./types/next-types.js";
 
 // This module will only be loaded once per process.
 
diff --git a/packages/open-next/src/adapters/server-adapter.ts b/packages/open-next/src/adapters/server-adapter.ts
index cd9108bd1..3fa6df50a 100644
--- a/packages/open-next/src/adapters/server-adapter.ts
+++ b/packages/open-next/src/adapters/server-adapter.ts
@@ -1,7 +1,8 @@
-import { createMainHandler } from "../core/createMainHandler.js";
 // We load every config here so that they are only loaded once
 // and during cold starts
-import { BuildId } from "./config/index.js";
+import { BuildId } from "config/index.js";
+
+import { createMainHandler } from "../core/createMainHandler.js";
 import { setNodeEnv } from "./util.js";
 
 // We load every config here so that they are only loaded once
diff --git a/packages/open-next/src/adapters/util.ts b/packages/open-next/src/adapters/util.ts
index 62cd2d798..d35545e89 100644
--- a/packages/open-next/src/adapters/util.ts
+++ b/packages/open-next/src/adapters/util.ts
@@ -1,3 +1,5 @@
+//TODO: We should probably move all the utils to a separate location
+
 export function setNodeEnv() {
   process.env.NODE_ENV = process.env.NODE_ENV ?? "production";
 }
@@ -6,39 +8,6 @@ export function generateUniqueId() {
   return Math.random().toString(36).slice(2, 8);
 }
 
-export function escapeRegex(str: string) {
-  let path = str.replace(/\(\.\)/g, "_µ1_");
-
-  path = path.replace(/\(\.{2}\)/g, "_µ2_");
-
-  path = path.replace(/\(\.{3}\)/g, "_µ3_");
-
-  return path;
-}
-
-export function unescapeRegex(str: string) {
-  let path = str.replace(/_µ1_/g, "(.)");
-
-  path = path.replace(/_µ2_/g, "(..)");
-
-  path = path.replace(/_µ3_/g, "(...)");
-
-  return path;
-}
-
-// AWS cookies are in a single `set-cookie` string, delimited by a comma
-export function parseCookies(
-  cookies?: string | string[],
-): string[] | undefined {
-  if (!cookies) return;
-
-  if (typeof cookies === "string") {
-    return cookies.split(/(?<!Expires=\w+),/i).map((c) => c.trim());
-  }
-
-  return cookies;
-}
-
 /**
  * Create an array of arrays of size `chunkSize` from `items`
  * @param items Array of T
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 352b9a08d..17347c17a 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -10,10 +10,9 @@ import {
   buildSync,
 } from "esbuild";
 
-import { BuildOptions, DangerousOptions } from "./adapters/types/open-next.js";
-import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import openNextPlugin from "./plugin.js";
+import { BuildOptions, DangerousOptions } from "./types/open-next.js";
 
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
diff --git a/packages/open-next/src/converters/aws-apigw-v1.ts b/packages/open-next/src/converters/aws-apigw-v1.ts
index 19411ef8d..dc275ed04 100644
--- a/packages/open-next/src/converters/aws-apigw-v1.ts
+++ b/packages/open-next/src/converters/aws-apigw-v1.ts
@@ -1,8 +1,7 @@
 import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
+import type { Converter, InternalEvent, InternalResult } from "types/open-next";
 
-import { InternalEvent, InternalResult } from "../adapters/event-mapper";
 import { debug } from "../adapters/logger";
-import { Converter } from "../adapters/types/open-next";
 import { removeUndefinedFromQuery } from "./utils";
 
 function normalizeAPIGatewayProxyEventHeaders(
diff --git a/packages/open-next/src/converters/aws-apigw-v2.ts b/packages/open-next/src/converters/aws-apigw-v2.ts
index 52a9c8152..77c282238 100644
--- a/packages/open-next/src/converters/aws-apigw-v2.ts
+++ b/packages/open-next/src/converters/aws-apigw-v2.ts
@@ -1,9 +1,9 @@
 import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda";
+import { parseCookies } from "http/util";
+import type { Converter, InternalEvent, InternalResult } from "types/open-next";
 
-import { InternalEvent, InternalResult } from "../adapters/event-mapper";
 import { debug } from "../adapters/logger";
-import { Converter } from "../adapters/types/open-next";
-import { parseCookies } from "../adapters/util";
+import { convertToQuery } from "../core/routing/util";
 import { removeUndefinedFromQuery } from "./utils";
 
 function normalizeAPIGatewayProxyEventV2Body(
@@ -51,7 +51,7 @@ async function convertFromAPIGatewayProxyEventV2(
     body: normalizeAPIGatewayProxyEventV2Body(event),
     headers: normalizeAPIGatewayProxyEventV2Headers(event),
     remoteAddress: requestContext.http.sourceIp,
-    query: removeUndefinedFromQuery(event.queryStringParameters ?? {}),
+    query: removeUndefinedFromQuery(convertToQuery(rawQueryString)),
     cookies:
       event.cookies?.reduce((acc, cur) => {
         const [key, value] = cur.split("=");
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index f2560466b..2205f9cb0 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -3,10 +3,10 @@ import {
   CloudFrontRequestEvent,
   CloudFrontRequestResult,
 } from "aws-lambda";
+import type { Converter, InternalEvent, InternalResult } from "types/open-next";
 
-import { InternalEvent, InternalResult } from "../adapters/event-mapper";
 import { debug } from "../adapters/logger";
-import { Converter } from "../adapters/types/open-next";
+import { convertToQuery } from "../core/routing/util";
 
 function normalizeCloudFrontRequestEventHeaders(
   rawHeaders: CloudFrontHeaders,
@@ -40,13 +40,7 @@ async function convertFromCloudFrontRequestEvent(
     ),
     headers: normalizeCloudFrontRequestEventHeaders(headers),
     remoteAddress: clientIp,
-    query: querystring.split("&").reduce(
-      (acc, cur) => ({
-        ...acc,
-        [cur.split("=")[0]]: cur.split("=")[1],
-      }),
-      {},
-    ),
+    query: convertToQuery(querystring),
     cookies:
       headers.cookie?.reduce((acc, cur) => {
         const { key, value } = cur;
diff --git a/packages/open-next/src/converters/docker.ts b/packages/open-next/src/converters/docker.ts
index c2278673a..8561aa7a9 100644
--- a/packages/open-next/src/converters/docker.ts
+++ b/packages/open-next/src/converters/docker.ts
@@ -1,8 +1,6 @@
 import { IncomingMessage } from "http";
-
-import { InternalResult } from "../adapters/event-mapper";
-import { Converter } from "../adapters/types/open-next";
-import { parseCookies } from "../adapters/util";
+import { parseCookies } from "http/util";
+import type { Converter, InternalResult } from "types/open-next";
 
 const converter: Converter = {
   convertFrom: async (req: IncomingMessage) => {
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 2986fd9dd..333006240 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -1,9 +1,10 @@
-import {
+import type {
   BuildOptions,
   Converter,
   OverrideOptions,
   Wrapper,
-} from "../adapters/types/open-next";
+} from "types/open-next";
+
 import type { IncrementalCache } from "../cache/incremental/types";
 import type { Queue } from "../queue/types";
 import { openNextHandler } from "./requestHandler";
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 7d9556804..c2a0b46e2 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -1,10 +1,11 @@
-import { BuildId } from "../adapters/config";
-import { InternalEvent, InternalResult } from "../adapters/event-mapper";
-import { IncomingMessage } from "../adapters/http";
+import { BuildId } from "config/index";
 import {
+  IncomingMessage,
   OpenNextNodeResponse,
   StreamCreator,
-} from "../adapters/http/openNextResponse";
+} from "http/index.js";
+import { InternalEvent, InternalResult } from "types/open-next";
+
 import { error } from "../adapters/logger";
 import { createServerResponse } from "../adapters/plugins/routing/util";
 import { handler as serverHandler } from "../adapters/plugins/serverHandler";
@@ -94,7 +95,7 @@ async function processRequest(
     });
   } catch (e: any) {
     error("NextJS request failed.", e);
-
+    //TODO: we could return the next 500 page here
     res.setHeader("Content-Type", "application/json");
     res.end(
       JSON.stringify(
diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index 3eedbb8cf..6d3551191 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -1,17 +1,22 @@
+import { NextConfig } from "config/index";
 import { compile, Match, match, PathFunction } from "path-to-regexp";
-
-import { NextConfig } from "../../adapters/config";
-import { InternalEvent, InternalResult } from "../../adapters/event-mapper";
-import { debug } from "../../adapters/logger";
-import {
+import type {
   Header,
   PrerenderManifest,
   RedirectDefinition,
   RewriteDefinition,
   RouteHas,
-} from "../../adapters/types/next-types";
-import { escapeRegex, unescapeRegex } from "../../adapters/util";
-import { convertToQueryString, getUrlParts, isExternal } from "./util";
+} from "types/next-types";
+import { InternalEvent, InternalResult } from "types/open-next";
+
+import { debug } from "../../adapters/logger";
+import {
+  convertToQueryString,
+  escapeRegex,
+  getUrlParts,
+  isExternal,
+  unescapeRegex,
+} from "./util";
 
 const routeHasMatcher =
   (
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index e2fc1cd3c..2eadda727 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -1,7 +1,8 @@
 import path from "node:path";
 
-import { NEXT_DIR, NextConfig } from "../../adapters/config/index.js";
-import { InternalEvent, InternalResult } from "../../adapters/event-mapper.js";
+import { NEXT_DIR, NextConfig } from "config/index.js";
+import { InternalEvent, InternalResult } from "types/open-next.js";
+
 //NOTE: we should try to avoid importing stuff from next as much as possible
 // every release of next could break this
 // const { run } = require("next/dist/server/web/sandbox");
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index bc7afe5e3..bc800b879 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -1,10 +1,11 @@
 import fs from "node:fs";
 import path from "node:path";
 
-import { isBinaryContentType } from "../../adapters/binary";
-import { OpenNextNodeResponse } from "../../adapters/http/openNextResponse";
-import { parseHeaders } from "../../adapters/http/util";
-import { MiddlewareManifest } from "../../adapters/types/next-types";
+import { OpenNextNodeResponse } from "http/index.js";
+import { parseHeaders } from "http/util.js";
+import type { MiddlewareManifest } from "types/next-types";
+
+import { isBinaryContentType } from "../../adapters/binary.js";
 
 export function isExternal(url?: string, host?: string) {
   if (!url) return false;
@@ -100,3 +101,23 @@ export function loadMiddlewareManifest(nextDir: string) {
   const json = fs.readFileSync(filePath, "utf-8");
   return JSON.parse(json) as MiddlewareManifest;
 }
+
+export function escapeRegex(str: string) {
+  let path = str.replace(/\(\.\)/g, "_µ1_");
+
+  path = path.replace(/\(\.{2}\)/g, "_µ2_");
+
+  path = path.replace(/\(\.{3}\)/g, "_µ3_");
+
+  return path;
+}
+
+export function unescapeRegex(str: string) {
+  let path = str.replace(/_µ1_/g, "(.)");
+
+  path = path.replace(/_µ2_/g, "(..)");
+
+  path = path.replace(/_µ3_/g, "(...)");
+
+  return path;
+}
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index fc0d0a981..fa2ab18dd 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -1,5 +1,3 @@
-import type { OutgoingHttpHeaders } from "http";
-
 //TODO: Replace this with injected values in case of external routing
 // We could use open-next plugins to replace these values at build time
 import {
@@ -7,8 +5,10 @@ import {
   ConfigHeaders,
   PrerenderManifest,
   RoutesManifest,
-} from "../adapters/config";
-import { InternalEvent, InternalResult } from "../adapters/event-mapper";
+} from "config/index";
+import type { OutgoingHttpHeaders } from "http";
+import { InternalEvent, InternalResult } from "types/open-next";
+
 import {
   addNextConfigHeaders,
   fixDataPage,
diff --git a/packages/open-next/src/http/index.ts b/packages/open-next/src/http/index.ts
new file mode 100644
index 000000000..e7e2b8a46
--- /dev/null
+++ b/packages/open-next/src/http/index.ts
@@ -0,0 +1,2 @@
+export * from "./openNextResponse.js";
+export * from "./request.js";
diff --git a/packages/open-next/src/adapters/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts
similarity index 97%
rename from packages/open-next/src/adapters/http/openNextResponse.ts
rename to packages/open-next/src/http/openNextResponse.ts
index b986735f3..b0d1fafdd 100644
--- a/packages/open-next/src/adapters/http/openNextResponse.ts
+++ b/packages/open-next/src/http/openNextResponse.ts
@@ -1,8 +1,7 @@
 import { OutgoingHttpHeader, OutgoingHttpHeaders } from "http";
 import { Transform, TransformCallback, Writable } from "stream";
 
-import { parseCookies } from "../util";
-import { convertHeader, parseHeaders } from "./util";
+import { convertHeader, parseCookies, parseHeaders } from "./util";
 
 const SET_COOKIE_HEADER = "set-cookie";
 
diff --git a/packages/open-next/src/adapters/http/request.ts b/packages/open-next/src/http/request.ts
similarity index 100%
rename from packages/open-next/src/adapters/http/request.ts
rename to packages/open-next/src/http/request.ts
diff --git a/packages/open-next/src/http/util.ts b/packages/open-next/src/http/util.ts
new file mode 100644
index 000000000..0e3be5fdd
--- /dev/null
+++ b/packages/open-next/src/http/util.ts
@@ -0,0 +1,42 @@
+import http from "node:http";
+
+export const parseHeaders = (
+  headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
+) => {
+  const result: Record<string, string> = {};
+  if (!headers) {
+    return result;
+  }
+
+  for (const [key, value] of Object.entries(headers)) {
+    if (value === undefined) {
+      continue;
+    } else {
+      result[key] = convertHeader(value);
+    }
+  }
+
+  return result;
+};
+
+export const convertHeader = (header: http.OutgoingHttpHeader) => {
+  if (typeof header === "string") {
+    return header;
+  } else if (Array.isArray(header)) {
+    return header.join(",");
+  } else {
+    return String(header);
+  }
+};
+
+export function parseCookies(
+  cookies?: string | string[],
+): string[] | undefined {
+  if (!cookies) return;
+
+  if (typeof cookies === "string") {
+    return cookies.split(/(?<!Expires=\w+),/i).map((c) => c.trim());
+  }
+
+  return cookies;
+}
diff --git a/packages/open-next/src/adapters/types/aws-lambda.ts b/packages/open-next/src/types/aws-lambda.ts
similarity index 100%
rename from packages/open-next/src/adapters/types/aws-lambda.ts
rename to packages/open-next/src/types/aws-lambda.ts
diff --git a/packages/open-next/src/adapters/types/next-types.ts b/packages/open-next/src/types/next-types.ts
similarity index 91%
rename from packages/open-next/src/adapters/types/next-types.ts
rename to packages/open-next/src/types/next-types.ts
index a05cabcdb..9c6754f39 100644
--- a/packages/open-next/src/adapters/types/next-types.ts
+++ b/packages/open-next/src/types/next-types.ts
@@ -1,9 +1,8 @@
 // NOTE: add more next config typings as they become relevant
 
-import { InternalEvent } from "../event-mapper.js";
-import { OpenNextNodeResponse } from "../http/openNextResponse.js";
-import { IncomingMessage } from "../http/request.js";
-import { ServerlessResponse } from "../http/response.js";
+import { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
+
+import { InternalEvent } from "./open-next";
 
 type RemotePattern = {
   protocol?: "http" | "https";
@@ -155,5 +154,5 @@ export interface PluginHandler {
     req: IncomingMessage,
     res: OpenNextNodeResponse,
     options: Options,
-  ): Promise<ServerlessResponse | undefined>;
+  ): Promise<OpenNextNodeResponse | undefined>;
 }
diff --git a/packages/open-next/src/adapters/types/open-next.ts b/packages/open-next/src/types/open-next.ts
similarity index 86%
rename from packages/open-next/src/adapters/types/open-next.ts
rename to packages/open-next/src/types/open-next.ts
index b07ddb359..b762b430a 100644
--- a/packages/open-next/src/adapters/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -1,6 +1,26 @@
-import { Queue } from "../../queue/types";
-import { InternalEvent, InternalResult } from "../event-mapper";
-import { StreamCreator } from "../http/openNextResponse";
+import { StreamCreator } from "http/index.js";
+
+import { Queue } from "../queue/types";
+
+export type InternalEvent = {
+  readonly type: "v1" | "v2" | "cf";
+  readonly method: string;
+  readonly rawPath: string;
+  readonly url: string;
+  readonly body: Buffer;
+  readonly headers: Record<string, string>;
+  readonly query: Record<string, string | string[]>;
+  readonly cookies: Record<string, string>;
+  readonly remoteAddress: string;
+};
+
+export type InternalResult = {
+  readonly type: "v1" | "v2" | "cf";
+  statusCode: number;
+  headers: Record<string, string | string[]>;
+  body: string;
+  isBase64Encoded: boolean;
+};
 
 export interface DangerousOptions {
   /**
diff --git a/packages/open-next/src/adapters/types/plugin.ts b/packages/open-next/src/types/plugin.ts
similarity index 81%
rename from packages/open-next/src/adapters/types/plugin.ts
rename to packages/open-next/src/types/plugin.ts
index a2ece3e91..22fd00e82 100644
--- a/packages/open-next/src/adapters/types/plugin.ts
+++ b/packages/open-next/src/types/plugin.ts
@@ -1,6 +1,6 @@
-import type { InternalEvent, InternalResult } from "../event-mapper";
-import { OpenNextNodeResponse } from "../http/openNextResponse";
-import type { IncomingMessage } from "../http/request";
+import { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
+
+import { InternalEvent, InternalResult } from "./open-next";
 
 export type ProcessInternalEventResult<Response extends OpenNextNodeResponse> =
   | {
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index a1a61e36a..198ee8c12 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -2,11 +2,10 @@ import { Writable } from "node:stream";
 import zlib from "node:zlib";
 
 import { APIGatewayProxyEventV2 } from "aws-lambda";
+import { StreamCreator } from "http/index.js";
+import { parseCookies, parseHeaders } from "http/util.js";
+import { Wrapper } from "types/open-next";
 
-import { StreamCreator } from "../adapters/http/openNextResponse";
-import { parseHeaders } from "../adapters/http/util";
-import { Wrapper } from "../adapters/types/open-next";
-import { parseCookies } from "../adapters/util";
 import { WarmerEvent } from "../adapters/warmer-function";
 
 type AwsLambdaEvent = APIGatewayProxyEventV2 | WarmerEvent;
diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index 709843ec5..ba0028529 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -1,4 +1,4 @@
-import {
+import type {
   APIGatewayProxyEvent,
   APIGatewayProxyEventV2,
   APIGatewayProxyResult,
@@ -6,8 +6,8 @@ import {
   CloudFrontRequestEvent,
   CloudFrontRequestResult,
 } from "aws-lambda";
+import type { Wrapper } from "types/open-next";
 
-import { Wrapper } from "../adapters/types/open-next";
 import { WarmerEvent } from "../adapters/warmer-function";
 
 type AwsLambdaEvent =
diff --git a/packages/open-next/src/wrappers/docker.ts b/packages/open-next/src/wrappers/docker.ts
index cb36c66ab..1908d2f2a 100644
--- a/packages/open-next/src/wrappers/docker.ts
+++ b/packages/open-next/src/wrappers/docker.ts
@@ -1,9 +1,9 @@
 import debug from "debug";
 import { createServer } from "http";
+import { StreamCreator } from "http/index.js";
+import type { Wrapper } from "types/open-next";
 
-import { StreamCreator } from "../adapters/http/openNextResponse";
 import { error } from "../adapters/logger";
-import { Wrapper } from "../adapters/types/open-next";
 
 const wrapper: Wrapper = async (handler, converter) => {
   const server = createServer(async (req, res) => {
diff --git a/packages/open-next/tsconfig.json b/packages/open-next/tsconfig.json
index 6d8322954..c4c987fd4 100644
--- a/packages/open-next/tsconfig.json
+++ b/packages/open-next/tsconfig.json
@@ -5,6 +5,11 @@
     "module": "esnext",
     "lib": ["DOM", "ESNext"],
     "outDir": "./dist",
-    "allowSyntheticDefaultImports": true
+    "allowSyntheticDefaultImports": true,
+    "paths": {
+      "types/*": ["./src/types/*"],
+      "config/*": ["./src/adapters/config/*"],
+      "http/*": ["./src/http/*"],
+    }
   }
 }

From 84132709d47d70e5dfa9499eb86951ad431cee4a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 21 Nov 2023 11:52:43 +0100
Subject: [PATCH 016/102] refactor: cleanup plugins

added a deletes options in open-next plugin
---
 .../adapters/plugins/13.5/serverHandler.ts    |  24 --
 .../src/adapters/plugins/13.5/util.ts         |   5 -
 .../src/adapters/plugins/routing/util.ts      | 266 ------------------
 .../plugins/serverHandler.replacement.ts      |  28 --
 .../src/adapters/plugins/serverHandler.ts     |  20 --
 .../src/adapters/plugins/util.replacement.ts  |  25 --
 .../plugins/without-routing/requestHandler.ts |  14 +
 packages/open-next/src/build.ts               |  81 ++----
 packages/open-next/src/core/requestHandler.ts |  27 +-
 .../src/{adapters => core}/require-hooks.ts   |   2 +-
 packages/open-next/src/core/routing/util.ts   | 265 ++++++++++++++++-
 .../src/{adapters/plugins => core}/util.ts    |  13 +-
 packages/open-next/src/plugin.ts              |  28 +-
 13 files changed, 353 insertions(+), 445 deletions(-)
 delete mode 100644 packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/13.5/util.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/routing/util.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/serverHandler.ts
 delete mode 100644 packages/open-next/src/adapters/plugins/util.replacement.ts
 create mode 100644 packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
 rename packages/open-next/src/{adapters => core}/require-hooks.ts (99%)
 rename packages/open-next/src/{adapters/plugins => core}/util.ts (89%)

diff --git a/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts b/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
deleted file mode 100644
index 099a7e3ea..000000000
--- a/packages/open-next/src/adapters/plugins/13.5/serverHandler.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-/*eslint-disable simple-import-sort/imports */
-import type { Options, PluginHandler } from "types/next-types.js";
-import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
-//#override imports
-//@ts-ignore
-import { requestHandler } from "./util.js";
-//@ts-ignore
-import { proxyRequest } from "./routing/util.js";
-//#endOverride
-
-//#override handler
-export const handler: PluginHandler = async (
-  req: IncomingMessage,
-  res: OpenNextNodeResponse,
-  options: Options,
-) => {
-  if (options.isExternalRewrite) {
-    return proxyRequest(req, res);
-  } else {
-    // Next Server
-    return requestHandler(req, res);
-  }
-};
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/13.5/util.ts b/packages/open-next/src/adapters/plugins/13.5/util.ts
deleted file mode 100644
index 4bf53b923..000000000
--- a/packages/open-next/src/adapters/plugins/13.5/util.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { debug } from "../../logger.js";
-
-//#override requireHooks
-debug("No need to override require hooks with next 13.4.20+");
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/routing/util.ts b/packages/open-next/src/adapters/plugins/routing/util.ts
deleted file mode 100644
index 1bb6c6d18..000000000
--- a/packages/open-next/src/adapters/plugins/routing/util.ts
+++ /dev/null
@@ -1,266 +0,0 @@
-import { BuildId, HtmlPages } from "config/index.js";
-import crypto from "crypto";
-import { OutgoingHttpHeaders, ServerResponse } from "http";
-import {
-  IncomingMessage,
-  OpenNextNodeResponse,
-  StreamCreator,
-} from "http/index.js";
-import { InternalEvent } from "types/open-next";
-
-import { debug } from "../../logger.js";
-
-declare global {
-  var openNextDebug: boolean;
-  var openNextVersion: string;
-  var lastModified: number;
-}
-
-enum CommonHeaders {
-  CACHE_CONTROL = "cache-control",
-  NEXT_CACHE = "x-nextjs-cache",
-}
-
-export async function proxyRequest(
-  req: IncomingMessage,
-  res: OpenNextNodeResponse,
-) {
-  // TODO: we should use our own version instead of the one bundled with Next.js
-  const HttpProxy = require("next/dist/compiled/http-proxy") as any;
-
-  const proxy = new HttpProxy({
-    changeOrigin: true,
-    ignorePath: true,
-    xfwd: true,
-  });
-
-  await new Promise<void>((resolve, reject) => {
-    proxy.on("proxyRes", (proxyRes: ServerResponse) => {
-      const body: Uint8Array[] = [];
-      proxyRes.on("data", function (chunk) {
-        body.push(chunk);
-      });
-      proxyRes.on("end", function () {
-        const newBody = Buffer.concat(body).toString();
-        debug(`Proxying response`, {
-          // @ts-ignore TODO: get correct type for the proxyRes
-          headers: proxyRes.getHeaders?.() || proxyRes.headers,
-          body: newBody,
-        });
-        res.end(newBody);
-        resolve();
-      });
-    });
-
-    proxy.on("error", (err: any) => {
-      reject(err);
-    });
-
-    debug(`Proxying`, { url: req.url, headers: req.headers });
-
-    proxy.web(req, res, {
-      target: req.url,
-      headers: req.headers,
-    });
-  });
-}
-
-export function fixCacheHeaderForHtmlPages(
-  rawPath: string,
-  headers: OutgoingHttpHeaders,
-) {
-  // WORKAROUND: `NextServer` does not set cache headers for HTML pages — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-cache-headers-for-html-pages
-  if (HtmlPages.includes(rawPath) && headers[CommonHeaders.CACHE_CONTROL]) {
-    headers[CommonHeaders.CACHE_CONTROL] =
-      "public, max-age=0, s-maxage=31536000, must-revalidate";
-  }
-}
-
-export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
-  // WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
-  let cacheControl = headers[CommonHeaders.CACHE_CONTROL];
-  if (!cacheControl) return;
-  if (Array.isArray(cacheControl)) {
-    cacheControl = cacheControl.join(",");
-  }
-  if (typeof cacheControl !== "string") return;
-  headers[CommonHeaders.CACHE_CONTROL] = cacheControl.replace(
-    /\bstale-while-revalidate(?!=)/,
-    "stale-while-revalidate=2592000", // 30 days
-  );
-}
-
-export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
-  headers["X-OpenNext"] = "1";
-  if (globalThis.openNextDebug) {
-    headers["X-OpenNext-Version"] = globalThis.openNextVersion;
-  }
-}
-
-export async function revalidateIfRequired(
-  host: string,
-  rawPath: string,
-  headers: OutgoingHttpHeaders,
-  req?: IncomingMessage,
-) {
-  fixISRHeaders(headers);
-
-  if (headers[CommonHeaders.NEXT_CACHE] === "STALE") {
-    // If the URL is rewritten, revalidation needs to be done on the rewritten URL.
-    // - Link to Next.js doc: https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation
-    // - Link to NextInternalRequestMeta: https://github.com/vercel/next.js/blob/57ab2818b93627e91c937a130fb56a36c41629c3/packages/next/src/server/request-meta.ts#L11
-    // @ts-ignore
-    const internalMeta = req?.[Symbol.for("NextInternalRequestMeta")];
-
-    // When using Pages Router, two requests will be received:
-    // 1. one for the page: /foo
-    // 2. one for the json data: /_next/data/BUILD_ID/foo.json
-    // The rewritten url is correct for 1, but that for the second request
-    // does not include the "/_next/data/" prefix. Need to add it.
-    const revalidateUrl = internalMeta?._nextDidRewrite
-      ? rawPath.startsWith("/_next/data/")
-        ? `/_next/data/${BuildId}${internalMeta?._nextRewroteUrl}.json`
-        : internalMeta?._nextRewroteUrl
-      : rawPath;
-
-    // We need to pass etag to the revalidation queue to try to bypass the default 5 min deduplication window.
-    // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html
-    // If you need to have a revalidation happen more frequently than 5 minutes,
-    // your page will need to have a different etag to bypass the deduplication window.
-    // If data has the same etag during these 5 min dedup window, it will be deduplicated and not revalidated.
-    try {
-      const hash = (str: string) =>
-        crypto.createHash("md5").update(str).digest("hex");
-
-      const lastModified =
-        globalThis.lastModified > 0 ? globalThis.lastModified : "";
-
-      // await sqsClient.send(
-      //   new SendMessageCommand({
-      //     QueueUrl: REVALIDATION_QUEUE_URL,
-      //     MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
-      //     MessageBody: JSON.stringify({ host, url: revalidateUrl }),
-      //     MessageGroupId: generateMessageGroupId(rawPath),
-      //   }),
-      // );
-
-      await globalThis.queue.send({
-        MessageBody: { host, url: revalidateUrl },
-        MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
-        MessageGroupId: generateMessageGroupId(rawPath),
-      });
-    } catch (e) {
-      debug(`Failed to revalidate stale page ${rawPath}`);
-      debug(e);
-    }
-  }
-}
-
-// Since we're using a FIFO queue, every messageGroupId is treated sequentially
-// This could cause a backlog of messages in the queue if there is too much page to
-// revalidate at once. To avoid this, we generate a random messageGroupId for each
-// revalidation request.
-// We can't just use a random string because we need to ensure that the same rawPath
-// will always have the same messageGroupId.
-// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#answer-47593316
-function generateMessageGroupId(rawPath: string) {
-  let a = cyrb128(rawPath);
-  // We use mulberry32 to generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
-  var t = (a += 0x6d2b79f5);
-  t = Math.imul(t ^ (t >>> 15), t | 1);
-  t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
-  const randomFloat = ((t ^ (t >>> 14)) >>> 0) / 4294967296;
-  // This will generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
-  // This means that we could have 1000 revalidate request at the same time
-  const maxConcurrency = parseInt(
-    process.env.MAX_REVALIDATE_CONCURRENCY ?? "10",
-  );
-  const randomInt = Math.floor(randomFloat * maxConcurrency);
-  return `revalidate-${randomInt}`;
-}
-
-// Used to generate a hash int from a string
-function cyrb128(str: string) {
-  let h1 = 1779033703,
-    h2 = 3144134277,
-    h3 = 1013904242,
-    h4 = 2773480762;
-  for (let i = 0, k; i < str.length; i++) {
-    k = str.charCodeAt(i);
-    h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
-    h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
-    h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
-    h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
-  }
-  h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
-  h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
-  h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
-  h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
-  (h1 ^= h2 ^ h3 ^ h4), (h2 ^= h1), (h3 ^= h1), (h4 ^= h1);
-  return h1 >>> 0;
-}
-
-export function fixISRHeaders(headers: OutgoingHttpHeaders) {
-  if (headers[CommonHeaders.NEXT_CACHE] === "REVALIDATED") {
-    headers[CommonHeaders.CACHE_CONTROL] =
-      "private, no-cache, no-store, max-age=0, must-revalidate";
-    return;
-  }
-  if (
-    headers[CommonHeaders.NEXT_CACHE] === "HIT" &&
-    globalThis.lastModified > 0
-  ) {
-    // calculate age
-    const age = Math.round((Date.now() - globalThis.lastModified) / 1000);
-    // extract s-maxage from cache-control
-    const regex = /s-maxage=(\d+)/;
-    const cacheControl = headers[CommonHeaders.CACHE_CONTROL];
-    debug("cache-control", cacheControl, globalThis.lastModified, Date.now());
-    if (typeof cacheControl !== "string") return;
-    const match = cacheControl.match(regex);
-    const sMaxAge = match ? parseInt(match[1]) : undefined;
-
-    // 31536000 is the default s-maxage value for SSG pages
-    if (sMaxAge && sMaxAge !== 31536000) {
-      const remainingTtl = Math.max(sMaxAge - age, 1);
-      headers[
-        CommonHeaders.CACHE_CONTROL
-      ] = `s-maxage=${remainingTtl}, stale-while-revalidate=2592000`;
-    }
-
-    // reset lastModified
-    globalThis.lastModified = 0;
-  }
-  if (headers[CommonHeaders.NEXT_CACHE] !== "STALE") return;
-
-  // If the cache is stale, we revalidate in the background
-  // In order for CloudFront SWR to work, we set the stale-while-revalidate value to 2 seconds
-  // This will cause CloudFront to cache the stale data for a short period of time while we revalidate in the background
-  // Once the revalidation is complete, CloudFront will serve the fresh data
-  headers[CommonHeaders.CACHE_CONTROL] =
-    "s-maxage=2, stale-while-revalidate=2592000";
-}
-
-export function createServerResponse(
-  internalEvent: InternalEvent,
-  headers: Record<string, string | string[] | undefined>,
-  responseStream?: StreamCreator,
-) {
-  return new OpenNextNodeResponse(
-    (_headers) => {
-      fixCacheHeaderForHtmlPages(internalEvent.rawPath, _headers);
-      fixSWRCacheHeader(_headers);
-      addOpenNextHeader(_headers);
-      fixISRHeaders(_headers);
-    },
-    async (_headers) => {
-      await revalidateIfRequired(
-        internalEvent.headers.host,
-        internalEvent.rawPath,
-        _headers,
-      );
-    },
-    responseStream,
-    headers,
-  );
-}
diff --git a/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts b/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
deleted file mode 100644
index 1059affce..000000000
--- a/packages/open-next/src/adapters/plugins/serverHandler.replacement.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-/*eslint-disable simple-import-sort/imports */
-import type { Options, PluginHandler } from "types/next-types.js";
-import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
-//#override imports
-
-import { proxyRequest } from "./routing/util.js";
-import { requestHandler, setNextjsPrebundledReact } from "./util.js";
-//#endOverride
-
-//#override handler
-export const handler: PluginHandler = async (
-  req: IncomingMessage,
-  res: OpenNextNodeResponse,
-  options: Options,
-) => {
-  let { internalEvent } = options;
-
-  const { rawPath } = internalEvent;
-
-  if (options.isExternalRewrite) {
-    return proxyRequest(req, res);
-  } else {
-    setNextjsPrebundledReact(rawPath);
-    // Next Server
-    return requestHandler(req, res);
-  }
-};
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/serverHandler.ts b/packages/open-next/src/adapters/plugins/serverHandler.ts
deleted file mode 100644
index 050ce2871..000000000
--- a/packages/open-next/src/adapters/plugins/serverHandler.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
-import type { Options, PluginHandler } from "types/next-types.js";
-
-//#override imports
-import { requestHandler, setNextjsPrebundledReact } from "./util.js";
-//#endOverride
-
-//TODO: refactor this, we don't need to override this anymore, we could use the replacement
-// and remove setNextjsPrebundledReact where we need to
-// It would be handy to change the plugin to allow delete without having to create a replacement file
-//#override handler
-export const handler: PluginHandler = async (
-  req: IncomingMessage,
-  res: OpenNextNodeResponse,
-  options: Options,
-) => {
-  setNextjsPrebundledReact(options.internalEvent.rawPath);
-  return requestHandler(req, res);
-};
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/util.replacement.ts b/packages/open-next/src/adapters/plugins/util.replacement.ts
deleted file mode 100644
index 585b9617c..000000000
--- a/packages/open-next/src/adapters/plugins/util.replacement.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import { NextConfig } from "config/index";
-
-//#override requestHandler
-// @ts-ignore
-export const requestHandler = new NextServer.default({
-  conf: {
-    ...NextConfig,
-    // Next.js compression should be disabled because of a bug in the bundled
-    // `compression` package — https://github.com/vercel/next.js/issues/11669
-    compress: false,
-    // By default, Next.js uses local disk to store ISR cache. We will use
-    // our own cache handler to store the cache on S3.
-    experimental: {
-      ...NextConfig.experimental,
-      // This uses the request.headers.host as the URL
-      // https://github.com/vercel/next.js/blob/canary/packages/next/src/server/next-server.ts#L1749-L1754
-      trustHostHeader: true,
-      incrementalCacheHandlerPath: `${process.env.LAMBDA_TASK_ROOT}/cache.cjs`,
-    },
-  },
-  customServer: false,
-  dev: false,
-  dir: __dirname,
-}).getRequestHandler();
-//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
new file mode 100644
index 000000000..b5e879dbc
--- /dev/null
+++ b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
@@ -0,0 +1,14 @@
+import { InternalEvent } from "types/open-next";
+
+import { MiddlewareOutputEvent } from "../../../core/routingHandler";
+// This is available in requestHandler.ts
+declare const internalEvent: InternalEvent;
+
+//#override withRouting
+// eslint-disable-next-line unused-imports/no-unused-vars
+const preprocessResult: MiddlewareOutputEvent = {
+  internalEvent: internalEvent,
+  isExternalRewrite: false,
+  headers: {},
+};
+//#endOverride
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 17347c17a..6709b9b5f 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -10,6 +10,7 @@ import {
   buildSync,
 } from "esbuild";
 
+import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import openNextPlugin from "./plugin.js";
 import { BuildOptions, DangerousOptions } from "./types/open-next.js";
@@ -602,59 +603,33 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   //       "serverless-http" package which is not a dependency in user's
   //       Next.js app.
 
-  let plugins =
-    compareSemver(options.nextVersion, "13.4.13") >= 0
-      ? [
-          openNextPlugin({
-            name: "opennext-13.4.13-serverHandler",
-            target: /plugins\/serverHandler\.js/g,
-            replacements: ["./serverHandler.replacement.js"],
-          }),
-          openNextPlugin({
-            name: "opennext-13.4.13-util",
-            target: /plugins\/util\.js/g,
-            replacements: ["./util.replacement.js"],
-          }),
-          openNextPlugin({
-            name: "opennext-13.4.13-default",
-            target: /plugins\/routing\/default\.js/g,
-            replacements: ["./default.replacement.js"],
-          }),
-        ]
-      : undefined;
-
-  if (compareSemver(options.nextVersion, "13.5.1") >= 0) {
-    plugins = [
-      openNextPlugin({
-        name: "opennext-13.5-serverHandler",
-        target: /plugins\/serverHandler\.js/g,
-        replacements: ["./13.5/serverHandler.js"],
-      }),
-      openNextPlugin({
-        name: "opennext-13.5-util",
-        target: /plugins\/util\.js/g,
-        replacements: ["./13.5/util.js", "./util.replacement.js"],
-      }),
-      openNextPlugin({
-        name: "opennext-13.5-default",
-        target: /plugins\/routing\/default\.js/g,
-        replacements: ["./default.replacement.js"],
-      }),
-    ];
-  }
-
-  if (streaming) {
-    // const streamingPlugin = openNextPlugin({
-    //   name: "opennext-streaming",
-    //   target: /plugins\/lambdaHandler\.js/g,
-    //   replacements: ["./streaming.replacement.js"],
-    // });
-    // if (plugins) {
-    //   plugins.push(streamingPlugin);
-    // } else {
-    //   plugins = [streamingPlugin];
-    // }
-  }
+  const disableNextPrebundledReact =
+    compareSemver(options.nextVersion, "13.5.1") >= 0 ||
+    compareSemver(options.nextVersion, "13.4.1") <= 0;
+
+  const disableRouting = compareSemver(options.nextVersion, "13.4.13") <= 0;
+  const plugins = [
+    openNextPlugin({
+      name: "requestHandlerOverride",
+      target: /core\/requestHandler.js/g,
+      deletes: disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : [],
+      replacements: disableRouting
+        ? [
+            require.resolve(
+              "./adapters/plugins/without-routing/requestHandler.js",
+            ),
+          ]
+        : [],
+    }),
+    openNextPlugin({
+      name: "core/util",
+      target: /core\/util.js/g,
+      deletes: [
+        ...(disableNextPrebundledReact ? ["requireHooks"] : []),
+        ...(disableRouting ? ["trustHostHeader"] : []),
+      ],
+    }),
+  ];
 
   if (plugins && plugins.length > 0) {
     logger.debug(
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index c2a0b46e2..011b0cf00 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -1,4 +1,3 @@
-import { BuildId } from "config/index";
 import {
   IncomingMessage,
   OpenNextNodeResponse,
@@ -7,10 +6,9 @@ import {
 import { InternalEvent, InternalResult } from "types/open-next";
 
 import { error } from "../adapters/logger";
-import { createServerResponse } from "../adapters/plugins/routing/util";
-import { handler as serverHandler } from "../adapters/plugins/serverHandler";
-import { convertRes } from "./routing/util";
+import { convertRes, createServerResponse, proxyRequest } from "./routing/util";
 import routingHandler from "./routingHandler";
+import { requestHandler, setNextjsPrebundledReact } from "./util";
 
 export async function openNextHandler(
   internalEvent: InternalEvent,
@@ -20,8 +18,9 @@ export async function openNextHandler(
     internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
   }
 
-  //TODO: replace this line for next <= 13.4.12
+  //#override withRouting
   const preprocessResult = await routingHandler(internalEvent);
+  //#endOverride
 
   if ("type" in preprocessResult) {
     // res is used only in the streaming case
@@ -88,11 +87,19 @@ async function processRequest(
   try {
     // `serverHandler` is replaced at build time depending on user's
     // nextjs version to patch Nextjs 13.4.x and future breaking changes.
-    await serverHandler(req, res, {
-      internalEvent,
-      buildId: BuildId,
-      isExternalRewrite,
-    });
+
+    const { rawPath } = internalEvent;
+
+    if (isExternalRewrite) {
+      return proxyRequest(req, res);
+    } else {
+      //#override applyNextjsPrebundledReact
+      setNextjsPrebundledReact(rawPath);
+      //#endOverride
+
+      // Next Server
+      await requestHandler(req, res);
+    }
   } catch (e: any) {
     error("NextJS request failed.", e);
     //TODO: we could return the next 500 page here
diff --git a/packages/open-next/src/adapters/require-hooks.ts b/packages/open-next/src/core/require-hooks.ts
similarity index 99%
rename from packages/open-next/src/adapters/require-hooks.ts
rename to packages/open-next/src/core/require-hooks.ts
index 2dfb27dcc..963dbfa96 100644
--- a/packages/open-next/src/adapters/require-hooks.ts
+++ b/packages/open-next/src/core/require-hooks.ts
@@ -4,7 +4,7 @@
 
 import type { NextConfig } from "types/next-types.js";
 
-import { error } from "./logger.js";
+import { error } from "../adapters/logger.js";
 
 // This module will only be loaded once per process.
 
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index bc800b879..68c63c21e 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -1,11 +1,20 @@
+import crypto from "node:crypto";
 import fs from "node:fs";
+import { OutgoingHttpHeaders, ServerResponse } from "node:http";
 import path from "node:path";
 
-import { OpenNextNodeResponse } from "http/index.js";
+import { BuildId, HtmlPages } from "config/index.js";
+import {
+  IncomingMessage,
+  OpenNextNodeResponse,
+  StreamCreator,
+} from "http/index.js";
 import { parseHeaders } from "http/util.js";
 import type { MiddlewareManifest } from "types/next-types";
+import { InternalEvent } from "types/open-next.js";
 
 import { isBinaryContentType } from "../../adapters/binary.js";
+import { debug } from "../../adapters/logger.js";
 
 export function isExternal(url?: string, host?: string) {
   if (!url) return false;
@@ -121,3 +130,257 @@ export function unescapeRegex(str: string) {
 
   return path;
 }
+
+export async function proxyRequest(
+  req: IncomingMessage,
+  res: OpenNextNodeResponse,
+) {
+  // TODO: we should use our own version instead of the one bundled with Next.js
+  const HttpProxy = require("next/dist/compiled/http-proxy") as any;
+
+  const proxy = new HttpProxy({
+    changeOrigin: true,
+    ignorePath: true,
+    xfwd: true,
+  });
+
+  await new Promise<void>((resolve, reject) => {
+    proxy.on("proxyRes", (proxyRes: ServerResponse) => {
+      const body: Uint8Array[] = [];
+      proxyRes.on("data", function (chunk) {
+        body.push(chunk);
+      });
+      proxyRes.on("end", function () {
+        const newBody = Buffer.concat(body).toString();
+        debug(`Proxying response`, {
+          headers: proxyRes.getHeaders(),
+          body: newBody,
+        });
+        res.end(newBody);
+        resolve();
+      });
+    });
+
+    proxy.on("error", (err: any) => {
+      reject(err);
+    });
+
+    debug(`Proxying`, { url: req.url, headers: req.headers });
+
+    proxy.web(req, res, {
+      target: req.url,
+      headers: req.headers,
+    });
+  });
+}
+
+declare global {
+  var openNextDebug: boolean;
+  var openNextVersion: string;
+  var lastModified: number;
+}
+
+enum CommonHeaders {
+  CACHE_CONTROL = "cache-control",
+  NEXT_CACHE = "x-nextjs-cache",
+}
+
+export function fixCacheHeaderForHtmlPages(
+  rawPath: string,
+  headers: OutgoingHttpHeaders,
+) {
+  // WORKAROUND: `NextServer` does not set cache headers for HTML pages — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-cache-headers-for-html-pages
+  if (HtmlPages.includes(rawPath) && headers[CommonHeaders.CACHE_CONTROL]) {
+    headers[CommonHeaders.CACHE_CONTROL] =
+      "public, max-age=0, s-maxage=31536000, must-revalidate";
+  }
+}
+
+export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
+  // WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
+  let cacheControl = headers[CommonHeaders.CACHE_CONTROL];
+  if (!cacheControl) return;
+  if (Array.isArray(cacheControl)) {
+    cacheControl = cacheControl.join(",");
+  }
+  if (typeof cacheControl !== "string") return;
+  headers[CommonHeaders.CACHE_CONTROL] = cacheControl.replace(
+    /\bstale-while-revalidate(?!=)/,
+    "stale-while-revalidate=2592000", // 30 days
+  );
+}
+
+export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
+  headers["X-OpenNext"] = "1";
+  if (globalThis.openNextDebug) {
+    headers["X-OpenNext-Version"] = globalThis.openNextVersion;
+  }
+}
+
+export async function revalidateIfRequired(
+  host: string,
+  rawPath: string,
+  headers: OutgoingHttpHeaders,
+  req?: IncomingMessage,
+) {
+  fixISRHeaders(headers);
+
+  if (headers[CommonHeaders.NEXT_CACHE] === "STALE") {
+    // If the URL is rewritten, revalidation needs to be done on the rewritten URL.
+    // - Link to Next.js doc: https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation
+    // - Link to NextInternalRequestMeta: https://github.com/vercel/next.js/blob/57ab2818b93627e91c937a130fb56a36c41629c3/packages/next/src/server/request-meta.ts#L11
+    // @ts-ignore
+    const internalMeta = req?.[Symbol.for("NextInternalRequestMeta")];
+
+    // When using Pages Router, two requests will be received:
+    // 1. one for the page: /foo
+    // 2. one for the json data: /_next/data/BUILD_ID/foo.json
+    // The rewritten url is correct for 1, but that for the second request
+    // does not include the "/_next/data/" prefix. Need to add it.
+    const revalidateUrl = internalMeta?._nextDidRewrite
+      ? rawPath.startsWith("/_next/data/")
+        ? `/_next/data/${BuildId}${internalMeta?._nextRewroteUrl}.json`
+        : internalMeta?._nextRewroteUrl
+      : rawPath;
+
+    // We need to pass etag to the revalidation queue to try to bypass the default 5 min deduplication window.
+    // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html
+    // If you need to have a revalidation happen more frequently than 5 minutes,
+    // your page will need to have a different etag to bypass the deduplication window.
+    // If data has the same etag during these 5 min dedup window, it will be deduplicated and not revalidated.
+    try {
+      const hash = (str: string) =>
+        crypto.createHash("md5").update(str).digest("hex");
+
+      const lastModified =
+        globalThis.lastModified > 0 ? globalThis.lastModified : "";
+
+      // await sqsClient.send(
+      //   new SendMessageCommand({
+      //     QueueUrl: REVALIDATION_QUEUE_URL,
+      //     MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
+      //     MessageBody: JSON.stringify({ host, url: revalidateUrl }),
+      //     MessageGroupId: generateMessageGroupId(rawPath),
+      //   }),
+      // );
+
+      await globalThis.queue.send({
+        MessageBody: { host, url: revalidateUrl },
+        MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
+        MessageGroupId: generateMessageGroupId(rawPath),
+      });
+    } catch (e) {
+      debug(`Failed to revalidate stale page ${rawPath}`);
+      debug(e);
+    }
+  }
+}
+
+// Since we're using a FIFO queue, every messageGroupId is treated sequentially
+// This could cause a backlog of messages in the queue if there is too much page to
+// revalidate at once. To avoid this, we generate a random messageGroupId for each
+// revalidation request.
+// We can't just use a random string because we need to ensure that the same rawPath
+// will always have the same messageGroupId.
+// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#answer-47593316
+function generateMessageGroupId(rawPath: string) {
+  let a = cyrb128(rawPath);
+  // We use mulberry32 to generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
+  var t = (a += 0x6d2b79f5);
+  t = Math.imul(t ^ (t >>> 15), t | 1);
+  t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
+  const randomFloat = ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+  // This will generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
+  // This means that we could have 1000 revalidate request at the same time
+  const maxConcurrency = parseInt(
+    process.env.MAX_REVALIDATE_CONCURRENCY ?? "10",
+  );
+  const randomInt = Math.floor(randomFloat * maxConcurrency);
+  return `revalidate-${randomInt}`;
+}
+
+// Used to generate a hash int from a string
+function cyrb128(str: string) {
+  let h1 = 1779033703,
+    h2 = 3144134277,
+    h3 = 1013904242,
+    h4 = 2773480762;
+  for (let i = 0, k; i < str.length; i++) {
+    k = str.charCodeAt(i);
+    h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
+    h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
+    h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
+    h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
+  }
+  h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
+  h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
+  h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
+  h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
+  (h1 ^= h2 ^ h3 ^ h4), (h2 ^= h1), (h3 ^= h1), (h4 ^= h1);
+  return h1 >>> 0;
+}
+
+export function fixISRHeaders(headers: OutgoingHttpHeaders) {
+  if (headers[CommonHeaders.NEXT_CACHE] === "REVALIDATED") {
+    headers[CommonHeaders.CACHE_CONTROL] =
+      "private, no-cache, no-store, max-age=0, must-revalidate";
+    return;
+  }
+  if (
+    headers[CommonHeaders.NEXT_CACHE] === "HIT" &&
+    globalThis.lastModified > 0
+  ) {
+    // calculate age
+    const age = Math.round((Date.now() - globalThis.lastModified) / 1000);
+    // extract s-maxage from cache-control
+    const regex = /s-maxage=(\d+)/;
+    const cacheControl = headers[CommonHeaders.CACHE_CONTROL];
+    debug("cache-control", cacheControl, globalThis.lastModified, Date.now());
+    if (typeof cacheControl !== "string") return;
+    const match = cacheControl.match(regex);
+    const sMaxAge = match ? parseInt(match[1]) : undefined;
+
+    // 31536000 is the default s-maxage value for SSG pages
+    if (sMaxAge && sMaxAge !== 31536000) {
+      const remainingTtl = Math.max(sMaxAge - age, 1);
+      headers[
+        CommonHeaders.CACHE_CONTROL
+      ] = `s-maxage=${remainingTtl}, stale-while-revalidate=2592000`;
+    }
+
+    // reset lastModified
+    globalThis.lastModified = 0;
+  }
+  if (headers[CommonHeaders.NEXT_CACHE] !== "STALE") return;
+
+  // If the cache is stale, we revalidate in the background
+  // In order for CloudFront SWR to work, we set the stale-while-revalidate value to 2 seconds
+  // This will cause CloudFront to cache the stale data for a short period of time while we revalidate in the background
+  // Once the revalidation is complete, CloudFront will serve the fresh data
+  headers[CommonHeaders.CACHE_CONTROL] =
+    "s-maxage=2, stale-while-revalidate=2592000";
+}
+
+export function createServerResponse(
+  internalEvent: InternalEvent,
+  headers: Record<string, string | string[] | undefined>,
+  responseStream?: StreamCreator,
+) {
+  return new OpenNextNodeResponse(
+    (_headers) => {
+      fixCacheHeaderForHtmlPages(internalEvent.rawPath, _headers);
+      fixSWRCacheHeader(_headers);
+      addOpenNextHeader(_headers);
+      fixISRHeaders(_headers);
+    },
+    async (_headers) => {
+      await revalidateIfRequired(
+        internalEvent.headers.host,
+        internalEvent.rawPath,
+        _headers,
+      );
+    },
+    responseStream,
+    headers,
+  );
+}
diff --git a/packages/open-next/src/adapters/plugins/util.ts b/packages/open-next/src/core/util.ts
similarity index 89%
rename from packages/open-next/src/adapters/plugins/util.ts
rename to packages/open-next/src/core/util.ts
index 373cc48c3..6f800a3d9 100644
--- a/packages/open-next/src/adapters/plugins/util.ts
+++ b/packages/open-next/src/core/util.ts
@@ -10,11 +10,11 @@ import {
 import NextServer from "next/dist/server/next-server.js";
 import type { MiddlewareManifest } from "types/next-types.js";
 
-import { debug } from "../logger.js";
+import { debug } from "../adapters/logger.js";
 import {
   applyOverride as applyNextjsRequireHooksOverride,
   overrideHooks as overrideNextjsRequireHooks,
-} from "../require-hooks.js";
+} from "./require-hooks.js";
 
 // WORKAROUND: Set `__NEXT_PRIVATE_PREBUNDLED_REACT` to use prebundled React — https://github.com/serverless-stack/open-next#workaround-set-__next_private_prebundled_react-to-use-prebundled-react
 // Step 1: Need to override the require hooks for React before Next.js server
@@ -28,7 +28,6 @@ overrideNextjsRequireHooks(NextConfig);
 applyNextjsRequireHooksOverride();
 //#endOverride
 
-//#override requestHandler
 // @ts-ignore
 export const requestHandler = new NextServer.default({
   hostname: "localhost",
@@ -42,6 +41,13 @@ export const requestHandler = new NextServer.default({
     // our own cache handler to store the cache on S3.
     experimental: {
       ...NextConfig.experimental,
+      // This uses the request.headers.host as the URL
+      // https://github.com/vercel/next.js/blob/canary/packages/next/src/server/next-server.ts#L1749-L1754
+      //#override trustHostHeader
+      trustHostHeader: true,
+      //#endOverride
+
+      //TODO: change env.LAMBDA_TASK_ROOT
       incrementalCacheHandlerPath: `${process.env.LAMBDA_TASK_ROOT}/cache.cjs`,
     },
   },
@@ -49,7 +55,6 @@ export const requestHandler = new NextServer.default({
   dev: false,
   dir: __dirname,
 }).getRequestHandler();
-//#endOverride
 
 export function getMiddlewareMatch(middlewareManifest: MiddlewareManifest) {
   const rootMiddleware = middlewareManifest.middleware["/"];
diff --git a/packages/open-next/src/plugin.ts b/packages/open-next/src/plugin.ts
index 90060f36a..f50bd43a1 100644
--- a/packages/open-next/src/plugin.ts
+++ b/packages/open-next/src/plugin.ts
@@ -1,5 +1,4 @@
 import { readFile } from "node:fs/promises";
-import path from "node:path";
 
 import { Plugin } from "esbuild";
 
@@ -7,7 +6,8 @@ import logger from "./logger.js";
 
 export interface IPluginSettings {
   target: RegExp;
-  replacements: string[];
+  replacements?: string[];
+  deletes?: string[];
   name?: string;
 }
 
@@ -18,7 +18,8 @@ const importPattern = /\/\/#import([\s\S]*?)\n\/\/#endImport/gm;
  *
  * openNextPlugin({
  *   target: /plugins\/default\.js/g,
- *   replacements: ["./13_4_13.js"],
+ *   replacements: [require.resolve("./plugins/default.js")],
+ *   deletes: ["id1"],
  * })
  *
  * To inject arbritary code by using (import at top of file):
@@ -43,12 +44,14 @@ const importPattern = /\/\/#import([\s\S]*?)\n\/\/#endImport/gm;
  *
  * @param opts.target - the target file to replace
  * @param opts.replacements - list of files used to replace the imports/overrides in the target
- *                          - the path is relative to the target path
+ *                          - the path is absolute
+ * @param opts.deletes - list of ids to delete from the target
  * @returns
  */
 export default function openNextPlugin({
   target,
   replacements,
+  deletes,
   name,
 }: IPluginSettings): Plugin {
   return {
@@ -57,9 +60,18 @@ export default function openNextPlugin({
       build.onLoad({ filter: target }, async (args) => {
         let contents = await readFile(args.path, "utf-8");
 
-        await Promise.all(
-          replacements.map(async (fp) => {
-            const p = path.join(args.path, "..", fp);
+        await Promise.all([
+          ...(deletes ?? []).map(async (id) => {
+            const pattern = new RegExp(
+              `\/\/#override (${id})\n([\\s\\S]*?)\/\/#endOverride`,
+            );
+            logger.debug(
+              `Open-next plugin ${name} -- Deleting override for ${id}`,
+            );
+            contents = contents.replace(pattern, "");
+          }),
+          ...(replacements ?? []).map(async (fp) => {
+            const p = fp;
             const replacementFile = await readFile(p, "utf-8");
             const matches = replacementFile.matchAll(overridePattern);
 
@@ -80,7 +92,7 @@ export default function openNextPlugin({
               contents = contents.replace(pattern, replacement);
             }
           }),
-        );
+        ]);
 
         return {
           contents,

From a063596b8ea8e549e13ceb3fbaed32e11b4edb19 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 23 Nov 2023 11:26:34 +0100
Subject: [PATCH 017/102] make other lambdas overridable as well

---
 .../open-next/src/adapters/dynamo-provider.ts |  87 +++++-----
 .../adapters/image-optimization-adapter.ts    | 104 +++++++-----
 packages/open-next/src/adapters/revalidate.ts |  26 ++-
 .../open-next/src/adapters/warmer-function.ts | 107 +++++++-----
 packages/open-next/src/build.ts               |  46 ++++-
 packages/open-next/src/cache/tag/dynamoDb.ts  |  10 +-
 packages/open-next/src/cache/tag/types.ts     |   4 +-
 .../open-next/src/converters/aws-apigw-v1.ts  |   2 +-
 .../open-next/src/converters/aws-apigw-v2.ts  |   2 +-
 .../src/converters/aws-cloudfront.ts          |   2 +-
 packages/open-next/src/converters/docker.ts   |   2 +-
 packages/open-next/src/converters/dummy.ts    |  23 +++
 .../src/converters/sqs-revalidate.ts          |  24 +++
 .../src/core/createGenericHandler.ts          |  59 +++++++
 .../open-next/src/core/createMainHandler.ts   |  48 +-----
 packages/open-next/src/core/requestHandler.ts |   2 +-
 packages/open-next/src/core/resolve.ts        |  61 +++++++
 packages/open-next/src/types/open-next.ts     | 160 +++++++++++++-----
 18 files changed, 543 insertions(+), 226 deletions(-)
 create mode 100644 packages/open-next/src/converters/dummy.ts
 create mode 100644 packages/open-next/src/converters/sqs-revalidate.ts
 create mode 100644 packages/open-next/src/core/createGenericHandler.ts
 create mode 100644 packages/open-next/src/core/resolve.ts

diff --git a/packages/open-next/src/adapters/dynamo-provider.ts b/packages/open-next/src/adapters/dynamo-provider.ts
index d2e677c58..0b267ae9a 100644
--- a/packages/open-next/src/adapters/dynamo-provider.ts
+++ b/packages/open-next/src/adapters/dynamo-provider.ts
@@ -1,20 +1,16 @@
-import {
-  BatchWriteItemCommand,
-  DynamoDBClient,
-} from "@aws-sdk/client-dynamodb";
-import { CdkCustomResourceEvent, CdkCustomResourceResponse } from "aws-lambda";
 import { readFileSync } from "fs";
 
+import { createGenericHandler } from "../core/createGenericHandler.js";
+import { resolveTagCache } from "../core/resolve.js";
 import {
   getDynamoBatchWriteCommandConcurrency,
   MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT,
 } from "./constants.js";
 import { chunk } from "./util.js";
 
-const PHYSICAL_RESOURCE_ID = "dynamodb-cache";
-
-const dynamoClient = new DynamoDBClient({});
+const PHYSICAL_RESOURCE_ID = "dynamodb-cache" as const;
 
+//TODO: modify this, we should use the same format as the cache
 type DataType = {
   tag: {
     S: string;
@@ -27,62 +23,69 @@ type DataType = {
   };
 };
 
-export async function handler(
-  event: CdkCustomResourceEvent,
-): Promise<CdkCustomResourceResponse> {
-  switch (event.RequestType) {
-    case "Create":
-    case "Update":
-      return insert();
-    case "Delete":
+interface InitializationFunctionEvent {
+  type: "initializationFunction";
+  requestType: "create" | "update" | "delete";
+  resourceId: typeof PHYSICAL_RESOURCE_ID;
+}
+
+const tagCache = await resolveTagCache(
+  globalThis.openNextConfig.initializationFunction?.tagCache,
+);
+
+export const handler = createGenericHandler({
+  handler: defaultHandler,
+  type: "initializationFunction",
+});
+
+async function defaultHandler(
+  event: InitializationFunctionEvent,
+): Promise<InitializationFunctionEvent> {
+  switch (event.requestType) {
+    case "create":
+    case "update":
+      return insert(event.requestType);
+    case "delete":
       return remove();
   }
 }
 
-async function insert(): Promise<CdkCustomResourceResponse> {
-  const tableName = process.env.CACHE_DYNAMO_TABLE!;
-
+async function insert(
+  requestType: InitializationFunctionEvent["requestType"],
+): Promise<InitializationFunctionEvent> {
   const file = readFileSync(`dynamodb-cache.json`, "utf8");
 
   const data: DataType[] = JSON.parse(file);
 
-  const dataChunks = chunk(data, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT);
+  const parsedData = data.map((item) => ({
+    tag: item.tag.S,
+    path: item.path.S,
+    revalidatedAt: parseInt(item.revalidatedAt.N),
+  }));
 
-  const batchWriteParamsArray = dataChunks.map((chunk) => {
-    return {
-      RequestItems: {
-        [tableName]: chunk.map((item) => ({
-          PutRequest: {
-            Item: item,
-          },
-        })),
-      },
-    };
-  });
+  const dataChunks = chunk(parsedData, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT);
 
   const paramsChunks = chunk(
-    batchWriteParamsArray,
+    dataChunks,
     getDynamoBatchWriteCommandConcurrency(),
   );
 
   for (const paramsChunk of paramsChunks) {
-    await Promise.all(
-      paramsChunk.map((params) =>
-        dynamoClient.send(new BatchWriteItemCommand(params)),
-      ),
-    );
+    await Promise.all(paramsChunk.map((params) => tagCache.writeTags(params)));
   }
 
   return {
-    PhysicalResourceId: PHYSICAL_RESOURCE_ID,
-    Data: {},
+    type: "initializationFunction",
+    requestType,
+    resourceId: PHYSICAL_RESOURCE_ID,
   };
 }
 
-async function remove(): Promise<CdkCustomResourceResponse> {
+async function remove(): Promise<InitializationFunctionEvent> {
   // Do we want to actually delete anything here?
   return {
-    PhysicalResourceId: PHYSICAL_RESOURCE_ID,
-    Data: {},
+    type: "initializationFunction",
+    requestType: "delete",
+    resourceId: PHYSICAL_RESOURCE_ID,
   };
 }
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index 19f2f94e0..4d018a612 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -1,16 +1,14 @@
-import { IncomingMessage, ServerResponse } from "node:http";
+import {
+  IncomingMessage,
+  OutgoingHttpHeaders,
+  ServerResponse,
+} from "node:http";
 import https from "node:https";
 import path from "node:path";
 import { Writable } from "node:stream";
 
 import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
-import type {
-  APIGatewayProxyEvent,
-  APIGatewayProxyEventHeaders,
-  APIGatewayProxyEventQueryStringParameters,
-  APIGatewayProxyEventV2,
-  APIGatewayProxyResultV2,
-} from "aws-lambda";
+import type { APIGatewayProxyEventHeaders } from "aws-lambda";
 import { loadConfig } from "config/util.js";
 // @ts-ignore
 import { defaultConfig } from "next/dist/server/config-shared";
@@ -21,7 +19,9 @@ import {
 } from "next/dist/server/image-optimizer";
 // @ts-ignore
 import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
+import { InternalEvent, InternalResult } from "types/open-next.js";
 
+import { createGenericHandler } from "../core/createGenericHandler.js";
 import { awsLogger, debug, error } from "./logger.js";
 import { setNodeEnv } from "./util.js";
 
@@ -50,15 +50,20 @@ debug("Init config", {
 // Handler //
 /////////////
 
-export async function handler(
-  event: APIGatewayProxyEventV2 | APIGatewayProxyEvent,
-): Promise<APIGatewayProxyResultV2> {
+export const handler = createGenericHandler({
+  handler: defaultHandler,
+  type: "imageOptimization",
+});
+
+export async function defaultHandler(
+  event: InternalEvent,
+): Promise<InternalResult> {
   // Images are handled via header and query param information.
   debug("handler event", event);
-  const { headers: rawHeaders, queryStringParameters: queryString } = event;
+  const { headers, query: queryString } = event;
 
   try {
-    const headers = normalizeHeaderKeysToLowercase(rawHeaders);
+    // const headers = normalizeHeaderKeysToLowercase(rawHeaders);
     ensureBucketExists();
     const imageParams = validateImageParams(
       headers,
@@ -76,13 +81,13 @@ export async function handler(
 // Helper functions //
 //////////////////////
 
-function normalizeHeaderKeysToLowercase(headers: APIGatewayProxyEventHeaders) {
-  // Make header keys lowercase to ensure integrity
-  return Object.entries(headers).reduce(
-    (acc, [key, value]) => ({ ...acc, [key.toLowerCase()]: value }),
-    {} as APIGatewayProxyEventHeaders,
-  );
-}
+// function normalizeHeaderKeysToLowercase(headers: APIGatewayProxyEventHeaders) {
+//   // Make header keys lowercase to ensure integrity
+//   return Object.entries(headers).reduce(
+//     (acc, [key, value]) => ({ ...acc, [key.toLowerCase()]: value }),
+//     {} as APIGatewayProxyEventHeaders,
+//   );
+// }
 
 function ensureBucketExists() {
   if (!BUCKET_NAME) {
@@ -91,15 +96,15 @@ function ensureBucketExists() {
 }
 
 function validateImageParams(
-  headers: APIGatewayProxyEventHeaders,
-  queryString?: APIGatewayProxyEventQueryStringParameters,
+  headers: OutgoingHttpHeaders,
+  query?: InternalEvent["query"],
 ) {
   // Next.js checks if external image URL matches the
   // `images.remotePatterns`
   const imageParams = ImageOptimizerCache.validateParams(
     // @ts-ignore
     { headers },
-    queryString,
+    query,
     nextConfig,
     false,
   );
@@ -127,8 +132,9 @@ async function optimizeImage(
   return result;
 }
 
-function buildSuccessResponse(result: any) {
+function buildSuccessResponse(result: any): InternalResult {
   return {
+    type: "core",
     statusCode: 200,
     body: result.buffer.toString("base64"),
     isBase64Encoded: true,
@@ -140,9 +146,11 @@ function buildSuccessResponse(result: any) {
   };
 }
 
-function buildFailureResponse(e: any) {
+function buildFailureResponse(e: any): InternalResult {
   debug(e);
   return {
+    type: "core",
+    isBase64Encoded: false,
     statusCode: 500,
     headers: {
       Vary: "Accept",
@@ -154,6 +162,31 @@ function buildFailureResponse(e: any) {
   };
 }
 
+const resolveLoader = () => {
+  const openNextParams = globalThis.openNextConfig.imageOptimization;
+  if (typeof openNextParams?.loader === "function") {
+    return openNextParams.loader();
+  } else {
+    return Promise.resolve(async (key: string) => {
+      const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, "");
+      const response = await s3Client.send(
+        new GetObjectCommand({
+          Bucket: BUCKET_NAME,
+          Key: keyPrefix
+            ? keyPrefix + "/" + key.replace(/^\//, "")
+            : key.replace(/^\//, ""),
+        }),
+      );
+      return {
+        body: response.Body,
+        contentType: response.ContentType,
+        cacheControl: response.CacheControl,
+      };
+    });
+  }
+};
+const loader = await resolveLoader();
+
 async function downloadHandler(
   _req: IncomingMessage,
   res: ServerResponse,
@@ -186,17 +219,10 @@ async function downloadHandler(
     else {
       // Download image from S3
       // note: S3 expects keys without leading `/`
-      const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, "");
-      const response = await s3Client.send(
-        new GetObjectCommand({
-          Bucket: BUCKET_NAME,
-          Key: keyPrefix
-            ? keyPrefix + "/" + url.href.replace(/^\//, "")
-            : url.href.replace(/^\//, ""),
-        }),
-      );
 
-      if (!response.Body) {
+      const response = await loader(url.href);
+
+      if (!response.body) {
         throw new Error("Empty response body from the S3 request.");
       }
 
@@ -205,11 +231,11 @@ async function downloadHandler(
 
       // Respect the bucket file's content-type and cache-control
       // imageOptimizer will use this to set the results.maxAge
-      if (response.ContentType) {
-        res.setHeader("Content-Type", response.ContentType);
+      if (response.contentType) {
+        res.setHeader("Content-Type", response.contentType);
       }
-      if (response.CacheControl) {
-        res.setHeader("Cache-Control", response.CacheControl);
+      if (response.cacheControl) {
+        res.setHeader("Cache-Control", response.cacheControl);
       }
     }
   } catch (e: any) {
diff --git a/packages/open-next/src/adapters/revalidate.ts b/packages/open-next/src/adapters/revalidate.ts
index 5014da2b8..c992a874e 100644
--- a/packages/open-next/src/adapters/revalidate.ts
+++ b/packages/open-next/src/adapters/revalidate.ts
@@ -3,8 +3,7 @@ import type { IncomingMessage } from "node:http";
 import https from "node:https";
 import path from "node:path";
 
-import type { SQSEvent } from "aws-lambda";
-
+import { createGenericHandler } from "../core/createGenericHandler.js";
 import { debug, error } from "./logger.js";
 
 const prerenderManifest = loadPrerenderManifest();
@@ -17,9 +16,17 @@ interface PrerenderManifest {
   };
 }
 
-export const handler = async (event: SQSEvent) => {
-  for (const record of event.Records) {
-    const { host, url } = JSON.parse(record.body);
+export interface RevalidateEvent {
+  type: "revalidate";
+  records: {
+    host: string;
+    url: string;
+  }[];
+}
+
+const defaultHandler = async (event: RevalidateEvent) => {
+  for (const record of event.records) {
+    const { host, url } = record;
     debug(`Revalidating stale page`, { host, url });
 
     // Make a HEAD request to the page to revalidate it. This will trigger
@@ -49,8 +56,17 @@ export const handler = async (event: SQSEvent) => {
       req.end();
     });
   }
+  return {
+    type: "revalidate",
+  };
 };
 
+export const handler = await createGenericHandler({
+  handler: defaultHandler,
+  type: "revalidate",
+  defaultConverter: "sqs-revalidate",
+});
+
 function loadPrerenderManifest() {
   const filePath = path.join("prerender-manifest.json");
   const json = fs.readFileSync(filePath, "utf-8");
diff --git a/packages/open-next/src/adapters/warmer-function.ts b/packages/open-next/src/adapters/warmer-function.ts
index 4ee564fae..c79edd382 100644
--- a/packages/open-next/src/adapters/warmer-function.ts
+++ b/packages/open-next/src/adapters/warmer-function.ts
@@ -1,6 +1,6 @@
 import { InvokeCommand, LambdaClient } from "@aws-sdk/client-lambda";
-import type { Context } from "aws-lambda";
 
+import { createGenericHandler } from "../core/createGenericHandler.js";
 import { debug, error } from "./logger.js";
 import { generateUniqueId } from "./util.js";
 
@@ -17,10 +17,70 @@ export interface WarmerEvent {
 }
 
 export interface WarmerResponse {
+  type: "warmer";
   serverId: string;
 }
 
-export async function handler(_event: any, context: Context) {
+const resolveWarmerInvoke = async () => {
+  const openNextParams = globalThis.openNextConfig.warmer!;
+  if (typeof openNextParams?.invokeFunction === "function") {
+    return await openNextParams.invokeFunction();
+  } else {
+    return Promise.resolve(async (warmerId: string) => {
+      const ret = await Promise.all(
+        Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
+          try {
+            return lambda.send(
+              new InvokeCommand({
+                FunctionName: FUNCTION_NAME,
+                InvocationType: "RequestResponse",
+                Payload: Buffer.from(
+                  JSON.stringify({
+                    type: "warmer",
+                    warmerId,
+                    index: i,
+                    concurrency: CONCURRENCY,
+                    delay: 75,
+                  } satisfies WarmerEvent),
+                ),
+              }),
+            );
+          } catch (e) {
+            error(`failed to warm up #${i}`, e);
+            // ignore error
+          }
+        }),
+      );
+
+      // Print status
+
+      return ret
+        .map((r, i) => {
+          if (r?.StatusCode !== 200 || !r?.Payload) {
+            error(`failed to warm up #${i}:`, r?.Payload?.toString());
+            return;
+          }
+          const payload = JSON.parse(
+            Buffer.from(r.Payload).toString(),
+          ) as WarmerResponse;
+          return {
+            statusCode: r.StatusCode,
+            payload,
+            type: "warmer" as const,
+          };
+        })
+        .filter((r): r is Exclude<typeof r, undefined> => !!r);
+    });
+  }
+};
+
+export const handler = await createGenericHandler({
+  handler: defaultHandler,
+  type: "warmer",
+  defaultConverter: "dummy",
+});
+
+async function defaultHandler() {
   const warmerId = `warmer-${generateUniqueId()}`;
   debug({
     event: "warmer invoked",
@@ -29,48 +89,17 @@ export async function handler(_event: any, context: Context) {
     warmerId,
   });
 
-  // Warm
-  const ret = await Promise.all(
-    Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
-      try {
-        return lambda.send(
-          new InvokeCommand({
-            FunctionName: FUNCTION_NAME,
-            InvocationType: "RequestResponse",
-            Payload: Buffer.from(
-              JSON.stringify({
-                type: "warmer",
-                warmerId,
-                index: i,
-                concurrency: CONCURRENCY,
-                delay: 75,
-              } satisfies WarmerEvent),
-            ),
-          }),
-        );
-      } catch (e) {
-        error(`failed to warm up #${i}`, e);
-        // ignore error
-      }
-    }),
-  );
+  const invokeFn = await resolveWarmerInvoke();
+
+  const warmedServerIds = await invokeFn(warmerId);
 
-  // Print status
-  const warmedServerIds: string[] = [];
-  ret.forEach((r, i) => {
-    if (r?.StatusCode !== 200 || !r?.Payload) {
-      error(`failed to warm up #${i}:`, r?.Payload?.toString());
-      return;
-    }
-    const payload = JSON.parse(
-      Buffer.from(r.Payload).toString(),
-    ) as WarmerResponse;
-    warmedServerIds.push(payload.serverId);
-  });
   debug({
     event: "warmer result",
     sent: CONCURRENCY,
     success: warmedServerIds.length,
     uniqueServersWarmed: [...new Set(warmedServerIds)].length,
   });
+  return {
+    type: "warmer",
+  };
 }
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 6709b9b5f..08a32f76d 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -51,6 +51,7 @@ export async function build(
   // Generate deployable bundle
   printHeader("Generating bundle");
   initOutputDir();
+  createOpenNextConfigBundle(options.tempDir);
   createStaticAssets();
   if (!options.dangerous?.disableIncrementalCache) {
     createCacheAssets(monorepoRoot, options.dangerous?.disableDynamoDBCache);
@@ -64,6 +65,16 @@ export async function build(
   }
 }
 
+function createOpenNextConfigBundle(tempDir: string) {
+  esbuildSync({
+    entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
+    outfile: path.join(tempDir, "open-next.config.js"),
+    bundle: true,
+    format: "cjs",
+    target: ["node18"],
+  });
+}
+
 function normalizeOptions(opts: BuildOptions, root: string) {
   const appPath = path.join(process.cwd(), opts.appPath || ".");
   const buildOutputPath = path.join(process.cwd(), opts.buildOutputPath || ".");
@@ -212,6 +223,12 @@ function createWarmerBundle() {
   const outputPath = path.join(outputDir, "warmer-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
+  // Copy open-next.config.js into the bundle
+  fs.copyFileSync(
+    path.join(options.tempDir, "open-next.config.js"),
+    path.join(outputPath, "open-next.config.js"),
+  );
+
   // Build Lambda code
   // note: bundle in OpenNext package b/c the adatper relys on the
   //       "serverless-http" package which is not a dependency in user's
@@ -249,6 +266,12 @@ function createRevalidationBundle() {
   const outputPath = path.join(outputDir, "revalidation-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
+  //Copy open-next.config.js into the bundle
+  fs.copyFileSync(
+    path.join(options.tempDir, "open-next.config.js"),
+    path.join(outputPath, "open-next.config.js"),
+  );
+
   // Build Lambda code
   esbuildSync({
     external: ["next", "styled-jsx", "react"],
@@ -272,6 +295,12 @@ function createImageOptimizationBundle() {
   const outputPath = path.join(outputDir, "image-optimization-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
+  // Copy open-next.config.js into the bundle
+  fs.copyFileSync(
+    path.join(options.tempDir, "open-next.config.js"),
+    path.join(outputPath, "open-next.config.js"),
+  );
+
   // Build Lambda code (1st pass)
   // note: bundle in OpenNext package b/c the adapter relies on the
   //       "@aws-sdk/client-s3" package which is not a dependency in user's
@@ -540,6 +569,12 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
         target: ["node18"],
       });
 
+      //Copy open-next.config.js into the bundle
+      fs.copyFileSync(
+        path.join(options.tempDir, "open-next.config.js"),
+        path.join(providerPath, "open-next.config.js"),
+      );
+
       // TODO: check if metafiles doesn't contain duplicates
       fs.writeFileSync(
         path.join(providerPath, "dynamodb-cache.json"),
@@ -575,13 +610,10 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
 
   // Copy open-next.config.js
   // We should reuse the one we created at the beginning of the build
-  esbuildSync({
-    entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
-    outfile: path.join(outputPath, packagePath, "open-next.config.js"),
-    bundle: true,
-    format: "cjs",
-    target: ["node18"],
-  });
+  fs.copyFileSync(
+    path.join(options.tempDir, "open-next.config.js"),
+    path.join(outputPath, packagePath, "open-next.config.js"),
+  );
 
   // Bundle middleware
   createMiddleware();
diff --git a/packages/open-next/src/cache/tag/dynamoDb.ts b/packages/open-next/src/cache/tag/dynamoDb.ts
index 27f24d9f0..5cf76bb2c 100644
--- a/packages/open-next/src/cache/tag/dynamoDb.ts
+++ b/packages/open-next/src/cache/tag/dynamoDb.ts
@@ -29,11 +29,11 @@ function buildDynamoKey(key: string) {
   return path.posix.join(NEXT_BUILD_ID ?? "", key);
 }
 
-function buildDynamoObject(path: string, tags: string) {
+function buildDynamoObject(path: string, tags: string, revalidatedAt?: number) {
   return {
     path: { S: buildDynamoKey(path) },
     tag: { S: buildDynamoKey(tags) },
-    revalidatedAt: { N: `${Date.now()}` },
+    revalidatedAt: { N: `${revalidatedAt ?? Date.now()}` },
   };
 }
 
@@ -127,7 +127,11 @@ const tagCache: TagCache = {
                 [CACHE_DYNAMO_TABLE ?? ""]: Items.map((Item) => ({
                   PutRequest: {
                     Item: {
-                      ...buildDynamoObject(Item.path, Item.tag),
+                      ...buildDynamoObject(
+                        Item.path,
+                        Item.tag,
+                        Item.revalidatedAt,
+                      ),
                     },
                   },
                 })),
diff --git a/packages/open-next/src/cache/tag/types.ts b/packages/open-next/src/cache/tag/types.ts
index 564d17813..eb4643787 100644
--- a/packages/open-next/src/cache/tag/types.ts
+++ b/packages/open-next/src/cache/tag/types.ts
@@ -2,5 +2,7 @@ export type TagCache = {
   getByTag(tag: string): Promise<string[]>;
   getByPath(path: string): Promise<string[]>;
   getLastModified(path: string, lastModified?: number): Promise<number>;
-  writeTags(tags: { tag: string; path: string }[]): Promise<void>;
+  writeTags(
+    tags: { tag: string; path: string; revalidatedAt?: number }[],
+  ): Promise<void>;
 };
diff --git a/packages/open-next/src/converters/aws-apigw-v1.ts b/packages/open-next/src/converters/aws-apigw-v1.ts
index dc275ed04..7750fc457 100644
--- a/packages/open-next/src/converters/aws-apigw-v1.ts
+++ b/packages/open-next/src/converters/aws-apigw-v1.ts
@@ -56,7 +56,7 @@ async function convertFromAPIGatewayProxyEvent(
 ): Promise<InternalEvent> {
   const { path, body, httpMethod, requestContext, isBase64Encoded } = event;
   return {
-    type: "v1",
+    type: "core",
     method: httpMethod,
     rawPath: path,
     url: path + normalizeAPIGatewayProxyEventQueryParams(event),
diff --git a/packages/open-next/src/converters/aws-apigw-v2.ts b/packages/open-next/src/converters/aws-apigw-v2.ts
index 77c282238..6af649453 100644
--- a/packages/open-next/src/converters/aws-apigw-v2.ts
+++ b/packages/open-next/src/converters/aws-apigw-v2.ts
@@ -44,7 +44,7 @@ async function convertFromAPIGatewayProxyEventV2(
 ): Promise<InternalEvent> {
   const { rawPath, rawQueryString, requestContext } = event;
   return {
-    type: "v2",
+    type: "core",
     method: requestContext.http.method,
     rawPath,
     url: rawPath + (rawQueryString ? `?${rawQueryString}` : ""),
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index 2205f9cb0..cb4bd40f1 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -30,7 +30,7 @@ async function convertFromCloudFrontRequestEvent(
   const { method, uri, querystring, body, headers, clientIp } =
     event.Records[0].cf.request;
   return {
-    type: "cf",
+    type: "core",
     method,
     rawPath: uri,
     url: uri + (querystring ? `?${querystring}` : ""),
diff --git a/packages/open-next/src/converters/docker.ts b/packages/open-next/src/converters/docker.ts
index 8561aa7a9..112bef7c4 100644
--- a/packages/open-next/src/converters/docker.ts
+++ b/packages/open-next/src/converters/docker.ts
@@ -17,7 +17,7 @@ const converter: Converter = {
     const url = new URL(req.url!, `http://${req.headers.host}`);
     const query = Object.fromEntries(url.searchParams.entries());
     return {
-      type: "v2",
+      type: "core",
       method: req.method ?? "GET",
       rawPath: url.pathname,
       url: url.toString(),
diff --git a/packages/open-next/src/converters/dummy.ts b/packages/open-next/src/converters/dummy.ts
new file mode 100644
index 000000000..c1bc833b1
--- /dev/null
+++ b/packages/open-next/src/converters/dummy.ts
@@ -0,0 +1,23 @@
+import { Converter } from "types/open-next";
+
+type DummyEventOrResult = {
+  type: "dummy";
+  original: any;
+};
+
+const converter: Converter<DummyEventOrResult, DummyEventOrResult> = {
+  convertFrom(event) {
+    return Promise.resolve({
+      type: "dummy",
+      original: event,
+    });
+  },
+  convertTo(internalResult) {
+    return Promise.resolve({
+      type: "dummy",
+      original: internalResult,
+    });
+  },
+};
+
+export default converter;
diff --git a/packages/open-next/src/converters/sqs-revalidate.ts b/packages/open-next/src/converters/sqs-revalidate.ts
new file mode 100644
index 000000000..d701782ce
--- /dev/null
+++ b/packages/open-next/src/converters/sqs-revalidate.ts
@@ -0,0 +1,24 @@
+import { SQSEvent } from "aws-lambda";
+import { Converter } from "types/open-next";
+
+import { RevalidateEvent } from "../adapters/revalidate";
+
+const converter: Converter<RevalidateEvent, { type: "revalidate" }> = {
+  convertFrom(event: SQSEvent) {
+    const records = event.Records.map((record) => {
+      const { host, url } = JSON.parse(record.body);
+      return { host, url };
+    });
+    return Promise.resolve({
+      type: "revalidate",
+      records,
+    });
+  },
+  convertTo() {
+    return Promise.resolve({
+      type: "revalidate",
+    });
+  },
+};
+
+export default converter;
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
new file mode 100644
index 000000000..612e180ba
--- /dev/null
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -0,0 +1,59 @@
+import type {
+  BaseEventOrResult,
+  BuildOptions,
+  DefaultOverrideOptions,
+  InternalEvent,
+  InternalResult,
+  OpenNextHandler,
+} from "types/open-next";
+
+import { resolveConverter, resolveWrapper } from "./resolve";
+
+declare global {
+  var openNextConfig: Partial<BuildOptions>;
+}
+
+type HandlerType =
+  | "imageOptimization"
+  | "revalidate"
+  | "warmer"
+  | "middleware"
+  | "initializationFunction";
+
+type GenericHandler<
+  Type extends HandlerType,
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> = {
+  handler: OpenNextHandler<E, R>;
+  type: Type;
+  defaultConverter?: string;
+};
+
+export async function createGenericHandler<
+  Type extends HandlerType,
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+>(handler: GenericHandler<Type, E, R>) {
+  //First we load the config
+  const config: BuildOptions = await import(
+    process.cwd() + "/open-next.config.js"
+  ).then((m) => m.default);
+
+  globalThis.openNextConfig = {
+    [handler.type]: config[handler.type],
+  };
+  const override = config[handler.type]
+    ?.override as any as DefaultOverrideOptions<E, R>;
+
+  // From the config, we create the adapter
+  const adapter = await resolveConverter<E, R>(
+    override?.converter,
+    handler.defaultConverter,
+  );
+
+  // Then we create the handler
+  const wrapper = await resolveWrapper<E, R>(override?.wrapper);
+
+  return wrapper(handler.handler, adapter);
+}
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 333006240..649ec8edf 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -1,47 +1,15 @@
-import type {
-  BuildOptions,
-  Converter,
-  OverrideOptions,
-  Wrapper,
-} from "types/open-next";
+import type { BuildOptions, OverrideOptions } from "types/open-next";
 
 import type { IncrementalCache } from "../cache/incremental/types";
 import type { Queue } from "../queue/types";
 import { openNextHandler } from "./requestHandler";
+import { resolveConverter, resolveTagCache, resolveWrapper } from "./resolve";
 
 declare global {
   var queue: Queue;
   var incrementalCache: IncrementalCache;
 }
 
-async function resolveConverter(
-  converter: OverrideOptions["converter"],
-): Promise<Converter> {
-  if (typeof converter === "string") {
-    const m = await import(`../converters/${converter}.js`);
-    return m.default;
-  } else if (typeof converter === "function") {
-    return converter();
-  } else {
-    const m_1 = await import("../converters/aws-apigw-v2.js");
-    return m_1.default;
-  }
-}
-
-async function resolveWrapper(
-  wrapper: OverrideOptions["wrapper"],
-): Promise<Wrapper> {
-  if (typeof wrapper === "string") {
-    const m = await import(`../wrappers/${wrapper}.js`);
-    return m.default;
-  } else if (typeof wrapper === "function") {
-    return wrapper();
-  } else {
-    const m_1 = await import("../wrappers/aws-lambda.js");
-    return m_1.default;
-  }
-}
-
 async function resolveQueue(queue: OverrideOptions["queue"]) {
   if (typeof queue === "string") {
     const m = await import(`../queue/${queue}.js`);
@@ -68,18 +36,6 @@ async function resolveIncrementalCache(
   }
 }
 
-async function resolveTagCache(tagCache: OverrideOptions["tagCache"]) {
-  if (typeof tagCache === "string") {
-    const m = await import(`../cache/tag/${tagCache}.js`);
-    return m.default;
-  } else if (typeof tagCache === "function") {
-    return tagCache();
-  } else {
-    const m_1 = await import("../cache/tag/dynamoDb.js");
-    return m_1.default;
-  }
-}
-
 export async function createMainHandler() {
   //First we load the config
   const config: BuildOptions = await import(
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 011b0cf00..e65bc4f1c 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -13,7 +13,7 @@ import { requestHandler, setNextjsPrebundledReact } from "./util";
 export async function openNextHandler(
   internalEvent: InternalEvent,
   responseStreaming?: StreamCreator,
-): Promise<InternalResult | void> {
+): Promise<InternalResult> {
   if (internalEvent.headers["x-forwarded-host"]) {
     internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
   }
diff --git a/packages/open-next/src/core/resolve.ts b/packages/open-next/src/core/resolve.ts
new file mode 100644
index 000000000..63d32b5a3
--- /dev/null
+++ b/packages/open-next/src/core/resolve.ts
@@ -0,0 +1,61 @@
+import {
+  BaseEventOrResult,
+  Converter,
+  DefaultOverrideOptions,
+  InternalEvent,
+  InternalResult,
+  OverrideOptions,
+  Wrapper,
+} from "types/open-next.js";
+
+import { TagCache } from "../cache/tag/types.js";
+
+// TODO: We should probably use open-next plugins here to avoid bundling unnecessary code
+
+export async function resolveConverter<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+>(
+  converter: DefaultOverrideOptions<E, R>["converter"],
+  defaultConverter: string = "aws-apigw-v2",
+): Promise<Converter<E, R>> {
+  if (typeof converter === "string") {
+    const m = await import(`../converters/${converter}.js`);
+    return m.default;
+  } else if (typeof converter === "function") {
+    return converter();
+  } else {
+    const m_1 = await import(`../converters/${defaultConverter}.js`);
+    return m_1.default;
+  }
+}
+
+export async function resolveWrapper<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+>(wrapper: DefaultOverrideOptions<E, R>["wrapper"]): Promise<Wrapper<E, R>> {
+  if (typeof wrapper === "string") {
+    const m = await import(`../wrappers/${wrapper}.js`);
+    return m.default;
+  } else if (typeof wrapper === "function") {
+    return wrapper();
+  } else {
+    const m_1 = await import("../wrappers/aws-lambda.js");
+    // @ts-expect-error
+    return m_1.default;
+  }
+}
+
+export async function resolveTagCache(
+  tagCache: OverrideOptions["tagCache"],
+): Promise<TagCache> {
+  if (typeof tagCache === "string") {
+    const m = await import(`../cache/tag/${tagCache}.js`);
+    return m.default;
+  } else if (typeof tagCache === "function") {
+    return tagCache();
+  } else {
+    const m_1 = await import("../cache/tag/dynamoDb.js");
+    return m_1.default;
+  }
+}
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index b762b430a..6d36dbaaa 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -1,9 +1,17 @@
+import type { Readable } from "node:stream";
+
 import { StreamCreator } from "http/index.js";
 
+import { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
+import { IncrementalCache } from "../cache/incremental/types";
+import { TagCache } from "../cache/tag/types";
 import { Queue } from "../queue/types";
 
+export type BaseEventOrResult<T extends string = string> = {
+  type: T;
+};
+
 export type InternalEvent = {
-  readonly type: "v1" | "v2" | "cf";
   readonly method: string;
   readonly rawPath: string;
   readonly url: string;
@@ -12,15 +20,14 @@ export type InternalEvent = {
   readonly query: Record<string, string | string[]>;
   readonly cookies: Record<string, string>;
   readonly remoteAddress: string;
-};
+} & BaseEventOrResult<"core">;
 
 export type InternalResult = {
-  readonly type: "v1" | "v2" | "cf";
   statusCode: number;
   headers: Record<string, string | string[]>;
   body: string;
   isBase64Encoded: boolean;
-};
+} & BaseEventOrResult<"core">;
 
 export interface DangerousOptions {
   /**
@@ -38,35 +45,47 @@ export interface DangerousOptions {
 
 export type LazyLoadedOverride<T> = () => Promise<T>;
 
-export type OpenNextHandler = (
-  event: InternalEvent,
-  responseStream?: StreamCreator,
-) => Promise<InternalResult | void>;
+export type OpenNextHandler<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> = (event: E, responseStream?: StreamCreator) => Promise<R>;
 
-export type Converter = {
-  convertFrom: (event: any) => Promise<InternalEvent>;
-  convertTo: (result: any) => any;
+export type Converter<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> = {
+  convertFrom: (event: any) => Promise<E>;
+  convertTo: (result: R) => any;
 };
 
-export type Wrapper = (
-  handler: OpenNextHandler,
-  converter: Converter,
+export type Wrapper<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> = (
+  handler: OpenNextHandler<E, R>,
+  converter: Converter<E, R>,
 ) => Promise<(...args: any[]) => any>;
 
-//TODO: properly type this
-export type IncrementalCache = {
-  get(key: string): Promise<any>;
-  set(key: string, value: any): Promise<void>;
-};
+type Warmer = (warmerId: string) => Promise<
+  {
+    statusCode: number;
+    payload: {
+      serverId: string;
+    };
+    type: "warmer";
+  }[]
+>;
 
-export type TagCache = {
-  getByTag(tag: string): Promise<string[]>;
-  getByPath(path: string): Promise<string[]>;
-  getLastModified(path: string, lastModified?: number): Promise<number>;
-  writeTags(tags: { tag: string; path: string }): Promise<void>;
-};
+type ImageLoader = (url: string) => Promise<{
+  body?: Readable;
+  contentType?: string;
+  cacheControl?: string;
+}>;
 
-export interface OverrideOptions {
+export interface DefaultOverrideOptions<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> {
   /**
    * This is the main entrypoint of your app.
    * @default "aws-lambda"
@@ -75,7 +94,7 @@ export interface OverrideOptions {
     | "aws-lambda"
     | "aws-lambda-streaming"
     | "docker"
-    | LazyLoadedOverride<Wrapper>;
+    | LazyLoadedOverride<Wrapper<E, R>>;
 
   /**
    * This code convert the event to InternalEvent and InternalResult to the expected output.
@@ -86,8 +105,10 @@ export interface OverrideOptions {
     | "aws-apigw-v1"
     | "aws-cloudfront"
     | "docker"
-    | LazyLoadedOverride<Converter>;
+    | LazyLoadedOverride<Converter<E, R>>;
+}
 
+export interface OverrideOptions extends DefaultOverrideOptions {
   /**
    * Add possibility to override the default s3 cache. Used for fetch cache and html/rsc/json cache.
    * @default "s3"
@@ -107,7 +128,27 @@ export interface OverrideOptions {
   queue?: "sqs" | LazyLoadedOverride<Queue>;
 }
 
-interface FunctionOptions {
+export interface DefaultFunctionOptions<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> {
+  /**
+   * Minify the server bundle.
+   * @default false
+   */
+  minify?: boolean;
+  /**
+   * Print debug information.
+   * @default false
+   */
+  debug?: boolean;
+  /**
+   * Enable overriding the default lambda.
+   */
+  override?: DefaultOverrideOptions<E, R>;
+}
+
+interface FunctionOptions extends DefaultFunctionOptions {
   /**
    * TODO: implement edge runtime
    * @default "node"
@@ -119,16 +160,6 @@ interface FunctionOptions {
    * @default []
    */
   routes?: string[];
-  /**
-   * Minify the server bundle.
-   * @default false
-   */
-  minify?: boolean;
-  /**
-   * Print debug information.
-   * @default false
-   */
-  debug?: boolean;
   /**
    * Enable streaming mode.
    * @default false
@@ -145,6 +176,57 @@ export interface BuildOptions {
     default: Omit<FunctionOptions, "routes">;
     [key: string]: FunctionOptions;
   };
+
+  /**
+   * Override the default middleware
+   * If you set this options, the middleware need to be deployed separately.
+   * It supports both edge and node runtime.
+   * TODO: actually implement it
+   * @default undefined
+   */
+  middleware?: DefaultFunctionOptions & {
+    //We force the middleware to be a function
+    external: true;
+  };
+
+  /**
+   * Override the default warmer
+   * By default, works for lambda only.
+   * If you override this, you'll need to handle the warmer event in the wrapper
+   * @default undefined
+   */
+  warmer?: DefaultFunctionOptions<WarmerEvent, WarmerResponse> & {
+    invokeFunction: "aws-lambda" | LazyLoadedOverride<Warmer>;
+  };
+
+  /**
+   * Override the default revalidate function
+   * By default, works for lambda and on SQS event.
+   * Supports only node runtime
+   */
+  revalidate?: DefaultFunctionOptions<
+    { host: string; url: string; type: "revalidate" },
+    { type: "revalidate" }
+  >;
+
+  /**
+   * Override the default revalidate function
+   * By default, works on lambda and for S3 key.
+   * Supports only node runtime
+   */
+  imageOptimization?: DefaultFunctionOptions & {
+    loader?: "s3" | LazyLoadedOverride<ImageLoader>;
+  };
+
+  /**
+   * Override the default initialization function
+   * By default, works for lambda and on SQS event.
+   * Supports only node runtime
+   */
+  initializationFunction?: DefaultFunctionOptions & {
+    tagCache?: "dynamodb" | LazyLoadedOverride<TagCache>;
+  };
+
   /**
    * The command to build the Next.js app.
    * @default `npm run build`, `yarn build`, or `pnpm build` based on the lock file found in the app's directory or any of its parent directories.

From 8dabff08b0f44a210475f3ed8921014e1fc9b7ff Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 23 Nov 2023 16:45:38 +0100
Subject: [PATCH 018/102] externalMiddleware

---
 .../open-next/src/adapters/config/index.ts    |  3 +
 .../open-next/src/adapters/config/plugin.ts   | 97 +++++++++++++++++++
 .../open-next/src/adapters/config/util.ts     |  7 ++
 packages/open-next/src/adapters/middleware.ts | 24 +++++
 packages/open-next/src/build.ts               | 84 ++++++++++++++--
 packages/open-next/src/converters/edge.ts     | 73 ++++++++++++++
 .../src/core/createGenericHandler.ts          |  7 +-
 packages/open-next/src/core/resolve.ts        | 23 +++--
 .../open-next/src/core/routing/middleware.ts  | 12 +--
 packages/open-next/src/core/routing/util.ts   | 23 ++---
 packages/open-next/src/http/index.ts          |  2 +
 packages/open-next/src/http/request.ts        |  2 +-
 packages/open-next/src/types/next-types.ts    |  2 +-
 packages/open-next/src/types/open-next.ts     |  4 +-
 packages/open-next/src/types/plugin.ts        | 33 -------
 packages/open-next/src/wrappers/cloudflare.ts | 20 ++++
 packages/open-next/src/wrappers/docker.ts     |  6 +-
 17 files changed, 336 insertions(+), 86 deletions(-)
 create mode 100644 packages/open-next/src/adapters/config/plugin.ts
 create mode 100644 packages/open-next/src/adapters/middleware.ts
 create mode 100644 packages/open-next/src/converters/edge.ts
 delete mode 100644 packages/open-next/src/types/plugin.ts
 create mode 100644 packages/open-next/src/wrappers/cloudflare.ts

diff --git a/packages/open-next/src/adapters/config/index.ts b/packages/open-next/src/adapters/config/index.ts
index c686074fa..8592a7e2e 100644
--- a/packages/open-next/src/adapters/config/index.ts
+++ b/packages/open-next/src/adapters/config/index.ts
@@ -7,6 +7,7 @@ import {
   loadConfig,
   loadConfigHeaders,
   loadHtmlPages,
+  loadMiddlewareManifest,
   loadPrerenderManifest,
   loadPublicAssets,
   loadRoutesManifest,
@@ -17,6 +18,7 @@ export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");
 
 debug({ NEXT_DIR, OPEN_NEXT_DIR });
 
+//TODO: inject these values at build time
 export const NextConfig = loadConfig(NEXT_DIR);
 export const BuildId = loadBuildId(NEXT_DIR);
 export const HtmlPages = loadHtmlPages(NEXT_DIR);
@@ -25,3 +27,4 @@ export const RoutesManifest = loadRoutesManifest(NEXT_DIR);
 export const ConfigHeaders = loadConfigHeaders(NEXT_DIR);
 export const PrerenderManifest = loadPrerenderManifest(NEXT_DIR);
 export const AppPathsManifestKeys = loadAppPathsManifestKeys(NEXT_DIR);
+export const MiddlewareManifest = loadMiddlewareManifest(NEXT_DIR);
diff --git a/packages/open-next/src/adapters/config/plugin.ts b/packages/open-next/src/adapters/config/plugin.ts
new file mode 100644
index 000000000..901671f00
--- /dev/null
+++ b/packages/open-next/src/adapters/config/plugin.ts
@@ -0,0 +1,97 @@
+import { Plugin } from "esbuild";
+import { readFileSync } from "fs";
+import path from "path";
+
+import {
+  loadAppPathsManifestKeys,
+  loadBuildId,
+  loadConfig,
+  loadConfigHeaders,
+  loadHtmlPages,
+  loadMiddlewareManifest,
+  loadPrerenderManifest,
+  loadRoutesManifest,
+} from "./util.js";
+
+export interface IPluginSettings {
+  nextDir: string;
+  outputPath: string;
+  overrides?: {
+    wrapper?: string;
+    converter?: string;
+  };
+}
+
+/**
+ * @returns
+ */
+export default function openNextConfigPlugin({
+  nextDir,
+  outputPath,
+  overrides,
+}: IPluginSettings): Plugin {
+  return {
+    name: "opennext-config",
+    setup(build) {
+      build.onResolve({ filter: /\.\/middleware.mjs/g }, () => {
+        console.log("middleware.mjs");
+        return {
+          path: path.join(outputPath, "edgeFunctionHandler.js"),
+        };
+      });
+
+      build.onLoad({ filter: /core\/resolve.ts/g }, async (args) => {
+        let contents = readFileSync(args.path, "utf-8");
+        if (overrides?.wrapper) {
+          contents = contents.replace(
+            "../wrappers/aws-lambda.js",
+            `../wrappers/${overrides.wrapper}.js`,
+          );
+        }
+        return {
+          contents,
+        };
+      });
+
+      build.onLoad({ filter: /adapters\/config\/index.ts/g }, async () => {
+        console.log("opennext-config-plugin");
+        const NextConfig = loadConfig(nextDir);
+        const BuildId = loadBuildId(nextDir);
+        const HtmlPages = loadHtmlPages(nextDir);
+        const RoutesManifest = loadRoutesManifest(nextDir);
+        const ConfigHeaders = loadConfigHeaders(nextDir);
+        const PrerenderManifest = loadPrerenderManifest(nextDir);
+        const AppPathsManifestKeys = loadAppPathsManifestKeys(nextDir);
+        const MiddlewareManifest = loadMiddlewareManifest(nextDir);
+
+        const contents = `
+import path from "path";
+
+import { debug } from "../logger";
+
+if(!globalThis.__dirname) {
+  globalThis.__dirname = ""
+}
+
+export const NEXT_DIR = path.join(__dirname, ".next");
+export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");
+
+debug({ NEXT_DIR, OPEN_NEXT_DIR });
+
+export const NextConfig = ${JSON.stringify(NextConfig)};
+export const BuildId = ${JSON.stringify(BuildId)};
+export const HtmlPages = ${JSON.stringify(HtmlPages)};
+export const RoutesManifest = ${JSON.stringify(RoutesManifest)};
+export const ConfigHeaders = ${JSON.stringify(ConfigHeaders)};
+export const PrerenderManifest = ${JSON.stringify(PrerenderManifest)};
+export const AppPathsManifestKeys = ${JSON.stringify(AppPathsManifestKeys)};
+export const MiddlewareManifest = ${JSON.stringify(MiddlewareManifest)};
+
+        `;
+        return {
+          contents,
+        };
+      });
+    },
+  };
+}
diff --git a/packages/open-next/src/adapters/config/util.ts b/packages/open-next/src/adapters/config/util.ts
index 309328119..aa11ee3b5 100644
--- a/packages/open-next/src/adapters/config/util.ts
+++ b/packages/open-next/src/adapters/config/util.ts
@@ -1,6 +1,7 @@
 import fs from "fs";
 import path from "path";
 import {
+  MiddlewareManifest,
   NextConfig,
   PrerenderManifest,
   RoutesManifest,
@@ -100,3 +101,9 @@ export function loadAppPathsManifestKeys(nextDir: string) {
     return cleanedKey === "" ? "/" : cleanedKey;
   });
 }
+
+export function loadMiddlewareManifest(nextDir: string) {
+  const filePath = path.join(nextDir, "server", "middleware-manifest.json");
+  const json = fs.readFileSync(filePath, "utf-8");
+  return JSON.parse(json) as MiddlewareManifest;
+}
diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
new file mode 100644
index 000000000..dd1f44a78
--- /dev/null
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -0,0 +1,24 @@
+import { InternalEvent } from "types/open-next";
+
+import { createGenericHandler } from "../core/createGenericHandler";
+import routingHandler from "../core/routingHandler";
+
+const defaultHandler = async (internalEvent: InternalEvent) => {
+  const result = await routingHandler(internalEvent);
+  if ("internalEvent" in result) {
+    return {
+      type: "middleware",
+      internalEvent: result.internalEvent,
+      headers: result.headers,
+      isExternalRewrite: result.isExternalRewrite,
+    };
+  } else {
+    return result;
+  }
+};
+
+export const handler = await createGenericHandler({
+  handler: defaultHandler,
+  type: "middleware",
+  defaultConverter: "edge",
+});
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 08a32f76d..25120c128 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -10,6 +10,7 @@ import {
   buildSync,
 } from "esbuild";
 
+import openNextConfigPlugin from "./adapters/config/plugin.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import openNextPlugin from "./plugin.js";
@@ -109,6 +110,7 @@ function normalizeOptions(opts: BuildOptions, root: string) {
     buildCommand: opts.buildCommand,
     dangerous: opts.dangerous,
     streaming: opts.functions.default.streaming ?? false,
+    externalMiddleware: opts.middleware?.external ?? false,
   };
 }
 
@@ -639,7 +641,9 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
     compareSemver(options.nextVersion, "13.5.1") >= 0 ||
     compareSemver(options.nextVersion, "13.4.1") <= 0;
 
-  const disableRouting = compareSemver(options.nextVersion, "13.4.13") <= 0;
+  const disableRouting =
+    compareSemver(options.nextVersion, "13.4.13") <= 0 ||
+    options.externalMiddleware;
   const plugins = [
     openNextPlugin({
       name: "requestHandlerOverride",
@@ -695,10 +699,10 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
   addCacheHandler(outputPath, options.dangerous);
 }
 
-function createMiddleware() {
+async function createMiddleware() {
   console.info(`Bundling middleware function...`);
 
-  const { appBuildOutputPath, outputDir } = options;
+  const { appBuildOutputPath, outputDir, externalMiddleware } = options;
 
   // Get middleware manifest
   const middlewareManifest = JSON.parse(
@@ -710,13 +714,66 @@ function createMiddleware() {
 
   const entry = middlewareManifest.middleware["/"];
 
-  // Build edge function
-  buildEdgeFunction(
-    entry,
-    path.join(__dirname, "core", "edgeFunctionHandler.js"),
-    path.join(outputDir, "server-function", "middleware.mjs"),
-    appBuildOutputPath,
-  );
+  // Create output folder
+  let outputPath = path.join(outputDir, "server-function");
+  if (externalMiddleware) {
+    outputPath = path.join(outputDir, "middleware");
+    fs.mkdirSync(outputPath, { recursive: true });
+
+    // Copy open-next.config.js
+    fs.copyFileSync(
+      path.join(options.tempDir, "open-next.config.js"),
+      path.join(outputPath, "open-next.config.js"),
+    );
+
+    // Bundle middleware
+    await esbuildAsync({
+      entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
+      inject: entry.files.map((file: string) =>
+        path.join(appBuildOutputPath, ".next", file),
+      ),
+      bundle: true,
+      outfile: path.join(outputPath, "handler.mjs"),
+      external: ["node:*", "fs", "path", "next", "@aws-sdk/*", "stream"],
+      target: "es2022",
+      platform: "neutral",
+      plugins: [
+        openNextConfigPlugin({
+          nextDir: path.join(appBuildOutputPath, ".next"),
+          outputPath: path.join(__dirname, "core"),
+          overrides: {
+            wrapper: "cloudflare",
+          },
+        }),
+      ],
+      treeShaking: true,
+      alias: {
+        path: "node:path",
+        stream: "node:stream",
+      },
+      banner: {
+        js: `
+globalThis._ENTRIES = {};
+globalThis.self = globalThis;
+globalThis.process = {env: {}}
+
+import {Buffer} from "node:buffer";
+globalThis.Buffer = Buffer;
+
+import {AsyncLocalStorage} from "node:async_hooks";
+globalThis.AsyncLocalStorage = AsyncLocalStorage;
+  
+  `,
+      },
+    });
+  } else {
+    buildEdgeFunction(
+      entry,
+      path.join(__dirname, "core", "edgeFunctionHandler.js"),
+      path.join(outputPath, "middleware.mjs"),
+      appBuildOutputPath,
+    );
+  }
 }
 
 function buildEdgeFunction(
@@ -882,6 +939,7 @@ function addCacheHandler(outputPath: string, options?: DangerousOptions) {
 
 function esbuildSync(esbuildOptions: ESBuildOptions) {
   const { openNextVersion, debug } = options;
+  console.log(["./open-next.config.js", ...(esbuildOptions.external ?? [])]);
   const result = buildSync({
     target: "esnext",
     format: "esm",
@@ -890,6 +948,7 @@ function esbuildSync(esbuildOptions: ESBuildOptions) {
     minify: debug ? false : true,
     sourcemap: debug ? "inline" : false,
     ...esbuildOptions,
+    external: ["./open-next.config.js", ...(esbuildOptions.external ?? [])],
     banner: {
       ...esbuildOptions.banner,
       js: [
@@ -920,6 +979,11 @@ async function esbuildAsync(esbuildOptions: ESBuildOptions) {
     minify: debug ? false : true,
     sourcemap: debug ? "inline" : false,
     ...esbuildOptions,
+    external: [
+      ...(esbuildOptions.external ?? []),
+      "next",
+      "./open-next.config.js",
+    ],
     banner: {
       ...esbuildOptions.banner,
       js: [
diff --git a/packages/open-next/src/converters/edge.ts b/packages/open-next/src/converters/edge.ts
new file mode 100644
index 000000000..bad375747
--- /dev/null
+++ b/packages/open-next/src/converters/edge.ts
@@ -0,0 +1,73 @@
+import { parseCookies } from "http/util";
+import { Converter, InternalEvent, InternalResult } from "types/open-next";
+
+import { MiddlewareOutputEvent } from "../core/routingHandler";
+
+const converter: Converter<
+  InternalEvent,
+  InternalResult | ({ type: "middleware" } & MiddlewareOutputEvent)
+> = {
+  convertFrom: async (event: Request) => {
+    const searchParams = new URL(event.url).searchParams;
+    const query: Record<string, string | string[]> = {};
+    for (const [key, value] of searchParams.entries()) {
+      if (query[key]) {
+        if (Array.isArray(query[key])) {
+          (query[key] as string[]).push(value);
+        } else {
+          query[key] = [query[key] as string, value];
+        }
+      } else {
+        query[key] = value;
+      }
+    }
+    //Transform body into Buffer
+    const body = await event.arrayBuffer();
+    const headers: Record<string, string> = {};
+    event.headers.forEach((value, key) => {
+      headers[key] = value;
+    });
+    const rawPath = new URL(event.url).pathname;
+
+    return {
+      type: "core",
+      method: event.method,
+      rawPath,
+      url: event.url,
+      body: event.method !== "GET" ? Buffer.from(body) : undefined,
+      headers: headers,
+      remoteAddress: (event.headers.get("x-forwarded-for") as string) ?? "::1",
+      query,
+      cookies: Object.fromEntries(
+        parseCookies(event.headers.get("cookie") ?? "")?.map((cookie) => {
+          const [key, value] = cookie.split("=");
+          return [key, value];
+        }) ?? [],
+      ),
+    };
+  },
+  convertTo: async (result) => {
+    if ("internalEvent" in result) {
+      const url = result.isExternalRewrite
+        ? result.internalEvent.url
+        : `https://${result.internalEvent.headers.host}${result.internalEvent.url}`;
+      const req = new Request(url, {
+        body: result.internalEvent.body,
+        method: result.internalEvent.method,
+        headers: result.internalEvent.headers,
+      });
+      return fetch(req);
+    } else {
+      const headers = new Headers();
+      for (const [key, value] of Object.entries(result.headers)) {
+        headers.set(key, Array.isArray(value) ? value.join(",") : value);
+      }
+      return new Response(result.body, {
+        status: result.statusCode,
+        headers: headers,
+      });
+    }
+  },
+};
+
+export default converter;
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
index 612e180ba..21c10361a 100644
--- a/packages/open-next/src/core/createGenericHandler.ts
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -36,9 +36,10 @@ export async function createGenericHandler<
   R extends BaseEventOrResult = InternalResult,
 >(handler: GenericHandler<Type, E, R>) {
   //First we load the config
-  const config: BuildOptions = await import(
-    process.cwd() + "/open-next.config.js"
-  ).then((m) => m.default);
+  //@ts-expect-error
+  const config: BuildOptions = await import("./open-next.config.js").then(
+    (m) => m.default,
+  );
 
   globalThis.openNextConfig = {
     [handler.type]: config[handler.type],
diff --git a/packages/open-next/src/core/resolve.ts b/packages/open-next/src/core/resolve.ts
index 63d32b5a3..d79d99b76 100644
--- a/packages/open-next/src/core/resolve.ts
+++ b/packages/open-next/src/core/resolve.ts
@@ -19,10 +19,7 @@ export async function resolveConverter<
   converter: DefaultOverrideOptions<E, R>["converter"],
   defaultConverter: string = "aws-apigw-v2",
 ): Promise<Converter<E, R>> {
-  if (typeof converter === "string") {
-    const m = await import(`../converters/${converter}.js`);
-    return m.default;
-  } else if (typeof converter === "function") {
+  if (typeof converter === "function") {
     return converter();
   } else {
     const m_1 = await import(`../converters/${defaultConverter}.js`);
@@ -34,27 +31,29 @@ export async function resolveWrapper<
   E extends BaseEventOrResult = InternalEvent,
   R extends BaseEventOrResult = InternalResult,
 >(wrapper: DefaultOverrideOptions<E, R>["wrapper"]): Promise<Wrapper<E, R>> {
-  if (typeof wrapper === "string") {
-    const m = await import(`../wrappers/${wrapper}.js`);
-    return m.default;
-  } else if (typeof wrapper === "function") {
+  if (typeof wrapper === "function") {
     return wrapper();
   } else {
+    // This will be replaced by the bundler
     const m_1 = await import("../wrappers/aws-lambda.js");
     // @ts-expect-error
     return m_1.default;
   }
 }
 
+/**
+ *
+ * @param tagCache
+ * @returns
+ * @__PURE__
+ */
 export async function resolveTagCache(
   tagCache: OverrideOptions["tagCache"],
 ): Promise<TagCache> {
-  if (typeof tagCache === "string") {
-    const m = await import(`../cache/tag/${tagCache}.js`);
-    return m.default;
-  } else if (typeof tagCache === "function") {
+  if (typeof tagCache === "function") {
     return tagCache();
   } else {
+    // This will be replaced by the bundler
     const m_1 = await import("../cache/tag/dynamoDb.js");
     return m_1.default;
   }
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 2eadda727..0cf122b49 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -1,6 +1,4 @@
-import path from "node:path";
-
-import { NEXT_DIR, NextConfig } from "config/index.js";
+import { MiddlewareManifest, NextConfig } from "config/index.js";
 import { InternalEvent, InternalResult } from "types/open-next.js";
 
 //NOTE: we should try to avoid importing stuff from next as much as possible
@@ -16,10 +14,9 @@ import {
   convertToQueryString,
   getMiddlewareMatch,
   isExternal,
-  loadMiddlewareManifest,
 } from "./util.js";
 
-const middlewareManifest = loadMiddlewareManifest(NEXT_DIR);
+const middlewareManifest = MiddlewareManifest;
 
 const middleMatch = getMiddlewareMatch(middlewareManifest);
 
@@ -53,9 +50,6 @@ export async function handleMiddleware(
   // but that was discarded for simplicity. The MiddlewareInfo type still has the original
   // structure, but as of now, the only useful property on it is the "/" key (ie root).
   const middlewareInfo = middlewareManifest.middleware["/"];
-  middlewareInfo.paths = middlewareInfo.files.map((file) =>
-    path.join(NEXT_DIR, file),
-  );
 
   const host = internalEvent.headers.host
     ? `https://${internalEvent.headers.host}`
@@ -112,7 +106,7 @@ export async function handleMiddleware(
         trailingSlash: NextConfig.trailingSlash,
       },
       url,
-      body: convertBodyToReadableStream(internalEvent.body),
+      body: convertBodyToReadableStream(internalEvent.body ?? ""),
     },
   );
   const statusCode = result.status;
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index 68c63c21e..21ecf3912 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -1,14 +1,9 @@
 import crypto from "node:crypto";
-import fs from "node:fs";
 import { OutgoingHttpHeaders, ServerResponse } from "node:http";
-import path from "node:path";
 
 import { BuildId, HtmlPages } from "config/index.js";
-import {
-  IncomingMessage,
-  OpenNextNodeResponse,
-  StreamCreator,
-} from "http/index.js";
+import type { IncomingMessage, StreamCreator } from "http/index.js";
+import { OpenNextNodeResponse } from "http/openNextResponse.js";
 import { parseHeaders } from "http/util.js";
 import type { MiddlewareManifest } from "types/next-types";
 import { InternalEvent } from "types/open-next.js";
@@ -105,12 +100,6 @@ export function getMiddlewareMatch(middlewareManifest: MiddlewareManifest) {
   return rootMiddleware.matchers.map(({ regexp }) => new RegExp(regexp));
 }
 
-export function loadMiddlewareManifest(nextDir: string) {
-  const filePath = path.join(nextDir, "server", "middleware-manifest.json");
-  const json = fs.readFileSync(filePath, "utf-8");
-  return JSON.parse(json) as MiddlewareManifest;
-}
-
 export function escapeRegex(str: string) {
   let path = str.replace(/\(\.\)/g, "_µ1_");
 
@@ -361,6 +350,14 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
     "s-maxage=2, stale-while-revalidate=2592000";
 }
 
+/**
+ *
+ * @param internalEvent
+ * @param headers
+ * @param responseStream
+ * @returns
+ * @__PURE__
+ */
 export function createServerResponse(
   internalEvent: InternalEvent,
   headers: Record<string, string | string[] | undefined>,
diff --git a/packages/open-next/src/http/index.ts b/packages/open-next/src/http/index.ts
index e7e2b8a46..49efb2fe4 100644
--- a/packages/open-next/src/http/index.ts
+++ b/packages/open-next/src/http/index.ts
@@ -1,2 +1,4 @@
+// @__PURE__
 export * from "./openNextResponse.js";
+// @__PURE__
 export * from "./request.js";
diff --git a/packages/open-next/src/http/request.ts b/packages/open-next/src/http/request.ts
index 180132562..d33b13955 100644
--- a/packages/open-next/src/http/request.ts
+++ b/packages/open-next/src/http/request.ts
@@ -16,7 +16,7 @@ export class IncomingMessage extends http.IncomingMessage {
     method: string;
     url: string;
     headers: Record<string, string>;
-    body: Buffer;
+    body?: Buffer;
     remoteAddress: string;
   }) {
     super({
diff --git a/packages/open-next/src/types/next-types.ts b/packages/open-next/src/types/next-types.ts
index 9c6754f39..0d31c2d3a 100644
--- a/packages/open-next/src/types/next-types.ts
+++ b/packages/open-next/src/types/next-types.ts
@@ -1,6 +1,6 @@
 // NOTE: add more next config typings as they become relevant
 
-import { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
+import type { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
 
 import { InternalEvent } from "./open-next";
 
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 6d36dbaaa..ea4eaf85b 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -15,7 +15,7 @@ export type InternalEvent = {
   readonly method: string;
   readonly rawPath: string;
   readonly url: string;
-  readonly body: Buffer;
+  readonly body?: Buffer;
   readonly headers: Record<string, string>;
   readonly query: Record<string, string | string[]>;
   readonly cookies: Record<string, string>;
@@ -94,6 +94,7 @@ export interface DefaultOverrideOptions<
     | "aws-lambda"
     | "aws-lambda-streaming"
     | "docker"
+    | "cloudflare"
     | LazyLoadedOverride<Wrapper<E, R>>;
 
   /**
@@ -104,6 +105,7 @@ export interface DefaultOverrideOptions<
     | "aws-apigw-v2"
     | "aws-apigw-v1"
     | "aws-cloudfront"
+    | "edge"
     | "docker"
     | LazyLoadedOverride<Converter<E, R>>;
 }
diff --git a/packages/open-next/src/types/plugin.ts b/packages/open-next/src/types/plugin.ts
deleted file mode 100644
index 22fd00e82..000000000
--- a/packages/open-next/src/types/plugin.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { IncomingMessage, OpenNextNodeResponse } from "http/index.js";
-
-import { InternalEvent, InternalResult } from "./open-next";
-
-export type ProcessInternalEventResult<Response extends OpenNextNodeResponse> =
-  | {
-      internalEvent: InternalEvent;
-      req: IncomingMessage;
-      res: Response;
-      isExternalRewrite: boolean;
-    }
-  | InternalResult;
-
-export type ProcessInternalEvent<
-  Response extends OpenNextNodeResponse = OpenNextNodeResponse,
-> = (
-  internalEvent: InternalEvent,
-  createResponse: CreateResponse<Response>,
-) => Promise<ProcessInternalEventResult<Response>>;
-
-export interface PostProcessOptions<
-  Response extends OpenNextNodeResponse = OpenNextNodeResponse,
-> {
-  internalEvent: InternalEvent;
-  req: IncomingMessage;
-  res: Response;
-  isExternalRewrite?: boolean;
-}
-
-export type CreateResponse<Response extends OpenNextNodeResponse> = (
-  method: string,
-  headers: Record<string, string | string[] | undefined>,
-) => Response;
diff --git a/packages/open-next/src/wrappers/cloudflare.ts b/packages/open-next/src/wrappers/cloudflare.ts
new file mode 100644
index 000000000..7d5db285b
--- /dev/null
+++ b/packages/open-next/src/wrappers/cloudflare.ts
@@ -0,0 +1,20 @@
+import type { InternalEvent, InternalResult, Wrapper } from "types/open-next";
+
+import { MiddlewareOutputEvent } from "../core/routingHandler";
+
+const handler: Wrapper<
+  InternalEvent,
+  InternalResult | ({ type: "middleware" } & MiddlewareOutputEvent)
+> =
+  async (handler, converter) =>
+  async (event: Request): Promise<Response> => {
+    const internalEvent = await converter.convertFrom(event);
+
+    const response = await handler(internalEvent);
+
+    const result: Response = converter.convertTo(response);
+
+    return result;
+  };
+
+export default handler;
diff --git a/packages/open-next/src/wrappers/docker.ts b/packages/open-next/src/wrappers/docker.ts
index 1908d2f2a..032de2443 100644
--- a/packages/open-next/src/wrappers/docker.ts
+++ b/packages/open-next/src/wrappers/docker.ts
@@ -1,9 +1,9 @@
-import debug from "debug";
-import { createServer } from "http";
+import { createServer } from "node:http";
+
 import { StreamCreator } from "http/index.js";
 import type { Wrapper } from "types/open-next";
 
-import { error } from "../adapters/logger";
+import { debug, error } from "../adapters/logger";
 
 const wrapper: Wrapper = async (handler, converter) => {
   const server = createServer(async (req, res) => {

From 2630bb77b94db9361f757fbcdc66d0c94ecab13e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 24 Nov 2023 12:34:30 +0100
Subject: [PATCH 019/102] improve plugins

---
 .../open-next/src/adapters/config/plugin.ts   |  97 ----------------
 packages/open-next/src/adapters/middleware.ts |   7 +-
 packages/open-next/src/adapters/revalidate.ts |   1 -
 .../open-next/src/adapters/warmer-function.ts |   1 -
 packages/open-next/src/build.ts               |  92 +++++++++------
 .../src/converters/{docker.ts => node.ts}     |   0
 .../src/core/createGenericHandler.ts          |   6 +-
 packages/open-next/src/core/resolve.ts        |   6 +-
 packages/open-next/src/plugins/edge.ts        | 108 ++++++++++++++++++
 .../src/{plugin.ts => plugins/replacement.ts} |   4 +-
 packages/open-next/src/plugins/resolve.ts     |  52 +++++++++
 packages/open-next/src/types/open-next.ts     |  36 +++---
 12 files changed, 252 insertions(+), 158 deletions(-)
 delete mode 100644 packages/open-next/src/adapters/config/plugin.ts
 rename packages/open-next/src/converters/{docker.ts => node.ts} (100%)
 create mode 100644 packages/open-next/src/plugins/edge.ts
 rename packages/open-next/src/{plugin.ts => plugins/replacement.ts} (97%)
 create mode 100644 packages/open-next/src/plugins/resolve.ts

diff --git a/packages/open-next/src/adapters/config/plugin.ts b/packages/open-next/src/adapters/config/plugin.ts
deleted file mode 100644
index 901671f00..000000000
--- a/packages/open-next/src/adapters/config/plugin.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { Plugin } from "esbuild";
-import { readFileSync } from "fs";
-import path from "path";
-
-import {
-  loadAppPathsManifestKeys,
-  loadBuildId,
-  loadConfig,
-  loadConfigHeaders,
-  loadHtmlPages,
-  loadMiddlewareManifest,
-  loadPrerenderManifest,
-  loadRoutesManifest,
-} from "./util.js";
-
-export interface IPluginSettings {
-  nextDir: string;
-  outputPath: string;
-  overrides?: {
-    wrapper?: string;
-    converter?: string;
-  };
-}
-
-/**
- * @returns
- */
-export default function openNextConfigPlugin({
-  nextDir,
-  outputPath,
-  overrides,
-}: IPluginSettings): Plugin {
-  return {
-    name: "opennext-config",
-    setup(build) {
-      build.onResolve({ filter: /\.\/middleware.mjs/g }, () => {
-        console.log("middleware.mjs");
-        return {
-          path: path.join(outputPath, "edgeFunctionHandler.js"),
-        };
-      });
-
-      build.onLoad({ filter: /core\/resolve.ts/g }, async (args) => {
-        let contents = readFileSync(args.path, "utf-8");
-        if (overrides?.wrapper) {
-          contents = contents.replace(
-            "../wrappers/aws-lambda.js",
-            `../wrappers/${overrides.wrapper}.js`,
-          );
-        }
-        return {
-          contents,
-        };
-      });
-
-      build.onLoad({ filter: /adapters\/config\/index.ts/g }, async () => {
-        console.log("opennext-config-plugin");
-        const NextConfig = loadConfig(nextDir);
-        const BuildId = loadBuildId(nextDir);
-        const HtmlPages = loadHtmlPages(nextDir);
-        const RoutesManifest = loadRoutesManifest(nextDir);
-        const ConfigHeaders = loadConfigHeaders(nextDir);
-        const PrerenderManifest = loadPrerenderManifest(nextDir);
-        const AppPathsManifestKeys = loadAppPathsManifestKeys(nextDir);
-        const MiddlewareManifest = loadMiddlewareManifest(nextDir);
-
-        const contents = `
-import path from "path";
-
-import { debug } from "../logger";
-
-if(!globalThis.__dirname) {
-  globalThis.__dirname = ""
-}
-
-export const NEXT_DIR = path.join(__dirname, ".next");
-export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");
-
-debug({ NEXT_DIR, OPEN_NEXT_DIR });
-
-export const NextConfig = ${JSON.stringify(NextConfig)};
-export const BuildId = ${JSON.stringify(BuildId)};
-export const HtmlPages = ${JSON.stringify(HtmlPages)};
-export const RoutesManifest = ${JSON.stringify(RoutesManifest)};
-export const ConfigHeaders = ${JSON.stringify(ConfigHeaders)};
-export const PrerenderManifest = ${JSON.stringify(PrerenderManifest)};
-export const AppPathsManifestKeys = ${JSON.stringify(AppPathsManifestKeys)};
-export const MiddlewareManifest = ${JSON.stringify(MiddlewareManifest)};
-
-        `;
-        return {
-          contents,
-        };
-      });
-    },
-  };
-}
diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
index dd1f44a78..a5c59dd62 100644
--- a/packages/open-next/src/adapters/middleware.ts
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -4,6 +4,8 @@ import { createGenericHandler } from "../core/createGenericHandler";
 import routingHandler from "../core/routingHandler";
 
 const defaultHandler = async (internalEvent: InternalEvent) => {
+  // TODO: We need to handle splitted function here
+  // We should probably create an host resolver to redirect correctly
   const result = await routingHandler(internalEvent);
   if ("internalEvent" in result) {
     return {
@@ -20,5 +22,8 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
 export const handler = await createGenericHandler({
   handler: defaultHandler,
   type: "middleware",
-  defaultConverter: "edge",
 });
+
+export default {
+  fetch: handler,
+};
diff --git a/packages/open-next/src/adapters/revalidate.ts b/packages/open-next/src/adapters/revalidate.ts
index c992a874e..471d4b1a7 100644
--- a/packages/open-next/src/adapters/revalidate.ts
+++ b/packages/open-next/src/adapters/revalidate.ts
@@ -64,7 +64,6 @@ const defaultHandler = async (event: RevalidateEvent) => {
 export const handler = await createGenericHandler({
   handler: defaultHandler,
   type: "revalidate",
-  defaultConverter: "sqs-revalidate",
 });
 
 function loadPrerenderManifest() {
diff --git a/packages/open-next/src/adapters/warmer-function.ts b/packages/open-next/src/adapters/warmer-function.ts
index c79edd382..95a1d6224 100644
--- a/packages/open-next/src/adapters/warmer-function.ts
+++ b/packages/open-next/src/adapters/warmer-function.ts
@@ -77,7 +77,6 @@ const resolveWarmerInvoke = async () => {
 export const handler = await createGenericHandler({
   handler: defaultHandler,
   type: "warmer",
-  defaultConverter: "dummy",
 });
 
 async function defaultHandler() {
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 25120c128..fc14d8187 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -10,10 +10,11 @@ import {
   buildSync,
 } from "esbuild";
 
-import openNextConfigPlugin from "./adapters/config/plugin.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
-import openNextPlugin from "./plugin.js";
+import { openNextEdgePlugins } from "./plugins/edge.js";
+import { openNextReplacementPlugin } from "./plugins/replacement.js";
+import { openNextResolvePlugin } from "./plugins/resolve.js";
 import { BuildOptions, DangerousOptions } from "./types/open-next.js";
 
 const require = topLevelCreateRequire(import.meta.url);
@@ -55,12 +56,15 @@ export async function build(
   createOpenNextConfigBundle(options.tempDir);
   createStaticAssets();
   if (!options.dangerous?.disableIncrementalCache) {
-    createCacheAssets(monorepoRoot, options.dangerous?.disableDynamoDBCache);
+    await createCacheAssets(
+      monorepoRoot,
+      options.dangerous?.disableDynamoDBCache,
+    );
   }
   await createServerBundle(monorepoRoot, options.streaming);
-  createRevalidationBundle();
+  await createRevalidationBundle();
   createImageOptimizationBundle();
-  createWarmerBundle();
+  await createWarmerBundle();
   if (options.minify) {
     await minifyServerBundle();
   }
@@ -216,7 +220,7 @@ function initOutputDir() {
   fs.mkdirSync(tempDir, { recursive: true });
 }
 
-function createWarmerBundle() {
+async function createWarmerBundle() {
   logger.info(`Bundling warmer function...`);
 
   const { outputDir } = options;
@@ -235,10 +239,17 @@ function createWarmerBundle() {
   // note: bundle in OpenNext package b/c the adatper relys on the
   //       "serverless-http" package which is not a dependency in user's
   //       Next.js app.
-  esbuildSync({
+  await esbuildAsync({
     entryPoints: [path.join(__dirname, "adapters", "warmer-function.js")],
     external: ["next"],
     outfile: path.join(outputPath, "index.mjs"),
+    plugins: [
+      openNextResolvePlugin({
+        overrides: {
+          converter: "dummy",
+        },
+      }),
+    ],
     banner: {
       js: [
         "import { createRequire as topLevelCreateRequire } from 'module';",
@@ -259,7 +270,7 @@ async function minifyServerBundle() {
   });
 }
 
-function createRevalidationBundle() {
+async function createRevalidationBundle() {
   logger.info(`Bundling revalidation function...`);
 
   const { appBuildOutputPath, outputDir } = options;
@@ -275,10 +286,17 @@ function createRevalidationBundle() {
   );
 
   // Build Lambda code
-  esbuildSync({
+  esbuildAsync({
     external: ["next", "styled-jsx", "react"],
     entryPoints: [path.join(__dirname, "adapters", "revalidate.js")],
     outfile: path.join(outputPath, "index.mjs"),
+    plugins: [
+      openNextResolvePlugin({
+        overrides: {
+          converter: "sqs-revalidate",
+        },
+      }),
+    ],
   });
 
   // Copy over .next/prerender-manifest.json file
@@ -407,7 +425,10 @@ function createStaticAssets() {
   }
 }
 
-function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
+async function createCacheAssets(
+  monorepoRoot: string,
+  disableDynamoDBCache = false,
+) {
   logger.info(`Bundling cache assets...`);
 
   const { appBuildOutputPath, outputDir } = options;
@@ -564,11 +585,18 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
     if (metaFiles.length > 0) {
       const providerPath = path.join(outputDir, "dynamodb-provider");
 
-      esbuildSync({
+      await esbuildAsync({
         external: ["@aws-sdk/client-dynamodb"],
         entryPoints: [path.join(__dirname, "adapters", "dynamo-provider.js")],
         outfile: path.join(providerPath, "index.mjs"),
         target: ["node18"],
+        plugins: [
+          openNextResolvePlugin({
+            overrides: {
+              converter: "dummy",
+            },
+          }),
+        ],
       });
 
       //Copy open-next.config.js into the bundle
@@ -645,7 +673,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
     compareSemver(options.nextVersion, "13.4.13") <= 0 ||
     options.externalMiddleware;
   const plugins = [
-    openNextPlugin({
+    openNextReplacementPlugin({
       name: "requestHandlerOverride",
       target: /core\/requestHandler.js/g,
       deletes: disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : [],
@@ -657,7 +685,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
           ]
         : [],
     }),
-    openNextPlugin({
+    openNextReplacementPlugin({
       name: "core/util",
       target: /core\/util.js/g,
       deletes: [
@@ -729,42 +757,38 @@ async function createMiddleware() {
     // Bundle middleware
     await esbuildAsync({
       entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
-      inject: entry.files.map((file: string) =>
-        path.join(appBuildOutputPath, ".next", file),
-      ),
+      // inject: ,
       bundle: true,
       outfile: path.join(outputPath, "handler.mjs"),
-      external: ["node:*", "fs", "path", "next", "@aws-sdk/*", "stream"],
+      external: ["node:*", "next", "@aws-sdk/*"],
       target: "es2022",
       platform: "neutral",
       plugins: [
-        openNextConfigPlugin({
-          nextDir: path.join(appBuildOutputPath, ".next"),
-          outputPath: path.join(__dirname, "core"),
+        openNextResolvePlugin({
           overrides: {
             wrapper: "cloudflare",
+            converter: "edge",
           },
         }),
+        openNextEdgePlugins({
+          entryFiles: entry.files.map((file: string) =>
+            path.join(appBuildOutputPath, ".next", file),
+          ),
+          nextDir: path.join(appBuildOutputPath, ".next"),
+          edgeFunctionHandlerPath: path.join(
+            __dirname,
+            "core",
+            "edgeFunctionHandler.js",
+          ),
+        }),
       ],
       treeShaking: true,
       alias: {
         path: "node:path",
         stream: "node:stream",
       },
-      banner: {
-        js: `
-globalThis._ENTRIES = {};
-globalThis.self = globalThis;
-globalThis.process = {env: {}}
-
-import {Buffer} from "node:buffer";
-globalThis.Buffer = Buffer;
-
-import {AsyncLocalStorage} from "node:async_hooks";
-globalThis.AsyncLocalStorage = AsyncLocalStorage;
-  
-  `,
-      },
+      conditions: ["module"],
+      mainFields: ["module", "main"],
     });
   } else {
     buildEdgeFunction(
diff --git a/packages/open-next/src/converters/docker.ts b/packages/open-next/src/converters/node.ts
similarity index 100%
rename from packages/open-next/src/converters/docker.ts
rename to packages/open-next/src/converters/node.ts
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
index 21c10361a..aef5e67ef 100644
--- a/packages/open-next/src/core/createGenericHandler.ts
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -27,7 +27,6 @@ type GenericHandler<
 > = {
   handler: OpenNextHandler<E, R>;
   type: Type;
-  defaultConverter?: string;
 };
 
 export async function createGenericHandler<
@@ -48,10 +47,7 @@ export async function createGenericHandler<
     ?.override as any as DefaultOverrideOptions<E, R>;
 
   // From the config, we create the adapter
-  const adapter = await resolveConverter<E, R>(
-    override?.converter,
-    handler.defaultConverter,
-  );
+  const adapter = await resolveConverter<E, R>(override?.converter);
 
   // Then we create the handler
   const wrapper = await resolveWrapper<E, R>(override?.wrapper);
diff --git a/packages/open-next/src/core/resolve.ts b/packages/open-next/src/core/resolve.ts
index d79d99b76..918d85d6c 100644
--- a/packages/open-next/src/core/resolve.ts
+++ b/packages/open-next/src/core/resolve.ts
@@ -10,19 +10,17 @@ import {
 
 import { TagCache } from "../cache/tag/types.js";
 
-// TODO: We should probably use open-next plugins here to avoid bundling unnecessary code
-
 export async function resolveConverter<
   E extends BaseEventOrResult = InternalEvent,
   R extends BaseEventOrResult = InternalResult,
 >(
   converter: DefaultOverrideOptions<E, R>["converter"],
-  defaultConverter: string = "aws-apigw-v2",
 ): Promise<Converter<E, R>> {
   if (typeof converter === "function") {
     return converter();
   } else {
-    const m_1 = await import(`../converters/${defaultConverter}.js`);
+    const m_1 = await import(`../converters/aws-apigw-v2.js`);
+    // @ts-expect-error
     return m_1.default;
   }
 }
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
new file mode 100644
index 000000000..5f89f9c52
--- /dev/null
+++ b/packages/open-next/src/plugins/edge.ts
@@ -0,0 +1,108 @@
+import { readFileSync } from "node:fs";
+
+import { Plugin } from "esbuild";
+
+import {
+  loadAppPathsManifestKeys,
+  loadBuildId,
+  loadConfig,
+  loadConfigHeaders,
+  loadHtmlPages,
+  loadMiddlewareManifest,
+  loadPrerenderManifest,
+  loadRoutesManifest,
+} from "../adapters/config/util.js";
+
+export interface IPluginSettings {
+  nextDir: string;
+  edgeFunctionHandlerPath?: string;
+  entryFiles: string[];
+}
+
+/**
+ * TODO: Handle wasm import
+ * @param opts.nextDir - The path to the .next directory
+ * @param opts.edgeFunctionHandlerPath - The path to the edgeFunctionHandler.js file that we'll use to bundle the routing
+ * @param opts.entryFiles - The entry files that we'll inject into the edgeFunctionHandler.js file
+ * @returns
+ */
+export function openNextEdgePlugins({
+  nextDir,
+  edgeFunctionHandlerPath,
+  entryFiles,
+}: IPluginSettings): Plugin {
+  return {
+    name: "opennext-edge",
+    setup(build) {
+      if (edgeFunctionHandlerPath) {
+        // If we bundle the routing, we need to resolve the middleware
+        build.onResolve({ filter: /\.\/middleware.mjs/g }, () => {
+          return {
+            path: edgeFunctionHandlerPath,
+          };
+        });
+      }
+
+      // We inject the entry files into the edgeFunctionHandler
+      build.onLoad({ filter: /\/edgeFunctionHandler.js/g }, async (args) => {
+        let contents = readFileSync(args.path, "utf-8");
+        contents = `
+globalThis._ENTRIES = {};
+globalThis.self = globalThis;
+globalThis.process = {env: {}}
+
+import {Buffer} from "node:buffer";
+globalThis.Buffer = Buffer;
+
+import {AsyncLocalStorage} from "node:async_hooks";
+globalThis.AsyncLocalStorage = AsyncLocalStorage;
+${entryFiles?.map((file) => `require("${file}");`).join("\n")}
+${contents}        
+        `;
+        return {
+          contents,
+        };
+      });
+
+      build.onLoad({ filter: /adapters\/config\/index.ts/g }, async () => {
+        console.log("opennext-config-plugin");
+        const NextConfig = loadConfig(nextDir);
+        const BuildId = loadBuildId(nextDir);
+        const HtmlPages = loadHtmlPages(nextDir);
+        const RoutesManifest = loadRoutesManifest(nextDir);
+        const ConfigHeaders = loadConfigHeaders(nextDir);
+        const PrerenderManifest = loadPrerenderManifest(nextDir);
+        const AppPathsManifestKeys = loadAppPathsManifestKeys(nextDir);
+        const MiddlewareManifest = loadMiddlewareManifest(nextDir);
+
+        const contents = `
+  import path from "path";
+  
+  import { debug } from "../logger";
+  
+  if(!globalThis.__dirname) {
+    globalThis.__dirname = ""
+  }
+  
+  export const NEXT_DIR = path.join(__dirname, ".next");
+  export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");
+  
+  debug({ NEXT_DIR, OPEN_NEXT_DIR });
+  
+  export const NextConfig = ${JSON.stringify(NextConfig)};
+  export const BuildId = ${JSON.stringify(BuildId)};
+  export const HtmlPages = ${JSON.stringify(HtmlPages)};
+  export const RoutesManifest = ${JSON.stringify(RoutesManifest)};
+  export const ConfigHeaders = ${JSON.stringify(ConfigHeaders)};
+  export const PrerenderManifest = ${JSON.stringify(PrerenderManifest)};
+  export const AppPathsManifestKeys = ${JSON.stringify(AppPathsManifestKeys)};
+  export const MiddlewareManifest = ${JSON.stringify(MiddlewareManifest)};
+  
+          `;
+        return {
+          contents,
+        };
+      });
+    },
+  };
+}
diff --git a/packages/open-next/src/plugin.ts b/packages/open-next/src/plugins/replacement.ts
similarity index 97%
rename from packages/open-next/src/plugin.ts
rename to packages/open-next/src/plugins/replacement.ts
index f50bd43a1..60279ed33 100644
--- a/packages/open-next/src/plugin.ts
+++ b/packages/open-next/src/plugins/replacement.ts
@@ -2,7 +2,7 @@ import { readFile } from "node:fs/promises";
 
 import { Plugin } from "esbuild";
 
-import logger from "./logger.js";
+import logger from "../logger.js";
 
 export interface IPluginSettings {
   target: RegExp;
@@ -48,7 +48,7 @@ const importPattern = /\/\/#import([\s\S]*?)\n\/\/#endImport/gm;
  * @param opts.deletes - list of ids to delete from the target
  * @returns
  */
-export default function openNextPlugin({
+export function openNextReplacementPlugin({
   target,
   replacements,
   deletes,
diff --git a/packages/open-next/src/plugins/resolve.ts b/packages/open-next/src/plugins/resolve.ts
new file mode 100644
index 000000000..54a751a06
--- /dev/null
+++ b/packages/open-next/src/plugins/resolve.ts
@@ -0,0 +1,52 @@
+import { readFileSync } from "node:fs";
+
+import { Plugin } from "esbuild";
+import type {
+  IncludedConverter,
+  IncludedIncrementalCache,
+  IncludedQueue,
+  IncludedTagCache,
+  IncludedWrapper,
+} from "types/open-next";
+
+export interface IPluginSettings {
+  overrides: {
+    wrapper?: IncludedWrapper;
+    converter?: IncludedConverter;
+    // Right now theses do nothing since there is only one implementation
+    tag?: IncludedTagCache;
+    queue?: IncludedQueue;
+    incrementalCache?: IncludedIncrementalCache;
+  };
+}
+
+/**
+ * @param opts.overrides - The name of the overrides to use
+ * @returns
+ */
+export function openNextResolvePlugin({ overrides }: IPluginSettings): Plugin {
+  return {
+    name: "opennext-resolve",
+    setup(build) {
+      build.onLoad({ filter: /core\/resolve.js/g }, async (args) => {
+        let contents = readFileSync(args.path, "utf-8");
+        if (overrides?.wrapper) {
+          contents = contents.replace(
+            "../wrappers/aws-lambda.js",
+            `../wrappers/${overrides.wrapper}.js`,
+          );
+        }
+        if (overrides?.converter) {
+          console.log("converter", overrides.converter);
+          contents = contents.replace(
+            "../converters/aws-apigw-v2.js",
+            `../converters/${overrides.converter}.js`,
+          );
+        }
+        return {
+          contents,
+        };
+      });
+    },
+  };
+}
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index ea4eaf85b..e7ff73b1f 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -82,6 +82,27 @@ type ImageLoader = (url: string) => Promise<{
   cacheControl?: string;
 }>;
 
+export type IncludedWrapper =
+  | "aws-lambda"
+  | "aws-lambda-streaming"
+  | "node"
+  | "cloudflare";
+
+export type IncludedConverter =
+  | "aws-apigw-v2"
+  | "aws-apigw-v1"
+  | "aws-cloudfront"
+  | "edge"
+  | "node"
+  | "sqs-revalidate"
+  | "dummy";
+
+export type IncludedQueue = "sqs";
+
+export type IncludedIncrementalCache = "s3";
+
+export type IncludedTagCache = "dynamodb";
+
 export interface DefaultOverrideOptions<
   E extends BaseEventOrResult = InternalEvent,
   R extends BaseEventOrResult = InternalResult,
@@ -90,24 +111,13 @@ export interface DefaultOverrideOptions<
    * This is the main entrypoint of your app.
    * @default "aws-lambda"
    */
-  wrapper?:
-    | "aws-lambda"
-    | "aws-lambda-streaming"
-    | "docker"
-    | "cloudflare"
-    | LazyLoadedOverride<Wrapper<E, R>>;
+  wrapper?: IncludedWrapper | LazyLoadedOverride<Wrapper<E, R>>;
 
   /**
    * This code convert the event to InternalEvent and InternalResult to the expected output.
    * @default "aws-apigw-v2"
    */
-  converter?:
-    | "aws-apigw-v2"
-    | "aws-apigw-v1"
-    | "aws-cloudfront"
-    | "edge"
-    | "docker"
-    | LazyLoadedOverride<Converter<E, R>>;
+  converter?: IncludedConverter | LazyLoadedOverride<Converter<E, R>>;
 }
 
 export interface OverrideOptions extends DefaultOverrideOptions {

From 51b5d46330b3f76efdc648e1e7eeecf1775a0804 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 24 Nov 2023 16:27:35 +0100
Subject: [PATCH 020/102] fix proxy request and make it work with streaming

---
 .../open-next/src/adapters/dynamo-provider.ts |  4 +-
 .../adapters/image-optimization-adapter.ts    |  2 +-
 packages/open-next/src/build.ts               | 22 +++++-
 packages/open-next/src/core/requestHandler.ts |  2 +-
 packages/open-next/src/core/routing/util.ts   | 77 ++++++++++---------
 .../src/wrappers/aws-lambda-streaming.ts      |  8 +-
 .../src/wrappers/{docker.ts => node.ts}       |  0
 7 files changed, 73 insertions(+), 42 deletions(-)
 rename packages/open-next/src/wrappers/{docker.ts => node.ts} (100%)

diff --git a/packages/open-next/src/adapters/dynamo-provider.ts b/packages/open-next/src/adapters/dynamo-provider.ts
index 0b267ae9a..5d15e6c0c 100644
--- a/packages/open-next/src/adapters/dynamo-provider.ts
+++ b/packages/open-next/src/adapters/dynamo-provider.ts
@@ -30,10 +30,10 @@ interface InitializationFunctionEvent {
 }
 
 const tagCache = await resolveTagCache(
-  globalThis.openNextConfig.initializationFunction?.tagCache,
+  globalThis.openNextConfig?.initializationFunction?.tagCache,
 );
 
-export const handler = createGenericHandler({
+export const handler = await createGenericHandler({
   handler: defaultHandler,
   type: "initializationFunction",
 });
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index 4d018a612..d27081c05 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -50,7 +50,7 @@ debug("Init config", {
 // Handler //
 /////////////
 
-export const handler = createGenericHandler({
+export const handler = await createGenericHandler({
   handler: defaultHandler,
   type: "imageOptimization",
 });
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index fc14d8187..ac1c2ae28 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -61,7 +61,7 @@ export async function build(
       options.dangerous?.disableDynamoDBCache,
     );
   }
-  await createServerBundle(monorepoRoot, options.streaming);
+  await createServerBundle(monorepoRoot, opts);
   await createRevalidationBundle();
   createImageOptimizationBundle();
   await createWarmerBundle();
@@ -621,7 +621,10 @@ async function createCacheAssets(
 /* Server Helper Functions */
 /***************************/
 
-async function createServerBundle(monorepoRoot: string, streaming = false) {
+async function createServerBundle(
+  monorepoRoot: string,
+  buildOptions: BuildOptions,
+) {
   logger.info(`Bundling server function...`);
 
   const { appPath, appBuildOutputPath, outputDir } = options;
@@ -669,6 +672,8 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
     compareSemver(options.nextVersion, "13.5.1") >= 0 ||
     compareSemver(options.nextVersion, "13.4.1") <= 0;
 
+  const overrides = buildOptions.functions.default.override ?? {};
+
   const disableRouting =
     compareSemver(options.nextVersion, "13.4.13") <= 0 ||
     options.externalMiddleware;
@@ -693,6 +698,19 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
         ...(disableRouting ? ["trustHostHeader"] : []),
       ],
     }),
+
+    openNextResolvePlugin({
+      overrides: {
+        converter:
+          typeof overrides.converter === "function"
+            ? "dummy"
+            : overrides.converter,
+        wrapper:
+          typeof overrides.wrapper === "function"
+            ? "aws-lambda"
+            : overrides.wrapper,
+      },
+    }),
   ];
 
   if (plugins && plugins.length > 0) {
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index e65bc4f1c..de8994068 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -91,7 +91,7 @@ async function processRequest(
     const { rawPath } = internalEvent;
 
     if (isExternalRewrite) {
-      return proxyRequest(req, res);
+      return proxyRequest(internalEvent, res);
     } else {
       //#override applyNextjsPrebundledReact
       setNextjsPrebundledReact(rawPath);
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index 21ecf3912..ccd00c370 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -1,5 +1,6 @@
 import crypto from "node:crypto";
-import { OutgoingHttpHeaders, ServerResponse } from "node:http";
+import { OutgoingHttpHeaders } from "node:http";
+import { request } from "node:https";
 
 import { BuildId, HtmlPages } from "config/index.js";
 import type { IncomingMessage, StreamCreator } from "http/index.js";
@@ -121,46 +122,52 @@ export function unescapeRegex(str: string) {
 }
 
 export async function proxyRequest(
-  req: IncomingMessage,
+  internalEvent: InternalEvent,
   res: OpenNextNodeResponse,
 ) {
-  // TODO: we should use our own version instead of the one bundled with Next.js
-  const HttpProxy = require("next/dist/compiled/http-proxy") as any;
-
-  const proxy = new HttpProxy({
-    changeOrigin: true,
-    ignorePath: true,
-    xfwd: true,
-  });
-
+  const { url, headers, method, body } = internalEvent;
+  debug("proxyRequest", url);
   await new Promise<void>((resolve, reject) => {
-    proxy.on("proxyRes", (proxyRes: ServerResponse) => {
-      const body: Uint8Array[] = [];
-      proxyRes.on("data", function (chunk) {
-        body.push(chunk);
-      });
-      proxyRes.on("end", function () {
-        const newBody = Buffer.concat(body).toString();
-        debug(`Proxying response`, {
-          headers: proxyRes.getHeaders(),
-          body: newBody,
+    const { host: _host, ...filteredHeaders } = headers;
+    debug("filteredHeaders", filteredHeaders);
+    const req = request(
+      url,
+      {
+        headers: filteredHeaders,
+        method,
+        rejectUnauthorized: false,
+      },
+      (_res) => {
+        res.writeHead(_res.statusCode ?? 200, _res.headers);
+        if (_res.headers["content-encoding"] === "br") {
+          _res.pipe(require("node:zlib").createBrotliDecompress()).pipe(res);
+        } else if (_res.headers["content-encoding"] === "gzip") {
+          _res.pipe(require("node:zlib").createGunzip()).pipe(res);
+        } else {
+          _res.pipe(res);
+        }
+
+        _res.on("error", (e) => {
+          console.log("error", e);
+          res.end();
+          reject(e);
         });
-        res.end(newBody);
-        resolve();
-      });
-    });
-
-    proxy.on("error", (err: any) => {
-      reject(err);
-    });
-
-    debug(`Proxying`, { url: req.url, headers: req.headers });
+        res.on("end", () => {
+          console.log("end");
+          // res.end();
+          resolve();
+        });
+      },
+    );
 
-    proxy.web(req, res, {
-      target: req.url,
-      headers: req.headers,
-    });
+    if (body && method !== "GET" && method !== "HEAD") {
+      req.write(body);
+    }
+    req.end();
   });
+  // console.log("result", result);
+  // res.writeHead(result.status, resHeaders);
+  // res.end(await result.text());
 }
 
 declare global {
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 198ee8c12..c8a4cd44d 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -6,6 +6,7 @@ import { StreamCreator } from "http/index.js";
 import { parseCookies, parseHeaders } from "http/util.js";
 import { Wrapper } from "types/open-next";
 
+import { error } from "../adapters/logger";
 import { WarmerEvent } from "../adapters/warmer-function";
 
 type AwsLambdaEvent = APIGatewayProxyEventV2 | WarmerEvent;
@@ -19,10 +20,15 @@ const handler: Wrapper = async (handler, converter) =>
       let _headersSent = false;
 
       //Handle compression
-      const acceptEncoding = internalEvent.headers["accept-encoding"] ?? "";
+      const acceptEncoding = internalEvent.headers?.["accept-encoding"] ?? "";
       let contentEncoding: string;
       let compressedStream: Writable | undefined;
 
+      responseStream.on("error", (err) => {
+        error(err);
+        responseStream.end();
+      });
+
       if (acceptEncoding.includes("br")) {
         contentEncoding = "br";
         compressedStream = zlib.createBrotliCompress({
diff --git a/packages/open-next/src/wrappers/docker.ts b/packages/open-next/src/wrappers/node.ts
similarity index 100%
rename from packages/open-next/src/wrappers/docker.ts
rename to packages/open-next/src/wrappers/node.ts

From bac04a28502dec0fabf17af9286c0da57201dbd1 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 25 Nov 2023 09:04:12 +0100
Subject: [PATCH 021/102] bugfix

---
 .../adapters/image-optimization-adapter.ts    |  2 +-
 packages/open-next/src/build.ts               | 13 +++---
 packages/open-next/src/core/requestHandler.ts |  2 +-
 .../open-next/src/core/routing/middleware.ts  | 40 ++++++-------------
 packages/open-next/src/core/routing/util.ts   |  6 +--
 packages/open-next/src/core/routingHandler.ts |  3 +-
 .../open-next/src/http/openNextResponse.ts    |  7 +++-
 .../src/wrappers/aws-lambda-streaming.ts      | 18 +++------
 packages/open-next/src/wrappers/node.ts       |  3 ++
 9 files changed, 40 insertions(+), 54 deletions(-)

diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index d27081c05..d8bb958f6 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -227,7 +227,7 @@ async function downloadHandler(
       }
 
       // @ts-ignore
-      pipeRes(response.Body, res);
+      pipeRes(response.body, res);
 
       // Respect the bucket file's content-type and cache-control
       // imageOptimizer will use this to set the results.maxAge
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index ac1c2ae28..0307ea81e 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -643,13 +643,14 @@ async function createServerBundle(
 
   // Copy open-next.config.js
   // We should reuse the one we created at the beginning of the build
+  fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });
   fs.copyFileSync(
     path.join(options.tempDir, "open-next.config.js"),
     path.join(outputPath, packagePath, "open-next.config.js"),
   );
 
   // Bundle middleware
-  createMiddleware();
+  createMiddleware(packagePath);
 
   // Copy over standalone output files
   // note: if user uses pnpm as the package manager, node_modules contain
@@ -745,7 +746,7 @@ async function createServerBundle(
   addCacheHandler(outputPath, options.dangerous);
 }
 
-async function createMiddleware() {
+async function createMiddleware(packagePath: string) {
   console.info(`Bundling middleware function...`);
 
   const { appBuildOutputPath, outputDir, externalMiddleware } = options;
@@ -759,6 +760,9 @@ async function createMiddleware() {
   );
 
   const entry = middlewareManifest.middleware["/"];
+  if (!entry) {
+    return;
+  }
 
   // Create output folder
   let outputPath = path.join(outputDir, "server-function");
@@ -777,7 +781,7 @@ async function createMiddleware() {
       entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
       // inject: ,
       bundle: true,
-      outfile: path.join(outputPath, "handler.mjs"),
+      outfile: path.join(outputPath, packagePath, "handler.mjs"),
       external: ["node:*", "next", "@aws-sdk/*"],
       target: "es2022",
       platform: "neutral",
@@ -812,7 +816,7 @@ async function createMiddleware() {
     buildEdgeFunction(
       entry,
       path.join(__dirname, "core", "edgeFunctionHandler.js"),
-      path.join(outputPath, "middleware.mjs"),
+      path.join(outputPath, packagePath, "middleware.mjs"),
       appBuildOutputPath,
     );
   }
@@ -981,7 +985,6 @@ function addCacheHandler(outputPath: string, options?: DangerousOptions) {
 
 function esbuildSync(esbuildOptions: ESBuildOptions) {
   const { openNextVersion, debug } = options;
-  console.log(["./open-next.config.js", ...(esbuildOptions.external ?? [])]);
   const result = buildSync({
     target: "esnext",
     format: "esm",
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index de8994068..ec3662411 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -48,7 +48,7 @@ export async function openNextHandler(
     const req = new IncomingMessage(reqProps);
     const res = createServerResponse(
       preprocessedEvent,
-      preprocessResult.headers as any,
+      preprocessResult.headers as Record<string, string | string[]>,
       responseStreaming,
     );
 
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 0cf122b49..94bf8a7ef 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -8,8 +8,6 @@ import { InternalEvent, InternalResult } from "types/open-next.js";
 // const {
 //   signalFromNodeResponse,
 // } = require("next/dist/server/web/spec-extension/adapters/next-request");
-// @ts-expect-error - This is bundled
-import middleware from "./middleware.mjs";
 import {
   convertToQueryString,
   getMiddlewareMatch,
@@ -58,27 +56,6 @@ export async function handleMiddleware(
   initialUrl.search = convertToQueryString(query);
   const url = initialUrl.toString();
 
-  // const result: MiddlewareResult = await run({
-  //   distDir: NEXT_DIR,
-  //   name: middlewareInfo.name || "/",
-  //   paths: middlewareInfo.paths || [],
-  //   edgeFunctionEntry: middlewareInfo,
-  //   request: {
-  //     headers: req.headers,
-  //     method: req.method || "GET",
-  //     nextConfig: {
-  //       basePath: NextConfig.basePath,
-  //       i18n: NextConfig.i18n,
-  //       trailingSlash: NextConfig.trailingSlash,
-  //     },
-  //     url,
-  //     body: getCloneableBody(req),
-  //     signal: signalFromNodeResponse(res),
-  //   },
-  //   useCache: true,
-  //   onWarning: console.warn,
-  // });
-
   const convertBodyToReadableStream = (body: string | Buffer) => {
     const readable = new ReadableStream({
       start(controller) {
@@ -89,7 +66,10 @@ export async function handleMiddleware(
     return readable;
   };
 
-  const result: Response = await middleware(
+  // @ts-expect-error - This is bundled
+  const middleware = await import("./middleware.mjs");
+
+  const result: Response = await middleware.default(
     [
       {
         name: middlewareInfo.name || "/",
@@ -125,7 +105,7 @@ export async function handleMiddleware(
   */
   const responseHeaders = result.headers as Headers;
   const reqHeaders: Record<string, string> = {};
-  const resHeaders: Record<string, string> = {};
+  const resHeaders: Record<string, string | string[]> = {};
 
   responseHeaders.delete("x-middleware-override-headers");
   const xMiddlewareKey = "x-middleware-request-";
@@ -133,10 +113,14 @@ export async function handleMiddleware(
     if (key.startsWith(xMiddlewareKey)) {
       const k = key.substring(xMiddlewareKey.length);
       reqHeaders[k] = value;
-      // req.headers[k] = value;
     } else {
-      resHeaders[key] = value;
-      // res.setHeader(key, value);
+      if (key.toLowerCase() === "set-cookie") {
+        resHeaders[key] = resHeaders[key]
+          ? [...resHeaders[key], value]
+          : [value];
+      } else {
+        resHeaders[key] = value;
+      }
     }
   });
 
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index ccd00c370..9baa472bf 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -10,7 +10,7 @@ import type { MiddlewareManifest } from "types/next-types";
 import { InternalEvent } from "types/open-next.js";
 
 import { isBinaryContentType } from "../../adapters/binary.js";
-import { debug } from "../../adapters/logger.js";
+import { debug, error } from "../../adapters/logger.js";
 
 export function isExternal(url?: string, host?: string) {
   if (!url) return false;
@@ -148,13 +148,11 @@ export async function proxyRequest(
         }
 
         _res.on("error", (e) => {
-          console.log("error", e);
+          error("proxyRequest error", e);
           res.end();
           reject(e);
         });
         res.on("end", () => {
-          console.log("end");
-          // res.end();
           resolve();
         });
       },
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index fa2ab18dd..9ac75c6bc 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -9,6 +9,7 @@ import {
 import type { OutgoingHttpHeaders } from "http";
 import { InternalEvent, InternalResult } from "types/open-next";
 
+import { debug } from "../adapters/logger";
 import {
   addNextConfigHeaders,
   fixDataPage,
@@ -35,7 +36,7 @@ export default async function routingHandler(
 
   const redirect = handleRedirects(internalEvent, RoutesManifest.redirects);
   if (redirect) {
-    console.log("redirect", redirect);
+    debug("redirect", redirect);
     return redirect;
   }
 
diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts
index b0d1fafdd..ff8610493 100644
--- a/packages/open-next/src/http/openNextResponse.ts
+++ b/packages/open-next/src/http/openNextResponse.ts
@@ -13,6 +13,7 @@ export interface StreamCreator {
   }): Writable;
   // Just to fix an issue with aws lambda streaming with empty body
   onWrite?: () => void;
+  onFinish: () => void;
 }
 
 // We only need to implement the methods that are used by next.js
@@ -38,7 +39,11 @@ export class OpenNextNodeResponse extends Transform {
       ) as string[];
     }
     this.once("finish", () => {
+      if (!this.headersSent) {
+        this.flushHeaders();
+      }
       onEnd(this.headers);
+      this.streamCreator?.onFinish();
     });
   }
 
@@ -87,13 +92,13 @@ export class OpenNextNodeResponse extends Transform {
   // Only used directly in next@14+
   flushHeaders() {
     this.headersSent = true;
+    this.fixHeaders(this.headers);
     if (this.initialHeaders) {
       this.headers = {
         ...this.headers,
         ...this.initialHeaders,
       };
     }
-    this.fixHeaders(this.headers);
 
     if (this.streamCreator) {
       this.responseStream = this.streamCreator?.writeHeaders({
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index c8a4cd44d..d14c195c0 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -3,7 +3,6 @@ import zlib from "node:zlib";
 
 import { APIGatewayProxyEventV2 } from "aws-lambda";
 import { StreamCreator } from "http/index.js";
-import { parseCookies, parseHeaders } from "http/util.js";
 import { Wrapper } from "types/open-next";
 
 import { error } from "../adapters/logger";
@@ -95,22 +94,15 @@ const handler: Wrapper = async (handler, converter) =>
             compressedStream?.uncork();
           }
         },
+        onFinish: () => {
+          if (!_hasWriten) {
+            compressedStream?.end(new Uint8Array(8));
+          }
+        },
       };
 
       const response = await handler(internalEvent, streamCreator);
 
-      if (!compressedStream.writableFinished) {
-        // If the headers are not sent, we need to send them
-        if (!_headersSent) {
-          streamCreator.writeHeaders({
-            statusCode: response?.statusCode ?? 500,
-            cookies: parseCookies(response?.headers["set-cookie"]) ?? [],
-            headers: parseHeaders(response?.headers),
-          });
-        }
-        compressedStream.end(_hasWriten ? undefined : new Uint8Array(8));
-      }
-
       return converter.convertTo(response);
     },
   );
diff --git a/packages/open-next/src/wrappers/node.ts b/packages/open-next/src/wrappers/node.ts
index 032de2443..12f6639a6 100644
--- a/packages/open-next/src/wrappers/node.ts
+++ b/packages/open-next/src/wrappers/node.ts
@@ -14,6 +14,9 @@ const wrapper: Wrapper = async (handler, converter) => {
         res.uncork();
         return res;
       },
+      onFinish: () => {
+        // Is it necessary to do something here?
+      },
     };
 
     await handler(internalEvent, _res);

From df1142b803de6a5f44c55e6e19babdd52dba215f Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 25 Nov 2023 15:06:09 +0100
Subject: [PATCH 022/102] fix host

---
 packages/open-next/src/build.ts               | 7 ++++---
 packages/open-next/src/core/requestHandler.ts | 4 +++-
 packages/open-next/src/core/util.ts           | 2 ++
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 0307ea81e..62fb43bc2 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -675,9 +675,9 @@ async function createServerBundle(
 
   const overrides = buildOptions.functions.default.override ?? {};
 
-  const disableRouting =
-    compareSemver(options.nextVersion, "13.4.13") <= 0 ||
-    options.externalMiddleware;
+  const isBefore13413 = compareSemver(options.nextVersion, "13.4.13") <= 0;
+
+  const disableRouting = isBefore13413 || options.externalMiddleware;
   const plugins = [
     openNextReplacementPlugin({
       name: "requestHandlerOverride",
@@ -697,6 +697,7 @@ async function createServerBundle(
       deletes: [
         ...(disableNextPrebundledReact ? ["requireHooks"] : []),
         ...(disableRouting ? ["trustHostHeader"] : []),
+        ...(!isBefore13413 ? ["requestHandlerHost"] : []),
       ],
     }),
 
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index ec3662411..1c2c7695b 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -5,7 +5,7 @@ import {
 } from "http/index.js";
 import { InternalEvent, InternalResult } from "types/open-next";
 
-import { error } from "../adapters/logger";
+import { debug, error } from "../adapters/logger";
 import { convertRes, createServerResponse, proxyRequest } from "./routing/util";
 import routingHandler from "./routingHandler";
 import { requestHandler, setNextjsPrebundledReact } from "./util";
@@ -17,6 +17,7 @@ export async function openNextHandler(
   if (internalEvent.headers["x-forwarded-host"]) {
     internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
   }
+  debug("internalEvent", internalEvent);
 
   //#override withRouting
   const preprocessResult = await routingHandler(internalEvent);
@@ -33,6 +34,7 @@ export async function openNextHandler(
     return preprocessResult;
   } else {
     const preprocessedEvent = preprocessResult.internalEvent;
+    debug("preprocessedEvent", preprocessedEvent);
     const reqProps = {
       method: preprocessedEvent.method,
       url: preprocessedEvent.url,
diff --git a/packages/open-next/src/core/util.ts b/packages/open-next/src/core/util.ts
index 6f800a3d9..ac851f010 100644
--- a/packages/open-next/src/core/util.ts
+++ b/packages/open-next/src/core/util.ts
@@ -30,8 +30,10 @@ applyNextjsRequireHooksOverride();
 
 // @ts-ignore
 export const requestHandler = new NextServer.default({
+  //#override requestHandlerHost
   hostname: "localhost",
   port: 3000,
+  //#endOverride
   conf: {
     ...NextConfig,
     // Next.js compression should be disabled because of a bug in the bundled

From 53b95d8d86bb59d3bf1a45f866d0c35a46835946 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 28 Nov 2023 11:52:21 +0100
Subject: [PATCH 023/102] refactor wrapper

---
 packages/open-next/src/build.ts                  |  1 -
 .../open-next/src/core/createGenericHandler.ts   |  6 ++++--
 packages/open-next/src/core/createMainHandler.ts |  5 ++++-
 packages/open-next/src/types/open-next.ts        | 16 ++++++++++------
 .../src/wrappers/aws-lambda-streaming.ts         | 10 +++++++---
 packages/open-next/src/wrappers/aws-lambda.ts    | 10 +++++++---
 packages/open-next/src/wrappers/cloudflare.ts    | 14 +++++++++++---
 packages/open-next/src/wrappers/node.ts          | 10 +++++++---
 8 files changed, 50 insertions(+), 22 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 62fb43bc2..9d78f9323 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -113,7 +113,6 @@ function normalizeOptions(opts: BuildOptions, root: string) {
       false,
     buildCommand: opts.buildCommand,
     dangerous: opts.dangerous,
-    streaming: opts.functions.default.streaming ?? false,
     externalMiddleware: opts.middleware?.external ?? false,
   };
 }
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
index aef5e67ef..fca40ddbe 100644
--- a/packages/open-next/src/core/createGenericHandler.ts
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -7,6 +7,7 @@ import type {
   OpenNextHandler,
 } from "types/open-next";
 
+import { debug } from "../adapters/logger";
 import { resolveConverter, resolveWrapper } from "./resolve";
 
 declare global {
@@ -35,7 +36,7 @@ export async function createGenericHandler<
   R extends BaseEventOrResult = InternalResult,
 >(handler: GenericHandler<Type, E, R>) {
   //First we load the config
-  //@ts-expect-error
+  // @ts-expect-error
   const config: BuildOptions = await import("./open-next.config.js").then(
     (m) => m.default,
   );
@@ -51,6 +52,7 @@ export async function createGenericHandler<
 
   // Then we create the handler
   const wrapper = await resolveWrapper<E, R>(override?.wrapper);
+  debug("Using wrapper", wrapper.name);
 
-  return wrapper(handler.handler, adapter);
+  return wrapper.wrapper(handler.handler, adapter);
 }
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 649ec8edf..ecb9cf520 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -1,5 +1,6 @@
 import type { BuildOptions, OverrideOptions } from "types/open-next";
 
+import { debug } from "../adapters/logger";
 import type { IncrementalCache } from "../cache/incremental/types";
 import type { Queue } from "../queue/types";
 import { openNextHandler } from "./requestHandler";
@@ -59,5 +60,7 @@ export async function createMainHandler() {
   // Then we create the handler
   const wrapper = await resolveWrapper(thisFunction.override?.wrapper);
 
-  return wrapper(openNextHandler, adapter);
+  debug("Using wrapper", wrapper.name);
+
+  return wrapper.wrapper(openNextHandler, adapter);
 }
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index e7ff73b1f..dd5451ccb 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -58,7 +58,7 @@ export type Converter<
   convertTo: (result: R) => any;
 };
 
-export type Wrapper<
+export type WrapperHandler<
   E extends BaseEventOrResult = InternalEvent,
   R extends BaseEventOrResult = InternalResult,
 > = (
@@ -66,6 +66,15 @@ export type Wrapper<
   converter: Converter<E, R>,
 ) => Promise<(...args: any[]) => any>;
 
+export type Wrapper<
+  E extends BaseEventOrResult = InternalEvent,
+  R extends BaseEventOrResult = InternalResult,
+> = {
+  wrapper: WrapperHandler<E, R>;
+  name: string;
+  supportStreaming: boolean;
+};
+
 type Warmer = (warmerId: string) => Promise<
   {
     statusCode: number;
@@ -172,11 +181,6 @@ interface FunctionOptions extends DefaultFunctionOptions {
    * @default []
    */
   routes?: string[];
-  /**
-   * Enable streaming mode.
-   * @default false
-   */
-  streaming?: boolean;
   /**
    * Enable overriding the default lambda.
    */
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index d14c195c0..3d60af0bb 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -3,7 +3,7 @@ import zlib from "node:zlib";
 
 import { APIGatewayProxyEventV2 } from "aws-lambda";
 import { StreamCreator } from "http/index.js";
-import { Wrapper } from "types/open-next";
+import { WrapperHandler } from "types/open-next";
 
 import { error } from "../adapters/logger";
 import { WarmerEvent } from "../adapters/warmer-function";
@@ -11,7 +11,7 @@ import { WarmerEvent } from "../adapters/warmer-function";
 type AwsLambdaEvent = APIGatewayProxyEventV2 | WarmerEvent;
 
 type AwsLambdaReturn = void;
-const handler: Wrapper = async (handler, converter) =>
+const handler: WrapperHandler = async (handler, converter) =>
   awslambda.streamifyResponse(
     async (event: AwsLambdaEvent, responseStream): Promise<AwsLambdaReturn> => {
       const internalEvent = await converter.convertFrom(event);
@@ -107,4 +107,8 @@ const handler: Wrapper = async (handler, converter) =>
     },
   );
 
-export default handler;
+export default {
+  wrapper: handler,
+  name: "aws-lambda-streaming",
+  supportStreaming: true,
+};
diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index ba0028529..7f894709a 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -6,7 +6,7 @@ import type {
   CloudFrontRequestEvent,
   CloudFrontRequestResult,
 } from "aws-lambda";
-import type { Wrapper } from "types/open-next";
+import type { WrapperHandler } from "types/open-next";
 
 import { WarmerEvent } from "../adapters/warmer-function";
 
@@ -21,7 +21,7 @@ type AwsLambdaReturn =
   | APIGatewayProxyResult
   | CloudFrontRequestResult;
 
-const handler: Wrapper =
+const handler: WrapperHandler =
   async (handler, converter) =>
   async (event: AwsLambdaEvent): Promise<AwsLambdaReturn> => {
     const internalEvent = await converter.convertFrom(event);
@@ -31,4 +31,8 @@ const handler: Wrapper =
     return converter.convertTo(response);
   };
 
-export default handler;
+export default {
+  wrapper: handler,
+  name: "aws-lambda",
+  supportStreaming: false,
+};
diff --git a/packages/open-next/src/wrappers/cloudflare.ts b/packages/open-next/src/wrappers/cloudflare.ts
index 7d5db285b..073f0bb90 100644
--- a/packages/open-next/src/wrappers/cloudflare.ts
+++ b/packages/open-next/src/wrappers/cloudflare.ts
@@ -1,8 +1,12 @@
-import type { InternalEvent, InternalResult, Wrapper } from "types/open-next";
+import type {
+  InternalEvent,
+  InternalResult,
+  WrapperHandler,
+} from "types/open-next";
 
 import { MiddlewareOutputEvent } from "../core/routingHandler";
 
-const handler: Wrapper<
+const handler: WrapperHandler<
   InternalEvent,
   InternalResult | ({ type: "middleware" } & MiddlewareOutputEvent)
 > =
@@ -17,4 +21,8 @@ const handler: Wrapper<
     return result;
   };
 
-export default handler;
+export default {
+  wrapper: handler,
+  name: "cloudflare",
+  supportStreaming: true,
+};
diff --git a/packages/open-next/src/wrappers/node.ts b/packages/open-next/src/wrappers/node.ts
index 12f6639a6..08f8f0aee 100644
--- a/packages/open-next/src/wrappers/node.ts
+++ b/packages/open-next/src/wrappers/node.ts
@@ -1,11 +1,11 @@
 import { createServer } from "node:http";
 
 import { StreamCreator } from "http/index.js";
-import type { Wrapper } from "types/open-next";
+import type { WrapperHandler } from "types/open-next";
 
 import { debug, error } from "../adapters/logger";
 
-const wrapper: Wrapper = async (handler, converter) => {
+const wrapper: WrapperHandler = async (handler, converter) => {
   const server = createServer(async (req, res) => {
     const internalEvent = await converter.convertFrom(req);
     const _res: StreamCreator = {
@@ -54,4 +54,8 @@ const wrapper: Wrapper = async (handler, converter) => {
   };
 };
 
-export default wrapper;
+export default {
+  wrapper,
+  name: "node",
+  supportStreaming: true,
+};

From 5b69d2f8bde7584aeaacc4d7c09e77e032cefbdc Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 28 Nov 2023 12:45:48 +0100
Subject: [PATCH 024/102] generate basic dockerfile

---
 packages/open-next/src/build.ts           | 29 ++++++++++++++++++++++-
 packages/open-next/src/core/util.ts       |  2 +-
 packages/open-next/src/plugins/resolve.ts |  1 -
 packages/open-next/src/types/open-next.ts |  6 +++++
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 9d78f9323..af1d4bb2f 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -620,6 +620,12 @@ async function createCacheAssets(
 /* Server Helper Functions */
 /***************************/
 
+function shouldGenerateDockerfile(
+  options: BuildOptions["functions"]["default"],
+) {
+  return options.override?.generateDockerfile ?? false;
+}
+
 async function createServerBundle(
   monorepoRoot: string,
   buildOptions: BuildOptions,
@@ -743,7 +749,28 @@ async function createServerBundle(
   addPublicFilesList(outputPath, packagePath);
   injectMiddlewareGeolocation(outputPath, packagePath);
   removeCachedPages(outputPath, packagePath);
-  addCacheHandler(outputPath, options.dangerous);
+  addCacheHandler(
+    path.join(outputPath, packagePath, ".next"),
+    options.dangerous,
+  );
+
+  const shouldGenerateDocker = shouldGenerateDockerfile(
+    buildOptions.functions.default,
+  );
+  if (shouldGenerateDocker) {
+    fs.writeFileSync(
+      path.join(outputPath, "Dockerfile"),
+      typeof shouldGenerateDocker === "string"
+        ? shouldGenerateDocker
+        : `
+FROM node:18-alpine
+WORKDIR /app
+COPY . /app
+EXPOSE 3000
+CMD ["node", "index.mjs"]
+    `,
+    );
+  }
 }
 
 async function createMiddleware(packagePath: string) {
diff --git a/packages/open-next/src/core/util.ts b/packages/open-next/src/core/util.ts
index ac851f010..7f7838812 100644
--- a/packages/open-next/src/core/util.ts
+++ b/packages/open-next/src/core/util.ts
@@ -50,7 +50,7 @@ export const requestHandler = new NextServer.default({
       //#endOverride
 
       //TODO: change env.LAMBDA_TASK_ROOT
-      incrementalCacheHandlerPath: `${process.env.LAMBDA_TASK_ROOT}/cache.cjs`,
+      incrementalCacheHandlerPath: `./cache.cjs`,
     },
   },
   customServer: false,
diff --git a/packages/open-next/src/plugins/resolve.ts b/packages/open-next/src/plugins/resolve.ts
index 54a751a06..3e8a07d6b 100644
--- a/packages/open-next/src/plugins/resolve.ts
+++ b/packages/open-next/src/plugins/resolve.ts
@@ -37,7 +37,6 @@ export function openNextResolvePlugin({ overrides }: IPluginSettings): Plugin {
           );
         }
         if (overrides?.converter) {
-          console.log("converter", overrides.converter);
           contents = contents.replace(
             "../converters/aws-apigw-v2.js",
             `../converters/${overrides.converter}.js`,
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index dd5451ccb..c1e3b0600 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -127,6 +127,12 @@ export interface DefaultOverrideOptions<
    * @default "aws-apigw-v2"
    */
   converter?: IncludedConverter | LazyLoadedOverride<Converter<E, R>>;
+  /**
+   * Generate a basic dockerfile to deploy the app.
+   * If a string is provided, it will be used as the base dockerfile.
+   * @default false
+   */
+  generateDockerfile?: boolean | string;
 }
 
 export interface OverrideOptions extends DefaultOverrideOptions {

From a3dcb26adfc4813bae9a1302a1f1bf04992ff1c8 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 28 Nov 2023 13:18:28 +0100
Subject: [PATCH 025/102] Only build open-next config once

---
 packages/open-next/src/build.ts | 24 +++++++++++++++---------
 packages/open-next/src/index.ts | 20 +-------------------
 2 files changed, 16 insertions(+), 28 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index af1d4bb2f..5aebbade2 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -25,13 +25,15 @@ export type PublicFiles = {
   files: string[];
 };
 
-export async function build(
-  opts: BuildOptions = {
-    functions: {
-      default: {},
-    },
-  },
-) {
+export async function build() {
+  const outputTmpPath = path.join(process.cwd(), ".open-next", ".build");
+
+  // Compile open-next.config.ts
+  createOpenNextConfigBundle(outputTmpPath);
+
+  const config = await import(outputTmpPath + "/open-next.config.js");
+  const opts = config.default as BuildOptions;
+
   const { root: monorepoRoot, packager } = findMonorepoRoot(
     path.join(process.cwd(), opts.appPath || "."),
   );
@@ -53,7 +55,6 @@ export async function build(
   // Generate deployable bundle
   printHeader("Generating bundle");
   initOutputDir();
-  createOpenNextConfigBundle(options.tempDir);
   createStaticAssets();
   if (!options.dangerous?.disableIncrementalCache) {
     await createCacheAssets(
@@ -71,7 +72,7 @@ export async function build(
 }
 
 function createOpenNextConfigBundle(tempDir: string) {
-  esbuildSync({
+  buildSync({
     entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
     outfile: path.join(tempDir, "open-next.config.js"),
     bundle: true,
@@ -215,8 +216,13 @@ function printOpenNextVersion() {
 
 function initOutputDir() {
   const { outputDir, tempDir } = options;
+  const openNextConfig = readFileSync(
+    path.join(tempDir, "open-next.config.js"),
+    "utf8",
+  );
   fs.rmSync(outputDir, { recursive: true, force: true });
   fs.mkdirSync(tempDir, { recursive: true });
+  fs.writeFileSync(path.join(tempDir, "open-next.config.js"), openNextConfig);
 }
 
 async function createWarmerBundle() {
diff --git a/packages/open-next/src/index.ts b/packages/open-next/src/index.ts
index 9fb8f375f..d94eab711 100755
--- a/packages/open-next/src/index.ts
+++ b/packages/open-next/src/index.ts
@@ -1,8 +1,5 @@
 #!/usr/bin/env node
 
-import * as esbuild from "esbuild";
-import path from "path";
-
 import { build } from "./build.js";
 
 const command = process.argv[2];
@@ -11,22 +8,7 @@ if (command !== "build") printHelp();
 const args = parseArgs();
 if (Object.keys(args).includes("--help")) printHelp();
 
-//TODO: validate config file
-
-const outputTmpPath = path.join(process.cwd(), ".open-next", ".build");
-
-// Compile open-next.config.ts
-esbuild.buildSync({
-  entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
-  outfile: path.join(outputTmpPath, "open-next.config.js"),
-  bundle: true,
-  format: "cjs",
-  target: ["node18"],
-});
-
-const config = await import(outputTmpPath + "/open-next.config.js");
-
-build(config.default);
+build();
 
 function parseArgs() {
   return process.argv.slice(2).reduce(

From a50cdf7910bcd3e3dbae1b6f0cd34a5a8d692587 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 29 Nov 2023 14:04:28 +0100
Subject: [PATCH 026/102] generate basic output file for IAC to use

---
 packages/open-next/src/build.ts               | 522 +++++++-----------
 .../open-next/src/build/generateOutput.ts     | 233 ++++++++
 packages/open-next/src/build/helper.ts        | 188 +++++++
 .../open-next/src/core/createMainHandler.ts   |   5 +-
 packages/open-next/src/types/open-next.ts     |  30 +-
 5 files changed, 638 insertions(+), 340 deletions(-)
 create mode 100644 packages/open-next/src/build/generateOutput.ts
 create mode 100644 packages/open-next/src/build/helper.ts

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 5aebbade2..e802f15d4 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -4,18 +4,30 @@ import { createRequire as topLevelCreateRequire } from "node:module";
 import path from "node:path";
 import url from "node:url";
 
-import {
-  build as buildAsync,
-  BuildOptions as ESBuildOptions,
-  buildSync,
-} from "esbuild";
+import { buildSync } from "esbuild";
 
+import { generateOutput } from "./build/generateOutput.js";
+import {
+  compareSemver,
+  esbuildAsync,
+  esbuildSync,
+  getBuildId,
+  getHtmlPages,
+  getNextVersion,
+  getOpenNextVersion,
+  removeFiles,
+  traverseFiles,
+} from "./build/helper.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import { openNextEdgePlugins } from "./plugins/edge.js";
 import { openNextReplacementPlugin } from "./plugins/replacement.js";
 import { openNextResolvePlugin } from "./plugins/resolve.js";
-import { BuildOptions, DangerousOptions } from "./types/open-next.js";
+import {
+  BuildOptions,
+  DangerousOptions,
+  FunctionOptions,
+} from "./types/open-next.js";
 
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
@@ -66,6 +78,7 @@ export async function build() {
   await createRevalidationBundle();
   createImageOptimizationBundle();
   await createWarmerBundle();
+  await generateOutput(options.appBuildOutputPath, opts);
   if (options.minify) {
     await minifyServerBundle();
   }
@@ -105,13 +118,8 @@ function normalizeOptions(opts: BuildOptions, root: string) {
     outputDir,
     tempDir: path.join(outputDir, ".build"),
     minify:
-      opts.functions.default.minify ??
-      Boolean(process.env.OPEN_NEXT_MINIFY) ??
-      false,
-    debug:
-      opts.functions.default.debug ??
-      Boolean(process.env.OPEN_NEXT_DEBUG) ??
-      false,
+      opts.default.minify ?? Boolean(process.env.OPEN_NEXT_MINIFY) ?? false,
+    debug: opts.default.debug ?? Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
     buildCommand: opts.buildCommand,
     dangerous: opts.dangerous,
     externalMiddleware: opts.middleware?.external ?? false,
@@ -244,26 +252,29 @@ async function createWarmerBundle() {
   // note: bundle in OpenNext package b/c the adatper relys on the
   //       "serverless-http" package which is not a dependency in user's
   //       Next.js app.
-  await esbuildAsync({
-    entryPoints: [path.join(__dirname, "adapters", "warmer-function.js")],
-    external: ["next"],
-    outfile: path.join(outputPath, "index.mjs"),
-    plugins: [
-      openNextResolvePlugin({
-        overrides: {
-          converter: "dummy",
-        },
-      }),
-    ],
-    banner: {
-      js: [
-        "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(""),
+  await esbuildAsync(
+    {
+      entryPoints: [path.join(__dirname, "adapters", "warmer-function.js")],
+      external: ["next"],
+      outfile: path.join(outputPath, "index.mjs"),
+      plugins: [
+        openNextResolvePlugin({
+          overrides: {
+            converter: "dummy",
+          },
+        }),
+      ],
+      banner: {
+        js: [
+          "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,
+  );
 }
 
 async function minifyServerBundle() {
@@ -291,18 +302,21 @@ async function createRevalidationBundle() {
   );
 
   // Build Lambda code
-  esbuildAsync({
-    external: ["next", "styled-jsx", "react"],
-    entryPoints: [path.join(__dirname, "adapters", "revalidate.js")],
-    outfile: path.join(outputPath, "index.mjs"),
-    plugins: [
-      openNextResolvePlugin({
-        overrides: {
-          converter: "sqs-revalidate",
-        },
-      }),
-    ],
-  });
+  esbuildAsync(
+    {
+      external: ["next", "styled-jsx", "react"],
+      entryPoints: [path.join(__dirname, "adapters", "revalidate.js")],
+      outfile: path.join(outputPath, "index.mjs"),
+      plugins: [
+        openNextResolvePlugin({
+          overrides: {
+            converter: "sqs-revalidate",
+          },
+        }),
+      ],
+    },
+    options,
+  );
 
   // Copy over .next/prerender-manifest.json file
   fs.copyFileSync(
@@ -330,32 +344,38 @@ function createImageOptimizationBundle() {
   // note: bundle in OpenNext package b/c the adapter relies on the
   //       "@aws-sdk/client-s3" package which is not a dependency in user's
   //       Next.js app.
-  esbuildSync({
-    entryPoints: [
-      path.join(__dirname, "adapters", "image-optimization-adapter.js"),
-    ],
-    external: ["sharp", "next"],
-    outfile: path.join(outputPath, "index.mjs"),
-  });
+  esbuildSync(
+    {
+      entryPoints: [
+        path.join(__dirname, "adapters", "image-optimization-adapter.js"),
+      ],
+      external: ["sharp", "next"],
+      outfile: path.join(outputPath, "index.mjs"),
+    },
+    options,
+  );
 
   // Build Lambda code (2nd pass)
   // note: bundle in user's Next.js app again b/c the adapter relies on the
   //       "next" package. And the "next" package from user's app should
   //       be used.
-  esbuildSync({
-    entryPoints: [path.join(outputPath, "index.mjs")],
-    external: ["sharp"],
-    allowOverwrite: true,
-    outfile: path.join(outputPath, "index.mjs"),
-    banner: {
-      js: [
-        "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("\n"),
+  esbuildSync(
+    {
+      entryPoints: [path.join(outputPath, "index.mjs")],
+      external: ["sharp"],
+      allowOverwrite: true,
+      outfile: path.join(outputPath, "index.mjs"),
+      banner: {
+        js: [
+          "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("\n"),
+      },
     },
-  });
+    options,
+  );
 
   // Copy over .next/required-server-files.json file
   fs.mkdirSync(path.join(outputPath, ".next"));
@@ -590,19 +610,22 @@ async function createCacheAssets(
     if (metaFiles.length > 0) {
       const providerPath = path.join(outputDir, "dynamodb-provider");
 
-      await esbuildAsync({
-        external: ["@aws-sdk/client-dynamodb"],
-        entryPoints: [path.join(__dirname, "adapters", "dynamo-provider.js")],
-        outfile: path.join(providerPath, "index.mjs"),
-        target: ["node18"],
-        plugins: [
-          openNextResolvePlugin({
-            overrides: {
-              converter: "dummy",
-            },
-          }),
-        ],
-      });
+      await esbuildAsync(
+        {
+          external: ["@aws-sdk/client-dynamodb"],
+          entryPoints: [path.join(__dirname, "adapters", "dynamo-provider.js")],
+          outfile: path.join(providerPath, "index.mjs"),
+          target: ["node18"],
+          plugins: [
+            openNextResolvePlugin({
+              overrides: {
+                converter: "dummy",
+              },
+            }),
+          ],
+        },
+        options,
+      );
 
       //Copy open-next.config.js into the bundle
       fs.copyFileSync(
@@ -626,9 +649,7 @@ async function createCacheAssets(
 /* Server Helper Functions */
 /***************************/
 
-function shouldGenerateDockerfile(
-  options: BuildOptions["functions"]["default"],
-) {
+function shouldGenerateDockerfile(options: FunctionOptions) {
   return options.override?.generateDockerfile ?? false;
 }
 
@@ -684,7 +705,7 @@ async function createServerBundle(
     compareSemver(options.nextVersion, "13.5.1") >= 0 ||
     compareSemver(options.nextVersion, "13.4.1") <= 0;
 
-  const overrides = buildOptions.functions.default.override ?? {};
+  const overrides = buildOptions.default.override ?? {};
 
   const isBefore13413 = compareSemver(options.nextVersion, "13.4.13") <= 0;
 
@@ -733,21 +754,24 @@ async function createServerBundle(
         .join(",")}] for Next version: ${options.nextVersion}`,
     );
   }
-  await esbuildAsync({
-    entryPoints: [path.join(__dirname, "adapters", "server-adapter.js")],
-    external: ["next", "./middleware.mjs"],
-    outfile: path.join(outputPath, packagePath, "index.mjs"),
-    banner: {
-      js: [
-        `globalThis.monorepoPackagePath = "${packagePath}";`,
-        "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(""),
+  await esbuildAsync(
+    {
+      entryPoints: [path.join(__dirname, "adapters", "server-adapter.js")],
+      external: ["next", "./middleware.mjs"],
+      outfile: path.join(outputPath, packagePath, "index.mjs"),
+      banner: {
+        js: [
+          `globalThis.monorepoPackagePath = "${packagePath}";`,
+          "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(""),
+      },
+      plugins,
     },
-    plugins,
-  });
+    options,
+  );
 
   if (isMonorepo) {
     addMonorepoEntrypoint(outputPath, packagePath);
@@ -760,9 +784,7 @@ async function createServerBundle(
     options.dangerous,
   );
 
-  const shouldGenerateDocker = shouldGenerateDockerfile(
-    buildOptions.functions.default,
-  );
+  const shouldGenerateDocker = shouldGenerateDockerfile(buildOptions.default);
   if (shouldGenerateDocker) {
     fs.writeFileSync(
       path.join(outputPath, "Dockerfile"),
@@ -810,41 +832,44 @@ async function createMiddleware(packagePath: string) {
     );
 
     // Bundle middleware
-    await esbuildAsync({
-      entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
-      // inject: ,
-      bundle: true,
-      outfile: path.join(outputPath, packagePath, "handler.mjs"),
-      external: ["node:*", "next", "@aws-sdk/*"],
-      target: "es2022",
-      platform: "neutral",
-      plugins: [
-        openNextResolvePlugin({
-          overrides: {
-            wrapper: "cloudflare",
-            converter: "edge",
-          },
-        }),
-        openNextEdgePlugins({
-          entryFiles: entry.files.map((file: string) =>
-            path.join(appBuildOutputPath, ".next", file),
-          ),
-          nextDir: path.join(appBuildOutputPath, ".next"),
-          edgeFunctionHandlerPath: path.join(
-            __dirname,
-            "core",
-            "edgeFunctionHandler.js",
-          ),
-        }),
-      ],
-      treeShaking: true,
-      alias: {
-        path: "node:path",
-        stream: "node:stream",
+    await esbuildAsync(
+      {
+        entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
+        // inject: ,
+        bundle: true,
+        outfile: path.join(outputPath, packagePath, "handler.mjs"),
+        external: ["node:*", "next", "@aws-sdk/*"],
+        target: "es2022",
+        platform: "neutral",
+        plugins: [
+          openNextResolvePlugin({
+            overrides: {
+              wrapper: "cloudflare",
+              converter: "edge",
+            },
+          }),
+          openNextEdgePlugins({
+            entryFiles: entry.files.map((file: string) =>
+              path.join(appBuildOutputPath, ".next", file),
+            ),
+            nextDir: path.join(appBuildOutputPath, ".next"),
+            edgeFunctionHandlerPath: path.join(
+              __dirname,
+              "core",
+              "edgeFunctionHandler.js",
+            ),
+          }),
+        ],
+        treeShaking: true,
+        alias: {
+          path: "node:path",
+          stream: "node:stream",
+        },
+        conditions: ["module"],
+        mainFields: ["module", "main"],
       },
-      conditions: ["module"],
-      mainFields: ["module", "main"],
-    });
+      options,
+    );
   } else {
     buildEdgeFunction(
       entry,
@@ -861,18 +886,19 @@ function buildEdgeFunction(
   outfile: string,
   appBuildOutputPath: string,
 ) {
-  esbuildSync({
-    entryPoints: [entrypoint],
-    inject: entry.files.map((file: string) =>
-      path.join(appBuildOutputPath, ".next", file),
-    ),
-    bundle: true,
-    outfile,
-    external: ["node:*"],
-    target: "es2022",
-    platform: "neutral",
-    banner: {
-      js: `
+  esbuildSync(
+    {
+      entryPoints: [entrypoint],
+      inject: entry.files.map((file: string) =>
+        path.join(appBuildOutputPath, ".next", file),
+      ),
+      bundle: true,
+      outfile,
+      external: ["node:*"],
+      target: "es2022",
+      platform: "neutral",
+      banner: {
+        js: `
 globalThis._ENTRIES = {};
 globalThis.self = globalThis;
 
@@ -883,8 +909,10 @@ import {AsyncLocalStorage} from "node:async_hooks";
 globalThis.AsyncLocalStorage = AsyncLocalStorage;
 
 `,
+      },
     },
-  });
+    options,
+  );
 }
 
 function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
@@ -992,191 +1020,25 @@ function removeCachedPages(outputPath: string, packagePath: string) {
     );
 }
 
-function addCacheHandler(outputPath: string, options?: DangerousOptions) {
-  esbuildSync({
-    external: ["next", "styled-jsx", "react"],
-    entryPoints: [path.join(__dirname, "adapters", "cache.js")],
-    outfile: path.join(outputPath, "cache.cjs"),
-    target: ["node18"],
-    format: "cjs",
-    banner: {
-      js: [
-        `globalThis.disableIncrementalCache = ${
-          options?.disableIncrementalCache ?? false
-        };`,
-        `globalThis.disableDynamoDBCache = ${
-          options?.disableDynamoDBCache ?? false
-        };`,
-      ].join(""),
-    },
-  });
-}
-
-/********************/
-/* Helper Functions */
-/********************/
-
-function esbuildSync(esbuildOptions: ESBuildOptions) {
-  const { openNextVersion, debug } = options;
-  const result = buildSync({
-    target: "esnext",
-    format: "esm",
-    platform: "node",
-    bundle: true,
-    minify: debug ? false : true,
-    sourcemap: debug ? "inline" : false,
-    ...esbuildOptions,
-    external: ["./open-next.config.js", ...(esbuildOptions.external ?? [])],
-    banner: {
-      ...esbuildOptions.banner,
-      js: [
-        esbuildOptions.banner?.js || "",
-        `globalThis.openNextDebug = ${debug};`,
-        `globalThis.openNextVersion = "${openNextVersion}";`,
-      ].join(""),
-    },
-  });
-
-  if (result.errors.length > 0) {
-    result.errors.forEach((error) => logger.error(error));
-    throw new Error(
-      `There was a problem bundling ${
-        (esbuildOptions.entryPoints as string[])[0]
-      }.`,
-    );
-  }
-}
-
-async function esbuildAsync(esbuildOptions: ESBuildOptions) {
-  const { openNextVersion, debug } = options;
-  const result = await buildAsync({
-    target: "esnext",
-    format: "esm",
-    platform: "node",
-    bundle: true,
-    minify: debug ? false : true,
-    sourcemap: debug ? "inline" : false,
-    ...esbuildOptions,
-    external: [
-      ...(esbuildOptions.external ?? []),
-      "next",
-      "./open-next.config.js",
-    ],
-    banner: {
-      ...esbuildOptions.banner,
-      js: [
-        esbuildOptions.banner?.js || "",
-        `globalThis.openNextDebug = ${debug};`,
-        `globalThis.openNextVersion = "${openNextVersion}";`,
-      ].join(""),
+function addCacheHandler(outputPath: string, opts?: DangerousOptions) {
+  esbuildSync(
+    {
+      external: ["next", "styled-jsx", "react"],
+      entryPoints: [path.join(__dirname, "adapters", "cache.js")],
+      outfile: path.join(outputPath, "cache.cjs"),
+      target: ["node18"],
+      format: "cjs",
+      banner: {
+        js: [
+          `globalThis.disableIncrementalCache = ${
+            opts?.disableIncrementalCache ?? false
+          };`,
+          `globalThis.disableDynamoDBCache = ${
+            opts?.disableDynamoDBCache ?? false
+          };`,
+        ].join(""),
+      },
     },
-  });
-
-  if (result.errors.length > 0) {
-    result.errors.forEach((error) => logger.error(error));
-    throw new Error(
-      `There was a problem bundling ${
-        (esbuildOptions.entryPoints as string[])[0]
-      }.`,
-    );
-  }
-}
-
-function removeFiles(
-  root: string,
-  conditionFn: (file: string) => boolean,
-  searchingDir: string = "",
-) {
-  traverseFiles(
-    root,
-    conditionFn,
-    (filePath) => fs.rmSync(filePath, { force: true }),
-    searchingDir,
-  );
-}
-
-function traverseFiles(
-  root: string,
-  conditionFn: (file: string) => boolean,
-  callbackFn: (filePath: string) => void,
-  searchingDir: string = "",
-) {
-  fs.readdirSync(path.join(root, searchingDir)).forEach((file) => {
-    const filePath = path.join(root, searchingDir, file);
-
-    if (fs.statSync(filePath).isDirectory()) {
-      traverseFiles(
-        root,
-        conditionFn,
-        callbackFn,
-        path.join(searchingDir, file),
-      );
-      return;
-    }
-
-    if (conditionFn(path.join(searchingDir, file))) {
-      callbackFn(filePath);
-    }
-  });
-}
-
-function getHtmlPages(dotNextPath: string) {
-  // Get a list of HTML pages
-  //
-  // sample return value:
-  // Set([
-  //   '404.html',
-  //   'csr.html',
-  //   'image-html-tag.html',
-  // ])
-  const manifestPath = path.join(
-    dotNextPath,
-    ".next/server/pages-manifest.json",
+    options,
   );
-  const manifest = fs.readFileSync(manifestPath, "utf-8");
-  return Object.entries(JSON.parse(manifest))
-    .filter(([_, value]) => (value as string).endsWith(".html"))
-    .map(([_, value]) => (value as string).replace(/^pages\//, ""))
-    .reduce((acc, page) => {
-      acc.add(page);
-      return acc;
-    }, new Set<string>());
-}
-
-function getBuildId(dotNextPath: string) {
-  return fs
-    .readFileSync(path.join(dotNextPath, ".next/BUILD_ID"), "utf-8")
-    .trim();
-}
-
-function getOpenNextVersion() {
-  return require(path.join(__dirname, "../package.json")).version;
-}
-
-function getNextVersion(nextPackageJsonPath: string) {
-  const version = require(nextPackageJsonPath)?.dependencies?.next;
-  // require('next/package.json').version
-
-  if (!version) {
-    throw new Error("Failed to find Next version");
-  }
-
-  // Drop the -canary.n suffix
-  return version.split("-")[0];
-}
-
-function compareSemver(v1: string, v2: string): number {
-  if (v1 === "latest") return 1;
-  if (/^[^\d]/.test(v1)) {
-    v1 = v1.substring(1);
-  }
-  if (/^[^\d]/.test(v2)) {
-    v2 = v2.substring(1);
-  }
-  const [major1, minor1, patch1] = v1.split(".").map(Number);
-  const [major2, minor2, patch2] = v2.split(".").map(Number);
-
-  if (major1 !== major2) return major1 - major2;
-  if (minor1 !== minor2) return minor1 - minor2;
-  return patch1 - patch2;
 }
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
new file mode 100644
index 000000000..7e3a58ea5
--- /dev/null
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -0,0 +1,233 @@
+import * as fs from "node:fs";
+import path from "node:path";
+
+import { BuildOptions, FunctionOptions } from "types/open-next";
+
+type BaseFunction = {
+  handler: string;
+  bundle: string;
+};
+
+type OpenNextOrigins =
+  | ({
+      type: "function";
+      streaming?: boolean;
+    } & BaseFunction)
+  | {
+      type: "ecs";
+      bundle: string;
+      dockerfile: string;
+    }
+  | {
+      type: "s3";
+      originPath: string;
+      copy: {
+        from: string;
+        to: string;
+        cached: boolean;
+        versionedSubDir?: string;
+      }[];
+    };
+
+interface OpenNextOutput {
+  edgeFunctions: {
+    [key: string]: BaseFunction;
+  };
+  origins: {
+    [key: string]: OpenNextOrigins;
+  };
+  behaviors: {
+    pattern: string;
+    origin?: string;
+    edgeFunction?: string;
+  }[];
+  additionalProps?: {
+    disableIncrementalCache?: boolean;
+    disableTagCache?: boolean;
+    initializationFunction?: BaseFunction;
+    warmer?: BaseFunction;
+    revalidationFunction?: BaseFunction;
+  };
+}
+
+async function canStream(opts: FunctionOptions) {
+  if (!opts.override?.wrapper) {
+    console.log("Why not here?"), opts.override;
+    return false;
+  } else {
+    if (typeof opts.override.wrapper === "string") {
+      return opts.override.wrapper === "aws-lambda-streaming";
+    } else {
+      const wrapper = await opts.override.wrapper();
+      return wrapper.supportStreaming;
+    }
+  }
+}
+
+export async function generateOutput(
+  outputPath: string,
+  buildOptions: BuildOptions,
+) {
+  const edgeFunctions: OpenNextOutput["edgeFunctions"] = {};
+  const isExternalMiddleware = buildOptions.middleware?.external ?? false;
+  if (isExternalMiddleware) {
+    edgeFunctions.middleware = {
+      bundle: ".open-next/middleware",
+      handler: "index.handler",
+    };
+  }
+  // Add edge functions
+  Object.entries(buildOptions.functions).forEach(([key, value]) => {
+    if (value.runtime === "edge") {
+      edgeFunctions[key] = {
+        bundle: `.open-next/functions/${key}`,
+        handler: "index.handler",
+      };
+    }
+  });
+
+  // First add s3 origins and image optimization
+  const origins: OpenNextOutput["origins"] = {
+    s3: {
+      type: "s3",
+      originPath: "_assets",
+      copy: [
+        {
+          from: ".open-next/assets",
+          to: "_assets",
+          cached: true,
+          versionedSubDir: "_next",
+        },
+        ...(buildOptions.dangerous?.disableIncrementalCache
+          ? []
+          : [
+              {
+                from: ".open-next/cache",
+                to: "cache",
+                cached: false,
+              },
+            ]),
+      ],
+    },
+    imageOptimizer: {
+      type: "function",
+      handler: "index.handler",
+      bundle: ".open-next/image-optimization-function",
+      streaming: false,
+    },
+  };
+
+  const defaultOriginCanstream = await canStream(buildOptions.default);
+  origins.default = buildOptions.default.override?.generateDockerfile
+    ? {
+        type: "ecs",
+        bundle: ".open-next/default-function",
+        dockerfile: ".open-next/default-function/Dockerfile",
+      }
+    : {
+        type: "function",
+        handler: "index.handler",
+        bundle: ".open-next/default-function",
+        streaming: defaultOriginCanstream,
+      };
+
+  // Then add function origins
+  Promise.all(
+    Object.entries(buildOptions.functions).map(async ([key, value]) => {
+      if (!value.runtime || value.runtime === "node") {
+        const streaming = await canStream(value);
+        origins[key] = {
+          type: "function",
+          handler: "index.handler",
+          bundle: `.open-next/functions/${key}`,
+          streaming,
+        };
+      }
+    }),
+  );
+
+  // Then add ecs origins
+  Object.entries(buildOptions.functions).forEach(([key, value]) => {
+    if (value.override?.generateDockerfile) {
+      origins[key] = {
+        type: "ecs",
+        bundle: `.open-next/functions/${key}`,
+        dockerfile: `.open-next/functions/${key}/Dockerfile`,
+      };
+    }
+  });
+
+  // Then we need to compute the behaviors
+  const behaviors: OpenNextOutput["behaviors"] = [
+    { pattern: "_next/image*", origin: "imageOptimizer" },
+    {
+      pattern: "*",
+      origin: "default",
+      edgeFunction: isExternalMiddleware ? "middleware" : undefined,
+    },
+    //TODO: add base files
+  ];
+
+  //Compute behaviors for assets files
+  const assetPath = path.join(outputPath, ".open-next", "assets");
+  fs.readdirSync(assetPath).forEach((item) => {
+    if (fs.statSync(path.join(assetPath, item)).isDirectory()) {
+      behaviors.push({
+        pattern: `${item}/*`,
+        origin: "s3",
+      });
+    } else {
+      behaviors.push({
+        pattern: item,
+        origin: "s3",
+      });
+    }
+  });
+
+  // Then we add the routes
+  Object.entries(buildOptions.functions).forEach(([key, value]) => {
+    const patterns = "patterns" in value ? value.patterns : ["*"];
+    patterns.forEach((pattern) => {
+      behaviors.push({
+        pattern,
+        origin: value.placement === "global" ? undefined : key,
+        edgeFunction:
+          value.placement === "global"
+            ? key
+            : isExternalMiddleware
+            ? "middleware"
+            : undefined,
+      });
+    });
+  });
+
+  const output: OpenNextOutput = {
+    edgeFunctions,
+    origins,
+    behaviors,
+    additionalProps: {
+      disableIncrementalCache: buildOptions.dangerous?.disableIncrementalCache,
+      disableTagCache: buildOptions.dangerous?.disableDynamoDBCache,
+      warmer: {
+        handler: "index.handler",
+        bundle: ".open-next/warmer-function",
+      },
+      initializationFunction: buildOptions.dangerous?.disableDynamoDBCache
+        ? undefined
+        : {
+            handler: "index.handler",
+            bundle: ".open-next/initialization-function",
+          },
+      revalidationFunction: buildOptions.dangerous?.disableIncrementalCache
+        ? undefined
+        : {
+            handler: "index.handler",
+            bundle: ".open-next/revalidation-function",
+          },
+    },
+  };
+  fs.writeFileSync(
+    path.join(outputPath, ".open-next", "open-next.output.json"),
+    JSON.stringify(output),
+  );
+}
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
new file mode 100644
index 000000000..144f90750
--- /dev/null
+++ b/packages/open-next/src/build/helper.ts
@@ -0,0 +1,188 @@
+import fs from "node:fs";
+import { createRequire as topLevelCreateRequire } from "node:module";
+import path from "node:path";
+import url from "node:url";
+
+import {
+  build as buildAsync,
+  BuildOptions as ESBuildOptions,
+  buildSync,
+} from "esbuild";
+
+import logger from "../logger.js";
+
+const require = topLevelCreateRequire(import.meta.url);
+const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
+
+interface Options {
+  openNextVersion: string;
+  debug: boolean;
+}
+
+export function esbuildSync(esbuildOptions: ESBuildOptions, options: Options) {
+  const { openNextVersion, debug } = options;
+  const result = buildSync({
+    target: "esnext",
+    format: "esm",
+    platform: "node",
+    bundle: true,
+    minify: debug ? false : true,
+    sourcemap: debug ? "inline" : false,
+    ...esbuildOptions,
+    external: ["./open-next.config.js", ...(esbuildOptions.external ?? [])],
+    banner: {
+      ...esbuildOptions.banner,
+      js: [
+        esbuildOptions.banner?.js || "",
+        `globalThis.openNextDebug = ${debug};`,
+        `globalThis.openNextVersion = "${openNextVersion}";`,
+      ].join(""),
+    },
+  });
+
+  if (result.errors.length > 0) {
+    result.errors.forEach((error) => logger.error(error));
+    throw new Error(
+      `There was a problem bundling ${
+        (esbuildOptions.entryPoints as string[])[0]
+      }.`,
+    );
+  }
+}
+
+export async function esbuildAsync(
+  esbuildOptions: ESBuildOptions,
+  options: Options,
+) {
+  const { openNextVersion, debug } = options;
+  const result = await buildAsync({
+    target: "esnext",
+    format: "esm",
+    platform: "node",
+    bundle: true,
+    minify: debug ? false : true,
+    sourcemap: debug ? "inline" : false,
+    ...esbuildOptions,
+    external: [
+      ...(esbuildOptions.external ?? []),
+      "next",
+      "./open-next.config.js",
+    ],
+    banner: {
+      ...esbuildOptions.banner,
+      js: [
+        esbuildOptions.banner?.js || "",
+        `globalThis.openNextDebug = ${debug};`,
+        `globalThis.openNextVersion = "${openNextVersion}";`,
+      ].join(""),
+    },
+  });
+
+  if (result.errors.length > 0) {
+    result.errors.forEach((error) => logger.error(error));
+    throw new Error(
+      `There was a problem bundling ${
+        (esbuildOptions.entryPoints as string[])[0]
+      }.`,
+    );
+  }
+}
+
+export function removeFiles(
+  root: string,
+  conditionFn: (file: string) => boolean,
+  searchingDir: string = "",
+) {
+  traverseFiles(
+    root,
+    conditionFn,
+    (filePath) => fs.rmSync(filePath, { force: true }),
+    searchingDir,
+  );
+}
+
+export function traverseFiles(
+  root: string,
+  conditionFn: (file: string) => boolean,
+  callbackFn: (filePath: string) => void,
+  searchingDir: string = "",
+) {
+  fs.readdirSync(path.join(root, searchingDir)).forEach((file) => {
+    const filePath = path.join(root, searchingDir, file);
+
+    if (fs.statSync(filePath).isDirectory()) {
+      traverseFiles(
+        root,
+        conditionFn,
+        callbackFn,
+        path.join(searchingDir, file),
+      );
+      return;
+    }
+
+    if (conditionFn(path.join(searchingDir, file))) {
+      callbackFn(filePath);
+    }
+  });
+}
+
+export function getHtmlPages(dotNextPath: string) {
+  // Get a list of HTML pages
+  //
+  // sample return value:
+  // Set([
+  //   '404.html',
+  //   'csr.html',
+  //   'image-html-tag.html',
+  // ])
+  const manifestPath = path.join(
+    dotNextPath,
+    ".next/server/pages-manifest.json",
+  );
+  const manifest = fs.readFileSync(manifestPath, "utf-8");
+  return Object.entries(JSON.parse(manifest))
+    .filter(([_, value]) => (value as string).endsWith(".html"))
+    .map(([_, value]) => (value as string).replace(/^pages\//, ""))
+    .reduce((acc, page) => {
+      acc.add(page);
+      return acc;
+    }, new Set<string>());
+}
+
+export function getBuildId(dotNextPath: string) {
+  return fs
+    .readFileSync(path.join(dotNextPath, ".next/BUILD_ID"), "utf-8")
+    .trim();
+}
+
+export function getOpenNextVersion() {
+  return require(path.join(__dirname, "../../package.json")).version;
+}
+
+export function getNextVersion(nextPackageJsonPath: string) {
+  const version = require(nextPackageJsonPath)?.dependencies?.next;
+  // require('next/package.json').version
+
+  if (!version) {
+    throw new Error("Failed to find Next version");
+  }
+
+  // Drop the -canary.n suffix
+  return version.split("-")[0];
+}
+
+export function compareSemver(v1: string, v2: string): number {
+  if (v1 === "latest") return 1;
+  if (/^[^\d]/.test(v1)) {
+    v1 = v1.substring(1);
+  }
+  if (/^[^\d]/.test(v2)) {
+    v2 = v2.substring(1);
+  }
+  const [major1, minor1, patch1] = v1.split(".").map(Number);
+  const [major2, minor2, patch2] = v2.split(".").map(Number);
+
+  if (major1 !== major2) return major1 - major2;
+  if (minor1 !== minor2) return minor1 - minor2;
+  return patch1 - patch2;
+}
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index ecb9cf520..1bd00e704 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -9,6 +9,7 @@ import { resolveConverter, resolveTagCache, resolveWrapper } from "./resolve";
 declare global {
   var queue: Queue;
   var incrementalCache: IncrementalCache;
+  var fnName: string | undefined;
 }
 
 async function resolveQueue(queue: OverrideOptions["queue"]) {
@@ -43,7 +44,9 @@ export async function createMainHandler() {
     process.cwd() + "/open-next.config.js"
   ).then((m) => m.default);
 
-  const thisFunction = config.functions.default;
+  const thisFunction = globalThis.fnName
+    ? config.functions[globalThis.fnName]
+    : config.default;
 
   // Default queue
   globalThis.queue = await resolveQueue(thisFunction.override?.queue);
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index c1e3b0600..e537418d9 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -175,29 +175,41 @@ export interface DefaultFunctionOptions<
   override?: DefaultOverrideOptions<E, R>;
 }
 
-interface FunctionOptions extends DefaultFunctionOptions {
+export interface FunctionOptions extends DefaultFunctionOptions {
   /**
-   * TODO: implement edge runtime
+   * Runtime used
    * @default "node"
    */
   runtime?: "node" | "edge";
+  /**
+   * @default "regional"
+   */
+  placement?: "regional" | "global";
+  /**
+   * Enable overriding the default lambda.
+   */
+  override?: OverrideOptions;
+}
+
+export interface SplittedFunctionOptions extends FunctionOptions {
   /**
    * Here you should specify all the routes you want to use.
    * If not provided, all the routes will be used.
    * @default []
    */
-  routes?: string[];
+  routes: string[];
+
   /**
-   * Enable overriding the default lambda.
+   * Cloudfront compatible patterns.
+   * i.e. /api/*
+   * @default []
    */
-  override?: OverrideOptions;
+  patterns: string[];
 }
 
 export interface BuildOptions {
-  functions: {
-    default: Omit<FunctionOptions, "routes">;
-    [key: string]: FunctionOptions;
-  };
+  default: FunctionOptions;
+  functions: Record<string, SplittedFunctionOptions>;
 
   /**
    * Override the default middleware

From d422b0b4ac2e6bd06ac7774632030bb1ef0d088b Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 30 Nov 2023 20:17:45 +0100
Subject: [PATCH 027/102] basic splitting

---
 .../open-next/src/adapters/config/index.ts    |   4 +-
 packages/open-next/src/build.ts               | 296 +++---------------
 .../open-next/src/build/copyTracedFiles.ts    | 156 +++++++++
 .../open-next/src/build/createServerBundle.ts | 252 +++++++++++++++
 .../open-next/src/build/generateOutput.ts     |   6 +-
 packages/open-next/src/build/helper.ts        |  48 ++-
 packages/open-next/src/core/util.ts           |   1 -
 7 files changed, 493 insertions(+), 270 deletions(-)
 create mode 100644 packages/open-next/src/build/copyTracedFiles.ts
 create mode 100644 packages/open-next/src/build/createServerBundle.ts

diff --git a/packages/open-next/src/adapters/config/index.ts b/packages/open-next/src/adapters/config/index.ts
index 8592a7e2e..e11dadda3 100644
--- a/packages/open-next/src/adapters/config/index.ts
+++ b/packages/open-next/src/adapters/config/index.ts
@@ -9,7 +9,7 @@ import {
   loadHtmlPages,
   loadMiddlewareManifest,
   loadPrerenderManifest,
-  loadPublicAssets,
+  // loadPublicAssets,
   loadRoutesManifest,
 } from "./util.js";
 
@@ -22,7 +22,7 @@ debug({ NEXT_DIR, OPEN_NEXT_DIR });
 export const NextConfig = loadConfig(NEXT_DIR);
 export const BuildId = loadBuildId(NEXT_DIR);
 export const HtmlPages = loadHtmlPages(NEXT_DIR);
-export const PublicAssets = loadPublicAssets(OPEN_NEXT_DIR);
+// export const PublicAssets = loadPublicAssets(OPEN_NEXT_DIR);
 export const RoutesManifest = loadRoutesManifest(NEXT_DIR);
 export const ConfigHeaders = loadConfigHeaders(NEXT_DIR);
 export const PrerenderManifest = loadPrerenderManifest(NEXT_DIR);
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index e802f15d4..9d8d7d6d4 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -1,37 +1,30 @@
 import cp from "node:child_process";
 import fs, { readFileSync } from "node:fs";
-import { createRequire as topLevelCreateRequire } from "node:module";
 import path from "node:path";
 import url from "node:url";
 
 import { buildSync } from "esbuild";
 
+import { createServerBundle } from "./build/createServerBundle.js";
 import { generateOutput } from "./build/generateOutput.js";
 import {
-  compareSemver,
   esbuildAsync,
   esbuildSync,
   getBuildId,
   getHtmlPages,
-  getNextVersion,
-  getOpenNextVersion,
+  normalizeOptions,
+  Options,
   removeFiles,
   traverseFiles,
 } from "./build/helper.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import { openNextEdgePlugins } from "./plugins/edge.js";
-import { openNextReplacementPlugin } from "./plugins/replacement.js";
 import { openNextResolvePlugin } from "./plugins/resolve.js";
-import {
-  BuildOptions,
-  DangerousOptions,
-  FunctionOptions,
-} from "./types/open-next.js";
+import { BuildOptions } from "./types/open-next.js";
 
-const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
-let options: ReturnType<typeof normalizeOptions>;
+let options: Options;
 
 export type PublicFiles = {
   files: string[];
@@ -67,6 +60,13 @@ export async function build() {
   // Generate deployable bundle
   printHeader("Generating bundle");
   initOutputDir();
+
+  // Compile cache.ts
+  compileCache(options);
+
+  // Compile middleware
+  await createMiddleware();
+
   createStaticAssets();
   if (!options.dangerous?.disableIncrementalCache) {
     await createCacheAssets(
@@ -74,7 +74,7 @@ export async function build() {
       options.dangerous?.disableDynamoDBCache,
     );
   }
-  await createServerBundle(monorepoRoot, opts);
+  await createServerBundle(opts, options);
   await createRevalidationBundle();
   createImageOptimizationBundle();
   await createWarmerBundle();
@@ -94,38 +94,6 @@ function createOpenNextConfigBundle(tempDir: string) {
   });
 }
 
-function normalizeOptions(opts: BuildOptions, root: string) {
-  const appPath = path.join(process.cwd(), opts.appPath || ".");
-  const buildOutputPath = path.join(process.cwd(), opts.buildOutputPath || ".");
-  const outputDir = path.join(buildOutputPath, ".open-next");
-
-  let nextPackageJsonPath: string;
-  if (opts.packageJsonPath) {
-    const _pkgPath = path.join(process.cwd(), opts.packageJsonPath);
-    nextPackageJsonPath = _pkgPath.endsWith("package.json")
-      ? _pkgPath
-      : path.join(_pkgPath, "./package.json");
-  } else {
-    nextPackageJsonPath = findNextPackageJsonPath(appPath, root);
-  }
-  return {
-    openNextVersion: getOpenNextVersion(),
-    nextVersion: getNextVersion(nextPackageJsonPath),
-    nextPackageJsonPath,
-    appPath,
-    appBuildOutputPath: buildOutputPath,
-    appPublicPath: path.join(appPath, "public"),
-    outputDir,
-    tempDir: path.join(outputDir, ".build"),
-    minify:
-      opts.default.minify ?? Boolean(process.env.OPEN_NEXT_MINIFY) ?? false,
-    debug: opts.default.debug ?? Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
-    buildCommand: opts.buildCommand,
-    dangerous: opts.dangerous,
-    externalMiddleware: opts.middleware?.external ?? false,
-  };
-}
-
 function checkRunningInsideNextjsApp() {
   const { appPath } = options;
   const extension = ["js", "cjs", "mjs"].find((ext) =>
@@ -163,13 +131,6 @@ function findMonorepoRoot(appPath: string) {
   return { root: appPath, packager: "npm" as const };
 }
 
-function findNextPackageJsonPath(appPath: string, root: string) {
-  // This is needed for the case where the app is a single-version monorepo and the package.json is in the root of the monorepo
-  return fs.existsSync(path.join(appPath, "./package.json"))
-    ? path.join(appPath, "./package.json")
-    : path.join(root, "./package.json");
-}
-
 function setStandaloneBuildMode(monorepoRoot: string) {
   // Equivalent to setting `target: "standalone"` in next.config.js
   process.env.NEXT_PRIVATE_STANDALONE = "true";
@@ -649,162 +610,37 @@ async function createCacheAssets(
 /* Server Helper Functions */
 /***************************/
 
-function shouldGenerateDockerfile(options: FunctionOptions) {
-  return options.override?.generateDockerfile ?? false;
-}
-
-async function createServerBundle(
-  monorepoRoot: string,
-  buildOptions: BuildOptions,
-) {
-  logger.info(`Bundling server function...`);
-
-  const { appPath, appBuildOutputPath, outputDir } = options;
-
-  // Create output folder
-  const outputPath = path.join(outputDir, "server-function");
-  fs.mkdirSync(outputPath, { recursive: true });
-
-  // Resolve path to the Next.js app if inside the monorepo
-  // note: if user's app is inside a monorepo, standalone mode places
-  //       `node_modules` inside `.next/standalone`, and others inside
-  //       `.next/standalone/package/path` (ie. `.next`, `server.js`).
-  //       We need to output the handler file inside the package path.
-  const isMonorepo = monorepoRoot !== appPath;
-  const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
-
-  // Copy open-next.config.js
-  // We should reuse the one we created at the beginning of the build
-  fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });
-  fs.copyFileSync(
-    path.join(options.tempDir, "open-next.config.js"),
-    path.join(outputPath, packagePath, "open-next.config.js"),
-  );
-
-  // Bundle middleware
-  createMiddleware(packagePath);
-
-  // Copy over standalone output files
-  // note: if user uses pnpm as the package manager, node_modules contain
-  //       symlinks. We don't want to resolve the symlinks when copying.
-  fs.cpSync(path.join(appBuildOutputPath, ".next/standalone"), outputPath, {
-    recursive: true,
-    verbatimSymlinks: true,
-  });
-
-  // Standalone output already has a Node server "server.js", remove it.
-  // It will be replaced with the Lambda handler.
-  fs.rmSync(path.join(outputPath, packagePath, "server.js"), { force: true });
-
-  // Build Lambda code
-  // note: bundle in OpenNext package b/c the adapter relies on the
-  //       "serverless-http" package which is not a dependency in user's
-  //       Next.js app.
-
-  const disableNextPrebundledReact =
-    compareSemver(options.nextVersion, "13.5.1") >= 0 ||
-    compareSemver(options.nextVersion, "13.4.1") <= 0;
-
-  const overrides = buildOptions.default.override ?? {};
-
-  const isBefore13413 = compareSemver(options.nextVersion, "13.4.13") <= 0;
-
-  const disableRouting = isBefore13413 || options.externalMiddleware;
-  const plugins = [
-    openNextReplacementPlugin({
-      name: "requestHandlerOverride",
-      target: /core\/requestHandler.js/g,
-      deletes: disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : [],
-      replacements: disableRouting
-        ? [
-            require.resolve(
-              "./adapters/plugins/without-routing/requestHandler.js",
-            ),
-          ]
-        : [],
-    }),
-    openNextReplacementPlugin({
-      name: "core/util",
-      target: /core\/util.js/g,
-      deletes: [
-        ...(disableNextPrebundledReact ? ["requireHooks"] : []),
-        ...(disableRouting ? ["trustHostHeader"] : []),
-        ...(!isBefore13413 ? ["requestHandlerHost"] : []),
-      ],
-    }),
-
-    openNextResolvePlugin({
-      overrides: {
-        converter:
-          typeof overrides.converter === "function"
-            ? "dummy"
-            : overrides.converter,
-        wrapper:
-          typeof overrides.wrapper === "function"
-            ? "aws-lambda"
-            : overrides.wrapper,
-      },
-    }),
-  ];
-
-  if (plugins && plugins.length > 0) {
-    logger.debug(
-      `Applying plugins:: [${plugins
-        .map(({ name }) => name)
-        .join(",")}] for Next version: ${options.nextVersion}`,
-    );
-  }
-  await esbuildAsync(
+function compileCache(options: Options) {
+  const outfile = path.join(options.outputDir, ".build", "cache.cjs");
+  const dangerousOptions = options.dangerous;
+  esbuildSync(
     {
-      entryPoints: [path.join(__dirname, "adapters", "server-adapter.js")],
-      external: ["next", "./middleware.mjs"],
-      outfile: path.join(outputPath, packagePath, "index.mjs"),
+      external: ["next", "styled-jsx", "react", "@aws-sdk/*"],
+      entryPoints: [path.join(__dirname, "adapters", "cache.js")],
+      outfile,
+      target: ["node18"],
+      format: "cjs",
       banner: {
         js: [
-          `globalThis.monorepoPackagePath = "${packagePath}";`,
-          "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));",
+          `globalThis.disableIncrementalCache = ${
+            dangerousOptions?.disableIncrementalCache ?? false
+          };`,
+          `globalThis.disableDynamoDBCache = ${
+            dangerousOptions?.disableDynamoDBCache ?? false
+          };`,
         ].join(""),
       },
-      plugins,
     },
     options,
   );
-
-  if (isMonorepo) {
-    addMonorepoEntrypoint(outputPath, packagePath);
-  }
-  addPublicFilesList(outputPath, packagePath);
-  injectMiddlewareGeolocation(outputPath, packagePath);
-  removeCachedPages(outputPath, packagePath);
-  addCacheHandler(
-    path.join(outputPath, packagePath, ".next"),
-    options.dangerous,
-  );
-
-  const shouldGenerateDocker = shouldGenerateDockerfile(buildOptions.default);
-  if (shouldGenerateDocker) {
-    fs.writeFileSync(
-      path.join(outputPath, "Dockerfile"),
-      typeof shouldGenerateDocker === "string"
-        ? shouldGenerateDocker
-        : `
-FROM node:18-alpine
-WORKDIR /app
-COPY . /app
-EXPOSE 3000
-CMD ["node", "index.mjs"]
-    `,
-    );
-  }
+  return outfile;
 }
 
-async function createMiddleware(packagePath: string) {
+async function createMiddleware() {
   console.info(`Bundling middleware function...`);
 
-  const { appBuildOutputPath, outputDir, externalMiddleware } = options;
+  const { appBuildOutputPath, outputDir, externalMiddleware, tempDir } =
+    options;
 
   // Get middleware manifest
   const middlewareManifest = JSON.parse(
@@ -837,7 +673,7 @@ async function createMiddleware(packagePath: string) {
         entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
         // inject: ,
         bundle: true,
-        outfile: path.join(outputPath, packagePath, "handler.mjs"),
+        outfile: path.join(outputPath, "handler.mjs"),
         external: ["node:*", "next", "@aws-sdk/*"],
         target: "es2022",
         platform: "neutral",
@@ -874,7 +710,7 @@ async function createMiddleware(packagePath: string) {
     buildEdgeFunction(
       entry,
       path.join(__dirname, "core", "edgeFunctionHandler.js"),
-      path.join(outputPath, packagePath, "middleware.mjs"),
+      path.join(outputDir, ".build", "middleware.mjs"),
       appBuildOutputPath,
     );
   }
@@ -915,21 +751,7 @@ globalThis.AsyncLocalStorage = AsyncLocalStorage;
   );
 }
 
-function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
-  // Note: in the monorepo case, the handler file is output to
-  //       `.next/standalone/package/path/index.mjs`, but we want
-  //       the Lambda function to be able to find the handler at
-  //       the root of the bundle. We will create a dummy `index.mjs`
-  //       that re-exports the real handler.
-
-  // Always use posix path for import path
-  const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
-  fs.writeFileSync(
-    path.join(outputPath, "index.mjs"),
-    [`export * from "./${packagePosixPath}/index.mjs";`].join(""),
-  );
-}
-
+//TODO: Why do we need this? People have access to the headers in the middleware
 function injectMiddlewareGeolocation(outputPath: string, packagePath: string) {
   // WORKAROUND: Set `NextRequest` geolocation data — https://github.com/serverless-stack/open-next#workaround-set-nextrequest-geolocation-data
 
@@ -994,51 +816,3 @@ function addPublicFilesList(outputPath: string, packagePath: string) {
     JSON.stringify(acc),
   );
 }
-
-function removeCachedPages(outputPath: string, packagePath: string) {
-  // Pre-rendered pages will be served out from S3 by the cache handler
-  const dotNextPath = path.join(outputPath, packagePath);
-  const isFallbackTruePage = /\[.*\]/;
-  const htmlPages = getHtmlPages(dotNextPath);
-  [".next/server/pages", ".next/server/app"]
-    .map((dir) => path.join(dotNextPath, dir))
-    .filter(fs.existsSync)
-    .forEach((dir) =>
-      removeFiles(
-        dir,
-        (file) =>
-          file.endsWith(".json") ||
-          file.endsWith(".rsc") ||
-          file.endsWith(".meta") ||
-          (file.endsWith(".html") &&
-            // do not remove static HTML files
-            !htmlPages.has(file) &&
-            // do not remove HTML files with "[param].html" format
-            // b/c they are used for "fallback:true" pages
-            !isFallbackTruePage.test(file)),
-      ),
-    );
-}
-
-function addCacheHandler(outputPath: string, opts?: DangerousOptions) {
-  esbuildSync(
-    {
-      external: ["next", "styled-jsx", "react"],
-      entryPoints: [path.join(__dirname, "adapters", "cache.js")],
-      outfile: path.join(outputPath, "cache.cjs"),
-      target: ["node18"],
-      format: "cjs",
-      banner: {
-        js: [
-          `globalThis.disableIncrementalCache = ${
-            opts?.disableIncrementalCache ?? false
-          };`,
-          `globalThis.disableDynamoDBCache = ${
-            opts?.disableDynamoDBCache ?? false
-          };`,
-        ].join(""),
-      },
-    },
-    options,
-  );
-}
diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
new file mode 100644
index 000000000..0e1757270
--- /dev/null
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -0,0 +1,156 @@
+import {
+  copyFileSync,
+  existsSync,
+  mkdirSync,
+  readdirSync,
+  readFileSync,
+  statSync,
+  writeFileSync,
+} from "fs";
+import path from "path";
+
+//TODO: need to make it work with monorepo
+export async function copyTracedFiles(
+  buildOutputPath: string,
+  outputDir: string,
+  routes: string[],
+) {
+  console.time("copyTracedFiles");
+  const dotNextDir = path.join(buildOutputPath, ".next");
+  const standaloneDir = path.join(dotNextDir, "standalone");
+  const standaloneNextDir = path.join(standaloneDir, ".next");
+
+  const extractFiles = (files: string[], from = standaloneNextDir) => {
+    return files.map((f) => path.resolve(from, f));
+  };
+
+  // On next 14+, we might not have to include those files
+  // For next 13, we need to include them otherwise we get runtime error
+  const requiredServerFiles = JSON.parse(
+    readFileSync(path.join(dotNextDir, "next-server.js.nft.json"), "utf8"),
+  );
+
+  const filesToCopy = new Map<string, string>();
+
+  // Files necessary by the server
+  extractFiles(requiredServerFiles.files).forEach((f) => {
+    filesToCopy.set(f, f.replace(standaloneDir, outputDir));
+  });
+
+  // create directory for pages
+  mkdirSync(path.join(outputDir, ".next/server/pages"), { recursive: true });
+  mkdirSync(path.join(outputDir, ".next/server/app"), { recursive: true });
+  mkdirSync(path.join(outputDir, ".next/server/chunks"), { recursive: true });
+
+  const computeCopyFilesForPage = (pagePath: string) => {
+    const fullFilePath = `server/${pagePath}.js`;
+    const requiredFiles = JSON.parse(
+      readFileSync(
+        path.join(standaloneNextDir, `${fullFilePath}.nft.json`),
+        "utf8",
+      ),
+    );
+    const dir = path.dirname(fullFilePath);
+    extractFiles(
+      requiredFiles.files,
+      path.join(standaloneNextDir, dir),
+    ).forEach((f) => {
+      filesToCopy.set(f, f.replace(standaloneDir, outputDir));
+    });
+
+    filesToCopy.set(
+      path.join(standaloneNextDir, fullFilePath),
+      path.join(outputDir, ".next", fullFilePath),
+    );
+  };
+
+  const hasPageDir = routes.some((route) => route.startsWith("pages/"));
+  const hasAppDir = routes.some((route) => route.startsWith("app/"));
+
+  // We need to copy all the base files like _app, _document, _error, etc
+  // One thing to note, is that next try to load every routes that might be needed in advance
+  // So if you have a [slug].tsx at the root, this route will always be loaded for 1st level request
+  // along with _app and _document
+  if (hasPageDir) {
+    //Page dir
+    computeCopyFilesForPage("pages/_app");
+    computeCopyFilesForPage("pages/_document");
+    computeCopyFilesForPage("pages/_error");
+  }
+
+  if (hasAppDir) {
+    //App dir
+    computeCopyFilesForPage("app/_not-found");
+  }
+
+  //Files we actually want to include
+  routes.forEach((route) => {
+    computeCopyFilesForPage(route);
+  });
+
+  //Actually copy the files
+  filesToCopy.forEach((to, from) => {
+    if (
+      from.includes("node_modules") &&
+      //TODO: we need to figure which packages we could safely remove
+      (from.includes("@edge-runtime") ||
+        from.includes("caniuse-lite") ||
+        from.includes("jest-worker") ||
+        from.includes("sharp"))
+    ) {
+      return;
+    }
+    mkdirSync(path.dirname(to), { recursive: true });
+    copyFileSync(from, to);
+  });
+
+  mkdirSync(path.join(outputDir, ".next"), { recursive: true });
+
+  readdirSync(standaloneNextDir).forEach((f) => {
+    if (statSync(path.join(standaloneNextDir, f)).isDirectory()) return;
+    copyFileSync(
+      path.join(standaloneNextDir, f),
+      path.join(path.join(outputDir, ".next"), f),
+    );
+  });
+
+  // We then need to copy all the files at the root of server
+
+  mkdirSync(path.join(outputDir, ".next/server"), { recursive: true });
+
+  readdirSync(path.join(standaloneNextDir, "server")).forEach((f) => {
+    if (statSync(path.join(standaloneNextDir, "server", f)).isDirectory())
+      return;
+    if (f !== "server.js") {
+      copyFileSync(
+        path.join(standaloneNextDir, "server", f),
+        path.join(path.join(outputDir, ".next/server"), f),
+      );
+    }
+  });
+
+  // TODO: Recompute all the files.
+  // vercel doesn't seem to do it, but it seems wasteful to have all those files
+  // we replace the pages-manifest.json with an empty one if we don't have a pages dir so that
+  // next doesn't try to load _app, _document
+  if (!hasPageDir) {
+    writeFileSync(
+      path.join(outputDir, ".next/server/pages-manifest.json"),
+      "{}",
+    );
+  }
+
+  //TODO: Find what else we need to copy
+  const copyStaticFile = (filePath: string) => {
+    if (existsSync(path.join(standaloneNextDir, filePath))) {
+      copyFileSync(
+        path.join(standaloneNextDir, filePath),
+        path.join(outputDir, ".next", filePath),
+      );
+    }
+  };
+  copyStaticFile("server/pages/404.html");
+  copyStaticFile("server/pages/500.html");
+
+  console.timeEnd("copyTracedFiles");
+}
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
new file mode 100644
index 000000000..19dfef014
--- /dev/null
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -0,0 +1,252 @@
+import { createRequire as topLevelCreateRequire } from "node:module";
+
+import fs from "fs";
+import path from "path";
+import { BuildOptions, FunctionOptions } from "types/open-next";
+import url from "url";
+
+import logger from "../logger.js";
+import { openNextReplacementPlugin } from "../plugins/replacement.js";
+import { openNextResolvePlugin } from "../plugins/resolve.js";
+import { copyTracedFiles } from "./copyTracedFiles.js";
+import type { Options } from "./helper.js";
+import { compareSemver, esbuildAsync, traverseFiles } from "./helper.js";
+
+const require = topLevelCreateRequire(import.meta.url);
+const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
+
+export async function createServerBundle(
+  options: BuildOptions,
+  buildRuntimeOptions: Options,
+) {
+  const foundRoutes = new Set<string>();
+  // Get all functions to build
+  const defaultFn = options.default;
+  const functions = Object.entries(options.functions);
+
+  const promises = functions.map(async ([name, fnOptions]) => {
+    const routes = fnOptions.routes;
+    routes.forEach((route) => foundRoutes.add(route));
+    await generateBundle(name, buildRuntimeOptions, fnOptions);
+  });
+
+  // We build every other function than default before so we know which route there is left
+  await Promise.all(promises);
+
+  const remainingRoutes = new Set<string>();
+
+  // Find remaining routes
+  const serverPath = path.join(
+    buildRuntimeOptions.appBuildOutputPath,
+    ".next",
+    "standalone",
+    ".next",
+    "server",
+  );
+
+  // Find app dir routes
+  const appPath = path.join(serverPath, "app");
+  traverseFiles(
+    appPath,
+    (file) => {
+      if (file.endsWith("page.js") || file.endsWith("route.js")) {
+        const route = `app/${file.replace(/\.js$/, "")}`;
+        // console.log(`Found remaining route: ${route}`);
+        if (!foundRoutes.has(route)) {
+          remainingRoutes.add(route);
+        }
+      }
+      return false;
+    },
+    () => {},
+  );
+
+  // Find pages dir routes
+  const pagePath = path.join(serverPath, "pages");
+  traverseFiles(
+    pagePath,
+    (file) => {
+      if (file.endsWith(".js")) {
+        const route = `pages/${file.replace(/\.js$/, "")}`;
+        if (!foundRoutes.has(route)) {
+          remainingRoutes.add(route);
+        }
+      }
+      return false;
+    },
+    () => {},
+  );
+
+  // Generate default function
+  await generateBundle("default", buildRuntimeOptions, {
+    ...defaultFn,
+    routes: Array.from(remainingRoutes),
+    patterns: ["*"],
+  });
+}
+
+async function generateBundle(
+  name: string,
+  options: Options,
+  fnOptions: BuildOptions["functions"][string],
+) {
+  const { appPath, appBuildOutputPath, outputDir, monorepoRoot } = options;
+
+  // Create output folder
+  const outputPath = path.join(outputDir, "server-functions", name);
+  fs.mkdirSync(outputPath, { recursive: true });
+
+  // Resolve path to the Next.js app if inside the monorepo
+  // note: if user's app is inside a monorepo, standalone mode places
+  //       `node_modules` inside `.next/standalone`, and others inside
+  //       `.next/standalone/package/path` (ie. `.next`, `server.js`).
+  //       We need to output the handler file inside the package path.
+  const isMonorepo = monorepoRoot !== appPath;
+  const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
+
+  // Copy cache file
+  // It needs to be inside ".next"
+  fs.mkdirSync(path.join(outputPath, packagePath, ".next"), {
+    recursive: true,
+  });
+  fs.copyFileSync(
+    path.join(outputDir, ".build", "cache.cjs"),
+    path.join(outputPath, packagePath, ".next", "cache.cjs"),
+  );
+
+  // // Copy middleware
+  if (!options.externalMiddleware) {
+    fs.copyFileSync(
+      path.join(outputDir, ".build", "middleware.mjs"),
+      path.join(outputPath, packagePath, "middleware.mjs"),
+    );
+  }
+
+  // Copy open-next.config.js
+  fs.copyFileSync(
+    path.join(outputDir, ".build", "open-next.config.js"),
+    path.join(outputPath, packagePath, "open-next.config.js"),
+  );
+  // Copy all necessary traced files
+  copyTracedFiles(
+    appBuildOutputPath,
+    outputPath,
+    fnOptions.routes ?? ["app/page.tsx"],
+  );
+
+  // Build Lambda code
+  // note: bundle in OpenNext package b/c the adapter relies on the
+  //       "serverless-http" package which is not a dependency in user's
+  //       Next.js app.
+
+  const disableNextPrebundledReact =
+    compareSemver(options.nextVersion, "13.5.1") >= 0 ||
+    compareSemver(options.nextVersion, "13.4.1") <= 0;
+
+  const overrides = fnOptions.override ?? {};
+
+  const isBefore13413 = compareSemver(options.nextVersion, "13.4.13") <= 0;
+
+  const disableRouting = isBefore13413 || options.externalMiddleware;
+  const plugins = [
+    openNextReplacementPlugin({
+      name: "requestHandlerOverride",
+      target: /core\/requestHandler.js/g,
+      deletes: disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : [],
+      replacements: disableRouting
+        ? [
+            require.resolve(
+              "../adapters/plugins/without-routing/requestHandler.js",
+            ),
+          ]
+        : [],
+    }),
+    openNextReplacementPlugin({
+      name: "core/util",
+      target: /core\/util.js/g,
+      deletes: [
+        ...(disableNextPrebundledReact ? ["requireHooks"] : []),
+        ...(disableRouting ? ["trustHostHeader"] : []),
+        ...(!isBefore13413 ? ["requestHandlerHost"] : []),
+      ],
+    }),
+
+    openNextResolvePlugin({
+      overrides: {
+        converter:
+          typeof overrides.converter === "function"
+            ? "dummy"
+            : overrides.converter,
+        wrapper:
+          typeof overrides.wrapper === "function"
+            ? "aws-lambda"
+            : overrides.wrapper,
+      },
+    }),
+  ];
+
+  if (plugins && plugins.length > 0) {
+    logger.debug(
+      `Applying plugins:: [${plugins
+        .map(({ name }) => name)
+        .join(",")}] for Next version: ${options.nextVersion}`,
+    );
+  }
+  await esbuildAsync(
+    {
+      entryPoints: [path.join(__dirname, "../adapters", "server-adapter.js")],
+      external: ["next", "./middleware.mjs"],
+      outfile: path.join(outputPath, packagePath, "index.mjs"),
+      banner: {
+        js: [
+          `globalThis.monorepoPackagePath = "${packagePath}";`,
+          "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(""),
+      },
+      plugins,
+    },
+    options,
+  );
+
+  if (isMonorepo) {
+    addMonorepoEntrypoint(outputPath, packagePath);
+  }
+
+  const shouldGenerateDocker = shouldGenerateDockerfile(fnOptions);
+  if (shouldGenerateDocker) {
+    fs.writeFileSync(
+      path.join(outputPath, "Dockerfile"),
+      typeof shouldGenerateDocker === "string"
+        ? shouldGenerateDocker
+        : `
+FROM node:18-alpine
+WORKDIR /app
+COPY . /app
+EXPOSE 3000
+CMD ["node", "index.mjs"]
+    `,
+    );
+  }
+}
+
+function shouldGenerateDockerfile(options: FunctionOptions) {
+  return options.override?.generateDockerfile ?? false;
+}
+
+function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
+  // Note: in the monorepo case, the handler file is output to
+  //       `.next/standalone/package/path/index.mjs`, but we want
+  //       the Lambda function to be able to find the handler at
+  //       the root of the bundle. We will create a dummy `index.mjs`
+  //       that re-exports the real handler.
+
+  // Always use posix path for import path
+  const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
+  fs.writeFileSync(
+    path.join(outputPath, "index.mjs"),
+    [`export * from "./${packagePosixPath}/index.mjs";`].join(""),
+  );
+}
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index 7e3a58ea5..bd3ab4bfd 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -139,7 +139,7 @@ export async function generateOutput(
         origins[key] = {
           type: "function",
           handler: "index.handler",
-          bundle: `.open-next/functions/${key}`,
+          bundle: `.open-next/server-functions/${key}`,
           streaming,
         };
       }
@@ -151,8 +151,8 @@ export async function generateOutput(
     if (value.override?.generateDockerfile) {
       origins[key] = {
         type: "ecs",
-        bundle: `.open-next/functions/${key}`,
-        dockerfile: `.open-next/functions/${key}/Dockerfile`,
+        bundle: `.open-next/server-functions/${key}`,
+        dockerfile: `.open-next/server-functions/${key}/Dockerfile`,
       };
     }
   });
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
index 144f90750..530716f3f 100644
--- a/packages/open-next/src/build/helper.ts
+++ b/packages/open-next/src/build/helper.ts
@@ -8,15 +8,53 @@ import {
   BuildOptions as ESBuildOptions,
   buildSync,
 } from "esbuild";
+import { BuildOptions } from "types/open-next.js";
 
 import logger from "../logger.js";
 
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
-interface Options {
-  openNextVersion: string;
-  debug: boolean;
+export type Options = ReturnType<typeof normalizeOptions>;
+
+export function normalizeOptions(opts: BuildOptions, root: string) {
+  const appPath = path.join(process.cwd(), opts.appPath || ".");
+  const buildOutputPath = path.join(process.cwd(), opts.buildOutputPath || ".");
+  const outputDir = path.join(buildOutputPath, ".open-next");
+
+  let nextPackageJsonPath: string;
+  if (opts.packageJsonPath) {
+    const _pkgPath = path.join(process.cwd(), opts.packageJsonPath);
+    nextPackageJsonPath = _pkgPath.endsWith("package.json")
+      ? _pkgPath
+      : path.join(_pkgPath, "./package.json");
+  } else {
+    nextPackageJsonPath = findNextPackageJsonPath(appPath, root);
+  }
+  return {
+    openNextVersion: getOpenNextVersion(),
+    nextVersion: getNextVersion(nextPackageJsonPath),
+    nextPackageJsonPath,
+    appPath,
+    appBuildOutputPath: buildOutputPath,
+    appPublicPath: path.join(appPath, "public"),
+    outputDir,
+    tempDir: path.join(outputDir, ".build"),
+    minify:
+      opts.default.minify ?? Boolean(process.env.OPEN_NEXT_MINIFY) ?? false,
+    debug: opts.default.debug ?? Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
+    buildCommand: opts.buildCommand,
+    dangerous: opts.dangerous,
+    externalMiddleware: opts.middleware?.external ?? false,
+    monorepoRoot: root,
+  };
+}
+
+function findNextPackageJsonPath(appPath: string, root: string) {
+  // This is needed for the case where the app is a single-version monorepo and the package.json is in the root of the monorepo
+  return fs.existsSync(path.join(appPath, "./package.json"))
+    ? path.join(appPath, "./package.json")
+    : path.join(root, "./package.json");
 }
 
 export function esbuildSync(esbuildOptions: ESBuildOptions, options: Options) {
@@ -27,7 +65,9 @@ export function esbuildSync(esbuildOptions: ESBuildOptions, options: Options) {
     platform: "node",
     bundle: true,
     minify: debug ? false : true,
+    mainFields: ["module", "main"],
     sourcemap: debug ? "inline" : false,
+    sourcesContent: false,
     ...esbuildOptions,
     external: ["./open-next.config.js", ...(esbuildOptions.external ?? [])],
     banner: {
@@ -61,7 +101,9 @@ export async function esbuildAsync(
     platform: "node",
     bundle: true,
     minify: debug ? false : true,
+    mainFields: ["module", "main"],
     sourcemap: debug ? "inline" : false,
+    sourcesContent: false,
     ...esbuildOptions,
     external: [
       ...(esbuildOptions.external ?? []),
diff --git a/packages/open-next/src/core/util.ts b/packages/open-next/src/core/util.ts
index 7f7838812..bb2453178 100644
--- a/packages/open-next/src/core/util.ts
+++ b/packages/open-next/src/core/util.ts
@@ -49,7 +49,6 @@ export const requestHandler = new NextServer.default({
       trustHostHeader: true,
       //#endOverride
 
-      //TODO: change env.LAMBDA_TASK_ROOT
       incrementalCacheHandlerPath: `./cache.cjs`,
     },
   },

From 3763bab82190aa646cbf29d66dde3259b1bd6f06 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 1 Dec 2023 16:31:44 +0100
Subject: [PATCH 028/102] bundled next server

---
 examples/app-pages-router/open-next.config.ts |  11 +-
 examples/app-router/open-next.config.ts       |  10 +-
 examples/pages-router/open-next.config.ts     |   5 +-
 .../open-next/src/build/bundleNextServer.ts   | 106 +++++++++++
 .../open-next/src/build/copyTracedFiles.ts    |  17 +-
 .../open-next/src/build/createServerBundle.ts |  85 ++++++---
 .../open-next/src/build/generateOutput.ts     | 165 ++++++++++--------
 packages/open-next/src/types/open-next.ts     |   7 +
 8 files changed, 289 insertions(+), 117 deletions(-)
 create mode 100644 packages/open-next/src/build/bundleNextServer.ts

diff --git a/examples/app-pages-router/open-next.config.ts b/examples/app-pages-router/open-next.config.ts
index 6b4345ecf..8b09de17e 100644
--- a/examples/app-pages-router/open-next.config.ts
+++ b/examples/app-pages-router/open-next.config.ts
@@ -1,6 +1,15 @@
 const config = {
+  default: {},
   functions: {
-    default: {},
+    api: {
+      routes: [
+        "app/api/page",
+        "app/api/client/route",
+        "app/api/host/route",
+        "pages/api/hello",
+      ],
+      patterns: ["/api/*"],
+    },
   },
   buildCommand: "npx turbo build",
 };
diff --git a/examples/app-router/open-next.config.ts b/examples/app-router/open-next.config.ts
index 423ca55ef..65c916ac1 100644
--- a/examples/app-router/open-next.config.ts
+++ b/examples/app-router/open-next.config.ts
@@ -1,11 +1,11 @@
 const config = {
-  functions: {
-    default: {
-      override: {
-        wrapper: "aws-lambda-streaming",
-      },
+  default: {
+    override: {
+      wrapper: "aws-lambda-streaming",
     },
+    experimentalBundledNextServer: true,
   },
+  functions: {},
   buildCommand: "npx turbo build",
 };
 
diff --git a/examples/pages-router/open-next.config.ts b/examples/pages-router/open-next.config.ts
index 6b4345ecf..a03cdc05d 100644
--- a/examples/pages-router/open-next.config.ts
+++ b/examples/pages-router/open-next.config.ts
@@ -1,7 +1,8 @@
 const config = {
-  functions: {
-    default: {},
+  default: {
+    experimentalBundledNextServer: true,
   },
+  functions: {},
   buildCommand: "npx turbo build",
 };
 
diff --git a/packages/open-next/src/build/bundleNextServer.ts b/packages/open-next/src/build/bundleNextServer.ts
new file mode 100644
index 000000000..aa44bab58
--- /dev/null
+++ b/packages/open-next/src/build/bundleNextServer.ts
@@ -0,0 +1,106 @@
+import { createRequire as topLevelCreateRequire } from "node:module";
+
+import { build } from "esbuild";
+import path from "path";
+
+const externals = [
+  // This one was causing trouble, don't know why
+  "../experimental/testmode/server",
+
+  // sharedExternals
+  "styled-jsx",
+  "styled-jsx/style",
+  "@opentelemetry/api",
+  "next/dist/compiled/@next/react-dev-overlay/dist/middleware",
+  "next/dist/compiled/@ampproject/toolbox-optimizer",
+  "next/dist/compiled/edge-runtime",
+  "next/dist/compiled/@edge-runtime/ponyfill",
+  "next/dist/compiled/undici",
+  "next/dist/compiled/raw-body",
+  "next/dist/server/capsize-font-metrics.json",
+  "critters",
+  "next/dist/compiled/node-html-parser",
+  "next/dist/compiled/compression",
+  "next/dist/compiled/jsonwebtoken",
+  "next/dist/compiled/@opentelemetry/api",
+  "next/dist/compiled/@mswjs/interceptors/ClientRequest",
+  "next/dist/compiled/ws",
+
+  // externalsMap
+  // In the config they replace it, but we don't use this one inside NextServer anymore 13.4.12+
+  // For earlier versions we might have to alias it
+  "./web/sandbox",
+
+  // pagesExternal
+  "react",
+  "react-dom",
+  "react-server-dom-webpack",
+  "react-server-dom-turbopack",
+
+  // We need to remove this since this is what webpack is building
+  // Adding it cause to add a lot of unnecessary deps
+  "next/dist/compiled/next-server",
+];
+
+export async function bundleNextServer(outputDir: string, appPath: string) {
+  const require = topLevelCreateRequire(`${appPath}/package.json`);
+  const entrypoint = require.resolve("next/dist/esm/server/next-server.js");
+
+  await build({
+    entryPoints: [entrypoint],
+    bundle: true,
+    platform: "node",
+    target: ["node18"],
+    // packages: "external",
+    format: "cjs",
+    external: externals,
+    minify: true,
+    outfile: path.join(outputDir, "next-server.runtime.prod.js"),
+    sourcemap: false,
+    plugins: [
+      {
+        name: "opennext-next-server",
+        setup(build) {
+          // This was an attempt at reducing server bundle size
+          // It might be the better way to go in the future
+          build.onResolve({ filter: /\.\/module.compiled/ }, (args) => {
+            console.log(args);
+            const dir = args.resolveDir.split("/").slice(-1);
+            console.log(dir);
+            return {
+              path: path.join(
+                "next/dist/compiled/next-server/",
+                `${dir}.runtime.prod.js`,
+              ),
+              external: true,
+            };
+          });
+
+          build.onResolve({ filter: /[\\/]react-server\.node/ }, (args) => {
+            return {
+              path: args.path,
+              external: true,
+            };
+          });
+
+          build.onResolve(
+            { filter: /vendored[\\/]rsc[\\/]entrypoints/ },
+            (args) => {
+              return {
+                path: args.path,
+                external: true,
+              };
+            },
+          );
+
+          build.onResolve({ filter: /\.external/ }, (args) => {
+            return {
+              path: args.path.replace(/\.\./, "next/dist"),
+              external: true,
+            };
+          });
+        },
+      },
+    ],
+  });
+}
diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 0e1757270..086850aca 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -12,13 +12,15 @@ import path from "path";
 //TODO: need to make it work with monorepo
 export async function copyTracedFiles(
   buildOutputPath: string,
+  packagePath: string,
   outputDir: string,
   routes: string[],
+  bundledNextServer: boolean,
 ) {
   console.time("copyTracedFiles");
   const dotNextDir = path.join(buildOutputPath, ".next");
   const standaloneDir = path.join(dotNextDir, "standalone");
-  const standaloneNextDir = path.join(standaloneDir, ".next");
+  const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
 
   const extractFiles = (files: string[], from = standaloneNextDir) => {
     return files.map((f) => path.resolve(from, f));
@@ -27,7 +29,15 @@ export async function copyTracedFiles(
   // On next 14+, we might not have to include those files
   // For next 13, we need to include them otherwise we get runtime error
   const requiredServerFiles = JSON.parse(
-    readFileSync(path.join(dotNextDir, "next-server.js.nft.json"), "utf8"),
+    readFileSync(
+      path.join(
+        dotNextDir,
+        bundledNextServer
+          ? "next-minimal-server.js.nft.json"
+          : "next-server.js.nft.json",
+      ),
+      "utf8",
+    ),
   );
 
   const filesToCopy = new Map<string, string>();
@@ -93,8 +103,7 @@ export async function copyTracedFiles(
     if (
       from.includes("node_modules") &&
       //TODO: we need to figure which packages we could safely remove
-      (from.includes("@edge-runtime") ||
-        from.includes("caniuse-lite") ||
+      (from.includes("caniuse-lite") ||
         from.includes("jest-worker") ||
         from.includes("sharp"))
     ) {
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 19dfef014..e57bc98b3 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -1,3 +1,4 @@
+import { existsSync } from "node:fs";
 import { createRequire as topLevelCreateRequire } from "node:module";
 
 import fs from "fs";
@@ -8,6 +9,7 @@ import url from "url";
 import logger from "../logger.js";
 import { openNextReplacementPlugin } from "../plugins/replacement.js";
 import { openNextResolvePlugin } from "../plugins/resolve.js";
+import { bundleNextServer } from "./bundleNextServer.js";
 import { copyTracedFiles } from "./copyTracedFiles.js";
 import type { Options } from "./helper.js";
 import { compareSemver, esbuildAsync, traverseFiles } from "./helper.js";
@@ -35,47 +37,56 @@ export async function createServerBundle(
 
   const remainingRoutes = new Set<string>();
 
+  const { monorepoRoot, appBuildOutputPath } = buildRuntimeOptions;
+
+  const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
+
   // Find remaining routes
   const serverPath = path.join(
     buildRuntimeOptions.appBuildOutputPath,
     ".next",
     "standalone",
+    packagePath,
     ".next",
     "server",
   );
 
   // Find app dir routes
-  const appPath = path.join(serverPath, "app");
-  traverseFiles(
-    appPath,
-    (file) => {
-      if (file.endsWith("page.js") || file.endsWith("route.js")) {
-        const route = `app/${file.replace(/\.js$/, "")}`;
-        // console.log(`Found remaining route: ${route}`);
-        if (!foundRoutes.has(route)) {
-          remainingRoutes.add(route);
+  if (existsSync(path.join(serverPath, "app"))) {
+    const appPath = path.join(serverPath, "app");
+    traverseFiles(
+      appPath,
+      (file) => {
+        if (file.endsWith("page.js") || file.endsWith("route.js")) {
+          const route = `app/${file.replace(/\.js$/, "")}`;
+          // console.log(`Found remaining route: ${route}`);
+          if (!foundRoutes.has(route)) {
+            remainingRoutes.add(route);
+          }
         }
-      }
-      return false;
-    },
-    () => {},
-  );
+        return false;
+      },
+      () => {},
+    );
+  }
 
   // Find pages dir routes
-  const pagePath = path.join(serverPath, "pages");
-  traverseFiles(
-    pagePath,
-    (file) => {
-      if (file.endsWith(".js")) {
-        const route = `pages/${file.replace(/\.js$/, "")}`;
-        if (!foundRoutes.has(route)) {
-          remainingRoutes.add(route);
+  if (existsSync(path.join(serverPath, "pages"))) {
+    const pagePath = path.join(serverPath, "pages");
+    traverseFiles(
+      pagePath,
+      (file) => {
+        if (file.endsWith(".js")) {
+          const route = `pages/${file.replace(/\.js$/, "")}`;
+          if (!foundRoutes.has(route)) {
+            remainingRoutes.add(route);
+          }
         }
-      }
-      return false;
-    },
-    () => {},
-  );
+        return false;
+      },
+      () => {},
+    );
+  }
 
   // Generate default function
   await generateBundle("default", buildRuntimeOptions, {
@@ -114,8 +125,17 @@ async function generateBundle(
     path.join(outputPath, packagePath, ".next", "cache.cjs"),
   );
 
+  // Bundle next server if necessary
+  const isBundled = fnOptions.experimentalBundledNextServer ?? false;
+  if (isBundled) {
+    bundleNextServer(path.join(outputPath, packagePath), appPath);
+  }
+
   // // Copy middleware
-  if (!options.externalMiddleware) {
+  if (
+    !options.externalMiddleware &&
+    existsSync(path.join(outputDir, ".build", "middleware.mjs"))
+  ) {
     fs.copyFileSync(
       path.join(outputDir, ".build", "middleware.mjs"),
       path.join(outputPath, packagePath, "middleware.mjs"),
@@ -130,8 +150,10 @@ async function generateBundle(
   // Copy all necessary traced files
   copyTracedFiles(
     appBuildOutputPath,
+    packagePath,
     outputPath,
     fnOptions.routes ?? ["app/page.tsx"],
+    isBundled,
   );
 
   // Build Lambda code
@@ -195,7 +217,7 @@ async function generateBundle(
   await esbuildAsync(
     {
       entryPoints: [path.join(__dirname, "../adapters", "server-adapter.js")],
-      external: ["next", "./middleware.mjs"],
+      external: ["next", "./middleware.mjs", "./next-server.runtime.prod.js"],
       outfile: path.join(outputPath, packagePath, "index.mjs"),
       banner: {
         js: [
@@ -207,6 +229,11 @@ async function generateBundle(
         ].join(""),
       },
       plugins,
+      alias: {
+        "next/dist/server/next-server.js": isBundled
+          ? "./next-server.runtime.prod.js"
+          : "next/dist/server/next-server.js",
+      },
     },
     options,
   );
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index bd3ab4bfd..d42867c94 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -3,37 +3,48 @@ import path from "node:path";
 
 import { BuildOptions, FunctionOptions } from "types/open-next";
 
+import { getBuildId } from "./helper.js";
+
 type BaseFunction = {
   handler: string;
   bundle: string;
 };
 
+type OpenNextFunctionOrigin = {
+  type: "function";
+  streaming?: boolean;
+} & BaseFunction;
+
+type OpenNextECSOrigin = {
+  type: "ecs";
+  bundle: string;
+  dockerfile: string;
+};
+
+type OpenNextS3Origin = {
+  type: "s3";
+  originPath: string;
+  copy: {
+    from: string;
+    to: string;
+    cached: boolean;
+    versionedSubDir?: string;
+  }[];
+};
+
 type OpenNextOrigins =
-  | ({
-      type: "function";
-      streaming?: boolean;
-    } & BaseFunction)
-  | {
-      type: "ecs";
-      bundle: string;
-      dockerfile: string;
-    }
-  | {
-      type: "s3";
-      originPath: string;
-      copy: {
-        from: string;
-        to: string;
-        cached: boolean;
-        versionedSubDir?: string;
-      }[];
-    };
+  | OpenNextFunctionOrigin
+  | OpenNextECSOrigin
+  | OpenNextS3Origin;
 
 interface OpenNextOutput {
   edgeFunctions: {
     [key: string]: BaseFunction;
   };
   origins: {
+    s3: OpenNextS3Origin;
+    default: OpenNextFunctionOrigin | OpenNextECSOrigin;
+    imageOptimizer: OpenNextFunctionOrigin | OpenNextECSOrigin;
     [key: string]: OpenNextOrigins;
   };
   behaviors: {
@@ -52,7 +63,6 @@ interface OpenNextOutput {
 
 async function canStream(opts: FunctionOptions) {
   if (!opts.override?.wrapper) {
-    console.log("Why not here?"), opts.override;
     return false;
   } else {
     if (typeof opts.override.wrapper === "string") {
@@ -86,6 +96,8 @@ export async function generateOutput(
     }
   });
 
+  const defaultOriginCanstream = await canStream(buildOptions.default);
+
   // First add s3 origins and image optimization
   const origins: OpenNextOutput["origins"] = {
     s3: {
@@ -115,81 +127,54 @@ export async function generateOutput(
       bundle: ".open-next/image-optimization-function",
       streaming: false,
     },
+    default: buildOptions.default.override?.generateDockerfile
+      ? {
+          type: "ecs",
+          bundle: ".open-next/server-functions/default",
+          dockerfile: ".open-next/server-functions/default/Dockerfile",
+        }
+      : {
+          type: "function",
+          handler: "index.handler",
+          bundle: ".open-next/server-functions/default",
+          streaming: defaultOriginCanstream,
+        },
   };
 
-  const defaultOriginCanstream = await canStream(buildOptions.default);
-  origins.default = buildOptions.default.override?.generateDockerfile
-    ? {
-        type: "ecs",
-        bundle: ".open-next/default-function",
-        dockerfile: ".open-next/default-function/Dockerfile",
-      }
-    : {
-        type: "function",
-        handler: "index.handler",
-        bundle: ".open-next/default-function",
-        streaming: defaultOriginCanstream,
-      };
-
   // Then add function origins
-  Promise.all(
+  await Promise.all(
     Object.entries(buildOptions.functions).map(async ([key, value]) => {
       if (!value.runtime || value.runtime === "node") {
-        const streaming = await canStream(value);
-        origins[key] = {
-          type: "function",
-          handler: "index.handler",
-          bundle: `.open-next/server-functions/${key}`,
-          streaming,
-        };
+        if (value.override?.generateDockerfile) {
+          origins[key] = {
+            type: "ecs",
+            bundle: `.open-next/server-functions/${key}`,
+            dockerfile: `.open-next/server-functions/${key}/Dockerfile`,
+          };
+        } else {
+          const streaming = await canStream(value);
+          origins[key] = {
+            type: "function",
+            handler: "index.handler",
+            bundle: `.open-next/server-functions/${key}`,
+            streaming,
+          };
+        }
       }
     }),
   );
 
-  // Then add ecs origins
-  Object.entries(buildOptions.functions).forEach(([key, value]) => {
-    if (value.override?.generateDockerfile) {
-      origins[key] = {
-        type: "ecs",
-        bundle: `.open-next/server-functions/${key}`,
-        dockerfile: `.open-next/server-functions/${key}/Dockerfile`,
-      };
-    }
-  });
-
   // Then we need to compute the behaviors
   const behaviors: OpenNextOutput["behaviors"] = [
     { pattern: "_next/image*", origin: "imageOptimizer" },
-    {
-      pattern: "*",
-      origin: "default",
-      edgeFunction: isExternalMiddleware ? "middleware" : undefined,
-    },
-    //TODO: add base files
   ];
 
-  //Compute behaviors for assets files
-  const assetPath = path.join(outputPath, ".open-next", "assets");
-  fs.readdirSync(assetPath).forEach((item) => {
-    if (fs.statSync(path.join(assetPath, item)).isDirectory()) {
-      behaviors.push({
-        pattern: `${item}/*`,
-        origin: "s3",
-      });
-    } else {
-      behaviors.push({
-        pattern: item,
-        origin: "s3",
-      });
-    }
-  });
-
   // Then we add the routes
   Object.entries(buildOptions.functions).forEach(([key, value]) => {
     const patterns = "patterns" in value ? value.patterns : ["*"];
     patterns.forEach((pattern) => {
       behaviors.push({
-        pattern,
+        pattern: pattern.replace(/BUILD_ID/, getBuildId(outputPath)),
         origin: value.placement === "global" ? undefined : key,
         edgeFunction:
           value.placement === "global"
@@ -201,6 +186,34 @@ export async function generateOutput(
     });
   });
 
+  // We finish with the default behavior so that they don't override the others
+  behaviors.push({
+    pattern: "_next/data/*",
+    origin: "default",
+    edgeFunction: isExternalMiddleware ? "middleware" : undefined,
+  });
+  behaviors.push({
+    pattern: "*",
+    origin: "default",
+    edgeFunction: isExternalMiddleware ? "middleware" : undefined,
+  });
+
+  //Compute behaviors for assets files
+  const assetPath = path.join(outputPath, ".open-next", "assets");
+  fs.readdirSync(assetPath).forEach((item) => {
+    if (fs.statSync(path.join(assetPath, item)).isDirectory()) {
+      behaviors.push({
+        pattern: `${item}/*`,
+        origin: "s3",
+      });
+    } else {
+      behaviors.push({
+        pattern: item,
+        origin: "s3",
+      });
+    }
+  });
+
   const output: OpenNextOutput = {
     edgeFunctions,
     origins,
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index e537418d9..7b31f2ff7 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -189,6 +189,13 @@ export interface FunctionOptions extends DefaultFunctionOptions {
    * Enable overriding the default lambda.
    */
   override?: OverrideOptions;
+
+  /**
+   * Bundle Next server into a single file.
+   * This results in a way smaller bundle but it might break for some cases.
+   * @default false
+   */
+  experimentalBundledNextServer?: boolean;
 }
 
 export interface SplittedFunctionOptions extends FunctionOptions {

From a5995dbc2b302df434a56852e905c897fd4abf70 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 6 Dec 2023 11:48:29 +0100
Subject: [PATCH 029/102] fix external middleware cloudfront

---
 packages/open-next/src/adapters/middleware.ts |  3 +
 .../plugins/without-routing/requestHandler.ts | 15 +++-
 packages/open-next/src/build.ts               | 11 ++-
 .../open-next/src/build/bundleNextServer.ts   |  2 -
 .../open-next/src/build/createServerBundle.ts |  1 -
 .../open-next/src/build/generateOutput.ts     |  2 +-
 .../src/converters/aws-cloudfront.ts          | 90 +++++++++++++++++--
 .../open-next/src/core/createMainHandler.ts   |  2 +-
 packages/open-next/src/core/routing/util.ts   | 31 ++++++-
 packages/open-next/src/core/routingHandler.ts |  2 -
 packages/open-next/src/plugins/replacement.ts |  3 +-
 packages/open-next/src/types/open-next.ts     |  2 +-
 packages/open-next/src/wrappers/aws-lambda.ts |  2 +-
 13 files changed, 140 insertions(+), 26 deletions(-)

diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
index a5c59dd62..e2c561b7d 100644
--- a/packages/open-next/src/adapters/middleware.ts
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -1,5 +1,6 @@
 import { InternalEvent } from "types/open-next";
 
+import { debug } from "../adapters/logger";
 import { createGenericHandler } from "../core/createGenericHandler";
 import routingHandler from "../core/routingHandler";
 
@@ -8,6 +9,7 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
   // We should probably create an host resolver to redirect correctly
   const result = await routingHandler(internalEvent);
   if ("internalEvent" in result) {
+    debug("Middleware intercepted event", internalEvent);
     return {
       type: "middleware",
       internalEvent: result.internalEvent,
@@ -15,6 +17,7 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
       isExternalRewrite: result.isExternalRewrite,
     };
   } else {
+    debug("Middleware response", result);
     return result;
   }
 };
diff --git a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
index b5e879dbc..5c6ac54ca 100644
--- a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
+++ b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
@@ -5,10 +5,21 @@ import { MiddlewareOutputEvent } from "../../../core/routingHandler";
 declare const internalEvent: InternalEvent;
 
 //#override withRouting
-// eslint-disable-next-line unused-imports/no-unused-vars
+const overwrittenResponseHeaders = Object.entries(internalEvent.headers).reduce(
+  (acc, [key, value]) => {
+    if (!key.startsWith("x-middleware-response-")) {
+      return acc;
+    }
+    return { ...acc, [key.replace("x-middleware-response-", "")]: value };
+  },
+  {},
+);
 const preprocessResult: MiddlewareOutputEvent = {
   internalEvent: internalEvent,
   isExternalRewrite: false,
-  headers: {},
+  headers: overwrittenResponseHeaders,
 };
 //#endOverride
+
+// We need to export something otherwise when compiled in js it creates an empty export {} inside the override
+export default {};
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 9d8d7d6d4..b3080b429 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -680,8 +680,8 @@ async function createMiddleware() {
         plugins: [
           openNextResolvePlugin({
             overrides: {
-              wrapper: "cloudflare",
-              converter: "edge",
+              wrapper: "aws-lambda",
+              converter: "aws-cloudfront",
             },
           }),
           openNextEdgePlugins({
@@ -703,6 +703,13 @@ async function createMiddleware() {
         },
         conditions: ["module"],
         mainFields: ["module", "main"],
+        banner: {
+          js: `
+    const require = (await import("node:module")).createRequire(import.meta.url);
+    const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
+    const __dirname = (await import("node:path")).dirname(__filename);
+    `,
+        },
       },
       options,
     );
diff --git a/packages/open-next/src/build/bundleNextServer.ts b/packages/open-next/src/build/bundleNextServer.ts
index aa44bab58..5c3f5f8fa 100644
--- a/packages/open-next/src/build/bundleNextServer.ts
+++ b/packages/open-next/src/build/bundleNextServer.ts
@@ -64,9 +64,7 @@ export async function bundleNextServer(outputDir: string, appPath: string) {
           // This was an attempt at reducing server bundle size
           // It might be the better way to go in the future
           build.onResolve({ filter: /\.\/module.compiled/ }, (args) => {
-            console.log(args);
             const dir = args.resolveDir.split("/").slice(-1);
-            console.log(dir);
             return {
               path: path.join(
                 "next/dist/compiled/next-server/",
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index e57bc98b3..036fc42c7 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -59,7 +59,6 @@ export async function createServerBundle(
       (file) => {
         if (file.endsWith("page.js") || file.endsWith("route.js")) {
           const route = `app/${file.replace(/\.js$/, "")}`;
-          // console.log(`Found remaining route: ${route}`);
           if (!foundRoutes.has(route)) {
             remainingRoutes.add(route);
           }
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index d42867c94..766276490 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -83,7 +83,7 @@ export async function generateOutput(
   if (isExternalMiddleware) {
     edgeFunctions.middleware = {
       bundle: ".open-next/middleware",
-      handler: "index.handler",
+      handler: "handler.handler",
     };
   }
   // Add edge functions
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index cb4bd40f1..c263e84f3 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -1,12 +1,21 @@
 import {
   CloudFrontHeaders,
+  CloudFrontRequest,
   CloudFrontRequestEvent,
   CloudFrontRequestResult,
 } from "aws-lambda";
+import { OutgoingHttpHeader } from "http";
 import type { Converter, InternalEvent, InternalResult } from "types/open-next";
 
 import { debug } from "../adapters/logger";
-import { convertToQuery } from "../core/routing/util";
+import {
+  convertRes,
+  convertToQuery,
+  convertToQueryString,
+  createServerResponse,
+  proxyRequest,
+} from "../core/routing/util";
+import { MiddlewareOutputEvent } from "../core/routingHandler";
 
 function normalizeCloudFrontRequestEventHeaders(
   rawHeaders: CloudFrontHeaders,
@@ -49,25 +58,88 @@ async function convertFromCloudFrontRequestEvent(
   };
 }
 
-function convertToCloudFrontRequestResult(
-  result: InternalResult,
-): CloudFrontRequestResult {
-  const headers: CloudFrontHeaders = {};
-  Object.entries(result.headers)
+type MiddlewareEvent = {
+  type: "middleware";
+} & MiddlewareOutputEvent;
+
+function convertToCloudfrontHeaders(
+  headers: Record<string, OutgoingHttpHeader>,
+) {
+  const cloudfrontHeaders: CloudFrontHeaders = {};
+  Object.entries(headers)
     .filter(([key]) => key.toLowerCase() !== "content-length")
     .forEach(([key, value]) => {
-      headers[key] = [
-        ...(headers[key] || []),
+      cloudfrontHeaders[key] = [
+        ...(cloudfrontHeaders[key] || []),
         ...(Array.isArray(value)
           ? value.map((v) => ({ key, value: v }))
           : [{ key, value: value.toString() }]),
       ];
     });
+  return cloudfrontHeaders;
+}
+
+async function convertToCloudFrontRequestResult(
+  result: InternalResult | MiddlewareEvent,
+  originalRequest: CloudFrontRequestEvent,
+): Promise<CloudFrontRequestResult> {
+  let responseHeaders =
+    result.type === "middleware"
+      ? result.internalEvent.headers
+      : result.headers;
+  if (result.type === "middleware") {
+    const { method, clientIp, origin } = originalRequest.Records[0].cf.request;
+    const overwrittenResponseHeaders: Record<string, OutgoingHttpHeader> = {};
+    Object.entries(result.headers).forEach(([key, value]) => {
+      //TODO: handle those headers inside plugin
+      if (value)
+        overwrittenResponseHeaders[`x-middleware-response-${key}`] = value;
+    });
+
+    // Handle external rewrite
+    if (result.isExternalRewrite) {
+      const serverResponse = createServerResponse(result.internalEvent, {});
+      await proxyRequest(result.internalEvent, serverResponse);
+      const externalResult = convertRes(serverResponse);
+      debug("externalResult", {
+        status: externalResult.statusCode.toString(),
+        statusDescription: "OK",
+        headers: convertToCloudfrontHeaders(externalResult.headers),
+        bodyEncoding: externalResult.isBase64Encoded ? "base64" : "text",
+        body: externalResult.body,
+      });
+      return {
+        status: externalResult.statusCode.toString(),
+        statusDescription: "OK",
+        headers: convertToCloudfrontHeaders(externalResult.headers),
+        bodyEncoding: externalResult.isBase64Encoded ? "base64" : "text",
+        body: externalResult.body,
+      };
+    }
+
+    const response: CloudFrontRequest = {
+      clientIp,
+      method,
+      uri: result.internalEvent.rawPath,
+      querystring: convertToQueryString(result.internalEvent.query).replace(
+        "?",
+        "",
+      ),
+      headers: convertToCloudfrontHeaders({
+        ...responseHeaders,
+        ...overwrittenResponseHeaders,
+      }),
+      origin,
+    };
+    debug("response rewrite", response);
+
+    return response;
+  }
 
   const response: CloudFrontRequestResult = {
     status: result.statusCode.toString(),
     statusDescription: "OK",
-    headers,
+    headers: convertToCloudfrontHeaders(responseHeaders),
     bodyEncoding: result.isBase64Encoded ? "base64" : "text",
     body: result.body,
   };
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 1bd00e704..3425c6467 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -3,7 +3,7 @@ import type { BuildOptions, OverrideOptions } from "types/open-next";
 import { debug } from "../adapters/logger";
 import type { IncrementalCache } from "../cache/incremental/types";
 import type { Queue } from "../queue/types";
-import { openNextHandler } from "./requestHandler";
+import { openNextHandler } from "./requestHandler.js";
 import { resolveConverter, resolveTagCache, resolveWrapper } from "./resolve";
 
 declare global {
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index 9baa472bf..c49ae9429 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -121,6 +121,28 @@ export function unescapeRegex(str: string) {
   return path;
 }
 
+function filterHeadersForProxy(
+  headers: Record<string, string | string[] | undefined>,
+) {
+  const filteredHeaders: Record<string, string | string[]> = {};
+  const disallowedHeaders = [
+    "host",
+    "connection",
+    "via",
+    "x-cache",
+    "transfer-encoding",
+  ];
+  Object.entries(headers).forEach(([key, value]) => {
+    const lowerKey = key.toLowerCase();
+    if (disallowedHeaders.includes(lowerKey) || lowerKey.startsWith("x-amz"))
+      return;
+    else {
+      filteredHeaders[key] = value?.toString() ?? "";
+    }
+  });
+  return filteredHeaders;
+}
+
 export async function proxyRequest(
   internalEvent: InternalEvent,
   res: OpenNextNodeResponse,
@@ -128,7 +150,7 @@ export async function proxyRequest(
   const { url, headers, method, body } = internalEvent;
   debug("proxyRequest", url);
   await new Promise<void>((resolve, reject) => {
-    const { host: _host, ...filteredHeaders } = headers;
+    const filteredHeaders = filterHeadersForProxy(headers);
     debug("filteredHeaders", filteredHeaders);
     const req = request(
       url,
@@ -138,7 +160,10 @@ export async function proxyRequest(
         rejectUnauthorized: false,
       },
       (_res) => {
-        res.writeHead(_res.statusCode ?? 200, _res.headers);
+        res.writeHead(
+          _res.statusCode ?? 200,
+          filterHeadersForProxy(_res.headers),
+        );
         if (_res.headers["content-encoding"] === "br") {
           _res.pipe(require("node:zlib").createBrotliDecompress()).pipe(res);
         } else if (_res.headers["content-encoding"] === "gzip") {
@@ -152,7 +177,7 @@ export async function proxyRequest(
           res.end();
           reject(e);
         });
-        res.on("end", () => {
+        _res.on("end", () => {
           resolve();
         });
       },
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index 9ac75c6bc..08c4d134c 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -1,5 +1,3 @@
-//TODO: Replace this with injected values in case of external routing
-// We could use open-next plugins to replace these values at build time
 import {
   BuildId,
   ConfigHeaders,
diff --git a/packages/open-next/src/plugins/replacement.ts b/packages/open-next/src/plugins/replacement.ts
index 60279ed33..3f6d3bd8c 100644
--- a/packages/open-next/src/plugins/replacement.ts
+++ b/packages/open-next/src/plugins/replacement.ts
@@ -84,7 +84,8 @@ export function openNextReplacementPlugin({
               const replacement = match[2];
               const id = match[1];
               const pattern = new RegExp(
-                `\/\/#override (${id})\n([\\s\\S]*?)\n\/\/#endOverride`,
+                `\/\/#override (${id})\n([\\s\\S]*?)\/\/#endOverride`,
+                "g",
               );
               logger.debug(
                 `Open-next plugin ${name} -- Applying override for ${id} from ${fp}`,
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 7b31f2ff7..a1b34a5e2 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -55,7 +55,7 @@ export type Converter<
   R extends BaseEventOrResult = InternalResult,
 > = {
   convertFrom: (event: any) => Promise<E>;
-  convertTo: (result: R) => any;
+  convertTo: (result: R, originalRequest?: any) => any;
 };
 
 export type WrapperHandler<
diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index 7f894709a..377653e3a 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -28,7 +28,7 @@ const handler: WrapperHandler =
 
     const response = await handler(internalEvent);
 
-    return converter.convertTo(response);
+    return converter.convertTo(response, event);
   };
 
 export default {

From 6c1cbcfc21baf9d160607f2afe28ebc8dfb8ce32 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 6 Dec 2023 14:44:57 +0100
Subject: [PATCH 030/102] fix image adapter rebase

---
 .../open-next/src/adapters/config/index.ts    | 19 +++---
 packages/open-next/src/core/routing/util.ts   | 60 ++++++++++++++++++-
 2 files changed, 69 insertions(+), 10 deletions(-)

diff --git a/packages/open-next/src/adapters/config/index.ts b/packages/open-next/src/adapters/config/index.ts
index e11dadda3..d968bb0a3 100644
--- a/packages/open-next/src/adapters/config/index.ts
+++ b/packages/open-next/src/adapters/config/index.ts
@@ -19,12 +19,15 @@ export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");
 debug({ NEXT_DIR, OPEN_NEXT_DIR });
 
 //TODO: inject these values at build time
-export const NextConfig = loadConfig(NEXT_DIR);
-export const BuildId = loadBuildId(NEXT_DIR);
-export const HtmlPages = loadHtmlPages(NEXT_DIR);
+export const NextConfig = /* @__PURE__ */ loadConfig(NEXT_DIR);
+export const BuildId = /* @__PURE__ */ loadBuildId(NEXT_DIR);
+export const HtmlPages = /* @__PURE__ */ loadHtmlPages(NEXT_DIR);
 // export const PublicAssets = loadPublicAssets(OPEN_NEXT_DIR);
-export const RoutesManifest = loadRoutesManifest(NEXT_DIR);
-export const ConfigHeaders = loadConfigHeaders(NEXT_DIR);
-export const PrerenderManifest = loadPrerenderManifest(NEXT_DIR);
-export const AppPathsManifestKeys = loadAppPathsManifestKeys(NEXT_DIR);
-export const MiddlewareManifest = loadMiddlewareManifest(NEXT_DIR);
+export const RoutesManifest = /* @__PURE__ */ loadRoutesManifest(NEXT_DIR);
+export const ConfigHeaders = /* @__PURE__ */ loadConfigHeaders(NEXT_DIR);
+export const PrerenderManifest =
+  /* @__PURE__ */ loadPrerenderManifest(NEXT_DIR);
+export const AppPathsManifestKeys =
+  /* @__PURE__ */ loadAppPathsManifestKeys(NEXT_DIR);
+export const MiddlewareManifest =
+  /* @__PURE__ */ loadMiddlewareManifest(NEXT_DIR);
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index c49ae9429..bb4be4570 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -12,6 +12,10 @@ import { InternalEvent } from "types/open-next.js";
 import { isBinaryContentType } from "../../adapters/binary.js";
 import { debug, error } from "../../adapters/logger.js";
 
+/**
+ *
+ * @__PURE__
+ */
 export function isExternal(url?: string, host?: string) {
   if (!url) return false;
   const pattern = /^https?:\/\//;
@@ -21,6 +25,10 @@ export function isExternal(url?: string, host?: string) {
   return pattern.test(url);
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function getUrlParts(url: string, isExternal: boolean) {
   // NOTE: when redirect to a URL that contains search query params,
   // compile breaks b/c it does not allow for the '?' character
@@ -42,6 +50,10 @@ export function getUrlParts(url: string, isExternal: boolean) {
   };
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function convertRes(res: OpenNextNodeResponse) {
   // Format Next.js response to Lambda response
   const statusCode = res.statusCode || 200;
@@ -65,6 +77,7 @@ export function convertRes(res: OpenNextNodeResponse) {
  * Make sure that multi-value query parameters are transformed to
  * ?key=value1&key=value2&... so that Next converts those parameters
  * to an array when reading the query parameters
+ * @__PURE__
  */
 export function convertToQueryString(query: Record<string, string | string[]>) {
   const urlQuery = new URLSearchParams();
@@ -83,24 +96,34 @@ export function convertToQueryString(query: Record<string, string | string[]>) {
 /**
  * Given a raw query string, returns a record with key value-array pairs
  * similar to how multiValueQueryStringParameters are structured
+ * @__PURE__
  */
 export function convertToQuery(querystring: string) {
   const query = new URLSearchParams(querystring);
-  const queryObject: Record<string, string[]> = {};
+  const queryObject: Record<string, string[] | string> = {};
 
   for (const key of query.keys()) {
-    queryObject[key] = query.getAll(key);
+    const queries = query.getAll(key);
+    queryObject[key] = queries.length > 1 ? queries : queries[0];
   }
 
   return queryObject;
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function getMiddlewareMatch(middlewareManifest: MiddlewareManifest) {
   const rootMiddleware = middlewareManifest.middleware["/"];
   if (!rootMiddleware?.matchers) return [];
   return rootMiddleware.matchers.map(({ regexp }) => new RegExp(regexp));
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function escapeRegex(str: string) {
   let path = str.replace(/\(\.\)/g, "_µ1_");
 
@@ -111,6 +134,10 @@ export function escapeRegex(str: string) {
   return path;
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function unescapeRegex(str: string) {
   let path = str.replace(/_µ1_/g, "(.)");
 
@@ -121,6 +148,10 @@ export function unescapeRegex(str: string) {
   return path;
 }
 
+/**
+ *
+ * @__PURE__
+ */
 function filterHeadersForProxy(
   headers: Record<string, string | string[] | undefined>,
 ) {
@@ -131,6 +162,7 @@ function filterHeadersForProxy(
     "via",
     "x-cache",
     "transfer-encoding",
+    "content-encoding",
   ];
   Object.entries(headers).forEach(([key, value]) => {
     const lowerKey = key.toLowerCase();
@@ -143,6 +175,10 @@ function filterHeadersForProxy(
   return filteredHeaders;
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export async function proxyRequest(
   internalEvent: InternalEvent,
   res: OpenNextNodeResponse,
@@ -204,6 +240,10 @@ enum CommonHeaders {
   NEXT_CACHE = "x-nextjs-cache",
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function fixCacheHeaderForHtmlPages(
   rawPath: string,
   headers: OutgoingHttpHeaders,
@@ -215,6 +255,10 @@ export function fixCacheHeaderForHtmlPages(
   }
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
   // WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
   let cacheControl = headers[CommonHeaders.CACHE_CONTROL];
@@ -229,6 +273,10 @@ export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
   );
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
   headers["X-OpenNext"] = "1";
   if (globalThis.openNextDebug) {
@@ -236,6 +284,10 @@ export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
   }
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export async function revalidateIfRequired(
   host: string,
   rawPath: string,
@@ -339,6 +391,10 @@ function cyrb128(str: string) {
   return h1 >>> 0;
 }
 
+/**
+ *
+ * @__PURE__
+ */
 export function fixISRHeaders(headers: OutgoingHttpHeaders) {
   if (headers[CommonHeaders.NEXT_CACHE] === "REVALIDATED") {
     headers[CommonHeaders.CACHE_CONTROL] =

From 23bdd785c1145311b342a6b61f170971e1284e8e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 7 Dec 2023 14:29:09 +0100
Subject: [PATCH 031/102] couple of fix for node

---
 packages/open-next/src/converters/node.ts |  2 +-
 packages/open-next/src/types/open-next.ts | 10 +++++-----
 packages/open-next/src/wrappers/node.ts   | 10 ++++++++--
 3 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/packages/open-next/src/converters/node.ts b/packages/open-next/src/converters/node.ts
index 112bef7c4..84ee58961 100644
--- a/packages/open-next/src/converters/node.ts
+++ b/packages/open-next/src/converters/node.ts
@@ -20,7 +20,7 @@ const converter: Converter = {
       type: "core",
       method: req.method ?? "GET",
       rawPath: url.pathname,
-      url: url.toString(),
+      url: url.pathname + url.search,
       body,
       headers: Object.fromEntries(
         Object.entries(req.headers ?? {})
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index a1b34a5e2..ec01d4c4d 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -1,11 +1,11 @@
 import type { Readable } from "node:stream";
 
-import { StreamCreator } from "http/index.js";
+import type { StreamCreator } from "http/index.js";
 
-import { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
-import { IncrementalCache } from "../cache/incremental/types";
-import { TagCache } from "../cache/tag/types";
-import { Queue } from "../queue/types";
+import type { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
+import type { IncrementalCache } from "../cache/incremental/types";
+import type { TagCache } from "../cache/tag/types";
+import type { Queue } from "../queue/types";
 
 export type BaseEventOrResult<T extends string = string> = {
   type: T;
diff --git a/packages/open-next/src/wrappers/node.ts b/packages/open-next/src/wrappers/node.ts
index 08f8f0aee..2aeff3589 100644
--- a/packages/open-next/src/wrappers/node.ts
+++ b/packages/open-next/src/wrappers/node.ts
@@ -18,8 +18,14 @@ const wrapper: WrapperHandler = async (handler, converter) => {
         // Is it necessary to do something here?
       },
     };
-
-    await handler(internalEvent, _res);
+    if (internalEvent.rawPath === "/__health") {
+      res.writeHead(200, {
+        "Content-Type": "text/plain",
+      });
+      res.end("OK");
+    } else {
+      await handler(internalEvent, _res);
+    }
   });
 
   await new Promise<void>((resolve) => {

From d4aa85fcb9c5d7a2f9b8ffa5d898d0253e21f01a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 7 Dec 2023 17:00:51 +0100
Subject: [PATCH 032/102] package version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index da3ab54b0..5ced12a1d 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "2.3.3",
+  "version": "3.0.0-rc.1",
   "bin": {
     "open-next": "./dist/index.js"
   },

From cc9dcbbaadc6793d6866220e32ab892c84dbb041 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Mon, 11 Dec 2023 11:42:36 +0100
Subject: [PATCH 033/102] support for warmer with splitted fn

---
 .../open-next/src/adapters/warmer-function.ts | 125 +++++++++---------
 .../open-next/src/core/createMainHandler.ts   |   4 +
 packages/open-next/src/types/open-next.ts     |  12 +-
 .../src/wrappers/aws-lambda-streaming.ts      |  18 ++-
 packages/open-next/src/wrappers/aws-lambda.ts |  18 ++-
 5 files changed, 105 insertions(+), 72 deletions(-)

diff --git a/packages/open-next/src/adapters/warmer-function.ts b/packages/open-next/src/adapters/warmer-function.ts
index 95a1d6224..141822764 100644
--- a/packages/open-next/src/adapters/warmer-function.ts
+++ b/packages/open-next/src/adapters/warmer-function.ts
@@ -1,13 +1,7 @@
-import { InvokeCommand, LambdaClient } from "@aws-sdk/client-lambda";
-
 import { createGenericHandler } from "../core/createGenericHandler.js";
 import { debug, error } from "./logger.js";
 import { generateUniqueId } from "./util.js";
 
-const lambda = new LambdaClient({});
-const FUNCTION_NAME = process.env.FUNCTION_NAME!;
-const CONCURRENCY = parseInt(process.env.CONCURRENCY!);
-
 export interface WarmerEvent {
   type: "warmer";
   warmerId: string;
@@ -27,49 +21,74 @@ const resolveWarmerInvoke = async () => {
     return await openNextParams.invokeFunction();
   } else {
     return Promise.resolve(async (warmerId: string) => {
-      const ret = await Promise.all(
-        Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
-          try {
-            return lambda.send(
-              new InvokeCommand({
-                FunctionName: FUNCTION_NAME,
-                InvocationType: "RequestResponse",
-                Payload: Buffer.from(
-                  JSON.stringify({
-                    type: "warmer",
-                    warmerId,
-                    index: i,
-                    concurrency: CONCURRENCY,
-                    delay: 75,
-                  } satisfies WarmerEvent),
-                ),
-              }),
-            );
-          } catch (e) {
-            error(`failed to warm up #${i}`, e);
-            // ignore error
-          }
-        }),
+      const { InvokeCommand, LambdaClient } = await import(
+        "@aws-sdk/client-lambda"
       );
+      const lambda = new LambdaClient({});
+      const warmParams = JSON.parse(process.env.WARM_PARAMS!) as {
+        concurrency: number;
+        function: string;
+      }[];
+
+      for (const warmParam of warmParams) {
+        const { concurrency: CONCURRENCY, function: FUNCTION_NAME } = warmParam;
+        debug({
+          event: "warmer invoked",
+          functionName: FUNCTION_NAME,
+          concurrency: CONCURRENCY,
+          warmerId,
+        });
+        const ret = await Promise.all(
+          Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
+            try {
+              return lambda.send(
+                new InvokeCommand({
+                  FunctionName: FUNCTION_NAME,
+                  InvocationType: "RequestResponse",
+                  Payload: Buffer.from(
+                    JSON.stringify({
+                      type: "warmer",
+                      warmerId,
+                      index: i,
+                      concurrency: CONCURRENCY,
+                      delay: 75,
+                    } satisfies WarmerEvent),
+                  ),
+                }),
+              );
+            } catch (e) {
+              error(`failed to warm up #${i}`, e);
+              // ignore error
+            }
+          }),
+        );
+
+        // Print status
 
-      // Print status
+        const warmedServerIds = ret
+          .map((r, i) => {
+            if (r?.StatusCode !== 200 || !r?.Payload) {
+              error(`failed to warm up #${i}:`, r?.Payload?.toString());
+              return;
+            }
+            const payload = JSON.parse(
+              Buffer.from(r.Payload).toString(),
+            ) as WarmerResponse;
+            return {
+              statusCode: r.StatusCode,
+              payload,
+              type: "warmer" as const,
+            };
+          })
+          .filter((r): r is Exclude<typeof r, undefined> => !!r);
 
-      return ret
-        .map((r, i) => {
-          if (r?.StatusCode !== 200 || !r?.Payload) {
-            error(`failed to warm up #${i}:`, r?.Payload?.toString());
-            return;
-          }
-          const payload = JSON.parse(
-            Buffer.from(r.Payload).toString(),
-          ) as WarmerResponse;
-          return {
-            statusCode: r.StatusCode,
-            payload,
-            type: "warmer" as const,
-          };
-        })
-        .filter((r): r is Exclude<typeof r, undefined> => !!r);
+        debug({
+          event: "warmer result",
+          sent: CONCURRENCY,
+          success: warmedServerIds.length,
+          uniqueServersWarmed: [...new Set(warmedServerIds)].length,
+        });
+      }
     });
   }
 };
@@ -81,23 +100,11 @@ export const handler = await createGenericHandler({
 
 async function defaultHandler() {
   const warmerId = `warmer-${generateUniqueId()}`;
-  debug({
-    event: "warmer invoked",
-    functionName: FUNCTION_NAME,
-    concurrency: CONCURRENCY,
-    warmerId,
-  });
 
   const invokeFn = await resolveWarmerInvoke();
 
-  const warmedServerIds = await invokeFn(warmerId);
+  await invokeFn(warmerId);
 
-  debug({
-    event: "warmer result",
-    sent: CONCURRENCY,
-    success: warmedServerIds.length,
-    uniqueServersWarmed: [...new Set(warmedServerIds)].length,
-  });
   return {
     type: "warmer",
   };
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 3425c6467..8f1e635d3 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -1,6 +1,7 @@
 import type { BuildOptions, OverrideOptions } from "types/open-next";
 
 import { debug } from "../adapters/logger";
+import { generateUniqueId } from "../adapters/util";
 import type { IncrementalCache } from "../cache/incremental/types";
 import type { Queue } from "../queue/types";
 import { openNextHandler } from "./requestHandler.js";
@@ -10,6 +11,7 @@ declare global {
   var queue: Queue;
   var incrementalCache: IncrementalCache;
   var fnName: string | undefined;
+  var serverId: string;
 }
 
 async function resolveQueue(queue: OverrideOptions["queue"]) {
@@ -48,6 +50,8 @@ export async function createMainHandler() {
     ? config.functions[globalThis.fnName]
     : config.default;
 
+  globalThis.serverId = generateUniqueId();
+
   // Default queue
   globalThis.queue = await resolveQueue(thisFunction.override?.queue);
 
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index ec01d4c4d..71293831e 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -75,15 +75,7 @@ export type Wrapper<
   supportStreaming: boolean;
 };
 
-type Warmer = (warmerId: string) => Promise<
-  {
-    statusCode: number;
-    payload: {
-      serverId: string;
-    };
-    type: "warmer";
-  }[]
->;
+type Warmer = (warmerId: string) => Promise<void>;
 
 type ImageLoader = (url: string) => Promise<{
   body?: Readable;
@@ -214,6 +206,7 @@ export interface SplittedFunctionOptions extends FunctionOptions {
   patterns: string[];
 }
 
+//TODO: rename to OpenNextConfig or something similar
 export interface BuildOptions {
   default: FunctionOptions;
   functions: Record<string, SplittedFunctionOptions>;
@@ -222,7 +215,6 @@ export interface BuildOptions {
    * Override the default middleware
    * If you set this options, the middleware need to be deployed separately.
    * It supports both edge and node runtime.
-   * TODO: actually implement it
    * @default undefined
    */
   middleware?: DefaultFunctionOptions & {
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 3d60af0bb..3e2c76a0f 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -6,14 +6,30 @@ import { StreamCreator } from "http/index.js";
 import { WrapperHandler } from "types/open-next";
 
 import { error } from "../adapters/logger";
-import { WarmerEvent } from "../adapters/warmer-function";
+import { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
 
 type AwsLambdaEvent = APIGatewayProxyEventV2 | WarmerEvent;
 
 type AwsLambdaReturn = void;
+
+function formatWarmerResponse(event: WarmerEvent) {
+  const result = new Promise<WarmerResponse>((resolve) => {
+    setTimeout(() => {
+      resolve({ serverId, type: "warmer" } satisfies WarmerResponse);
+    }, event.delay);
+  });
+  return result;
+}
+
 const handler: WrapperHandler = async (handler, converter) =>
   awslambda.streamifyResponse(
     async (event: AwsLambdaEvent, responseStream): Promise<AwsLambdaReturn> => {
+      if ("type" in event) {
+        const result = await formatWarmerResponse(event);
+        responseStream.end(Buffer.from(JSON.stringify(result)), "utf-8");
+        return;
+      }
+
       const internalEvent = await converter.convertFrom(event);
       let _hasWriten = false;
       let _headersSent = false;
diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index 377653e3a..3477e9ff0 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -8,7 +8,7 @@ import type {
 } from "aws-lambda";
 import type { WrapperHandler } from "types/open-next";
 
-import { WarmerEvent } from "../adapters/warmer-function";
+import { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
 
 type AwsLambdaEvent =
   | APIGatewayProxyEventV2
@@ -19,11 +19,25 @@ type AwsLambdaEvent =
 type AwsLambdaReturn =
   | APIGatewayProxyResultV2
   | APIGatewayProxyResult
-  | CloudFrontRequestResult;
+  | CloudFrontRequestResult
+  | WarmerResponse;
+
+function formatWarmerResponse(event: WarmerEvent) {
+  return new Promise<WarmerResponse>((resolve) => {
+    setTimeout(() => {
+      resolve({ serverId, type: "warmer" } satisfies WarmerResponse);
+    }, event.delay);
+  });
+}
 
 const handler: WrapperHandler =
   async (handler, converter) =>
   async (event: AwsLambdaEvent): Promise<AwsLambdaReturn> => {
+    // Handle warmer event
+    if ("type" in event) {
+      return formatWarmerResponse(event);
+    }
+
     const internalEvent = await converter.convertFrom(event);
 
     const response = await handler(internalEvent);

From 7c4145dce4af3ad1e1acb6e05ba65743737bcbb1 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 12 Dec 2023 16:43:20 +0100
Subject: [PATCH 034/102] basic support for edge runtime

There is some restriction:
Only 1 route per function
Support only app route and page
No streaming
---
 .../open-next/src/adapters/edge-adapter.ts    |  73 +++++++++
 packages/open-next/src/build.ts               | 149 ++++--------------
 .../open-next/src/build/createServerBundle.ts |   9 +-
 .../src/build/edge/createEdgeBundle.ts        | 138 ++++++++++++++++
 .../open-next/src/build/generateOutput.ts     |   4 +-
 .../open-next/src/core/edgeFunctionHandler.ts |  46 ++----
 .../open-next/src/core/routing/middleware.ts  |  51 ++----
 packages/open-next/src/core/routing/util.ts   |  18 +++
 packages/open-next/src/plugins/edge.ts        |  18 ++-
 packages/open-next/src/types/next-types.ts    |  28 ++--
 10 files changed, 319 insertions(+), 215 deletions(-)
 create mode 100644 packages/open-next/src/adapters/edge-adapter.ts
 create mode 100644 packages/open-next/src/build/edge/createEdgeBundle.ts

diff --git a/packages/open-next/src/adapters/edge-adapter.ts b/packages/open-next/src/adapters/edge-adapter.ts
new file mode 100644
index 000000000..57b3ab028
--- /dev/null
+++ b/packages/open-next/src/adapters/edge-adapter.ts
@@ -0,0 +1,73 @@
+import { InternalEvent, InternalResult } from "types/open-next";
+
+// We import it like that so that the edge plugin can replace it
+import { NextConfig } from "../adapters/config";
+import { createGenericHandler } from "../core/createGenericHandler";
+import {
+  convertBodyToReadableStream,
+  convertToQueryString,
+} from "../core/routing/util";
+
+const defaultHandler = async (
+  internalEvent: InternalEvent,
+): Promise<InternalResult> => {
+  // TODO: We need to handle splitted function here
+  // We should probably create an host resolver to redirect correctly
+
+  const host = internalEvent.headers.host
+    ? `https://${internalEvent.headers.host}`
+    : "http://localhost:3000";
+  const initialUrl = new URL(internalEvent.rawPath, host);
+  initialUrl.search = convertToQueryString(internalEvent.query);
+  const url = initialUrl.toString();
+
+  // @ts-expect-error - This is bundled
+  const handler = await import(`./middleware.mjs`);
+
+  const response: Response = await handler.default({
+    headers: internalEvent.headers,
+    method: internalEvent.method || "GET",
+    nextConfig: {
+      basePath: NextConfig.basePath,
+      i18n: NextConfig.i18n,
+      trailingSlash: NextConfig.trailingSlash,
+    },
+    url,
+    body: convertBodyToReadableStream(internalEvent.method, internalEvent.body),
+  });
+
+  const arrayBuffer = await response.arrayBuffer();
+  const buffer = Buffer.from(arrayBuffer);
+
+  const responseHeaders: Record<string, string | string[]> = {};
+  response.headers.forEach((value, key) => {
+    if (key.toLowerCase() === "set-cookie") {
+      responseHeaders[key] = responseHeaders[key]
+        ? [...responseHeaders[key], value]
+        : [value];
+    } else {
+      responseHeaders[key] = value;
+    }
+  });
+  console.log("responseHeaders", responseHeaders);
+  const body = buffer.toString();
+  console.log("body", body);
+
+  return {
+    type: "core",
+    statusCode: response.status,
+    headers: responseHeaders,
+    body: body,
+    // Do we need to handle base64 encoded response?
+    isBase64Encoded: false,
+  };
+};
+
+export const handler = await createGenericHandler({
+  handler: defaultHandler,
+  type: "middleware",
+});
+
+export default {
+  fetch: handler,
+};
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index b3080b429..768c50ed3 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -4,8 +4,10 @@ import path from "node:path";
 import url from "node:url";
 
 import { buildSync } from "esbuild";
+import { MiddlewareManifest } from "types/next-types.js";
 
 import { createServerBundle } from "./build/createServerBundle.js";
+import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js";
 import { generateOutput } from "./build/generateOutput.js";
 import {
   esbuildAsync,
@@ -19,7 +21,6 @@ import {
 } from "./build/helper.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
-import { openNextEdgePlugins } from "./plugins/edge.js";
 import { openNextResolvePlugin } from "./plugins/resolve.js";
 import { BuildOptions } from "./types/open-next.js";
 
@@ -639,8 +640,7 @@ function compileCache(options: Options) {
 async function createMiddleware() {
   console.info(`Bundling middleware function...`);
 
-  const { appBuildOutputPath, outputDir, externalMiddleware, tempDir } =
-    options;
+  const { appBuildOutputPath, outputDir, externalMiddleware } = options;
 
   // Get middleware manifest
   const middlewareManifest = JSON.parse(
@@ -648,7 +648,7 @@ async function createMiddleware() {
       path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
       "utf8",
     ),
-  );
+  ) as MiddlewareManifest;
 
   const entry = middlewareManifest.middleware["/"];
   if (!entry) {
@@ -657,6 +657,20 @@ async function createMiddleware() {
 
   // Create output folder
   let outputPath = path.join(outputDir, "server-function");
+
+  const commonMiddlewareOptions = {
+    files: entry.files,
+    routes: [
+      {
+        name: entry.name || "/",
+        page: entry.page,
+        regex: entry.matchers.map((m) => m.regexp),
+      },
+    ],
+    options,
+    appBuildOutputPath,
+  };
+
   if (externalMiddleware) {
     outputPath = path.join(outputDir, "middleware");
     fs.mkdirSync(outputPath, { recursive: true });
@@ -668,96 +682,21 @@ async function createMiddleware() {
     );
 
     // Bundle middleware
-    await esbuildAsync(
-      {
-        entryPoints: [path.join(__dirname, "adapters", "middleware.js")],
-        // inject: ,
-        bundle: true,
-        outfile: path.join(outputPath, "handler.mjs"),
-        external: ["node:*", "next", "@aws-sdk/*"],
-        target: "es2022",
-        platform: "neutral",
-        plugins: [
-          openNextResolvePlugin({
-            overrides: {
-              wrapper: "aws-lambda",
-              converter: "aws-cloudfront",
-            },
-          }),
-          openNextEdgePlugins({
-            entryFiles: entry.files.map((file: string) =>
-              path.join(appBuildOutputPath, ".next", file),
-            ),
-            nextDir: path.join(appBuildOutputPath, ".next"),
-            edgeFunctionHandlerPath: path.join(
-              __dirname,
-              "core",
-              "edgeFunctionHandler.js",
-            ),
-          }),
-        ],
-        treeShaking: true,
-        alias: {
-          path: "node:path",
-          stream: "node:stream",
-        },
-        conditions: ["module"],
-        mainFields: ["module", "main"],
-        banner: {
-          js: `
-    const require = (await import("node:module")).createRequire(import.meta.url);
-    const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
-    const __dirname = (await import("node:path")).dirname(__filename);
-    `,
-        },
-      },
-      options,
-    );
+    await buildEdgeBundle({
+      entrypoint: path.join(__dirname, "adapters", "middleware.js"),
+      outfile: path.join(outputPath, "handler.mjs"),
+      ...commonMiddlewareOptions,
+      defaultConverter: "aws-cloudfront",
+    });
   } else {
-    buildEdgeFunction(
-      entry,
-      path.join(__dirname, "core", "edgeFunctionHandler.js"),
-      path.join(outputDir, ".build", "middleware.mjs"),
-      appBuildOutputPath,
-    );
+    await buildEdgeBundle({
+      entrypoint: path.join(__dirname, "core", "edgeFunctionHandler.js"),
+      outfile: path.join(outputDir, ".build", "middleware.mjs"),
+      ...commonMiddlewareOptions,
+    });
   }
 }
 
-function buildEdgeFunction(
-  entry: any,
-  entrypoint: string,
-  outfile: string,
-  appBuildOutputPath: string,
-) {
-  esbuildSync(
-    {
-      entryPoints: [entrypoint],
-      inject: entry.files.map((file: string) =>
-        path.join(appBuildOutputPath, ".next", file),
-      ),
-      bundle: true,
-      outfile,
-      external: ["node:*"],
-      target: "es2022",
-      platform: "neutral",
-      banner: {
-        js: `
-globalThis._ENTRIES = {};
-globalThis.self = globalThis;
-
-import {Buffer} from "node:buffer";
-globalThis.Buffer = Buffer;
-
-import {AsyncLocalStorage} from "node:async_hooks";
-globalThis.AsyncLocalStorage = AsyncLocalStorage;
-
-`,
-      },
-    },
-    options,
-  );
-}
-
 //TODO: Why do we need this? People have access to the headers in the middleware
 function injectMiddlewareGeolocation(outputPath: string, packagePath: string) {
   // WORKAROUND: Set `NextRequest` geolocation data — https://github.com/serverless-stack/open-next#workaround-set-nextrequest-geolocation-data
@@ -793,33 +732,3 @@ function injectMiddlewareGeolocation(outputPath: string, packagePath: string) {
     );
   }
 }
-
-function addPublicFilesList(outputPath: string, packagePath: string) {
-  // Get a list of all files in /public
-  const { appPublicPath } = options;
-  const acc: PublicFiles = { files: [] };
-
-  function processDirectory(pathInPublic: string) {
-    const files = fs.readdirSync(path.join(appPublicPath, pathInPublic), {
-      withFileTypes: true,
-    });
-
-    for (const file of files) {
-      file.isDirectory()
-        ? processDirectory(path.join(pathInPublic, file.name))
-        : acc.files.push(path.posix.join(pathInPublic, file.name));
-    }
-  }
-
-  if (fs.existsSync(appPublicPath)) {
-    processDirectory("/");
-  }
-
-  // Save the list
-  const outputOpenNextPath = path.join(outputPath, packagePath, ".open-next");
-  fs.mkdirSync(outputOpenNextPath, { recursive: true });
-  fs.writeFileSync(
-    path.join(outputOpenNextPath, "public-files.json"),
-    JSON.stringify(acc),
-  );
-}
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 036fc42c7..74de666d3 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -11,6 +11,7 @@ import { openNextReplacementPlugin } from "../plugins/replacement.js";
 import { openNextResolvePlugin } from "../plugins/resolve.js";
 import { bundleNextServer } from "./bundleNextServer.js";
 import { copyTracedFiles } from "./copyTracedFiles.js";
+import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
 import type { Options } from "./helper.js";
 import { compareSemver, esbuildAsync, traverseFiles } from "./helper.js";
 
@@ -29,9 +30,15 @@ export async function createServerBundle(
   const promises = functions.map(async ([name, fnOptions]) => {
     const routes = fnOptions.routes;
     routes.forEach((route) => foundRoutes.add(route));
-    await generateBundle(name, buildRuntimeOptions, fnOptions);
+    if (fnOptions.runtime === "edge") {
+      await generateEdgeBundle(name, buildRuntimeOptions, fnOptions);
+    } else {
+      await generateBundle(name, buildRuntimeOptions, fnOptions);
+    }
   });
 
+  //TODO: throw an error if not all edge runtime routes has been bundled in a separate function
+
   // We build every other function than default before so we know which route there is left
   await Promise.all(promises);
 
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
new file mode 100644
index 000000000..d6c3d49bb
--- /dev/null
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -0,0 +1,138 @@
+import url from "node:url";
+
+import fs from "fs";
+import path from "path";
+import { MiddlewareManifest } from "types/next-types";
+import { BuildOptions, IncludedConverter } from "types/open-next";
+
+import { openNextEdgePlugins } from "../../plugins/edge.js";
+import { openNextResolvePlugin } from "../../plugins/resolve.js";
+import { esbuildAsync, Options } from "../helper.js";
+
+const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
+
+interface BuildEdgeBundleOptions {
+  appBuildOutputPath: string;
+  files: string[];
+  routes: {
+    name: string;
+    page: string;
+    regex: string[];
+  }[];
+  entrypoint: string;
+  outfile: string;
+  options: Options;
+  defaultConverter?: IncludedConverter;
+  additionalInject?: string;
+}
+
+export async function buildEdgeBundle({
+  appBuildOutputPath,
+  files,
+  routes,
+  entrypoint,
+  outfile,
+  options,
+  defaultConverter,
+  additionalInject,
+}: BuildEdgeBundleOptions) {
+  await esbuildAsync(
+    {
+      entryPoints: [entrypoint],
+      // inject: ,
+      bundle: true,
+      outfile,
+      external: ["node:*", "next", "@aws-sdk/*"],
+      target: "es2022",
+      platform: "neutral",
+      plugins: [
+        openNextResolvePlugin({
+          overrides: {
+            wrapper: "aws-lambda",
+            converter: defaultConverter,
+          },
+        }),
+        openNextEdgePlugins({
+          entryFiles: files.map((file: string) =>
+            path.join(appBuildOutputPath, ".next", file),
+          ),
+          routes,
+          nextDir: path.join(appBuildOutputPath, ".next"),
+          edgeFunctionHandlerPath: path.join(
+            __dirname,
+            "../../core",
+            "edgeFunctionHandler.js",
+          ),
+        }),
+      ],
+      treeShaking: true,
+      alias: {
+        path: "node:path",
+        stream: "node:stream",
+        fs: "node:fs",
+      },
+      conditions: ["module"],
+      mainFields: ["module", "main"],
+      banner: {
+        js: `
+  const require = (await import("node:module")).createRequire(import.meta.url);
+  const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
+  const __dirname = (await import("node:path")).dirname(__filename);
+  ${additionalInject ?? ""}
+  `,
+      },
+    },
+    options,
+  );
+}
+
+export async function generateEdgeBundle(
+  name: string,
+  options: Options,
+  fnOptions: BuildOptions["functions"][string],
+) {
+  const { appBuildOutputPath, outputDir } = options;
+
+  // Create output folder
+  const outputPath = path.join(outputDir, "server-functions", name);
+  fs.mkdirSync(outputPath, { recursive: true });
+
+  // Copy open-next.config.js
+  fs.copyFileSync(
+    path.join(outputDir, ".build", "open-next.config.js"),
+    path.join(outputPath, "open-next.config.js"),
+  );
+
+  // Load middleware manifest
+  const middlewareManifest = JSON.parse(
+    fs.readFileSync(
+      path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
+      "utf8",
+    ),
+  ) as MiddlewareManifest;
+
+  // Find functions
+  const functions = Object.values(middlewareManifest.functions).filter((fn) =>
+    fnOptions.routes.includes(fn.name),
+  );
+
+  if (functions.length > 1) {
+    throw new Error("Only one function is supported for now");
+  }
+  const fn = functions[0];
+
+  await buildEdgeBundle({
+    appBuildOutputPath,
+    files: fn.files,
+    routes: [
+      {
+        name: fn.name,
+        page: fn.page,
+        regex: fn.matchers.map((m) => m.regexp),
+      },
+    ],
+    entrypoint: path.join(__dirname, "../../adapters", "edge-adapter.js"),
+    outfile: path.join(outputPath, "index.mjs"),
+    options,
+  });
+}
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index 766276490..fd8685fca 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -88,7 +88,7 @@ export async function generateOutput(
   }
   // Add edge functions
   Object.entries(buildOptions.functions).forEach(([key, value]) => {
-    if (value.runtime === "edge") {
+    if (value.placement === "global") {
       edgeFunctions[key] = {
         bundle: `.open-next/functions/${key}`,
         handler: "index.handler",
@@ -144,7 +144,7 @@ export async function generateOutput(
   // Then add function origins
   await Promise.all(
     Object.entries(buildOptions.functions).map(async ([key, value]) => {
-      if (!value.runtime || value.runtime === "node") {
+      if (!value.placement || value.placement === "regional") {
         if (value.override?.generateDockerfile) {
           origins[key] = {
             type: "ecs",
diff --git a/packages/open-next/src/core/edgeFunctionHandler.ts b/packages/open-next/src/core/edgeFunctionHandler.ts
index 4a5962f4d..9924b9b45 100644
--- a/packages/open-next/src/core/edgeFunctionHandler.ts
+++ b/packages/open-next/src/core/edgeFunctionHandler.ts
@@ -37,25 +37,28 @@ interface Entries {
 }
 declare global {
   var _ENTRIES: Entries;
+  var _ROUTES: EdgeRoute[];
   var __storage__: Map<unknown, unknown>;
   var AsyncContext: any;
   //@ts-ignore
   var AsyncLocalStorage: any;
 }
 
-interface Route {
+export interface EdgeRoute {
   name: string;
   page: string;
-  regex: string;
+  regex: string[];
 }
 
+type EdgeRequest = Omit<RequestData, "page">;
+
 export default async function edgeFunctionHandler(
-  routes: Route[],
-  request: RequestData,
+  request: EdgeRequest,
 ): Promise<Response> {
   const path = new URL(request.url).pathname;
+  const routes = globalThis._ROUTES;
   const correspondingRoute = routes.find((route) =>
-    new RegExp(route.regex).test(path),
+    route.regex.some((r) => new RegExp(r).test(path)),
   );
 
   if (!correspondingRoute) {
@@ -67,10 +70,7 @@ export default async function edgeFunctionHandler(
   ].default({
     page: correspondingRoute.page,
     request: {
-      headers: request.headers,
-      method: request.method,
-      url: request.url,
-      signal: request.signal,
+      ...request,
       page: {
         name: correspondingRoute.name,
       },
@@ -80,31 +80,3 @@ export default async function edgeFunctionHandler(
   const response = result.response;
   return response;
 }
-
-// const route = "/ssr/page";
-
-// const toExport = {
-//   async fetch(req: Request, env: any, context: any) {
-//     const headers: Record<string, string> = {};
-//     req.headers.forEach((value, key) => {
-//       headers[key] = value;
-//     });
-//     const result = await self._ENTRIES[`middleware_app${route}`].default({
-//       page: route,
-//       request: {
-//         headers: headers,
-//         method: req.method,
-//         url: req.url,
-//         signal: req.signal,
-//         page: {
-//           name: route,
-//         },
-//       },
-//     });
-//     const response = result.response;
-//     context.waitUntil(result.waitUntil);
-//     return response;
-//   },
-// };
-
-// export default toExport;
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 94bf8a7ef..88b71651d 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -9,6 +9,7 @@ import { InternalEvent, InternalResult } from "types/open-next.js";
 //   signalFromNodeResponse,
 // } = require("next/dist/server/web/spec-extension/adapters/next-request");
 import {
+  convertBodyToReadableStream,
   convertToQueryString,
   getMiddlewareMatch,
   isExternal,
@@ -38,17 +39,6 @@ export async function handleMiddleware(
   // We bypass the middleware if the request is internal
   if (internalEvent.headers["x-isr"]) return internalEvent;
 
-  // const req = new IncomingMessage(internalEvent);
-  // const res = new OpenNextNodeResponse(
-  //   () => void 0,
-  //   () => Promise.resolve(),
-  // );
-
-  // NOTE: Next middleware was originally developed to support nested middlewares
-  // but that was discarded for simplicity. The MiddlewareInfo type still has the original
-  // structure, but as of now, the only useful property on it is the "/" key (ie root).
-  const middlewareInfo = middlewareManifest.middleware["/"];
-
   const host = internalEvent.headers.host
     ? `https://${internalEvent.headers.host}`
     : "http://localhost:3000";
@@ -56,39 +46,20 @@ export async function handleMiddleware(
   initialUrl.search = convertToQueryString(query);
   const url = initialUrl.toString();
 
-  const convertBodyToReadableStream = (body: string | Buffer) => {
-    const readable = new ReadableStream({
-      start(controller) {
-        controller.enqueue(body);
-        controller.close();
-      },
-    });
-    return readable;
-  };
-
   // @ts-expect-error - This is bundled
   const middleware = await import("./middleware.mjs");
 
-  const result: Response = await middleware.default(
-    [
-      {
-        name: middlewareInfo.name || "/",
-        page: middlewareInfo.page,
-        regex: middlewareInfo.matchers[0].regexp,
-      },
-    ],
-    {
-      headers: internalEvent.headers,
-      method: internalEvent.method || "GET",
-      nextConfig: {
-        basePath: NextConfig.basePath,
-        i18n: NextConfig.i18n,
-        trailingSlash: NextConfig.trailingSlash,
-      },
-      url,
-      body: convertBodyToReadableStream(internalEvent.body ?? ""),
+  const result: Response = await middleware.default({
+    headers: internalEvent.headers,
+    method: internalEvent.method || "GET",
+    nextConfig: {
+      basePath: NextConfig.basePath,
+      i18n: NextConfig.i18n,
+      trailingSlash: NextConfig.trailingSlash,
     },
-  );
+    url,
+    body: convertBodyToReadableStream(internalEvent.method, internalEvent.body),
+  });
   const statusCode = result.status;
 
   /* Apply override headers from middleware
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index bb4be4570..54fba4e10 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -175,6 +175,24 @@ function filterHeadersForProxy(
   return filteredHeaders;
 }
 
+/**
+ * @__PURE__
+ */
+export function convertBodyToReadableStream(
+  method: string,
+  body?: string | Buffer,
+) {
+  if (method === "GET" || method === "HEAD") return undefined;
+  if (!body) return undefined;
+  const readable = new ReadableStream({
+    start(controller) {
+      controller.enqueue(body);
+      controller.close();
+    },
+  });
+  return readable;
+}
+
 /**
  *
  * @__PURE__
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index 5f89f9c52..4103ecdaf 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -12,11 +12,13 @@ import {
   loadPrerenderManifest,
   loadRoutesManifest,
 } from "../adapters/config/util.js";
+import { EdgeRoute } from "../core/edgeFunctionHandler.js";
 
 export interface IPluginSettings {
   nextDir: string;
   edgeFunctionHandlerPath?: string;
   entryFiles: string[];
+  routes: EdgeRoute[];
 }
 
 /**
@@ -30,6 +32,7 @@ export function openNextEdgePlugins({
   nextDir,
   edgeFunctionHandlerPath,
   entryFiles,
+  routes,
 }: IPluginSettings): Plugin {
   return {
     name: "opennext-edge",
@@ -43,16 +46,27 @@ export function openNextEdgePlugins({
         });
       }
 
+      build.onResolve({ filter: /.mjs$/g }, (args) => {
+        return {
+          external: true,
+        };
+      });
+
       // We inject the entry files into the edgeFunctionHandler
       build.onLoad({ filter: /\/edgeFunctionHandler.js/g }, async (args) => {
         let contents = readFileSync(args.path, "utf-8");
         contents = `
 globalThis._ENTRIES = {};
 globalThis.self = globalThis;
-globalThis.process = {env: {}}
+if(!globalThis.process){
+  globalThis.process = {env: {}};
+}
+globalThis._ROUTES = ${JSON.stringify(routes)};
 
 import {Buffer} from "node:buffer";
 globalThis.Buffer = Buffer;
+import crypto from "node:crypto";
+globalThis.crypto = crypto;
 
 import {AsyncLocalStorage} from "node:async_hooks";
 globalThis.AsyncLocalStorage = AsyncLocalStorage;
@@ -64,7 +78,7 @@ ${contents}
         };
       });
 
-      build.onLoad({ filter: /adapters\/config\/index.ts/g }, async () => {
+      build.onLoad({ filter: /adapters\/config\/index/g }, async () => {
         console.log("opennext-config-plugin");
         const NextConfig = loadConfig(nextDir);
         const BuildId = loadBuildId(nextDir);
diff --git a/packages/open-next/src/types/next-types.ts b/packages/open-next/src/types/next-types.ts
index 0d31c2d3a..5bab5c37f 100644
--- a/packages/open-next/src/types/next-types.ts
+++ b/packages/open-next/src/types/next-types.ts
@@ -113,23 +113,25 @@ export interface RoutesManifest {
   headers?: Header[];
 }
 
+interface MiddlewareInfo {
+  files: string[];
+  paths?: string[];
+  name: string;
+  page: string;
+  matchers: {
+    regexp: string;
+    originalSource: string;
+  }[];
+  wasm: string[];
+  assets: string[];
+}
+
 export interface MiddlewareManifest {
   sortedMiddleware: string[];
   middleware: {
-    [key: string]: {
-      files: string[];
-      paths?: string[];
-      name: string;
-      page: string;
-      matchers: {
-        regexp: string;
-        originalSource: string;
-      }[];
-      wasm: string[];
-      assets: string[];
-    };
+    [key: string]: MiddlewareInfo;
   };
-  functions: { [key: string]: any };
+  functions: { [key: string]: MiddlewareInfo };
   version: number;
 }
 

From 132f3d5fddeef98b64754e974b4e1b9455725ce7 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 13 Dec 2023 21:26:53 +0100
Subject: [PATCH 035/102] external middleware support rewrite between splitted
 servers

---
 packages/open-next/src/adapters/middleware.ts | 54 ++++++++++++++++++-
 .../plugins/without-routing/requestHandler.ts |  1 +
 .../src/converters/aws-cloudfront.ts          | 21 +++++++-
 .../src/core/createGenericHandler.ts          |  4 +-
 packages/open-next/src/core/routingHandler.ts |  4 +-
 packages/open-next/src/types/open-next.ts     | 17 ++++++
 6 files changed, 94 insertions(+), 7 deletions(-)

diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
index e2c561b7d..196cd859b 100644
--- a/packages/open-next/src/adapters/middleware.ts
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -1,20 +1,70 @@
-import { InternalEvent } from "types/open-next";
+import { InternalEvent, Origin } from "types/open-next";
 
-import { debug } from "../adapters/logger";
+import { debug, error } from "../adapters/logger";
 import { createGenericHandler } from "../core/createGenericHandler";
 import routingHandler from "../core/routingHandler";
 
+const resolveOriginResolver = () => {
+  const openNextParams = globalThis.openNextConfig.middleware;
+  if (typeof openNextParams?.originResolver === "function") {
+    return openNextParams.originResolver();
+  } else {
+    return Promise.resolve(async (_path: string) => {
+      try {
+        const origin = JSON.parse(
+          process.env.OPEN_NEXT_ORIGIN ?? "{}",
+        ) as Record<string, Origin>;
+        for (const [key, value] of Object.entries(
+          globalThis.openNextConfig.functions ?? {},
+        )) {
+          if (
+            value.patterns.some((pattern) => {
+              // Convert cloudfront pattern to regex
+              return new RegExp(
+                // transform glob pattern to regex
+                "/" +
+                  pattern
+                    .replace(/\*\*/g, "(.*)")
+                    .replace(/\*/g, "([^/]*)")
+                    .replace(/\//g, "\\/")
+                    .replace(/\?/g, "."),
+              ).test(_path);
+            })
+          ) {
+            debug("Using origin", key, value.patterns);
+            return origin[key];
+          }
+        }
+        if (origin["default"]) {
+          debug("Using default origin", origin["default"]);
+          return origin["default"];
+        }
+        return false as const;
+      } catch (e) {
+        error("Error while resolving origin", e);
+        return false as const;
+      }
+    });
+  }
+};
+
 const defaultHandler = async (internalEvent: InternalEvent) => {
   // TODO: We need to handle splitted function here
   // We should probably create an host resolver to redirect correctly
+  const originResolver = await resolveOriginResolver();
   const result = await routingHandler(internalEvent);
   if ("internalEvent" in result) {
     debug("Middleware intercepted event", internalEvent);
+    let origin: Origin | false = false;
+    if (!result.isExternalRewrite) {
+      origin = await originResolver(result.internalEvent.rawPath);
+    }
     return {
       type: "middleware",
       internalEvent: result.internalEvent,
       headers: result.headers,
       isExternalRewrite: result.isExternalRewrite,
+      origin,
     };
   } else {
     debug("Middleware response", result);
diff --git a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
index 5c6ac54ca..8c7e9a020 100644
--- a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
+++ b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
@@ -18,6 +18,7 @@ const preprocessResult: MiddlewareOutputEvent = {
   internalEvent: internalEvent,
   isExternalRewrite: false,
   headers: overwrittenResponseHeaders,
+  origin: false,
 };
 //#endOverride
 
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index c263e84f3..50c25e3f4 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -1,4 +1,5 @@
 import {
+  CloudFrontCustomOrigin,
   CloudFrontHeaders,
   CloudFrontRequest,
   CloudFrontRequestEvent,
@@ -116,6 +117,18 @@ async function convertToCloudFrontRequestResult(
         body: externalResult.body,
       };
     }
+    let customOrigin = origin?.custom as CloudFrontCustomOrigin;
+    let host = responseHeaders["host"] ?? responseHeaders["Host"];
+    if (result.origin) {
+      customOrigin = {
+        ...customOrigin,
+        domainName: result.origin.host,
+        port: result.origin.port ?? 443,
+        protocol: result.origin.protocol ?? "https",
+        customHeaders: {},
+      };
+      host = result.origin.host;
+    }
 
     const response: CloudFrontRequest = {
       clientIp,
@@ -128,9 +141,15 @@ async function convertToCloudFrontRequestResult(
       headers: convertToCloudfrontHeaders({
         ...responseHeaders,
         ...overwrittenResponseHeaders,
+        host,
       }),
-      origin,
+      origin: origin?.custom
+        ? {
+            custom: customOrigin,
+          }
+        : origin,
     };
+
     debug("response rewrite", response);
 
     return response;
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
index fca40ddbe..6bcb6470f 100644
--- a/packages/open-next/src/core/createGenericHandler.ts
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -41,9 +41,7 @@ export async function createGenericHandler<
     (m) => m.default,
   );
 
-  globalThis.openNextConfig = {
-    [handler.type]: config[handler.type],
-  };
+  globalThis.openNextConfig = config;
   const override = config[handler.type]
     ?.override as any as DefaultOverrideOptions<E, R>;
 
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index 08c4d134c..b74c8f34e 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -5,7 +5,7 @@ import {
   RoutesManifest,
 } from "config/index";
 import type { OutgoingHttpHeaders } from "http";
-import { InternalEvent, InternalResult } from "types/open-next";
+import { InternalEvent, InternalResult, Origin } from "types/open-next";
 
 import { debug } from "../adapters/logger";
 import {
@@ -21,6 +21,7 @@ export interface MiddlewareOutputEvent {
   internalEvent: InternalEvent;
   headers: OutgoingHttpHeaders;
   isExternalRewrite: boolean;
+  origin: Origin | false;
 }
 
 export default async function routingHandler(
@@ -91,5 +92,6 @@ export default async function routingHandler(
       ...middlewareResponseHeaders,
     },
     isExternalRewrite,
+    origin: false,
   };
 }
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 71293831e..19a891005 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -83,6 +83,14 @@ type ImageLoader = (url: string) => Promise<{
   cacheControl?: string;
 }>;
 
+export interface Origin {
+  host: string;
+  protocol: "http" | "https";
+  port?: number;
+  customHeaders?: Record<string, string>;
+}
+type OriginResolver = (path: string) => Promise<Origin | false>;
+
 export type IncludedWrapper =
   | "aws-lambda"
   | "aws-lambda-streaming"
@@ -220,6 +228,15 @@ export interface BuildOptions {
   middleware?: DefaultFunctionOptions & {
     //We force the middleware to be a function
     external: true;
+
+    /**
+     * Origin resolver is used to resolve the origin for internal rewrite.
+     * By default, it uses the pattern-env origin resolver.
+     * Pattern env uses pattern set in split function options and an env variable OPEN_NEXT_ORIGIN
+     * OPEN_NEXT_ORIGIN should be a json stringified object with the key of the splitted function as key and the origin as value
+     * @default "pattern-env"
+     */
+    originResolver?: "pattern-env" | LazyLoadedOverride<OriginResolver>;
   };
 
   /**

From 65bec91d01414a3fe75c0626269179153f836bdf Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 13 Dec 2023 21:35:44 +0100
Subject: [PATCH 036/102] fix alias

---
 packages/open-next/package.json |  5 +++--
 pnpm-lock.yaml                  | 37 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index da3ab54b0..7a07d4c27 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -13,7 +13,7 @@
   "homepage": "https://open-next.js.org",
   "main": "./dist/index.js",
   "scripts": {
-    "build": "tsc",
+    "build": "tsc && tsc-alias",
     "dev": "tsc -w"
   },
   "exports": {
@@ -38,17 +38,18 @@
     "@aws-sdk/client-lambda": "^3.398.0",
     "@aws-sdk/client-s3": "^3.398.0",
     "@aws-sdk/client-sqs": "^3.398.0",
+    "@esbuild-plugins/node-resolve": "0.2.2",
     "@node-minify/core": "^8.0.6",
     "@node-minify/terser": "^8.0.6",
     "@tsconfig/node18": "^1.0.1",
     "esbuild": "0.19.2",
-    "@esbuild-plugins/node-resolve": "0.2.2",
     "path-to-regexp": "^6.2.1",
     "promise.series": "^0.2.0"
   },
   "devDependencies": {
     "@types/aws-lambda": "^8.10.109",
     "@types/node": "^18.16.1",
+    "tsc-alias": "^1.8.8",
     "typescript": "^4.9.3"
   },
   "bugs": {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 26e54d7a5..ba43901c7 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -283,6 +283,9 @@ importers:
       '@types/node':
         specifier: ^18.16.1
         version: 18.17.13
+      tsc-alias:
+        specifier: ^1.8.8
+        version: 1.8.8
       typescript:
         specifier: ^4.9.3
         version: 4.9.3
@@ -7790,6 +7793,11 @@ packages:
     engines: {node: '>= 12'}
     dev: false
 
+  /commander@9.5.0:
+    resolution: {integrity: sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==}
+    engines: {node: ^12.20.0 || >=14}
+    dev: true
+
   /commist@1.1.0:
     resolution: {integrity: sha512-rraC8NXWOEjhADbZe9QBNzLAN5Q3fsTPQtBV+fEVj6xKIgDgNiEVE6ZNfHpZOqfQ21YUzfVNUXLOEZquYvQPPg==}
     dependencies:
@@ -12464,6 +12472,11 @@ packages:
     resolution: {integrity: sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==}
     dev: true
 
+  /mylas@2.1.13:
+    resolution: {integrity: sha512-+MrqnJRtxdF+xngFfUUkIMQrUUL0KsxbADUkn23Z/4ibGg192Q+z+CQyiYwvWTsYjJygmMR8+w3ZDa98Zh6ESg==}
+    engines: {node: '>=12.0.0'}
+    dev: true
+
   /mz@2.7.0:
     resolution: {integrity: sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==}
     dependencies:
@@ -13202,6 +13215,13 @@ packages:
     hasBin: true
     dev: true
 
+  /plimit-lit@1.6.1:
+    resolution: {integrity: sha512-B7+VDyb8Tl6oMJT9oSO2CW8XC/T4UcJGrwOVoNGwOQsQYhlpfajmrMj5xeejqaASq3V/EqThyOeATEOMuSEXiA==}
+    engines: {node: '>=12'}
+    dependencies:
+      queue-lit: 1.5.2
+    dev: true
+
   /postcss-import@15.1.0(postcss@8.4.27):
     resolution: {integrity: sha512-hpr+J05B2FVYUAXHeK1YyI267J/dDDhMU6B6civm8hSY1jYJnBXxzKDKDswzJmtLHryrjhnDjqqp/49t8FALew==}
     engines: {node: '>=14.0.0'}
@@ -13457,6 +13477,11 @@ packages:
     resolution: {integrity: sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==}
     dev: true
 
+  /queue-lit@1.5.2:
+    resolution: {integrity: sha512-tLc36IOPeMAubu8BkW8YDBV+WyIgKlYU7zUNs0J5Vk9skSZ4JfGlPOqplP0aHdfv7HL0B2Pg6nwiq60Qc6M2Hw==}
+    engines: {node: '>=12'}
+    dev: true
+
   /queue-microtask@1.2.3:
     resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
 
@@ -14919,6 +14944,18 @@ packages:
       yn: 3.1.1
     dev: true
 
+  /tsc-alias@1.8.8:
+    resolution: {integrity: sha512-OYUOd2wl0H858NvABWr/BoSKNERw3N9GTi3rHPK8Iv4O1UyUXIrTTOAZNHsjlVpXFOhpJBVARI1s+rzwLivN3Q==}
+    hasBin: true
+    dependencies:
+      chokidar: 3.5.3
+      commander: 9.5.0
+      globby: 11.1.0
+      mylas: 2.1.13
+      normalize-path: 3.0.0
+      plimit-lit: 1.6.1
+    dev: true
+
   /tsconfig-paths@3.14.2:
     resolution: {integrity: sha512-o/9iXgCYc5L/JxCHPe3Hvh8Q/2xm5Z+p18PESBU6Ff33695QnCHBEjcytY2q19ua7Mbl/DavtBOLq+oG0RCL+g==}
     dependencies:

From 7a20b15c0682bc91019a36e9a89ce1ea808ccfed Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 13 Dec 2023 21:48:14 +0100
Subject: [PATCH 037/102] update package.json

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 5f9dedc34..9279b49e6 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.1",
+  "version": "3.0.0-rc.2",
   "bin": {
     "open-next": "./dist/index.js"
   },

From e864df27eb05c4262b691bce70356b46de2bc594 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 9 Jan 2024 12:10:46 +0100
Subject: [PATCH 038/102] use AsyncLocalStorage to scope lastModified to a
 single request

---
 packages/open-next/src/adapters/cache.ts      |  5 +-
 .../open-next/src/core/createMainHandler.ts   |  5 ++
 packages/open-next/src/core/requestHandler.ts | 52 ++++++++++++-------
 packages/open-next/src/core/routing/util.ts   | 22 ++++----
 4 files changed, 50 insertions(+), 34 deletions(-)

diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts
index be1421c47..f304f9eca 100644
--- a/packages/open-next/src/adapters/cache.ts
+++ b/packages/open-next/src/adapters/cache.ts
@@ -92,7 +92,7 @@ declare global {
   var tagCache: TagCache;
   var disableDynamoDBCache: boolean;
   var disableIncrementalCache: boolean;
-  var lastModified: number;
+  var lastModified: Record<string, number>;
 }
 
 export default class S3Cache {
@@ -170,7 +170,8 @@ export default class S3Cache {
         // If some tags are stale we need to force revalidation
         return null;
       }
-      globalThis.lastModified = _lastModified;
+      const requestId = globalThis.__als.getStore() ?? "";
+      globalThis.lastModified[requestId] = _lastModified;
       if (cacheData?.type === "route") {
         return {
           lastModified: _lastModified,
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 8f1e635d3..51355e7a4 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -1,3 +1,5 @@
+import type { AsyncLocalStorage } from "node:async_hooks";
+
 import type { BuildOptions, OverrideOptions } from "types/open-next";
 
 import { debug } from "../adapters/logger";
@@ -12,6 +14,7 @@ declare global {
   var incrementalCache: IncrementalCache;
   var fnName: string | undefined;
   var serverId: string;
+  var __als: AsyncLocalStorage<string>;
 }
 
 async function resolveQueue(queue: OverrideOptions["queue"]) {
@@ -61,6 +64,8 @@ export async function createMainHandler() {
 
   globalThis.tagCache = await resolveTagCache(thisFunction.override?.tagCache);
 
+  globalThis.lastModified = {};
+
   // From the config, we create the adapter
   const adapter = await resolveConverter(thisFunction.override?.converter);
 
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 1c2c7695b..33184a012 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -1,3 +1,5 @@
+import { AsyncLocalStorage } from "node:async_hooks";
+
 import {
   IncomingMessage,
   OpenNextNodeResponse,
@@ -10,6 +12,9 @@ import { convertRes, createServerResponse, proxyRequest } from "./routing/util";
 import routingHandler from "./routingHandler";
 import { requestHandler, setNextjsPrebundledReact } from "./util";
 
+// This is used to identify requests in the cache
+globalThis.__als = new AsyncLocalStorage<string>();
+
 export async function openNextHandler(
   internalEvent: InternalEvent,
   responseStreaming?: StreamCreator,
@@ -47,30 +52,37 @@ export async function openNextHandler(
       body: preprocessedEvent.body,
       remoteAddress: preprocessedEvent.remoteAddress,
     };
-    const req = new IncomingMessage(reqProps);
-    const res = createServerResponse(
-      preprocessedEvent,
-      preprocessResult.headers as Record<string, string | string[]>,
-      responseStreaming,
-    );
+    const requestId = Math.random().toString(36);
+    const internalResult = await globalThis.__als.run(requestId, async () => {
+      const req = new IncomingMessage(reqProps);
+      const res = createServerResponse(
+        preprocessedEvent,
+        preprocessResult.headers as Record<string, string | string[]>,
+        responseStreaming,
+      );
 
-    await processRequest(
-      req,
-      res,
-      preprocessedEvent,
-      preprocessResult.isExternalRewrite,
-    );
+      await processRequest(
+        req,
+        res,
+        preprocessedEvent,
+        preprocessResult.isExternalRewrite,
+      );
 
-    const { statusCode, headers, isBase64Encoded, body } = convertRes(res);
+      const { statusCode, headers, isBase64Encoded, body } = convertRes(res);
 
-    const internalResult = {
-      type: internalEvent.type,
-      statusCode,
-      headers,
-      body,
-      isBase64Encoded,
-    };
+      const internalResult = {
+        type: internalEvent.type,
+        statusCode,
+        headers,
+        body,
+        isBase64Encoded,
+      };
+
+      // reset lastModified. We need to do this to avoid memory leaks
+      delete globalThis.lastModified[requestId];
 
+      return internalResult;
+    });
     return internalResult;
   }
 }
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index 54fba4e10..a6138d1b7 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -250,7 +250,7 @@ export async function proxyRequest(
 declare global {
   var openNextDebug: boolean;
   var openNextVersion: string;
-  var lastModified: number;
+  var lastModified: Record<string, number>;
 }
 
 enum CommonHeaders {
@@ -299,6 +299,7 @@ export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
   headers["X-OpenNext"] = "1";
   if (globalThis.openNextDebug) {
     headers["X-OpenNext-Version"] = globalThis.openNextVersion;
+    headers["X-OpenNext-RequestId"] = globalThis.__als.getStore();
   }
 }
 
@@ -312,8 +313,6 @@ export async function revalidateIfRequired(
   headers: OutgoingHttpHeaders,
   req?: IncomingMessage,
 ) {
-  fixISRHeaders(headers);
-
   if (headers[CommonHeaders.NEXT_CACHE] === "STALE") {
     // If the URL is rewritten, revalidation needs to be done on the rewritten URL.
     // - Link to Next.js doc: https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation
@@ -340,9 +339,12 @@ export async function revalidateIfRequired(
     try {
       const hash = (str: string) =>
         crypto.createHash("md5").update(str).digest("hex");
+      const requestId = globalThis.__als.getStore() ?? "";
 
       const lastModified =
-        globalThis.lastModified > 0 ? globalThis.lastModified : "";
+        globalThis.lastModified[requestId] > 0
+          ? globalThis.lastModified[requestId]
+          : "";
 
       // await sqsClient.send(
       //   new SendMessageCommand({
@@ -419,12 +421,11 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
       "private, no-cache, no-store, max-age=0, must-revalidate";
     return;
   }
-  if (
-    headers[CommonHeaders.NEXT_CACHE] === "HIT" &&
-    globalThis.lastModified > 0
-  ) {
+  const requestId = globalThis.__als.getStore() ?? "";
+  const _lastModified = globalThis.lastModified[requestId] ?? 0;
+  if (headers[CommonHeaders.NEXT_CACHE] === "HIT" && _lastModified > 0) {
     // calculate age
-    const age = Math.round((Date.now() - globalThis.lastModified) / 1000);
+    const age = Math.round((Date.now() - _lastModified) / 1000);
     // extract s-maxage from cache-control
     const regex = /s-maxage=(\d+)/;
     const cacheControl = headers[CommonHeaders.CACHE_CONTROL];
@@ -440,9 +441,6 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
         CommonHeaders.CACHE_CONTROL
       ] = `s-maxage=${remainingTtl}, stale-while-revalidate=2592000`;
     }
-
-    // reset lastModified
-    globalThis.lastModified = 0;
   }
   if (headers[CommonHeaders.NEXT_CACHE] !== "STALE") return;
 

From 6f9a98766704c1f8f3806ce1e187f1e5f5655ec8 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 9 Jan 2024 12:14:44 +0100
Subject: [PATCH 039/102] merge upstream/main

---
 .github/workflows/e2e.yml                     |   2 +-
 docs/pages/common_issues/isr.mdx              |   4 +-
 docs/pages/v3/_meta.json                      |   6 +
 docs/pages/v3/config.mdx                      |  69 +++
 docs/pages/v3/index.mdx                       |  58 +++
 docs/pages/v3/override.mdx                    |  74 ++++
 docs/pages/v3/reference-implementation.mdx    | 408 ++++++++++++++++++
 packages/open-next/CHANGELOG.md               |   9 +
 packages/open-next/package.json               |   2 +-
 packages/open-next/src/adapters/cache.ts      |  14 +-
 packages/open-next/src/build.ts               |  25 +-
 .../open-next/src/build/createServerBundle.ts |   1 +
 .../open-next/src/core/routing/matcher.ts     |  11 +-
 packages/tests-unit/CHANGELOG.md              |  10 +
 14 files changed, 678 insertions(+), 15 deletions(-)
 create mode 100644 docs/pages/v3/_meta.json
 create mode 100644 docs/pages/v3/config.mdx
 create mode 100644 docs/pages/v3/index.mdx
 create mode 100644 docs/pages/v3/override.mdx
 create mode 100644 docs/pages/v3/reference-implementation.mdx

diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml
index 12e964884..686294b13 100644
--- a/.github/workflows/e2e.yml
+++ b/.github/workflows/e2e.yml
@@ -94,7 +94,7 @@ jobs:
         uses: actions/setup-node@v3
         with:
           cache: pnpm # cache pnpm store
-          node-version: 18.16.1
+          node-version: 18.18.2
 
       - name: Install packages
         run: pnpm install
diff --git a/docs/pages/common_issues/isr.mdx b/docs/pages/common_issues/isr.mdx
index 182333e0e..baac6f681 100644
--- a/docs/pages/common_issues/isr.mdx
+++ b/docs/pages/common_issues/isr.mdx
@@ -30,8 +30,8 @@ import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-clo
 
 const cloudFront = new CloudFrontClient({});
 
-function invalidateCFPaths(paths: string[]) {
-  cloudFront.send(
+async function invalidateCFPaths(paths: string[]) {
+  await cloudFront.send(
     new CreateInvalidationCommand({
       // Set CloudFront distribution ID here
       DistributionId: distributionId,
diff --git a/docs/pages/v3/_meta.json b/docs/pages/v3/_meta.json
new file mode 100644
index 000000000..8e89d285d
--- /dev/null
+++ b/docs/pages/v3/_meta.json
@@ -0,0 +1,6 @@
+{
+  "index": "What's new",
+  "config": "Configuration file",
+  "reference-implementation": "Reference Construct",
+  "override": "Advanced - Create your own override"
+}
\ No newline at end of file
diff --git a/docs/pages/v3/config.mdx b/docs/pages/v3/config.mdx
new file mode 100644
index 000000000..008dabfdb
--- /dev/null
+++ b/docs/pages/v3/config.mdx
@@ -0,0 +1,69 @@
+Here is a simple example of an `open-next.config.ts` file: 
+This file need to be at the same place as your `next.config.js` file
+
+`server` in here could refer to a lambda function, a docker container, a node server or whatever that can support running nodejs code. (Even cloudflare workers in the future)
+
+For more information about the options here, just look at the source file
+
+```ts
+import type { BuildOptions } from 'open-next/types/open-next'
+const config = {
+  default: { // This is the default server, similar to the server-function in open-next v2
+    // You don't have to provide the below, by default it will generate an output 
+    // for normal lambda as in open-next v2
+    override: { 
+      wrapper: "aws-lambda-streaming", // This is necessary to enable lambda streaming
+      // You can override any part that is a `LazyLoadedOverride` this way
+      queue: () => Promise.resolve({
+        send: async (message) => {
+          //Your custom code here
+        }
+      })
+    },
+  },
+  // Below we define the functions that we want to deploy in a different server
+  functions: { 
+    ssr: {
+      routes: [
+        "app/api/isr/route", "app/api/sse/route", "app/api/revalidateTag/route", // app dir Api routes
+        "app/route1/page", "app/route2/page", // app dir pages
+        "pages/route3" // page dir pages
+        ], // For app dir, you need to include route|page, no need to include layout or loading
+      patterns: ['api/*', 'route1', 'route2', 'route3'], // patterns needs to be in a cloudfront compatible format, this will be used to generate the output
+      override: {
+        wrapper: "aws-lambda-streaming",
+      },
+      experimentalBundledNextServer: true // This enables the bundled next server which is faster and reduce the size of the server
+    },
+    pageSsr: {
+      routes: ["pages/pageSsr"], // For page dir routes should be in the form `pages/${route}` without the extension, it should match the filesystem
+      patterns: [ 'pageSsr', "_next/data/BUILD_ID/pageSsr.json"],
+      override: {
+        wrapper: "node",
+        converter: "node",
+        // This is necessary to generate the dockerfile and for the implementation to know that it needs to deploy on docker
+        generateDockerfile: true,
+      },
+    },
+    edge: {
+      runtime: "edge",
+      routes: ["app/ssr/page"],
+      patterns: ["ssr"],
+      override: {}
+    }    
+  },
+  // By setting this, it will create another bundle for the middleware, 
+  // and the middleware will be deployed in a separate server. 
+  // If not set middleware will be bundled inside the servers
+  // It could be in lambda@edge, cloudflare workers, or anywhere else
+  // By default it uses lambda@edge
+  // This is not implemented in the reference construct implementation.
+  middleware: {
+    external: true 
+  }
+  buildCommand: "echo 'hello world'",
+} satisfies BuildOptions
+
+module.exports = config
+export type OpenNextConfig = typeof config
+```
\ No newline at end of file
diff --git a/docs/pages/v3/index.mdx b/docs/pages/v3/index.mdx
new file mode 100644
index 000000000..10416ea0e
--- /dev/null
+++ b/docs/pages/v3/index.mdx
@@ -0,0 +1,58 @@
+import { Callout } from 'nextra/components'
+
+<Callout type="warning" emoji="⚠️">
+
+`open-next@3.0.0-rc.2` is here!!! Please report any issues you find on [discord](https://discord.com/channels/983865673656705025/1164872233223729152) or on the github [PR](https://github.com/sst/open-next/pull/327)
+
+  This is a release candidate, it is not yet ready for production, but we are getting close. We are looking for feedback on this release, so please try it out and let us know what you think. See [getting started](#get-started) to quickly test it.
+  
+  It also requires an updated version of the IAC tools that you use, see the sst PR [here](https://github.com/sst/sst/pull/3567) for more information.
+</Callout>
+
+## What's new in V3?
+
+- Rewritten server (We moved from the serverless-http `ServerResponse` into our own version which respect nodejs stream)
+- A new `open-next.config.ts` file to configure your project
+- Native support for aws lambda, aws lambda streaming, lambda@edge, node and docker
+- In this `open-next.config.ts` you can now override a lot of things which allow for more customization:
+  - Wrapper component
+  - Converter component
+  - Incremental Cache
+  - Tag Cache
+  - Queue
+  - Custom warmer function
+  - Custom revalidation function
+  - Custom loader for image optimization
+  - Custom initialization function
+
+- Allow for splitting, you can now split your next app into multiple servers, which could each have their own configuration
+- An experimental bundled `NextServer` could be used which can reduce the size of your lambda by up to 24 MB
+- Support for the `edge` runtime of next (Only app router for now, only 1 route per function)
+
+## Get started
+
+The easiest way to get started is to use the [reference implementation construct](/v3/reference-implementation). Copy this reference implementation into your project and then use it like that in your sst or cdk project:
+
+```ts
+import { OpenNextCdkReferenceImplementation } from "path/to/reference-implementation"
+
+const site = new OpenNextCdkReferenceImplementation(stack, "site", {
+  openNextPath: ".open-next",
+})
+```
+
+You also need to create an `open-next.config.ts` file in your project root, you can find more info [here](/v3/config).
+
+A very simple example of this file could be:
+
+```ts
+import type { BuildOptions } from 'open-next/types/open-next'
+const config = {
+  default: {
+
+  }
+}
+module.exports = config
+```
+
+Then you need to run `npx open-next@3.0.0-rc.2 build` to build your project before running the `sst deploy` or `cdk deploy` command to deploy your project.
\ No newline at end of file
diff --git a/docs/pages/v3/override.mdx b/docs/pages/v3/override.mdx
new file mode 100644
index 000000000..5d083bdda
--- /dev/null
+++ b/docs/pages/v3/override.mdx
@@ -0,0 +1,74 @@
+In this version of open-next, you could override a lot of the default behaviour.
+
+For some real example of how to override each behaviour: 
+- [Wrapper](https://github.com/conico974/open-next/blob/feat/splitting/packages/open-next/src/wrappers/aws-lambda.ts)
+- [Converter](https://github.com/conico974/open-next/blob/feat/splitting/packages/open-next/src/converters/aws-apigw-v2.ts)
+- [IncrementalCache](https://github.com/conico974/open-next/blob/feat/splitting/packages/open-next/src/cache/incremental/s3.ts)
+- [TagCache](
+  https://github.com/conico974/open-next/blob/feat/splitting/packages/open-next/src/cache/tag/dynamoDb.ts
+)
+- [Queue](
+  https://github.com/conico974/open-next/blob/feat/splitting/packages/open-next/src/queue/sqs.ts
+)
+
+This means it could allow people to write their own custom open-next.
+For example you could create a custom `withGcp` plugin to allow to deploy open-next on GCP functions
+
+A boilerplate for such a plugin could look like this (This is not real code):
+
+```ts
+
+import { BuildOptions } from "open-next/types/open-next";
+
+function withGcp(config: TrimmedDownConfig): BuildOptions {
+  return {
+    default: {
+        override: {
+          wrapper: async () => (await import("./gcp-wrapper")).default,
+          converter: async () => (await import("./gcp-converter")).default,
+          incrementalCache: async () => (await import("./gcp-incremental-cache")).default,
+          tagCache: async () => (await import("./gcp-tag-cache")).default,
+          queue: async () => (await import("./gcp-queue")).default,
+        },
+        ...config.default,
+    },
+    functions: {
+      // Same as default but for each splitted function
+      //...
+    }
+    warmer: {
+      override: {
+        wrapper: async () => (await import("./gcp-wrapper")).default,
+        converter: async () => (await import("./gcp-converter")).default,
+      },
+      invokeFunction: async () => (await import("./gcp-invoke-function")).default,
+    },
+    revalidate: {
+      override: {
+        wrapper: async () => (await import("./gcp-wrapper")).default,
+        converter: async () => (await import("./gcp-queue-converter")).default,
+      },
+    },
+    imageOptimization: {
+      override: {
+        wrapper: async () => (await import("./gcp-wrapper")).default,
+        converter: async () => (await import("./gcp-converter")).default,
+      },
+      loader: async () => (await import("./gcp-object-loader")).default,
+    },
+  }
+}
+```
+
+Using this plugin would look like this inside `open-next.config.ts`:
+
+```ts
+import { withGcp } from "./with-gcp";
+const config = withGcp({
+  default: {
+    // ...
+  },
+});
+
+module.exports = config;
+```
\ No newline at end of file
diff --git a/docs/pages/v3/reference-implementation.mdx b/docs/pages/v3/reference-implementation.mdx
new file mode 100644
index 000000000..e79960b9a
--- /dev/null
+++ b/docs/pages/v3/reference-implementation.mdx
@@ -0,0 +1,408 @@
+import { Callout } from 'nextra/components'
+
+In order to help testing the rc release, we created a simple reference implementation using aws-cdk.
+
+If you wish to use it, just copy the code for the construct below. If you use it inside sst, make sure to use the same version of aws-cdk as sst.
+
+<Callout type="warning" emoji="⚠️">
+
+  This is a reference implementation, and it is not meant to be used in production.
+
+  The goal is to help with the adoption of the rc release, and to gather feedback from the community.
+  It also serves as a good example of how to use the new features.
+
+  There is some features that are not implemented like the warmer function, or everything related to lambda@edge(It requires inserting env variables which is out of scope of this implementation).
+</Callout>
+
+```ts
+import { Construct } from "constructs";
+import { readFileSync } from "fs";
+import path from "path";
+import { BlockPublicAccess, Bucket } from "aws-cdk-lib/aws-s3";
+import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment";
+import {
+	CustomResource,
+	Duration,
+	Fn,
+	RemovalPolicy,
+	Stack,
+} from "aws-cdk-lib/core";
+import {
+	AllowedMethods,
+	BehaviorOptions,
+	CacheCookieBehavior,
+	CacheHeaderBehavior,
+	CachePolicy,
+	CacheQueryStringBehavior,
+	CachedMethods,
+	Distribution,
+	ICachePolicy,
+	ViewerProtocolPolicy,
+} from "aws-cdk-lib/aws-cloudfront";
+import { HttpOrigin, S3Origin } from "aws-cdk-lib/aws-cloudfront-origins";
+import {
+	Code,
+	Function as CdkFunction,
+	FunctionUrlAuthType,
+	InvokeMode,
+	Runtime,
+} from "aws-cdk-lib/aws-lambda";
+import {
+	TableV2 as Table,
+	AttributeType,
+	Billing,
+} from "aws-cdk-lib/aws-dynamodb";
+import {
+	Service,
+	Source as AppRunnerSource,
+	Memory,
+	HealthCheck,
+	Cpu,
+} from "@aws-cdk/aws-apprunner-alpha";
+import { DockerImageAsset } from "aws-cdk-lib/aws-ecr-assets";
+import { Queue } from "aws-cdk-lib/aws-sqs";
+import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
+import { IGrantable } from "aws-cdk-lib/aws-iam";
+import { Provider } from "aws-cdk-lib/custom-resources";
+import { RetentionDays } from "aws-cdk-lib/aws-logs";
+
+type BaseFunction = {
+	handler: string;
+	bundle: string;
+};
+
+type OpenNextFunctionOrigin = {
+	type: "function";
+	streaming?: boolean;
+} & BaseFunction;
+
+type OpenNextECSOrigin = {
+	type: "ecs";
+	bundle: string;
+	dockerfile: string;
+};
+
+type OpenNextS3Origin = {
+	type: "s3";
+	originPath: string;
+	copy: {
+		from: string;
+		to: string;
+		cached: boolean;
+		versionedSubDir?: string;
+	}[];
+};
+
+type OpenNextOrigins =
+	| OpenNextFunctionOrigin
+	| OpenNextECSOrigin
+	| OpenNextS3Origin;
+
+interface OpenNextOutput {
+	edgeFunctions: {
+		[key: string]: BaseFunction;
+	};
+	origins: {
+		s3: OpenNextS3Origin;
+		default: OpenNextFunctionOrigin | OpenNextECSOrigin;
+		imageOptimizer: OpenNextFunctionOrigin | OpenNextECSOrigin;
+		[key: string]: OpenNextOrigins;
+	};
+	behaviors: {
+		pattern: string;
+		origin?: string;
+		edgeFunction?: string;
+	}[];
+	additionalProps?: {
+		disableIncrementalCache?: boolean;
+		disableTagCache?: boolean;
+		initializationFunction?: BaseFunction;
+		warmer?: BaseFunction;
+		revalidationFunction?: BaseFunction;
+	};
+}
+
+interface OpenNextCdkReferenceImplementationProps {
+	openNextPath: string;
+}
+
+export class OpenNextCdkReferenceImplementation extends Construct {
+	private openNextOutput: OpenNextOutput;
+	private bucket: Bucket;
+	private table: Table;
+	private queue: Queue;
+
+	private staticCachePolicy: ICachePolicy;
+	private serverCachePolicy: CachePolicy;
+
+	public distribution: Distribution;
+
+	constructor(
+		scope: Construct,
+		id: string,
+		props: OpenNextCdkReferenceImplementationProps,
+	) {
+		super(scope, id);
+		this.openNextOutput = JSON.parse(
+			readFileSync(
+				path.join(props.openNextPath, "open-next.output.json"),
+				"utf-8",
+			),
+		) as OpenNextOutput;
+
+		this.bucket = new Bucket(this, "OpenNextBucket", {
+			publicReadAccess: false,
+			blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
+			autoDeleteObjects: true,
+			removalPolicy: RemovalPolicy.DESTROY,
+			enforceSSL: true,
+		});
+		this.table = this.createRevalidationTable();
+		this.queue = this.createRevalidationQueue();
+
+		const origins = this.createOrigins();
+		this.serverCachePolicy = this.createServerCachePolicy();
+		this.staticCachePolicy = this.createStaticCachePolicy();
+		this.distribution = this.createDistribution(origins);
+	}
+
+	private createRevalidationTable() {
+		const table = new Table(this, "RevalidationTable", {
+			partitionKey: { name: "tag", type: AttributeType.STRING },
+			sortKey: { name: "path", type: AttributeType.STRING },
+			pointInTimeRecovery: true,
+			billing: Billing.onDemand(),
+			globalSecondaryIndexes: [
+				{
+					indexName: "revalidate",
+					partitionKey: { name: "path", type: AttributeType.STRING },
+					sortKey: { name: "revalidatedAt", type: AttributeType.NUMBER },
+				},
+			],
+			removalPolicy: RemovalPolicy.DESTROY,
+		});
+
+		const initFn = this.openNextOutput.additionalProps?.initializationFunction;
+
+		const insertFn = new CdkFunction(this, "RevalidationInsertFunction", {
+			description: "Next.js revalidation data insert",
+			handler: initFn?.handler ?? "index.handler",
+			// code: Code.fromAsset(initFn?.bundle ?? ""),
+			code: Code.fromAsset(".open-next/dynamodb-provider"),
+			runtime: Runtime.NODEJS_18_X,
+			timeout: Duration.minutes(15),
+			memorySize: 128,
+			environment: {
+				CACHE_DYNAMO_TABLE: table.tableName,
+			},
+		});
+
+		const provider = new Provider(this, "RevalidationProvider", {
+			onEventHandler: insertFn,
+			logRetention: RetentionDays.ONE_DAY,
+		});
+
+		new CustomResource(this, "RevalidationResource", {
+			serviceToken: provider.serviceToken,
+			properties: {
+				version: Date.now().toString(),
+			},
+		});
+
+		return table;
+	}
+
+	private createOrigins() {
+		const {
+			s3: s3Origin,
+			default: defaultOrigin,
+			imageOptimizer: imageOrigin,
+			...restOrigins
+		} = this.openNextOutput.origins;
+		const s3 = new S3Origin(this.bucket, {
+			originPath: s3Origin.originPath,
+		});
+		for (const copy of s3Origin.copy) {
+			new BucketDeployment(this, `OpenNextBucketDeployment${copy.from}`, {
+				sources: [Source.asset(copy.from)],
+				destinationBucket: this.bucket,
+				destinationKeyPrefix: copy.to,
+				prune: false,
+			});
+		}
+		const origins = {
+			s3: new S3Origin(this.bucket, {
+				originPath: s3Origin.originPath,
+				originAccessIdentity: undefined,
+			}),
+			default:
+				defaultOrigin.type === "function"
+					? this.createFunctionOrigin("default", defaultOrigin)
+					: this.createAppRunnerOrigin("default", defaultOrigin),
+			imageOptimizer:
+				imageOrigin.type === "function"
+					? this.createFunctionOrigin("imageOptimizer", imageOrigin)
+					: this.createAppRunnerOrigin("imageOptimizer", imageOrigin),
+			...Object.entries(restOrigins).reduce(
+				(acc, [key, value]) => {
+					if (value.type === "function") {
+						acc[key] = this.createFunctionOrigin(key, value);
+					} else if (value.type === "ecs") {
+						acc[key] = this.createAppRunnerOrigin(key, value);
+					}
+					return acc;
+				},
+				{} as Record<string, HttpOrigin>,
+			),
+		};
+		return origins;
+	}
+
+	private createRevalidationQueue() {
+		const queue = new Queue(this, "RevalidationQueue", {
+			fifo: true,
+			receiveMessageWaitTime: Duration.seconds(20),
+		});
+		const consumer = new CdkFunction(this, "RevalidationFunction", {
+			description: "Next.js revalidator",
+			handler: "index.handler",
+			code: Code.fromAsset(
+				this.openNextOutput.additionalProps?.revalidationFunction?.bundle ?? "",
+			),
+			runtime: Runtime.NODEJS_18_X,
+			timeout: Duration.seconds(30),
+		});
+		consumer.addEventSource(new SqsEventSource(queue, { batchSize: 5 }));
+		return queue;
+	}
+
+	private getEnvironment() {
+		return {
+			CACHE_BUCKET_NAME: this.bucket.bucketName,
+			CACHE_BUCKET_KEY_PREFIX: "_cache",
+			CACHE_BUCKET_REGION: Stack.of(this).region,
+			REVALIDATION_QUEUE_URL: this.queue.queueUrl,
+			REVALIDATION_QUEUE_REGION: Stack.of(this).region,
+			CACHE_DYNAMO_TABLE: this.table.tableName,
+			// Those 2 are used only for image optimizer
+			BUCKET_NAME: this.bucket.bucketName,
+			BUCKET_KEY_PREFIX: "_assets",
+		};
+	}
+
+	private grantPermissions(grantable: IGrantable) {
+		this.bucket.grantReadWrite(grantable);
+		this.table.grantReadWriteData(grantable);
+		this.queue.grantSendMessages(grantable);
+	}
+
+	private createFunctionOrigin(key: string, origin: OpenNextFunctionOrigin) {
+		const environment = this.getEnvironment();
+		const fn = new CdkFunction(this, `${key}Function`, {
+			runtime: Runtime.NODEJS_18_X,
+			handler: origin.handler,
+			code: Code.fromAsset(origin.bundle),
+			environment,
+			memorySize: 1024,
+		});
+		const fnUrl = fn.addFunctionUrl({
+			authType: FunctionUrlAuthType.NONE,
+			invokeMode: origin.streaming
+				? InvokeMode.RESPONSE_STREAM
+				: InvokeMode.BUFFERED,
+		});
+		this.grantPermissions(fn);
+		return new HttpOrigin(Fn.parseDomainName(fnUrl.url));
+	}
+
+  // We are using AppRunner because it is the easiest way to demonstrate the new feature.
+  // You can use any other container service like ECS, EKS, Fargate, etc.
+	private createAppRunnerOrigin(
+		key: string,
+		origin: OpenNextECSOrigin,
+	): HttpOrigin {
+		const imageAsset = new DockerImageAsset(this, `${key}ImageAsset`, {
+			directory: origin.bundle,
+			// file: origin.dockerfile,
+		});
+		const service = new Service(this, `${key}Service`, {
+			source: AppRunnerSource.fromAsset({
+				asset: imageAsset,
+
+				imageConfiguration: {
+					port: 3000,
+					environmentVariables: this.getEnvironment(),
+				},
+			}),
+			serviceName: key,
+			autoDeploymentsEnabled: false,
+			cpu: Cpu.HALF_VCPU,
+			memory: Memory.ONE_GB,
+			healthCheck: HealthCheck.http({
+				path: "/__health",
+			}),
+		});
+		this.grantPermissions(service);
+		return new HttpOrigin(service.serviceUrl);
+	}
+
+	private createDistribution(origins: Record<string, HttpOrigin | S3Origin>) {
+		const distribution = new Distribution(this, "OpenNextDistribution", {
+			defaultBehavior: {
+				origin: origins.default,
+				viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
+				allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
+				cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
+				cachePolicy: this.serverCachePolicy,
+			},
+			additionalBehaviors: this.openNextOutput.behaviors
+				.filter((b) => b.pattern !== "*")
+				.reduce(
+					(acc, behavior) => {
+						return {
+							...acc,
+							[behavior.pattern]: {
+								origin: behavior.origin
+									? origins[behavior.origin]
+									: origins.default,
+								viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
+								allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
+								cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
+								cachePolicy:
+									behavior.origin === "s3"
+										? this.staticCachePolicy
+										: this.serverCachePolicy,
+							},
+						};
+					},
+					{} as Record<string, BehaviorOptions>,
+				),
+		});
+		return distribution;
+	}
+
+	private createServerCachePolicy() {
+		return new CachePolicy(this, "OpenNextServerCachePolicy", {
+			queryStringBehavior: CacheQueryStringBehavior.all(),
+			headerBehavior: CacheHeaderBehavior.allowList(
+				"accept",
+				"accept-encoding",
+				"rsc",
+				"next-router-prefetch",
+				"next-router-state-tree",
+				"next-url",
+				"x-prerender-revalidate",
+			),
+			cookieBehavior: CacheCookieBehavior.none(),
+			defaultTtl: Duration.days(0),
+			maxTtl: Duration.days(365),
+			minTtl: Duration.days(0),
+		});
+	}
+
+	private createStaticCachePolicy() {
+		return CachePolicy.CACHING_OPTIMIZED;
+	}
+}
+
+```
diff --git a/packages/open-next/CHANGELOG.md b/packages/open-next/CHANGELOG.md
index 537473eff..10968b8d9 100644
--- a/packages/open-next/CHANGELOG.md
+++ b/packages/open-next/CHANGELOG.md
@@ -1,5 +1,14 @@
 # open-next
 
+## 2.3.4
+
+### Patch Changes
+
+- e773e67: try to match errors, fallback to raw key/value pair
+- 83b0838: add support for bun lockfile
+- bbf9b30: use dynamic import handler for monorepo entrypoint
+- fd90b26: Changes encoding on cache.body for binary data
+
 ## 2.3.3
 
 ### Patch Changes
diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 7a07d4c27..0dea7bb3f 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "2.3.3",
+  "version": "2.3.4",
   "bin": {
     "open-next": "./dist/index.js"
   },
diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts
index f304f9eca..e549aa2b5 100644
--- a/packages/open-next/src/adapters/cache.ts
+++ b/packages/open-next/src/adapters/cache.ts
@@ -1,5 +1,6 @@
 import { IncrementalCache } from "../cache/incremental/types.js";
 import { TagCache } from "../cache/tag/types.js";
+import { isBinaryContentType } from "./binary.js";
 import { debug, error, warn } from "./logger.js";
 
 interface CachedFetchValue {
@@ -177,7 +178,12 @@ export default class S3Cache {
           lastModified: _lastModified,
           value: {
             kind: "ROUTE",
-            body: Buffer.from(cacheData.body ?? Buffer.alloc(0)),
+            body: Buffer.from(
+              cacheData.body ?? Buffer.alloc(0),
+              isBinaryContentType(String(meta?.headers?.["content-type"]))
+                ? "base64"
+                : "utf8",
+            ),
             status: meta?.status,
             headers: meta?.headers,
           },
@@ -226,7 +232,11 @@ export default class S3Cache {
         key,
         {
           type: "route",
-          body: body.toString("utf8"),
+          body: body.toString(
+            isBinaryContentType(String(headers["content-type"]))
+              ? "base64"
+              : "utf8",
+          ),
           meta: {
             status,
             headers,
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 768c50ed3..ee5403501 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -6,6 +6,7 @@ import url from "node:url";
 import { buildSync } from "esbuild";
 import { MiddlewareManifest } from "types/next-types.js";
 
+import { isBinaryContentType } from "./adapters/binary.js";
 import { createServerBundle } from "./build/createServerBundle.js";
 import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js";
 import { generateOutput } from "./build/generateOutput.js";
@@ -115,6 +116,7 @@ function findMonorepoRoot(appPath: string) {
       { file: "package-lock.json", packager: "npm" as const },
       { file: "yarn.lock", packager: "yarn" as const },
       { file: "pnpm-lock.yaml", packager: "pnpm" as const },
+      { file: "bun.lockb", packager: "bun" as const },
     ].find((f) => fs.existsSync(path.join(currentPath, f.file)));
 
     if (found) {
@@ -139,11 +141,13 @@ function setStandaloneBuildMode(monorepoRoot: string) {
   process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT = monorepoRoot;
 }
 
-function buildNextjsApp(packager: "npm" | "yarn" | "pnpm") {
+function buildNextjsApp(packager: "npm" | "yarn" | "pnpm" | "bun") {
   const { nextPackageJsonPath } = options;
   const command =
     options.buildCommand ??
-    (packager === "npm" ? "npm run build" : `${packager} build`);
+    (["bun", "npm"].includes(packager)
+      ? `${packager} run build`
+      : `${packager} build`);
   cp.execSync(command, {
     stdio: "inherit",
     cwd: path.dirname(nextPackageJsonPath),
@@ -485,17 +489,26 @@ async function createCacheAssets(
 
   // Generate cache file
   Object.entries(cacheFilesPath).forEach(([cacheFilePath, files]) => {
+    const cacheFileMeta = files.meta
+      ? JSON.parse(fs.readFileSync(files.meta, "utf8"))
+      : undefined;
     const cacheFileContent = {
       type: files.body ? "route" : files.json ? "page" : "app",
-      meta: files.meta
-        ? JSON.parse(fs.readFileSync(files.meta, "utf8"))
-        : undefined,
+      meta: cacheFileMeta,
       html: files.html ? fs.readFileSync(files.html, "utf8") : undefined,
       json: files.json
         ? JSON.parse(fs.readFileSync(files.json, "utf8"))
         : undefined,
       rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined,
-      body: files.body ? fs.readFileSync(files.body, "utf8") : undefined,
+      body: files.body
+        ? fs
+            .readFileSync(files.body)
+            .toString(
+              isBinaryContentType(cacheFileMeta.headers["content-type"])
+                ? "base64"
+                : "utf8",
+            )
+        : undefined,
     };
     fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
   });
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 74de666d3..93c1a0941 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -269,6 +269,7 @@ function shouldGenerateDockerfile(options: FunctionOptions) {
   return options.override?.generateDockerfile ?? false;
 }
 
+//TODO: check if this PR is still necessary https://github.com/sst/open-next/pull/341
 function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
   // Note: in the monorepo case, the handler file is output to
   //       `.next/standalone/package/path/index.mjs`, but we want
diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index 6d3551191..d279d8cdb 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -110,9 +110,14 @@ export function addNextConfigHeaders(
       const fromSource = match(source);
       const _match = fromSource(rawPath);
       headers.forEach((h) => {
-        const key = convertMatch(_match, compile(h.key), h.key);
-        const value = convertMatch(_match, compile(h.value), h.value);
-        requestHeaders[key] = value;
+        try {
+          const key = convertMatch(_match, compile(h.key), h.key);
+          const value = convertMatch(_match, compile(h.value), h.value);
+          requestHeaders[key] = value;
+        } catch {
+          debug("Error matching header ", h.key, " with value ", h.value);
+          requestHeaders[h.key] = h.value;
+        }
       });
     }
   }
diff --git a/packages/tests-unit/CHANGELOG.md b/packages/tests-unit/CHANGELOG.md
index 679532bc7..336a52111 100644
--- a/packages/tests-unit/CHANGELOG.md
+++ b/packages/tests-unit/CHANGELOG.md
@@ -4,6 +4,16 @@
 
 ### Patch Changes
 
+- Updated dependencies [e773e67]
+- Updated dependencies [83b0838]
+- Updated dependencies [bbf9b30]
+- Updated dependencies [fd90b26]
+  - open-next@2.3.4
+
+## null
+
+### Patch Changes
+
 - Updated dependencies [abeb9cd]
   - open-next@2.3.3
 

From 142aac3794d3f4db5344a837002c5a335be8bfe6 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 20 Jan 2024 17:35:09 +0100
Subject: [PATCH 040/102] Add basic validation

---
 packages/open-next/package.json               |  1 +
 packages/open-next/src/build.ts               |  6 +-
 .../open-next/src/build/createServerBundle.ts | 15 +++--
 .../src/build/edge/createEdgeBundle.ts        |  4 +-
 .../open-next/src/build/generateOutput.ts     | 12 ++--
 .../open-next/src/build/validateConfig.ts     | 56 +++++++++++++++++++
 .../open-next/src/core/createMainHandler.ts   |  2 +-
 packages/open-next/src/logger.ts              |  8 ++-
 packages/open-next/src/plugins/replacement.ts |  7 ++-
 packages/open-next/src/plugins/resolve.ts     |  9 ++-
 packages/open-next/src/types/open-next.ts     |  6 +-
 pnpm-lock.yaml                                |  4 +-
 12 files changed, 104 insertions(+), 26 deletions(-)
 create mode 100644 packages/open-next/src/build/validateConfig.ts

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 0dea7bb3f..413c6b78f 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -42,6 +42,7 @@
     "@node-minify/core": "^8.0.6",
     "@node-minify/terser": "^8.0.6",
     "@tsconfig/node18": "^1.0.1",
+    "chalk": "^5.3.0",
     "esbuild": "0.19.2",
     "path-to-regexp": "^6.2.1",
     "promise.series": "^0.2.0"
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index ee5403501..cad287162 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -20,6 +20,7 @@ import {
   removeFiles,
   traverseFiles,
 } from "./build/helper.js";
+import { validateConfig } from "./build/validateConfig.js";
 import logger from "./logger.js";
 import { minifyAll } from "./minimize-js.js";
 import { openNextResolvePlugin } from "./plugins/resolve.js";
@@ -40,6 +41,7 @@ export async function build() {
 
   const config = await import(outputTmpPath + "/open-next.config.js");
   const opts = config.default as BuildOptions;
+  validateConfig(opts);
 
   const { root: monorepoRoot, packager } = findMonorepoRoot(
     path.join(process.cwd(), opts.appPath || "."),
@@ -73,7 +75,7 @@ export async function build() {
   if (!options.dangerous?.disableIncrementalCache) {
     await createCacheAssets(
       monorepoRoot,
-      options.dangerous?.disableDynamoDBCache,
+      options.dangerous?.disableTagCache,
     );
   }
   await createServerBundle(opts, options);
@@ -640,7 +642,7 @@ function compileCache(options: Options) {
             dangerousOptions?.disableIncrementalCache ?? false
           };`,
           `globalThis.disableDynamoDBCache = ${
-            dangerousOptions?.disableDynamoDBCache ?? false
+            dangerousOptions?.disableTagCache ?? false
           };`,
         ].join(""),
       },
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 93c1a0941..81b7aac55 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -3,7 +3,11 @@ import { createRequire as topLevelCreateRequire } from "node:module";
 
 import fs from "fs";
 import path from "path";
-import { BuildOptions, FunctionOptions } from "types/open-next";
+import {
+  BuildOptions,
+  FunctionOptions,
+  SplittedFunctionOptions,
+} from "types/open-next";
 import url from "url";
 
 import logger from "../logger.js";
@@ -25,7 +29,7 @@ export async function createServerBundle(
   const foundRoutes = new Set<string>();
   // Get all functions to build
   const defaultFn = options.default;
-  const functions = Object.entries(options.functions);
+  const functions = Object.entries(options.functions ?? {});
 
   const promises = functions.map(async ([name, fnOptions]) => {
     const routes = fnOptions.routes;
@@ -105,7 +109,7 @@ export async function createServerBundle(
 async function generateBundle(
   name: string,
   options: Options,
-  fnOptions: BuildOptions["functions"][string],
+  fnOptions: SplittedFunctionOptions,
 ) {
   const { appPath, appBuildOutputPath, outputDir, monorepoRoot } = options;
 
@@ -178,7 +182,7 @@ async function generateBundle(
   const disableRouting = isBefore13413 || options.externalMiddleware;
   const plugins = [
     openNextReplacementPlugin({
-      name: "requestHandlerOverride",
+      name: `requestHandlerOverride ${name}`,
       target: /core\/requestHandler.js/g,
       deletes: disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : [],
       replacements: disableRouting
@@ -190,7 +194,7 @@ async function generateBundle(
         : [],
     }),
     openNextReplacementPlugin({
-      name: "core/util",
+      name: `utilOverride ${name}`,
       target: /core\/util.js/g,
       deletes: [
         ...(disableNextPrebundledReact ? ["requireHooks"] : []),
@@ -200,6 +204,7 @@ async function generateBundle(
     }),
 
     openNextResolvePlugin({
+      fnName: name,
       overrides: {
         converter:
           typeof overrides.converter === "function"
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index d6c3d49bb..1a2585799 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -3,7 +3,7 @@ import url from "node:url";
 import fs from "fs";
 import path from "path";
 import { MiddlewareManifest } from "types/next-types";
-import { BuildOptions, IncludedConverter } from "types/open-next";
+import { IncludedConverter, SplittedFunctionOptions } from "types/open-next";
 
 import { openNextEdgePlugins } from "../../plugins/edge.js";
 import { openNextResolvePlugin } from "../../plugins/resolve.js";
@@ -89,7 +89,7 @@ export async function buildEdgeBundle({
 export async function generateEdgeBundle(
   name: string,
   options: Options,
-  fnOptions: BuildOptions["functions"][string],
+  fnOptions: SplittedFunctionOptions,
 ) {
   const { appBuildOutputPath, outputDir } = options;
 
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index fd8685fca..0d2df4fa9 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -87,7 +87,7 @@ export async function generateOutput(
     };
   }
   // Add edge functions
-  Object.entries(buildOptions.functions).forEach(([key, value]) => {
+  Object.entries(buildOptions.functions ?? {}).forEach(([key, value]) => {
     if (value.placement === "global") {
       edgeFunctions[key] = {
         bundle: `.open-next/functions/${key}`,
@@ -115,7 +115,7 @@ export async function generateOutput(
           : [
               {
                 from: ".open-next/cache",
-                to: "cache",
+                to: "_cache",
                 cached: false,
               },
             ]),
@@ -143,7 +143,7 @@ export async function generateOutput(
 
   // Then add function origins
   await Promise.all(
-    Object.entries(buildOptions.functions).map(async ([key, value]) => {
+    Object.entries(buildOptions.functions ?? {}).map(async ([key, value]) => {
       if (!value.placement || value.placement === "regional") {
         if (value.override?.generateDockerfile) {
           origins[key] = {
@@ -170,7 +170,7 @@ export async function generateOutput(
   ];
 
   // Then we add the routes
-  Object.entries(buildOptions.functions).forEach(([key, value]) => {
+  Object.entries(buildOptions.functions ?? {}).forEach(([key, value]) => {
     const patterns = "patterns" in value ? value.patterns : ["*"];
     patterns.forEach((pattern) => {
       behaviors.push({
@@ -220,12 +220,12 @@ export async function generateOutput(
     behaviors,
     additionalProps: {
       disableIncrementalCache: buildOptions.dangerous?.disableIncrementalCache,
-      disableTagCache: buildOptions.dangerous?.disableDynamoDBCache,
+      disableTagCache: buildOptions.dangerous?.disableTagCache,
       warmer: {
         handler: "index.handler",
         bundle: ".open-next/warmer-function",
       },
-      initializationFunction: buildOptions.dangerous?.disableDynamoDBCache
+      initializationFunction: buildOptions.dangerous?.disableTagCache
         ? undefined
         : {
             handler: "index.handler",
diff --git a/packages/open-next/src/build/validateConfig.ts b/packages/open-next/src/build/validateConfig.ts
new file mode 100644
index 000000000..8c355bb07
--- /dev/null
+++ b/packages/open-next/src/build/validateConfig.ts
@@ -0,0 +1,56 @@
+import {
+  BuildOptions,
+  FunctionOptions,
+  SplittedFunctionOptions,
+} from "types/open-next";
+
+import logger from "../logger.js";
+
+function validateFunctionOptions(fnOptions: FunctionOptions) {
+  if (fnOptions.runtime === "edge" && fnOptions.experimentalBundledNextServer) {
+    logger.warn(
+      "experimentalBundledNextServer has no effect for edge functions",
+    );
+  }
+  if (
+    fnOptions.override?.generateDockerfile &&
+    fnOptions.override.converter !== "node" &&
+    fnOptions.override.wrapper !== "node"
+  ) {
+    logger.warn(
+      "You've specified generateDockerfile without node converter and wrapper. Without custom converter and wrapper the dockerfile will not work",
+    );
+  }
+}
+
+function validateSplittedFunctionOptions(
+  fnOptions: SplittedFunctionOptions,
+  name: string,
+) {
+  validateFunctionOptions(fnOptions);
+  if (fnOptions.routes.length === 0) {
+    throw new Error(`Splitted function ${name} must have at least one route`);
+  }
+  if (fnOptions.runtime === "edge" && fnOptions.routes.length > 1) {
+    throw new Error(`Edge function ${name} can only have one route`);
+  }
+}
+
+export function validateConfig(config: BuildOptions) {
+  validateFunctionOptions(config.default);
+  Object.entries(config.functions ?? {}).forEach(([name, fnOptions]) => {
+    validateSplittedFunctionOptions(fnOptions, name);
+  });
+  if (config.dangerous?.disableIncrementalCache) {
+    logger.warn(
+      "You've disabled incremental cache. This means that ISR and SSG will not work.",
+    );
+  }
+  if (config.dangerous?.disableTagCache) {
+    logger.warn(
+      `You've disabled tag cache. 
+       This means that revalidatePath and revalidateTag from next/cache will not work.
+       It is safe to disable if you only use page router`,
+    );
+  }
+}
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 51355e7a4..2a1a2c191 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -50,7 +50,7 @@ export async function createMainHandler() {
   ).then((m) => m.default);
 
   const thisFunction = globalThis.fnName
-    ? config.functions[globalThis.fnName]
+    ? config.functions![globalThis.fnName]
     : config.default;
 
   globalThis.serverId = generateUniqueId();
diff --git a/packages/open-next/src/logger.ts b/packages/open-next/src/logger.ts
index b44dc254a..2d56fb9e6 100644
--- a/packages/open-next/src/logger.ts
+++ b/packages/open-next/src/logger.ts
@@ -1,3 +1,5 @@
+import chalk from "chalk";
+
 type LEVEL = "info" | "debug";
 
 let logLevel: LEVEL = "info";
@@ -6,9 +8,9 @@ export default {
   setLevel: (level: LEVEL) => (logLevel = level),
   debug: (...args: any[]) => {
     if (logLevel !== "debug") return;
-    console.log("DEBUG", ...args);
+    console.log(chalk.magenta("DEBUG"), ...args);
   },
   info: console.log,
-  warn: console.warn,
-  error: console.error,
+  warn: (...args: any[]) => console.warn(chalk.yellow("WARN"), ...args),
+  error: (...args: any[]) => console.error(chalk.red("ERROR"), ...args),
 };
diff --git a/packages/open-next/src/plugins/replacement.ts b/packages/open-next/src/plugins/replacement.ts
index 3f6d3bd8c..1e648fe10 100644
--- a/packages/open-next/src/plugins/replacement.ts
+++ b/packages/open-next/src/plugins/replacement.ts
@@ -1,5 +1,6 @@
 import { readFile } from "node:fs/promises";
 
+import chalk from "chalk";
 import { Plugin } from "esbuild";
 
 import logger from "../logger.js";
@@ -66,7 +67,8 @@ export function openNextReplacementPlugin({
               `\/\/#override (${id})\n([\\s\\S]*?)\/\/#endOverride`,
             );
             logger.debug(
-              `Open-next plugin ${name} -- Deleting override for ${id}`,
+              chalk.blue(`Open-next replacement plugin ${name}`),
+              ` -- Deleting override for ${id}`,
             );
             contents = contents.replace(pattern, "");
           }),
@@ -88,7 +90,8 @@ export function openNextReplacementPlugin({
                 "g",
               );
               logger.debug(
-                `Open-next plugin ${name} -- Applying override for ${id} from ${fp}`,
+                chalk.blue(`Open-next replacement plugin ${name}`),
+                `-- Applying override for ${id} from ${fp}`,
               );
               contents = contents.replace(pattern, replacement);
             }
diff --git a/packages/open-next/src/plugins/resolve.ts b/packages/open-next/src/plugins/resolve.ts
index 3e8a07d6b..eafa04874 100644
--- a/packages/open-next/src/plugins/resolve.ts
+++ b/packages/open-next/src/plugins/resolve.ts
@@ -9,6 +9,8 @@ import type {
   IncludedWrapper,
 } from "types/open-next";
 
+import logger from "../logger.js";
+
 export interface IPluginSettings {
   overrides: {
     wrapper?: IncludedWrapper;
@@ -18,16 +20,21 @@ export interface IPluginSettings {
     queue?: IncludedQueue;
     incrementalCache?: IncludedIncrementalCache;
   };
+  fnName?: string;
 }
 
 /**
  * @param opts.overrides - The name of the overrides to use
  * @returns
  */
-export function openNextResolvePlugin({ overrides }: IPluginSettings): Plugin {
+export function openNextResolvePlugin({
+  overrides,
+  fnName,
+}: IPluginSettings): Plugin {
   return {
     name: "opennext-resolve",
     setup(build) {
+      logger.debug(`OpenNext Resolve plugin for ${fnName}`);
       build.onLoad({ filter: /core\/resolve.js/g }, async (args) => {
         let contents = readFileSync(args.path, "utf-8");
         if (overrides?.wrapper) {
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 19a891005..9d5361e11 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -31,10 +31,10 @@ export type InternalResult = {
 
 export interface DangerousOptions {
   /**
-   * The dynamo db cache is used for revalidateTags and revalidatePath.
+   * The tag cache is used for revalidateTags and revalidatePath.
    * @default false
    */
-  disableDynamoDBCache?: boolean;
+  disableTagCache?: boolean;
   /**
    * The incremental cache is used for ISR and SSG.
    * Disable this only if you use only SSR
@@ -217,7 +217,7 @@ export interface SplittedFunctionOptions extends FunctionOptions {
 //TODO: rename to OpenNextConfig or something similar
 export interface BuildOptions {
   default: FunctionOptions;
-  functions: Record<string, SplittedFunctionOptions>;
+  functions?: Record<string, SplittedFunctionOptions>;
 
   /**
    * Override the default middleware
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ba43901c7..c3a70d179 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -267,6 +267,9 @@ importers:
       '@tsconfig/node18':
         specifier: ^1.0.1
         version: 1.0.3
+      chalk:
+        specifier: ^5.3.0
+        version: 5.3.0
       esbuild:
         specifier: 0.19.2
         version: 0.19.2
@@ -7588,7 +7591,6 @@ packages:
   /chalk@5.3.0:
     resolution: {integrity: sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==}
     engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}
-    dev: true
 
   /character-entities-html4@1.1.4:
     resolution: {integrity: sha512-HRcDxZuZqMx3/a+qrzxdBKBPUpxWEq9xw2OPZ3a/174ihfrQKVsFhqtthBInFy1zZ9GgZyFXOatNujm8M+El3g==}

From 9d03aecbce2681bc1fed0580c2c638d73aee9f15 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 20 Jan 2024 18:03:48 +0100
Subject: [PATCH 041/102] fix EISDIR issue with copying traced symlink

---
 .../open-next/src/build/copyTracedFiles.ts    | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 086850aca..fcac84c2e 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -4,12 +4,13 @@ import {
   mkdirSync,
   readdirSync,
   readFileSync,
+  readlinkSync,
   statSync,
+  symlinkSync,
   writeFileSync,
 } from "fs";
 import path from "path";
 
-//TODO: need to make it work with monorepo
 export async function copyTracedFiles(
   buildOutputPath: string,
   packagePath: string,
@@ -110,7 +111,25 @@ export async function copyTracedFiles(
       return;
     }
     mkdirSync(path.dirname(to), { recursive: true });
-    copyFileSync(from, to);
+    let symlink = null;
+    // For pnpm symlink we need to do that
+    // see https://github.com/vercel/next.js/blob/498f342b3552d6fc6f1566a1cc5acea324ce0dec/packages/next/src/build/utils.ts#L1932
+    try {
+      symlink = readlinkSync(from);
+    } catch (e) {
+      //Ignore
+    }
+    if (symlink) {
+      try {
+        symlinkSync(symlink, to);
+      } catch (e: any) {
+        if (e.code !== "EEXIST") {
+          throw e;
+        }
+      }
+    } else {
+      copyFileSync(from, to);
+    }
   });
 
   mkdirSync(path.join(outputDir, ".next"), { recursive: true });

From 2f3bd610908af9f549f29fa9e3cb5d37204ad36c Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 20 Jan 2024 19:16:36 +0100
Subject: [PATCH 042/102] added override name to the output for better IAC
 support

---
 .../adapters/image-optimization-adapter.ts    |  38 ++---
 packages/open-next/src/adapters/middleware.ts |  73 ++++-----
 .../open-next/src/adapters/warmer-function.ts | 138 +++++++++---------
 .../open-next/src/build/generateOutput.ts     | 118 +++++++++++++--
 .../open-next/src/cache/incremental/s3.ts     |   1 +
 .../open-next/src/cache/incremental/types.ts  |   1 +
 packages/open-next/src/cache/tag/dynamoDb.ts  |   1 +
 packages/open-next/src/cache/tag/types.ts     |   1 +
 .../open-next/src/converters/aws-apigw-v1.ts  |   1 +
 .../open-next/src/converters/aws-apigw-v2.ts  |   1 +
 .../src/converters/aws-cloudfront.ts          |   1 +
 packages/open-next/src/converters/dummy.ts    |   1 +
 packages/open-next/src/converters/edge.ts     |   1 +
 packages/open-next/src/converters/node.ts     |   1 +
 .../src/converters/sqs-revalidate.ts          |   1 +
 packages/open-next/src/queue/sqs.ts           |   1 +
 packages/open-next/src/queue/types.ts         |   1 +
 packages/open-next/src/types/open-next.ts     |  30 ++--
 18 files changed, 272 insertions(+), 138 deletions(-)

diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index d8bb958f6..f27ef0f42 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -19,7 +19,7 @@ import {
 } from "next/dist/server/image-optimizer";
 // @ts-ignore
 import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
-import { InternalEvent, InternalResult } from "types/open-next.js";
+import { ImageLoader, InternalEvent, InternalResult } from "types/open-next.js";
 
 import { createGenericHandler } from "../core/createGenericHandler.js";
 import { awsLogger, debug, error } from "./logger.js";
@@ -167,21 +167,25 @@ const resolveLoader = () => {
   if (typeof openNextParams?.loader === "function") {
     return openNextParams.loader();
   } else {
-    return Promise.resolve(async (key: string) => {
-      const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, "");
-      const response = await s3Client.send(
-        new GetObjectCommand({
-          Bucket: BUCKET_NAME,
-          Key: keyPrefix
-            ? keyPrefix + "/" + key.replace(/^\//, "")
-            : key.replace(/^\//, ""),
-        }),
-      );
-      return {
-        body: response.Body,
-        contentType: response.ContentType,
-        cacheControl: response.CacheControl,
-      };
+    return Promise.resolve<ImageLoader>({
+      name: "s3",
+      // @ts-ignore
+      load: async (key: string) => {
+        const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, "");
+        const response = await s3Client.send(
+          new GetObjectCommand({
+            Bucket: BUCKET_NAME,
+            Key: keyPrefix
+              ? keyPrefix + "/" + key.replace(/^\//, "")
+              : key.replace(/^\//, ""),
+          }),
+        );
+        return {
+          body: response.Body,
+          contentType: response.ContentType,
+          cacheControl: response.CacheControl,
+        };
+      },
     });
   }
 };
@@ -220,7 +224,7 @@ async function downloadHandler(
       // Download image from S3
       // note: S3 expects keys without leading `/`
 
-      const response = await loader(url.href);
+      const response = await loader.load(url.href);
 
       if (!response.body) {
         throw new Error("Empty response body from the S3 request.");
diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
index 196cd859b..df078cf8f 100644
--- a/packages/open-next/src/adapters/middleware.ts
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -1,4 +1,4 @@
-import { InternalEvent, Origin } from "types/open-next";
+import { InternalEvent, Origin, OriginResolver } from "types/open-next";
 
 import { debug, error } from "../adapters/logger";
 import { createGenericHandler } from "../core/createGenericHandler";
@@ -9,41 +9,44 @@ const resolveOriginResolver = () => {
   if (typeof openNextParams?.originResolver === "function") {
     return openNextParams.originResolver();
   } else {
-    return Promise.resolve(async (_path: string) => {
-      try {
-        const origin = JSON.parse(
-          process.env.OPEN_NEXT_ORIGIN ?? "{}",
-        ) as Record<string, Origin>;
-        for (const [key, value] of Object.entries(
-          globalThis.openNextConfig.functions ?? {},
-        )) {
-          if (
-            value.patterns.some((pattern) => {
-              // Convert cloudfront pattern to regex
-              return new RegExp(
-                // transform glob pattern to regex
-                "/" +
-                  pattern
-                    .replace(/\*\*/g, "(.*)")
-                    .replace(/\*/g, "([^/]*)")
-                    .replace(/\//g, "\\/")
-                    .replace(/\?/g, "."),
-              ).test(_path);
-            })
-          ) {
-            debug("Using origin", key, value.patterns);
-            return origin[key];
+    return Promise.resolve<OriginResolver>({
+      name: "env",
+      resolve: async (_path: string) => {
+        try {
+          const origin = JSON.parse(
+            process.env.OPEN_NEXT_ORIGIN ?? "{}",
+          ) as Record<string, Origin>;
+          for (const [key, value] of Object.entries(
+            globalThis.openNextConfig.functions ?? {},
+          )) {
+            if (
+              value.patterns.some((pattern) => {
+                // Convert cloudfront pattern to regex
+                return new RegExp(
+                  // transform glob pattern to regex
+                  "/" +
+                    pattern
+                      .replace(/\*\*/g, "(.*)")
+                      .replace(/\*/g, "([^/]*)")
+                      .replace(/\//g, "\\/")
+                      .replace(/\?/g, "."),
+                ).test(_path);
+              })
+            ) {
+              debug("Using origin", key, value.patterns);
+              return origin[key];
+            }
           }
+          if (origin["default"]) {
+            debug("Using default origin", origin["default"]);
+            return origin["default"];
+          }
+          return false as const;
+        } catch (e) {
+          error("Error while resolving origin", e);
+          return false as const;
         }
-        if (origin["default"]) {
-          debug("Using default origin", origin["default"]);
-          return origin["default"];
-        }
-        return false as const;
-      } catch (e) {
-        error("Error while resolving origin", e);
-        return false as const;
-      }
+      },
     });
   }
 };
@@ -57,7 +60,7 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
     debug("Middleware intercepted event", internalEvent);
     let origin: Origin | false = false;
     if (!result.isExternalRewrite) {
-      origin = await originResolver(result.internalEvent.rawPath);
+      origin = await originResolver.resolve(result.internalEvent.rawPath);
     }
     return {
       type: "middleware",
diff --git a/packages/open-next/src/adapters/warmer-function.ts b/packages/open-next/src/adapters/warmer-function.ts
index 141822764..00addca4b 100644
--- a/packages/open-next/src/adapters/warmer-function.ts
+++ b/packages/open-next/src/adapters/warmer-function.ts
@@ -1,3 +1,5 @@
+import { Warmer } from "types/open-next.js";
+
 import { createGenericHandler } from "../core/createGenericHandler.js";
 import { debug, error } from "./logger.js";
 import { generateUniqueId } from "./util.js";
@@ -20,75 +22,79 @@ const resolveWarmerInvoke = async () => {
   if (typeof openNextParams?.invokeFunction === "function") {
     return await openNextParams.invokeFunction();
   } else {
-    return Promise.resolve(async (warmerId: string) => {
-      const { InvokeCommand, LambdaClient } = await import(
-        "@aws-sdk/client-lambda"
-      );
-      const lambda = new LambdaClient({});
-      const warmParams = JSON.parse(process.env.WARM_PARAMS!) as {
-        concurrency: number;
-        function: string;
-      }[];
-
-      for (const warmParam of warmParams) {
-        const { concurrency: CONCURRENCY, function: FUNCTION_NAME } = warmParam;
-        debug({
-          event: "warmer invoked",
-          functionName: FUNCTION_NAME,
-          concurrency: CONCURRENCY,
-          warmerId,
-        });
-        const ret = await Promise.all(
-          Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
-            try {
-              return lambda.send(
-                new InvokeCommand({
-                  FunctionName: FUNCTION_NAME,
-                  InvocationType: "RequestResponse",
-                  Payload: Buffer.from(
-                    JSON.stringify({
-                      type: "warmer",
-                      warmerId,
-                      index: i,
-                      concurrency: CONCURRENCY,
-                      delay: 75,
-                    } satisfies WarmerEvent),
-                  ),
-                }),
-              );
-            } catch (e) {
-              error(`failed to warm up #${i}`, e);
-              // ignore error
-            }
-          }),
+    return Promise.resolve<Warmer>({
+      name: "aws-invoke",
+      invoke: async (warmerId: string) => {
+        const { InvokeCommand, LambdaClient } = await import(
+          "@aws-sdk/client-lambda"
         );
+        const lambda = new LambdaClient({});
+        const warmParams = JSON.parse(process.env.WARM_PARAMS!) as {
+          concurrency: number;
+          function: string;
+        }[];
+
+        for (const warmParam of warmParams) {
+          const { concurrency: CONCURRENCY, function: FUNCTION_NAME } =
+            warmParam;
+          debug({
+            event: "warmer invoked",
+            functionName: FUNCTION_NAME,
+            concurrency: CONCURRENCY,
+            warmerId,
+          });
+          const ret = await Promise.all(
+            Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
+              try {
+                return lambda.send(
+                  new InvokeCommand({
+                    FunctionName: FUNCTION_NAME,
+                    InvocationType: "RequestResponse",
+                    Payload: Buffer.from(
+                      JSON.stringify({
+                        type: "warmer",
+                        warmerId,
+                        index: i,
+                        concurrency: CONCURRENCY,
+                        delay: 75,
+                      } satisfies WarmerEvent),
+                    ),
+                  }),
+                );
+              } catch (e) {
+                error(`failed to warm up #${i}`, e);
+                // ignore error
+              }
+            }),
+          );
 
-        // Print status
+          // Print status
 
-        const warmedServerIds = ret
-          .map((r, i) => {
-            if (r?.StatusCode !== 200 || !r?.Payload) {
-              error(`failed to warm up #${i}:`, r?.Payload?.toString());
-              return;
-            }
-            const payload = JSON.parse(
-              Buffer.from(r.Payload).toString(),
-            ) as WarmerResponse;
-            return {
-              statusCode: r.StatusCode,
-              payload,
-              type: "warmer" as const,
-            };
-          })
-          .filter((r): r is Exclude<typeof r, undefined> => !!r);
+          const warmedServerIds = ret
+            .map((r, i) => {
+              if (r?.StatusCode !== 200 || !r?.Payload) {
+                error(`failed to warm up #${i}:`, r?.Payload?.toString());
+                return;
+              }
+              const payload = JSON.parse(
+                Buffer.from(r.Payload).toString(),
+              ) as WarmerResponse;
+              return {
+                statusCode: r.StatusCode,
+                payload,
+                type: "warmer" as const,
+              };
+            })
+            .filter((r): r is Exclude<typeof r, undefined> => !!r);
 
-        debug({
-          event: "warmer result",
-          sent: CONCURRENCY,
-          success: warmedServerIds.length,
-          uniqueServersWarmed: [...new Set(warmedServerIds)].length,
-        });
-      }
+          debug({
+            event: "warmer result",
+            sent: CONCURRENCY,
+            success: warmedServerIds.length,
+            uniqueServersWarmed: [...new Set(warmedServerIds)].length,
+          });
+        }
+      },
     });
   }
 };
@@ -103,7 +109,7 @@ async function defaultHandler() {
 
   const invokeFn = await resolveWarmerInvoke();
 
-  await invokeFn(warmerId);
+  await invokeFn.invoke(warmerId);
 
   return {
     type: "warmer",
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index 0d2df4fa9..790fe4be8 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -1,7 +1,14 @@
 import * as fs from "node:fs";
 import path from "node:path";
 
-import { BuildOptions, FunctionOptions } from "types/open-next";
+import {
+  BaseOverride,
+  BuildOptions,
+  DefaultOverrideOptions,
+  FunctionOptions,
+  LazyLoadedOverride,
+  OverrideOptions,
+} from "types/open-next";
 
 import { getBuildId } from "./helper.js";
 
@@ -13,14 +20,27 @@ type BaseFunction = {
 type OpenNextFunctionOrigin = {
   type: "function";
   streaming?: boolean;
+  wrapper: string;
+  converter: string;
 } & BaseFunction;
 
 type OpenNextECSOrigin = {
   type: "ecs";
   bundle: string;
+  wrapper: string;
+  converter: string;
   dockerfile: string;
 };
 
+type CommonOverride = {
+  queue: string;
+  incrementalCache: string;
+  tagCache: string;
+};
+
+type OpenNextServerFunctionOrigin = OpenNextFunctionOrigin & CommonOverride;
+type OpenNextServerECSOrigin = OpenNextECSOrigin & CommonOverride;
+
 type OpenNextS3Origin = {
   type: "s3";
   originPath: string;
@@ -33,18 +53,28 @@ type OpenNextS3Origin = {
 };
 
 type OpenNextOrigins =
-  | OpenNextFunctionOrigin
-  | OpenNextECSOrigin
+  | OpenNextServerFunctionOrigin
+  | OpenNextServerECSOrigin
   | OpenNextS3Origin;
 
+type ImageFnOrigins = OpenNextFunctionOrigin & { imageLoader: string };
+type ImageECSOrigins = OpenNextECSOrigin & { imageLoader: string };
+
+type ImageOrigins = ImageFnOrigins | ImageECSOrigins;
+
+type DefaultOrigins = {
+  s3: OpenNextS3Origin;
+  default: OpenNextServerFunctionOrigin | OpenNextServerECSOrigin;
+  imageOptimizer: ImageOrigins;
+};
+
 interface OpenNextOutput {
   edgeFunctions: {
     [key: string]: BaseFunction;
+  } & {
+    middleware?: BaseFunction & { pathResolver: string };
   };
-  origins: {
-    s3: OpenNextS3Origin;
-    default: OpenNextFunctionOrigin | OpenNextECSOrigin;
-    imageOptimizer: OpenNextFunctionOrigin | OpenNextECSOrigin;
+  origins: DefaultOrigins & {
     [key: string]: OpenNextOrigins;
   };
   behaviors: {
@@ -74,6 +104,53 @@ async function canStream(opts: FunctionOptions) {
   }
 }
 
+async function extractOverrideName(
+  defaultName: string,
+  override?: LazyLoadedOverride<BaseOverride> | string,
+) {
+  if (!override) {
+    return defaultName;
+  }
+  if (typeof override === "string") {
+    return override;
+  } else {
+    const overrideModule = await override();
+    return overrideModule.name;
+  }
+}
+
+async function extractOverrideFn(override?: DefaultOverrideOptions) {
+  if (!override) {
+    return {
+      wrapper: "aws-lambda",
+      converter: "aws-apigw-v2",
+    };
+  }
+  const wrapper = await extractOverrideName("aws-lambda", override.wrapper);
+  const converter = await extractOverrideName(
+    "aws-apigw-v2",
+    override.converter,
+  );
+  return { wrapper, converter };
+}
+
+async function extractCommonOverride(override?: OverrideOptions) {
+  if (!override) {
+    return {
+      queue: "sqs",
+      incrementalCache: "s3",
+      tagCache: "dynamodb",
+    };
+  }
+  const queue = await extractOverrideName("sqs", override.queue);
+  const incrementalCache = await extractOverrideName(
+    "s3",
+    override.incrementalCache,
+  );
+  const tagCache = await extractOverrideName("dynamodb", override.tagCache);
+  return { queue, incrementalCache, tagCache };
+}
+
 export async function generateOutput(
   outputPath: string,
   buildOptions: BuildOptions,
@@ -84,14 +161,20 @@ export async function generateOutput(
     edgeFunctions.middleware = {
       bundle: ".open-next/middleware",
       handler: "handler.handler",
+      pathResolver: await extractOverrideName(
+        "pattern-env",
+        buildOptions.middleware!.originResolver,
+      ),
+      ...(await extractOverrideFn(buildOptions.middleware?.override)),
     };
   }
   // Add edge functions
-  Object.entries(buildOptions.functions ?? {}).forEach(([key, value]) => {
+  Object.entries(buildOptions.functions ?? {}).forEach(async ([key, value]) => {
     if (value.placement === "global") {
       edgeFunctions[key] = {
         bundle: `.open-next/functions/${key}`,
         handler: "index.handler",
+        ...(await extractOverrideFn(value.override)),
       };
     }
   });
@@ -99,7 +182,8 @@ export async function generateOutput(
   const defaultOriginCanstream = await canStream(buildOptions.default);
 
   // First add s3 origins and image optimization
-  const origins: OpenNextOutput["origins"] = {
+
+  const defaultOrigins: DefaultOrigins = {
     s3: {
       type: "s3",
       originPath: "_assets",
@@ -126,21 +210,33 @@ export async function generateOutput(
       handler: "index.handler",
       bundle: ".open-next/image-optimization-function",
       streaming: false,
+      imageLoader: await extractOverrideName(
+        "s3",
+        buildOptions.imageOptimization?.loader,
+      ),
+      ...(await extractOverrideFn(buildOptions.imageOptimization?.override)),
     },
     default: buildOptions.default.override?.generateDockerfile
       ? {
           type: "ecs",
           bundle: ".open-next/server-functions/default",
           dockerfile: ".open-next/server-functions/default/Dockerfile",
+          ...(await extractOverrideFn(buildOptions.default.override)),
+          ...(await extractCommonOverride(buildOptions.default.override)),
         }
       : {
           type: "function",
           handler: "index.handler",
           bundle: ".open-next/server-functions/default",
           streaming: defaultOriginCanstream,
+          ...(await extractOverrideFn(buildOptions.default.override)),
+          ...(await extractCommonOverride(buildOptions.default.override)),
         },
   };
 
+  //@ts-expect-error - Not sure how to fix typing here, it complains about the type of imageOptimizer and s3
+  const origins: OpenNextOutput["origins"] = defaultOrigins;
+
   // Then add function origins
   await Promise.all(
     Object.entries(buildOptions.functions ?? {}).map(async ([key, value]) => {
@@ -150,6 +246,8 @@ export async function generateOutput(
             type: "ecs",
             bundle: `.open-next/server-functions/${key}`,
             dockerfile: `.open-next/server-functions/${key}/Dockerfile`,
+            ...(await extractOverrideFn(value.override)),
+            ...(await extractCommonOverride(value.override)),
           };
         } else {
           const streaming = await canStream(value);
@@ -158,6 +256,8 @@ export async function generateOutput(
             handler: "index.handler",
             bundle: `.open-next/server-functions/${key}`,
             streaming,
+            ...(await extractOverrideFn(value.override)),
+            ...(await extractCommonOverride(value.override)),
           };
         }
       }
diff --git a/packages/open-next/src/cache/incremental/s3.ts b/packages/open-next/src/cache/incremental/s3.ts
index 592d4fead..2e277ba19 100644
--- a/packages/open-next/src/cache/incremental/s3.ts
+++ b/packages/open-next/src/cache/incremental/s3.ts
@@ -72,6 +72,7 @@ const incrementalCache: IncrementalCache = {
       }),
     );
   },
+  name: "s3",
 };
 
 export default incrementalCache;
diff --git a/packages/open-next/src/cache/incremental/types.ts b/packages/open-next/src/cache/incremental/types.ts
index c2a4468de..60217cf50 100644
--- a/packages/open-next/src/cache/incremental/types.ts
+++ b/packages/open-next/src/cache/incremental/types.ts
@@ -46,4 +46,5 @@ export type IncrementalCache = {
     isFetch?: IsFetch,
   ): Promise<void>;
   delete(key: string): Promise<void>;
+  name: string;
 };
diff --git a/packages/open-next/src/cache/tag/dynamoDb.ts b/packages/open-next/src/cache/tag/dynamoDb.ts
index 5cf76bb2c..8eccf433f 100644
--- a/packages/open-next/src/cache/tag/dynamoDb.ts
+++ b/packages/open-next/src/cache/tag/dynamoDb.ts
@@ -144,6 +144,7 @@ const tagCache: TagCache = {
       error("Failed to batch write dynamo item", e);
     }
   },
+  name: "dynamoDb",
 };
 
 export default tagCache;
diff --git a/packages/open-next/src/cache/tag/types.ts b/packages/open-next/src/cache/tag/types.ts
index eb4643787..533e725c5 100644
--- a/packages/open-next/src/cache/tag/types.ts
+++ b/packages/open-next/src/cache/tag/types.ts
@@ -5,4 +5,5 @@ export type TagCache = {
   writeTags(
     tags: { tag: string; path: string; revalidatedAt?: number }[],
   ): Promise<void>;
+  name: string;
 };
diff --git a/packages/open-next/src/converters/aws-apigw-v1.ts b/packages/open-next/src/converters/aws-apigw-v1.ts
index 7750fc457..dabd88bcc 100644
--- a/packages/open-next/src/converters/aws-apigw-v1.ts
+++ b/packages/open-next/src/converters/aws-apigw-v1.ts
@@ -105,4 +105,5 @@ function convertToApiGatewayProxyResult(
 export default {
   convertFrom: convertFromAPIGatewayProxyEvent,
   convertTo: convertToApiGatewayProxyResult,
+  name: "aws-apigw-v1",
 } as Converter;
diff --git a/packages/open-next/src/converters/aws-apigw-v2.ts b/packages/open-next/src/converters/aws-apigw-v2.ts
index 6af649453..02787ed9b 100644
--- a/packages/open-next/src/converters/aws-apigw-v2.ts
+++ b/packages/open-next/src/converters/aws-apigw-v2.ts
@@ -88,4 +88,5 @@ function convertToApiGatewayProxyResultV2(
 export default {
   convertFrom: convertFromAPIGatewayProxyEventV2,
   convertTo: convertToApiGatewayProxyResultV2,
+  name: "aws-apigw-v2",
 } as Converter;
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index 50c25e3f4..968fcd8d7 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -169,4 +169,5 @@ async function convertToCloudFrontRequestResult(
 export default {
   convertFrom: convertFromCloudFrontRequestEvent,
   convertTo: convertToCloudFrontRequestResult,
+  name: "aws-cloudfront",
 } as Converter;
diff --git a/packages/open-next/src/converters/dummy.ts b/packages/open-next/src/converters/dummy.ts
index c1bc833b1..917b50e13 100644
--- a/packages/open-next/src/converters/dummy.ts
+++ b/packages/open-next/src/converters/dummy.ts
@@ -18,6 +18,7 @@ const converter: Converter<DummyEventOrResult, DummyEventOrResult> = {
       original: internalResult,
     });
   },
+  name: "dummy",
 };
 
 export default converter;
diff --git a/packages/open-next/src/converters/edge.ts b/packages/open-next/src/converters/edge.ts
index bad375747..ef5af0ee7 100644
--- a/packages/open-next/src/converters/edge.ts
+++ b/packages/open-next/src/converters/edge.ts
@@ -68,6 +68,7 @@ const converter: Converter<
       });
     }
   },
+  name: "edge",
 };
 
 export default converter;
diff --git a/packages/open-next/src/converters/node.ts b/packages/open-next/src/converters/node.ts
index 84ee58961..662c0cb11 100644
--- a/packages/open-next/src/converters/node.ts
+++ b/packages/open-next/src/converters/node.ts
@@ -49,6 +49,7 @@ const converter: Converter = {
     headers: internalResult.headers,
     statusCode: internalResult.statusCode,
   }),
+  name: "node",
 };
 
 export default converter;
diff --git a/packages/open-next/src/converters/sqs-revalidate.ts b/packages/open-next/src/converters/sqs-revalidate.ts
index d701782ce..459a93be3 100644
--- a/packages/open-next/src/converters/sqs-revalidate.ts
+++ b/packages/open-next/src/converters/sqs-revalidate.ts
@@ -19,6 +19,7 @@ const converter: Converter<RevalidateEvent, { type: "revalidate" }> = {
       type: "revalidate",
     });
   },
+  name: "sqs-revalidate",
 };
 
 export default converter;
diff --git a/packages/open-next/src/queue/sqs.ts b/packages/open-next/src/queue/sqs.ts
index cd3c90524..cbc2bdfec 100644
--- a/packages/open-next/src/queue/sqs.ts
+++ b/packages/open-next/src/queue/sqs.ts
@@ -22,6 +22,7 @@ const queue: Queue = {
       }),
     );
   },
+  name: "sqs",
 };
 
 export default queue;
diff --git a/packages/open-next/src/queue/types.ts b/packages/open-next/src/queue/types.ts
index 4916b7f89..eaba1cb6e 100644
--- a/packages/open-next/src/queue/types.ts
+++ b/packages/open-next/src/queue/types.ts
@@ -9,4 +9,5 @@ export interface QueueMessage {
 
 export interface Queue {
   send(message: QueueMessage): Promise<void>;
+  name: string;
 }
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 9d5361e11..3cb16aab5 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -43,7 +43,10 @@ export interface DangerousOptions {
   disableIncrementalCache?: boolean;
 }
 
-export type LazyLoadedOverride<T> = () => Promise<T>;
+export type BaseOverride = {
+  name: string;
+};
+export type LazyLoadedOverride<T extends BaseOverride> = () => Promise<T>;
 
 export type OpenNextHandler<
   E extends BaseEventOrResult = InternalEvent,
@@ -53,7 +56,7 @@ export type OpenNextHandler<
 export type Converter<
   E extends BaseEventOrResult = InternalEvent,
   R extends BaseEventOrResult = InternalResult,
-> = {
+> = BaseOverride & {
   convertFrom: (event: any) => Promise<E>;
   convertTo: (result: R, originalRequest?: any) => any;
 };
@@ -69,19 +72,22 @@ export type WrapperHandler<
 export type Wrapper<
   E extends BaseEventOrResult = InternalEvent,
   R extends BaseEventOrResult = InternalResult,
-> = {
+> = BaseOverride & {
   wrapper: WrapperHandler<E, R>;
-  name: string;
   supportStreaming: boolean;
 };
 
-type Warmer = (warmerId: string) => Promise<void>;
+export type Warmer = BaseOverride & {
+  invoke: (warmerId: string) => Promise<void>;
+};
 
-type ImageLoader = (url: string) => Promise<{
-  body?: Readable;
-  contentType?: string;
-  cacheControl?: string;
-}>;
+export type ImageLoader = BaseOverride & {
+  load: (url: string) => Promise<{
+    body?: Readable;
+    contentType?: string;
+    cacheControl?: string;
+  }>;
+};
 
 export interface Origin {
   host: string;
@@ -89,7 +95,9 @@ export interface Origin {
   port?: number;
   customHeaders?: Record<string, string>;
 }
-type OriginResolver = (path: string) => Promise<Origin | false>;
+export type OriginResolver = BaseOverride & {
+  resolve: (path: string) => Promise<Origin | false>;
+};
 
 export type IncludedWrapper =
   | "aws-lambda"

From 6b64a1d13180514f68f0b2c407f959b7cafb4409 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 21 Jan 2024 10:39:46 +0100
Subject: [PATCH 043/102] rename BuildOptions remove some unused options
 properly handle minify

---
 packages/open-next/src/build.ts               | 28 ++++---------------
 .../open-next/src/build/createServerBundle.ts | 24 ++++++++++++----
 .../src/build/edge/createEdgeBundle.ts        |  6 ++--
 .../open-next/src/build/generateOutput.ts     |  4 +--
 packages/open-next/src/build/helper.ts        | 21 +++++++-------
 .../open-next/src/build/validateConfig.ts     |  4 +--
 .../src/core/createGenericHandler.ts          |  6 ++--
 .../open-next/src/core/createMainHandler.ts   |  4 +--
 packages/open-next/src/types/open-next.ts     |  3 +-
 9 files changed, 49 insertions(+), 51 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index cad287162..49fc8e93d 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -11,23 +11,22 @@ import { createServerBundle } from "./build/createServerBundle.js";
 import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js";
 import { generateOutput } from "./build/generateOutput.js";
 import {
+  BuildOptions,
   esbuildAsync,
   esbuildSync,
   getBuildId,
   getHtmlPages,
   normalizeOptions,
-  Options,
   removeFiles,
   traverseFiles,
 } from "./build/helper.js";
 import { validateConfig } from "./build/validateConfig.js";
 import logger from "./logger.js";
-import { minifyAll } from "./minimize-js.js";
 import { openNextResolvePlugin } from "./plugins/resolve.js";
-import { BuildOptions } from "./types/open-next.js";
+import { OpenNextConfig } from "./types/open-next.js";
 
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
-let options: Options;
+let options: BuildOptions;
 
 export type PublicFiles = {
   files: string[];
@@ -40,7 +39,7 @@ export async function build() {
   createOpenNextConfigBundle(outputTmpPath);
 
   const config = await import(outputTmpPath + "/open-next.config.js");
-  const opts = config.default as BuildOptions;
+  const opts = config.default as OpenNextConfig;
   validateConfig(opts);
 
   const { root: monorepoRoot, packager } = findMonorepoRoot(
@@ -73,19 +72,13 @@ export async function build() {
 
   createStaticAssets();
   if (!options.dangerous?.disableIncrementalCache) {
-    await createCacheAssets(
-      monorepoRoot,
-      options.dangerous?.disableTagCache,
-    );
+    await createCacheAssets(monorepoRoot, options.dangerous?.disableTagCache);
   }
   await createServerBundle(opts, options);
   await createRevalidationBundle();
   createImageOptimizationBundle();
   await createWarmerBundle();
   await generateOutput(options.appBuildOutputPath, opts);
-  if (options.minify) {
-    await minifyServerBundle();
-  }
 }
 
 function createOpenNextConfigBundle(tempDir: string) {
@@ -245,15 +238,6 @@ async function createWarmerBundle() {
   );
 }
 
-async function minifyServerBundle() {
-  logger.info(`Minimizing server function...`);
-  const { outputDir } = options;
-  await minifyAll(path.join(outputDir, "server-function"), {
-    compress_json: true,
-    mangle: true,
-  });
-}
-
 async function createRevalidationBundle() {
   logger.info(`Bundling revalidation function...`);
 
@@ -626,7 +610,7 @@ async function createCacheAssets(
 /* Server Helper Functions */
 /***************************/
 
-function compileCache(options: Options) {
+function compileCache(options: BuildOptions) {
   const outfile = path.join(options.outputDir, ".build", "cache.cjs");
   const dangerousOptions = options.dangerous;
   esbuildSync(
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 81b7aac55..2ef842d8c 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -4,27 +4,28 @@ import { createRequire as topLevelCreateRequire } from "node:module";
 import fs from "fs";
 import path from "path";
 import {
-  BuildOptions,
   FunctionOptions,
+  OpenNextConfig,
   SplittedFunctionOptions,
 } from "types/open-next";
 import url from "url";
 
 import logger from "../logger.js";
+import { minifyAll } from "../minimize-js.js";
 import { openNextReplacementPlugin } from "../plugins/replacement.js";
 import { openNextResolvePlugin } from "../plugins/resolve.js";
 import { bundleNextServer } from "./bundleNextServer.js";
 import { copyTracedFiles } from "./copyTracedFiles.js";
 import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
-import type { Options } from "./helper.js";
+import type { BuildOptions } from "./helper.js";
 import { compareSemver, esbuildAsync, traverseFiles } from "./helper.js";
 
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
 export async function createServerBundle(
-  options: BuildOptions,
-  buildRuntimeOptions: Options,
+  options: OpenNextConfig,
+  buildRuntimeOptions: BuildOptions,
 ) {
   const foundRoutes = new Set<string>();
   // Get all functions to build
@@ -108,7 +109,7 @@ export async function createServerBundle(
 
 async function generateBundle(
   name: string,
-  options: Options,
+  options: BuildOptions,
   fnOptions: SplittedFunctionOptions,
 ) {
   const { appPath, appBuildOutputPath, outputDir, monorepoRoot } = options;
@@ -253,6 +254,10 @@ async function generateBundle(
     addMonorepoEntrypoint(outputPath, packagePath);
   }
 
+  if (fnOptions.minify) {
+    await minifyServerBundle(outputPath);
+  }
+
   const shouldGenerateDocker = shouldGenerateDockerfile(fnOptions);
   if (shouldGenerateDocker) {
     fs.writeFileSync(
@@ -289,3 +294,12 @@ function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
     [`export * from "./${packagePosixPath}/index.mjs";`].join(""),
   );
 }
+
+async function minifyServerBundle(outputDir: string) {
+  logger.info(`Minimizing server function...`);
+
+  await minifyAll(outputDir, {
+    compress_json: true,
+    mangle: true,
+  });
+}
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index 1a2585799..e9f743def 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -7,7 +7,7 @@ import { IncludedConverter, SplittedFunctionOptions } from "types/open-next";
 
 import { openNextEdgePlugins } from "../../plugins/edge.js";
 import { openNextResolvePlugin } from "../../plugins/resolve.js";
-import { esbuildAsync, Options } from "../helper.js";
+import { BuildOptions, esbuildAsync } from "../helper.js";
 
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
@@ -21,7 +21,7 @@ interface BuildEdgeBundleOptions {
   }[];
   entrypoint: string;
   outfile: string;
-  options: Options;
+  options: BuildOptions;
   defaultConverter?: IncludedConverter;
   additionalInject?: string;
 }
@@ -88,7 +88,7 @@ export async function buildEdgeBundle({
 
 export async function generateEdgeBundle(
   name: string,
-  options: Options,
+  options: BuildOptions,
   fnOptions: SplittedFunctionOptions,
 ) {
   const { appBuildOutputPath, outputDir } = options;
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index 790fe4be8..a1e25cf73 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -3,10 +3,10 @@ import path from "node:path";
 
 import {
   BaseOverride,
-  BuildOptions,
   DefaultOverrideOptions,
   FunctionOptions,
   LazyLoadedOverride,
+  OpenNextConfig,
   OverrideOptions,
 } from "types/open-next";
 
@@ -153,7 +153,7 @@ async function extractCommonOverride(override?: OverrideOptions) {
 
 export async function generateOutput(
   outputPath: string,
-  buildOptions: BuildOptions,
+  buildOptions: OpenNextConfig,
 ) {
   const edgeFunctions: OpenNextOutput["edgeFunctions"] = {};
   const isExternalMiddleware = buildOptions.middleware?.external ?? false;
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
index 530716f3f..df6c262c5 100644
--- a/packages/open-next/src/build/helper.ts
+++ b/packages/open-next/src/build/helper.ts
@@ -8,16 +8,16 @@ import {
   BuildOptions as ESBuildOptions,
   buildSync,
 } from "esbuild";
-import { BuildOptions } from "types/open-next.js";
+import { OpenNextConfig } from "types/open-next.js";
 
 import logger from "../logger.js";
 
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
-export type Options = ReturnType<typeof normalizeOptions>;
+export type BuildOptions = ReturnType<typeof normalizeOptions>;
 
-export function normalizeOptions(opts: BuildOptions, root: string) {
+export function normalizeOptions(opts: OpenNextConfig, root: string) {
   const appPath = path.join(process.cwd(), opts.appPath || ".");
   const buildOutputPath = path.join(process.cwd(), opts.buildOutputPath || ".");
   const outputDir = path.join(buildOutputPath, ".open-next");
@@ -40,9 +40,7 @@ export function normalizeOptions(opts: BuildOptions, root: string) {
     appPublicPath: path.join(appPath, "public"),
     outputDir,
     tempDir: path.join(outputDir, ".build"),
-    minify:
-      opts.default.minify ?? Boolean(process.env.OPEN_NEXT_MINIFY) ?? false,
-    debug: opts.default.debug ?? Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
+    debug: Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
     buildCommand: opts.buildCommand,
     dangerous: opts.dangerous,
     externalMiddleware: opts.middleware?.external ?? false,
@@ -57,7 +55,10 @@ function findNextPackageJsonPath(appPath: string, root: string) {
     : path.join(root, "./package.json");
 }
 
-export function esbuildSync(esbuildOptions: ESBuildOptions, options: Options) {
+export function esbuildSync(
+  esbuildOptions: ESBuildOptions,
+  options: BuildOptions,
+) {
   const { openNextVersion, debug } = options;
   const result = buildSync({
     target: "esnext",
@@ -92,7 +93,7 @@ export function esbuildSync(esbuildOptions: ESBuildOptions, options: Options) {
 
 export async function esbuildAsync(
   esbuildOptions: ESBuildOptions,
-  options: Options,
+  options: BuildOptions,
 ) {
   const { openNextVersion, debug } = options;
   const result = await buildAsync({
@@ -197,11 +198,11 @@ export function getBuildId(dotNextPath: string) {
     .trim();
 }
 
-export function getOpenNextVersion() {
+export function getOpenNextVersion(): string {
   return require(path.join(__dirname, "../../package.json")).version;
 }
 
-export function getNextVersion(nextPackageJsonPath: string) {
+export function getNextVersion(nextPackageJsonPath: string): string {
   const version = require(nextPackageJsonPath)?.dependencies?.next;
   // require('next/package.json').version
 
diff --git a/packages/open-next/src/build/validateConfig.ts b/packages/open-next/src/build/validateConfig.ts
index 8c355bb07..7fc2f147c 100644
--- a/packages/open-next/src/build/validateConfig.ts
+++ b/packages/open-next/src/build/validateConfig.ts
@@ -1,6 +1,6 @@
 import {
-  BuildOptions,
   FunctionOptions,
+  OpenNextConfig,
   SplittedFunctionOptions,
 } from "types/open-next";
 
@@ -36,7 +36,7 @@ function validateSplittedFunctionOptions(
   }
 }
 
-export function validateConfig(config: BuildOptions) {
+export function validateConfig(config: OpenNextConfig) {
   validateFunctionOptions(config.default);
   Object.entries(config.functions ?? {}).forEach(([name, fnOptions]) => {
     validateSplittedFunctionOptions(fnOptions, name);
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
index 6bcb6470f..5754aafb9 100644
--- a/packages/open-next/src/core/createGenericHandler.ts
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -1,9 +1,9 @@
 import type {
   BaseEventOrResult,
-  BuildOptions,
   DefaultOverrideOptions,
   InternalEvent,
   InternalResult,
+  OpenNextConfig,
   OpenNextHandler,
 } from "types/open-next";
 
@@ -11,7 +11,7 @@ import { debug } from "../adapters/logger";
 import { resolveConverter, resolveWrapper } from "./resolve";
 
 declare global {
-  var openNextConfig: Partial<BuildOptions>;
+  var openNextConfig: Partial<OpenNextConfig>;
 }
 
 type HandlerType =
@@ -37,7 +37,7 @@ export async function createGenericHandler<
 >(handler: GenericHandler<Type, E, R>) {
   //First we load the config
   // @ts-expect-error
-  const config: BuildOptions = await import("./open-next.config.js").then(
+  const config: OpenNextConfig = await import("./open-next.config.js").then(
     (m) => m.default,
   );
 
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 2a1a2c191..0104d1a66 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -1,6 +1,6 @@
 import type { AsyncLocalStorage } from "node:async_hooks";
 
-import type { BuildOptions, OverrideOptions } from "types/open-next";
+import type { OpenNextConfig, OverrideOptions } from "types/open-next";
 
 import { debug } from "../adapters/logger";
 import { generateUniqueId } from "../adapters/util";
@@ -45,7 +45,7 @@ async function resolveIncrementalCache(
 
 export async function createMainHandler() {
   //First we load the config
-  const config: BuildOptions = await import(
+  const config: OpenNextConfig = await import(
     process.cwd() + "/open-next.config.js"
   ).then((m) => m.default);
 
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 3cb16aab5..ea37285d1 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -222,8 +222,7 @@ export interface SplittedFunctionOptions extends FunctionOptions {
   patterns: string[];
 }
 
-//TODO: rename to OpenNextConfig or something similar
-export interface BuildOptions {
+export interface OpenNextConfig {
   default: FunctionOptions;
   functions?: Record<string, SplittedFunctionOptions>;
 

From 618d1ea262357539943c6c668911f6b12b220fce Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 21 Jan 2024 12:37:08 +0100
Subject: [PATCH 044/102] normalize locale path before passing to middleware

---
 .../open-next/src/build/copyTracedFiles.ts    |  9 ++++--
 packages/open-next/src/core/requestHandler.ts | 21 ++++++++++----
 .../open-next/src/core/routing/middleware.ts  | 28 +++++++++++++++++--
 3 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index fcac84c2e..ebba311ec 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -49,8 +49,13 @@ export async function copyTracedFiles(
   });
 
   // create directory for pages
-  mkdirSync(path.join(outputDir, ".next/server/pages"), { recursive: true });
-  mkdirSync(path.join(outputDir, ".next/server/app"), { recursive: true });
+  if (existsSync(path.join(standaloneDir, ".next/server/pages"))) {
+    mkdirSync(path.join(outputDir, ".next/server/pages"), { recursive: true });
+  }
+  if (existsSync(path.join(standaloneDir, ".next/server/app"))) {
+    mkdirSync(path.join(outputDir, ".next/server/app"), { recursive: true });
+  }
+
   mkdirSync(path.join(outputDir, ".next/server/chunks"), { recursive: true });
 
   const computeCopyFilesForPage = (pagePath: string) => {
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 33184a012..7c548552a 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -7,9 +7,9 @@ import {
 } from "http/index.js";
 import { InternalEvent, InternalResult } from "types/open-next";
 
-import { debug, error } from "../adapters/logger";
+import { debug, error, warn } from "../adapters/logger";
 import { convertRes, createServerResponse, proxyRequest } from "./routing/util";
-import routingHandler from "./routingHandler";
+import routingHandler, { MiddlewareOutputEvent } from "./routingHandler";
 import { requestHandler, setNextjsPrebundledReact } from "./util";
 
 // This is used to identify requests in the cache
@@ -25,7 +25,17 @@ export async function openNextHandler(
   debug("internalEvent", internalEvent);
 
   //#override withRouting
-  const preprocessResult = await routingHandler(internalEvent);
+  let preprocessResult: InternalResult | MiddlewareOutputEvent = {
+    internalEvent: internalEvent,
+    isExternalRewrite: false,
+    headers: {},
+    origin: false,
+  };
+  try {
+    preprocessResult = await routingHandler(internalEvent);
+  } catch (e) {
+    warn("Routing failed.", e);
+  }
   //#endOverride
 
   if ("type" in preprocessResult) {
@@ -54,10 +64,11 @@ export async function openNextHandler(
     };
     const requestId = Math.random().toString(36);
     const internalResult = await globalThis.__als.run(requestId, async () => {
+      const preprocessedResult = preprocessResult as MiddlewareOutputEvent;
       const req = new IncomingMessage(reqProps);
       const res = createServerResponse(
         preprocessedEvent,
-        preprocessResult.headers as Record<string, string | string[]>,
+        preprocessedResult.headers as Record<string, string | string[]>,
         responseStreaming,
       );
 
@@ -65,7 +76,7 @@ export async function openNextHandler(
         req,
         res,
         preprocessedEvent,
-        preprocessResult.isExternalRewrite,
+        preprocessedResult.isExternalRewrite,
       );
 
       const { statusCode, headers, isBase64Encoded, body } = convertRes(res);
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 88b71651d..b7dd0e0f6 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -29,12 +29,35 @@ type MiddlewareOutputEvent = InternalEvent & {
 // and res.body prior to processing the next-server.
 // @returns undefined | res.end()
 
+// NOTE: We need to normalize the locale path before passing it to the middleware
+// See https://github.com/vercel/next.js/blob/39589ff35003ba73f92b7f7b349b3fdd3458819f/packages/next/src/shared/lib/i18n/normalize-locale-path.ts#L15
+function normalizeLocalePath(pathname: string) {
+  // first item will be empty string from splitting at first char
+  const pathnameParts = pathname.split("/");
+  const locales = NextConfig.i18n?.locales;
+
+  (locales || []).some((locale) => {
+    if (
+      pathnameParts[1] &&
+      pathnameParts[1].toLowerCase() === locale.toLowerCase()
+    ) {
+      pathnameParts.splice(1, 1);
+      pathname = pathnameParts.join("/") || "/";
+      return true;
+    }
+    return false;
+  });
+
+  return locales && !pathname.endsWith("/") ? `${pathname}/` : pathname;
+}
+
 //    if res.end() is return, the parent needs to return and not process next server
 export async function handleMiddleware(
   internalEvent: InternalEvent,
 ): Promise<MiddlewareOutputEvent | InternalResult> {
   const { rawPath, query } = internalEvent;
-  const hasMatch = middleMatch.some((r) => r.test(rawPath));
+  const normalizedPath = normalizeLocalePath(rawPath);
+  const hasMatch = middleMatch.some((r) => r.test(normalizedPath));
   if (!hasMatch) return internalEvent;
   // We bypass the middleware if the request is internal
   if (internalEvent.headers["x-isr"]) return internalEvent;
@@ -42,9 +65,10 @@ export async function handleMiddleware(
   const host = internalEvent.headers.host
     ? `https://${internalEvent.headers.host}`
     : "http://localhost:3000";
-  const initialUrl = new URL(rawPath, host);
+  const initialUrl = new URL(normalizedPath, host);
   initialUrl.search = convertToQueryString(query);
   const url = initialUrl.toString();
+  console.log("url", url, normalizedPath);
 
   // @ts-expect-error - This is bundled
   const middleware = await import("./middleware.mjs");

From 809ae1aaeb470ec1ab78364e93cd399c0b8da9a6 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 21 Jan 2024 12:54:27 +0100
Subject: [PATCH 045/102] Copy necessary static files

---
 .../open-next/src/build/copyTracedFiles.ts    | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index ebba311ec..1c0ced36d 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -176,14 +176,29 @@ export async function copyTracedFiles(
   //TODO: Find what else we need to copy
   const copyStaticFile = (filePath: string) => {
     if (existsSync(path.join(standaloneNextDir, filePath))) {
+      mkdirSync(path.dirname(path.join(outputDir, ".next", filePath)), {
+        recursive: true,
+      });
       copyFileSync(
         path.join(standaloneNextDir, filePath),
         path.join(outputDir, ".next", filePath),
       );
     }
   };
-  copyStaticFile("server/pages/404.html");
-  copyStaticFile("server/pages/500.html");
+  // Get all the static files - Should be only for pages dir
+  if (hasPageDir) {
+    const staticFiles: Record<string, string> = JSON.parse(
+      readFileSync(
+        path.join(standaloneNextDir, "server/pages-manifest.json"),
+        "utf8",
+      ),
+    );
+    Object.values(staticFiles).forEach((f: string) => {
+      if (f.endsWith(".html")) {
+        copyStaticFile(`server/${f}`);
+      }
+    });
+  }
 
   console.timeEnd("copyTracedFiles");
 }

From 8076ae6897d68f3c4e81b6090438b2bb3c80112a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 23 Jan 2024 10:30:40 +0100
Subject: [PATCH 046/102] fix issues with fallback and i18n in page router

---
 .../open-next/src/build/copyTracedFiles.ts    | 40 +++++++++++++++++--
 packages/open-next/src/core/requestHandler.ts | 28 ++++++++++++-
 .../open-next/src/core/routing/matcher.ts     |  8 +++-
 packages/open-next/src/core/routingHandler.ts |  5 ++-
 packages/open-next/src/types/next-types.ts    |  1 +
 5 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 1c0ced36d..fe74196b9 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -10,6 +10,7 @@ import {
   writeFileSync,
 } from "fs";
 import path from "path";
+import { NextConfig, PrerenderManifest } from "types/next-types";
 
 export async function copyTracedFiles(
   buildOutputPath: string,
@@ -186,14 +187,45 @@ export async function copyTracedFiles(
     }
   };
   // Get all the static files - Should be only for pages dir
+  // Ideally we would filter only those that might get accessed in this specific functions
+  // Maybe even move this to s3 directly
   if (hasPageDir) {
-    const staticFiles: Record<string, string> = JSON.parse(
+    // First we get truly static files - i.e. pages without getStaticProps
+    const staticFiles: Array<string> = Object.values(
+      JSON.parse(
+        readFileSync(
+          path.join(standaloneNextDir, "server/pages-manifest.json"),
+          "utf8",
+        ),
+      ),
+    );
+    // Then we need to get all fallback: true dynamic routes html
+    const prerenderManifest = JSON.parse(
       readFileSync(
-        path.join(standaloneNextDir, "server/pages-manifest.json"),
+        path.join(standaloneNextDir, "prerender-manifest.json"),
         "utf8",
       ),
-    );
-    Object.values(staticFiles).forEach((f: string) => {
+    ) as PrerenderManifest;
+    const config = JSON.parse(
+      readFileSync(
+        path.join(standaloneNextDir, "required-server-files.json"),
+        "utf8",
+      ),
+    ).config as NextConfig;
+    const locales = config.i18n?.locales;
+    Object.values(prerenderManifest.dynamicRoutes).forEach((route) => {
+      if (typeof route.fallback === "string") {
+        if (locales) {
+          locales.forEach((locale) => {
+            staticFiles.push(`pages/${locale}${route.fallback}`);
+          });
+        } else {
+          staticFiles.push(`pages${route.fallback}`);
+        }
+      }
+    });
+
+    staticFiles.forEach((f: string) => {
       if (f.endsWith(".html")) {
         copyStaticFile(`server/${f}`);
       }
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 7c548552a..7efc7ee8b 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -126,8 +126,34 @@ async function processRequest(
       await requestHandler(req, res);
     }
   } catch (e: any) {
+    // This might fail when using bundled next, importing won't do the trick either
+    if (e.constructor.name === "NoFallbackError") {
+      // Do we need to handle _not-found
+      // Ideally this should never get triggered and be intercepted by the routing handler
+      tryRenderError("404", res, internalEvent);
+    } else {
+      error("NextJS request failed.", e);
+      tryRenderError("500", res, internalEvent);
+    }
+  }
+}
+
+async function tryRenderError(
+  type: "404" | "500",
+  res: OpenNextNodeResponse,
+  internalEvent: InternalEvent,
+) {
+  try {
+    const _req = new IncomingMessage({
+      method: "GET",
+      url: `/${type}`,
+      headers: internalEvent.headers,
+      body: internalEvent.body,
+      remoteAddress: internalEvent.remoteAddress,
+    });
+    await requestHandler(_req, res);
+  } catch (e) {
     error("NextJS request failed.", e);
-    //TODO: we could return the next 500 page here
     res.setHeader("Content-Type", "application/json");
     res.end(
       JSON.stringify(
diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index d279d8cdb..ef60611c2 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -263,7 +263,13 @@ export function handleFallbackFalse(
       const routeRegexExp = new RegExp(routeRegex);
       return routeRegexExp.test(rawPath);
     });
-  if (routeFallback && !Object.keys(routes).includes(rawPath)) {
+  const locales = NextConfig.i18n?.locales;
+  const routesAlreadyHaveLocale =
+    locales !== undefined && locales.includes(rawPath.split("/")[1]);
+  const localizedPath = routesAlreadyHaveLocale
+    ? rawPath
+    : `/${NextConfig.i18n?.defaultLocale}${rawPath}`;
+  if (routeFallback && !Object.keys(routes).includes(localizedPath)) {
     return {
       ...internalEvent,
       rawPath: "/404",
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index b74c8f34e..bb5a1358c 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -31,8 +31,6 @@ export default async function routingHandler(
 
   let internalEvent = fixDataPage(event, BuildId);
 
-  internalEvent = handleFallbackFalse(internalEvent, PrerenderManifest);
-
   const redirect = handleRedirects(internalEvent, RoutesManifest.redirects);
   if (redirect) {
     debug("redirect", redirect);
@@ -72,6 +70,9 @@ export default async function routingHandler(
     isExternalRewrite = afterRewrites.isExternalRewrite;
   }
 
+  // We want to run this just before the dynamic route check
+  internalEvent = handleFallbackFalse(internalEvent, PrerenderManifest);
+
   const isDynamicRoute = RoutesManifest.routes.dynamic.some((route) =>
     new RegExp(route.regex).test(event.rawPath),
   );
diff --git a/packages/open-next/src/types/next-types.ts b/packages/open-next/src/types/next-types.ts
index 5bab5c37f..75100e90a 100644
--- a/packages/open-next/src/types/next-types.ts
+++ b/packages/open-next/src/types/next-types.ts
@@ -66,6 +66,7 @@ export interface NextConfig {
   skipTrailingSlashRedirect?: boolean;
   i18n?: {
     locales: string[];
+    defaultLocale: string;
   };
   experimental: {
     serverActions?: boolean;

From e643578559d519cd01f2006836da8e00f472b624 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 23 Jan 2024 11:12:53 +0100
Subject: [PATCH 047/102] Add a big warning for build on windows

---
 packages/open-next/src/build.ts | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 49fc8e93d..6a11bdec5 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -1,5 +1,6 @@
 import cp from "node:child_process";
 import fs, { readFileSync } from "node:fs";
+import os from "node:os";
 import path from "node:path";
 import url from "node:url";
 
@@ -35,6 +36,14 @@ export type PublicFiles = {
 export async function build() {
   const outputTmpPath = path.join(process.cwd(), ".open-next", ".build");
 
+  if (os.platform() === "win32") {
+    logger.error(
+      "OpenNext is not properly supported on Windows. On windows you should use WSL. It might works or it might fail in unpredictable way at runtime",
+    );
+    // Wait 10s here so that the user see this message
+    await new Promise((resolve) => setTimeout(resolve, 10000));
+  }
+
   // Compile open-next.config.ts
   createOpenNextConfigBundle(outputTmpPath);
 

From 770ac8ae7efc4ebba29d577eb1db70525ab20361 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 26 Jan 2024 10:43:17 +0100
Subject: [PATCH 048/102] fix for cloudflare workers

---
 packages/open-next/src/adapters/middleware.ts |  4 +-
 packages/open-next/src/build.ts               | 51 +++++++------------
 .../open-next/src/build/createServerBundle.ts | 16 ++++--
 .../src/build/edge/createEdgeBundle.ts        | 36 +++++++++----
 packages/open-next/src/build/helper.ts        | 12 ++++-
 packages/open-next/src/converters/edge.ts     |  5 +-
 .../src/core/createGenericHandler.ts          |  2 +-
 .../open-next/src/core/createMainHandler.ts   |  2 +-
 packages/open-next/src/core/routing/util.ts   |  2 +-
 packages/open-next/src/plugins/edge.ts        | 23 +++++++++
 packages/open-next/src/wrappers/cloudflare.ts |  4 +-
 11 files changed, 101 insertions(+), 56 deletions(-)

diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
index df078cf8f..b47e8d451 100644
--- a/packages/open-next/src/adapters/middleware.ts
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -18,7 +18,7 @@ const resolveOriginResolver = () => {
           ) as Record<string, Origin>;
           for (const [key, value] of Object.entries(
             globalThis.openNextConfig.functions ?? {},
-          )) {
+          ).filter(([key]) => key !== "default")) {
             if (
               value.patterns.some((pattern) => {
                 // Convert cloudfront pattern to regex
@@ -52,8 +52,6 @@ const resolveOriginResolver = () => {
 };
 
 const defaultHandler = async (internalEvent: InternalEvent) => {
-  // TODO: We need to handle splitted function here
-  // We should probably create an host resolver to redirect correctly
   const originResolver = await resolveOriginResolver();
   const result = await routingHandler(internalEvent);
   if ("internalEvent" in result) {
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 6a11bdec5..49824687a 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -13,6 +13,7 @@ import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js";
 import { generateOutput } from "./build/generateOutput.js";
 import {
   BuildOptions,
+  copyOpenNextConfig,
   esbuildAsync,
   esbuildSync,
   getBuildId,
@@ -47,7 +48,7 @@ export async function build() {
   // Compile open-next.config.ts
   createOpenNextConfigBundle(outputTmpPath);
 
-  const config = await import(outputTmpPath + "/open-next.config.js");
+  const config = await import(outputTmpPath + "/open-next.config.mjs");
   const opts = config.default as OpenNextConfig;
   validateConfig(opts);
 
@@ -77,7 +78,7 @@ export async function build() {
   compileCache(options);
 
   // Compile middleware
-  await createMiddleware();
+  await createMiddleware(opts);
 
   createStaticAssets();
   if (!options.dangerous?.disableIncrementalCache) {
@@ -93,9 +94,9 @@ export async function build() {
 function createOpenNextConfigBundle(tempDir: string) {
   buildSync({
     entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
-    outfile: path.join(tempDir, "open-next.config.js"),
+    outfile: path.join(tempDir, "open-next.config.mjs"),
     bundle: true,
-    format: "cjs",
+    format: "esm",
     target: ["node18"],
   });
 }
@@ -195,12 +196,12 @@ function printOpenNextVersion() {
 function initOutputDir() {
   const { outputDir, tempDir } = options;
   const openNextConfig = readFileSync(
-    path.join(tempDir, "open-next.config.js"),
+    path.join(tempDir, "open-next.config.mjs"),
     "utf8",
   );
   fs.rmSync(outputDir, { recursive: true, force: true });
   fs.mkdirSync(tempDir, { recursive: true });
-  fs.writeFileSync(path.join(tempDir, "open-next.config.js"), openNextConfig);
+  fs.writeFileSync(path.join(tempDir, "open-next.config.mjs"), openNextConfig);
 }
 
 async function createWarmerBundle() {
@@ -212,11 +213,8 @@ async function createWarmerBundle() {
   const outputPath = path.join(outputDir, "warmer-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
-  // Copy open-next.config.js into the bundle
-  fs.copyFileSync(
-    path.join(options.tempDir, "open-next.config.js"),
-    path.join(outputPath, "open-next.config.js"),
-  );
+  // Copy open-next.config.mjs into the bundle
+  copyOpenNextConfig(options.tempDir, outputPath);
 
   // Build Lambda code
   // note: bundle in OpenNext package b/c the adatper relys on the
@@ -256,11 +254,8 @@ async function createRevalidationBundle() {
   const outputPath = path.join(outputDir, "revalidation-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
-  //Copy open-next.config.js into the bundle
-  fs.copyFileSync(
-    path.join(options.tempDir, "open-next.config.js"),
-    path.join(outputPath, "open-next.config.js"),
-  );
+  //Copy open-next.config.mjs into the bundle
+  copyOpenNextConfig(options.tempDir, outputPath);
 
   // Build Lambda code
   esbuildAsync(
@@ -295,11 +290,8 @@ function createImageOptimizationBundle() {
   const outputPath = path.join(outputDir, "image-optimization-function");
   fs.mkdirSync(outputPath, { recursive: true });
 
-  // Copy open-next.config.js into the bundle
-  fs.copyFileSync(
-    path.join(options.tempDir, "open-next.config.js"),
-    path.join(outputPath, "open-next.config.js"),
-  );
+  // Copy open-next.config.mjs into the bundle
+  copyOpenNextConfig(options.tempDir, outputPath);
 
   // Build Lambda code (1st pass)
   // note: bundle in OpenNext package b/c the adapter relies on the
@@ -597,11 +589,8 @@ async function createCacheAssets(
         options,
       );
 
-      //Copy open-next.config.js into the bundle
-      fs.copyFileSync(
-        path.join(options.tempDir, "open-next.config.js"),
-        path.join(providerPath, "open-next.config.js"),
-      );
+      //Copy open-next.config.mjs into the bundle
+      copyOpenNextConfig(options.tempDir, providerPath);
 
       // TODO: check if metafiles doesn't contain duplicates
       fs.writeFileSync(
@@ -645,7 +634,7 @@ function compileCache(options: BuildOptions) {
   return outfile;
 }
 
-async function createMiddleware() {
+async function createMiddleware(config: OpenNextConfig) {
   console.info(`Bundling middleware function...`);
 
   const { appBuildOutputPath, outputDir, externalMiddleware } = options;
@@ -683,17 +672,15 @@ async function createMiddleware() {
     outputPath = path.join(outputDir, "middleware");
     fs.mkdirSync(outputPath, { recursive: true });
 
-    // Copy open-next.config.js
-    fs.copyFileSync(
-      path.join(options.tempDir, "open-next.config.js"),
-      path.join(outputPath, "open-next.config.js"),
-    );
+    // Copy open-next.config.mjs
+    copyOpenNextConfig(options.tempDir, outputPath);
 
     // Bundle middleware
     await buildEdgeBundle({
       entrypoint: path.join(__dirname, "adapters", "middleware.js"),
       outfile: path.join(outputPath, "handler.mjs"),
       ...commonMiddlewareOptions,
+      overrides: config.middleware?.override,
       defaultConverter: "aws-cloudfront",
     });
   } else {
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 2ef842d8c..0e0e3d38e 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -18,7 +18,12 @@ import { bundleNextServer } from "./bundleNextServer.js";
 import { copyTracedFiles } from "./copyTracedFiles.js";
 import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
 import type { BuildOptions } from "./helper.js";
-import { compareSemver, esbuildAsync, traverseFiles } from "./helper.js";
+import {
+  compareSemver,
+  copyOpenNextConfig,
+  esbuildAsync,
+  traverseFiles,
+} from "./helper.js";
 
 const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
@@ -153,11 +158,12 @@ async function generateBundle(
     );
   }
 
-  // Copy open-next.config.js
-  fs.copyFileSync(
-    path.join(outputDir, ".build", "open-next.config.js"),
-    path.join(outputPath, packagePath, "open-next.config.js"),
+  // Copy open-next.config.mjs
+  copyOpenNextConfig(
+    path.join(outputDir, ".build"),
+    path.join(outputPath, packagePath),
   );
+
   // Copy all necessary traced files
   copyTracedFiles(
     appBuildOutputPath,
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index e9f743def..18c624e34 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -3,11 +3,15 @@ import url from "node:url";
 import fs from "fs";
 import path from "path";
 import { MiddlewareManifest } from "types/next-types";
-import { IncludedConverter, SplittedFunctionOptions } from "types/open-next";
+import {
+  DefaultOverrideOptions,
+  IncludedConverter,
+  SplittedFunctionOptions,
+} from "types/open-next";
 
 import { openNextEdgePlugins } from "../../plugins/edge.js";
 import { openNextResolvePlugin } from "../../plugins/resolve.js";
-import { BuildOptions, esbuildAsync } from "../helper.js";
+import { BuildOptions, copyOpenNextConfig, esbuildAsync } from "../helper.js";
 
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
@@ -22,6 +26,7 @@ interface BuildEdgeBundleOptions {
   entrypoint: string;
   outfile: string;
   options: BuildOptions;
+  overrides?: DefaultOverrideOptions;
   defaultConverter?: IncludedConverter;
   additionalInject?: string;
 }
@@ -34,6 +39,7 @@ export async function buildEdgeBundle({
   outfile,
   options,
   defaultConverter,
+  overrides,
   additionalInject,
 }: BuildEdgeBundleOptions) {
   await esbuildAsync(
@@ -48,8 +54,14 @@ export async function buildEdgeBundle({
       plugins: [
         openNextResolvePlugin({
           overrides: {
-            wrapper: "aws-lambda",
-            converter: defaultConverter,
+            wrapper:
+              typeof overrides?.wrapper === "string"
+                ? overrides.wrapper
+                : "aws-lambda",
+            converter:
+              typeof overrides?.converter === "string"
+                ? overrides.converter
+                : defaultConverter,
           },
         }),
         openNextEdgePlugins({
@@ -75,9 +87,15 @@ export async function buildEdgeBundle({
       mainFields: ["module", "main"],
       banner: {
         js: `
+  ${
+    overrides?.wrapper === "cloudflare"
+      ? ""
+      : `
   const require = (await import("node:module")).createRequire(import.meta.url);
   const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
   const __dirname = (await import("node:path")).dirname(__filename);
+  `
+  }
   ${additionalInject ?? ""}
   `,
       },
@@ -86,6 +104,8 @@ export async function buildEdgeBundle({
   );
 }
 
+export function copyMiddlewareAssetsAndWasm({}) {}
+
 export async function generateEdgeBundle(
   name: string,
   options: BuildOptions,
@@ -97,11 +117,8 @@ export async function generateEdgeBundle(
   const outputPath = path.join(outputDir, "server-functions", name);
   fs.mkdirSync(outputPath, { recursive: true });
 
-  // Copy open-next.config.js
-  fs.copyFileSync(
-    path.join(outputDir, ".build", "open-next.config.js"),
-    path.join(outputPath, "open-next.config.js"),
-  );
+  // Copy open-next.config.mjs
+  copyOpenNextConfig(path.join(outputDir, ".build"), outputPath);
 
   // Load middleware manifest
   const middlewareManifest = JSON.parse(
@@ -134,5 +151,6 @@ export async function generateEdgeBundle(
     entrypoint: path.join(__dirname, "../../adapters", "edge-adapter.js"),
     outfile: path.join(outputPath, "index.mjs"),
     options,
+    overrides: fnOptions.override,
   });
 }
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
index df6c262c5..2ece8544e 100644
--- a/packages/open-next/src/build/helper.ts
+++ b/packages/open-next/src/build/helper.ts
@@ -70,7 +70,7 @@ export function esbuildSync(
     sourcemap: debug ? "inline" : false,
     sourcesContent: false,
     ...esbuildOptions,
-    external: ["./open-next.config.js", ...(esbuildOptions.external ?? [])],
+    external: ["./open-next.config.mjs", ...(esbuildOptions.external ?? [])],
     banner: {
       ...esbuildOptions.banner,
       js: [
@@ -109,7 +109,7 @@ export async function esbuildAsync(
     external: [
       ...(esbuildOptions.external ?? []),
       "next",
-      "./open-next.config.js",
+      "./open-next.config.mjs",
     ],
     banner: {
       ...esbuildOptions.banner,
@@ -229,3 +229,11 @@ export function compareSemver(v1: string, v2: string): number {
   if (minor1 !== minor2) return minor1 - minor2;
   return patch1 - patch2;
 }
+
+export function copyOpenNextConfig(tempDir: string, outputPath: string) {
+  // Copy open-next.config.mjs
+  fs.copyFileSync(
+    path.join(tempDir, "open-next.config.mjs"),
+    path.join(outputPath, "open-next.config.mjs"),
+  );
+}
diff --git a/packages/open-next/src/converters/edge.ts b/packages/open-next/src/converters/edge.ts
index ef5af0ee7..4d6305068 100644
--- a/packages/open-next/src/converters/edge.ts
+++ b/packages/open-next/src/converters/edge.ts
@@ -50,12 +50,15 @@ const converter: Converter<
     if ("internalEvent" in result) {
       const url = result.isExternalRewrite
         ? result.internalEvent.url
-        : `https://${result.internalEvent.headers.host}${result.internalEvent.url}`;
+        : `https://${result.origin ?? result.internalEvent.headers.host}${
+            result.internalEvent.url
+          }`;
       const req = new Request(url, {
         body: result.internalEvent.body,
         method: result.internalEvent.method,
         headers: result.internalEvent.headers,
       });
+
       return fetch(req);
     } else {
       const headers = new Headers();
diff --git a/packages/open-next/src/core/createGenericHandler.ts b/packages/open-next/src/core/createGenericHandler.ts
index 5754aafb9..a9a3fa08c 100644
--- a/packages/open-next/src/core/createGenericHandler.ts
+++ b/packages/open-next/src/core/createGenericHandler.ts
@@ -37,7 +37,7 @@ export async function createGenericHandler<
 >(handler: GenericHandler<Type, E, R>) {
   //First we load the config
   // @ts-expect-error
-  const config: OpenNextConfig = await import("./open-next.config.js").then(
+  const config: OpenNextConfig = await import("./open-next.config.mjs").then(
     (m) => m.default,
   );
 
diff --git a/packages/open-next/src/core/createMainHandler.ts b/packages/open-next/src/core/createMainHandler.ts
index 0104d1a66..f09ba0707 100644
--- a/packages/open-next/src/core/createMainHandler.ts
+++ b/packages/open-next/src/core/createMainHandler.ts
@@ -46,7 +46,7 @@ async function resolveIncrementalCache(
 export async function createMainHandler() {
   //First we load the config
   const config: OpenNextConfig = await import(
-    process.cwd() + "/open-next.config.js"
+    process.cwd() + "/open-next.config.mjs"
   ).then((m) => m.default);
 
   const thisFunction = globalThis.fnName
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index a6138d1b7..576945662 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -1,6 +1,5 @@
 import crypto from "node:crypto";
 import { OutgoingHttpHeaders } from "node:http";
-import { request } from "node:https";
 
 import { BuildId, HtmlPages } from "config/index.js";
 import type { IncomingMessage, StreamCreator } from "http/index.js";
@@ -202,6 +201,7 @@ export async function proxyRequest(
   res: OpenNextNodeResponse,
 ) {
   const { url, headers, method, body } = internalEvent;
+  const request = await import("node:https").then((m) => m.request);
   debug("proxyRequest", url);
   await new Promise<void>((resolve, reject) => {
     const filteredHeaders = filterHeadersForProxy(headers);
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index 4103ecdaf..73bc236ef 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -52,6 +52,29 @@ export function openNextEdgePlugins({
         };
       });
 
+      //COpied from https://github.com/cloudflare/next-on-pages/blob/7a18efb5cab4d86c8e3e222fc94ea88ac05baffd/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts#L86-L112
+
+      build.onResolve({ filter: /^node:/ }, ({ kind, path }) => {
+        // this plugin converts `require("node:*")` calls, those are the only ones that
+        // need updating (esm imports to "node:*" are totally valid), so here we tag with the
+        // node-buffer namespace only imports that are require calls
+        return kind === "require-call"
+          ? { path, namespace: "node-built-in-modules" }
+          : undefined;
+      });
+
+      // we convert the imports we tagged with the node-built-in-modules namespace so that instead of `require("node:*")`
+      // they import from `export * from "node:*";`
+      build.onLoad(
+        { filter: /.*/, namespace: "node-built-in-modules" },
+        ({ path }) => {
+          return {
+            contents: `export * from '${path}'`,
+            loader: "js",
+          };
+        },
+      );
+
       // We inject the entry files into the edgeFunctionHandler
       build.onLoad({ filter: /\/edgeFunctionHandler.js/g }, async (args) => {
         let contents = readFileSync(args.path, "utf-8");
diff --git a/packages/open-next/src/wrappers/cloudflare.ts b/packages/open-next/src/wrappers/cloudflare.ts
index 073f0bb90..96d6d77f2 100644
--- a/packages/open-next/src/wrappers/cloudflare.ts
+++ b/packages/open-next/src/wrappers/cloudflare.ts
@@ -11,7 +11,9 @@ const handler: WrapperHandler<
   InternalResult | ({ type: "middleware" } & MiddlewareOutputEvent)
 > =
   async (handler, converter) =>
-  async (event: Request): Promise<Response> => {
+  async (event: Request, env: Record<string, string>): Promise<Response> => {
+    //@ts-expect-error - process is not defined in cloudflare workers
+    globalThis.process = { env };
     const internalEvent = await converter.convertFrom(event);
 
     const response = await handler(internalEvent);

From 09b7b546c5cc9de3548160e9c020176741cb93f9 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 27 Jan 2024 10:28:01 +0100
Subject: [PATCH 049/102] add wasm fils and assets

---
 packages/open-next/src/build.ts               |  9 +---
 .../src/build/edge/createEdgeBundle.ts        | 47 ++++++++++---------
 packages/open-next/src/plugins/edge.ts        | 25 +++++++---
 packages/open-next/src/types/next-types.ts    | 12 +++--
 4 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 49824687a..9b5b1891a 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -656,14 +656,7 @@ async function createMiddleware(config: OpenNextConfig) {
   let outputPath = path.join(outputDir, "server-function");
 
   const commonMiddlewareOptions = {
-    files: entry.files,
-    routes: [
-      {
-        name: entry.name || "/",
-        page: entry.page,
-        regex: entry.matchers.map((m) => m.regexp),
-      },
-    ],
+    middlewareInfo: entry,
     options,
     appBuildOutputPath,
   };
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index 18c624e34..fb7e58a6b 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -1,8 +1,9 @@
+import { mkdirSync } from "node:fs";
 import url from "node:url";
 
 import fs from "fs";
 import path from "path";
-import { MiddlewareManifest } from "types/next-types";
+import { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
 import {
   DefaultOverrideOptions,
   IncludedConverter,
@@ -17,12 +18,7 @@ const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
 interface BuildEdgeBundleOptions {
   appBuildOutputPath: string;
-  files: string[];
-  routes: {
-    name: string;
-    page: string;
-    regex: string[];
-  }[];
+  middlewareInfo: MiddlewareInfo;
   entrypoint: string;
   outfile: string;
   options: BuildOptions;
@@ -33,8 +29,7 @@ interface BuildEdgeBundleOptions {
 
 export async function buildEdgeBundle({
   appBuildOutputPath,
-  files,
-  routes,
+  middlewareInfo,
   entrypoint,
   outfile,
   options,
@@ -65,10 +60,7 @@ export async function buildEdgeBundle({
           },
         }),
         openNextEdgePlugins({
-          entryFiles: files.map((file: string) =>
-            path.join(appBuildOutputPath, ".next", file),
-          ),
-          routes,
+          middlewareInfo,
           nextDir: path.join(appBuildOutputPath, ".next"),
           edgeFunctionHandlerPath: path.join(
             __dirname,
@@ -138,16 +130,29 @@ export async function generateEdgeBundle(
   }
   const fn = functions[0];
 
+  //Copy wasm files
+  const wasmFiles = fn.wasm;
+  mkdirSync(path.join(outputPath, "wasm"), { recursive: true });
+  for (const wasmFile of wasmFiles) {
+    fs.copyFileSync(
+      path.join(appBuildOutputPath, ".next", wasmFile.filePath),
+      path.join(outputPath, `wasm/${wasmFile.name}.wasm`),
+    );
+  }
+
+  // Copy assets
+  const assets = fn.assets;
+  mkdirSync(path.join(outputPath, "assets"), { recursive: true });
+  for (const asset of assets) {
+    fs.copyFileSync(
+      path.join(appBuildOutputPath, ".next", asset.filePath),
+      path.join(outputPath, `assets/${asset.name}`),
+    );
+  }
+
   await buildEdgeBundle({
     appBuildOutputPath,
-    files: fn.files,
-    routes: [
-      {
-        name: fn.name,
-        page: fn.page,
-        regex: fn.matchers.map((m) => m.regexp),
-      },
-    ],
+    middlewareInfo: fn,
     entrypoint: path.join(__dirname, "../../adapters", "edge-adapter.js"),
     outfile: path.join(outputPath, "index.mjs"),
     options,
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index 73bc236ef..ef65a9406 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -1,6 +1,8 @@
 import { readFileSync } from "node:fs";
+import path from "node:path";
 
 import { Plugin } from "esbuild";
+import { MiddlewareInfo } from "types/next-types.js";
 
 import {
   loadAppPathsManifestKeys,
@@ -12,13 +14,11 @@ import {
   loadPrerenderManifest,
   loadRoutesManifest,
 } from "../adapters/config/util.js";
-import { EdgeRoute } from "../core/edgeFunctionHandler.js";
 
 export interface IPluginSettings {
   nextDir: string;
   edgeFunctionHandlerPath?: string;
-  entryFiles: string[];
-  routes: EdgeRoute[];
+  middlewareInfo: MiddlewareInfo;
 }
 
 /**
@@ -31,9 +31,19 @@ export interface IPluginSettings {
 export function openNextEdgePlugins({
   nextDir,
   edgeFunctionHandlerPath,
-  entryFiles,
-  routes,
+  middlewareInfo,
 }: IPluginSettings): Plugin {
+  const entryFiles = middlewareInfo.files.map((file: string) =>
+    path.join(nextDir, file),
+  );
+  const routes = [
+    {
+      name: middlewareInfo.name || "/",
+      page: middlewareInfo.page,
+      regex: middlewareInfo.matchers.map((m) => m.regexp),
+    },
+  ];
+  const wasmFiles = middlewareInfo.wasm ?? [];
   return {
     name: "opennext-edge",
     setup(build) {
@@ -46,7 +56,7 @@ export function openNextEdgePlugins({
         });
       }
 
-      build.onResolve({ filter: /.mjs$/g }, (args) => {
+      build.onResolve({ filter: /\.(mjs|wasm)$/g }, (args) => {
         return {
           external: true,
         };
@@ -93,6 +103,9 @@ globalThis.crypto = crypto;
 
 import {AsyncLocalStorage} from "node:async_hooks";
 globalThis.AsyncLocalStorage = AsyncLocalStorage;
+${wasmFiles
+  .map((file) => `import ${file.name} from './wasm/${file.name}.wasm';`)
+  .join("\n")}
 ${entryFiles?.map((file) => `require("${file}");`).join("\n")}
 ${contents}        
         `;
diff --git a/packages/open-next/src/types/next-types.ts b/packages/open-next/src/types/next-types.ts
index 75100e90a..f1f3f8077 100644
--- a/packages/open-next/src/types/next-types.ts
+++ b/packages/open-next/src/types/next-types.ts
@@ -114,7 +114,7 @@ export interface RoutesManifest {
   headers?: Header[];
 }
 
-interface MiddlewareInfo {
+export interface MiddlewareInfo {
   files: string[];
   paths?: string[];
   name: string;
@@ -123,8 +123,14 @@ interface MiddlewareInfo {
     regexp: string;
     originalSource: string;
   }[];
-  wasm: string[];
-  assets: string[];
+  wasm: {
+    filePath: string;
+    name: string;
+  }[];
+  assets: {
+    filePath: string;
+    name: string;
+  }[];
 }
 
 export interface MiddlewareManifest {

From 5daa00d9f988e4e22e72c9fc765cd0ad59f65e94 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 27 Jan 2024 11:02:50 +0100
Subject: [PATCH 050/102] fix 14.1 cache

---
 packages/open-next/src/build/createServerBundle.ts | 3 +++
 packages/open-next/src/core/util.ts                | 7 ++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 0e0e3d38e..07f1926d7 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -185,6 +185,7 @@ async function generateBundle(
   const overrides = fnOptions.override ?? {};
 
   const isBefore13413 = compareSemver(options.nextVersion, "13.4.13") <= 0;
+  const isAfter141 = compareSemver(options.nextVersion, "14.0.4") >= 0;
 
   const disableRouting = isBefore13413 || options.externalMiddleware;
   const plugins = [
@@ -207,6 +208,8 @@ async function generateBundle(
         ...(disableNextPrebundledReact ? ["requireHooks"] : []),
         ...(disableRouting ? ["trustHostHeader"] : []),
         ...(!isBefore13413 ? ["requestHandlerHost"] : []),
+        ...(!isAfter141 ? ["stableIncrementalCache"] : []),
+        ...(isAfter141 ? ["experimentalIncrementalCacheHandler"] : []),
       ],
     }),
 
diff --git a/packages/open-next/src/core/util.ts b/packages/open-next/src/core/util.ts
index bb2453178..d494d428b 100644
--- a/packages/open-next/src/core/util.ts
+++ b/packages/open-next/src/core/util.ts
@@ -41,6 +41,10 @@ export const requestHandler = new NextServer.default({
     compress: false,
     // By default, Next.js uses local disk to store ISR cache. We will use
     // our own cache handler to store the cache on S3.
+    //#override stableIncrementalCache
+    cacheHandler: `./cache.cjs`,
+    cacheMaxMemorySize: 0, // We need to disable memory cache
+    //#endOverride
     experimental: {
       ...NextConfig.experimental,
       // This uses the request.headers.host as the URL
@@ -48,8 +52,9 @@ export const requestHandler = new NextServer.default({
       //#override trustHostHeader
       trustHostHeader: true,
       //#endOverride
-
+      //#override experimentalIncrementalCacheHandler
       incrementalCacheHandlerPath: `./cache.cjs`,
+      //#endOverride
     },
   },
   customServer: false,

From e96f69d993a2ff49ff76a3f2c4cf9da903e21fbb Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 27 Jan 2024 11:51:07 +0100
Subject: [PATCH 051/102] fix wasm import node

---
 .../open-next/src/build/edge/createEdgeBundle.ts   |  1 +
 packages/open-next/src/plugins/edge.ts             | 14 +++++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index fb7e58a6b..48f9f4f3b 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -67,6 +67,7 @@ export async function buildEdgeBundle({
             "../../core",
             "edgeFunctionHandler.js",
           ),
+          isInCloudfare: overrides?.wrapper === "cloudflare",
         }),
       ],
       treeShaking: true,
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index ef65a9406..97bf4a97e 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -19,6 +19,7 @@ export interface IPluginSettings {
   nextDir: string;
   edgeFunctionHandlerPath?: string;
   middlewareInfo: MiddlewareInfo;
+  isInCloudfare?: boolean;
 }
 
 /**
@@ -32,6 +33,7 @@ export function openNextEdgePlugins({
   nextDir,
   edgeFunctionHandlerPath,
   middlewareInfo,
+  isInCloudfare,
 }: IPluginSettings): Plugin {
   const entryFiles = middlewareInfo.files.map((file: string) =>
     path.join(nextDir, file),
@@ -103,8 +105,18 @@ globalThis.crypto = crypto;
 
 import {AsyncLocalStorage} from "node:async_hooks";
 globalThis.AsyncLocalStorage = AsyncLocalStorage;
+${
+  isInCloudfare
+    ? ""
+    : `import {readFileSync} from "node:fs";
+import path from "node:path";`
+}
 ${wasmFiles
-  .map((file) => `import ${file.name} from './wasm/${file.name}.wasm';`)
+  .map((file) =>
+    isInCloudfare
+      ? `import ${file.name} from './wasm/${file.name}.wasm';`
+      : `const ${file.name} = readFileSync(path.join(__dirname,'/wasm/${file.name}.wasm'));`,
+  )
   .join("\n")}
 ${entryFiles?.map((file) => `require("${file}");`).join("\n")}
 ${contents}        

From 94fde9ff54c7058136fbf836b1a2823df18dc20b Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 27 Jan 2024 13:18:02 +0100
Subject: [PATCH 052/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 1c8b9a702..c91955ff0 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.2",
+  "version": "3.0.0-rc.3",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 0af277a4821515b444e0586595e15ad3694f84ce Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Mon, 5 Feb 2024 21:50:44 +0100
Subject: [PATCH 053/102] merge upstream

---
 docs/pages/common_issues/bundle_size.mdx      | 12 +++-
 docs/pages/v3/_meta.json                      |  1 +
 docs/pages/v3/config.mdx                      |  8 +--
 docs/pages/v3/index.mdx                       | 21 +++++--
 docs/pages/v3/override.mdx                    |  6 +-
 docs/pages/v3/reference-implementation.mdx    | 27 +++++++++
 docs/pages/v3/requirements.mdx                | 59 +++++++++++++++++++
 packages/open-next/CHANGELOG.md               |  8 +++
 packages/open-next/package.json               |  2 +-
 .../src/adapters/plugins/14.1/util.ts         | 31 ++++++++++
 .../open-next/src/core/routing/matcher.ts     | 28 +++++++--
 packages/open-next/src/core/routing/util.ts   |  2 +-
 packages/tests-e2e/CHANGELOG.md               |  7 +++
 packages/tests-e2e/package.json               |  3 +-
 .../tests/appRouter/trailing.test.ts          |  5 ++
 packages/tests-unit/CHANGELOG.md              |  9 +++
 16 files changed, 207 insertions(+), 22 deletions(-)
 create mode 100644 docs/pages/v3/requirements.mdx
 create mode 100644 packages/open-next/src/adapters/plugins/14.1/util.ts
 create mode 100644 packages/tests-e2e/CHANGELOG.md

diff --git a/docs/pages/common_issues/bundle_size.mdx b/docs/pages/common_issues/bundle_size.mdx
index ee2b65372..6132df44c 100644
--- a/docs/pages/common_issues/bundle_size.mdx
+++ b/docs/pages/common_issues/bundle_size.mdx
@@ -33,7 +33,17 @@ eg: `--arch=arm64 --platform=linux --target=18 --libc=glibc`
 
 ##### pdfjs
 
-If you need to use pdfjs, you should install it with `npm i pdfjs-dist--no-optional` because the optional dep: `canvas` takes about 180MB.
+- If you need to use pdfjs, you should install it with `npm i pdfjs-dist--no-optional` because the optional dep: `canvas` takes about 180MB.
+
+- If the above doesn't work (or gives some compilation errors) you can try:
+
+```js
+  experimental: {
+    outputFileTracingExcludes: {
+      "*": ["node_modules/canvas"],
+    },
+  },
+```
 
 ##### Others
 
diff --git a/docs/pages/v3/_meta.json b/docs/pages/v3/_meta.json
index 8e89d285d..caefcec50 100644
--- a/docs/pages/v3/_meta.json
+++ b/docs/pages/v3/_meta.json
@@ -2,5 +2,6 @@
   "index": "What's new",
   "config": "Configuration file",
   "reference-implementation": "Reference Construct",
+  "requirements": "Requirements",
   "override": "Advanced - Create your own override"
 }
\ No newline at end of file
diff --git a/docs/pages/v3/config.mdx b/docs/pages/v3/config.mdx
index 008dabfdb..56ee0ba5a 100644
--- a/docs/pages/v3/config.mdx
+++ b/docs/pages/v3/config.mdx
@@ -6,7 +6,7 @@ This file need to be at the same place as your `next.config.js` file
 For more information about the options here, just look at the source file
 
 ```ts
-import type { BuildOptions } from 'open-next/types/open-next'
+import type { OpenNextConfig } from 'open-next/types/open-next'
 const config = {
   default: { // This is the default server, similar to the server-function in open-next v2
     // You don't have to provide the below, by default it will generate an output 
@@ -62,8 +62,8 @@ const config = {
     external: true 
   }
   buildCommand: "echo 'hello world'",
-} satisfies BuildOptions
+} satisfies OpenNextConfig
 
-module.exports = config
-export type OpenNextConfig = typeof config
+export default config;
+export type Config = typeof config
 ```
\ No newline at end of file
diff --git a/docs/pages/v3/index.mdx b/docs/pages/v3/index.mdx
index 10416ea0e..db0161e60 100644
--- a/docs/pages/v3/index.mdx
+++ b/docs/pages/v3/index.mdx
@@ -2,11 +2,13 @@ import { Callout } from 'nextra/components'
 
 <Callout type="warning" emoji="⚠️">
 
-`open-next@3.0.0-rc.2` is here!!! Please report any issues you find on [discord](https://discord.com/channels/983865673656705025/1164872233223729152) or on the github [PR](https://github.com/sst/open-next/pull/327)
+`open-next@3.0.0-rc.3` is here!!! Please report any issues you find on [discord](https://discord.com/channels/983865673656705025/1164872233223729152) or on the github [PR](https://github.com/sst/open-next/pull/327)
 
-  This is a release candidate, it is not yet ready for production, but we are getting close. We are looking for feedback on this release, so please try it out and let us know what you think. See [getting started](#get-started) to quickly test it.
+  This is a release candidate, it is mostly ready for production (You might still experience some quirks). We are looking for feedback on this release, so please try it out and let us know what you think. See [getting started](#get-started) to quickly test it.
   
   It also requires an updated version of the IAC tools that you use, see the sst PR [here](https://github.com/sst/sst/pull/3567) for more information.
+  
+  You could also use SST Ion which should support it out of the box pretty soon. See [here for more info](https://github.com/sst/ion) or in the [ion discord](https://discord.com/channels/983865673656705025/1177071497974648952).
 </Callout>
 
 ## What's new in V3?
@@ -26,8 +28,13 @@ import { Callout } from 'nextra/components'
   - Custom initialization function
 
 - Allow for splitting, you can now split your next app into multiple servers, which could each have their own configuration
+- Allow to move the middleware/routing part in a separate lambda or cloudflare workers in front of your server functions
 - An experimental bundled `NextServer` could be used which can reduce the size of your lambda by up to 24 MB
-- Support for the `edge` runtime of next (Only app router for now, only 1 route per function)
+- Experimental support for the `edge` runtime of next with some limitations:
+  - Only app router for now
+  - Only 1 route per function 
+  - Works fine in node, only for api route in cloudflare workers
+  - No support for `revalidateTag` or `revalidatePath` for now
 
 ## Get started
 
@@ -46,13 +53,15 @@ You also need to create an `open-next.config.ts` file in your project root, you
 A very simple example of this file could be:
 
 ```ts
-import type { BuildOptions } from 'open-next/types/open-next'
+import type { OpenNextConfig } from 'open-next/types/open-next'
 const config = {
   default: {
 
   }
 }
-module.exports = config
+export default config;
 ```
 
-Then you need to run `npx open-next@3.0.0-rc.2 build` to build your project before running the `sst deploy` or `cdk deploy` command to deploy your project.
\ No newline at end of file
+Then you need to run `npx open-next@3.0.0-rc.3 build` to build your project before running the `sst deploy` or `cdk deploy` command to deploy your project.
+
+In V3 `open-next build` don't accept any arguments, all the args are passed in the `open-next.config.ts` file.
\ No newline at end of file
diff --git a/docs/pages/v3/override.mdx b/docs/pages/v3/override.mdx
index 5d083bdda..2cb1af3a8 100644
--- a/docs/pages/v3/override.mdx
+++ b/docs/pages/v3/override.mdx
@@ -18,9 +18,9 @@ A boilerplate for such a plugin could look like this (This is not real code):
 
 ```ts
 
-import { BuildOptions } from "open-next/types/open-next";
+import { OpenNextConfig } from "open-next/types/open-next";
 
-function withGcp(config: TrimmedDownConfig): BuildOptions {
+function withGcp(config: TrimmedDownConfig): OpenNextConfig {
   return {
     default: {
         override: {
@@ -70,5 +70,5 @@ const config = withGcp({
   },
 });
 
-module.exports = config;
+export default config;
 ```
\ No newline at end of file
diff --git a/docs/pages/v3/reference-implementation.mdx b/docs/pages/v3/reference-implementation.mdx
index e79960b9a..02d213fb5 100644
--- a/docs/pages/v3/reference-implementation.mdx
+++ b/docs/pages/v3/reference-implementation.mdx
@@ -38,6 +38,10 @@ import {
 	Distribution,
 	ICachePolicy,
 	ViewerProtocolPolicy,
+	FunctionEventType,
+	OriginRequestPolicy,
+	Function as CloudfrontFunction,
+	FunctionCode,
 } from "aws-cdk-lib/aws-cloudfront";
 import { HttpOrigin, S3Origin } from "aws-cdk-lib/aws-cloudfront-origins";
 import {
@@ -347,6 +351,22 @@ export class OpenNextCdkReferenceImplementation extends Construct {
 	}
 
 	private createDistribution(origins: Record<string, HttpOrigin | S3Origin>) {
+		const cloudfrontFunction = new CloudfrontFunction(this, 'OpenNextCfFunction', {
+			code: FunctionCode.fromInline(`
+			function handler(event) {
+				var request = event.request;
+				request.headers["x-forwarded-host"] = request.headers.host;
+				return request;
+			}
+			`)
+		})
+		const fnAssociations = [
+			{
+				function: cloudfrontFunction ,
+				eventType: FunctionEventType.VIEWER_REQUEST,
+			},
+		]
+
 		const distribution = new Distribution(this, "OpenNextDistribution", {
 			defaultBehavior: {
 				origin: origins.default,
@@ -354,6 +374,8 @@ export class OpenNextCdkReferenceImplementation extends Construct {
 				allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
 				cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
 				cachePolicy: this.serverCachePolicy,
+				originRequestPolicy: OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
+				functionAssociations: fnAssociations
 			},
 			additionalBehaviors: this.openNextOutput.behaviors
 				.filter((b) => b.pattern !== "*")
@@ -372,6 +394,11 @@ export class OpenNextCdkReferenceImplementation extends Construct {
 									behavior.origin === "s3"
 										? this.staticCachePolicy
 										: this.serverCachePolicy,
+								originRequestPolicy: 
+									behavior.origin === "s3" 
+										? undefined 
+										: OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
+								functionAssociations: fnAssociations
 							},
 						};
 					},
diff --git a/docs/pages/v3/requirements.mdx b/docs/pages/v3/requirements.mdx
new file mode 100644
index 000000000..71279afad
--- /dev/null
+++ b/docs/pages/v3/requirements.mdx
@@ -0,0 +1,59 @@
+There is a couple of requirements necessary for open-next V3 to work.
+It will be divided by functionality. This is still WIP, feel free to open a PR if you think something is missing.
+
+## General
+- For the node runtime, you need at least Node 18.
+- For the edge runtime, you can use both Node 18+ or cloudflare workers with `node_compat` flag enabled (Cloudflare workers support is experimental)
+- Open-next doesn't work well on Windows. We recommend using WSL2 or a Linux VM.
+
+## ISR/SSG
+ISR/SSG has 2 types of cache, the Incremental Cache and the Tag Cache. To actually trigger the ISR revalidation, we use a Queue system. 
+
+The tag cache is only used in app router.
+### Incremental Cache
+By default we use S3 as the incremental cache. You can override this in `open-next.config.ts`. For this to work you need to provide server functions with the following environment variables:
+- CACHE_BUCKET_REGION
+- CACHE_BUCKET_NAME
+- CACHE_BUCKET_KEY_PREFIX
+
+### Tag Cache
+By default we use DynamoDb as the tag cache. For this to work you need to provide server functions with the following environment variables:
+- CACHE_DYNAMO_TABLE
+- CACHE_BUCKET_REGION
+
+### Queue
+By default we use SQS as the queue. fFr this to work you need to provide server functions with the following environment variables:
+- REVALIDATION_QUEUE_REGION
+- REVALIDATION_QUEUE_URL
+
+## External Middleware
+If you decide to use external middleware, you need to provide the following environment variables:
+- OPEN_NEXT_ORIGIN
+
+This env variable should contain a stringified version of this, with every key corresponding to the key used in functions inside `open-next.config.ts`: 
+```ts
+// For cloudflare workers
+// THIS IS TEMPORARY, WE WILL CHANGE THIS TO USE THE SAME FORMAT AS NODE
+{
+  default: "example.com",
+  ssr: "example2.com",
+  ssg: "example3.com"
+}
+// Or for node
+{
+  default: {
+    host: "example.com",
+    protocol: "https",
+    port: 443 // Optional
+    customHeaders: {
+      "x-custom-header": "value"
+    } // Optional, headers that you'd want to pass to the origin
+  },
+  ...
+}
+```
+
+## Image Optimization
+For image optimization to work, you need to provide the following environment variables:
+- BUCKET_NAME
+- BUCKET_KEY_PREFIX
\ No newline at end of file
diff --git a/packages/open-next/CHANGELOG.md b/packages/open-next/CHANGELOG.md
index 10968b8d9..40998f46b 100644
--- a/packages/open-next/CHANGELOG.md
+++ b/packages/open-next/CHANGELOG.md
@@ -1,5 +1,13 @@
 # open-next
 
+## 2.3.5
+
+### Patch Changes
+
+- b9eefca: Fix Cache Support for Next@14.1.0
+- c80f1be: Fix trailing slash redirect to external domain
+- 186e28f: fix(open-next): correctly set cache control for html pages
+
 ## 2.3.4
 
 ### Patch Changes
diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 413c6b78f..08b6eea5a 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "2.3.4",
+  "version": "2.3.5",
   "bin": {
     "open-next": "./dist/index.js"
   },
diff --git a/packages/open-next/src/adapters/plugins/14.1/util.ts b/packages/open-next/src/adapters/plugins/14.1/util.ts
new file mode 100644
index 000000000..684c1aab8
--- /dev/null
+++ b/packages/open-next/src/adapters/plugins/14.1/util.ts
@@ -0,0 +1,31 @@
+import { NextConfig } from "../../config";
+import { debug } from "../../logger.js";
+
+//#override requestHandler
+// @ts-ignore
+export const requestHandler = new NextServer.default({
+  conf: {
+    ...NextConfig,
+    // Next.js compression should be disabled because of a bug in the bundled
+    // `compression` package — https://github.com/vercel/next.js/issues/11669
+    compress: false,
+    // By default, Next.js uses local disk to store ISR cache. We will use
+    // our own cache handler to store the cache on S3.
+    cacheHandler: `${process.env.LAMBDA_TASK_ROOT}/cache.cjs`,
+    cacheMaxMemorySize: 0,
+    experimental: {
+      ...NextConfig.experimental,
+      // This uses the request.headers.host as the URL
+      // https://github.com/vercel/next.js/blob/canary/packages/next/src/server/next-server.ts#L1749-L1754
+      trustHostHeader: true,
+    },
+  },
+  customServer: false,
+  dev: false,
+  dir: __dirname,
+}).getRequestHandler();
+//#endOverride
+
+//#override requireHooks
+debug("No need to override require hooks with next 13.4.20+");
+//#endOverride
diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index ef60611c2..055b08665 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -118,6 +118,14 @@ export function addNextConfigHeaders(
           debug("Error matching header ", h.key, " with value ", h.value);
           requestHeaders[h.key] = h.value;
         }
+        try {
+          const key = convertMatch(_match, compile(h.key), h.key);
+          const value = convertMatch(_match, compile(h.value), h.value);
+          requestHeaders[key] = value;
+        } catch {
+          debug("Error matching header ", h.key, " with value ", h.value);
+          requestHeaders[h.key] = h.value;
+        }
       });
     }
   }
@@ -174,10 +182,12 @@ export function handleRewrites<T extends RewriteDefinition>(
   };
 }
 
-export function handleRedirects(
-  event: InternalEvent,
-  redirects: RedirectDefinition[],
-): InternalResult | undefined {
+function handleTrailingSlashRedirect(event: InternalEvent) {
+  const url = new URL(event.url, "http://localhost");
+  // Someone is trying to redirect to a different origin, let's not do that
+  if (url.host !== "localhost") {
+    return false;
+  }
   if (
     NextConfig.trailingSlash &&
     !event.headers["x-nextjs-data"] &&
@@ -214,7 +224,15 @@ export function handleRedirects(
       body: "",
       isBase64Encoded: false,
     };
-  }
+  } else return false;
+}
+
+export function handleRedirects(
+  event: InternalEvent,
+  redirects: RedirectDefinition[],
+): InternalResult | undefined {
+  const trailingSlashRedirect = handleTrailingSlashRedirect(event);
+  if (trailingSlashRedirect) return trailingSlashRedirect;
   const { internalEvent, __rewrite } = handleRewrites(
     event,
     redirects.filter((r) => !r.internal),
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index 576945662..bcbc369fb 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -267,7 +267,7 @@ export function fixCacheHeaderForHtmlPages(
   headers: OutgoingHttpHeaders,
 ) {
   // WORKAROUND: `NextServer` does not set cache headers for HTML pages — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-cache-headers-for-html-pages
-  if (HtmlPages.includes(rawPath) && headers[CommonHeaders.CACHE_CONTROL]) {
+  if (HtmlPages.includes(rawPath)) {
     headers[CommonHeaders.CACHE_CONTROL] =
       "public, max-age=0, s-maxage=31536000, must-revalidate";
   }
diff --git a/packages/tests-e2e/CHANGELOG.md b/packages/tests-e2e/CHANGELOG.md
new file mode 100644
index 000000000..1da2dacea
--- /dev/null
+++ b/packages/tests-e2e/CHANGELOG.md
@@ -0,0 +1,7 @@
+# tests-e2e
+
+## null
+
+### Patch Changes
+
+- c80f1be: Fix trailing slash redirect to external domain
diff --git a/packages/tests-e2e/package.json b/packages/tests-e2e/package.json
index dc01a67ee..c2c20f000 100644
--- a/packages/tests-e2e/package.json
+++ b/packages/tests-e2e/package.json
@@ -12,5 +12,6 @@
     "@open-next/utils": "workspace:*",
     "start-server-and-test": "2.0.0",
     "ts-node": "10.9.1"
-  }
+  },
+  "version": null
 }
diff --git a/packages/tests-e2e/tests/appRouter/trailing.test.ts b/packages/tests-e2e/tests/appRouter/trailing.test.ts
index aeffc92ed..a64a22610 100644
--- a/packages/tests-e2e/tests/appRouter/trailing.test.ts
+++ b/packages/tests-e2e/tests/appRouter/trailing.test.ts
@@ -15,3 +15,8 @@ test("trailingSlash redirect with search parameters", async ({ page }) => {
   );
   expect(response?.request().url()).toMatch(/\/ssr\?happy=true$/);
 });
+
+test("trailingSlash redirect to external domain", async ({ page, baseURL }) => {
+  const response = await page.goto(`${baseURL}//sst.dev/`);
+  expect(response?.status()).toBe(404);
+});
diff --git a/packages/tests-unit/CHANGELOG.md b/packages/tests-unit/CHANGELOG.md
index 336a52111..090bd5dd5 100644
--- a/packages/tests-unit/CHANGELOG.md
+++ b/packages/tests-unit/CHANGELOG.md
@@ -4,6 +4,15 @@
 
 ### Patch Changes
 
+- Updated dependencies [b9eefca]
+- Updated dependencies [c80f1be]
+- Updated dependencies [186e28f]
+  - open-next@2.3.5
+
+## null
+
+### Patch Changes
+
 - Updated dependencies [e773e67]
 - Updated dependencies [83b0838]
 - Updated dependencies [bbf9b30]

From 51ba372023f97462db65bb7a54947441aba5b9ca Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 6 Feb 2024 09:58:37 +0100
Subject: [PATCH 054/102] make open-next.config.ts optional

---
 packages/open-next/src/build.ts | 47 ++++++++++++++++++++++++++-------
 packages/open-next/src/index.ts |  6 +++--
 2 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 9b5b1891a..4d8b57fc4 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -34,7 +34,7 @@ export type PublicFiles = {
   files: string[];
 };
 
-export async function build() {
+export async function build(openNextConfigPath?: string) {
   const outputTmpPath = path.join(process.cwd(), ".open-next", ".build");
 
   if (os.platform() === "win32") {
@@ -46,7 +46,7 @@ export async function build() {
   }
 
   // Compile open-next.config.ts
-  createOpenNextConfigBundle(outputTmpPath);
+  createOpenNextConfigBundle(outputTmpPath, openNextConfigPath);
 
   const config = await import(outputTmpPath + "/open-next.config.mjs");
   const opts = config.default as OpenNextConfig;
@@ -91,14 +91,41 @@ export async function build() {
   await generateOutput(options.appBuildOutputPath, opts);
 }
 
-function createOpenNextConfigBundle(tempDir: string) {
-  buildSync({
-    entryPoints: [path.join(process.cwd(), "open-next.config.ts")],
-    outfile: path.join(tempDir, "open-next.config.mjs"),
-    bundle: true,
-    format: "esm",
-    target: ["node18"],
-  });
+function createOpenNextConfigBundle(
+  tempDir: string,
+  openNextConfigPath?: string,
+) {
+  //Check if open-next.config.ts exists
+  const pathToOpenNextConfig = path.join(
+    process.cwd(),
+    openNextConfigPath ?? "open-next.config.ts",
+  );
+  if (!fs.existsSync(pathToOpenNextConfig)) {
+    //Create a simple open-next.config.mjs file
+    logger.warn(
+      "You don't have an open-next.config.ts file. Using default configuration.",
+    );
+    fs.writeFileSync(
+      path.join(tempDir, "open-next.config.mjs"),
+      `var config = {
+        default: {
+        },
+      };
+      var open_next_config_default = config;
+      export {
+        open_next_config_default as default
+      };
+      `,
+    );
+  } else {
+    buildSync({
+      entryPoints: [pathToOpenNextConfig],
+      outfile: path.join(tempDir, "open-next.config.mjs"),
+      bundle: true,
+      format: "esm",
+      target: ["node18"],
+    });
+  }
 }
 
 function checkRunningInsideNextjsApp() {
diff --git a/packages/open-next/src/index.ts b/packages/open-next/src/index.ts
index d94eab711..ec28ccf67 100755
--- a/packages/open-next/src/index.ts
+++ b/packages/open-next/src/index.ts
@@ -8,7 +8,7 @@ if (command !== "build") printHelp();
 const args = parseArgs();
 if (Object.keys(args).includes("--help")) printHelp();
 
-build();
+build(args["--config-path"]);
 
 function parseArgs() {
   return process.argv.slice(2).reduce(
@@ -33,7 +33,9 @@ function printHelp() {
   console.log("");
   console.log("Usage:");
   console.log("  npx open-next build");
-  console.log("  npx open-next build --build-command 'npm run custom:build'");
+  console.log(
+    "  npx open-next build --config-path ./path/to/open-next.config.ts",
+  );
   console.log("");
 
   process.exit(1);

From aa97c43c04effccec67307ea2558d4649cb3c711 Mon Sep 17 00:00:00 2001
From: Frank <wangfanjie@gmail.com>
Date: Fri, 16 Feb 2024 16:47:19 -0500
Subject: [PATCH 055/102] Fix cannot write default config file b/c folder not
 created (#364)

* Fix cannot write default config file b/c folder not created

* Removed copyTracedFiles debug log
---
 packages/open-next/src/build.ts               | 121 +++++++++---------
 .../open-next/src/build/copyTracedFiles.ts    |   5 +-
 .../open-next/src/build/createServerBundle.ts |  23 ++--
 .../open-next/src/build/generateOutput.ts     |  40 +++---
 packages/open-next/src/build/helper.ts        |  16 +--
 5 files changed, 105 insertions(+), 100 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 4d8b57fc4..9c927801a 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -29,35 +29,27 @@ import { OpenNextConfig } from "./types/open-next.js";
 
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 let options: BuildOptions;
+let config: OpenNextConfig;
 
 export type PublicFiles = {
   files: string[];
 };
 
 export async function build(openNextConfigPath?: string) {
-  const outputTmpPath = path.join(process.cwd(), ".open-next", ".build");
+  showWindowsWarning();
 
-  if (os.platform() === "win32") {
-    logger.error(
-      "OpenNext is not properly supported on Windows. On windows you should use WSL. It might works or it might fail in unpredictable way at runtime",
-    );
-    // Wait 10s here so that the user see this message
-    await new Promise((resolve) => setTimeout(resolve, 10000));
-  }
-
-  // Compile open-next.config.ts
-  createOpenNextConfigBundle(outputTmpPath, openNextConfigPath);
-
-  const config = await import(outputTmpPath + "/open-next.config.mjs");
-  const opts = config.default as OpenNextConfig;
-  validateConfig(opts);
+  // Load open-next.config.ts
+  const tempDir = initTempDir();
+  const configPath = compileOpenNextConfig(tempDir, openNextConfigPath);
+  config = (await import(configPath)).default as OpenNextConfig;
+  validateConfig(config);
 
   const { root: monorepoRoot, packager } = findMonorepoRoot(
-    path.join(process.cwd(), opts.appPath || "."),
+    path.join(process.cwd(), config.appPath || "."),
   );
 
   // Initialize options
-  options = normalizeOptions(opts, monorepoRoot);
+  options = normalizeOptions(config, monorepoRoot);
   logger.setLevel(options.debug ? "debug" : "info");
 
   // Pre-build validation
@@ -75,57 +67,70 @@ export async function build(openNextConfigPath?: string) {
   initOutputDir();
 
   // Compile cache.ts
-  compileCache(options);
+  compileCache();
 
   // Compile middleware
-  await createMiddleware(opts);
+  await createMiddleware();
 
   createStaticAssets();
-  if (!options.dangerous?.disableIncrementalCache) {
-    await createCacheAssets(monorepoRoot, options.dangerous?.disableTagCache);
-  }
-  await createServerBundle(opts, options);
+  await createCacheAssets(monorepoRoot);
+  await createServerBundle(config, options);
   await createRevalidationBundle();
   createImageOptimizationBundle();
   await createWarmerBundle();
-  await generateOutput(options.appBuildOutputPath, opts);
+  await generateOutput(options.appBuildOutputPath, config);
 }
 
-function createOpenNextConfigBundle(
-  tempDir: string,
-  openNextConfigPath?: string,
-) {
-  //Check if open-next.config.ts exists
-  const pathToOpenNextConfig = path.join(
+function showWindowsWarning() {
+  if (os.platform() !== "win32") return;
+
+  logger.warn("OpenNext is not fully compatible with Windows.");
+  logger.warn(
+    "For optimal performance, it is recommended to use Windows Subsystem for Linux (WSL).",
+  );
+  logger.warn(
+    "While OpenNext may function on Windows, it could encounter unpredictable failures during runtime.",
+  );
+}
+
+function initTempDir() {
+  const dir = path.join(process.cwd(), ".open-next");
+  const tempDir = path.join(dir, ".build");
+  fs.rmSync(dir, { recursive: true, force: true });
+  fs.mkdirSync(tempDir, { recursive: true });
+  return tempDir;
+}
+
+function compileOpenNextConfig(tempDir: string, openNextConfigPath?: string) {
+  const sourcePath = path.join(
     process.cwd(),
     openNextConfigPath ?? "open-next.config.ts",
   );
-  if (!fs.existsSync(pathToOpenNextConfig)) {
+  const outputPath = path.join(tempDir, "open-next.config.mjs");
+
+  //Check if open-next.config.ts exists
+  if (!fs.existsSync(sourcePath)) {
     //Create a simple open-next.config.mjs file
-    logger.warn(
-      "You don't have an open-next.config.ts file. Using default configuration.",
-    );
+    logger.debug("Cannot find open-next.config.ts. Using default config.");
     fs.writeFileSync(
-      path.join(tempDir, "open-next.config.mjs"),
-      `var config = {
-        default: {
-        },
-      };
-      var open_next_config_default = config;
-      export {
-        open_next_config_default as default
-      };
-      `,
+      outputPath,
+      [
+        "var config = { default: { } };",
+        "var open_next_config_default = config;",
+        "export { open_next_config_default as default };",
+      ].join("\n"),
     );
   } else {
     buildSync({
-      entryPoints: [pathToOpenNextConfig],
-      outfile: path.join(tempDir, "open-next.config.mjs"),
+      entryPoints: [sourcePath],
+      outfile: outputPath,
       bundle: true,
       format: "esm",
       target: ["node18"],
     });
   }
+
+  return outputPath;
 }
 
 function checkRunningInsideNextjsApp() {
@@ -176,7 +181,7 @@ function setStandaloneBuildMode(monorepoRoot: string) {
 function buildNextjsApp(packager: "npm" | "yarn" | "pnpm" | "bun") {
   const { nextPackageJsonPath } = options;
   const command =
-    options.buildCommand ??
+    config.buildCommand ??
     (["bun", "npm"].includes(packager)
       ? `${packager} run build`
       : `${packager} build`);
@@ -430,10 +435,9 @@ function createStaticAssets() {
   }
 }
 
-async function createCacheAssets(
-  monorepoRoot: string,
-  disableDynamoDBCache = false,
-) {
+async function createCacheAssets(monorepoRoot: string) {
+  if (config.dangerous?.disableIncrementalCache) return;
+
   logger.info(`Bundling cache assets...`);
 
   const { appBuildOutputPath, outputDir } = options;
@@ -527,7 +531,7 @@ async function createCacheAssets(
     fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
   });
 
-  if (!disableDynamoDBCache) {
+  if (!config.dangerous?.disableTagCache) {
     // Generate dynamodb data
     // We need to traverse the cache to find every .meta file
     const metaFiles: {
@@ -635,9 +639,8 @@ async function createCacheAssets(
 /* Server Helper Functions */
 /***************************/
 
-function compileCache(options: BuildOptions) {
+function compileCache() {
   const outfile = path.join(options.outputDir, ".build", "cache.cjs");
-  const dangerousOptions = options.dangerous;
   esbuildSync(
     {
       external: ["next", "styled-jsx", "react", "@aws-sdk/*"],
@@ -648,10 +651,10 @@ function compileCache(options: BuildOptions) {
       banner: {
         js: [
           `globalThis.disableIncrementalCache = ${
-            dangerousOptions?.disableIncrementalCache ?? false
+            config.dangerous?.disableIncrementalCache ?? false
           };`,
           `globalThis.disableDynamoDBCache = ${
-            dangerousOptions?.disableTagCache ?? false
+            config.dangerous?.disableTagCache ?? false
           };`,
         ].join(""),
       },
@@ -661,10 +664,10 @@ function compileCache(options: BuildOptions) {
   return outfile;
 }
 
-async function createMiddleware(config: OpenNextConfig) {
+async function createMiddleware() {
   console.info(`Bundling middleware function...`);
 
-  const { appBuildOutputPath, outputDir, externalMiddleware } = options;
+  const { appBuildOutputPath, outputDir } = options;
 
   // Get middleware manifest
   const middlewareManifest = JSON.parse(
@@ -688,7 +691,7 @@ async function createMiddleware(config: OpenNextConfig) {
     appBuildOutputPath,
   };
 
-  if (externalMiddleware) {
+  if (config.middleware?.external) {
     outputPath = path.join(outputDir, "middleware");
     fs.mkdirSync(outputPath, { recursive: true });
 
diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index fe74196b9..9cb8cc238 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -11,6 +11,7 @@ import {
 } from "fs";
 import path from "path";
 import { NextConfig, PrerenderManifest } from "types/next-types";
+import logger from "../logger";
 
 export async function copyTracedFiles(
   buildOutputPath: string,
@@ -19,7 +20,7 @@ export async function copyTracedFiles(
   routes: string[],
   bundledNextServer: boolean,
 ) {
-  console.time("copyTracedFiles");
+  const tsStart = Date.now();
   const dotNextDir = path.join(buildOutputPath, ".next");
   const standaloneDir = path.join(dotNextDir, "standalone");
   const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
@@ -232,5 +233,5 @@ export async function copyTracedFiles(
     });
   }
 
-  console.timeEnd("copyTracedFiles");
+  logger.debug("copyTracedFiles:", Date.now() - tsStart, "ms");
 }
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 07f1926d7..5f84d935c 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -29,21 +29,21 @@ const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
 export async function createServerBundle(
-  options: OpenNextConfig,
-  buildRuntimeOptions: BuildOptions,
+  config: OpenNextConfig,
+  options: BuildOptions,
 ) {
   const foundRoutes = new Set<string>();
   // Get all functions to build
-  const defaultFn = options.default;
-  const functions = Object.entries(options.functions ?? {});
+  const defaultFn = config.default;
+  const functions = Object.entries(config.functions ?? {});
 
   const promises = functions.map(async ([name, fnOptions]) => {
     const routes = fnOptions.routes;
     routes.forEach((route) => foundRoutes.add(route));
     if (fnOptions.runtime === "edge") {
-      await generateEdgeBundle(name, buildRuntimeOptions, fnOptions);
+      await generateEdgeBundle(name, options, fnOptions);
     } else {
-      await generateBundle(name, buildRuntimeOptions, fnOptions);
+      await generateBundle(name, config, options, fnOptions);
     }
   });
 
@@ -54,13 +54,13 @@ export async function createServerBundle(
 
   const remainingRoutes = new Set<string>();
 
-  const { monorepoRoot, appBuildOutputPath } = buildRuntimeOptions;
+  const { monorepoRoot, appBuildOutputPath } = options;
 
   const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
 
   // Find remaining routes
   const serverPath = path.join(
-    buildRuntimeOptions.appBuildOutputPath,
+    appBuildOutputPath,
     ".next",
     "standalone",
     packagePath,
@@ -105,7 +105,7 @@ export async function createServerBundle(
   }
 
   // Generate default function
-  await generateBundle("default", buildRuntimeOptions, {
+  await generateBundle("default", config, options, {
     ...defaultFn,
     routes: Array.from(remainingRoutes),
     patterns: ["*"],
@@ -114,6 +114,7 @@ export async function createServerBundle(
 
 async function generateBundle(
   name: string,
+  config: OpenNextConfig,
   options: BuildOptions,
   fnOptions: SplittedFunctionOptions,
 ) {
@@ -149,7 +150,7 @@ async function generateBundle(
 
   // // Copy middleware
   if (
-    !options.externalMiddleware &&
+    !config.middleware?.external &&
     existsSync(path.join(outputDir, ".build", "middleware.mjs"))
   ) {
     fs.copyFileSync(
@@ -187,7 +188,7 @@ async function generateBundle(
   const isBefore13413 = compareSemver(options.nextVersion, "13.4.13") <= 0;
   const isAfter141 = compareSemver(options.nextVersion, "14.0.4") >= 0;
 
-  const disableRouting = isBefore13413 || options.externalMiddleware;
+  const disableRouting = isBefore13413 || config.middleware?.external;
   const plugins = [
     openNextReplacementPlugin({
       name: `requestHandlerOverride ${name}`,
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index a1e25cf73..2e7ea5a03 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -153,23 +153,23 @@ async function extractCommonOverride(override?: OverrideOptions) {
 
 export async function generateOutput(
   outputPath: string,
-  buildOptions: OpenNextConfig,
+  config: OpenNextConfig,
 ) {
   const edgeFunctions: OpenNextOutput["edgeFunctions"] = {};
-  const isExternalMiddleware = buildOptions.middleware?.external ?? false;
+  const isExternalMiddleware = config.middleware?.external ?? false;
   if (isExternalMiddleware) {
     edgeFunctions.middleware = {
       bundle: ".open-next/middleware",
       handler: "handler.handler",
       pathResolver: await extractOverrideName(
         "pattern-env",
-        buildOptions.middleware!.originResolver,
+        config.middleware!.originResolver,
       ),
-      ...(await extractOverrideFn(buildOptions.middleware?.override)),
+      ...(await extractOverrideFn(config.middleware?.override)),
     };
   }
   // Add edge functions
-  Object.entries(buildOptions.functions ?? {}).forEach(async ([key, value]) => {
+  Object.entries(config.functions ?? {}).forEach(async ([key, value]) => {
     if (value.placement === "global") {
       edgeFunctions[key] = {
         bundle: `.open-next/functions/${key}`,
@@ -179,7 +179,7 @@ export async function generateOutput(
     }
   });
 
-  const defaultOriginCanstream = await canStream(buildOptions.default);
+  const defaultOriginCanstream = await canStream(config.default);
 
   // First add s3 origins and image optimization
 
@@ -194,7 +194,7 @@ export async function generateOutput(
           cached: true,
           versionedSubDir: "_next",
         },
-        ...(buildOptions.dangerous?.disableIncrementalCache
+        ...(config.dangerous?.disableIncrementalCache
           ? []
           : [
               {
@@ -212,25 +212,25 @@ export async function generateOutput(
       streaming: false,
       imageLoader: await extractOverrideName(
         "s3",
-        buildOptions.imageOptimization?.loader,
+        config.imageOptimization?.loader,
       ),
-      ...(await extractOverrideFn(buildOptions.imageOptimization?.override)),
+      ...(await extractOverrideFn(config.imageOptimization?.override)),
     },
-    default: buildOptions.default.override?.generateDockerfile
+    default: config.default.override?.generateDockerfile
       ? {
           type: "ecs",
           bundle: ".open-next/server-functions/default",
           dockerfile: ".open-next/server-functions/default/Dockerfile",
-          ...(await extractOverrideFn(buildOptions.default.override)),
-          ...(await extractCommonOverride(buildOptions.default.override)),
+          ...(await extractOverrideFn(config.default.override)),
+          ...(await extractCommonOverride(config.default.override)),
         }
       : {
           type: "function",
           handler: "index.handler",
           bundle: ".open-next/server-functions/default",
           streaming: defaultOriginCanstream,
-          ...(await extractOverrideFn(buildOptions.default.override)),
-          ...(await extractCommonOverride(buildOptions.default.override)),
+          ...(await extractOverrideFn(config.default.override)),
+          ...(await extractCommonOverride(config.default.override)),
         },
   };
 
@@ -239,7 +239,7 @@ export async function generateOutput(
 
   // Then add function origins
   await Promise.all(
-    Object.entries(buildOptions.functions ?? {}).map(async ([key, value]) => {
+    Object.entries(config.functions ?? {}).map(async ([key, value]) => {
       if (!value.placement || value.placement === "regional") {
         if (value.override?.generateDockerfile) {
           origins[key] = {
@@ -270,7 +270,7 @@ export async function generateOutput(
   ];
 
   // Then we add the routes
-  Object.entries(buildOptions.functions ?? {}).forEach(([key, value]) => {
+  Object.entries(config.functions ?? {}).forEach(([key, value]) => {
     const patterns = "patterns" in value ? value.patterns : ["*"];
     patterns.forEach((pattern) => {
       behaviors.push({
@@ -319,19 +319,19 @@ export async function generateOutput(
     origins,
     behaviors,
     additionalProps: {
-      disableIncrementalCache: buildOptions.dangerous?.disableIncrementalCache,
-      disableTagCache: buildOptions.dangerous?.disableTagCache,
+      disableIncrementalCache: config.dangerous?.disableIncrementalCache,
+      disableTagCache: config.dangerous?.disableTagCache,
       warmer: {
         handler: "index.handler",
         bundle: ".open-next/warmer-function",
       },
-      initializationFunction: buildOptions.dangerous?.disableTagCache
+      initializationFunction: config.dangerous?.disableTagCache
         ? undefined
         : {
             handler: "index.handler",
             bundle: ".open-next/initialization-function",
           },
-      revalidationFunction: buildOptions.dangerous?.disableIncrementalCache
+      revalidationFunction: config.dangerous?.disableIncrementalCache
         ? undefined
         : {
             handler: "index.handler",
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
index 2ece8544e..43d1584ea 100644
--- a/packages/open-next/src/build/helper.ts
+++ b/packages/open-next/src/build/helper.ts
@@ -17,14 +17,17 @@ const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 
 export type BuildOptions = ReturnType<typeof normalizeOptions>;
 
-export function normalizeOptions(opts: OpenNextConfig, root: string) {
-  const appPath = path.join(process.cwd(), opts.appPath || ".");
-  const buildOutputPath = path.join(process.cwd(), opts.buildOutputPath || ".");
+export function normalizeOptions(config: OpenNextConfig, root: string) {
+  const appPath = path.join(process.cwd(), config.appPath || ".");
+  const buildOutputPath = path.join(
+    process.cwd(),
+    config.buildOutputPath || ".",
+  );
   const outputDir = path.join(buildOutputPath, ".open-next");
 
   let nextPackageJsonPath: string;
-  if (opts.packageJsonPath) {
-    const _pkgPath = path.join(process.cwd(), opts.packageJsonPath);
+  if (config.packageJsonPath) {
+    const _pkgPath = path.join(process.cwd(), config.packageJsonPath);
     nextPackageJsonPath = _pkgPath.endsWith("package.json")
       ? _pkgPath
       : path.join(_pkgPath, "./package.json");
@@ -41,9 +44,6 @@ export function normalizeOptions(opts: OpenNextConfig, root: string) {
     outputDir,
     tempDir: path.join(outputDir, ".build"),
     debug: Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
-    buildCommand: opts.buildCommand,
-    dangerous: opts.dangerous,
-    externalMiddleware: opts.middleware?.external ?? false,
     monorepoRoot: root,
   };
 }

From 12bf5d29fa81889f22b903383d693cb5c852dcf4 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 16 Feb 2024 23:06:44 +0100
Subject: [PATCH 056/102] fix for monorepo

---
 packages/open-next/src/build.ts               |  1 +
 .../open-next/src/build/copyTracedFiles.ts    | 34 +++++++++----------
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 9c927801a..b1642a863 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -79,6 +79,7 @@ export async function build(openNextConfigPath?: string) {
   createImageOptimizationBundle();
   await createWarmerBundle();
   await generateOutput(options.appBuildOutputPath, config);
+  logger.info("OpenNext build complete.");
 }
 
 function showWindowsWarning() {
diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 9cb8cc238..1440a4f32 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -11,6 +11,7 @@ import {
 } from "fs";
 import path from "path";
 import { NextConfig, PrerenderManifest } from "types/next-types";
+
 import logger from "../logger";
 
 export async function copyTracedFiles(
@@ -24,6 +25,7 @@ export async function copyTracedFiles(
   const dotNextDir = path.join(buildOutputPath, ".next");
   const standaloneDir = path.join(dotNextDir, "standalone");
   const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
+  const outputNextDir = path.join(outputDir, packagePath, ".next");
 
   const extractFiles = (files: string[], from = standaloneNextDir) => {
     return files.map((f) => path.resolve(from, f));
@@ -52,13 +54,19 @@ export async function copyTracedFiles(
 
   // create directory for pages
   if (existsSync(path.join(standaloneDir, ".next/server/pages"))) {
-    mkdirSync(path.join(outputDir, ".next/server/pages"), { recursive: true });
+    mkdirSync(path.join(outputNextDir, "server/pages"), {
+      recursive: true,
+    });
   }
   if (existsSync(path.join(standaloneDir, ".next/server/app"))) {
-    mkdirSync(path.join(outputDir, ".next/server/app"), { recursive: true });
+    mkdirSync(path.join(outputNextDir, "server/app"), {
+      recursive: true,
+    });
   }
 
-  mkdirSync(path.join(outputDir, ".next/server/chunks"), { recursive: true });
+  mkdirSync(path.join(outputNextDir, "server/chunks"), {
+    recursive: true,
+  });
 
   const computeCopyFilesForPage = (pagePath: string) => {
     const fullFilePath = `server/${pagePath}.js`;
@@ -139,19 +147,14 @@ export async function copyTracedFiles(
     }
   });
 
-  mkdirSync(path.join(outputDir, ".next"), { recursive: true });
-
   readdirSync(standaloneNextDir).forEach((f) => {
     if (statSync(path.join(standaloneNextDir, f)).isDirectory()) return;
-    copyFileSync(
-      path.join(standaloneNextDir, f),
-      path.join(path.join(outputDir, ".next"), f),
-    );
+    copyFileSync(path.join(standaloneNextDir, f), path.join(outputNextDir, f));
   });
 
   // We then need to copy all the files at the root of server
 
-  mkdirSync(path.join(outputDir, ".next/server"), { recursive: true });
+  mkdirSync(path.join(outputNextDir, "server"), { recursive: true });
 
   readdirSync(path.join(standaloneNextDir, "server")).forEach((f) => {
     if (statSync(path.join(standaloneNextDir, "server", f)).isDirectory())
@@ -159,7 +162,7 @@ export async function copyTracedFiles(
     if (f !== "server.js") {
       copyFileSync(
         path.join(standaloneNextDir, "server", f),
-        path.join(path.join(outputDir, ".next/server"), f),
+        path.join(path.join(outputNextDir, "server"), f),
       );
     }
   });
@@ -169,21 +172,18 @@ export async function copyTracedFiles(
   // we replace the pages-manifest.json with an empty one if we don't have a pages dir so that
   // next doesn't try to load _app, _document
   if (!hasPageDir) {
-    writeFileSync(
-      path.join(outputDir, ".next/server/pages-manifest.json"),
-      "{}",
-    );
+    writeFileSync(path.join(outputNextDir, "server/pages-manifest.json"), "{}");
   }
 
   //TODO: Find what else we need to copy
   const copyStaticFile = (filePath: string) => {
     if (existsSync(path.join(standaloneNextDir, filePath))) {
-      mkdirSync(path.dirname(path.join(outputDir, ".next", filePath)), {
+      mkdirSync(path.dirname(path.join(outputNextDir, filePath)), {
         recursive: true,
       });
       copyFileSync(
         path.join(standaloneNextDir, filePath),
-        path.join(outputDir, ".next", filePath),
+        path.join(outputNextDir, filePath),
       );
     }
   };

From 1a420ebfac1cf1c7bea4adbbf806a9e39320a67a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 16 Feb 2024 23:12:05 +0100
Subject: [PATCH 057/102] fix for output for dynamodb provider

---
 packages/open-next/src/build/generateOutput.ts | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index 2e7ea5a03..345be7864 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -314,6 +314,11 @@ export async function generateOutput(
     }
   });
 
+  // Check if we produced a dynamodb provider output
+  const isTagCacheDisabled =
+    config.dangerous?.disableTagCache ||
+    !fs.existsSync(path.join(outputPath, ".open-next", "dynamodb-provider"));
+
   const output: OpenNextOutput = {
     edgeFunctions,
     origins,
@@ -325,11 +330,11 @@ export async function generateOutput(
         handler: "index.handler",
         bundle: ".open-next/warmer-function",
       },
-      initializationFunction: config.dangerous?.disableTagCache
+      initializationFunction: isTagCacheDisabled
         ? undefined
         : {
             handler: "index.handler",
-            bundle: ".open-next/initialization-function",
+            bundle: ".open-next/dynamodb-provider",
           },
       revalidationFunction: config.dangerous?.disableIncrementalCache
         ? undefined

From b2879f6ed972e02b4f4ffbde973c259824c1b25c Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 16 Feb 2024 23:22:05 +0100
Subject: [PATCH 058/102] fix dynamoProvider, skipTrailingSlash, weird ISR
 deduplication issue

---
 .../open-next/src/adapters/dynamo-provider.ts |  2 +-
 packages/open-next/src/build.ts               |  2 +-
 .../src/converters/custom-resource.ts         | 29 +++++++++++++++++++
 .../open-next/src/core/routing/matcher.ts     | 10 +++++--
 packages/open-next/src/core/routing/util.ts   | 13 +++------
 packages/open-next/src/types/open-next.ts     |  1 +
 6 files changed, 44 insertions(+), 13 deletions(-)
 create mode 100644 packages/open-next/src/converters/custom-resource.ts

diff --git a/packages/open-next/src/adapters/dynamo-provider.ts b/packages/open-next/src/adapters/dynamo-provider.ts
index 5d15e6c0c..b79226ff3 100644
--- a/packages/open-next/src/adapters/dynamo-provider.ts
+++ b/packages/open-next/src/adapters/dynamo-provider.ts
@@ -23,7 +23,7 @@ type DataType = {
   };
 };
 
-interface InitializationFunctionEvent {
+export interface InitializationFunctionEvent {
   type: "initializationFunction";
   requestType: "create" | "update" | "delete";
   resourceId: typeof PHYSICAL_RESOURCE_ID;
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index b1642a863..10465b68d 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -613,7 +613,7 @@ async function createCacheAssets(monorepoRoot: string) {
           plugins: [
             openNextResolvePlugin({
               overrides: {
-                converter: "dummy",
+                converter: "custom-resource",
               },
             }),
           ],
diff --git a/packages/open-next/src/converters/custom-resource.ts b/packages/open-next/src/converters/custom-resource.ts
new file mode 100644
index 000000000..46377d489
--- /dev/null
+++ b/packages/open-next/src/converters/custom-resource.ts
@@ -0,0 +1,29 @@
+import { CdkCustomResourceEvent } from "aws-lambda";
+import { Converter } from "types/open-next";
+
+import type { InitializationFunctionEvent } from "../adapters/dynamo-provider";
+
+const converter: Converter<
+  InitializationFunctionEvent,
+  InitializationFunctionEvent
+> = {
+  convertFrom(event: CdkCustomResourceEvent) {
+    return Promise.resolve({
+      type: "initializationFunction",
+      requestType: event.RequestType.toLowerCase() as
+        | "create"
+        | "update"
+        | "delete",
+      resourceId: "dynamodb-cache",
+    });
+  },
+  convertTo(internalResult) {
+    return Promise.resolve({
+      type: "dummy",
+      original: internalResult,
+    });
+  },
+  name: "customResource",
+};
+
+export default converter;
diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index 055b08665..dc061d803 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -184,8 +184,14 @@ export function handleRewrites<T extends RewriteDefinition>(
 
 function handleTrailingSlashRedirect(event: InternalEvent) {
   const url = new URL(event.url, "http://localhost");
-  // Someone is trying to redirect to a different origin, let's not do that
-  if (url.host !== "localhost") {
+
+  if (
+    // Someone is trying to redirect to a different origin, let's not do that
+    url.host !== "localhost" ||
+    NextConfig.skipTrailingSlashRedirect ||
+    // We should not apply trailing slash redirect to API routes
+    event.rawPath.startsWith("/api/")
+  ) {
     return false;
   }
   if (
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index bcbc369fb..83ceae17e 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -346,18 +346,13 @@ export async function revalidateIfRequired(
           ? globalThis.lastModified[requestId]
           : "";
 
-      // await sqsClient.send(
-      //   new SendMessageCommand({
-      //     QueueUrl: REVALIDATION_QUEUE_URL,
-      //     MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
-      //     MessageBody: JSON.stringify({ host, url: revalidateUrl }),
-      //     MessageGroupId: generateMessageGroupId(rawPath),
-      //   }),
-      // );
+      // For some weird cases, lastModified is not set, haven't been able to figure out yet why
+      // For those cases we add the etag to the deduplication id, it might help
+      const etag = headers["etag"] ?? headers["ETag"] ?? "";
 
       await globalThis.queue.send({
         MessageBody: { host, url: revalidateUrl },
-        MessageDeduplicationId: hash(`${rawPath}-${lastModified}`),
+        MessageDeduplicationId: hash(`${rawPath}-${lastModified}-${etag}`),
         MessageGroupId: generateMessageGroupId(rawPath),
       });
     } catch (e) {
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index ea37285d1..5fe81a464 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -112,6 +112,7 @@ export type IncludedConverter =
   | "edge"
   | "node"
   | "sqs-revalidate"
+  | "custom-resource"
   | "dummy";
 
 export type IncludedQueue = "sqs";

From 4a55c2b3d804e52faf0a4fb8c6d7c7231d34c9a9 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 16 Feb 2024 23:32:58 +0100
Subject: [PATCH 059/102] little improvement to streaming in lambda

---
 packages/open-next/src/wrappers/aws-lambda-streaming.ts | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 3e2c76a0f..3f48a671c 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -94,7 +94,11 @@ const handler: WrapperHandler = async (handler, converter) =>
 
           if (responseStream.writableCorked) {
             for (let i = 0; i < responseStream.writableCorked; i++) {
-              responseStream.uncork();
+              // For some reason, putting this in a setImmediate makes it work more often
+              // process.nextTick does not, which should be what we should use
+              setImmediate(() => {
+                responseStream.uncork();
+              });
             }
           }
 

From 8dc9e73210174f5cf9fc7aab67c6f5cf147670bb Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 16 Feb 2024 23:52:38 +0100
Subject: [PATCH 060/102] fix another monorepo error

---
 packages/open-next/src/build/copyTracedFiles.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 1440a4f32..bfbfa6ec1 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -12,7 +12,7 @@ import {
 import path from "path";
 import { NextConfig, PrerenderManifest } from "types/next-types";
 
-import logger from "../logger";
+import logger from "../logger.js";
 
 export async function copyTracedFiles(
   buildOutputPath: string,
@@ -86,7 +86,7 @@ export async function copyTracedFiles(
 
     filesToCopy.set(
       path.join(standaloneNextDir, fullFilePath),
-      path.join(outputDir, ".next", fullFilePath),
+      path.join(outputNextDir, fullFilePath),
     );
   };
 

From 2adb4ab1e5a665ec69abf76ad76ffb657b0c5b9a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 17 Feb 2024 00:04:15 +0100
Subject: [PATCH 061/102] e2e fixes for v3 rc

---
 examples/app-pages-router/open-next.config.ts |   7 +-
 examples/app-router/app/page.tsx              |   3 +
 examples/app-router/app/ssr/layout.tsx        |   4 +
 examples/shared/components/Filler/index.tsx   |  17 +
 examples/sst/stacks/AppPagesRouter.ts         |  15 +-
 examples/sst/stacks/AppRouter.ts              |  24 +-
 .../stacks/OpenNextReferenceImplementation.ts | 446 ++++++++++++++++++
 examples/sst/stacks/PagesRouter.ts            |  15 +-
 8 files changed, 503 insertions(+), 28 deletions(-)
 create mode 100644 examples/shared/components/Filler/index.tsx
 create mode 100644 examples/sst/stacks/OpenNextReferenceImplementation.ts

diff --git a/examples/app-pages-router/open-next.config.ts b/examples/app-pages-router/open-next.config.ts
index 8b09de17e..2a9b827b4 100644
--- a/examples/app-pages-router/open-next.config.ts
+++ b/examples/app-pages-router/open-next.config.ts
@@ -2,12 +2,7 @@ const config = {
   default: {},
   functions: {
     api: {
-      routes: [
-        "app/api/page",
-        "app/api/client/route",
-        "app/api/host/route",
-        "pages/api/hello",
-      ],
+      routes: ["app/api/client/route", "app/api/host/route", "pages/api/hello"],
       patterns: ["/api/*"],
     },
   },
diff --git a/examples/app-router/app/page.tsx b/examples/app-router/app/page.tsx
index 66da3ac80..2f347d3ca 100644
--- a/examples/app-router/app/page.tsx
+++ b/examples/app-router/app/page.tsx
@@ -44,6 +44,9 @@ export default function Home() {
         <Nav href={"/sse"} title="Server Sent Events">
           Server Sent Events via Streaming
         </Nav>
+        <Nav href={"/revalidate-tag"} title="Revalidate Tag">
+          Revalidate Tag
+        </Nav>
       </main>
     </>
   );
diff --git a/examples/app-router/app/ssr/layout.tsx b/examples/app-router/app/ssr/layout.tsx
index 3a8338f50..d51f11bc1 100644
--- a/examples/app-router/app/ssr/layout.tsx
+++ b/examples/app-router/app/ssr/layout.tsx
@@ -1,9 +1,13 @@
 import { PropsWithChildren } from "react";
 
+import Filler from "@example/shared/components/Filler";
+
 export default function Layout({ children }: PropsWithChildren) {
   return (
     <div>
       <h1>SSR</h1>
+      {/* 16 kb seems necessary here to prevent any buffering*/}
+      <Filler size={16} />
       {children}
     </div>
   );
diff --git a/examples/shared/components/Filler/index.tsx b/examples/shared/components/Filler/index.tsx
new file mode 100644
index 000000000..51ddf2e4d
--- /dev/null
+++ b/examples/shared/components/Filler/index.tsx
@@ -0,0 +1,17 @@
+interface FillerProps {
+  // Size in kb of the filler
+  size: number;
+}
+
+//This component is there to demonstrate how you could bypass streaming buffering in aws lambda.
+//Hopefully, this will be fixed in the future and this component will be removed.
+// https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/94
+export default function Filler({ size }: FillerProps) {
+  const str = "a".repeat(size * 1024);
+  const byteSize = new TextEncoder().encode(str).length;
+  return (
+    <script type="application/json">
+      {JSON.stringify({ filler: str, byteSize })}
+    </script>
+  );
+}
diff --git a/examples/sst/stacks/AppPagesRouter.ts b/examples/sst/stacks/AppPagesRouter.ts
index 80b8710ed..9a08c786f 100644
--- a/examples/sst/stacks/AppPagesRouter.ts
+++ b/examples/sst/stacks/AppPagesRouter.ts
@@ -1,15 +1,18 @@
-import { NextjsSite } from "sst/constructs";
+import { OpenNextCdkReferenceImplementation } from "./OpenNextReferenceImplementation";
 
 // NOTE: App Pages Router doesn't do streaming
 export function AppPagesRouter({ stack }) {
-  const site = new NextjsSite(stack, "apppagesrouter", {
+  const site = new OpenNextCdkReferenceImplementation(stack, "apppagesrouter", {
     path: "../app-pages-router",
-    buildCommand: "npm run openbuild",
-    bind: [],
-    environment: {},
   });
+  // const site = new NextjsSite(stack, "apppagesrouter", {
+  //   path: "../app-pages-router",
+  //   buildCommand: "npm run openbuild",
+  //   bind: [],
+  //   environment: {},
+  // });
 
   stack.addOutputs({
-    url: site.url,
+    url: site.distribution.domainName,
   });
 }
diff --git a/examples/sst/stacks/AppRouter.ts b/examples/sst/stacks/AppRouter.ts
index f0e5950d6..df0160a93 100644
--- a/examples/sst/stacks/AppRouter.ts
+++ b/examples/sst/stacks/AppRouter.ts
@@ -1,18 +1,22 @@
-import { NextjsSite } from "sst/constructs";
+import { OpenNextCdkReferenceImplementation } from "./OpenNextReferenceImplementation";
 
 export function AppRouter({ stack }) {
-  const site = new NextjsSite(stack, "approuter", {
+  // We should probably switch to ion once it's ready
+  const site = new OpenNextCdkReferenceImplementation(stack, "approuter", {
     path: "../app-router",
-    buildCommand: "npm run openbuild",
-    bind: [],
-    environment: {},
-    timeout: "20 seconds",
-    experimental: {
-      streaming: true,
-    },
   });
+  // const site = new NextjsSite(stack, "approuter", {
+  //   path: "../app-router",
+  //   buildCommand: "npm run openbuild",
+  //   bind: [],
+  //   environment: {},
+  //   timeout: "20 seconds",
+  //   experimental: {
+  //     streaming: true,
+  //   },
+  // });
 
   stack.addOutputs({
-    url: site.url,
+    url: site.distribution.domainName,
   });
 }
diff --git a/examples/sst/stacks/OpenNextReferenceImplementation.ts b/examples/sst/stacks/OpenNextReferenceImplementation.ts
new file mode 100644
index 000000000..85ecc9a4a
--- /dev/null
+++ b/examples/sst/stacks/OpenNextReferenceImplementation.ts
@@ -0,0 +1,446 @@
+import { execSync } from "node:child_process";
+
+import {
+  AllowedMethods,
+  BehaviorOptions,
+  CacheCookieBehavior,
+  CachedMethods,
+  CacheHeaderBehavior,
+  CachePolicy,
+  CacheQueryStringBehavior,
+  Distribution,
+  Function as CloudfrontFunction,
+  FunctionCode,
+  FunctionEventType,
+  ICachePolicy,
+  OriginRequestPolicy,
+  ViewerProtocolPolicy,
+} from "aws-cdk-lib/aws-cloudfront";
+import { HttpOrigin, S3Origin } from "aws-cdk-lib/aws-cloudfront-origins";
+import {
+  AttributeType,
+  Billing,
+  TableV2 as Table,
+} from "aws-cdk-lib/aws-dynamodb";
+import { IGrantable, Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
+import {
+  Code,
+  Function as CdkFunction,
+  FunctionUrlAuthType,
+  InvokeMode,
+  Runtime,
+} from "aws-cdk-lib/aws-lambda";
+import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
+import { RetentionDays } from "aws-cdk-lib/aws-logs";
+import { BlockPublicAccess, Bucket } from "aws-cdk-lib/aws-s3";
+import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment";
+import { Queue } from "aws-cdk-lib/aws-sqs";
+import {
+  CustomResource,
+  Duration,
+  Fn,
+  RemovalPolicy,
+  Stack,
+} from "aws-cdk-lib/core";
+import { Provider } from "aws-cdk-lib/custom-resources";
+import { Construct } from "constructs";
+import { readFileSync } from "fs";
+import path from "path";
+import { Stack as SSTStack } from "sst/constructs";
+
+type BaseFunction = {
+  handler: string;
+  bundle: string;
+};
+
+type OpenNextFunctionOrigin = {
+  type: "function";
+  streaming?: boolean;
+} & BaseFunction;
+
+type OpenNextECSOrigin = {
+  type: "ecs";
+  bundle: string;
+  dockerfile: string;
+};
+
+type OpenNextS3Origin = {
+  type: "s3";
+  originPath: string;
+  copy: {
+    from: string;
+    to: string;
+    cached: boolean;
+    versionedSubDir?: string;
+  }[];
+};
+
+type OpenNextOrigins =
+  | OpenNextFunctionOrigin
+  | OpenNextECSOrigin
+  | OpenNextS3Origin;
+
+interface OpenNextOutput {
+  edgeFunctions: {
+    [key: string]: BaseFunction;
+  };
+  origins: {
+    s3: OpenNextS3Origin;
+    default: OpenNextFunctionOrigin | OpenNextECSOrigin;
+    imageOptimizer: OpenNextFunctionOrigin | OpenNextECSOrigin;
+    [key: string]: OpenNextOrigins;
+  };
+  behaviors: {
+    pattern: string;
+    origin?: string;
+    edgeFunction?: string;
+  }[];
+  additionalProps?: {
+    disableIncrementalCache?: boolean;
+    disableTagCache?: boolean;
+    initializationFunction?: BaseFunction;
+    warmer?: BaseFunction;
+    revalidationFunction?: BaseFunction;
+  };
+}
+
+interface OpenNextCdkReferenceImplementationProps {
+  path: string;
+}
+
+export class OpenNextCdkReferenceImplementation extends Construct {
+  private openNextOutput: OpenNextOutput;
+  private openNextBasePath: string;
+  private bucket: Bucket;
+  private table: Table;
+  private queue: Queue;
+
+  private staticCachePolicy: ICachePolicy;
+  private serverCachePolicy: CachePolicy;
+
+  public distribution: Distribution;
+
+  constructor(
+    scope: Construct,
+    id: string,
+    props: OpenNextCdkReferenceImplementationProps,
+  ) {
+    super(scope, id);
+    this.openNextBasePath = path.join(process.cwd(), props.path);
+    execSync("npm run openbuild", {
+      cwd: path.join(process.cwd(), props.path),
+      stdio: "inherit",
+    });
+
+    this.openNextOutput = JSON.parse(
+      readFileSync(
+        path.join(this.openNextBasePath, ".open-next/open-next.output.json"),
+        "utf-8",
+      ),
+    ) as OpenNextOutput;
+
+    this.bucket = new Bucket(this, "OpenNextBucket", {
+      publicReadAccess: false,
+      blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
+      autoDeleteObjects: true,
+      removalPolicy: RemovalPolicy.DESTROY,
+      enforceSSL: true,
+    });
+    this.table = this.createRevalidationTable();
+    this.queue = this.createRevalidationQueue();
+
+    const origins = this.createOrigins();
+    this.serverCachePolicy = this.createServerCachePolicy();
+    this.staticCachePolicy = this.createStaticCachePolicy();
+    this.distribution = this.createDistribution(origins);
+    this.createInvalidation();
+  }
+
+  private createRevalidationTable() {
+    const table = new Table(this, "RevalidationTable", {
+      partitionKey: { name: "tag", type: AttributeType.STRING },
+      sortKey: { name: "path", type: AttributeType.STRING },
+      pointInTimeRecovery: true,
+      billing: Billing.onDemand(),
+      globalSecondaryIndexes: [
+        {
+          indexName: "revalidate",
+          partitionKey: { name: "path", type: AttributeType.STRING },
+          sortKey: { name: "revalidatedAt", type: AttributeType.NUMBER },
+        },
+      ],
+      removalPolicy: RemovalPolicy.DESTROY,
+    });
+
+    const initFn = this.openNextOutput.additionalProps?.initializationFunction;
+    if (initFn) {
+      const insertFn = new CdkFunction(this, "RevalidationInsertFunction", {
+        description: "Next.js revalidation data insert",
+        handler: initFn?.handler ?? "index.handler",
+        // code: Code.fromAsset(initFn?.bundle ?? ""),
+        code: Code.fromAsset(
+          path.join(this.openNextBasePath, ".open-next/dynamodb-provider"),
+        ),
+        runtime: Runtime.NODEJS_18_X,
+        timeout: Duration.minutes(15),
+        memorySize: 128,
+        environment: {
+          CACHE_DYNAMO_TABLE: table.tableName,
+        },
+        initialPolicy: [
+          new PolicyStatement({
+            actions: [
+              "dynamodb:BatchWriteItem",
+              "dynamodb:PutItem",
+              "dynamodb:UpdateItem",
+            ],
+            resources: [table.tableArn],
+          }),
+        ],
+      });
+
+      const provider = new Provider(this, "RevalidationProvider", {
+        onEventHandler: insertFn,
+        logRetention: RetentionDays.ONE_DAY,
+      });
+
+      new CustomResource(this, "RevalidationResource", {
+        serviceToken: provider.serviceToken,
+        properties: {
+          version: Date.now().toString(),
+        },
+      });
+    }
+
+    return table;
+  }
+
+  private createOrigins() {
+    const {
+      s3: s3Origin,
+      default: defaultOrigin,
+      imageOptimizer: imageOrigin,
+      ...restOrigins
+    } = this.openNextOutput.origins;
+    for (const copy of s3Origin.copy) {
+      new BucketDeployment(this, `OpenNextBucketDeployment${copy.from}`, {
+        sources: [Source.asset(path.join(this.openNextBasePath, copy.from))],
+        destinationBucket: this.bucket,
+        destinationKeyPrefix: copy.to,
+        prune: false,
+      });
+    }
+    const origins = {
+      s3: new S3Origin(this.bucket, {
+        originPath: s3Origin.originPath,
+        originAccessIdentity: undefined,
+      }),
+      default:
+        defaultOrigin.type === "function"
+          ? this.createFunctionOrigin("default", defaultOrigin)
+          : this.createAppRunnerOrigin("default", defaultOrigin),
+      imageOptimizer:
+        imageOrigin.type === "function"
+          ? this.createFunctionOrigin("imageOptimizer", imageOrigin)
+          : this.createAppRunnerOrigin("imageOptimizer", imageOrigin),
+      ...Object.entries(restOrigins).reduce(
+        (acc, [key, value]) => {
+          if (value.type === "function") {
+            acc[key] = this.createFunctionOrigin(key, value);
+            // eslint-disable-next-line sonarjs/elseif-without-else
+          } else if (value.type === "ecs") {
+            acc[key] = this.createAppRunnerOrigin(key, value);
+          }
+          return acc;
+        },
+        {} as Record<string, HttpOrigin>,
+      ),
+    };
+    return origins;
+  }
+
+  private createRevalidationQueue() {
+    const queue = new Queue(this, "RevalidationQueue", {
+      fifo: true,
+      receiveMessageWaitTime: Duration.seconds(20),
+    });
+    const consumer = new CdkFunction(this, "RevalidationFunction", {
+      description: "Next.js revalidator",
+      handler: "index.handler",
+      code: Code.fromAsset(
+        path.join(
+          this.openNextBasePath,
+          this.openNextOutput.additionalProps?.revalidationFunction?.bundle ??
+            "",
+        ),
+      ),
+      runtime: Runtime.NODEJS_18_X,
+      timeout: Duration.seconds(30),
+    });
+    consumer.addEventSource(new SqsEventSource(queue, { batchSize: 5 }));
+    return queue;
+  }
+
+  private getEnvironment() {
+    return {
+      CACHE_BUCKET_NAME: this.bucket.bucketName,
+      CACHE_BUCKET_KEY_PREFIX: "_cache",
+      CACHE_BUCKET_REGION: Stack.of(this).region,
+      REVALIDATION_QUEUE_URL: this.queue.queueUrl,
+      REVALIDATION_QUEUE_REGION: Stack.of(this).region,
+      CACHE_DYNAMO_TABLE: this.table.tableName,
+      // Those 2 are used only for image optimizer
+      BUCKET_NAME: this.bucket.bucketName,
+      BUCKET_KEY_PREFIX: "_assets",
+    };
+  }
+
+  private grantPermissions(grantable: IGrantable) {
+    this.bucket.grantReadWrite(grantable);
+    this.table.grantReadWriteData(grantable);
+    this.queue.grantSendMessages(grantable);
+  }
+
+  private createFunctionOrigin(key: string, origin: OpenNextFunctionOrigin) {
+    const environment = this.getEnvironment();
+    const fn = new CdkFunction(this, `${key}Function`, {
+      runtime: Runtime.NODEJS_18_X,
+      handler: origin.handler,
+      code: Code.fromAsset(path.join(this.openNextBasePath, origin.bundle)),
+      environment,
+      memorySize: 1024,
+      timeout: Duration.seconds(20),
+    });
+    const fnUrl = fn.addFunctionUrl({
+      authType: FunctionUrlAuthType.NONE,
+      invokeMode: origin.streaming
+        ? InvokeMode.RESPONSE_STREAM
+        : InvokeMode.BUFFERED,
+    });
+    this.grantPermissions(fn);
+    return new HttpOrigin(Fn.parseDomainName(fnUrl.url));
+  }
+
+  // We are using AppRunner because it is the easiest way to demonstrate the new feature.
+  // You can use any other container service like ECS, EKS, Fargate, etc.
+  private createAppRunnerOrigin(
+    _key: string,
+    _origin: OpenNextECSOrigin,
+  ): HttpOrigin {
+    throw new Error("Not implemented");
+  }
+
+  private createDistribution(origins: Record<string, HttpOrigin | S3Origin>) {
+    const cloudfrontFunction = new CloudfrontFunction(
+      this,
+      "OpenNextCfFunction",
+      {
+        code: FunctionCode.fromInline(`
+			function handler(event) {
+				var request = event.request;
+				request.headers["x-forwarded-host"] = request.headers.host;
+				return request;
+			}
+			`),
+      },
+    );
+    const fnAssociations = [
+      {
+        function: cloudfrontFunction,
+        eventType: FunctionEventType.VIEWER_REQUEST,
+      },
+    ];
+
+    const distribution = new Distribution(this, "OpenNextDistribution", {
+      defaultBehavior: {
+        origin: origins.default,
+        viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
+        allowedMethods: AllowedMethods.ALLOW_ALL,
+        cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
+        cachePolicy: this.serverCachePolicy,
+        originRequestPolicy: OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
+        functionAssociations: fnAssociations,
+      },
+      additionalBehaviors: this.openNextOutput.behaviors
+        .filter((b) => b.pattern !== "*")
+        .reduce(
+          (acc, behavior) => {
+            return {
+              ...acc,
+              [behavior.pattern]: {
+                origin: behavior.origin
+                  ? origins[behavior.origin]
+                  : origins.default,
+                viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
+                allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
+                cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
+                cachePolicy:
+                  behavior.origin === "s3"
+                    ? this.staticCachePolicy
+                    : this.serverCachePolicy,
+                originRequestPolicy:
+                  behavior.origin === "s3"
+                    ? undefined
+                    : OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
+                functionAssociations: fnAssociations,
+              },
+            };
+          },
+          {} as Record<string, BehaviorOptions>,
+        ),
+    });
+    return distribution;
+  }
+
+  private createInvalidation() {
+    const stack = SSTStack.of(this) as SSTStack;
+    const policy = new Policy(this, "OpenNextInvalidationPolicy", {
+      statements: [
+        new PolicyStatement({
+          actions: [
+            "cloudfront:CreateInvalidation",
+            "cloudfront:GetInvalidation",
+          ],
+          resources: [
+            `arn:${stack.partition}:cloudfront::${stack.account}:distribution/${this.distribution.distributionId}`,
+          ],
+        }),
+      ],
+    });
+
+    stack.customResourceHandler.role?.attachInlinePolicy(policy);
+    const resource = new CustomResource(this, "OpenNextInvalidationResource", {
+      serviceToken: stack.customResourceHandler.functionArn,
+      resourceType: "Custom::CloudFrontInvalidator",
+      properties: {
+        version: Date.now().toString(16) + Math.random().toString(16).slice(2),
+        distributionId: this.distribution.distributionId,
+        paths: ["/*"],
+        wait: true,
+      },
+    });
+    resource.node.addDependency(policy);
+  }
+
+  private createServerCachePolicy() {
+    return new CachePolicy(this, "OpenNextServerCachePolicy", {
+      queryStringBehavior: CacheQueryStringBehavior.all(),
+      headerBehavior: CacheHeaderBehavior.allowList(
+        "accept",
+        "rsc",
+        "next-router-prefetch",
+        "next-router-state-tree",
+        "next-url",
+        "x-prerender-revalidate",
+      ),
+      cookieBehavior: CacheCookieBehavior.none(),
+      defaultTtl: Duration.days(0),
+      maxTtl: Duration.days(365),
+      minTtl: Duration.days(0),
+    });
+  }
+
+  private createStaticCachePolicy() {
+    return CachePolicy.CACHING_OPTIMIZED;
+  }
+}
diff --git a/examples/sst/stacks/PagesRouter.ts b/examples/sst/stacks/PagesRouter.ts
index e6ae5842b..34b39b918 100644
--- a/examples/sst/stacks/PagesRouter.ts
+++ b/examples/sst/stacks/PagesRouter.ts
@@ -1,14 +1,17 @@
-import { NextjsSite } from "sst/constructs";
+import { OpenNextCdkReferenceImplementation } from "./OpenNextReferenceImplementation";
 
 export function PagesRouter({ stack }) {
-  const site = new NextjsSite(stack, "pagesrouter", {
+  const site = new OpenNextCdkReferenceImplementation(stack, "pagesrouter", {
     path: "../pages-router",
-    buildCommand: "npm run openbuild",
-    bind: [],
-    environment: {},
   });
+  // const site = new NextjsSite(stack, "pagesrouter", {
+  //   path: "../pages-router",
+  //   buildCommand: "npm run openbuild",
+  //   bind: [],
+  //   environment: {},
+  // });
 
   stack.addOutputs({
-    url: site.url,
+    url: site.distribution.domainName,
   });
 }

From e1a958971841d9ca4101f84d6028fb9a51faff6e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 17 Feb 2024 00:05:55 +0100
Subject: [PATCH 062/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index ecac2c343..181a3cfff 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.4",
+  "version": "3.0.0-rc.5",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 68858bbcd077769a209a09919b85a825a74536be Mon Sep 17 00:00:00 2001
From: Frank <wangfanjie@gmail.com>
Date: Tue, 20 Feb 2024 17:33:44 -0500
Subject: [PATCH 063/102] Not use custom-resource converter for dynamodb
 seeding adapter (#365)

* Not use custom-resource converter for dynamodb seeding adapter

* fix e2e

---------

Co-authored-by: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
---
 .../stacks/OpenNextReferenceImplementation.ts | 31 ++++++++++++-------
 .../open-next/src/adapters/dynamo-provider.ts |  5 +--
 packages/open-next/src/build.ts               |  2 +-
 .../src/converters/custom-resource.ts         | 29 -----------------
 packages/open-next/src/types/open-next.ts     |  1 -
 5 files changed, 24 insertions(+), 44 deletions(-)
 delete mode 100644 packages/open-next/src/converters/custom-resource.ts

diff --git a/examples/sst/stacks/OpenNextReferenceImplementation.ts b/examples/sst/stacks/OpenNextReferenceImplementation.ts
index 85ecc9a4a..2e29fe861 100644
--- a/examples/sst/stacks/OpenNextReferenceImplementation.ts
+++ b/examples/sst/stacks/OpenNextReferenceImplementation.ts
@@ -31,7 +31,6 @@ import {
   Runtime,
 } from "aws-cdk-lib/aws-lambda";
 import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
-import { RetentionDays } from "aws-cdk-lib/aws-logs";
 import { BlockPublicAccess, Bucket } from "aws-cdk-lib/aws-s3";
 import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment";
 import { Queue } from "aws-cdk-lib/aws-sqs";
@@ -42,7 +41,11 @@ import {
   RemovalPolicy,
   Stack,
 } from "aws-cdk-lib/core";
-import { Provider } from "aws-cdk-lib/custom-resources";
+import {
+  AwsCustomResource,
+  AwsCustomResourcePolicy,
+  PhysicalResourceId,
+} from "aws-cdk-lib/custom-resources";
 import { Construct } from "constructs";
 import { readFileSync } from "fs";
 import path from "path";
@@ -199,16 +202,22 @@ export class OpenNextCdkReferenceImplementation extends Construct {
         ],
       });
 
-      const provider = new Provider(this, "RevalidationProvider", {
-        onEventHandler: insertFn,
-        logRetention: RetentionDays.ONE_DAY,
-      });
-
-      new CustomResource(this, "RevalidationResource", {
-        serviceToken: provider.serviceToken,
-        properties: {
-          version: Date.now().toString(),
+      new AwsCustomResource(this, "RevalidationInitResource", {
+        onUpdate: {
+          service: "Lambda",
+          action: "invoke",
+          parameters: {
+            FunctionName: insertFn.functionArn,
+          },
+          physicalResourceId: PhysicalResourceId.of("dynamodb-cache"),
         },
+
+        policy: AwsCustomResourcePolicy.fromStatements([
+          new PolicyStatement({
+            actions: ["lambda:InvokeFunction"],
+            resources: [insertFn.functionArn],
+          }),
+        ]),
       });
     }
 
diff --git a/packages/open-next/src/adapters/dynamo-provider.ts b/packages/open-next/src/adapters/dynamo-provider.ts
index b79226ff3..76d924b5a 100644
--- a/packages/open-next/src/adapters/dynamo-provider.ts
+++ b/packages/open-next/src/adapters/dynamo-provider.ts
@@ -42,11 +42,12 @@ async function defaultHandler(
   event: InitializationFunctionEvent,
 ): Promise<InitializationFunctionEvent> {
   switch (event.requestType) {
+    case "delete":
+      return remove();
     case "create":
     case "update":
+    default:
       return insert(event.requestType);
-    case "delete":
-      return remove();
   }
 }
 
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 10465b68d..b1642a863 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -613,7 +613,7 @@ async function createCacheAssets(monorepoRoot: string) {
           plugins: [
             openNextResolvePlugin({
               overrides: {
-                converter: "custom-resource",
+                converter: "dummy",
               },
             }),
           ],
diff --git a/packages/open-next/src/converters/custom-resource.ts b/packages/open-next/src/converters/custom-resource.ts
deleted file mode 100644
index 46377d489..000000000
--- a/packages/open-next/src/converters/custom-resource.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { CdkCustomResourceEvent } from "aws-lambda";
-import { Converter } from "types/open-next";
-
-import type { InitializationFunctionEvent } from "../adapters/dynamo-provider";
-
-const converter: Converter<
-  InitializationFunctionEvent,
-  InitializationFunctionEvent
-> = {
-  convertFrom(event: CdkCustomResourceEvent) {
-    return Promise.resolve({
-      type: "initializationFunction",
-      requestType: event.RequestType.toLowerCase() as
-        | "create"
-        | "update"
-        | "delete",
-      resourceId: "dynamodb-cache",
-    });
-  },
-  convertTo(internalResult) {
-    return Promise.resolve({
-      type: "dummy",
-      original: internalResult,
-    });
-  },
-  name: "customResource",
-};
-
-export default converter;
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 5fe81a464..ea37285d1 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -112,7 +112,6 @@ export type IncludedConverter =
   | "edge"
   | "node"
   | "sqs-revalidate"
-  | "custom-resource"
   | "dummy";
 
 export type IncludedQueue = "sqs";

From 235742998dfc85aa8b83f9422b0f4d337973f196 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 20 Feb 2024 23:47:19 +0100
Subject: [PATCH 064/102] fix fallback false for route without i18n

---
 packages/open-next/src/core/routing/matcher.ts | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index dc061d803..95a308a1e 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -289,7 +289,9 @@ export function handleFallbackFalse(
     });
   const locales = NextConfig.i18n?.locales;
   const routesAlreadyHaveLocale =
-    locales !== undefined && locales.includes(rawPath.split("/")[1]);
+    (locales !== undefined && locales.includes(rawPath.split("/")[1])) ||
+    // If we don't use locales, we don't need to add the default locale
+    locales === undefined;
   const localizedPath = routesAlreadyHaveLocale
     ? rawPath
     : `/${NextConfig.i18n?.defaultLocale}${rawPath}`;

From 46562b17a707692d0112d1b2c1802bb11b20e82f Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 20 Feb 2024 23:53:36 +0100
Subject: [PATCH 065/102] version package update

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 181a3cfff..28f72c2c1 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.5",
+  "version": "3.0.0-rc.6",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 429914ad8943efaa81a1ce513e0d47cff244ed3e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 10 Mar 2024 11:30:22 +0100
Subject: [PATCH 066/102] Squashed commit of the following:

commit ff37de235e653e3aecdbb31168ab411add2d0330
Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Wed Mar 6 15:37:07 2024 +0100

    Version Packages (#378)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

commit 32353923a4a0e99217d04d5790feac539a4c0e15
Author: Iakhub Seitasanov <seitasanov.yahub@gmail.com>
Date:   Wed Mar 6 17:29:46 2024 +0300

    fix: prevent duplication of location header (#369)

    * fix: prevent duplication of location header

    * changeset

    * fix linting

    ---------

    Co-authored-by: conico974 <nicodorseuil@yahoo.fr>

commit af2d3ce484cf5bcd57d11d07174097457a9cd119
Author: Chung Wei Leong <15154097+chungweileong94@users.noreply.github.com>
Date:   Wed Mar 6 22:06:33 2024 +0800

    Fix image optimization support for Next 14.1.1 (#377)

    * Move image optimization to plugin

    * Refactor image optimization code

    * Added image optimization plugin for 14.1.1

    * Fix image optimization plugin

    * Add changeset

    * Revert default sharp version to 0.32.6

    * e2e test for image optimization

    * change one of the test to use an external image

    ---------

    Co-authored-by: Dorseuil Nicolas <nicodorseuil@yahoo.fr>

commit 3deb2022d0bb506e4c4b66baeae248c7c2b153e5
Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Tue Feb 13 08:39:35 2024 -0800

    Version Packages (#363)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

commit f9b90b6219b7eef71be69ee94185c9f5be8537db
Author: khuezy <khuezy.nguyen@gmail.com>
Date:   Tue Feb 13 08:35:10 2024 -0800

    changeset/2.3.6 (#362)

commit 40c2b36696001ba2482e952189d327d24d1800e8
Author: Patrick Ufer <46608534+patrickufer@users.noreply.github.com>
Date:   Tue Feb 13 09:23:40 2024 -0700

    security fix: upgrade sharp version to 0.32.6 (#361)

    * upgrade sharp version

commit 63fab055c6e0e1fe4591555cc0f91dcf3d4d739a
Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Fri Feb 2 00:14:11 2024 +0100

    Version Packages (#359)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

commit c80f1be1d987d48daec28a2e65934ecfb5aa107c
Author: conico974 <nicodorseuil@yahoo.fr>
Date:   Fri Feb 2 00:00:56 2024 +0100

    Fix trailing slash redirect to external domain (#358)

    * fix trailing slash redirect to external domain

    * changeset

commit 186e28f83409e6f96b5e05d3439c36a545eb7e08
Author: Jaden VanEckhout <jadenv@users.noreply.github.com>
Date:   Thu Feb 1 16:49:14 2024 -0600

    fix(open-next): correctly set cache control for html pages (#353)

    * fix(open-next): correctly set cache control for html pages

    * changeset

    ---------

    Co-authored-by: conico974 <nicodorseuil@yahoo.fr>

commit b9eefca37f1846ac05da5a9aa25a2d421cb77542
Author: Manuel Antunes <57446204+Manuel-Antunes@users.noreply.github.com>
Date:   Thu Feb 1 19:41:47 2024 -0300

    Fix Cache Support for Next@14.1.0 (#356)

    * feat: add cache support for next@14.1.0

    * fix: lint files

    * chore: apply the proposed changes

    * Fix typo

    * changeset

    ---------

    Co-authored-by: conico974 <nicodorseuil@yahoo.fr>

commit afd9605048cfdf1c0cd3f224da34a27c3f27128f
Author: conico974 <nicodorseuil@yahoo.fr>
Date:   Sat Jan 27 15:19:11 2024 +0100

    update docs for V3 (#351)

commit 46241fe9c43a3de2e5507acca7758ddf6d6aa6d1
Author: Abhishek Malik <abhimskywalker@users.noreply.github.com>
Date:   Sat Jan 27 19:45:18 2024 +0530

    Update bundle_size.mdx for excluding pdfjs-dist optional dependency docs (#346)

    * Update bundle_size.mdx for excluding pdfjs-dist optional dependency docs

    The current fix didn't work, but this updated fix did work for me. Hence proposing this as another solution.

    * Update docs/pages/common_issues/bundle_size.mdx

    Co-authored-by: khuezy <khuezy.nguyen@gmail.com>

    ---------

    Co-authored-by: khuezy <khuezy.nguyen@gmail.com>

commit 9a6473a9cfefda0da9d7a810cb983eeb8752ea82
Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Fri Jan 5 16:56:42 2024 +0100

    Version Packages (#345)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

commit bbf9b305679a332f05947feebdc3854bbd13ea4f
Author: Lucas Vieira <lucas.vieira94@outlook.com>
Date:   Fri Jan 5 12:45:13 2024 -0300

    fix(open-next): use dynamic import handler for monorepo entrypoint (#341)

    * fix(open-next): use dynamic import handler for monorepo entrypoint

    * changeset

    ---------

    Co-authored-by: Dorseuil Nicolas <nicodorseuil@yahoo.fr>

commit 83b08389ab80d72972ed32ec8e36c9ee5a3399db
Author: santiperone <sdperone97@gmail.com>
Date:   Fri Jan 5 12:38:12 2024 -0300

    add suport for bun lockfile in monorepo (#337)

    * add suport for bun lockfile in monorepo

    * changeset

    ---------

    Co-authored-by: Dorseuil Nicolas <nicodorseuil@yahoo.fr>

commit e773e67ed9394e2f3e6962f1ed29be0078e084c4
Author: Jan Stevens <JanStevens@users.noreply.github.com>
Date:   Fri Jan 5 16:31:27 2024 +0100

    fix: try to match errors, fall back to just adding raw key / value pare (#336)

    * fix: try to match errors, fall back to just adding raw key / value pair instead

    * changeset

    * fix lint

    ---------

    Co-authored-by: Dorseuil Nicolas <nicodorseuil@yahoo.fr>

commit fd90b260f5fe2c02bb1ca1f818b9c5c5d9937a1b
Author: Dylan Irion <61515823+dylanirion@users.noreply.github.com>
Date:   Fri Jan 5 17:22:28 2024 +0200

    Changes encoding on cache.body from utf8 to base64 (#329)

    * changes encoding on cache.body from utf8 to base64

    * retain utf8 for json content-type

    * opting for less greedy base64

    * use isBinaryContentType

    * changeset

    ---------

    Co-authored-by: Dorseuil Nicolas <nicodorseuil@yahoo.fr>

commit eb089800225bcedcd47b0b10a6938692f9e6cb0a
Author: sommeeeR <91796856+sommeeeer@users.noreply.github.com>
Date:   Fri Jan 5 16:02:47 2024 +0100

    fix: make invalidateCFPaths function async in docs (#344)

commit 83207d87d30b622bce299895bb2cf605ee0f39af
Author: conico974 <nicodorseuil@yahoo.fr>
Date:   Thu Dec 14 16:59:15 2023 +0100

    updated docs for v3 (#334)

commit 0e827ce4aefab5e337822cda0d43be218e919cb8
Author: conico974 <nicodorseuil@yahoo.fr>
Date:   Fri Dec 8 17:57:51 2023 +0100

    ci: update node e2e

commit 36da8198c30b9b0dc7cfd7628dbc3c6dffc59961
Author: conico974 <nicodorseuil@yahoo.fr>
Date:   Thu Dec 7 17:44:06 2023 +0100

    Initial docs for V3 (#330)

    * docs for V3

    * fix link

    * clearer routes in config
---
 .../app/image-optimization/page.tsx           |  14 +++++
 examples/app-pages-router/app/page.tsx        |   3 ++
 .../public/static/corporate_holiday_card.jpg  | Bin 0 -> 120302 bytes
 .../app/image-optimization/page.tsx           |  14 +++++
 examples/app-router/app/page.tsx              |   3 ++
 examples/app-router/next.config.js            |   8 +++
 .../public/static/corporate_holiday_card.jpg  | Bin 0 -> 120302 bytes
 packages/open-next/CHANGELOG.md               |  13 +++++
 .../adapters/image-optimization-adapter.ts    |  27 +++-------
 .../image-optimization.replacement.ts         |  51 ++++++++++++++++++
 .../image-optimization/image-optimization.ts  |  35 ++++++++++++
 packages/open-next/src/build.ts               |  29 ++++++++--
 .../open-next/src/core/routing/middleware.ts  |  18 +++----
 .../appPagesRouter/image-optimization.test.ts |  20 +++++++
 .../appRouter/image-optimization.test.ts      |  20 +++++++
 packages/tests-unit/CHANGELOG.md              |  15 ++++++
 16 files changed, 236 insertions(+), 34 deletions(-)
 create mode 100644 examples/app-pages-router/app/image-optimization/page.tsx
 create mode 100644 examples/app-pages-router/public/static/corporate_holiday_card.jpg
 create mode 100644 examples/app-router/app/image-optimization/page.tsx
 create mode 100644 examples/app-router/public/static/corporate_holiday_card.jpg
 create mode 100644 packages/open-next/src/adapters/plugins/image-optimization/image-optimization.replacement.ts
 create mode 100644 packages/open-next/src/adapters/plugins/image-optimization/image-optimization.ts
 create mode 100644 packages/tests-e2e/tests/appPagesRouter/image-optimization.test.ts
 create mode 100644 packages/tests-e2e/tests/appRouter/image-optimization.test.ts

diff --git a/examples/app-pages-router/app/image-optimization/page.tsx b/examples/app-pages-router/app/image-optimization/page.tsx
new file mode 100644
index 000000000..f43655af4
--- /dev/null
+++ b/examples/app-pages-router/app/image-optimization/page.tsx
@@ -0,0 +1,14 @@
+import Image from "next/image";
+
+export default function ImageOptimization() {
+  return (
+    <div>
+      <Image
+        src="/static/corporate_holiday_card.jpg"
+        alt="Corporate Holiday Card"
+        width={300}
+        height={300}
+      />
+    </div>
+  );
+}
diff --git a/examples/app-pages-router/app/page.tsx b/examples/app-pages-router/app/page.tsx
index 0fd02970e..1b32a5c3d 100644
--- a/examples/app-pages-router/app/page.tsx
+++ b/examples/app-pages-router/app/page.tsx
@@ -33,6 +33,9 @@ export default function Home() {
         <Nav href={"/parallel"} title="Parallel">
           Parallel routing
         </Nav>
+        <Nav href={"/image-optimization"} title="Image Optimization">
+          Image Optimization with next/image
+        </Nav>
       </main>
       <h1>Pages Router</h1>
       <main className="grid grid-cols-2 gap-4 p-10 [&>a]:border">
diff --git a/examples/app-pages-router/public/static/corporate_holiday_card.jpg b/examples/app-pages-router/public/static/corporate_holiday_card.jpg
new file mode 100644
index 0000000000000000000000000000000000000000..0df96ae239c2f5f46bcf9080a2273379254eaf1a
GIT binary patch
diff --git a/examples/app-router/app/image-optimization/page.tsx b/examples/app-router/app/image-optimization/page.tsx
new file mode 100644
index 000000000..baba473bc
--- /dev/null
+++ b/examples/app-router/app/image-optimization/page.tsx
@@ -0,0 +1,14 @@
+import Image from "next/image";
+
+export default function ImageOptimization() {
+  return (
+    <div>
+      <Image
+        src="https://open-next.js.org/architecture.png"
+        alt="Open Next architecture"
+        width={300}
+        height={300}
+      />
+    </div>
+  );
+}
diff --git a/examples/app-router/app/page.tsx b/examples/app-router/app/page.tsx
index 2f347d3ca..e07d65a31 100644
--- a/examples/app-router/app/page.tsx
+++ b/examples/app-router/app/page.tsx
@@ -47,6 +47,9 @@ export default function Home() {
         <Nav href={"/revalidate-tag"} title="Revalidate Tag">
           Revalidate Tag
         </Nav>
+        <Nav href={"/image-optimization"} title="Image Optimization">
+          Image Optimization with next/image
+        </Nav>
       </main>
     </>
   );
diff --git a/examples/app-router/next.config.js b/examples/app-router/next.config.js
index 447979148..ce0565a83 100644
--- a/examples/app-router/next.config.js
+++ b/examples/app-router/next.config.js
@@ -8,6 +8,14 @@ const nextConfig = {
   experimental: {
     serverActions: true,
   },
+  images: {
+    remotePatterns: [
+      {
+        protocol: "https",
+        hostname: "open-next.js.org",
+      },
+    ],
+  },
   redirects: () => {
     return [
       {
diff --git a/examples/app-router/public/static/corporate_holiday_card.jpg b/examples/app-router/public/static/corporate_holiday_card.jpg
new file mode 100644
index 0000000000000000000000000000000000000000..0df96ae239c2f5f46bcf9080a2273379254eaf1a
GIT binary patch
diff --git a/packages/open-next/CHANGELOG.md b/packages/open-next/CHANGELOG.md
index 40998f46b..e3fb0e95c 100644
--- a/packages/open-next/CHANGELOG.md
+++ b/packages/open-next/CHANGELOG.md
@@ -1,5 +1,18 @@
 # open-next
 
+## 2.3.7
+
+### Patch Changes
+
+- 3235392: fix: prevent duplication of location header
+- af2d3ce: Fix Image Optimization Support for Next@14.1.1
+
+## 2.3.6
+
+### Patch Changes
+
+- f9b90b6: Security fix: sharp@0.33.2
+
 ## 2.3.5
 
 ### Patch Changes
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index f27ef0f42..f8a0b1434 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -8,12 +8,10 @@ import path from "node:path";
 import { Writable } from "node:stream";
 
 import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
-import type { APIGatewayProxyEventHeaders } from "aws-lambda";
 import { loadConfig } from "config/util.js";
 // @ts-ignore
 import { defaultConfig } from "next/dist/server/config-shared";
 import {
-  imageOptimizer,
   ImageOptimizerCache,
   // @ts-ignore
 } from "next/dist/server/image-optimizer";
@@ -23,6 +21,7 @@ import { ImageLoader, InternalEvent, InternalResult } from "types/open-next.js";
 
 import { createGenericHandler } from "../core/createGenericHandler.js";
 import { awsLogger, debug, error } from "./logger.js";
+import { optimizeImage } from "./plugins/image-optimization/image-optimization.js";
 import { setNodeEnv } from "./util.js";
 
 // Expected environment variables
@@ -69,7 +68,12 @@ export async function defaultHandler(
       headers,
       queryString === null ? undefined : queryString,
     );
-    const result = await optimizeImage(headers, imageParams);
+    const result = await optimizeImage(
+      headers,
+      imageParams,
+      nextConfig,
+      downloadHandler,
+    );
 
     return buildSuccessResponse(result);
   } catch (e: any) {
@@ -115,23 +119,6 @@ function validateImageParams(
   return imageParams;
 }
 
-async function optimizeImage(
-  headers: APIGatewayProxyEventHeaders,
-  imageParams: any,
-) {
-  const result = await imageOptimizer(
-    // @ts-ignore
-    { headers },
-    {}, // res object is not necessary as it's not actually used.
-    imageParams,
-    nextConfig,
-    false, // not in dev mode
-    downloadHandler,
-  );
-  debug("optimized result", result);
-  return result;
-}
-
 function buildSuccessResponse(result: any): InternalResult {
   return {
     type: "core",
diff --git a/packages/open-next/src/adapters/plugins/image-optimization/image-optimization.replacement.ts b/packages/open-next/src/adapters/plugins/image-optimization/image-optimization.replacement.ts
new file mode 100644
index 000000000..4b948be4d
--- /dev/null
+++ b/packages/open-next/src/adapters/plugins/image-optimization/image-optimization.replacement.ts
@@ -0,0 +1,51 @@
+import type { IncomingMessage, ServerResponse } from "node:http";
+
+import type { APIGatewayProxyEventHeaders } from "aws-lambda";
+import type { NextConfig } from "next/dist/server/config-shared";
+//#override imports
+import {
+  // @ts-ignore
+  fetchExternalImage,
+  // @ts-ignore
+  fetchInternalImage,
+  imageOptimizer,
+} from "next/dist/server/image-optimizer";
+//#endOverride
+import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
+
+import { debug } from "../../logger.js";
+
+//#override optimizeImage
+export async function optimizeImage(
+  headers: APIGatewayProxyEventHeaders,
+  imageParams: any,
+  nextConfig: NextConfig,
+  handleRequest: (
+    newReq: IncomingMessage,
+    newRes: ServerResponse,
+    newParsedUrl?: NextUrlWithParsedQuery,
+  ) => Promise<void>,
+) {
+  const { isAbsolute, href } = imageParams;
+
+  const imageUpstream = isAbsolute
+    ? await fetchExternalImage(href)
+    : await fetchInternalImage(
+        href,
+        // @ts-ignore
+        { headers },
+        {}, // res object is not necessary as it's not actually used.
+        handleRequest,
+      );
+
+  // @ts-ignore
+  const result = await imageOptimizer(
+    imageUpstream,
+    imageParams,
+    nextConfig,
+    false, // not in dev mode
+  );
+  debug("optimized result", result);
+  return result;
+}
+//#endOverride
diff --git a/packages/open-next/src/adapters/plugins/image-optimization/image-optimization.ts b/packages/open-next/src/adapters/plugins/image-optimization/image-optimization.ts
new file mode 100644
index 000000000..5a9fc75ea
--- /dev/null
+++ b/packages/open-next/src/adapters/plugins/image-optimization/image-optimization.ts
@@ -0,0 +1,35 @@
+import { IncomingMessage, ServerResponse } from "node:http";
+
+import { APIGatewayProxyEventHeaders } from "aws-lambda";
+import { NextConfig } from "next/dist/server/config-shared";
+//#override imports
+import { imageOptimizer } from "next/dist/server/image-optimizer";
+//#endOverride
+import { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
+
+import { debug } from "../../logger.js";
+
+//#override optimizeImage
+export async function optimizeImage(
+  headers: APIGatewayProxyEventHeaders,
+  imageParams: any,
+  nextConfig: NextConfig,
+  handleRequest: (
+    newReq: IncomingMessage,
+    newRes: ServerResponse,
+    newParsedUrl: NextUrlWithParsedQuery,
+  ) => Promise<void>,
+) {
+  const result = await imageOptimizer(
+    // @ts-ignore
+    { headers },
+    {}, // res object is not necessary as it's not actually used.
+    imageParams,
+    nextConfig,
+    false, // not in dev mode
+    handleRequest,
+  );
+  debug("optimized result", result);
+  return result;
+}
+//#endOverride
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index b1642a863..7b19e6a10 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -1,5 +1,6 @@
 import cp from "node:child_process";
 import fs, { readFileSync } from "node:fs";
+import { createRequire as topLevelCreateRequire } from "node:module";
 import os from "node:os";
 import path from "node:path";
 import url from "node:url";
@@ -13,6 +14,7 @@ import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js";
 import { generateOutput } from "./build/generateOutput.js";
 import {
   BuildOptions,
+  compareSemver,
   copyOpenNextConfig,
   esbuildAsync,
   esbuildSync,
@@ -24,9 +26,11 @@ import {
 } from "./build/helper.js";
 import { validateConfig } from "./build/validateConfig.js";
 import logger from "./logger.js";
+import { openNextReplacementPlugin } from "./plugins/replacement.js";
 import { openNextResolvePlugin } from "./plugins/resolve.js";
 import { OpenNextConfig } from "./types/open-next.js";
 
+const require = topLevelCreateRequire(import.meta.url);
 const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
 let options: BuildOptions;
 let config: OpenNextConfig;
@@ -74,9 +78,10 @@ export async function build(openNextConfigPath?: string) {
 
   createStaticAssets();
   await createCacheAssets(monorepoRoot);
+
   await createServerBundle(config, options);
   await createRevalidationBundle();
-  createImageOptimizationBundle();
+  await createImageOptimizationBundle();
   await createWarmerBundle();
   await generateOutput(options.appBuildOutputPath, config);
   logger.info("OpenNext build complete.");
@@ -314,7 +319,7 @@ async function createRevalidationBundle() {
   );
 }
 
-function createImageOptimizationBundle() {
+async function createImageOptimizationBundle() {
   logger.info(`Bundling image optimization function...`);
 
   const { appPath, appBuildOutputPath, outputDir } = options;
@@ -326,17 +331,33 @@ function createImageOptimizationBundle() {
   // Copy open-next.config.mjs into the bundle
   copyOpenNextConfig(options.tempDir, outputPath);
 
+  const plugins =
+    compareSemver(options.nextVersion, "14.1.1") >= 0
+      ? [
+          openNextReplacementPlugin({
+            name: "opennext-14.1.1-image-optimization",
+            target: /plugins\/image-optimization\/image-optimization\.js/g,
+            replacements: [
+              require.resolve(
+                "./adapters/plugins/image-optimization/image-optimization.replacement.js",
+              ),
+            ],
+          }),
+        ]
+      : undefined;
+
   // Build Lambda code (1st pass)
   // note: bundle in OpenNext package b/c the adapter relies on the
   //       "@aws-sdk/client-s3" package which is not a dependency in user's
   //       Next.js app.
-  esbuildSync(
+  await esbuildAsync(
     {
       entryPoints: [
         path.join(__dirname, "adapters", "image-optimization-adapter.js"),
       ],
       external: ["sharp", "next"],
       outfile: path.join(outputPath, "index.mjs"),
+      plugins,
     },
     options,
   );
@@ -375,7 +396,7 @@ function createImageOptimizationBundle() {
   // For SHARP_IGNORE_GLOBAL_LIBVIPS see: https://github.com/lovell/sharp/blob/main/docs/install.md#aws-lambda
 
   const nodeOutputPath = path.resolve(outputPath);
-  const sharpVersion = process.env.SHARP_VERSION ?? "0.32.5";
+  const sharpVersion = process.env.SHARP_VERSION ?? "0.32.6";
 
   //check if we are running in Windows environment then set env variables accordingly.
   try {
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index b7dd0e0f6..2de20cfcc 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -122,21 +122,19 @@ export async function handleMiddleware(
   // If the middleware returned a Redirect, we set the `Location` header with
   // the redirected url and end the response.
   if (statusCode >= 300 && statusCode < 400) {
-    const location = result.headers
-      .get("location")
-      ?.replace(
-        "http://localhost:3000",
-        `https://${internalEvent.headers.host}`,
-      );
+    resHeaders.location =
+      responseHeaders
+        .get("location")
+        ?.replace(
+          "http://localhost:3000",
+          `https://${internalEvent.headers.host}`,
+        ) ?? resHeaders.location;
     // res.setHeader("Location", location);
     return {
       body: "",
       type: internalEvent.type,
       statusCode: statusCode,
-      headers: {
-        ...resHeaders,
-        Location: location ?? "",
-      },
+      headers: resHeaders,
       isBase64Encoded: false,
     };
   }
diff --git a/packages/tests-e2e/tests/appPagesRouter/image-optimization.test.ts b/packages/tests-e2e/tests/appPagesRouter/image-optimization.test.ts
new file mode 100644
index 000000000..c26cfe6ec
--- /dev/null
+++ b/packages/tests-e2e/tests/appPagesRouter/image-optimization.test.ts
@@ -0,0 +1,20 @@
+import { expect, test } from "@playwright/test";
+
+test("Image Optimization", async ({ page }) => {
+  await page.goto("/");
+
+  const imageResponsePromise = page.waitForResponse(
+    /corporate_holiday_card.jpg/,
+  );
+  await page.locator('[href="/image-optimization"]').click();
+  const imageResponse = await imageResponsePromise;
+
+  await page.waitForURL("/image-optimization");
+
+  const imageContentType = imageResponse.headers()["content-type"];
+  expect(imageContentType).toBe("image/webp");
+
+  let el = page.locator("img");
+  await expect(el).toHaveJSProperty("complete", true);
+  await expect(el).not.toHaveJSProperty("naturalWidth", 0);
+});
diff --git a/packages/tests-e2e/tests/appRouter/image-optimization.test.ts b/packages/tests-e2e/tests/appRouter/image-optimization.test.ts
new file mode 100644
index 000000000..66eb64ad0
--- /dev/null
+++ b/packages/tests-e2e/tests/appRouter/image-optimization.test.ts
@@ -0,0 +1,20 @@
+import { expect, test } from "@playwright/test";
+
+test("Image Optimization", async ({ page }) => {
+  await page.goto("/");
+
+  const imageResponsePromise = page.waitForResponse(
+    /https%3A%2F%2Fopen-next.js.org%2Farchitecture.png/,
+  );
+  await page.locator('[href="/image-optimization"]').click();
+  const imageResponse = await imageResponsePromise;
+
+  await page.waitForURL("/image-optimization");
+
+  const imageContentType = imageResponse.headers()["content-type"];
+  expect(imageContentType).toBe("image/webp");
+
+  let el = page.locator("img");
+  await expect(el).toHaveJSProperty("complete", true);
+  await expect(el).not.toHaveJSProperty("naturalWidth", 0);
+});
diff --git a/packages/tests-unit/CHANGELOG.md b/packages/tests-unit/CHANGELOG.md
index 090bd5dd5..c95583030 100644
--- a/packages/tests-unit/CHANGELOG.md
+++ b/packages/tests-unit/CHANGELOG.md
@@ -4,6 +4,21 @@
 
 ### Patch Changes
 
+- Updated dependencies [3235392]
+- Updated dependencies [af2d3ce]
+  - open-next@2.3.7
+
+## null
+
+### Patch Changes
+
+- Updated dependencies [f9b90b6]
+  - open-next@2.3.6
+
+## null
+
+### Patch Changes
+
 - Updated dependencies [b9eefca]
 - Updated dependencies [c80f1be]
 - Updated dependencies [186e28f]

From 5dcb4c2f8b2b5629e650aad74faf73325627387a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 10 Mar 2024 14:54:47 +0100
Subject: [PATCH 067/102] fix for next 12

---
 packages/open-next/src/adapters/cache.ts      | 45 +++++++++----------
 .../open-next/src/build/copyTracedFiles.ts    |  2 +-
 .../open-next/src/build/createServerBundle.ts |  7 +--
 packages/open-next/src/core/util.ts           |  6 +--
 .../open-next/src/http/openNextResponse.ts    |  6 +++
 5 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts
index e549aa2b5..5e6f4582e 100644
--- a/packages/open-next/src/adapters/cache.ts
+++ b/packages/open-next/src/adapters/cache.ts
@@ -95,15 +95,9 @@ declare global {
   var disableIncrementalCache: boolean;
   var lastModified: Record<string, number>;
 }
-
+// We need to use globalThis client here as this class can be defined at load time in next 12 but client is not available at load time
 export default class S3Cache {
-  private client: IncrementalCache;
-  private tagClient: TagCache;
-
-  constructor(_ctx: CacheHandlerContext) {
-    this.client = globalThis.incrementalCache;
-    this.tagClient = globalThis.tagCache;
-  }
+  constructor(_ctx: CacheHandlerContext) {}
 
   public async get(
     key: string,
@@ -129,9 +123,12 @@ export default class S3Cache {
   async getFetchCache(key: string) {
     debug("get fetch cache", { key });
     try {
-      const { value, lastModified } = await this.client.get(key, true);
+      const { value, lastModified } = await globalThis.incrementalCache.get(
+        key,
+        true,
+      );
       // const { Body, LastModified } = await this.getS3Object(key, "fetch");
-      const _lastModified = await this.tagClient.getLastModified(
+      const _lastModified = await globalThis.tagCache.getLastModified(
         key,
         lastModified,
       );
@@ -154,16 +151,14 @@ export default class S3Cache {
 
   async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> {
     try {
-      const { value: cacheData, lastModified } = await this.client.get(
-        key,
-        false,
-      );
+      const { value: cacheData, lastModified } =
+        await globalThis.incrementalCache.get(key, false);
       // const { Body, LastModified } = await this.getS3Object(key, "cache");
       // const cacheData = JSON.parse(
       //   (await Body?.transformToString()) ?? "{}",
       // ) as S3CachedFile;
       const meta = cacheData?.meta;
-      const _lastModified = await this.tagClient.getLastModified(
+      const _lastModified = await globalThis.tagCache.getLastModified(
         key,
         lastModified,
       );
@@ -228,7 +223,7 @@ export default class S3Cache {
     }
     if (data?.kind === "ROUTE") {
       const { body, status, headers } = data;
-      await this.client.set(
+      await globalThis.incrementalCache.set(
         key,
         {
           type: "route",
@@ -248,7 +243,7 @@ export default class S3Cache {
       const { html, pageData } = data;
       const isAppPath = typeof pageData === "string";
       if (isAppPath) {
-        this.client.set(
+        globalThis.incrementalCache.set(
           key,
           {
             type: "app",
@@ -258,7 +253,7 @@ export default class S3Cache {
           false,
         );
       } else {
-        this.client.set(
+        globalThis.incrementalCache.set(
           key,
           {
             type: "page",
@@ -269,9 +264,9 @@ export default class S3Cache {
         );
       }
     } else if (data?.kind === "FETCH") {
-      await this.client.set<true>(key, data, true);
+      await globalThis.incrementalCache.set<true>(key, data, true);
     } else if (data?.kind === "REDIRECT") {
-      await this.client.set(
+      await globalThis.incrementalCache.set(
         key,
         {
           type: "redirect",
@@ -280,7 +275,7 @@ export default class S3Cache {
         false,
       );
     } else if (data === null || data === undefined) {
-      await this.client.delete(key);
+      await globalThis.incrementalCache.delete(key);
     }
     // Write derivedTags to dynamodb
     // If we use an in house version of getDerivedTags in build we should use it here instead of next's one
@@ -293,10 +288,10 @@ export default class S3Cache {
     debug("derivedTags", derivedTags);
     // Get all tags stored in dynamodb for the given key
     // If any of the derived tags are not stored in dynamodb for the given key, write them
-    const storedTags = await this.tagClient.getByPath(key);
+    const storedTags = await globalThis.tagCache.getByPath(key);
     const tagsToWrite = derivedTags.filter((tag) => !storedTags.includes(tag));
     if (tagsToWrite.length > 0) {
-      await this.tagClient.writeTags(
+      await globalThis.tagCache.writeTags(
         tagsToWrite.map((tag) => ({
           path: key,
           tag: tag,
@@ -311,10 +306,10 @@ export default class S3Cache {
     }
     debug("revalidateTag", tag);
     // Find all keys with the given tag
-    const paths = await this.tagClient.getByTag(tag);
+    const paths = await globalThis.tagCache.getByTag(tag);
     debug("Items", paths);
     // Update all keys with the given tag with revalidatedAt set to now
-    await this.tagClient.writeTags(
+    await globalThis.tagCache.writeTags(
       paths?.map((path) => ({
         path: path,
         tag: tag,
diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index bfbfa6ec1..979285046 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -120,7 +120,7 @@ export async function copyTracedFiles(
       from.includes("node_modules") &&
       //TODO: we need to figure which packages we could safely remove
       (from.includes("caniuse-lite") ||
-        from.includes("jest-worker") ||
+        // from.includes("jest-worker") || This ones seems necessary for next 12
         from.includes("sharp"))
     ) {
       return;
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 5f84d935c..eba4bbea5 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -132,14 +132,9 @@ async function generateBundle(
   const isMonorepo = monorepoRoot !== appPath;
   const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
 
-  // Copy cache file
-  // It needs to be inside ".next"
-  fs.mkdirSync(path.join(outputPath, packagePath, ".next"), {
-    recursive: true,
-  });
   fs.copyFileSync(
     path.join(outputDir, ".build", "cache.cjs"),
-    path.join(outputPath, packagePath, ".next", "cache.cjs"),
+    path.join(outputPath, packagePath, "cache.cjs"),
   );
 
   // Bundle next server if necessary
diff --git a/packages/open-next/src/core/util.ts b/packages/open-next/src/core/util.ts
index d494d428b..e97822102 100644
--- a/packages/open-next/src/core/util.ts
+++ b/packages/open-next/src/core/util.ts
@@ -27,7 +27,7 @@ import {
 overrideNextjsRequireHooks(NextConfig);
 applyNextjsRequireHooksOverride();
 //#endOverride
-
+const cacheHandlerPath = require.resolve("./cache.cjs");
 // @ts-ignore
 export const requestHandler = new NextServer.default({
   //#override requestHandlerHost
@@ -42,7 +42,7 @@ export const requestHandler = new NextServer.default({
     // By default, Next.js uses local disk to store ISR cache. We will use
     // our own cache handler to store the cache on S3.
     //#override stableIncrementalCache
-    cacheHandler: `./cache.cjs`,
+    cacheHandler: cacheHandlerPath,
     cacheMaxMemorySize: 0, // We need to disable memory cache
     //#endOverride
     experimental: {
@@ -53,7 +53,7 @@ export const requestHandler = new NextServer.default({
       trustHostHeader: true,
       //#endOverride
       //#override experimentalIncrementalCacheHandler
-      incrementalCacheHandlerPath: `./cache.cjs`,
+      incrementalCacheHandlerPath: cacheHandlerPath,
       //#endOverride
     },
   },
diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts
index ff8610493..503cd6c83 100644
--- a/packages/open-next/src/http/openNextResponse.ts
+++ b/packages/open-next/src/http/openNextResponse.ts
@@ -47,6 +47,12 @@ export class OpenNextNodeResponse extends Transform {
     });
   }
 
+  // Necessary for next 12
+  // We might have to implement all the methods here
+  get originalResponse() {
+    return this;
+  }
+
   get finished() {
     return this.writableFinished && this.responseStream?.writableFinished;
   }

From 8be46473b07cb4fb86c861806917041d4191475c Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 10 Mar 2024 15:56:31 +0100
Subject: [PATCH 068/102] add support for basePath

---
 packages/open-next/src/build.ts               |  2 +-
 .../open-next/src/build/generateOutput.ts     | 38 +++++++++++++++----
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 7b19e6a10..e1ce7da9d 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -83,7 +83,7 @@ export async function build(openNextConfigPath?: string) {
   await createRevalidationBundle();
   await createImageOptimizationBundle();
   await createWarmerBundle();
-  await generateOutput(options.appBuildOutputPath, config);
+  await generateOutput(options.appPath, options.appBuildOutputPath, config);
   logger.info("OpenNext build complete.");
 }
 
diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts
index 345be7864..7247c7f96 100644
--- a/packages/open-next/src/build/generateOutput.ts
+++ b/packages/open-next/src/build/generateOutput.ts
@@ -1,6 +1,7 @@
 import * as fs from "node:fs";
 import path from "node:path";
 
+import { NextConfig } from "types/next-types.js";
 import {
   BaseOverride,
   DefaultOverrideOptions,
@@ -151,7 +152,17 @@ async function extractCommonOverride(override?: OverrideOptions) {
   return { queue, incrementalCache, tagCache };
 }
 
+function prefixPattern(basePath: string) {
+  // Prefix CloudFront distribution behavior path patterns with `basePath` if configured
+  return (pattern: string) => {
+    return basePath && basePath.length > 0
+      ? `${basePath.slice(1)}/${pattern}`
+      : pattern;
+  };
+}
+
 export async function generateOutput(
+  appPath: string,
   outputPath: string,
   config: OpenNextConfig,
 ) {
@@ -181,6 +192,15 @@ export async function generateOutput(
 
   const defaultOriginCanstream = await canStream(config.default);
 
+  //Load required-server-files.json
+  const requiredServerFiles = JSON.parse(
+    fs.readFileSync(
+      path.join(appPath, ".next", "required-server-files.json"),
+      "utf-8",
+    ),
+  ).config as NextConfig;
+  const prefixer = prefixPattern(requiredServerFiles.basePath ?? "");
+
   // First add s3 origins and image optimization
 
   const defaultOrigins: DefaultOrigins = {
@@ -190,9 +210,11 @@ export async function generateOutput(
       copy: [
         {
           from: ".open-next/assets",
-          to: "_assets",
+          to: requiredServerFiles.basePath
+            ? `_assets${requiredServerFiles.basePath}`
+            : "_assets",
           cached: true,
-          versionedSubDir: "_next",
+          versionedSubDir: prefixer("_next"),
         },
         ...(config.dangerous?.disableIncrementalCache
           ? []
@@ -266,7 +288,7 @@ export async function generateOutput(
 
   // Then we need to compute the behaviors
   const behaviors: OpenNextOutput["behaviors"] = [
-    { pattern: "_next/image*", origin: "imageOptimizer" },
+    { pattern: prefixer("_next/image*"), origin: "imageOptimizer" },
   ];
 
   // Then we add the routes
@@ -274,7 +296,7 @@ export async function generateOutput(
     const patterns = "patterns" in value ? value.patterns : ["*"];
     patterns.forEach((pattern) => {
       behaviors.push({
-        pattern: pattern.replace(/BUILD_ID/, getBuildId(outputPath)),
+        pattern: prefixer(pattern.replace(/BUILD_ID/, getBuildId(outputPath))),
         origin: value.placement === "global" ? undefined : key,
         edgeFunction:
           value.placement === "global"
@@ -288,12 +310,12 @@ export async function generateOutput(
 
   // We finish with the default behavior so that they don't override the others
   behaviors.push({
-    pattern: "_next/data/*",
+    pattern: prefixer("_next/data/*"),
     origin: "default",
     edgeFunction: isExternalMiddleware ? "middleware" : undefined,
   });
   behaviors.push({
-    pattern: "*",
+    pattern: "*", // This is the default behavior
     origin: "default",
     edgeFunction: isExternalMiddleware ? "middleware" : undefined,
   });
@@ -303,12 +325,12 @@ export async function generateOutput(
   fs.readdirSync(assetPath).forEach((item) => {
     if (fs.statSync(path.join(assetPath, item)).isDirectory()) {
       behaviors.push({
-        pattern: `${item}/*`,
+        pattern: prefixer(`${item}/*`),
         origin: "s3",
       });
     } else {
       behaviors.push({
-        pattern: item,
+        pattern: prefixer(item),
         origin: "s3",
       });
     }

From be93d3b750cd5b95df0894b00b5004c49fcb37e0 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Mon, 11 Mar 2024 11:41:38 +0100
Subject: [PATCH 069/102] allow customization of sharp runtime

---
 packages/open-next/src/build.ts           | 10 +++++++---
 packages/open-next/src/types/open-next.ts |  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index e1ce7da9d..db7cc6deb 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -81,7 +81,7 @@ export async function build(openNextConfigPath?: string) {
 
   await createServerBundle(config, options);
   await createRevalidationBundle();
-  await createImageOptimizationBundle();
+  await createImageOptimizationBundle(config);
   await createWarmerBundle();
   await generateOutput(options.appPath, options.appBuildOutputPath, config);
   logger.info("OpenNext build complete.");
@@ -319,7 +319,7 @@ async function createRevalidationBundle() {
   );
 }
 
-async function createImageOptimizationBundle() {
+async function createImageOptimizationBundle(config: OpenNextConfig) {
   logger.info(`Bundling image optimization function...`);
 
   const { appPath, appBuildOutputPath, outputDir } = options;
@@ -398,10 +398,14 @@ async function createImageOptimizationBundle() {
   const nodeOutputPath = path.resolve(outputPath);
   const sharpVersion = process.env.SHARP_VERSION ?? "0.32.6";
 
+  const arch = config.imageOptimization?.arch ?? "arm64";
+  const nodeVersion = config.imageOptimization?.nodeVersion ?? "18";
+
   //check if we are running in Windows environment then set env variables accordingly.
   try {
     cp.execSync(
-      `npm install --arch=arm64 --platform=linux --target=18 --libc=glibc --prefix="${nodeOutputPath}" sharp@${sharpVersion}`,
+      // We might want to change the arch args to cpu args, it seems to be the documented way
+      `npm install --arch=${arch} --platform=linux --target=${nodeVersion} --libc=glibc --prefix="${nodeOutputPath}" sharp@${sharpVersion}`,
       {
         stdio: "pipe",
         cwd: appPath,
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index ea37285d1..323d534f7 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -273,6 +273,8 @@ export interface OpenNextConfig {
    */
   imageOptimization?: DefaultFunctionOptions & {
     loader?: "s3" | LazyLoadedOverride<ImageLoader>;
+    arch: "x64" | "arm64";
+    nodeVersion: "18" | "20";
   };
 
   /**

From 63dc3b2b599881b23d67fe78574765ec36c331f8 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Mon, 11 Mar 2024 15:49:00 +0100
Subject: [PATCH 070/102] updated edge converter to match behaviour of lambda

---
 packages/open-next/src/converters/edge.ts | 21 +++++++++++++++------
 packages/open-next/src/plugins/edge.ts    |  1 -
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/packages/open-next/src/converters/edge.ts b/packages/open-next/src/converters/edge.ts
index 4d6305068..32d5923ee 100644
--- a/packages/open-next/src/converters/edge.ts
+++ b/packages/open-next/src/converters/edge.ts
@@ -48,15 +48,24 @@ const converter: Converter<
   },
   convertTo: async (result) => {
     if ("internalEvent" in result) {
-      const url = result.isExternalRewrite
-        ? result.internalEvent.url
-        : `https://${result.origin ?? result.internalEvent.headers.host}${
-            result.internalEvent.url
-          }`;
+      let url = result.internalEvent.url;
+      if (!result.isExternalRewrite) {
+        if (result.origin) {
+          url = `${result.origin.protocol}://${result.origin.host}${
+            result.origin.port ? `:${result.origin.port}` : ""
+          }${url}`;
+        } else {
+          url = `https://${result.internalEvent.headers.host}${url}`;
+        }
+      }
+
       const req = new Request(url, {
         body: result.internalEvent.body,
         method: result.internalEvent.method,
-        headers: result.internalEvent.headers,
+        headers: {
+          ...result.internalEvent.headers,
+          "x-forwarded-host": result.internalEvent.headers.host,
+        },
       });
 
       return fetch(req);
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index 97bf4a97e..23cc1e89f 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -127,7 +127,6 @@ ${contents}
       });
 
       build.onLoad({ filter: /adapters\/config\/index/g }, async () => {
-        console.log("opennext-config-plugin");
         const NextConfig = loadConfig(nextDir);
         const BuildId = loadBuildId(nextDir);
         const HtmlPages = loadHtmlPages(nextDir);

From 3a489ad394cf1042c237c669157b6cdb837bf3d3 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Mon, 11 Mar 2024 15:52:52 +0100
Subject: [PATCH 071/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 28f72c2c1..8ef7a4baa 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.6",
+  "version": "3.0.0-rc.7",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 356a57374ccf08d587fd66385c5a66094fc54839 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 12 Mar 2024 12:32:02 +0100
Subject: [PATCH 072/102] fix monorepo

---
 packages/open-next/src/build/createServerBundle.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index eba4bbea5..1f6e9d8a3 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -122,7 +122,6 @@ async function generateBundle(
 
   // Create output folder
   const outputPath = path.join(outputDir, "server-functions", name);
-  fs.mkdirSync(outputPath, { recursive: true });
 
   // Resolve path to the Next.js app if inside the monorepo
   // note: if user's app is inside a monorepo, standalone mode places
@@ -131,6 +130,7 @@ async function generateBundle(
   //       We need to output the handler file inside the package path.
   const isMonorepo = monorepoRoot !== appPath;
   const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
+  fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });
 
   fs.copyFileSync(
     path.join(outputDir, ".build", "cache.cjs"),

From 293243633f74c3fab38c2b6ff7fab44f1d06095b Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 12 Mar 2024 23:10:31 +0100
Subject: [PATCH 073/102] improved streaming

https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/94#issuecomment-1992245429
---
 examples/app-router/app/ssr/layout.tsx          |  4 +---
 packages/open-next/src/http/openNextResponse.ts | 17 +++++++++++++++--
 .../src/wrappers/aws-lambda-streaming.ts        |  1 +
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/examples/app-router/app/ssr/layout.tsx b/examples/app-router/app/ssr/layout.tsx
index d51f11bc1..5ab0b64a7 100644
--- a/examples/app-router/app/ssr/layout.tsx
+++ b/examples/app-router/app/ssr/layout.tsx
@@ -1,13 +1,11 @@
 import { PropsWithChildren } from "react";
 
-import Filler from "@example/shared/components/Filler";
-
 export default function Layout({ children }: PropsWithChildren) {
   return (
     <div>
       <h1>SSR</h1>
       {/* 16 kb seems necessary here to prevent any buffering*/}
-      <Filler size={16} />
+      {/* <Filler size={16} /> */}
       {children}
     </div>
   );
diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts
index 503cd6c83..ba5648ef4 100644
--- a/packages/open-next/src/http/openNextResponse.ts
+++ b/packages/open-next/src/http/openNextResponse.ts
@@ -14,6 +14,7 @@ export interface StreamCreator {
   // Just to fix an issue with aws lambda streaming with empty body
   onWrite?: () => void;
   onFinish: () => void;
+  waitForFirstWrite?: boolean;
 }
 
 // We only need to implement the methods that are used by next.js
@@ -23,6 +24,7 @@ export class OpenNextNodeResponse extends Transform {
   headers: OutgoingHttpHeaders = {};
   private _cookies: string[] = [];
   private responseStream?: Writable;
+  private hasDoneFirstWrite: boolean = false;
   headersSent: boolean = false;
   _chunks: Buffer[] = [];
 
@@ -144,7 +146,18 @@ export class OpenNextNodeResponse extends Transform {
     if (!this.headersSent) {
       this.flushHeaders();
     }
-    this._internalWrite(chunk, encoding);
-    callback();
+    if (this.streamCreator?.waitForFirstWrite && !this.hasDoneFirstWrite) {
+      const waitTime = parseInt(
+        process.env.STREAMING_INITIAL_WRITE_WAIT_TIME ?? "25",
+      );
+      new Promise((resolve) => setTimeout(resolve, waitTime)).then(() => {
+        this._internalWrite(chunk, encoding);
+        this.hasDoneFirstWrite = true;
+        callback();
+      });
+    } else {
+      this._internalWrite(chunk, encoding);
+      callback();
+    }
   }
 }
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 3f48a671c..8e3a2fdd3 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -119,6 +119,7 @@ const handler: WrapperHandler = async (handler, converter) =>
             compressedStream?.end(new Uint8Array(8));
           }
         },
+        waitForFirstWrite: true,
       };
 
       const response = await handler(internalEvent, streamCreator);

From f1b2b78ce622ceae496ee566abf74f07018619f4 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Tue, 12 Mar 2024 23:11:05 +0100
Subject: [PATCH 074/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 8ef7a4baa..752df776a 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.7",
+  "version": "3.0.0-rc.8",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 2ab17176d903800a65ba2390454abd6640abfebb Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 15 Mar 2024 13:36:12 +0100
Subject: [PATCH 075/102] fix open-next config build that depends on node

---
 packages/open-next/src/build.ts                       | 2 ++
 packages/open-next/src/build/createServerBundle.ts    | 1 +
 packages/open-next/src/build/edge/createEdgeBundle.ts | 2 ++
 3 files changed, 5 insertions(+)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index db7cc6deb..5b820e1f9 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -133,6 +133,8 @@ function compileOpenNextConfig(tempDir: string, openNextConfigPath?: string) {
       bundle: true,
       format: "esm",
       target: ["node18"],
+      external: ["node:*"],
+      platform: "neutral",
     });
   }
 
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 1f6e9d8a3..c96e1ace8 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -119,6 +119,7 @@ async function generateBundle(
   fnOptions: SplittedFunctionOptions,
 ) {
   const { appPath, appBuildOutputPath, outputDir, monorepoRoot } = options;
+  logger.info(`Building server function: ${name}...`);
 
   // Create output folder
   const outputPath = path.join(outputDir, "server-functions", name);
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index 48f9f4f3b..367e1de0e 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -10,6 +10,7 @@ import {
   SplittedFunctionOptions,
 } from "types/open-next";
 
+import logger from "../../logger.js";
 import { openNextEdgePlugins } from "../../plugins/edge.js";
 import { openNextResolvePlugin } from "../../plugins/resolve.js";
 import { BuildOptions, copyOpenNextConfig, esbuildAsync } from "../helper.js";
@@ -105,6 +106,7 @@ export async function generateEdgeBundle(
   fnOptions: SplittedFunctionOptions,
 ) {
   const { appBuildOutputPath, outputDir } = options;
+  logger.info(`Generating edge bundle for: ${name}`);
 
   // Create output folder
   const outputPath = path.join(outputDir, "server-functions", name);

From a2e5491800b266744d25c2640a08c078ac0d5834 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 23 Mar 2024 10:20:23 +0100
Subject: [PATCH 076/102] fix crypto middleware node 20

---
 packages/open-next/src/plugins/edge.ts | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index 23cc1e89f..7243028f6 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -101,7 +101,9 @@ globalThis._ROUTES = ${JSON.stringify(routes)};
 import {Buffer} from "node:buffer";
 globalThis.Buffer = Buffer;
 import crypto from "node:crypto";
-globalThis.crypto = crypto;
+if(!globalThis.crypto){
+  globalThis.crypto = crypto;
+}
 
 import {AsyncLocalStorage} from "node:async_hooks";
 globalThis.AsyncLocalStorage = AsyncLocalStorage;

From 51a26391cf72a2720b5ae5a5eac21b7ee7da8e10 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 23 Mar 2024 13:33:22 +0100
Subject: [PATCH 077/102] Sync

---
 docs/package-lock.json                        | 5150 +++++++++++++++++
 docs/pages/common_issues/_meta.json           |    3 +
 docs/pages/common_issues/isr.mdx              |    8 +-
 docs/pages/inner_workings/_meta.json          |    3 +
 packages/open-next/CHANGELOG.md               |    6 +
 .../src/converters/aws-cloudfront.ts          |   11 +
 packages/tests-unit/CHANGELOG.md              |    7 +
 .../tests-unit/tests/event-mapper.test.ts     |  115 +
 8 files changed, 5299 insertions(+), 4 deletions(-)
 create mode 100644 docs/package-lock.json
 create mode 100644 docs/pages/common_issues/_meta.json
 create mode 100644 docs/pages/inner_workings/_meta.json
 create mode 100644 packages/tests-unit/tests/event-mapper.test.ts

diff --git a/docs/package-lock.json b/docs/package-lock.json
new file mode 100644
index 000000000..12072d9c0
--- /dev/null
+++ b/docs/package-lock.json
@@ -0,0 +1,5150 @@
+{
+  "name": "open-next-docs",
+  "version": "0.1.0",
+  "lockfileVersion": 3,
+  "requires": true,
+  "packages": {
+    "": {
+      "name": "open-next-docs",
+      "version": "0.1.0",
+      "dependencies": {
+        "@types/node": "18.11.9",
+        "@types/react": "18.0.25",
+        "@types/react-dom": "18.0.9",
+        "next": "13.4.12",
+        "nextra": "^2.13.1",
+        "nextra-theme-docs": "^2.13.1",
+        "react": "18.2.0",
+        "react-dom": "18.2.0",
+        "typescript": "4.9.3"
+      }
+    },
+    "node_modules/@babel/runtime": {
+      "version": "7.24.0",
+      "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.24.0.tgz",
+      "integrity": "sha512-Chk32uHMg6TnQdvw2e9IlqPpFX/6NLuK0Ys2PqLb7/gL5uFn9mXvK715FGLlOLQrcO4qIkNHkvPGktzzXexsFw==",
+      "dependencies": {
+        "regenerator-runtime": "^0.14.0"
+      },
+      "engines": {
+        "node": ">=6.9.0"
+      }
+    },
+    "node_modules/@braintree/sanitize-url": {
+      "version": "6.0.4",
+      "resolved": "https://registry.npmjs.org/@braintree/sanitize-url/-/sanitize-url-6.0.4.tgz",
+      "integrity": "sha512-s3jaWicZd0pkP0jf5ysyHUI/RE7MHos6qlToFcGWXVp+ykHOy77OUMrfbgJ9it2C5bow7OIQwYYaHjk9XlBQ2A=="
+    },
+    "node_modules/@headlessui/react": {
+      "version": "1.7.18",
+      "resolved": "https://registry.npmjs.org/@headlessui/react/-/react-1.7.18.tgz",
+      "integrity": "sha512-4i5DOrzwN4qSgNsL4Si61VMkUcWbcSKueUV7sFhpHzQcSShdlHENE5+QBntMSRvHt8NyoFO2AGG8si9lq+w4zQ==",
+      "dependencies": {
+        "@tanstack/react-virtual": "^3.0.0-beta.60",
+        "client-only": "^0.0.1"
+      },
+      "engines": {
+        "node": ">=10"
+      },
+      "peerDependencies": {
+        "react": "^16 || ^17 || ^18",
+        "react-dom": "^16 || ^17 || ^18"
+      }
+    },
+    "node_modules/@mdx-js/mdx": {
+      "version": "2.3.0",
+      "resolved": "https://registry.npmjs.org/@mdx-js/mdx/-/mdx-2.3.0.tgz",
+      "integrity": "sha512-jLuwRlz8DQfQNiUCJR50Y09CGPq3fLtmtUQfVrj79E0JWu3dvsVcxVIcfhR5h0iXu+/z++zDrYeiJqifRynJkA==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "@types/mdx": "^2.0.0",
+        "estree-util-build-jsx": "^2.0.0",
+        "estree-util-is-identifier-name": "^2.0.0",
+        "estree-util-to-js": "^1.1.0",
+        "estree-walker": "^3.0.0",
+        "hast-util-to-estree": "^2.0.0",
+        "markdown-extensions": "^1.0.0",
+        "periscopic": "^3.0.0",
+        "remark-mdx": "^2.0.0",
+        "remark-parse": "^10.0.0",
+        "remark-rehype": "^10.0.0",
+        "unified": "^10.0.0",
+        "unist-util-position-from-estree": "^1.0.0",
+        "unist-util-stringify-position": "^3.0.0",
+        "unist-util-visit": "^4.0.0",
+        "vfile": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/@mdx-js/mdx/node_modules/unist-util-visit": {
+      "version": "4.1.2",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-4.1.2.tgz",
+      "integrity": "sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^5.1.1"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/@mdx-js/mdx/node_modules/unist-util-visit-parents": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz",
+      "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/@mdx-js/react": {
+      "version": "2.3.0",
+      "resolved": "https://registry.npmjs.org/@mdx-js/react/-/react-2.3.0.tgz",
+      "integrity": "sha512-zQH//gdOmuu7nt2oJR29vFhDv88oGPmVw6BggmrHeMI+xgEkp1B2dX9/bMBSYtK0dyLX/aOmesKS09g222K1/g==",
+      "dependencies": {
+        "@types/mdx": "^2.0.0",
+        "@types/react": ">=16"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      },
+      "peerDependencies": {
+        "react": ">=16"
+      }
+    },
+    "node_modules/@napi-rs/simple-git": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git/-/simple-git-0.1.16.tgz",
+      "integrity": "sha512-C5wRPw9waqL2jk3jEDeJv+f7ScuO3N0a39HVdyFLkwKxHH4Sya4ZbzZsu2JLi6eEqe7RuHipHL6mC7B2OfYZZw==",
+      "engines": {
+        "node": ">= 10"
+      },
+      "optionalDependencies": {
+        "@napi-rs/simple-git-android-arm-eabi": "0.1.16",
+        "@napi-rs/simple-git-android-arm64": "0.1.16",
+        "@napi-rs/simple-git-darwin-arm64": "0.1.16",
+        "@napi-rs/simple-git-darwin-x64": "0.1.16",
+        "@napi-rs/simple-git-linux-arm-gnueabihf": "0.1.16",
+        "@napi-rs/simple-git-linux-arm64-gnu": "0.1.16",
+        "@napi-rs/simple-git-linux-arm64-musl": "0.1.16",
+        "@napi-rs/simple-git-linux-x64-gnu": "0.1.16",
+        "@napi-rs/simple-git-linux-x64-musl": "0.1.16",
+        "@napi-rs/simple-git-win32-arm64-msvc": "0.1.16",
+        "@napi-rs/simple-git-win32-x64-msvc": "0.1.16"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-android-arm-eabi": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-android-arm-eabi/-/simple-git-android-arm-eabi-0.1.16.tgz",
+      "integrity": "sha512-dbrCL0Pl5KZG7x7tXdtVsA5CO6At5ohDX3myf5xIYn9kN4jDFxsocl8bNt6Vb/hZQoJd8fI+k5VlJt+rFhbdVw==",
+      "cpu": [
+        "arm"
+      ],
+      "optional": true,
+      "os": [
+        "android"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-android-arm64": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-android-arm64/-/simple-git-android-arm64-0.1.16.tgz",
+      "integrity": "sha512-xYz+TW5J09iK8SuTAKK2D5MMIsBUXVSs8nYp7HcMi8q6FCRO7yJj96YfP9PvKsc/k64hOyqGmL5DhCzY9Cu1FQ==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "android"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-darwin-arm64": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-darwin-arm64/-/simple-git-darwin-arm64-0.1.16.tgz",
+      "integrity": "sha512-XfgsYqxhUE022MJobeiX563TJqyQyX4FmYCnqrtJwAfivESVeAJiH6bQIum8dDEYMHXCsG7nL8Ok0Dp8k2m42g==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "darwin"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-darwin-x64": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-darwin-x64/-/simple-git-darwin-x64-0.1.16.tgz",
+      "integrity": "sha512-tkEVBhD6vgRCbeWsaAQqM3bTfpIVGeitamPPRVSbsq8qgzJ5Dx6ZedH27R7KSsA/uao7mZ3dsrNLXbu1Wy5MzA==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "darwin"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-linux-arm-gnueabihf": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-linux-arm-gnueabihf/-/simple-git-linux-arm-gnueabihf-0.1.16.tgz",
+      "integrity": "sha512-R6VAyNnp/yRaT7DV1Ao3r67SqTWDa+fNq2LrNy0Z8gXk2wB9ZKlrxFtLPE1WSpWknWtyRDLpRlsorh7Evk7+7w==",
+      "cpu": [
+        "arm"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-linux-arm64-gnu": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-linux-arm64-gnu/-/simple-git-linux-arm64-gnu-0.1.16.tgz",
+      "integrity": "sha512-LAGI0opFKw/HBMCV2qIBK3uWSEW9h4xd2ireZKLJy8DBPymX6NrWIamuxYNyCuACnFdPRxR4LaRFy4J5ZwuMdw==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-linux-arm64-musl": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-linux-arm64-musl/-/simple-git-linux-arm64-musl-0.1.16.tgz",
+      "integrity": "sha512-I57Ph0F0Yn2KW93ep+V1EzKhACqX0x49vvSiapqIsdDA2PifdEWLc1LJarBolmK7NKoPqKmf6lAKKO9lhiZzkg==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-linux-x64-gnu": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-linux-x64-gnu/-/simple-git-linux-x64-gnu-0.1.16.tgz",
+      "integrity": "sha512-AZYYFY2V7hlcQASPEOWyOa3e1skzTct9QPzz0LiDM3f/hCFY/wBaU2M6NC5iG3d2Kr38heuyFS/+JqxLm5WaKA==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-linux-x64-musl": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-linux-x64-musl/-/simple-git-linux-x64-musl-0.1.16.tgz",
+      "integrity": "sha512-9TyMcYSBJwjT8jwjY9m24BZbu7ozyWTjsmYBYNtK3B0Um1Ov6jthSNneLVvouQ6x+k3Ow+00TiFh6bvmT00r8g==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-win32-arm64-msvc": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-win32-arm64-msvc/-/simple-git-win32-arm64-msvc-0.1.16.tgz",
+      "integrity": "sha512-uslJ1WuAHCYJWui6xjsyT47SjX6KOHDtClmNO8hqKz1pmDSNY7AjyUY8HxvD1lK9bDnWwc4JYhikS9cxCqHybw==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "win32"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@napi-rs/simple-git-win32-x64-msvc": {
+      "version": "0.1.16",
+      "resolved": "https://registry.npmjs.org/@napi-rs/simple-git-win32-x64-msvc/-/simple-git-win32-x64-msvc-0.1.16.tgz",
+      "integrity": "sha512-SoEaVeCZCDF1MP+M9bMSXsZWgEjk4On9GWADO5JOulvzR1bKjk0s9PMHwe/YztR9F0sJzrCxwtvBZowhSJsQPg==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "win32"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/env": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/env/-/env-13.4.12.tgz",
+      "integrity": "sha512-RmHanbV21saP/6OEPBJ7yJMuys68cIf8OBBWd7+uj40LdpmswVAwe1uzeuFyUsd6SfeITWT3XnQfn6wULeKwDQ=="
+    },
+    "node_modules/@next/swc-darwin-arm64": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.12.tgz",
+      "integrity": "sha512-deUrbCXTMZ6ZhbOoloqecnUeNpUOupi8SE2tx4jPfNS9uyUR9zK4iXBvH65opVcA/9F5I/p8vDXSYbUlbmBjZg==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "darwin"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-darwin-x64": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.12.tgz",
+      "integrity": "sha512-WRvH7RxgRHlC1yb5oG0ZLx8F7uci9AivM5/HGGv9ZyG2Als8Ij64GC3d+mQ5sJhWjusyU6T6V1WKTUoTmOB0zQ==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "darwin"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-linux-arm64-gnu": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.12.tgz",
+      "integrity": "sha512-YEKracAWuxp54tKiAvvq73PUs9lok57cc8meYRibTWe/VdPB2vLgkTVWFcw31YDuRXdEhdX0fWS6Q+ESBhnEig==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-linux-arm64-musl": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.12.tgz",
+      "integrity": "sha512-LhJR7/RAjdHJ2Isl2pgc/JaoxNk0KtBgkVpiDJPVExVWA1c6gzY57+3zWuxuyWzTG+fhLZo2Y80pLXgIJv7g3g==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-linux-x64-gnu": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.12.tgz",
+      "integrity": "sha512-1DWLL/B9nBNiQRng+1aqs3OaZcxC16Nf+mOnpcrZZSdyKHek3WQh6j/fkbukObgNGwmCoVevLUa/p3UFTTqgqg==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-linux-x64-musl": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.12.tgz",
+      "integrity": "sha512-kEAJmgYFhp0VL+eRWmUkVxLVunn7oL9Mdue/FS8yzRBVj7Z0AnIrHpTIeIUl1bbdQq1VaoOztnKicAjfkLTRCQ==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "linux"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-win32-arm64-msvc": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.12.tgz",
+      "integrity": "sha512-GMLuL/loR6yIIRTnPRY6UGbLL9MBdw2anxkOnANxvLvsml4F0HNIgvnU3Ej4BjbqMTNjD4hcPFdlEow4XHPdZA==",
+      "cpu": [
+        "arm64"
+      ],
+      "optional": true,
+      "os": [
+        "win32"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-win32-ia32-msvc": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.12.tgz",
+      "integrity": "sha512-PhgNqN2Vnkm7XaMdRmmX0ZSwZXQAtamBVSa9A/V1dfKQCV1rjIZeiy/dbBnVYGdj63ANfsOR/30XpxP71W0eww==",
+      "cpu": [
+        "ia32"
+      ],
+      "optional": true,
+      "os": [
+        "win32"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@next/swc-win32-x64-msvc": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.12.tgz",
+      "integrity": "sha512-Z+56e/Ljt0bUs+T+jPjhFyxYBcdY2RIq9ELFU+qAMQMteHo7ymbV7CKmlcX59RI9C4YzN8PgMgLyAoi916b5HA==",
+      "cpu": [
+        "x64"
+      ],
+      "optional": true,
+      "os": [
+        "win32"
+      ],
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/@popperjs/core": {
+      "version": "2.11.8",
+      "resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.8.tgz",
+      "integrity": "sha512-P1st0aksCrn9sGZhp8GMYwBnQsbvAWsZAX44oXNNvLHGqAOcoVxmjZiohstwQ7SqKnbR47akdNi+uleWD8+g6A==",
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/popperjs"
+      }
+    },
+    "node_modules/@swc/helpers": {
+      "version": "0.5.1",
+      "resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.5.1.tgz",
+      "integrity": "sha512-sJ902EfIzn1Fa+qYmjdQqh8tPsoxyBz+8yBKC2HKUxyezKJFwPGOn7pv4WY6QuQW//ySQi5lJjA/ZT9sNWWNTg==",
+      "dependencies": {
+        "tslib": "^2.4.0"
+      }
+    },
+    "node_modules/@tanstack/react-virtual": {
+      "version": "3.1.3",
+      "resolved": "https://registry.npmjs.org/@tanstack/react-virtual/-/react-virtual-3.1.3.tgz",
+      "integrity": "sha512-YCzcbF/Ws/uZ0q3Z6fagH+JVhx4JLvbSflgldMgLsuvB8aXjZLLb3HvrEVxY480F9wFlBiXlvQxOyXb5ENPrNA==",
+      "dependencies": {
+        "@tanstack/virtual-core": "3.1.3"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/tannerlinsley"
+      },
+      "peerDependencies": {
+        "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
+        "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
+      }
+    },
+    "node_modules/@tanstack/virtual-core": {
+      "version": "3.1.3",
+      "resolved": "https://registry.npmjs.org/@tanstack/virtual-core/-/virtual-core-3.1.3.tgz",
+      "integrity": "sha512-Y5B4EYyv1j9V8LzeAoOVeTg0LI7Fo5InYKgAjkY1Pu9GjtUwX/EKxNcU7ng3sKr99WEf+bPTcktAeybyMOYo+g==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/tannerlinsley"
+      }
+    },
+    "node_modules/@theguild/remark-mermaid": {
+      "version": "0.0.5",
+      "resolved": "https://registry.npmjs.org/@theguild/remark-mermaid/-/remark-mermaid-0.0.5.tgz",
+      "integrity": "sha512-e+ZIyJkEv9jabI4m7q29wZtZv+2iwPGsXJ2d46Zi7e+QcFudiyuqhLhHG/3gX3ZEB+hxTch+fpItyMS8jwbIcw==",
+      "dependencies": {
+        "mermaid": "^10.2.2",
+        "unist-util-visit": "^5.0.0"
+      },
+      "peerDependencies": {
+        "react": "^18.2.0"
+      }
+    },
+    "node_modules/@theguild/remark-npm2yarn": {
+      "version": "0.2.1",
+      "resolved": "https://registry.npmjs.org/@theguild/remark-npm2yarn/-/remark-npm2yarn-0.2.1.tgz",
+      "integrity": "sha512-jUTFWwDxtLEFtGZh/TW/w30ySaDJ8atKWH8dq2/IiQF61dPrGfETpl0WxD0VdBfuLOeU14/kop466oBSRO/5CA==",
+      "dependencies": {
+        "npm-to-yarn": "^2.1.0",
+        "unist-util-visit": "^5.0.0"
+      }
+    },
+    "node_modules/@types/acorn": {
+      "version": "4.0.6",
+      "resolved": "https://registry.npmjs.org/@types/acorn/-/acorn-4.0.6.tgz",
+      "integrity": "sha512-veQTnWP+1D/xbxVrPC3zHnCZRjSrKfhbMUlEA43iMZLu7EsnTtkJklIuwrCPbOi8YkvDQAiW05VQQFvvz9oieQ==",
+      "dependencies": {
+        "@types/estree": "*"
+      }
+    },
+    "node_modules/@types/d3-scale": {
+      "version": "4.0.8",
+      "resolved": "https://registry.npmjs.org/@types/d3-scale/-/d3-scale-4.0.8.tgz",
+      "integrity": "sha512-gkK1VVTr5iNiYJ7vWDI+yUFFlszhNMtVeneJ6lUTKPjprsvLLI9/tgEGiXJOnlINJA8FyA88gfnQsHbybVZrYQ==",
+      "dependencies": {
+        "@types/d3-time": "*"
+      }
+    },
+    "node_modules/@types/d3-scale-chromatic": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/@types/d3-scale-chromatic/-/d3-scale-chromatic-3.0.3.tgz",
+      "integrity": "sha512-laXM4+1o5ImZv3RpFAsTRn3TEkzqkytiOY0Dz0sq5cnd1dtNlk6sHLon4OvqaiJb28T0S/TdsBI3Sjsy+keJrw=="
+    },
+    "node_modules/@types/d3-time": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/@types/d3-time/-/d3-time-3.0.3.tgz",
+      "integrity": "sha512-2p6olUZ4w3s+07q3Tm2dbiMZy5pCDfYwtLXXHUnVzXgQlZ/OyPtUz6OL382BkOuGlLXqfT+wqv8Fw2v8/0geBw=="
+    },
+    "node_modules/@types/debug": {
+      "version": "4.1.12",
+      "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz",
+      "integrity": "sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==",
+      "dependencies": {
+        "@types/ms": "*"
+      }
+    },
+    "node_modules/@types/estree": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz",
+      "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw=="
+    },
+    "node_modules/@types/estree-jsx": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/@types/estree-jsx/-/estree-jsx-1.0.5.tgz",
+      "integrity": "sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg==",
+      "dependencies": {
+        "@types/estree": "*"
+      }
+    },
+    "node_modules/@types/hast": {
+      "version": "2.3.10",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-2.3.10.tgz",
+      "integrity": "sha512-McWspRw8xx8J9HurkVBfYj0xKoE25tOFlHGdx4MJ5xORQrMGZNqJhVQWaIbm6Oyla5kYOXtDiopzKRJzEOkwJw==",
+      "dependencies": {
+        "@types/unist": "^2"
+      }
+    },
+    "node_modules/@types/js-yaml": {
+      "version": "4.0.9",
+      "resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.9.tgz",
+      "integrity": "sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg=="
+    },
+    "node_modules/@types/katex": {
+      "version": "0.16.7",
+      "resolved": "https://registry.npmjs.org/@types/katex/-/katex-0.16.7.tgz",
+      "integrity": "sha512-HMwFiRujE5PjrgwHQ25+bsLJgowjGjm5Z8FVSf0N6PwgJrwxH0QxzHYDcKsTfV3wva0vzrpqMTJS2jXPr5BMEQ=="
+    },
+    "node_modules/@types/mdast": {
+      "version": "3.0.15",
+      "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.15.tgz",
+      "integrity": "sha512-LnwD+mUEfxWMa1QpDraczIn6k0Ee3SMicuYSSzS6ZYl2gKS09EClnJYGd8Du6rfc5r/GZEk5o1mRb8TaTj03sQ==",
+      "dependencies": {
+        "@types/unist": "^2"
+      }
+    },
+    "node_modules/@types/mdx": {
+      "version": "2.0.11",
+      "resolved": "https://registry.npmjs.org/@types/mdx/-/mdx-2.0.11.tgz",
+      "integrity": "sha512-HM5bwOaIQJIQbAYfax35HCKxx7a3KrK3nBtIqJgSOitivTD1y3oW9P3rxY9RkXYPUk7y/AjAohfHKmFpGE79zw=="
+    },
+    "node_modules/@types/ms": {
+      "version": "0.7.34",
+      "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.34.tgz",
+      "integrity": "sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g=="
+    },
+    "node_modules/@types/node": {
+      "version": "18.11.9",
+      "license": "MIT"
+    },
+    "node_modules/@types/prop-types": {
+      "version": "15.7.11",
+      "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.11.tgz",
+      "integrity": "sha512-ga8y9v9uyeiLdpKddhxYQkxNDrfvuPrlFb0N1qnZZByvcElJaXthF1UhvCh9TLWJBEHeNtdnbysW7Y6Uq8CVng=="
+    },
+    "node_modules/@types/react": {
+      "version": "18.0.25",
+      "license": "MIT",
+      "dependencies": {
+        "@types/prop-types": "*",
+        "@types/scheduler": "*",
+        "csstype": "^3.0.2"
+      }
+    },
+    "node_modules/@types/react-dom": {
+      "version": "18.0.9",
+      "license": "MIT",
+      "dependencies": {
+        "@types/react": "*"
+      }
+    },
+    "node_modules/@types/react-dom/node_modules/@types/react": {
+      "version": "18.2.20",
+      "license": "MIT",
+      "dependencies": {
+        "@types/prop-types": "*",
+        "@types/scheduler": "*",
+        "csstype": "^3.0.2"
+      }
+    },
+    "node_modules/@types/scheduler": {
+      "version": "0.16.8",
+      "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.8.tgz",
+      "integrity": "sha512-WZLiwShhwLRmeV6zH+GkbOFT6Z6VklCItrDioxUnv+u4Ll+8vKeFySoFyK/0ctcRpOmwAicELfmys1sDc/Rw+A=="
+    },
+    "node_modules/@types/unist": {
+      "version": "2.0.10",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.10.tgz",
+      "integrity": "sha512-IfYcSBWE3hLpBg8+X2SEa8LVkJdJEkT2Ese2aaLs3ptGdVtABxndrMaxuFlQ1qdFf9Q5rDvDpxI3WwgvKFAsQA=="
+    },
+    "node_modules/@ungap/structured-clone": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz",
+      "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ=="
+    },
+    "node_modules/acorn": {
+      "version": "8.11.3",
+      "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz",
+      "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==",
+      "bin": {
+        "acorn": "bin/acorn"
+      },
+      "engines": {
+        "node": ">=0.4.0"
+      }
+    },
+    "node_modules/acorn-jsx": {
+      "version": "5.3.2",
+      "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz",
+      "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==",
+      "peerDependencies": {
+        "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0"
+      }
+    },
+    "node_modules/ansi-sequence-parser": {
+      "version": "1.1.1",
+      "resolved": "https://registry.npmjs.org/ansi-sequence-parser/-/ansi-sequence-parser-1.1.1.tgz",
+      "integrity": "sha512-vJXt3yiaUL4UU546s3rPXlsry/RnM730G1+HkpKE012AN0sx1eOrxSu95oKDIonskeLTijMgqWZ3uDEe3NFvyg=="
+    },
+    "node_modules/ansi-styles": {
+      "version": "3.2.1",
+      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
+      "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
+      "dependencies": {
+        "color-convert": "^1.9.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/arch": {
+      "version": "2.2.0",
+      "resolved": "https://registry.npmjs.org/arch/-/arch-2.2.0.tgz",
+      "integrity": "sha512-Of/R0wqp83cgHozfIYLbBMnej79U/SVGOOyuB3VVFv1NRM/PSFMK12x9KVtiYzJqmnU5WR2qp0Z5rHb7sWGnFQ==",
+      "funding": [
+        {
+          "type": "github",
+          "url": "https://github.com/sponsors/feross"
+        },
+        {
+          "type": "patreon",
+          "url": "https://www.patreon.com/feross"
+        },
+        {
+          "type": "consulting",
+          "url": "https://feross.org/support"
+        }
+      ]
+    },
+    "node_modules/arg": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/arg/-/arg-1.0.0.tgz",
+      "integrity": "sha512-Wk7TEzl1KqvTGs/uyhmHO/3XLd3t1UeU4IstvPXVzGPM522cTjqjNZ99esCkcL52sjqjo8e8CTBcWhkxvGzoAw=="
+    },
+    "node_modules/argparse": {
+      "version": "1.0.10",
+      "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
+      "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
+      "dependencies": {
+        "sprintf-js": "~1.0.2"
+      }
+    },
+    "node_modules/astring": {
+      "version": "1.8.6",
+      "resolved": "https://registry.npmjs.org/astring/-/astring-1.8.6.tgz",
+      "integrity": "sha512-ISvCdHdlTDlH5IpxQJIex7BWBywFWgjJSVdwst+/iQCoEYnyOaQ95+X1JGshuBjGp6nxKUy1jMgE3zPqN7fQdg==",
+      "bin": {
+        "astring": "bin/astring"
+      }
+    },
+    "node_modules/bail": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/bail/-/bail-2.0.2.tgz",
+      "integrity": "sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/busboy": {
+      "version": "1.6.0",
+      "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",
+      "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==",
+      "dependencies": {
+        "streamsearch": "^1.1.0"
+      },
+      "engines": {
+        "node": ">=10.16.0"
+      }
+    },
+    "node_modules/caniuse-lite": {
+      "version": "1.0.30001597",
+      "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001597.tgz",
+      "integrity": "sha512-7LjJvmQU6Sj7bL0j5b5WY/3n7utXUJvAe1lxhsHDbLmwX9mdL86Yjtr+5SRCyf8qME4M7pU2hswj0FpyBVCv9w==",
+      "funding": [
+        {
+          "type": "opencollective",
+          "url": "https://opencollective.com/browserslist"
+        },
+        {
+          "type": "tidelift",
+          "url": "https://tidelift.com/funding/github/npm/caniuse-lite"
+        },
+        {
+          "type": "github",
+          "url": "https://github.com/sponsors/ai"
+        }
+      ]
+    },
+    "node_modules/ccount": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/ccount/-/ccount-2.0.1.tgz",
+      "integrity": "sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/chalk": {
+      "version": "2.3.0",
+      "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.3.0.tgz",
+      "integrity": "sha512-Az5zJR2CBujap2rqXGaJKaPHyJ0IrUimvYNX+ncCy8PJP4ltOGTrHUIo097ZaL2zMeKYpiCdqDvS6zdrTFok3Q==",
+      "dependencies": {
+        "ansi-styles": "^3.1.0",
+        "escape-string-regexp": "^1.0.5",
+        "supports-color": "^4.0.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/chalk/node_modules/escape-string-regexp": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
+      "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==",
+      "engines": {
+        "node": ">=0.8.0"
+      }
+    },
+    "node_modules/character-entities": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/character-entities/-/character-entities-2.0.2.tgz",
+      "integrity": "sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/character-entities-html4": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/character-entities-html4/-/character-entities-html4-2.1.0.tgz",
+      "integrity": "sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/character-entities-legacy": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/character-entities-legacy/-/character-entities-legacy-3.0.0.tgz",
+      "integrity": "sha512-RpPp0asT/6ufRm//AJVwpViZbGM/MkjQFxJccQRHmISF/22NBtsHqAWmL+/pmkPWoIUJdWyeVleTl1wydHATVQ==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/character-reference-invalid": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/character-reference-invalid/-/character-reference-invalid-2.0.1.tgz",
+      "integrity": "sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/client-only": {
+      "version": "0.0.1",
+      "resolved": "https://registry.npmjs.org/client-only/-/client-only-0.0.1.tgz",
+      "integrity": "sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA=="
+    },
+    "node_modules/clipboardy": {
+      "version": "1.2.2",
+      "resolved": "https://registry.npmjs.org/clipboardy/-/clipboardy-1.2.2.tgz",
+      "integrity": "sha512-16KrBOV7bHmHdxcQiCvfUFYVFyEah4FI8vYT1Fr7CGSA4G+xBWMEfUEQJS1hxeHGtI9ju1Bzs9uXSbj5HZKArw==",
+      "dependencies": {
+        "arch": "^2.1.0",
+        "execa": "^0.8.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/clsx": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.0.tgz",
+      "integrity": "sha512-m3iNNWpd9rl3jvvcBnu70ylMdrXt8Vlq4HYadnU5fwcOtvkSQWPmj7amUcDT2qYI7risszBjI5AUIUox9D16pg==",
+      "engines": {
+        "node": ">=6"
+      }
+    },
+    "node_modules/color-convert": {
+      "version": "1.9.3",
+      "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz",
+      "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==",
+      "dependencies": {
+        "color-name": "1.1.3"
+      }
+    },
+    "node_modules/color-name": {
+      "version": "1.1.3",
+      "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
+      "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw=="
+    },
+    "node_modules/comma-separated-tokens": {
+      "version": "2.0.3",
+      "resolved": "https://registry.npmjs.org/comma-separated-tokens/-/comma-separated-tokens-2.0.3.tgz",
+      "integrity": "sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/commander": {
+      "version": "8.3.0",
+      "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
+      "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
+      "engines": {
+        "node": ">= 12"
+      }
+    },
+    "node_modules/compute-scroll-into-view": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/compute-scroll-into-view/-/compute-scroll-into-view-3.1.0.tgz",
+      "integrity": "sha512-rj8l8pD4bJ1nx+dAkMhV1xB5RuZEyVysfxJqB1pRchh1KVvwOv9b7CGB8ZfjTImVv2oF+sYMUkMZq6Na5Ftmbg=="
+    },
+    "node_modules/cose-base": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/cose-base/-/cose-base-1.0.3.tgz",
+      "integrity": "sha512-s9whTXInMSgAp/NVXVNuVxVKzGH2qck3aQlVHxDCdAEPgtMKwc4Wq6/QKhgdEdgbLSi9rBTAcPoRa6JpiG4ksg==",
+      "dependencies": {
+        "layout-base": "^1.0.0"
+      }
+    },
+    "node_modules/cross-spawn": {
+      "version": "5.1.0",
+      "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz",
+      "integrity": "sha512-pTgQJ5KC0d2hcY8eyL1IzlBPYjTkyH72XRZPnLyKus2mBfNjQs3klqbJU2VILqZryAZUt9JOb3h/mWMy23/f5A==",
+      "dependencies": {
+        "lru-cache": "^4.0.1",
+        "shebang-command": "^1.2.0",
+        "which": "^1.2.9"
+      }
+    },
+    "node_modules/csstype": {
+      "version": "3.1.3",
+      "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
+      "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="
+    },
+    "node_modules/cytoscape": {
+      "version": "3.28.1",
+      "resolved": "https://registry.npmjs.org/cytoscape/-/cytoscape-3.28.1.tgz",
+      "integrity": "sha512-xyItz4O/4zp9/239wCcH8ZcFuuZooEeF8KHRmzjDfGdXsj3OG9MFSMA0pJE0uX3uCN/ygof6hHf4L7lst+JaDg==",
+      "dependencies": {
+        "heap": "^0.2.6",
+        "lodash": "^4.17.21"
+      },
+      "engines": {
+        "node": ">=0.10"
+      }
+    },
+    "node_modules/cytoscape-cose-bilkent": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/cytoscape-cose-bilkent/-/cytoscape-cose-bilkent-4.1.0.tgz",
+      "integrity": "sha512-wgQlVIUJF13Quxiv5e1gstZ08rnZj2XaLHGoFMYXz7SkNfCDOOteKBE6SYRfA9WxxI/iBc3ajfDoc6hb/MRAHQ==",
+      "dependencies": {
+        "cose-base": "^1.0.0"
+      },
+      "peerDependencies": {
+        "cytoscape": "^3.2.0"
+      }
+    },
+    "node_modules/d3": {
+      "version": "7.9.0",
+      "resolved": "https://registry.npmjs.org/d3/-/d3-7.9.0.tgz",
+      "integrity": "sha512-e1U46jVP+w7Iut8Jt8ri1YsPOvFpg46k+K8TpCb0P+zjCkjkPnV7WzfDJzMHy1LnA+wj5pLT1wjO901gLXeEhA==",
+      "dependencies": {
+        "d3-array": "3",
+        "d3-axis": "3",
+        "d3-brush": "3",
+        "d3-chord": "3",
+        "d3-color": "3",
+        "d3-contour": "4",
+        "d3-delaunay": "6",
+        "d3-dispatch": "3",
+        "d3-drag": "3",
+        "d3-dsv": "3",
+        "d3-ease": "3",
+        "d3-fetch": "3",
+        "d3-force": "3",
+        "d3-format": "3",
+        "d3-geo": "3",
+        "d3-hierarchy": "3",
+        "d3-interpolate": "3",
+        "d3-path": "3",
+        "d3-polygon": "3",
+        "d3-quadtree": "3",
+        "d3-random": "3",
+        "d3-scale": "4",
+        "d3-scale-chromatic": "3",
+        "d3-selection": "3",
+        "d3-shape": "3",
+        "d3-time": "3",
+        "d3-time-format": "4",
+        "d3-timer": "3",
+        "d3-transition": "3",
+        "d3-zoom": "3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-array": {
+      "version": "3.2.4",
+      "resolved": "https://registry.npmjs.org/d3-array/-/d3-array-3.2.4.tgz",
+      "integrity": "sha512-tdQAmyA18i4J7wprpYq8ClcxZy3SC31QMeByyCFyRt7BVHdREQZ5lpzoe5mFEYZUWe+oq8HBvk9JjpibyEV4Jg==",
+      "dependencies": {
+        "internmap": "1 - 2"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-axis": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/d3-axis/-/d3-axis-3.0.0.tgz",
+      "integrity": "sha512-IH5tgjV4jE/GhHkRV0HiVYPDtvfjHQlQfJHs0usq7M30XcSBvOotpmH1IgkcXsO/5gEQZD43B//fc7SRT5S+xw==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-brush": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/d3-brush/-/d3-brush-3.0.0.tgz",
+      "integrity": "sha512-ALnjWlVYkXsVIGlOsuWH1+3udkYFI48Ljihfnh8FZPF2QS9o+PzGLBslO0PjzVoHLZ2KCVgAM8NVkXPJB2aNnQ==",
+      "dependencies": {
+        "d3-dispatch": "1 - 3",
+        "d3-drag": "2 - 3",
+        "d3-interpolate": "1 - 3",
+        "d3-selection": "3",
+        "d3-transition": "3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-chord": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-chord/-/d3-chord-3.0.1.tgz",
+      "integrity": "sha512-VE5S6TNa+j8msksl7HwjxMHDM2yNK3XCkusIlpX5kwauBfXuyLAtNg9jCp/iHH61tgI4sb6R/EIMWCqEIdjT/g==",
+      "dependencies": {
+        "d3-path": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-color": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/d3-color/-/d3-color-3.1.0.tgz",
+      "integrity": "sha512-zg/chbXyeBtMQ1LbD/WSoW2DpC3I0mpmPdW+ynRTj/x2DAWYrIY7qeZIHidozwV24m4iavr15lNwIwLxRmOxhA==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-contour": {
+      "version": "4.0.2",
+      "resolved": "https://registry.npmjs.org/d3-contour/-/d3-contour-4.0.2.tgz",
+      "integrity": "sha512-4EzFTRIikzs47RGmdxbeUvLWtGedDUNkTcmzoeyg4sP/dvCexO47AaQL7VKy/gul85TOxw+IBgA8US2xwbToNA==",
+      "dependencies": {
+        "d3-array": "^3.2.0"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-delaunay": {
+      "version": "6.0.4",
+      "resolved": "https://registry.npmjs.org/d3-delaunay/-/d3-delaunay-6.0.4.tgz",
+      "integrity": "sha512-mdjtIZ1XLAM8bm/hx3WwjfHt6Sggek7qH043O8KEjDXN40xi3vx/6pYSVTwLjEgiXQTbvaouWKynLBiUZ6SK6A==",
+      "dependencies": {
+        "delaunator": "5"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-dispatch": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-dispatch/-/d3-dispatch-3.0.1.tgz",
+      "integrity": "sha512-rzUyPU/S7rwUflMyLc1ETDeBj0NRuHKKAcvukozwhshr6g6c5d8zh4c2gQjY2bZ0dXeGLWc1PF174P2tVvKhfg==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-drag": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/d3-drag/-/d3-drag-3.0.0.tgz",
+      "integrity": "sha512-pWbUJLdETVA8lQNJecMxoXfH6x+mO2UQo8rSmZ+QqxcbyA3hfeprFgIT//HW2nlHChWeIIMwS2Fq+gEARkhTkg==",
+      "dependencies": {
+        "d3-dispatch": "1 - 3",
+        "d3-selection": "3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-dsv": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-dsv/-/d3-dsv-3.0.1.tgz",
+      "integrity": "sha512-UG6OvdI5afDIFP9w4G0mNq50dSOsXHJaRE8arAS5o9ApWnIElp8GZw1Dun8vP8OyHOZ/QJUKUJwxiiCCnUwm+Q==",
+      "dependencies": {
+        "commander": "7",
+        "iconv-lite": "0.6",
+        "rw": "1"
+      },
+      "bin": {
+        "csv2json": "bin/dsv2json.js",
+        "csv2tsv": "bin/dsv2dsv.js",
+        "dsv2dsv": "bin/dsv2dsv.js",
+        "dsv2json": "bin/dsv2json.js",
+        "json2csv": "bin/json2dsv.js",
+        "json2dsv": "bin/json2dsv.js",
+        "json2tsv": "bin/json2dsv.js",
+        "tsv2csv": "bin/dsv2dsv.js",
+        "tsv2json": "bin/dsv2json.js"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-dsv/node_modules/commander": {
+      "version": "7.2.0",
+      "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz",
+      "integrity": "sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==",
+      "engines": {
+        "node": ">= 10"
+      }
+    },
+    "node_modules/d3-ease": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-ease/-/d3-ease-3.0.1.tgz",
+      "integrity": "sha512-wR/XK3D3XcLIZwpbvQwQ5fK+8Ykds1ip7A2Txe0yxncXSdq1L9skcG7blcedkOX+ZcgxGAmLX1FrRGbADwzi0w==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-fetch": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-fetch/-/d3-fetch-3.0.1.tgz",
+      "integrity": "sha512-kpkQIM20n3oLVBKGg6oHrUchHM3xODkTzjMoj7aWQFq5QEM+R6E4WkzT5+tojDY7yjez8KgCBRoj4aEr99Fdqw==",
+      "dependencies": {
+        "d3-dsv": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-force": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/d3-force/-/d3-force-3.0.0.tgz",
+      "integrity": "sha512-zxV/SsA+U4yte8051P4ECydjD/S+qeYtnaIyAs9tgHCqfguma/aAQDjo85A9Z6EKhBirHRJHXIgJUlffT4wdLg==",
+      "dependencies": {
+        "d3-dispatch": "1 - 3",
+        "d3-quadtree": "1 - 3",
+        "d3-timer": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-format": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/d3-format/-/d3-format-3.1.0.tgz",
+      "integrity": "sha512-YyUI6AEuY/Wpt8KWLgZHsIU86atmikuoOmCfommt0LYHiQSPjvX2AcFc38PX0CBpr2RCyZhjex+NS/LPOv6YqA==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-geo": {
+      "version": "3.1.1",
+      "resolved": "https://registry.npmjs.org/d3-geo/-/d3-geo-3.1.1.tgz",
+      "integrity": "sha512-637ln3gXKXOwhalDzinUgY83KzNWZRKbYubaG+fGVuc/dxO64RRljtCTnf5ecMyE1RIdtqpkVcq0IbtU2S8j2Q==",
+      "dependencies": {
+        "d3-array": "2.5.0 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-hierarchy": {
+      "version": "3.1.2",
+      "resolved": "https://registry.npmjs.org/d3-hierarchy/-/d3-hierarchy-3.1.2.tgz",
+      "integrity": "sha512-FX/9frcub54beBdugHjDCdikxThEqjnR93Qt7PvQTOHxyiNCAlvMrHhclk3cD5VeAaq9fxmfRp+CnWw9rEMBuA==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-interpolate": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-interpolate/-/d3-interpolate-3.0.1.tgz",
+      "integrity": "sha512-3bYs1rOD33uo8aqJfKP3JWPAibgw8Zm2+L9vBKEHJ2Rg+viTR7o5Mmv5mZcieN+FRYaAOWX5SJATX6k1PWz72g==",
+      "dependencies": {
+        "d3-color": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-path": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/d3-path/-/d3-path-3.1.0.tgz",
+      "integrity": "sha512-p3KP5HCf/bvjBSSKuXid6Zqijx7wIfNW+J/maPs+iwR35at5JCbLUT0LzF1cnjbCHWhqzQTIN2Jpe8pRebIEFQ==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-polygon": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-polygon/-/d3-polygon-3.0.1.tgz",
+      "integrity": "sha512-3vbA7vXYwfe1SYhED++fPUQlWSYTTGmFmQiany/gdbiWgU/iEyQzyymwL9SkJjFFuCS4902BSzewVGsHHmHtXg==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-quadtree": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-quadtree/-/d3-quadtree-3.0.1.tgz",
+      "integrity": "sha512-04xDrxQTDTCFwP5H6hRhsRcb9xxv2RzkcsygFzmkSIOJy3PeRJP7sNk3VRIbKXcog561P9oU0/rVH6vDROAgUw==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-random": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-random/-/d3-random-3.0.1.tgz",
+      "integrity": "sha512-FXMe9GfxTxqd5D6jFsQ+DJ8BJS4E/fT5mqqdjovykEB2oFbTMDVdg1MGFxfQW+FBOGoB++k8swBrgwSHT1cUXQ==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-sankey": {
+      "version": "0.12.3",
+      "resolved": "https://registry.npmjs.org/d3-sankey/-/d3-sankey-0.12.3.tgz",
+      "integrity": "sha512-nQhsBRmM19Ax5xEIPLMY9ZmJ/cDvd1BG3UVvt5h3WRxKg5zGRbvnteTyWAbzeSvlh3tW7ZEmq4VwR5mB3tutmQ==",
+      "dependencies": {
+        "d3-array": "1 - 2",
+        "d3-shape": "^1.2.0"
+      }
+    },
+    "node_modules/d3-sankey/node_modules/d3-array": {
+      "version": "2.12.1",
+      "resolved": "https://registry.npmjs.org/d3-array/-/d3-array-2.12.1.tgz",
+      "integrity": "sha512-B0ErZK/66mHtEsR1TkPEEkwdy+WDesimkM5gpZr5Dsg54BiTA5RXtYW5qTLIAcekaS9xfZrzBLF/OAkB3Qn1YQ==",
+      "dependencies": {
+        "internmap": "^1.0.0"
+      }
+    },
+    "node_modules/d3-sankey/node_modules/d3-path": {
+      "version": "1.0.9",
+      "resolved": "https://registry.npmjs.org/d3-path/-/d3-path-1.0.9.tgz",
+      "integrity": "sha512-VLaYcn81dtHVTjEHd8B+pbe9yHWpXKZUC87PzoFmsFrJqgFwDe/qxfp5MlfsfM1V5E/iVt0MmEbWQ7FVIXh/bg=="
+    },
+    "node_modules/d3-sankey/node_modules/d3-shape": {
+      "version": "1.3.7",
+      "resolved": "https://registry.npmjs.org/d3-shape/-/d3-shape-1.3.7.tgz",
+      "integrity": "sha512-EUkvKjqPFUAZyOlhY5gzCxCeI0Aep04LwIRpsZ/mLFelJiUfnK56jo5JMDSE7yyP2kLSb6LtF+S5chMk7uqPqw==",
+      "dependencies": {
+        "d3-path": "1"
+      }
+    },
+    "node_modules/d3-sankey/node_modules/internmap": {
+      "version": "1.0.1",
+      "resolved": "https://registry.npmjs.org/internmap/-/internmap-1.0.1.tgz",
+      "integrity": "sha512-lDB5YccMydFBtasVtxnZ3MRBHuaoE8GKsppq+EchKL2U4nK/DmEpPHNH8MZe5HkMtpSiTSOZwfN0tzYjO/lJEw=="
+    },
+    "node_modules/d3-scale": {
+      "version": "4.0.2",
+      "resolved": "https://registry.npmjs.org/d3-scale/-/d3-scale-4.0.2.tgz",
+      "integrity": "sha512-GZW464g1SH7ag3Y7hXjf8RoUuAFIqklOAq3MRl4OaWabTFJY9PN/E1YklhXLh+OQ3fM9yS2nOkCoS+WLZ6kvxQ==",
+      "dependencies": {
+        "d3-array": "2.10.0 - 3",
+        "d3-format": "1 - 3",
+        "d3-interpolate": "1.2.0 - 3",
+        "d3-time": "2.1.1 - 3",
+        "d3-time-format": "2 - 4"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-scale-chromatic": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/d3-scale-chromatic/-/d3-scale-chromatic-3.1.0.tgz",
+      "integrity": "sha512-A3s5PWiZ9YCXFye1o246KoscMWqf8BsD9eRiJ3He7C9OBaxKhAd5TFCdEx/7VbKtxxTsu//1mMJFrEt572cEyQ==",
+      "dependencies": {
+        "d3-color": "1 - 3",
+        "d3-interpolate": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-selection": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/d3-selection/-/d3-selection-3.0.0.tgz",
+      "integrity": "sha512-fmTRWbNMmsmWq6xJV8D19U/gw/bwrHfNXxrIN+HfZgnzqTHp9jOmKMhsTUjXOJnZOdZY9Q28y4yebKzqDKlxlQ==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-shape": {
+      "version": "3.2.0",
+      "resolved": "https://registry.npmjs.org/d3-shape/-/d3-shape-3.2.0.tgz",
+      "integrity": "sha512-SaLBuwGm3MOViRq2ABk3eLoxwZELpH6zhl3FbAoJ7Vm1gofKx6El1Ib5z23NUEhF9AsGl7y+dzLe5Cw2AArGTA==",
+      "dependencies": {
+        "d3-path": "^3.1.0"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-time": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/d3-time/-/d3-time-3.1.0.tgz",
+      "integrity": "sha512-VqKjzBLejbSMT4IgbmVgDjpkYrNWUYJnbCGo874u7MMKIWsILRX+OpX/gTk8MqjpT1A/c6HY2dCA77ZN0lkQ2Q==",
+      "dependencies": {
+        "d3-array": "2 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-time-format": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/d3-time-format/-/d3-time-format-4.1.0.tgz",
+      "integrity": "sha512-dJxPBlzC7NugB2PDLwo9Q8JiTR3M3e4/XANkreKSUxF8vvXKqm1Yfq4Q5dl8budlunRVlUUaDUgFt7eA8D6NLg==",
+      "dependencies": {
+        "d3-time": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-timer": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-timer/-/d3-timer-3.0.1.tgz",
+      "integrity": "sha512-ndfJ/JxxMd3nw31uyKoY2naivF+r29V+Lc0svZxe1JvvIRmi8hUsrMvdOwgS1o6uBHmiz91geQ0ylPP0aj1VUA==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/d3-transition": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/d3-transition/-/d3-transition-3.0.1.tgz",
+      "integrity": "sha512-ApKvfjsSR6tg06xrL434C0WydLr7JewBB3V+/39RMHsaXTOG0zmt/OAXeng5M5LBm0ojmxJrpomQVZ1aPvBL4w==",
+      "dependencies": {
+        "d3-color": "1 - 3",
+        "d3-dispatch": "1 - 3",
+        "d3-ease": "1 - 3",
+        "d3-interpolate": "1 - 3",
+        "d3-timer": "1 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      },
+      "peerDependencies": {
+        "d3-selection": "2 - 3"
+      }
+    },
+    "node_modules/d3-zoom": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/d3-zoom/-/d3-zoom-3.0.0.tgz",
+      "integrity": "sha512-b8AmV3kfQaqWAuacbPuNbL6vahnOJflOhexLzMMNLga62+/nh0JzvJ0aO/5a5MVgUFGS7Hu1P9P03o3fJkDCyw==",
+      "dependencies": {
+        "d3-dispatch": "1 - 3",
+        "d3-drag": "2 - 3",
+        "d3-interpolate": "1 - 3",
+        "d3-selection": "2 - 3",
+        "d3-transition": "2 - 3"
+      },
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/dagre-d3-es": {
+      "version": "7.0.10",
+      "resolved": "https://registry.npmjs.org/dagre-d3-es/-/dagre-d3-es-7.0.10.tgz",
+      "integrity": "sha512-qTCQmEhcynucuaZgY5/+ti3X/rnszKZhEQH/ZdWdtP1tA/y3VoHJzcVrO9pjjJCNpigfscAtoUB5ONcd2wNn0A==",
+      "dependencies": {
+        "d3": "^7.8.2",
+        "lodash-es": "^4.17.21"
+      }
+    },
+    "node_modules/dayjs": {
+      "version": "1.11.10",
+      "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz",
+      "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ=="
+    },
+    "node_modules/debug": {
+      "version": "4.3.4",
+      "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
+      "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
+      "dependencies": {
+        "ms": "2.1.2"
+      },
+      "engines": {
+        "node": ">=6.0"
+      },
+      "peerDependenciesMeta": {
+        "supports-color": {
+          "optional": true
+        }
+      }
+    },
+    "node_modules/decode-named-character-reference": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/decode-named-character-reference/-/decode-named-character-reference-1.0.2.tgz",
+      "integrity": "sha512-O8x12RzrUF8xyVcY0KJowWsmaJxQbmy0/EtnNtHRpsOcT7dFk5W598coHqBVpmWo1oQQfsCqfCmkZN5DJrZVdg==",
+      "dependencies": {
+        "character-entities": "^2.0.0"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/delaunator": {
+      "version": "5.0.1",
+      "resolved": "https://registry.npmjs.org/delaunator/-/delaunator-5.0.1.tgz",
+      "integrity": "sha512-8nvh+XBe96aCESrGOqMp/84b13H9cdKbG5P2ejQCh4d4sK9RL4371qou9drQjMhvnPmhWl5hnmqbEE0fXr9Xnw==",
+      "dependencies": {
+        "robust-predicates": "^3.0.2"
+      }
+    },
+    "node_modules/dequal": {
+      "version": "2.0.3",
+      "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz",
+      "integrity": "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==",
+      "engines": {
+        "node": ">=6"
+      }
+    },
+    "node_modules/devlop": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/devlop/-/devlop-1.1.0.tgz",
+      "integrity": "sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==",
+      "dependencies": {
+        "dequal": "^2.0.0"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/diff": {
+      "version": "5.2.0",
+      "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz",
+      "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==",
+      "engines": {
+        "node": ">=0.3.1"
+      }
+    },
+    "node_modules/dompurify": {
+      "version": "3.0.9",
+      "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.0.9.tgz",
+      "integrity": "sha512-uyb4NDIvQ3hRn6NiC+SIFaP4mJ/MdXlvtunaqK9Bn6dD3RuB/1S/gasEjDHD8eiaqdSael2vBv+hOs7Y+jhYOQ=="
+    },
+    "node_modules/elkjs": {
+      "version": "0.9.2",
+      "resolved": "https://registry.npmjs.org/elkjs/-/elkjs-0.9.2.tgz",
+      "integrity": "sha512-2Y/RaA1pdgSHpY0YG4TYuYCD2wh97CRvu22eLG3Kz0pgQ/6KbIFTxsTnDc4MH/6hFlg2L/9qXrDMG0nMjP63iw=="
+    },
+    "node_modules/entities": {
+      "version": "4.5.0",
+      "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
+      "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
+      "engines": {
+        "node": ">=0.12"
+      },
+      "funding": {
+        "url": "https://github.com/fb55/entities?sponsor=1"
+      }
+    },
+    "node_modules/escape-string-regexp": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",
+      "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==",
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/esprima": {
+      "version": "4.0.1",
+      "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
+      "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
+      "bin": {
+        "esparse": "bin/esparse.js",
+        "esvalidate": "bin/esvalidate.js"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/estree-util-attach-comments": {
+      "version": "2.1.1",
+      "resolved": "https://registry.npmjs.org/estree-util-attach-comments/-/estree-util-attach-comments-2.1.1.tgz",
+      "integrity": "sha512-+5Ba/xGGS6mnwFbXIuQiDPTbuTxuMCooq3arVv7gPZtYpjp+VXH/NkHAP35OOefPhNG/UGqU3vt/LTABwcHX0w==",
+      "dependencies": {
+        "@types/estree": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/estree-util-build-jsx": {
+      "version": "2.2.2",
+      "resolved": "https://registry.npmjs.org/estree-util-build-jsx/-/estree-util-build-jsx-2.2.2.tgz",
+      "integrity": "sha512-m56vOXcOBuaF+Igpb9OPAy7f9w9OIkb5yhjsZuaPm7HoGi4oTOQi0h2+yZ+AtKklYFZ+rPC4n0wYCJCEU1ONqg==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "estree-util-is-identifier-name": "^2.0.0",
+        "estree-walker": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/estree-util-is-identifier-name": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/estree-util-is-identifier-name/-/estree-util-is-identifier-name-2.1.0.tgz",
+      "integrity": "sha512-bEN9VHRyXAUOjkKVQVvArFym08BTWB0aJPppZZr0UNyAqWsLaVfAqP7hbaTJjzHifmB5ebnR8Wm7r7yGN/HonQ==",
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/estree-util-to-js": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/estree-util-to-js/-/estree-util-to-js-1.2.0.tgz",
+      "integrity": "sha512-IzU74r1PK5IMMGZXUVZbmiu4A1uhiPgW5hm1GjcOfr4ZzHaMPpLNJjR7HjXiIOzi25nZDrgFTobHTkV5Q6ITjA==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "astring": "^1.8.0",
+        "source-map": "^0.7.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/estree-util-value-to-estree": {
+      "version": "1.3.0",
+      "resolved": "https://registry.npmjs.org/estree-util-value-to-estree/-/estree-util-value-to-estree-1.3.0.tgz",
+      "integrity": "sha512-Y+ughcF9jSUJvncXwqRageavjrNPAI+1M/L3BI3PyLp1nmgYTGUXU6t5z1Y7OWuThoDdhPME07bQU+d5LxdJqw==",
+      "dependencies": {
+        "is-plain-obj": "^3.0.0"
+      },
+      "engines": {
+        "node": ">=12.0.0"
+      }
+    },
+    "node_modules/estree-util-visit": {
+      "version": "1.2.1",
+      "resolved": "https://registry.npmjs.org/estree-util-visit/-/estree-util-visit-1.2.1.tgz",
+      "integrity": "sha512-xbgqcrkIVbIG+lI/gzbvd9SGTJL4zqJKBFttUl5pP27KhAjtMKbX/mQXJ7qgyXpMgVy/zvpm0xoQQaGL8OloOw==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "@types/unist": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/estree-walker": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz",
+      "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==",
+      "dependencies": {
+        "@types/estree": "^1.0.0"
+      }
+    },
+    "node_modules/execa": {
+      "version": "0.8.0",
+      "resolved": "https://registry.npmjs.org/execa/-/execa-0.8.0.tgz",
+      "integrity": "sha512-zDWS+Rb1E8BlqqhALSt9kUhss8Qq4nN3iof3gsOdyINksElaPyNBtKUMTR62qhvgVWR0CqCX7sdnKe4MnUbFEA==",
+      "dependencies": {
+        "cross-spawn": "^5.0.1",
+        "get-stream": "^3.0.0",
+        "is-stream": "^1.1.0",
+        "npm-run-path": "^2.0.0",
+        "p-finally": "^1.0.0",
+        "signal-exit": "^3.0.0",
+        "strip-eof": "^1.0.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/extend": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
+      "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="
+    },
+    "node_modules/extend-shallow": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-2.0.1.tgz",
+      "integrity": "sha512-zCnTtlxNoAiDc3gqY2aYAWFx7XWWiasuF2K8Me5WbN8otHKTUKBwjPtNpRs/rbUZm7KxWAaNj7P1a/p52GbVug==",
+      "dependencies": {
+        "is-extendable": "^0.1.0"
+      },
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/flexsearch": {
+      "version": "0.7.43",
+      "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.43.tgz",
+      "integrity": "sha512-c5o/+Um8aqCSOXGcZoqZOm+NqtVwNsvVpWv6lfmSclU954O3wvQKxxK8zj74fPaSJbXpSLTs4PRhh+wnoCXnKg=="
+    },
+    "node_modules/focus-visible": {
+      "version": "5.2.0",
+      "resolved": "https://registry.npmjs.org/focus-visible/-/focus-visible-5.2.0.tgz",
+      "integrity": "sha512-Rwix9pBtC1Nuy5wysTmKy+UjbDJpIfg8eHjw0rjZ1mX4GNLz1Bmd16uDpI3Gk1i70Fgcs8Csg2lPm8HULFg9DQ=="
+    },
+    "node_modules/get-stream": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",
+      "integrity": "sha512-GlhdIUuVakc8SJ6kK0zAFbiGzRFzNnY4jUuEbV9UROo4Y+0Ny4fjvcZFVTeDA4odpFyOQzaw6hXukJSq/f28sQ==",
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/git-up": {
+      "version": "7.0.0",
+      "resolved": "https://registry.npmjs.org/git-up/-/git-up-7.0.0.tgz",
+      "integrity": "sha512-ONdIrbBCFusq1Oy0sC71F5azx8bVkvtZtMJAsv+a6lz5YAmbNnLD6HAB4gptHZVLPR8S2/kVN6Gab7lryq5+lQ==",
+      "dependencies": {
+        "is-ssh": "^1.4.0",
+        "parse-url": "^8.1.0"
+      }
+    },
+    "node_modules/git-url-parse": {
+      "version": "13.1.1",
+      "resolved": "https://registry.npmjs.org/git-url-parse/-/git-url-parse-13.1.1.tgz",
+      "integrity": "sha512-PCFJyeSSdtnbfhSNRw9Wk96dDCNx+sogTe4YNXeXSJxt7xz5hvXekuRn9JX7m+Mf4OscCu8h+mtAl3+h5Fo8lQ==",
+      "dependencies": {
+        "git-up": "^7.0.0"
+      }
+    },
+    "node_modules/github-slugger": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/github-slugger/-/github-slugger-2.0.0.tgz",
+      "integrity": "sha512-IaOQ9puYtjrkq7Y0Ygl9KDZnrf/aiUJYUpVf89y8kyaxbRG7Y1SrX/jaumrv81vc61+kiMempujsM3Yw7w5qcw=="
+    },
+    "node_modules/glob-to-regexp": {
+      "version": "0.4.1",
+      "resolved": "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz",
+      "integrity": "sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw=="
+    },
+    "node_modules/graceful-fs": {
+      "version": "4.2.11",
+      "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
+      "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="
+    },
+    "node_modules/gray-matter": {
+      "version": "4.0.3",
+      "resolved": "https://registry.npmjs.org/gray-matter/-/gray-matter-4.0.3.tgz",
+      "integrity": "sha512-5v6yZd4JK3eMI3FqqCouswVqwugaA9r4dNZB1wwcmrD02QkV5H0y7XBQW8QwQqEaZY1pM9aqORSORhJRdNK44Q==",
+      "dependencies": {
+        "js-yaml": "^3.13.1",
+        "kind-of": "^6.0.2",
+        "section-matter": "^1.0.0",
+        "strip-bom-string": "^1.0.0"
+      },
+      "engines": {
+        "node": ">=6.0"
+      }
+    },
+    "node_modules/has-flag": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz",
+      "integrity": "sha512-P+1n3MnwjR/Epg9BBo1KT8qbye2g2Ou4sFumihwt6I4tsUX7jnLcX4BTOSKg/B1ZrIYMN9FcEnG4x5a7NB8Eng==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/hash-obj": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/hash-obj/-/hash-obj-4.0.0.tgz",
+      "integrity": "sha512-FwO1BUVWkyHasWDW4S8o0ssQXjvyghLV2rfVhnN36b2bbcj45eGiuzdn9XOvOpjV3TKQD7Gm2BWNXdE9V4KKYg==",
+      "dependencies": {
+        "is-obj": "^3.0.0",
+        "sort-keys": "^5.0.0",
+        "type-fest": "^1.0.2"
+      },
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/hast-util-from-dom": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/hast-util-from-dom/-/hast-util-from-dom-5.0.0.tgz",
+      "integrity": "sha512-d6235voAp/XR3Hh5uy7aGLbM3S4KamdW0WEgOaU1YoewnuYw4HXb5eRtv9g65m/RFGEfUY1Mw4UqCc5Y8L4Stg==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "hastscript": "^8.0.0",
+        "web-namespaces": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-dom/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-from-html": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/hast-util-from-html/-/hast-util-from-html-2.0.1.tgz",
+      "integrity": "sha512-RXQBLMl9kjKVNkJTIO6bZyb2n+cUH8LFaSSzo82jiLT6Tfc+Pt7VQCS+/h3YwG4jaNE2TA2sdJisGWR+aJrp0g==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "devlop": "^1.1.0",
+        "hast-util-from-parse5": "^8.0.0",
+        "parse5": "^7.0.0",
+        "vfile": "^6.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-html-isomorphic": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/hast-util-from-html-isomorphic/-/hast-util-from-html-isomorphic-2.0.0.tgz",
+      "integrity": "sha512-zJfpXq44yff2hmE0XmwEOzdWin5xwH+QIhMLOScpX91e/NSGPsAzNCvLQDIEPyO2TXi+lBmU6hjLIhV8MwP2kw==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "hast-util-from-dom": "^5.0.0",
+        "hast-util-from-html": "^2.0.0",
+        "unist-util-remove-position": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-html-isomorphic/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-from-html/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-from-html/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/hast-util-from-html/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-html/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-parse5": {
+      "version": "8.0.1",
+      "resolved": "https://registry.npmjs.org/hast-util-from-parse5/-/hast-util-from-parse5-8.0.1.tgz",
+      "integrity": "sha512-Er/Iixbc7IEa7r/XLtuG52zoqn/b3Xng/w6aZQ0xGVxzhw5xUFxcRqdPzP6yFi/4HBYRaifaI5fQ1RH8n0ZeOQ==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "@types/unist": "^3.0.0",
+        "devlop": "^1.0.0",
+        "hastscript": "^8.0.0",
+        "property-information": "^6.0.0",
+        "vfile": "^6.0.0",
+        "vfile-location": "^5.0.0",
+        "web-namespaces": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-parse5/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-from-parse5/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/hast-util-from-parse5/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-from-parse5/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-is-element": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/hast-util-is-element/-/hast-util-is-element-3.0.0.tgz",
+      "integrity": "sha512-Val9mnv2IWpLbNPqc/pUem+a7Ipj2aHacCwgNfTiK0vJKl0LF+4Ba4+v1oPHFpf3bLYmreq0/l3Gud9S5OH42g==",
+      "dependencies": {
+        "@types/hast": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-is-element/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-parse-selector": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/hast-util-parse-selector/-/hast-util-parse-selector-4.0.0.tgz",
+      "integrity": "sha512-wkQCkSYoOGCRKERFWcxMVMOcYE2K1AaNLU8DXS9arxnLOUEWbOXKXiJUNzEpqZ3JOKpnha3jkFrumEjVliDe7A==",
+      "dependencies": {
+        "@types/hast": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-parse-selector/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-raw": {
+      "version": "9.0.2",
+      "resolved": "https://registry.npmjs.org/hast-util-raw/-/hast-util-raw-9.0.2.tgz",
+      "integrity": "sha512-PldBy71wO9Uq1kyaMch9AHIghtQvIwxBUkv823pKmkTM3oV1JxtsTNYdevMxvUHqcnOAuO65JKU2+0NOxc2ksA==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "@types/unist": "^3.0.0",
+        "@ungap/structured-clone": "^1.0.0",
+        "hast-util-from-parse5": "^8.0.0",
+        "hast-util-to-parse5": "^8.0.0",
+        "html-void-elements": "^3.0.0",
+        "mdast-util-to-hast": "^13.0.0",
+        "parse5": "^7.0.0",
+        "unist-util-position": "^5.0.0",
+        "unist-util-visit": "^5.0.0",
+        "vfile": "^6.0.0",
+        "web-namespaces": "^2.0.0",
+        "zwitch": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-raw/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-raw/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/hast-util-raw/node_modules/unist-util-position": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-position/-/unist-util-position-5.0.0.tgz",
+      "integrity": "sha512-fucsC7HjXvkB5R3kTCO7kUjRdrS0BJt3M/FPxmHMBOm8JQi2BsHAHFsy27E0EolP8rp0NzXsJ+jNPyDWvOJZPA==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-raw/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-raw/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-to-estree": {
+      "version": "2.3.3",
+      "resolved": "https://registry.npmjs.org/hast-util-to-estree/-/hast-util-to-estree-2.3.3.tgz",
+      "integrity": "sha512-ihhPIUPxN0v0w6M5+IiAZZrn0LH2uZomeWwhn7uP7avZC6TE7lIiEh2yBMPr5+zi1aUCXq6VoYRgs2Bw9xmycQ==",
+      "dependencies": {
+        "@types/estree": "^1.0.0",
+        "@types/estree-jsx": "^1.0.0",
+        "@types/hast": "^2.0.0",
+        "@types/unist": "^2.0.0",
+        "comma-separated-tokens": "^2.0.0",
+        "estree-util-attach-comments": "^2.0.0",
+        "estree-util-is-identifier-name": "^2.0.0",
+        "hast-util-whitespace": "^2.0.0",
+        "mdast-util-mdx-expression": "^1.0.0",
+        "mdast-util-mdxjs-esm": "^1.0.0",
+        "property-information": "^6.0.0",
+        "space-separated-tokens": "^2.0.0",
+        "style-to-object": "^0.4.1",
+        "unist-util-position": "^4.0.0",
+        "zwitch": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-to-parse5": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/hast-util-to-parse5/-/hast-util-to-parse5-8.0.0.tgz",
+      "integrity": "sha512-3KKrV5ZVI8if87DVSi1vDeByYrkGzg4mEfeu4alwgmmIeARiBLKCZS2uw5Gb6nU9x9Yufyj3iudm6i7nl52PFw==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "comma-separated-tokens": "^2.0.0",
+        "devlop": "^1.0.0",
+        "property-information": "^6.0.0",
+        "space-separated-tokens": "^2.0.0",
+        "web-namespaces": "^2.0.0",
+        "zwitch": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-to-parse5/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-to-text": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/hast-util-to-text/-/hast-util-to-text-4.0.0.tgz",
+      "integrity": "sha512-EWiE1FSArNBPUo1cKWtzqgnuRQwEeQbQtnFJRYV1hb1BWDgrAlBU0ExptvZMM/KSA82cDpm2sFGf3Dmc5Mza3w==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "@types/unist": "^3.0.0",
+        "hast-util-is-element": "^3.0.0",
+        "unist-util-find-after": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hast-util-to-text/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/hast-util-to-text/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/hast-util-whitespace": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/hast-util-whitespace/-/hast-util-whitespace-2.0.1.tgz",
+      "integrity": "sha512-nAxA0v8+vXSBDt3AnRUNjyRIQ0rD+ntpbAp4LnPkumc5M9yUbSMa4XDU9Q6etY4f1Wp4bNgvc1yjiZtsTTrSng==",
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hastscript": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/hastscript/-/hastscript-8.0.0.tgz",
+      "integrity": "sha512-dMOtzCEd3ABUeSIISmrETiKuyydk1w0pa+gE/uormcTpSYuaNJPbX1NU3JLyscSLjwAQM8bWMhhIlnCqnRvDTw==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "comma-separated-tokens": "^2.0.0",
+        "hast-util-parse-selector": "^4.0.0",
+        "property-information": "^6.0.0",
+        "space-separated-tokens": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/hastscript/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/heap": {
+      "version": "0.2.7",
+      "resolved": "https://registry.npmjs.org/heap/-/heap-0.2.7.tgz",
+      "integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg=="
+    },
+    "node_modules/html-void-elements": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/html-void-elements/-/html-void-elements-3.0.0.tgz",
+      "integrity": "sha512-bEqo66MRXsUGxWHV5IP0PUiAWwoEjba4VCzg0LjFJBpchPaTfyfCKTG6bc5F8ucKec3q5y6qOdGyYTSBEvhCrg==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/iconv-lite": {
+      "version": "0.6.3",
+      "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
+      "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
+      "dependencies": {
+        "safer-buffer": ">= 2.1.2 < 3.0.0"
+      },
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/inline-style-parser": {
+      "version": "0.1.1",
+      "resolved": "https://registry.npmjs.org/inline-style-parser/-/inline-style-parser-0.1.1.tgz",
+      "integrity": "sha512-7NXolsK4CAS5+xvdj5OMMbI962hU/wvwoxk+LWR9Ek9bVtyuuYScDN6eS0rUm6TxApFpw7CX1o4uJzcd4AyD3Q=="
+    },
+    "node_modules/internmap": {
+      "version": "2.0.3",
+      "resolved": "https://registry.npmjs.org/internmap/-/internmap-2.0.3.tgz",
+      "integrity": "sha512-5Hh7Y1wQbvY5ooGgPbDaL5iYLAPzMTUrjMulskHLH6wnv/A+1q5rgEaiuqEjB+oxGXIVZs1FF+R/KPN3ZSQYYg==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
+    "node_modules/intersection-observer": {
+      "version": "0.12.2",
+      "resolved": "https://registry.npmjs.org/intersection-observer/-/intersection-observer-0.12.2.tgz",
+      "integrity": "sha512-7m1vEcPCxXYI8HqnL8CKI6siDyD+eIWSwgB3DZA+ZTogxk9I4CDnj4wilt9x/+/QbHI4YG5YZNmC6458/e9Ktg=="
+    },
+    "node_modules/is-alphabetical": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/is-alphabetical/-/is-alphabetical-2.0.1.tgz",
+      "integrity": "sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/is-alphanumerical": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/is-alphanumerical/-/is-alphanumerical-2.0.1.tgz",
+      "integrity": "sha512-hmbYhX/9MUMF5uh7tOXyK/n0ZvWpad5caBA17GsC6vyuCqaWliRG5K1qS9inmUhEMaOBIW7/whAnSwveW/LtZw==",
+      "dependencies": {
+        "is-alphabetical": "^2.0.0",
+        "is-decimal": "^2.0.0"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/is-buffer": {
+      "version": "2.0.5",
+      "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz",
+      "integrity": "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==",
+      "funding": [
+        {
+          "type": "github",
+          "url": "https://github.com/sponsors/feross"
+        },
+        {
+          "type": "patreon",
+          "url": "https://www.patreon.com/feross"
+        },
+        {
+          "type": "consulting",
+          "url": "https://feross.org/support"
+        }
+      ],
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/is-decimal": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/is-decimal/-/is-decimal-2.0.1.tgz",
+      "integrity": "sha512-AAB9hiomQs5DXWcRB1rqsxGUstbRroFOPPVAomNk/3XHR5JyEZChOyTWe2oayKnsSsr/kcGqF+z6yuH6HHpN0A==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/is-extendable": {
+      "version": "0.1.1",
+      "resolved": "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz",
+      "integrity": "sha512-5BMULNob1vgFX6EjQw5izWDxrecWK9AM72rugNr0TFldMOi0fj6Jk+zeKIt0xGj4cEfQIJth4w3OKWOJ4f+AFw==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/is-hexadecimal": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/is-hexadecimal/-/is-hexadecimal-2.0.1.tgz",
+      "integrity": "sha512-DgZQp241c8oO6cA1SbTEWiXeoxV42vlcJxgH+B3hi1AiqqKruZR3ZGF8In3fj4+/y/7rHvlOZLZtgJ/4ttYGZg==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/is-obj": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-3.0.0.tgz",
+      "integrity": "sha512-IlsXEHOjtKhpN8r/tRFj2nDyTmHvcfNeu/nrRIcXE17ROeatXchkojffa1SpdqW4cr/Fj6QkEf/Gn4zf6KKvEQ==",
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/is-plain-obj": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-3.0.0.tgz",
+      "integrity": "sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==",
+      "engines": {
+        "node": ">=10"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/is-reference": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-3.0.2.tgz",
+      "integrity": "sha512-v3rht/LgVcsdZa3O2Nqs+NMowLOxeOm7Ay9+/ARQ2F+qEoANRcqrjAZKGN0v8ymUetZGgkp26LTnGT7H0Qo9Pg==",
+      "dependencies": {
+        "@types/estree": "*"
+      }
+    },
+    "node_modules/is-ssh": {
+      "version": "1.4.0",
+      "resolved": "https://registry.npmjs.org/is-ssh/-/is-ssh-1.4.0.tgz",
+      "integrity": "sha512-x7+VxdxOdlV3CYpjvRLBv5Lo9OJerlYanjwFrPR9fuGPjCiNiCzFgAWpiLAohSbsnH4ZAys3SBh+hq5rJosxUQ==",
+      "dependencies": {
+        "protocols": "^2.0.1"
+      }
+    },
+    "node_modules/is-stream": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz",
+      "integrity": "sha512-uQPm8kcs47jx38atAcWTVxyltQYoPT68y9aWYdV6yWXSyW8mzSat0TL6CiWdZeCdF3KrAvpVtnHbTv4RN+rqdQ==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/isexe": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
+      "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw=="
+    },
+    "node_modules/js-tokens": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
+      "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="
+    },
+    "node_modules/js-yaml": {
+      "version": "3.14.1",
+      "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz",
+      "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==",
+      "dependencies": {
+        "argparse": "^1.0.7",
+        "esprima": "^4.0.0"
+      },
+      "bin": {
+        "js-yaml": "bin/js-yaml.js"
+      }
+    },
+    "node_modules/jsonc-parser": {
+      "version": "3.2.1",
+      "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.1.tgz",
+      "integrity": "sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA=="
+    },
+    "node_modules/katex": {
+      "version": "0.16.9",
+      "resolved": "https://registry.npmjs.org/katex/-/katex-0.16.9.tgz",
+      "integrity": "sha512-fsSYjWS0EEOwvy81j3vRA8TEAhQhKiqO+FQaKWp0m39qwOzHVBgAUBIXWj1pB+O2W3fIpNa6Y9KSKCVbfPhyAQ==",
+      "funding": [
+        "https://opencollective.com/katex",
+        "https://github.com/sponsors/katex"
+      ],
+      "dependencies": {
+        "commander": "^8.3.0"
+      },
+      "bin": {
+        "katex": "cli.js"
+      }
+    },
+    "node_modules/khroma": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/khroma/-/khroma-2.1.0.tgz",
+      "integrity": "sha512-Ls993zuzfayK269Svk9hzpeGUKob/sIgZzyHYdjQoAdQetRKpOLj+k/QQQ/6Qi0Yz65mlROrfd+Ev+1+7dz9Kw=="
+    },
+    "node_modules/kind-of": {
+      "version": "6.0.3",
+      "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz",
+      "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/kleur": {
+      "version": "4.1.5",
+      "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz",
+      "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==",
+      "engines": {
+        "node": ">=6"
+      }
+    },
+    "node_modules/layout-base": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/layout-base/-/layout-base-1.0.2.tgz",
+      "integrity": "sha512-8h2oVEZNktL4BH2JCOI90iD1yXwL6iNW7KcCKT2QZgQJR2vbqDsldCTPRU9NifTCqHZci57XvQQ15YTu+sTYPg=="
+    },
+    "node_modules/lodash": {
+      "version": "4.17.21",
+      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
+      "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
+    },
+    "node_modules/lodash-es": {
+      "version": "4.17.21",
+      "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz",
+      "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw=="
+    },
+    "node_modules/lodash.get": {
+      "version": "4.4.2",
+      "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
+      "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ=="
+    },
+    "node_modules/longest-streak": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/longest-streak/-/longest-streak-3.1.0.tgz",
+      "integrity": "sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/loose-envify": {
+      "version": "1.4.0",
+      "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
+      "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",
+      "dependencies": {
+        "js-tokens": "^3.0.0 || ^4.0.0"
+      },
+      "bin": {
+        "loose-envify": "cli.js"
+      }
+    },
+    "node_modules/lru-cache": {
+      "version": "4.1.5",
+      "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz",
+      "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==",
+      "dependencies": {
+        "pseudomap": "^1.0.2",
+        "yallist": "^2.1.2"
+      }
+    },
+    "node_modules/markdown-extensions": {
+      "version": "1.1.1",
+      "resolved": "https://registry.npmjs.org/markdown-extensions/-/markdown-extensions-1.1.1.tgz",
+      "integrity": "sha512-WWC0ZuMzCyDHYCasEGs4IPvLyTGftYwh6wIEOULOF0HXcqZlhwRzrK0w2VUlxWA98xnvb/jszw4ZSkJ6ADpM6Q==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/markdown-table": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.3.tgz",
+      "integrity": "sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/match-sorter": {
+      "version": "6.3.4",
+      "resolved": "https://registry.npmjs.org/match-sorter/-/match-sorter-6.3.4.tgz",
+      "integrity": "sha512-jfZW7cWS5y/1xswZo8VBOdudUiSd9nifYRWphc9M5D/ee4w4AoXLgBEdRbgVaxbMuagBPeUC5y2Hi8DO6o9aDg==",
+      "dependencies": {
+        "@babel/runtime": "^7.23.8",
+        "remove-accents": "0.5.0"
+      }
+    },
+    "node_modules/mdast-util-definitions": {
+      "version": "5.1.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-definitions/-/mdast-util-definitions-5.1.2.tgz",
+      "integrity": "sha512-8SVPMuHqlPME/z3gqVwWY4zVXn8lqKv/pAhC57FuJ40ImXyBpmO5ukh98zB2v7Blql2FiHjHv9LVztSIqjY+MA==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "@types/unist": "^2.0.0",
+        "unist-util-visit": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-definitions/node_modules/unist-util-visit": {
+      "version": "4.1.2",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-4.1.2.tgz",
+      "integrity": "sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^5.1.1"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-definitions/node_modules/unist-util-visit-parents": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz",
+      "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-find-and-replace": {
+      "version": "2.2.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-find-and-replace/-/mdast-util-find-and-replace-2.2.2.tgz",
+      "integrity": "sha512-MTtdFRz/eMDHXzeK6W3dO7mXUlF82Gom4y0oOgvHhh/HXZAGvIQDUvQ0SuUx+j2tv44b8xTHOm8K/9OoRFnXKw==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "escape-string-regexp": "^5.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-find-and-replace/node_modules/unist-util-visit-parents": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz",
+      "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-from-markdown": {
+      "version": "1.3.1",
+      "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-1.3.1.tgz",
+      "integrity": "sha512-4xTO/M8c82qBcnQc1tgpNtubGUW/Y1tBQ1B0i5CtSoelOLKFYlElIr3bvgREYYO5iRqbMY1YuqZng0GVOI8Qww==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "@types/unist": "^2.0.0",
+        "decode-named-character-reference": "^1.0.0",
+        "mdast-util-to-string": "^3.1.0",
+        "micromark": "^3.0.0",
+        "micromark-util-decode-numeric-character-reference": "^1.0.0",
+        "micromark-util-decode-string": "^1.0.0",
+        "micromark-util-normalize-identifier": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "unist-util-stringify-position": "^3.0.0",
+        "uvu": "^0.5.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-gfm": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-gfm/-/mdast-util-gfm-2.0.2.tgz",
+      "integrity": "sha512-qvZ608nBppZ4icQlhQQIAdc6S3Ffj9RGmzwUKUWuEICFnd1LVkN3EktF7ZHAgfcEdvZB5owU9tQgt99e2TlLjg==",
+      "dependencies": {
+        "mdast-util-from-markdown": "^1.0.0",
+        "mdast-util-gfm-autolink-literal": "^1.0.0",
+        "mdast-util-gfm-footnote": "^1.0.0",
+        "mdast-util-gfm-strikethrough": "^1.0.0",
+        "mdast-util-gfm-table": "^1.0.0",
+        "mdast-util-gfm-task-list-item": "^1.0.0",
+        "mdast-util-to-markdown": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-gfm-autolink-literal": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/mdast-util-gfm-autolink-literal/-/mdast-util-gfm-autolink-literal-1.0.3.tgz",
+      "integrity": "sha512-My8KJ57FYEy2W2LyNom4n3E7hKTuQk/0SES0u16tjA9Z3oFkF4RrC/hPAPgjlSpezsOvI8ObcXcElo92wn5IGA==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "ccount": "^2.0.0",
+        "mdast-util-find-and-replace": "^2.0.0",
+        "micromark-util-character": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-gfm-footnote": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-gfm-footnote/-/mdast-util-gfm-footnote-1.0.2.tgz",
+      "integrity": "sha512-56D19KOGbE00uKVj3sgIykpwKL179QsVFwx/DCW0u/0+URsryacI4MAdNJl0dh+u2PSsD9FtxPFbHCzJ78qJFQ==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "mdast-util-to-markdown": "^1.3.0",
+        "micromark-util-normalize-identifier": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-gfm-strikethrough": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/mdast-util-gfm-strikethrough/-/mdast-util-gfm-strikethrough-1.0.3.tgz",
+      "integrity": "sha512-DAPhYzTYrRcXdMjUtUjKvW9z/FNAMTdU0ORyMcbmkwYNbKocDpdk+PX1L1dQgOID/+vVs1uBQ7ElrBQfZ0cuiQ==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "mdast-util-to-markdown": "^1.3.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-gfm-table": {
+      "version": "1.0.7",
+      "resolved": "https://registry.npmjs.org/mdast-util-gfm-table/-/mdast-util-gfm-table-1.0.7.tgz",
+      "integrity": "sha512-jjcpmNnQvrmN5Vx7y7lEc2iIOEytYv7rTvu+MeyAsSHTASGCCRA79Igg2uKssgOs1i1po8s3plW0sTu1wkkLGg==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "markdown-table": "^3.0.0",
+        "mdast-util-from-markdown": "^1.0.0",
+        "mdast-util-to-markdown": "^1.3.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-gfm-task-list-item": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-gfm-task-list-item/-/mdast-util-gfm-task-list-item-1.0.2.tgz",
+      "integrity": "sha512-PFTA1gzfp1B1UaiJVyhJZA1rm0+Tzn690frc/L8vNX1Jop4STZgOE6bxUhnzdVSB+vm2GU1tIsuQcA9bxTQpMQ==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "mdast-util-to-markdown": "^1.3.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-math": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-math/-/mdast-util-math-2.0.2.tgz",
+      "integrity": "sha512-8gmkKVp9v6+Tgjtq6SYx9kGPpTf6FVYRa53/DLh479aldR9AyP48qeVOgNZ5X7QUK7nOy4yw7vg6mbiGcs9jWQ==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "longest-streak": "^3.0.0",
+        "mdast-util-to-markdown": "^1.3.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/mdast-util-mdx/-/mdast-util-mdx-2.0.1.tgz",
+      "integrity": "sha512-38w5y+r8nyKlGvNjSEqWrhG0w5PmnRA+wnBvm+ulYCct7nsGYhFVb0lljS9bQav4psDAS1eGkP2LMVcZBi/aqw==",
+      "dependencies": {
+        "mdast-util-from-markdown": "^1.0.0",
+        "mdast-util-mdx-expression": "^1.0.0",
+        "mdast-util-mdx-jsx": "^2.0.0",
+        "mdast-util-mdxjs-esm": "^1.0.0",
+        "mdast-util-to-markdown": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx-expression": {
+      "version": "1.3.2",
+      "resolved": "https://registry.npmjs.org/mdast-util-mdx-expression/-/mdast-util-mdx-expression-1.3.2.tgz",
+      "integrity": "sha512-xIPmR5ReJDu/DHH1OoIT1HkuybIfRGYRywC+gJtI7qHjCJp/M9jrmBEJW22O8lskDWm562BX2W8TiAwRTb0rKA==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "@types/hast": "^2.0.0",
+        "@types/mdast": "^3.0.0",
+        "mdast-util-from-markdown": "^1.0.0",
+        "mdast-util-to-markdown": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx-jsx": {
+      "version": "2.1.4",
+      "resolved": "https://registry.npmjs.org/mdast-util-mdx-jsx/-/mdast-util-mdx-jsx-2.1.4.tgz",
+      "integrity": "sha512-DtMn9CmVhVzZx3f+optVDF8yFgQVt7FghCRNdlIaS3X5Bnym3hZwPbg/XW86vdpKjlc1PVj26SpnLGeJBXD3JA==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "@types/hast": "^2.0.0",
+        "@types/mdast": "^3.0.0",
+        "@types/unist": "^2.0.0",
+        "ccount": "^2.0.0",
+        "mdast-util-from-markdown": "^1.1.0",
+        "mdast-util-to-markdown": "^1.3.0",
+        "parse-entities": "^4.0.0",
+        "stringify-entities": "^4.0.0",
+        "unist-util-remove-position": "^4.0.0",
+        "unist-util-stringify-position": "^3.0.0",
+        "vfile-message": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx-jsx/node_modules/unist-util-remove-position": {
+      "version": "4.0.2",
+      "resolved": "https://registry.npmjs.org/unist-util-remove-position/-/unist-util-remove-position-4.0.2.tgz",
+      "integrity": "sha512-TkBb0HABNmxzAcfLf4qsIbFbaPDvMO6wa3b3j4VcEzFVaw1LBKwnW4/sRJ/atSLSzoIg41JWEdnE7N6DIhGDGQ==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-visit": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx-jsx/node_modules/unist-util-visit": {
+      "version": "4.1.2",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-4.1.2.tgz",
+      "integrity": "sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^5.1.1"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx-jsx/node_modules/unist-util-visit-parents": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz",
+      "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdx-jsx/node_modules/vfile-message": {
+      "version": "3.1.4",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-3.1.4.tgz",
+      "integrity": "sha512-fa0Z6P8HUrQN4BZaX05SIVXic+7kE3b05PWAtPuYP9QLHsLKYR7/AlLW3NtOrpXRLeawpDLMsVkmk5DG0NXgWw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-mdxjs-esm": {
+      "version": "1.3.1",
+      "resolved": "https://registry.npmjs.org/mdast-util-mdxjs-esm/-/mdast-util-mdxjs-esm-1.3.1.tgz",
+      "integrity": "sha512-SXqglS0HrEvSdUEfoXFtcg7DRl7S2cwOXc7jkuusG472Mmjag34DUDeOJUZtl+BVnyeO1frIgVpHlNRWc2gk/w==",
+      "dependencies": {
+        "@types/estree-jsx": "^1.0.0",
+        "@types/hast": "^2.0.0",
+        "@types/mdast": "^3.0.0",
+        "mdast-util-from-markdown": "^1.0.0",
+        "mdast-util-to-markdown": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-phrasing": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/mdast-util-phrasing/-/mdast-util-phrasing-3.0.1.tgz",
+      "integrity": "sha512-WmI1gTXUBJo4/ZmSk79Wcb2HcjPJBzM1nlI/OUWA8yk2X9ik3ffNbBGsU+09BFmXaL1IBb9fiuvq6/KMiNycSg==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-hast": {
+      "version": "13.1.0",
+      "resolved": "https://registry.npmjs.org/mdast-util-to-hast/-/mdast-util-to-hast-13.1.0.tgz",
+      "integrity": "sha512-/e2l/6+OdGp/FB+ctrJ9Avz71AN/GRH3oi/3KAx/kMnoUsD6q0woXlDT8lLEeViVKE7oZxE7RXzvO3T8kF2/sA==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "@types/mdast": "^4.0.0",
+        "@ungap/structured-clone": "^1.0.0",
+        "devlop": "^1.0.0",
+        "micromark-util-sanitize-uri": "^2.0.0",
+        "trim-lines": "^3.0.0",
+        "unist-util-position": "^5.0.0",
+        "unist-util-visit": "^5.0.0",
+        "vfile": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/@types/mdast": {
+      "version": "4.0.3",
+      "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.3.tgz",
+      "integrity": "sha512-LsjtqsyF+d2/yFOYaN22dHZI1Cpwkrj+g06G8+qtUKlhovPW89YhqSnfKtMbkgmEtYpH2gydRNULd6y8mciAFg==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/mdast-util-to-hast/node_modules/micromark-util-character": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-character/-/micromark-util-character-2.1.0.tgz",
+      "integrity": "sha512-KvOVV+X1yLBfs9dCBSopq/+G1PcgT3lAK07mC4BzXi5E7ahzMAF8oIupDDJ6mievI6F+lAATkbQQlQixJfT3aQ==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-symbol": "^2.0.0",
+        "micromark-util-types": "^2.0.0"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/micromark-util-encode": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-encode/-/micromark-util-encode-2.0.0.tgz",
+      "integrity": "sha512-pS+ROfCXAGLWCOc8egcBvT0kf27GoWMqtdarNfDcjb6YLuV5cM3ioG45Ys2qOVqeqSbjaKg72vU+Wby3eddPsA==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/mdast-util-to-hast/node_modules/micromark-util-sanitize-uri": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-sanitize-uri/-/micromark-util-sanitize-uri-2.0.0.tgz",
+      "integrity": "sha512-WhYv5UEcZrbAtlsnPuChHUAsu/iBPOVaEVsntLBIdpibO0ddy8OzavZz3iL2xVvBZOpolujSliP65Kq0/7KIYw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-character": "^2.0.0",
+        "micromark-util-encode": "^2.0.0",
+        "micromark-util-symbol": "^2.0.0"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/micromark-util-symbol": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-symbol/-/micromark-util-symbol-2.0.0.tgz",
+      "integrity": "sha512-8JZt9ElZ5kyTnO94muPxIGS8oyElRJaiJO8EzV6ZSyGQ1Is8xwl4Q45qU5UOg+bGH4AikWziz0iN4sFLWs8PGw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/mdast-util-to-hast/node_modules/micromark-util-types": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-types/-/micromark-util-types-2.0.0.tgz",
+      "integrity": "sha512-oNh6S2WMHWRZrmutsRmDDfkzKtxF+bc2VxLC9dvtrDIRFln627VsFP6fLMgTryGDljgLPjkrzQSDcPrjPyDJ5w==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/mdast-util-to-hast/node_modules/unist-util-position": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-position/-/unist-util-position-5.0.0.tgz",
+      "integrity": "sha512-fucsC7HjXvkB5R3kTCO7kUjRdrS0BJt3M/FPxmHMBOm8JQi2BsHAHFsy27E0EolP8rp0NzXsJ+jNPyDWvOJZPA==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-hast/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-markdown": {
+      "version": "1.5.0",
+      "resolved": "https://registry.npmjs.org/mdast-util-to-markdown/-/mdast-util-to-markdown-1.5.0.tgz",
+      "integrity": "sha512-bbv7TPv/WC49thZPg3jXuqzuvI45IL2EVAr/KxF0BSdHsU0ceFHOmwQn6evxAh1GaoK/6GQ1wp4R4oW2+LFL/A==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "@types/unist": "^2.0.0",
+        "longest-streak": "^3.0.0",
+        "mdast-util-phrasing": "^3.0.0",
+        "mdast-util-to-string": "^3.0.0",
+        "micromark-util-decode-string": "^1.0.0",
+        "unist-util-visit": "^4.0.0",
+        "zwitch": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-markdown/node_modules/unist-util-visit": {
+      "version": "4.1.2",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-4.1.2.tgz",
+      "integrity": "sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^5.1.1"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-markdown/node_modules/unist-util-visit-parents": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz",
+      "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mdast-util-to-string": {
+      "version": "3.2.0",
+      "resolved": "https://registry.npmjs.org/mdast-util-to-string/-/mdast-util-to-string-3.2.0.tgz",
+      "integrity": "sha512-V4Zn/ncyN1QNSqSBxTrMOLpjr+IKdHl2v3KVLoWmDPscP4r9GcCi71gjgvUV1SFSKh92AjAG4peFuBl2/YgCJg==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/mermaid": {
+      "version": "10.9.0",
+      "resolved": "https://registry.npmjs.org/mermaid/-/mermaid-10.9.0.tgz",
+      "integrity": "sha512-swZju0hFox/B/qoLKK0rOxxgh8Cf7rJSfAUc1u8fezVihYMvrJAS45GzAxTVf4Q+xn9uMgitBcmWk7nWGXOs/g==",
+      "dependencies": {
+        "@braintree/sanitize-url": "^6.0.1",
+        "@types/d3-scale": "^4.0.3",
+        "@types/d3-scale-chromatic": "^3.0.0",
+        "cytoscape": "^3.28.1",
+        "cytoscape-cose-bilkent": "^4.1.0",
+        "d3": "^7.4.0",
+        "d3-sankey": "^0.12.3",
+        "dagre-d3-es": "7.0.10",
+        "dayjs": "^1.11.7",
+        "dompurify": "^3.0.5",
+        "elkjs": "^0.9.0",
+        "katex": "^0.16.9",
+        "khroma": "^2.0.0",
+        "lodash-es": "^4.17.21",
+        "mdast-util-from-markdown": "^1.3.0",
+        "non-layered-tidy-tree-layout": "^2.0.2",
+        "stylis": "^4.1.3",
+        "ts-dedent": "^2.2.0",
+        "uuid": "^9.0.0",
+        "web-worker": "^1.2.0"
+      }
+    },
+    "node_modules/micromark": {
+      "version": "3.2.0",
+      "resolved": "https://registry.npmjs.org/micromark/-/micromark-3.2.0.tgz",
+      "integrity": "sha512-uD66tJj54JLYq0De10AhWycZWGQNUvDI55xPgk2sQM5kn1JYlhbCMTtEeT27+vAhW2FBQxLlOmS3pmA7/2z4aA==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "@types/debug": "^4.0.0",
+        "debug": "^4.0.0",
+        "decode-named-character-reference": "^1.0.0",
+        "micromark-core-commonmark": "^1.0.1",
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-chunked": "^1.0.0",
+        "micromark-util-combine-extensions": "^1.0.0",
+        "micromark-util-decode-numeric-character-reference": "^1.0.0",
+        "micromark-util-encode": "^1.0.0",
+        "micromark-util-normalize-identifier": "^1.0.0",
+        "micromark-util-resolve-all": "^1.0.0",
+        "micromark-util-sanitize-uri": "^1.0.0",
+        "micromark-util-subtokenize": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.1",
+        "uvu": "^0.5.0"
+      }
+    },
+    "node_modules/micromark-core-commonmark": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-core-commonmark/-/micromark-core-commonmark-1.1.0.tgz",
+      "integrity": "sha512-BgHO1aRbolh2hcrzL2d1La37V0Aoz73ymF8rAcKnohLy93titmv62E0gP8Hrx9PKcKrqCZ1BbLGbP3bEhoXYlw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "decode-named-character-reference": "^1.0.0",
+        "micromark-factory-destination": "^1.0.0",
+        "micromark-factory-label": "^1.0.0",
+        "micromark-factory-space": "^1.0.0",
+        "micromark-factory-title": "^1.0.0",
+        "micromark-factory-whitespace": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-chunked": "^1.0.0",
+        "micromark-util-classify-character": "^1.0.0",
+        "micromark-util-html-tag-name": "^1.0.0",
+        "micromark-util-normalize-identifier": "^1.0.0",
+        "micromark-util-resolve-all": "^1.0.0",
+        "micromark-util-subtokenize": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.1",
+        "uvu": "^0.5.0"
+      }
+    },
+    "node_modules/micromark-extension-gfm": {
+      "version": "2.0.3",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm/-/micromark-extension-gfm-2.0.3.tgz",
+      "integrity": "sha512-vb9OoHqrhCmbRidQv/2+Bc6pkP0FrtlhurxZofvOEy5o8RtuuvTq+RQ1Vw5ZDNrVraQZu3HixESqbG+0iKk/MQ==",
+      "dependencies": {
+        "micromark-extension-gfm-autolink-literal": "^1.0.0",
+        "micromark-extension-gfm-footnote": "^1.0.0",
+        "micromark-extension-gfm-strikethrough": "^1.0.0",
+        "micromark-extension-gfm-table": "^1.0.0",
+        "micromark-extension-gfm-tagfilter": "^1.0.0",
+        "micromark-extension-gfm-task-list-item": "^1.0.0",
+        "micromark-util-combine-extensions": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-gfm-autolink-literal": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm-autolink-literal/-/micromark-extension-gfm-autolink-literal-1.0.5.tgz",
+      "integrity": "sha512-z3wJSLrDf8kRDOh2qBtoTRD53vJ+CWIyo7uyZuxf/JAbNJjiHsOpG1y5wxk8drtv3ETAHutCu6N3thkOOgueWg==",
+      "dependencies": {
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-sanitize-uri": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-gfm-footnote": {
+      "version": "1.1.2",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm-footnote/-/micromark-extension-gfm-footnote-1.1.2.tgz",
+      "integrity": "sha512-Yxn7z7SxgyGWRNa4wzf8AhYYWNrwl5q1Z8ii+CSTTIqVkmGZF1CElX2JI8g5yGoM3GAman9/PVCUFUSJ0kB/8Q==",
+      "dependencies": {
+        "micromark-core-commonmark": "^1.0.0",
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-normalize-identifier": "^1.0.0",
+        "micromark-util-sanitize-uri": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-gfm-strikethrough": {
+      "version": "1.0.7",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm-strikethrough/-/micromark-extension-gfm-strikethrough-1.0.7.tgz",
+      "integrity": "sha512-sX0FawVE1o3abGk3vRjOH50L5TTLr3b5XMqnP9YDRb34M0v5OoZhG+OHFz1OffZ9dlwgpTBKaT4XW/AsUVnSDw==",
+      "dependencies": {
+        "micromark-util-chunked": "^1.0.0",
+        "micromark-util-classify-character": "^1.0.0",
+        "micromark-util-resolve-all": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-gfm-table": {
+      "version": "1.0.7",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm-table/-/micromark-extension-gfm-table-1.0.7.tgz",
+      "integrity": "sha512-3ZORTHtcSnMQEKtAOsBQ9/oHp9096pI/UvdPtN7ehKvrmZZ2+bbWhi0ln+I9drmwXMt5boocn6OlwQzNXeVeqw==",
+      "dependencies": {
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-gfm-tagfilter": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm-tagfilter/-/micromark-extension-gfm-tagfilter-1.0.2.tgz",
+      "integrity": "sha512-5XWB9GbAUSHTn8VPU8/1DBXMuKYT5uOgEjJb8gN3mW0PNW5OPHpSdojoqf+iq1xo7vWzw/P8bAHY0n6ijpXF7g==",
+      "dependencies": {
+        "micromark-util-types": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-gfm-task-list-item": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/micromark-extension-gfm-task-list-item/-/micromark-extension-gfm-task-list-item-1.0.5.tgz",
+      "integrity": "sha512-RMFXl2uQ0pNQy6Lun2YBYT9g9INXtWJULgbt01D/x8/6yJ2qpKyzdZD3pi6UIkzF++Da49xAelVKUeUMqd5eIQ==",
+      "dependencies": {
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-math": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/micromark-extension-math/-/micromark-extension-math-2.1.2.tgz",
+      "integrity": "sha512-es0CcOV89VNS9wFmyn+wyFTKweXGW4CEvdaAca6SWRWPyYCbBisnjaHLjWO4Nszuiud84jCpkHsqAJoa768Pvg==",
+      "dependencies": {
+        "@types/katex": "^0.16.0",
+        "katex": "^0.16.0",
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-mdx-expression": {
+      "version": "1.0.8",
+      "resolved": "https://registry.npmjs.org/micromark-extension-mdx-expression/-/micromark-extension-mdx-expression-1.0.8.tgz",
+      "integrity": "sha512-zZpeQtc5wfWKdzDsHRBY003H2Smg+PUi2REhqgIhdzAa5xonhP03FcXxqFSerFiNUr5AWmHpaNPQTBVOS4lrXw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "@types/estree": "^1.0.0",
+        "micromark-factory-mdx-expression": "^1.0.0",
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-events-to-acorn": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      }
+    },
+    "node_modules/micromark-extension-mdx-jsx": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/micromark-extension-mdx-jsx/-/micromark-extension-mdx-jsx-1.0.5.tgz",
+      "integrity": "sha512-gPH+9ZdmDflbu19Xkb8+gheqEDqkSpdCEubQyxuz/Hn8DOXiXvrXeikOoBA71+e8Pfi0/UYmU3wW3H58kr7akA==",
+      "dependencies": {
+        "@types/acorn": "^4.0.0",
+        "@types/estree": "^1.0.0",
+        "estree-util-is-identifier-name": "^2.0.0",
+        "micromark-factory-mdx-expression": "^1.0.0",
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0",
+        "vfile-message": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-mdx-jsx/node_modules/vfile-message": {
+      "version": "3.1.4",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-3.1.4.tgz",
+      "integrity": "sha512-fa0Z6P8HUrQN4BZaX05SIVXic+7kE3b05PWAtPuYP9QLHsLKYR7/AlLW3NtOrpXRLeawpDLMsVkmk5DG0NXgWw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-mdx-md": {
+      "version": "1.0.1",
+      "resolved": "https://registry.npmjs.org/micromark-extension-mdx-md/-/micromark-extension-mdx-md-1.0.1.tgz",
+      "integrity": "sha512-7MSuj2S7xjOQXAjjkbjBsHkMtb+mDGVW6uI2dBL9snOBCbZmoNgDAeZ0nSn9j3T42UE/g2xVNMn18PJxZvkBEA==",
+      "dependencies": {
+        "micromark-util-types": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-mdxjs": {
+      "version": "1.0.1",
+      "resolved": "https://registry.npmjs.org/micromark-extension-mdxjs/-/micromark-extension-mdxjs-1.0.1.tgz",
+      "integrity": "sha512-7YA7hF6i5eKOfFUzZ+0z6avRG52GpWR8DL+kN47y3f2KhxbBZMhmxe7auOeaTBrW2DenbbZTf1ea9tA2hDpC2Q==",
+      "dependencies": {
+        "acorn": "^8.0.0",
+        "acorn-jsx": "^5.0.0",
+        "micromark-extension-mdx-expression": "^1.0.0",
+        "micromark-extension-mdx-jsx": "^1.0.0",
+        "micromark-extension-mdx-md": "^1.0.0",
+        "micromark-extension-mdxjs-esm": "^1.0.0",
+        "micromark-util-combine-extensions": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-mdxjs-esm": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/micromark-extension-mdxjs-esm/-/micromark-extension-mdxjs-esm-1.0.5.tgz",
+      "integrity": "sha512-xNRBw4aoURcyz/S69B19WnZAkWJMxHMT5hE36GtDAyhoyn/8TuAeqjFJQlwk+MKQsUD7b3l7kFX+vlfVWgcX1w==",
+      "dependencies": {
+        "@types/estree": "^1.0.0",
+        "micromark-core-commonmark": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-events-to-acorn": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "unist-util-position-from-estree": "^1.1.0",
+        "uvu": "^0.5.0",
+        "vfile-message": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-extension-mdxjs-esm/node_modules/vfile-message": {
+      "version": "3.1.4",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-3.1.4.tgz",
+      "integrity": "sha512-fa0Z6P8HUrQN4BZaX05SIVXic+7kE3b05PWAtPuYP9QLHsLKYR7/AlLW3NtOrpXRLeawpDLMsVkmk5DG0NXgWw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-factory-destination": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-factory-destination/-/micromark-factory-destination-1.1.0.tgz",
+      "integrity": "sha512-XaNDROBgx9SgSChd69pjiGKbV+nfHGDPVYFs5dOoDd7ZnMAE+Cuu91BCpsY8RT2NP9vo/B8pds2VQNCLiu0zhg==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-factory-label": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-factory-label/-/micromark-factory-label-1.1.0.tgz",
+      "integrity": "sha512-OLtyez4vZo/1NjxGhcpDSbHQ+m0IIGnT8BoPamh+7jVlzLJBH98zzuCoUeMxvM6WsNeh8wx8cKvqLiPHEACn0w==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      }
+    },
+    "node_modules/micromark-factory-mdx-expression": {
+      "version": "1.0.9",
+      "resolved": "https://registry.npmjs.org/micromark-factory-mdx-expression/-/micromark-factory-mdx-expression-1.0.9.tgz",
+      "integrity": "sha512-jGIWzSmNfdnkJq05c7b0+Wv0Kfz3NJ3N4cBjnbO4zjXIlxJr+f8lk+5ZmwFvqdAbUy2q6B5rCY//g0QAAaXDWA==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "@types/estree": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-events-to-acorn": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "unist-util-position-from-estree": "^1.0.0",
+        "uvu": "^0.5.0",
+        "vfile-message": "^3.0.0"
+      }
+    },
+    "node_modules/micromark-factory-mdx-expression/node_modules/vfile-message": {
+      "version": "3.1.4",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-3.1.4.tgz",
+      "integrity": "sha512-fa0Z6P8HUrQN4BZaX05SIVXic+7kE3b05PWAtPuYP9QLHsLKYR7/AlLW3NtOrpXRLeawpDLMsVkmk5DG0NXgWw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-factory-space": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-factory-space/-/micromark-factory-space-1.1.0.tgz",
+      "integrity": "sha512-cRzEj7c0OL4Mw2v6nwzttyOZe8XY/Z8G0rzmWQZTBi/jjwyw/U4uqKtUORXQrR5bAZZnbTI/feRV/R7hc4jQYQ==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-factory-title": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-factory-title/-/micromark-factory-title-1.1.0.tgz",
+      "integrity": "sha512-J7n9R3vMmgjDOCY8NPw55jiyaQnH5kBdV2/UXCtZIpnHH3P6nHUKaH7XXEYuWwx/xUJcawa8plLBEjMPU24HzQ==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-factory-whitespace": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-factory-whitespace/-/micromark-factory-whitespace-1.1.0.tgz",
+      "integrity": "sha512-v2WlmiymVSp5oMg+1Q0N1Lxmt6pMhIHD457whWM7/GUlEks1hI9xj5w3zbc4uuMKXGisksZk8DzP2UyGbGqNsQ==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-factory-space": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-character": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-character/-/micromark-util-character-1.2.0.tgz",
+      "integrity": "sha512-lXraTwcX3yH/vMDaFWCQJP1uIszLVebzUa3ZHdrgxr7KEU/9mL4mVgCpGbyhvNLNlauROiNUq7WN5u7ndbY6xg==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-chunked": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-chunked/-/micromark-util-chunked-1.1.0.tgz",
+      "integrity": "sha512-Ye01HXpkZPNcV6FiyoW2fGZDUw4Yc7vT0E9Sad83+bEDiCJ1uXu0S3mr8WLpsz3HaG3x2q0HM6CTuPdcZcluFQ==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-symbol": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-classify-character": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-classify-character/-/micromark-util-classify-character-1.1.0.tgz",
+      "integrity": "sha512-SL0wLxtKSnklKSUplok1WQFoGhUdWYKggKUiqhX+Swala+BtptGCu5iPRc+xvzJ4PXE/hwM3FNXsfEVgoZsWbw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-combine-extensions": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-combine-extensions/-/micromark-util-combine-extensions-1.1.0.tgz",
+      "integrity": "sha512-Q20sp4mfNf9yEqDL50WwuWZHUrCO4fEyeDCnMGmG5Pr0Cz15Uo7KBs6jq+dq0EgX4DPwwrh9m0X+zPV1ypFvUA==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-chunked": "^1.0.0",
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-decode-numeric-character-reference": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-decode-numeric-character-reference/-/micromark-util-decode-numeric-character-reference-1.1.0.tgz",
+      "integrity": "sha512-m9V0ExGv0jB1OT21mrWcuf4QhP46pH1KkfWy9ZEezqHKAxkj4mPCy3nIH1rkbdMlChLHX531eOrymlwyZIf2iw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-symbol": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-decode-string": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-decode-string/-/micromark-util-decode-string-1.1.0.tgz",
+      "integrity": "sha512-YphLGCK8gM1tG1bd54azwyrQRjCFcmgj2S2GoJDNnh4vYtnL38JS8M4gpxzOPNyHdNEpheyWXCTnnTDY3N+NVQ==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "decode-named-character-reference": "^1.0.0",
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-decode-numeric-character-reference": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-encode": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-encode/-/micromark-util-encode-1.1.0.tgz",
+      "integrity": "sha512-EuEzTWSTAj9PA5GOAs992GzNh2dGQO52UvAbtSOMvXTxv3Criqb6IOzJUBCmEqrrXSblJIJBbFFv6zPxpreiJw==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/micromark-util-events-to-acorn": {
+      "version": "1.2.3",
+      "resolved": "https://registry.npmjs.org/micromark-util-events-to-acorn/-/micromark-util-events-to-acorn-1.2.3.tgz",
+      "integrity": "sha512-ij4X7Wuc4fED6UoLWkmo0xJQhsktfNh1J0m8g4PbIMPlx+ek/4YdW5mvbye8z/aZvAPUoxgXHrwVlXAPKMRp1w==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "@types/acorn": "^4.0.0",
+        "@types/estree": "^1.0.0",
+        "@types/unist": "^2.0.0",
+        "estree-util-visit": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0",
+        "vfile-message": "^3.0.0"
+      }
+    },
+    "node_modules/micromark-util-events-to-acorn/node_modules/vfile-message": {
+      "version": "3.1.4",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-3.1.4.tgz",
+      "integrity": "sha512-fa0Z6P8HUrQN4BZaX05SIVXic+7kE3b05PWAtPuYP9QLHsLKYR7/AlLW3NtOrpXRLeawpDLMsVkmk5DG0NXgWw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/micromark-util-html-tag-name": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-html-tag-name/-/micromark-util-html-tag-name-1.2.0.tgz",
+      "integrity": "sha512-VTQzcuQgFUD7yYztuQFKXT49KghjtETQ+Wv/zUjGSGBioZnkA4P1XXZPT1FHeJA6RwRXSF47yvJ1tsJdoxwO+Q==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/micromark-util-normalize-identifier": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-normalize-identifier/-/micromark-util-normalize-identifier-1.1.0.tgz",
+      "integrity": "sha512-N+w5vhqrBihhjdpM8+5Xsxy71QWqGn7HYNUvch71iV2PM7+E3uWGox1Qp90loa1ephtCxG2ftRV/Conitc6P2Q==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-symbol": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-resolve-all": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-resolve-all/-/micromark-util-resolve-all-1.1.0.tgz",
+      "integrity": "sha512-b/G6BTMSg+bX+xVCshPTPyAu2tmA0E4X98NSR7eIbeC6ycCqCeE7wjfDIgzEbkzdEVJXRtOG4FbEm/uGbCRouA==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-types": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-sanitize-uri": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-sanitize-uri/-/micromark-util-sanitize-uri-1.2.0.tgz",
+      "integrity": "sha512-QO4GXv0XZfWey4pYFndLUKEAktKkG5kZTdUNaTAkzbuJxn2tNBOr+QtxR2XpWaMhbImT2dPzyLrPXLlPhph34A==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-character": "^1.0.0",
+        "micromark-util-encode": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0"
+      }
+    },
+    "node_modules/micromark-util-subtokenize": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-subtokenize/-/micromark-util-subtokenize-1.1.0.tgz",
+      "integrity": "sha512-kUQHyzRoxvZO2PuLzMt2P/dwVsTiivCK8icYTeR+3WgbuPqfHgPPy7nFKbeqRivBvn/3N3GBiNC+JRTMSxEC7A==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ],
+      "dependencies": {
+        "micromark-util-chunked": "^1.0.0",
+        "micromark-util-symbol": "^1.0.0",
+        "micromark-util-types": "^1.0.0",
+        "uvu": "^0.5.0"
+      }
+    },
+    "node_modules/micromark-util-symbol": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-symbol/-/micromark-util-symbol-1.1.0.tgz",
+      "integrity": "sha512-uEjpEYY6KMs1g7QfJ2eX1SQEV+ZT4rUD3UcF6l57acZvLNK7PBZL+ty82Z1qhK1/yXIY4bdx04FKMgR0g4IAag==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/micromark-util-types": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/micromark-util-types/-/micromark-util-types-1.1.0.tgz",
+      "integrity": "sha512-ukRBgie8TIAcacscVHSiddHjO4k/q3pnedmzMQ4iwDcK0FtFCohKOlFbaOL/mPgfnPsL3C1ZyxJa4sbWrBl3jg==",
+      "funding": [
+        {
+          "type": "GitHub Sponsors",
+          "url": "https://github.com/sponsors/unifiedjs"
+        },
+        {
+          "type": "OpenCollective",
+          "url": "https://opencollective.com/unified"
+        }
+      ]
+    },
+    "node_modules/mri": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz",
+      "integrity": "sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==",
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/ms": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
+      "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
+    },
+    "node_modules/nanoid": {
+      "version": "3.3.7",
+      "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz",
+      "integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==",
+      "funding": [
+        {
+          "type": "github",
+          "url": "https://github.com/sponsors/ai"
+        }
+      ],
+      "bin": {
+        "nanoid": "bin/nanoid.cjs"
+      },
+      "engines": {
+        "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
+      }
+    },
+    "node_modules/next": {
+      "version": "13.4.12",
+      "resolved": "https://registry.npmjs.org/next/-/next-13.4.12.tgz",
+      "integrity": "sha512-eHfnru9x6NRmTMcjQp6Nz0J4XH9OubmzOa7CkWL+AUrUxpibub3vWwttjduu9No16dug1kq04hiUUpo7J3m3Xw==",
+      "dependencies": {
+        "@next/env": "13.4.12",
+        "@swc/helpers": "0.5.1",
+        "busboy": "1.6.0",
+        "caniuse-lite": "^1.0.30001406",
+        "postcss": "8.4.14",
+        "styled-jsx": "5.1.1",
+        "watchpack": "2.4.0",
+        "zod": "3.21.4"
+      },
+      "bin": {
+        "next": "dist/bin/next"
+      },
+      "engines": {
+        "node": ">=16.8.0"
+      },
+      "optionalDependencies": {
+        "@next/swc-darwin-arm64": "13.4.12",
+        "@next/swc-darwin-x64": "13.4.12",
+        "@next/swc-linux-arm64-gnu": "13.4.12",
+        "@next/swc-linux-arm64-musl": "13.4.12",
+        "@next/swc-linux-x64-gnu": "13.4.12",
+        "@next/swc-linux-x64-musl": "13.4.12",
+        "@next/swc-win32-arm64-msvc": "13.4.12",
+        "@next/swc-win32-ia32-msvc": "13.4.12",
+        "@next/swc-win32-x64-msvc": "13.4.12"
+      },
+      "peerDependencies": {
+        "@opentelemetry/api": "^1.1.0",
+        "fibers": ">= 3.1.0",
+        "react": "^18.2.0",
+        "react-dom": "^18.2.0",
+        "sass": "^1.3.0"
+      },
+      "peerDependenciesMeta": {
+        "@opentelemetry/api": {
+          "optional": true
+        },
+        "fibers": {
+          "optional": true
+        },
+        "sass": {
+          "optional": true
+        }
+      }
+    },
+    "node_modules/next-mdx-remote": {
+      "version": "4.4.1",
+      "resolved": "https://registry.npmjs.org/next-mdx-remote/-/next-mdx-remote-4.4.1.tgz",
+      "integrity": "sha512-1BvyXaIou6xy3XoNF4yaMZUCb6vD2GTAa5ciOa6WoO+gAUTYsb1K4rI/HSC2ogAWLrb/7VSV52skz07vOzmqIQ==",
+      "dependencies": {
+        "@mdx-js/mdx": "^2.2.1",
+        "@mdx-js/react": "^2.2.1",
+        "vfile": "^5.3.0",
+        "vfile-matter": "^3.0.1"
+      },
+      "engines": {
+        "node": ">=14",
+        "npm": ">=7"
+      },
+      "peerDependencies": {
+        "react": ">=16.x <=18.x",
+        "react-dom": ">=16.x <=18.x"
+      }
+    },
+    "node_modules/next-seo": {
+      "version": "6.5.0",
+      "resolved": "https://registry.npmjs.org/next-seo/-/next-seo-6.5.0.tgz",
+      "integrity": "sha512-MfzUeWTN/x/rsKp/1n0213eojO97lIl0unxqbeCY+6pAucViHDA8GSLRRcXpgjsSmBxfCFdfpu7LXbt4ANQoNQ==",
+      "peerDependencies": {
+        "next": "^8.1.1-canary.54 || >=9.0.0",
+        "react": ">=16.0.0",
+        "react-dom": ">=16.0.0"
+      }
+    },
+    "node_modules/next-themes": {
+      "version": "0.2.1",
+      "resolved": "https://registry.npmjs.org/next-themes/-/next-themes-0.2.1.tgz",
+      "integrity": "sha512-B+AKNfYNIzh0vqQQKqQItTS8evEouKD7H5Hj3kmuPERwddR2TxvDSFZuTj6T7Jfn1oyeUyJMydPl1Bkxkh0W7A==",
+      "peerDependencies": {
+        "next": "*",
+        "react": "*",
+        "react-dom": "*"
+      }
+    },
+    "node_modules/nextra": {
+      "version": "2.13.4",
+      "resolved": "https://registry.npmjs.org/nextra/-/nextra-2.13.4.tgz",
+      "integrity": "sha512-7of2rSBxuUa3+lbMmZwG9cqgftcoNOVQLTT6Rxf3EhBR9t1EI7b43dted8YoqSNaigdE3j1CoyNkX8N/ZzlEpw==",
+      "dependencies": {
+        "@headlessui/react": "^1.7.17",
+        "@mdx-js/mdx": "^2.3.0",
+        "@mdx-js/react": "^2.3.0",
+        "@napi-rs/simple-git": "^0.1.9",
+        "@theguild/remark-mermaid": "^0.0.5",
+        "@theguild/remark-npm2yarn": "^0.2.0",
+        "clsx": "^2.0.0",
+        "github-slugger": "^2.0.0",
+        "graceful-fs": "^4.2.11",
+        "gray-matter": "^4.0.3",
+        "katex": "^0.16.9",
+        "lodash.get": "^4.4.2",
+        "next-mdx-remote": "^4.2.1",
+        "p-limit": "^3.1.0",
+        "rehype-katex": "^7.0.0",
+        "rehype-pretty-code": "0.9.11",
+        "rehype-raw": "^7.0.0",
+        "remark-gfm": "^3.0.1",
+        "remark-math": "^5.1.1",
+        "remark-reading-time": "^2.0.1",
+        "shiki": "^0.14.3",
+        "slash": "^3.0.0",
+        "title": "^3.5.3",
+        "unist-util-remove": "^4.0.0",
+        "unist-util-visit": "^5.0.0",
+        "zod": "^3.22.3"
+      },
+      "engines": {
+        "node": ">=16"
+      },
+      "peerDependencies": {
+        "next": ">=9.5.3",
+        "react": ">=16.13.1",
+        "react-dom": ">=16.13.1"
+      }
+    },
+    "node_modules/nextra-theme-docs": {
+      "version": "2.13.4",
+      "resolved": "https://registry.npmjs.org/nextra-theme-docs/-/nextra-theme-docs-2.13.4.tgz",
+      "integrity": "sha512-2XOoMfwBCTYBt8ds4ZHftt9Wyf2XsykiNo02eir/XEYB+sGeUoE77kzqfidjEOKCSzOHYbK9BDMcg2+B/2vYRw==",
+      "dependencies": {
+        "@headlessui/react": "^1.7.17",
+        "@popperjs/core": "^2.11.8",
+        "clsx": "^2.0.0",
+        "escape-string-regexp": "^5.0.0",
+        "flexsearch": "^0.7.31",
+        "focus-visible": "^5.2.0",
+        "git-url-parse": "^13.1.0",
+        "intersection-observer": "^0.12.2",
+        "match-sorter": "^6.3.1",
+        "next-seo": "^6.0.0",
+        "next-themes": "^0.2.1",
+        "scroll-into-view-if-needed": "^3.1.0",
+        "zod": "^3.22.3"
+      },
+      "peerDependencies": {
+        "next": ">=9.5.3",
+        "nextra": "2.13.4",
+        "react": ">=16.13.1",
+        "react-dom": ">=16.13.1"
+      }
+    },
+    "node_modules/nextra-theme-docs/node_modules/zod": {
+      "version": "3.22.4",
+      "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.4.tgz",
+      "integrity": "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==",
+      "funding": {
+        "url": "https://github.com/sponsors/colinhacks"
+      }
+    },
+    "node_modules/nextra/node_modules/zod": {
+      "version": "3.22.4",
+      "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.4.tgz",
+      "integrity": "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==",
+      "funding": {
+        "url": "https://github.com/sponsors/colinhacks"
+      }
+    },
+    "node_modules/non-layered-tidy-tree-layout": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/non-layered-tidy-tree-layout/-/non-layered-tidy-tree-layout-2.0.2.tgz",
+      "integrity": "sha512-gkXMxRzUH+PB0ax9dUN0yYF0S25BqeAYqhgMaLUFmpXLEk7Fcu8f4emJuOAY0V8kjDICxROIKsTAKsV/v355xw=="
+    },
+    "node_modules/npm-run-path": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz",
+      "integrity": "sha512-lJxZYlT4DW/bRUtFh1MQIWqmLwQfAxnqWG4HhEdjMlkrJYnJn0Jrr2u3mgxqaWsdiBc76TYkTG/mhrnYTuzfHw==",
+      "dependencies": {
+        "path-key": "^2.0.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/npm-to-yarn": {
+      "version": "2.2.1",
+      "resolved": "https://registry.npmjs.org/npm-to-yarn/-/npm-to-yarn-2.2.1.tgz",
+      "integrity": "sha512-O/j/ROyX0KGLG7O6Ieut/seQ0oiTpHF2tXAcFbpdTLQFiaNtkyTXXocM1fwpaa60dg1qpWj0nHlbNhx6qwuENQ==",
+      "engines": {
+        "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
+      },
+      "funding": {
+        "url": "https://github.com/nebrelbug/npm-to-yarn?sponsor=1"
+      }
+    },
+    "node_modules/p-finally": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
+      "integrity": "sha512-LICb2p9CB7FS+0eR1oqWnHhp0FljGLZCWBE9aix0Uye9W8LTQPwMTYVGWQWIw9RdQiDg4+epXQODwIYJtSJaow==",
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/p-limit": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz",
+      "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==",
+      "dependencies": {
+        "yocto-queue": "^0.1.0"
+      },
+      "engines": {
+        "node": ">=10"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/parse-entities": {
+      "version": "4.0.1",
+      "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-4.0.1.tgz",
+      "integrity": "sha512-SWzvYcSJh4d/SGLIOQfZ/CoNv6BTlI6YEQ7Nj82oDVnRpwe/Z/F1EMx42x3JAOwGBlCjeCH0BRJQbQ/opHL17w==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "character-entities": "^2.0.0",
+        "character-entities-legacy": "^3.0.0",
+        "character-reference-invalid": "^2.0.0",
+        "decode-named-character-reference": "^1.0.0",
+        "is-alphanumerical": "^2.0.0",
+        "is-decimal": "^2.0.0",
+        "is-hexadecimal": "^2.0.0"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/parse-numeric-range": {
+      "version": "1.3.0",
+      "resolved": "https://registry.npmjs.org/parse-numeric-range/-/parse-numeric-range-1.3.0.tgz",
+      "integrity": "sha512-twN+njEipszzlMJd4ONUYgSfZPDxgHhT9Ahed5uTigpQn90FggW4SA/AIPq/6a149fTbE9qBEcSwE3FAEp6wQQ=="
+    },
+    "node_modules/parse-path": {
+      "version": "7.0.0",
+      "resolved": "https://registry.npmjs.org/parse-path/-/parse-path-7.0.0.tgz",
+      "integrity": "sha512-Euf9GG8WT9CdqwuWJGdf3RkUcTBArppHABkO7Lm8IzRQp0e2r/kkFnmhu4TSK30Wcu5rVAZLmfPKSBBi9tWFog==",
+      "dependencies": {
+        "protocols": "^2.0.0"
+      }
+    },
+    "node_modules/parse-url": {
+      "version": "8.1.0",
+      "resolved": "https://registry.npmjs.org/parse-url/-/parse-url-8.1.0.tgz",
+      "integrity": "sha512-xDvOoLU5XRrcOZvnI6b8zA6n9O9ejNk/GExuz1yBuWUGn9KA97GI6HTs6u02wKara1CeVmZhH+0TZFdWScR89w==",
+      "dependencies": {
+        "parse-path": "^7.0.0"
+      }
+    },
+    "node_modules/parse5": {
+      "version": "7.1.2",
+      "resolved": "https://registry.npmjs.org/parse5/-/parse5-7.1.2.tgz",
+      "integrity": "sha512-Czj1WaSVpaoj0wbhMzLmWD69anp2WH7FXMB9n1Sy8/ZFF9jolSQVMu1Ij5WIyGmcBmhk7EOndpO4mIpihVqAXw==",
+      "dependencies": {
+        "entities": "^4.4.0"
+      },
+      "funding": {
+        "url": "https://github.com/inikulin/parse5?sponsor=1"
+      }
+    },
+    "node_modules/path-key": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz",
+      "integrity": "sha512-fEHGKCSmUSDPv4uoj8AlD+joPlq3peND+HRYyxFz4KPw4z926S/b8rIuFs2FYJg3BwsxJf6A9/3eIdLaYC+9Dw==",
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/periscopic": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-3.1.0.tgz",
+      "integrity": "sha512-vKiQ8RRtkl9P+r/+oefh25C3fhybptkHKCZSPlcXiJux2tJF55GnEj3BVn4A5gKfq9NWWXXrxkHBwVPUfH0opw==",
+      "dependencies": {
+        "@types/estree": "^1.0.0",
+        "estree-walker": "^3.0.0",
+        "is-reference": "^3.0.0"
+      }
+    },
+    "node_modules/picocolors": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",
+      "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ=="
+    },
+    "node_modules/postcss": {
+      "version": "8.4.14",
+      "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.14.tgz",
+      "integrity": "sha512-E398TUmfAYFPBSdzgeieK2Y1+1cpdxJx8yXbK/m57nRhKSmk1GB2tO4lbLBtlkfPQTDKfe4Xqv1ASWPpayPEig==",
+      "funding": [
+        {
+          "type": "opencollective",
+          "url": "https://opencollective.com/postcss/"
+        },
+        {
+          "type": "tidelift",
+          "url": "https://tidelift.com/funding/github/npm/postcss"
+        }
+      ],
+      "dependencies": {
+        "nanoid": "^3.3.4",
+        "picocolors": "^1.0.0",
+        "source-map-js": "^1.0.2"
+      },
+      "engines": {
+        "node": "^10 || ^12 || >=14"
+      }
+    },
+    "node_modules/property-information": {
+      "version": "6.4.1",
+      "resolved": "https://registry.npmjs.org/property-information/-/property-information-6.4.1.tgz",
+      "integrity": "sha512-OHYtXfu5aI2sS2LWFSN5rgJjrQ4pCy8i1jubJLe2QvMF8JJ++HXTUIVWFLfXJoaOfvYYjk2SN8J2wFUWIGXT4w==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/protocols": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/protocols/-/protocols-2.0.1.tgz",
+      "integrity": "sha512-/XJ368cyBJ7fzLMwLKv1e4vLxOju2MNAIokcr7meSaNcVbWz/CPcW22cP04mwxOErdA5mwjA8Q6w/cdAQxVn7Q=="
+    },
+    "node_modules/pseudomap": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
+      "integrity": "sha512-b/YwNhb8lk1Zz2+bXXpS/LK9OisiZZ1SNsSLxN1x2OXVEhW2Ckr/7mWE5vrC1ZTiJlD9g19jWszTmJsB+oEpFQ=="
+    },
+    "node_modules/react": {
+      "version": "18.2.0",
+      "resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz",
+      "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==",
+      "dependencies": {
+        "loose-envify": "^1.1.0"
+      },
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/react-dom": {
+      "version": "18.2.0",
+      "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz",
+      "integrity": "sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==",
+      "dependencies": {
+        "loose-envify": "^1.1.0",
+        "scheduler": "^0.23.0"
+      },
+      "peerDependencies": {
+        "react": "^18.2.0"
+      }
+    },
+    "node_modules/reading-time": {
+      "version": "1.5.0",
+      "resolved": "https://registry.npmjs.org/reading-time/-/reading-time-1.5.0.tgz",
+      "integrity": "sha512-onYyVhBNr4CmAxFsKS7bz+uTLRakypIe4R+5A824vBSkQy/hB3fZepoVEf8OVAxzLvK+H/jm9TzpI3ETSm64Kg=="
+    },
+    "node_modules/regenerator-runtime": {
+      "version": "0.14.1",
+      "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz",
+      "integrity": "sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw=="
+    },
+    "node_modules/rehype-katex": {
+      "version": "7.0.0",
+      "resolved": "https://registry.npmjs.org/rehype-katex/-/rehype-katex-7.0.0.tgz",
+      "integrity": "sha512-h8FPkGE00r2XKU+/acgqwWUlyzve1IiOKwsEkg4pDL3k48PiE0Pt+/uLtVHDVkN1yA4iurZN6UES8ivHVEQV6Q==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "@types/katex": "^0.16.0",
+        "hast-util-from-html-isomorphic": "^2.0.0",
+        "hast-util-to-text": "^4.0.0",
+        "katex": "^0.16.0",
+        "unist-util-visit-parents": "^6.0.0",
+        "vfile": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/rehype-katex/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/rehype-katex/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/rehype-katex/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/rehype-katex/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/rehype-pretty-code": {
+      "version": "0.9.11",
+      "resolved": "https://registry.npmjs.org/rehype-pretty-code/-/rehype-pretty-code-0.9.11.tgz",
+      "integrity": "sha512-Eq90eCYXQJISktfRZ8PPtwc5SUyH6fJcxS8XOMnHPUQZBtC6RYo67gGlley9X2nR8vlniPj0/7oCDEYHKQa/oA==",
+      "dependencies": {
+        "@types/hast": "^2.0.0",
+        "hash-obj": "^4.0.0",
+        "parse-numeric-range": "^1.3.0"
+      },
+      "engines": {
+        "node": ">=16"
+      },
+      "peerDependencies": {
+        "shiki": "*"
+      }
+    },
+    "node_modules/rehype-raw": {
+      "version": "7.0.0",
+      "resolved": "https://registry.npmjs.org/rehype-raw/-/rehype-raw-7.0.0.tgz",
+      "integrity": "sha512-/aE8hCfKlQeA8LmyeyQvQF3eBiLRGNlfBJEvWH7ivp9sBqs7TNqBL5X3v157rM4IFETqDnIOO+z5M/biZbo9Ww==",
+      "dependencies": {
+        "@types/hast": "^3.0.0",
+        "hast-util-raw": "^9.0.0",
+        "vfile": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/rehype-raw/node_modules/@types/hast": {
+      "version": "3.0.4",
+      "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz",
+      "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==",
+      "dependencies": {
+        "@types/unist": "*"
+      }
+    },
+    "node_modules/rehype-raw/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/rehype-raw/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/rehype-raw/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-gfm": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/remark-gfm/-/remark-gfm-3.0.1.tgz",
+      "integrity": "sha512-lEFDoi2PICJyNrACFOfDD3JlLkuSbOa5Wd8EPt06HUdptv8Gn0bxYTdbU/XXQ3swAPkEaGxxPN9cbnMHvVu1Ig==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "mdast-util-gfm": "^2.0.0",
+        "micromark-extension-gfm": "^2.0.0",
+        "unified": "^10.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-math": {
+      "version": "5.1.1",
+      "resolved": "https://registry.npmjs.org/remark-math/-/remark-math-5.1.1.tgz",
+      "integrity": "sha512-cE5T2R/xLVtfFI4cCePtiRn+e6jKMtFDR3P8V3qpv8wpKjwvHoBA4eJzvX+nVrnlNy0911bdGmuspCSwetfYHw==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "mdast-util-math": "^2.0.0",
+        "micromark-extension-math": "^2.0.0",
+        "unified": "^10.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-mdx": {
+      "version": "2.3.0",
+      "resolved": "https://registry.npmjs.org/remark-mdx/-/remark-mdx-2.3.0.tgz",
+      "integrity": "sha512-g53hMkpM0I98MU266IzDFMrTD980gNF3BJnkyFcmN+dD873mQeD5rdMO3Y2X+x8umQfbSE0PcoEDl7ledSA+2g==",
+      "dependencies": {
+        "mdast-util-mdx": "^2.0.0",
+        "micromark-extension-mdxjs": "^1.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-parse": {
+      "version": "10.0.2",
+      "resolved": "https://registry.npmjs.org/remark-parse/-/remark-parse-10.0.2.tgz",
+      "integrity": "sha512-3ydxgHa/ZQzG8LvC7jTXccARYDcRld3VfcgIIFs7bI6vbRSxJJmzgLEIIoYKyrfhaY+ujuWaf/PJiMZXoiCXgw==",
+      "dependencies": {
+        "@types/mdast": "^3.0.0",
+        "mdast-util-from-markdown": "^1.0.0",
+        "unified": "^10.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-reading-time": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/remark-reading-time/-/remark-reading-time-2.0.1.tgz",
+      "integrity": "sha512-fy4BKy9SRhtYbEHvp6AItbRTnrhiDGbqLQTSYVbQPGuRCncU1ubSsh9p/W5QZSxtYcUXv8KGL0xBgPLyNJA1xw==",
+      "dependencies": {
+        "estree-util-is-identifier-name": "^2.0.0",
+        "estree-util-value-to-estree": "^1.3.0",
+        "reading-time": "^1.3.0",
+        "unist-util-visit": "^3.1.0"
+      }
+    },
+    "node_modules/remark-reading-time/node_modules/unist-util-visit": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-3.1.0.tgz",
+      "integrity": "sha512-Szoh+R/Ll68QWAyQyZZpQzZQm2UPbxibDvaY8Xc9SUtYgPsDzx5AWSk++UUt2hJuow8mvwR+rG+LQLw+KsuAKA==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-reading-time/node_modules/unist-util-visit-parents": {
+      "version": "4.1.1",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-4.1.1.tgz",
+      "integrity": "sha512-1xAFJXAKpnnJl8G7K5KgU7FY55y3GcLIXqkzUj5QF/QVP7biUm0K0O2oqVkYsdjzJKifYeWn9+o6piAK2hGSHw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-rehype": {
+      "version": "10.1.0",
+      "resolved": "https://registry.npmjs.org/remark-rehype/-/remark-rehype-10.1.0.tgz",
+      "integrity": "sha512-EFmR5zppdBp0WQeDVZ/b66CWJipB2q2VLNFMabzDSGR66Z2fQii83G5gTBbgGEnEEA0QRussvrFHxk1HWGJskw==",
+      "dependencies": {
+        "@types/hast": "^2.0.0",
+        "@types/mdast": "^3.0.0",
+        "mdast-util-to-hast": "^12.1.0",
+        "unified": "^10.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-rehype/node_modules/mdast-util-to-hast": {
+      "version": "12.3.0",
+      "resolved": "https://registry.npmjs.org/mdast-util-to-hast/-/mdast-util-to-hast-12.3.0.tgz",
+      "integrity": "sha512-pits93r8PhnIoU4Vy9bjW39M2jJ6/tdHyja9rrot9uujkN7UTU9SDnE6WNJz/IGyQk3XHX6yNNtrBH6cQzm8Hw==",
+      "dependencies": {
+        "@types/hast": "^2.0.0",
+        "@types/mdast": "^3.0.0",
+        "mdast-util-definitions": "^5.0.0",
+        "micromark-util-sanitize-uri": "^1.1.0",
+        "trim-lines": "^3.0.0",
+        "unist-util-generated": "^2.0.0",
+        "unist-util-position": "^4.0.0",
+        "unist-util-visit": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-rehype/node_modules/unist-util-visit": {
+      "version": "4.1.2",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-4.1.2.tgz",
+      "integrity": "sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0",
+        "unist-util-visit-parents": "^5.1.1"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remark-rehype/node_modules/unist-util-visit-parents": {
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz",
+      "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-is": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/remove-accents": {
+      "version": "0.5.0",
+      "resolved": "https://registry.npmjs.org/remove-accents/-/remove-accents-0.5.0.tgz",
+      "integrity": "sha512-8g3/Otx1eJaVD12e31UbJj1YzdtVvzH85HV7t+9MJYk/u3XmkOUJ5Ys9wQrf9PCPK8+xn4ymzqYCiZl6QWKn+A=="
+    },
+    "node_modules/robust-predicates": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/robust-predicates/-/robust-predicates-3.0.2.tgz",
+      "integrity": "sha512-IXgzBWvWQwE6PrDI05OvmXUIruQTcoMDzRsOd5CDvHCVLcLHMTSYvOK5Cm46kWqlV3yAbuSpBZdJ5oP5OUoStg=="
+    },
+    "node_modules/rw": {
+      "version": "1.3.3",
+      "resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz",
+      "integrity": "sha512-PdhdWy89SiZogBLaw42zdeqtRJ//zFd2PgQavcICDUgJT5oW10QCRKbJ6bg4r0/UY2M6BWd5tkxuGFRvCkgfHQ=="
+    },
+    "node_modules/sade": {
+      "version": "1.8.1",
+      "resolved": "https://registry.npmjs.org/sade/-/sade-1.8.1.tgz",
+      "integrity": "sha512-xal3CZX1Xlo/k4ApwCFrHVACi9fBqJ7V+mwhBsuf/1IOKbBy098Fex+Wa/5QMubw09pSZ/u8EY8PWgevJsXp1A==",
+      "dependencies": {
+        "mri": "^1.1.0"
+      },
+      "engines": {
+        "node": ">=6"
+      }
+    },
+    "node_modules/safer-buffer": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
+      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
+    },
+    "node_modules/scheduler": {
+      "version": "0.23.0",
+      "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.0.tgz",
+      "integrity": "sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==",
+      "dependencies": {
+        "loose-envify": "^1.1.0"
+      }
+    },
+    "node_modules/scroll-into-view-if-needed": {
+      "version": "3.1.0",
+      "resolved": "https://registry.npmjs.org/scroll-into-view-if-needed/-/scroll-into-view-if-needed-3.1.0.tgz",
+      "integrity": "sha512-49oNpRjWRvnU8NyGVmUaYG4jtTkNonFZI86MmGRDqBphEK2EXT9gdEUoQPZhuBM8yWHxCWbobltqYO5M4XrUvQ==",
+      "dependencies": {
+        "compute-scroll-into-view": "^3.0.2"
+      }
+    },
+    "node_modules/section-matter": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/section-matter/-/section-matter-1.0.0.tgz",
+      "integrity": "sha512-vfD3pmTzGpufjScBh50YHKzEu2lxBWhVEHsNGoEXmCmn2hKGfeNLYMzCJpe8cD7gqX7TJluOVpBkAequ6dgMmA==",
+      "dependencies": {
+        "extend-shallow": "^2.0.1",
+        "kind-of": "^6.0.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/shebang-command": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz",
+      "integrity": "sha512-EV3L1+UQWGor21OmnvojK36mhg+TyIKDh3iFBKBohr5xeXIhNBcx8oWdgkTEEQ+BEFFYdLRuqMfd5L84N1V5Vg==",
+      "dependencies": {
+        "shebang-regex": "^1.0.0"
+      },
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/shebang-regex": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz",
+      "integrity": "sha512-wpoSFAxys6b2a2wHZ1XpDSgD7N9iVjg29Ph9uV/uaP9Ex/KXlkTZTeddxDPSYQpgvzKLGJke2UU0AzoGCjNIvQ==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/shiki": {
+      "version": "0.14.7",
+      "resolved": "https://registry.npmjs.org/shiki/-/shiki-0.14.7.tgz",
+      "integrity": "sha512-dNPAPrxSc87ua2sKJ3H5dQ/6ZaY8RNnaAqK+t0eG7p0Soi2ydiqbGOTaZCqaYvA/uZYfS1LJnemt3Q+mSfcPCg==",
+      "dependencies": {
+        "ansi-sequence-parser": "^1.1.0",
+        "jsonc-parser": "^3.2.0",
+        "vscode-oniguruma": "^1.7.0",
+        "vscode-textmate": "^8.0.0"
+      }
+    },
+    "node_modules/signal-exit": {
+      "version": "3.0.7",
+      "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz",
+      "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ=="
+    },
+    "node_modules/slash": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
+      "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==",
+      "engines": {
+        "node": ">=8"
+      }
+    },
+    "node_modules/sort-keys": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/sort-keys/-/sort-keys-5.0.0.tgz",
+      "integrity": "sha512-Pdz01AvCAottHTPQGzndktFNdbRA75BgOfeT1hH+AMnJFv8lynkPi42rfeEhpx1saTEI3YNMWxfqu0sFD1G8pw==",
+      "dependencies": {
+        "is-plain-obj": "^4.0.0"
+      },
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/sort-keys/node_modules/is-plain-obj": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz",
+      "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==",
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/source-map": {
+      "version": "0.7.4",
+      "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz",
+      "integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==",
+      "engines": {
+        "node": ">= 8"
+      }
+    },
+    "node_modules/source-map-js": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz",
+      "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/space-separated-tokens": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/space-separated-tokens/-/space-separated-tokens-2.0.2.tgz",
+      "integrity": "sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/sprintf-js": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
+      "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g=="
+    },
+    "node_modules/streamsearch": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz",
+      "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==",
+      "engines": {
+        "node": ">=10.0.0"
+      }
+    },
+    "node_modules/stringify-entities": {
+      "version": "4.0.3",
+      "resolved": "https://registry.npmjs.org/stringify-entities/-/stringify-entities-4.0.3.tgz",
+      "integrity": "sha512-BP9nNHMhhfcMbiuQKCqMjhDP5yBCAxsPu4pHFFzJ6Alo9dZgY4VLDPutXqIjpRiMoKdp7Av85Gr73Q5uH9k7+g==",
+      "dependencies": {
+        "character-entities-html4": "^2.0.0",
+        "character-entities-legacy": "^3.0.0"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/strip-bom-string": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/strip-bom-string/-/strip-bom-string-1.0.0.tgz",
+      "integrity": "sha512-uCC2VHvQRYu+lMh4My/sFNmF2klFymLX1wHJeXnbEJERpV/ZsVuonzerjfrGpIGF7LBVa1O7i9kjiWvJiFck8g==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/strip-eof": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",
+      "integrity": "sha512-7FCwGGmx8mD5xQd3RPUvnSpUXHM3BWuzjtpD4TXsfcZ9EL4azvVVUscFYwD9nx8Kh+uCBC00XBtAykoMHwTh8Q==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/style-to-object": {
+      "version": "0.4.4",
+      "resolved": "https://registry.npmjs.org/style-to-object/-/style-to-object-0.4.4.tgz",
+      "integrity": "sha512-HYNoHZa2GorYNyqiCaBgsxvcJIn7OHq6inEga+E6Ke3m5JkoqpQbnFssk4jwe+K7AhGa2fcha4wSOf1Kn01dMg==",
+      "dependencies": {
+        "inline-style-parser": "0.1.1"
+      }
+    },
+    "node_modules/styled-jsx": {
+      "version": "5.1.1",
+      "resolved": "https://registry.npmjs.org/styled-jsx/-/styled-jsx-5.1.1.tgz",
+      "integrity": "sha512-pW7uC1l4mBZ8ugbiZrcIsiIvVx1UmTfw7UkC3Um2tmfUq9Bhk8IiyEIPl6F8agHgjzku6j0xQEZbfA5uSgSaCw==",
+      "dependencies": {
+        "client-only": "0.0.1"
+      },
+      "engines": {
+        "node": ">= 12.0.0"
+      },
+      "peerDependencies": {
+        "react": ">= 16.8.0 || 17.x.x || ^18.0.0-0"
+      },
+      "peerDependenciesMeta": {
+        "@babel/core": {
+          "optional": true
+        },
+        "babel-plugin-macros": {
+          "optional": true
+        }
+      }
+    },
+    "node_modules/stylis": {
+      "version": "4.3.1",
+      "resolved": "https://registry.npmjs.org/stylis/-/stylis-4.3.1.tgz",
+      "integrity": "sha512-EQepAV+wMsIaGVGX1RECzgrcqRRU/0sYOHkeLsZ3fzHaHXZy4DaOOX0vOlGQdlsjkh3mFHAIlVimpwAs4dslyQ=="
+    },
+    "node_modules/supports-color": {
+      "version": "4.5.0",
+      "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.5.0.tgz",
+      "integrity": "sha512-ycQR/UbvI9xIlEdQT1TQqwoXtEldExbCEAJgRo5YXlmSKjv6ThHnP9/vwGa1gr19Gfw+LkFd7KqYMhzrRC5JYw==",
+      "dependencies": {
+        "has-flag": "^2.0.0"
+      },
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "node_modules/title": {
+      "version": "3.5.3",
+      "resolved": "https://registry.npmjs.org/title/-/title-3.5.3.tgz",
+      "integrity": "sha512-20JyowYglSEeCvZv3EZ0nZ046vLarO37prvV0mbtQV7C8DJPGgN967r8SJkqd3XK3K3lD3/Iyfp3avjfil8Q2Q==",
+      "dependencies": {
+        "arg": "1.0.0",
+        "chalk": "2.3.0",
+        "clipboardy": "1.2.2",
+        "titleize": "1.0.0"
+      },
+      "bin": {
+        "title": "bin/title.js"
+      }
+    },
+    "node_modules/titleize": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/titleize/-/titleize-1.0.0.tgz",
+      "integrity": "sha512-TARUb7z1pGvlLxgPk++7wJ6aycXF3GJ0sNSBTAsTuJrQG5QuZlkUQP+zl+nbjAh4gMX9yDw9ZYklMd7vAfJKEw==",
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/trim-lines": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/trim-lines/-/trim-lines-3.0.1.tgz",
+      "integrity": "sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/trough": {
+      "version": "2.2.0",
+      "resolved": "https://registry.npmjs.org/trough/-/trough-2.2.0.tgz",
+      "integrity": "sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/ts-dedent": {
+      "version": "2.2.0",
+      "resolved": "https://registry.npmjs.org/ts-dedent/-/ts-dedent-2.2.0.tgz",
+      "integrity": "sha512-q5W7tVM71e2xjHZTlgfTDoPF/SmqKG5hddq9SzR49CH2hayqRKJtQ4mtRlSxKaJlR/+9rEM+mnBHf7I2/BQcpQ==",
+      "engines": {
+        "node": ">=6.10"
+      }
+    },
+    "node_modules/tslib": {
+      "version": "2.6.2",
+      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz",
+      "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q=="
+    },
+    "node_modules/type-fest": {
+      "version": "1.4.0",
+      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-1.4.0.tgz",
+      "integrity": "sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==",
+      "engines": {
+        "node": ">=10"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/typescript": {
+      "version": "4.9.3",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.3.tgz",
+      "integrity": "sha512-CIfGzTelbKNEnLpLdGFgdyKhG23CKdKgQPOBc+OUNrkJ2vr+KSzsSV5kq5iWhEQbok+quxgGzrAtGWCyU7tHnA==",
+      "bin": {
+        "tsc": "bin/tsc",
+        "tsserver": "bin/tsserver"
+      },
+      "engines": {
+        "node": ">=4.2.0"
+      }
+    },
+    "node_modules/unified": {
+      "version": "10.1.2",
+      "resolved": "https://registry.npmjs.org/unified/-/unified-10.1.2.tgz",
+      "integrity": "sha512-pUSWAi/RAnVy1Pif2kAoeWNBa3JVrx0MId2LASj8G+7AiHWoKZNTomq6LG326T68U7/e263X6fTdcXIy7XnF7Q==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "bail": "^2.0.0",
+        "extend": "^3.0.0",
+        "is-buffer": "^2.0.0",
+        "is-plain-obj": "^4.0.0",
+        "trough": "^2.0.0",
+        "vfile": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unified/node_modules/is-plain-obj": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz",
+      "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==",
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/unist-util-find-after": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-find-after/-/unist-util-find-after-5.0.0.tgz",
+      "integrity": "sha512-amQa0Ep2m6hE2g72AugUItjbuM8X8cGQnFoHk0pGfrFeT9GZhzN5SW8nRsiGKK7Aif4CrACPENkA6P/Lw6fHGQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-is": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-find-after/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/unist-util-find-after/node_modules/unist-util-is": {
+      "version": "6.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-6.0.0.tgz",
+      "integrity": "sha512-2qCTHimwdxLfz+YzdGfkqNlH0tLi9xjTnHddPmJwtIG9MGsdbutfTc4P+haPD7l7Cjxf/WZj+we5qfVPvvxfYw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-generated": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/unist-util-generated/-/unist-util-generated-2.0.1.tgz",
+      "integrity": "sha512-qF72kLmPxAw0oN2fwpWIqbXAVyEqUzDHMsbtPvOudIlUzXYFIeQIuxXQCRCFh22B7cixvU0MG7m3MW8FTq/S+A==",
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-is": {
+      "version": "5.2.1",
+      "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-5.2.1.tgz",
+      "integrity": "sha512-u9njyyfEh43npf1M+yGKDGVPbY/JWEemg5nH05ncKPfi+kBbKBJoTdsogMu33uhytuLlv9y0O7GH7fEdwLdLQw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-position": {
+      "version": "4.0.4",
+      "resolved": "https://registry.npmjs.org/unist-util-position/-/unist-util-position-4.0.4.tgz",
+      "integrity": "sha512-kUBE91efOWfIVBo8xzh/uZQ7p9ffYRtUbMRZBNFYwf0RK8koUMx6dGUfwylLOKmaT2cs4wSW96QoYUSXAyEtpg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-position-from-estree": {
+      "version": "1.1.2",
+      "resolved": "https://registry.npmjs.org/unist-util-position-from-estree/-/unist-util-position-from-estree-1.1.2.tgz",
+      "integrity": "sha512-poZa0eXpS+/XpoQwGwl79UUdea4ol2ZuCYguVaJS4qzIOMDzbqz8a3erUCOmubSZkaOuGamb3tX790iwOIROww==",
+      "dependencies": {
+        "@types/unist": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-remove": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-remove/-/unist-util-remove-4.0.0.tgz",
+      "integrity": "sha512-b4gokeGId57UVRX/eVKej5gXqGlc9+trkORhFJpu9raqZkZhU0zm8Doi05+HaiBsMEIJowL+2WtQ5ItjsngPXg==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-is": "^6.0.0",
+        "unist-util-visit-parents": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-remove-position": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-remove-position/-/unist-util-remove-position-5.0.0.tgz",
+      "integrity": "sha512-Hp5Kh3wLxv0PHj9m2yZhhLt58KzPtEYKQQ4yxfYFEO7EvHwzyDYnduhHnY1mDxoqr7VUwVuHXk9RXKIiYS1N8Q==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-visit": "^5.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-remove-position/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/unist-util-remove/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/unist-util-remove/node_modules/unist-util-is": {
+      "version": "6.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-6.0.0.tgz",
+      "integrity": "sha512-2qCTHimwdxLfz+YzdGfkqNlH0tLi9xjTnHddPmJwtIG9MGsdbutfTc4P+haPD7l7Cjxf/WZj+we5qfVPvvxfYw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-stringify-position": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz",
+      "integrity": "sha512-k5GzIBZ/QatR8N5X2y+drfpWG8IDBzdnVj6OInRNWm1oXrzydiaAT2OQiA8DPRRZyAKb9b6I2a6PxYklZD0gKg==",
+      "dependencies": {
+        "@types/unist": "^2.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-visit": {
+      "version": "5.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-5.0.0.tgz",
+      "integrity": "sha512-MR04uvD+07cwl/yhVuVWAtw+3GOR/knlL55Nd/wAdblk27GCVt3lqpTivy/tkJcZoNPzTwS1Y+KMojlLDhoTzg==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-is": "^6.0.0",
+        "unist-util-visit-parents": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-visit-parents": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-6.0.1.tgz",
+      "integrity": "sha512-L/PqWzfTP9lzzEa6CKs0k2nARxTdZduw3zyh8d2NVBnsyvHjSX4TWse388YrrQKbvI8w20fGjGlhgT96WwKykw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-is": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-visit-parents/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/unist-util-visit-parents/node_modules/unist-util-is": {
+      "version": "6.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-6.0.0.tgz",
+      "integrity": "sha512-2qCTHimwdxLfz+YzdGfkqNlH0tLi9xjTnHddPmJwtIG9MGsdbutfTc4P+haPD7l7Cjxf/WZj+we5qfVPvvxfYw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/unist-util-visit/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/unist-util-visit/node_modules/unist-util-is": {
+      "version": "6.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-6.0.0.tgz",
+      "integrity": "sha512-2qCTHimwdxLfz+YzdGfkqNlH0tLi9xjTnHddPmJwtIG9MGsdbutfTc4P+haPD7l7Cjxf/WZj+we5qfVPvvxfYw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/uuid": {
+      "version": "9.0.1",
+      "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz",
+      "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==",
+      "funding": [
+        "https://github.com/sponsors/broofa",
+        "https://github.com/sponsors/ctavan"
+      ],
+      "bin": {
+        "uuid": "dist/bin/uuid"
+      }
+    },
+    "node_modules/uvu": {
+      "version": "0.5.6",
+      "resolved": "https://registry.npmjs.org/uvu/-/uvu-0.5.6.tgz",
+      "integrity": "sha512-+g8ENReyr8YsOc6fv/NVJs2vFdHBnBNdfE49rshrTzDWOlUx4Gq7KOS2GD8eqhy2j+Ejq29+SbKH8yjkAqXqoA==",
+      "dependencies": {
+        "dequal": "^2.0.0",
+        "diff": "^5.0.0",
+        "kleur": "^4.0.3",
+        "sade": "^1.7.3"
+      },
+      "bin": {
+        "uvu": "bin.js"
+      },
+      "engines": {
+        "node": ">=8"
+      }
+    },
+    "node_modules/vfile": {
+      "version": "5.3.7",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-5.3.7.tgz",
+      "integrity": "sha512-r7qlzkgErKjobAmyNIkkSpizsFPYiUPuJb5pNW1RB4JcYVZhs4lIbVqk8XPk033CV/1z8ss5pkax8SuhGpcG8g==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "is-buffer": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0",
+        "vfile-message": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile-location": {
+      "version": "5.0.2",
+      "resolved": "https://registry.npmjs.org/vfile-location/-/vfile-location-5.0.2.tgz",
+      "integrity": "sha512-NXPYyxyBSH7zB5U6+3uDdd6Nybz6o6/od9rk8bp9H8GR3L+cm/fC0uUTbqBmUTnMCUDslAGBOIKNfvvb+gGlDg==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "vfile": "^6.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile-location/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/vfile-location/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile-location/node_modules/vfile": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.1.tgz",
+      "integrity": "sha512-1bYqc7pt6NIADBJ98UiG0Bn/CHIVOoZ/IyEkqIruLg0mE1BKzkOXY2D6CSqQIcKqgadppE5lrxgWXJmXd7zZJw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0",
+        "vfile-message": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile-matter": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/vfile-matter/-/vfile-matter-3.0.1.tgz",
+      "integrity": "sha512-CAAIDwnh6ZdtrqAuxdElUqQRQDQgbbIrYtDYI8gCjXS1qQ+1XdLoK8FIZWxJwn0/I+BkSSZpar3SOgjemQz4fg==",
+      "dependencies": {
+        "@types/js-yaml": "^4.0.0",
+        "is-buffer": "^2.0.0",
+        "js-yaml": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile-matter/node_modules/argparse": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
+      "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
+    },
+    "node_modules/vfile-matter/node_modules/js-yaml": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
+      "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
+      "dependencies": {
+        "argparse": "^2.0.1"
+      },
+      "bin": {
+        "js-yaml": "bin/js-yaml.js"
+      }
+    },
+    "node_modules/vfile-message": {
+      "version": "4.0.2",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-4.0.2.tgz",
+      "integrity": "sha512-jRDZ1IMLttGj41KcZvlrYAaI3CfqpLpfpf+Mfig13viT6NKvRzWZ+lXz0Y5D60w6uJIBAOGq9mSHf0gktF0duw==",
+      "dependencies": {
+        "@types/unist": "^3.0.0",
+        "unist-util-stringify-position": "^4.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile-message/node_modules/@types/unist": {
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz",
+      "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ=="
+    },
+    "node_modules/vfile-message/node_modules/unist-util-stringify-position": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
+      "integrity": "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==",
+      "dependencies": {
+        "@types/unist": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vfile/node_modules/vfile-message": {
+      "version": "3.1.4",
+      "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-3.1.4.tgz",
+      "integrity": "sha512-fa0Z6P8HUrQN4BZaX05SIVXic+7kE3b05PWAtPuYP9QLHsLKYR7/AlLW3NtOrpXRLeawpDLMsVkmk5DG0NXgWw==",
+      "dependencies": {
+        "@types/unist": "^2.0.0",
+        "unist-util-stringify-position": "^3.0.0"
+      },
+      "funding": {
+        "type": "opencollective",
+        "url": "https://opencollective.com/unified"
+      }
+    },
+    "node_modules/vscode-oniguruma": {
+      "version": "1.7.0",
+      "resolved": "https://registry.npmjs.org/vscode-oniguruma/-/vscode-oniguruma-1.7.0.tgz",
+      "integrity": "sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA=="
+    },
+    "node_modules/vscode-textmate": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-8.0.0.tgz",
+      "integrity": "sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg=="
+    },
+    "node_modules/watchpack": {
+      "version": "2.4.0",
+      "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.4.0.tgz",
+      "integrity": "sha512-Lcvm7MGST/4fup+ifyKi2hjyIAwcdI4HRgtvTpIUxBRhB+RFtUh8XtDOxUfctVCnhVi+QQj49i91OyvzkJl6cg==",
+      "dependencies": {
+        "glob-to-regexp": "^0.4.1",
+        "graceful-fs": "^4.1.2"
+      },
+      "engines": {
+        "node": ">=10.13.0"
+      }
+    },
+    "node_modules/web-namespaces": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/web-namespaces/-/web-namespaces-2.0.1.tgz",
+      "integrity": "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    },
+    "node_modules/web-worker": {
+      "version": "1.3.0",
+      "resolved": "https://registry.npmjs.org/web-worker/-/web-worker-1.3.0.tgz",
+      "integrity": "sha512-BSR9wyRsy/KOValMgd5kMyr3JzpdeoR9KVId8u5GVlTTAtNChlsE4yTxeY7zMdNSyOmoKBv8NH2qeRY9Tg+IaA=="
+    },
+    "node_modules/which": {
+      "version": "1.3.1",
+      "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
+      "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==",
+      "dependencies": {
+        "isexe": "^2.0.0"
+      },
+      "bin": {
+        "which": "bin/which"
+      }
+    },
+    "node_modules/yallist": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
+      "integrity": "sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A=="
+    },
+    "node_modules/yocto-queue": {
+      "version": "0.1.0",
+      "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
+      "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==",
+      "engines": {
+        "node": ">=10"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/zod": {
+      "version": "3.21.4",
+      "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz",
+      "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==",
+      "funding": {
+        "url": "https://github.com/sponsors/colinhacks"
+      }
+    },
+    "node_modules/zwitch": {
+      "version": "2.0.4",
+      "resolved": "https://registry.npmjs.org/zwitch/-/zwitch-2.0.4.tgz",
+      "integrity": "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/wooorm"
+      }
+    }
+  }
+}
diff --git a/docs/pages/common_issues/_meta.json b/docs/pages/common_issues/_meta.json
new file mode 100644
index 000000000..6232261fa
--- /dev/null
+++ b/docs/pages/common_issues/_meta.json
@@ -0,0 +1,3 @@
+{
+  "isr": "ISR"
+}
diff --git a/docs/pages/common_issues/isr.mdx b/docs/pages/common_issues/isr.mdx
index baac6f681..e6a4c398b 100644
--- a/docs/pages/common_issues/isr.mdx
+++ b/docs/pages/common_issues/isr.mdx
@@ -6,7 +6,7 @@ import {Callout} from 'nextra/components'
 
 #### On-demand revalidation
 
-When you manualy revalidates the Next.js cache for a specific page, the ISR cache files stored on S3 will be updated. However, it is still necessary to invalidate the CloudFront cache:
+When you manually revalidate the Next.js cache for a specific page, the ISR cache files stored on S3 will be updated. However, it is still necessary to invalidate the CloudFront cache:
 
 ```ts
 // pages/api/revalidate.js
@@ -30,7 +30,7 @@ import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-clo
 
 const cloudFront = new CloudFrontClient({});
 
-async function invalidateCFPaths(paths: string[]) {
+async function invalidateCloudFrontPaths(paths: string[]) {
   await cloudFront.send(
     new CreateInvalidationCommand({
       // Set CloudFront distribution ID here
@@ -55,10 +55,10 @@ Due to these costs, if multiple paths require invalidation, it is more economica
 
 ```ts
 // This costs $0.005 x 3 = $0.015 after the first 1000 paths
-await invalidateCFPaths(["/page/a", "/page/b", "/page/c"]);
+await invalidateCloudFrontPaths(["/page/a", "/page/b", "/page/c"]);
 
 // This costs $0.005, but also invalidates other routes such as "page/d"
-await invalidateCFPaths(["/page/*"]);
+await invalidateCloudFrontPaths(["/page/*"]);
 ```
 
 For on-demand revalidation via the [`next/cache` module](https://nextjs.org/docs/app/building-your-application/data-fetching/revalidating#using-on-demand-revalidation), if you want to retrieve the associated paths for a given tag, you can use this function:
diff --git a/docs/pages/inner_workings/_meta.json b/docs/pages/inner_workings/_meta.json
new file mode 100644
index 000000000..6232261fa
--- /dev/null
+++ b/docs/pages/inner_workings/_meta.json
@@ -0,0 +1,3 @@
+{
+  "isr": "ISR"
+}
diff --git a/packages/open-next/CHANGELOG.md b/packages/open-next/CHANGELOG.md
index e3fb0e95c..0d3f871c2 100644
--- a/packages/open-next/CHANGELOG.md
+++ b/packages/open-next/CHANGELOG.md
@@ -1,5 +1,11 @@
 # open-next
 
+## 2.3.8
+
+### Patch Changes
+
+- 8cfb801: fix(open-next): parse cookies when converting response to cloudfront
+
 ## 2.3.7
 
 ### Patch Changes
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index 968fcd8d7..00ba3ca2b 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -6,6 +6,7 @@ import {
   CloudFrontRequestResult,
 } from "aws-lambda";
 import { OutgoingHttpHeader } from "http";
+import { parseCookies } from "http/util";
 import type { Converter, InternalEvent, InternalResult } from "types/open-next";
 
 import { debug } from "../adapters/logger";
@@ -70,6 +71,16 @@ function convertToCloudfrontHeaders(
   Object.entries(headers)
     .filter(([key]) => key.toLowerCase() !== "content-length")
     .forEach(([key, value]) => {
+      if (key === "set-cookie") {
+        const cookies = parseCookies(`${value}`);
+        if (cookies) {
+          cloudfrontHeaders[key] = cookies.map((cookie) => ({
+            key,
+            value: cookie,
+          }));
+        }
+        return;
+      }
       cloudfrontHeaders[key] = [
         ...(cloudfrontHeaders[key] || []),
         ...(Array.isArray(value)
diff --git a/packages/tests-unit/CHANGELOG.md b/packages/tests-unit/CHANGELOG.md
index c95583030..c4a4ffbee 100644
--- a/packages/tests-unit/CHANGELOG.md
+++ b/packages/tests-unit/CHANGELOG.md
@@ -4,6 +4,13 @@
 
 ### Patch Changes
 
+- Updated dependencies [8cfb801]
+  - open-next@2.3.8
+
+## null
+
+### Patch Changes
+
 - Updated dependencies [3235392]
 - Updated dependencies [af2d3ce]
   - open-next@2.3.7
diff --git a/packages/tests-unit/tests/event-mapper.test.ts b/packages/tests-unit/tests/event-mapper.test.ts
new file mode 100644
index 000000000..312a2c1bc
--- /dev/null
+++ b/packages/tests-unit/tests/event-mapper.test.ts
@@ -0,0 +1,115 @@
+import { CloudFrontRequestResult } from "aws-lambda";
+
+//TODO: rewrite this test to use converter instead of event-mapper
+import { convertTo } from "../../open-next/src/adapters/event-mapper";
+
+describe("convertTo", () => {
+  describe("CloudFront Result", () => {
+    it("Should parse the headers", () => {
+      const response = convertTo({
+        body: "",
+        headers: {
+          "content-type": "application/json",
+          test: "test",
+        },
+        isBase64Encoded: false,
+        statusCode: 200,
+        type: "cf",
+      }) as CloudFrontRequestResult;
+
+      expect(response?.headers).toStrictEqual({
+        "content-type": [
+          {
+            key: "content-type",
+            value: "application/json",
+          },
+        ],
+        test: [
+          {
+            key: "test",
+            value: "test",
+          },
+        ],
+      });
+    });
+
+    it("Should parse the headers with arrays", () => {
+      const response = convertTo({
+        body: "",
+        headers: {
+          test: ["test1", "test2"],
+        },
+        isBase64Encoded: false,
+        statusCode: 200,
+        type: "cf",
+      }) as CloudFrontRequestResult;
+
+      expect(response?.headers).toStrictEqual({
+        test: [
+          {
+            key: "test",
+            value: "test1",
+          },
+          {
+            key: "test",
+            value: "test2",
+          },
+        ],
+      });
+    });
+
+    it("Should parse the headers with cookies", () => {
+      const response = convertTo({
+        body: "",
+        headers: {
+          "set-cookie":
+            "test=1; Path=/; HttpOnly; Secure; SameSite=None, test=2; Path=/; HttpOnly; Secure; SameSite=None",
+        },
+        isBase64Encoded: false,
+        statusCode: 200,
+        type: "cf",
+      }) as CloudFrontRequestResult;
+
+      expect(response?.headers).toStrictEqual({
+        "set-cookie": [
+          {
+            key: "set-cookie",
+            value: "test=1; Path=/; HttpOnly; Secure; SameSite=None",
+          },
+          {
+            key: "set-cookie",
+            value: "test=2; Path=/; HttpOnly; Secure; SameSite=None",
+          },
+        ],
+      });
+    });
+
+    it("Should parse the headers with cookies + expires", () => {
+      const response = convertTo({
+        body: "",
+        headers: {
+          "set-cookie":
+            "test=1; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None, test=2; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
+        },
+        isBase64Encoded: false,
+        statusCode: 200,
+        type: "cf",
+      }) as CloudFrontRequestResult;
+
+      expect(response?.headers).toStrictEqual({
+        "set-cookie": [
+          {
+            key: "set-cookie",
+            value:
+              "test=1; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
+          },
+          {
+            key: "set-cookie",
+            value:
+              "test=2; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
+          },
+        ],
+      });
+    });
+  });
+});

From 059db6800d9c879a05195e316ead434bce8d80ce Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 23 Mar 2024 14:59:17 +0100
Subject: [PATCH 078/102] fix resolve in image optimization also fix image opt
 not using streaming

---
 .../open-next/src/adapters/edge-adapter.ts    |  4 +-
 .../adapters/image-optimization-adapter.ts    | 48 ++++++++++++---
 packages/open-next/src/build.ts               | 59 ++++++++++++-------
 .../open-next/src/build/createServerBundle.ts | 10 +---
 .../open-next/src/core/routing/middleware.ts  |  2 +-
 packages/open-next/src/plugins/resolve.ts     | 26 ++++----
 6 files changed, 100 insertions(+), 49 deletions(-)

diff --git a/packages/open-next/src/adapters/edge-adapter.ts b/packages/open-next/src/adapters/edge-adapter.ts
index 57b3ab028..09470a8ac 100644
--- a/packages/open-next/src/adapters/edge-adapter.ts
+++ b/packages/open-next/src/adapters/edge-adapter.ts
@@ -49,9 +49,9 @@ const defaultHandler = async (
       responseHeaders[key] = value;
     }
   });
-  console.log("responseHeaders", responseHeaders);
+  // console.log("responseHeaders", responseHeaders);
   const body = buffer.toString();
-  console.log("body", body);
+  // console.log("body", body);
 
   return {
     type: "core",
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index f8a0b1434..a4d8aa647 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -9,6 +9,7 @@ import { Writable } from "node:stream";
 
 import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
 import { loadConfig } from "config/util.js";
+import { OpenNextNodeResponse, StreamCreator } from "http/openNextResponse.js";
 // @ts-ignore
 import { defaultConfig } from "next/dist/server/config-shared";
 import {
@@ -27,8 +28,6 @@ import { setNodeEnv } from "./util.js";
 // Expected environment variables
 const { BUCKET_NAME, BUCKET_KEY_PREFIX } = process.env;
 
-const s3Client = new S3Client({ logger: awsLogger });
-
 setNodeEnv();
 const nextDir = path.join(__dirname, ".next");
 const config = loadConfig(nextDir);
@@ -56,6 +55,7 @@ export const handler = await createGenericHandler({
 
 export async function defaultHandler(
   event: InternalEvent,
+  streamCreator?: StreamCreator,
 ): Promise<InternalResult> {
   // Images are handled via header and query param information.
   debug("handler event", event);
@@ -63,7 +63,7 @@ export async function defaultHandler(
 
   try {
     // const headers = normalizeHeaderKeysToLowercase(rawHeaders);
-    ensureBucketExists();
+
     const imageParams = validateImageParams(
       headers,
       queryString === null ? undefined : queryString,
@@ -75,9 +75,9 @@ export async function defaultHandler(
       downloadHandler,
     );
 
-    return buildSuccessResponse(result);
+    return buildSuccessResponse(result, streamCreator);
   } catch (e: any) {
-    return buildFailureResponse(e);
+    return buildFailureResponse(e, streamCreator);
   }
 }
 
@@ -119,7 +119,23 @@ function validateImageParams(
   return imageParams;
 }
 
-function buildSuccessResponse(result: any): InternalResult {
+function buildSuccessResponse(
+  result: any,
+  streamCreator?: StreamCreator,
+): InternalResult {
+  if (streamCreator) {
+    const response = new OpenNextNodeResponse(
+      () => void 0,
+      async () => void 0,
+      streamCreator,
+    );
+    response.writeHead(200, {
+      Vary: "Accept",
+      "Cache-Control": `public,max-age=${result.maxAge},immutable`,
+      "Content-Type": result.contentType,
+    });
+    response.end(result.buffer);
+  }
   return {
     type: "core",
     statusCode: 200,
@@ -133,8 +149,24 @@ function buildSuccessResponse(result: any): InternalResult {
   };
 }
 
-function buildFailureResponse(e: any): InternalResult {
+function buildFailureResponse(
+  e: any,
+  streamCreator?: StreamCreator,
+): InternalResult {
   debug(e);
+  if (streamCreator) {
+    const response = new OpenNextNodeResponse(
+      () => void 0,
+      async () => void 0,
+      streamCreator,
+    );
+    response.writeHead(500, {
+      Vary: "Accept",
+      "Cache-Control": `public,max-age=60,immutable`,
+      "Content-Type": "application/json",
+    });
+    response.end(e?.message || e?.toString() || e);
+  }
   return {
     type: "core",
     isBase64Encoded: false,
@@ -154,10 +186,12 @@ const resolveLoader = () => {
   if (typeof openNextParams?.loader === "function") {
     return openNextParams.loader();
   } else {
+    const s3Client = new S3Client({ logger: awsLogger });
     return Promise.resolve<ImageLoader>({
       name: "s3",
       // @ts-ignore
       load: async (key: string) => {
+        ensureBucketExists();
         const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, "");
         const response = await s3Client.send(
           new GetObjectCommand({
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 5b820e1f9..22da479f4 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -80,9 +80,9 @@ export async function build(openNextConfigPath?: string) {
   await createCacheAssets(monorepoRoot);
 
   await createServerBundle(config, options);
-  await createRevalidationBundle();
+  await createRevalidationBundle(config);
   await createImageOptimizationBundle(config);
-  await createWarmerBundle();
+  await createWarmerBundle(config);
   await generateOutput(options.appPath, options.appBuildOutputPath, config);
   logger.info("OpenNext build complete.");
 }
@@ -244,7 +244,7 @@ function initOutputDir() {
   fs.writeFileSync(path.join(tempDir, "open-next.config.mjs"), openNextConfig);
 }
 
-async function createWarmerBundle() {
+async function createWarmerBundle(config: OpenNextConfig) {
   logger.info(`Bundling warmer function...`);
 
   const { outputDir } = options;
@@ -268,8 +268,10 @@ async function createWarmerBundle() {
       plugins: [
         openNextResolvePlugin({
           overrides: {
-            converter: "dummy",
+            converter: config.warmer?.override?.converter ?? "dummy",
+            wrapper: config.warmer?.override?.wrapper,
           },
+          fnName: "warmer",
         }),
       ],
       banner: {
@@ -285,7 +287,7 @@ async function createWarmerBundle() {
   );
 }
 
-async function createRevalidationBundle() {
+async function createRevalidationBundle(config: OpenNextConfig) {
   logger.info(`Bundling revalidation function...`);
 
   const { appBuildOutputPath, outputDir } = options;
@@ -305,8 +307,11 @@ async function createRevalidationBundle() {
       outfile: path.join(outputPath, "index.mjs"),
       plugins: [
         openNextResolvePlugin({
+          fnName: "revalidate",
           overrides: {
-            converter: "sqs-revalidate",
+            converter:
+              config.revalidate?.override?.converter ?? "sqs-revalidate",
+            wrapper: config.revalidate?.override?.wrapper,
           },
         }),
       ],
@@ -333,20 +338,29 @@ async function createImageOptimizationBundle(config: OpenNextConfig) {
   // Copy open-next.config.mjs into the bundle
   copyOpenNextConfig(options.tempDir, outputPath);
 
-  const plugins =
-    compareSemver(options.nextVersion, "14.1.1") >= 0
-      ? [
-          openNextReplacementPlugin({
-            name: "opennext-14.1.1-image-optimization",
-            target: /plugins\/image-optimization\/image-optimization\.js/g,
-            replacements: [
-              require.resolve(
-                "./adapters/plugins/image-optimization/image-optimization.replacement.js",
-              ),
-            ],
-          }),
-        ]
-      : undefined;
+  const plugins = [
+    openNextResolvePlugin({
+      fnName: "imageOptimization",
+      overrides: {
+        converter: config.imageOptimization?.override?.converter,
+        wrapper: config.imageOptimization?.override?.wrapper,
+      },
+    }),
+  ];
+
+  if (compareSemver(options.nextVersion, "14.1.1") >= 0) {
+    plugins.push(
+      openNextReplacementPlugin({
+        name: "opennext-14.1.1-image-optimization",
+        target: /plugins\/image-optimization\/image-optimization\.js/g,
+        replacements: [
+          require.resolve(
+            "./adapters/plugins/image-optimization/image-optimization.replacement.js",
+          ),
+        ],
+      }),
+    );
+  }
 
   // Build Lambda code (1st pass)
   // note: bundle in OpenNext package b/c the adapter relies on the
@@ -639,8 +653,11 @@ async function createCacheAssets(monorepoRoot: string) {
           target: ["node18"],
           plugins: [
             openNextResolvePlugin({
+              fnName: "initializationFunction",
               overrides: {
-                converter: "dummy",
+                converter:
+                  config.initializationFunction?.override?.converter ?? "dummy",
+                wrapper: config.initializationFunction?.override?.wrapper,
               },
             }),
           ],
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index c96e1ace8..ed2233dc3 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -213,14 +213,8 @@ async function generateBundle(
     openNextResolvePlugin({
       fnName: name,
       overrides: {
-        converter:
-          typeof overrides.converter === "function"
-            ? "dummy"
-            : overrides.converter,
-        wrapper:
-          typeof overrides.wrapper === "function"
-            ? "aws-lambda"
-            : overrides.wrapper,
+        converter: overrides.converter,
+        wrapper: overrides.wrapper,
       },
     }),
   ];
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 2de20cfcc..d52c4a4ae 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -68,7 +68,7 @@ export async function handleMiddleware(
   const initialUrl = new URL(normalizedPath, host);
   initialUrl.search = convertToQueryString(query);
   const url = initialUrl.toString();
-  console.log("url", url, normalizedPath);
+  // console.log("url", url, normalizedPath);
 
   // @ts-expect-error - This is bundled
   const middleware = await import("./middleware.mjs");
diff --git a/packages/open-next/src/plugins/resolve.ts b/packages/open-next/src/plugins/resolve.ts
index eafa04874..1767d3091 100644
--- a/packages/open-next/src/plugins/resolve.ts
+++ b/packages/open-next/src/plugins/resolve.ts
@@ -2,19 +2,18 @@ import { readFileSync } from "node:fs";
 
 import { Plugin } from "esbuild";
 import type {
-  IncludedConverter,
+  DefaultOverrideOptions,
   IncludedIncrementalCache,
   IncludedQueue,
   IncludedTagCache,
-  IncludedWrapper,
 } from "types/open-next";
 
 import logger from "../logger.js";
 
 export interface IPluginSettings {
-  overrides: {
-    wrapper?: IncludedWrapper;
-    converter?: IncludedConverter;
+  overrides?: {
+    wrapper?: DefaultOverrideOptions<any, any>["wrapper"];
+    converter?: DefaultOverrideOptions<any, any>["converter"];
     // Right now theses do nothing since there is only one implementation
     tag?: IncludedTagCache;
     queue?: IncludedQueue;
@@ -37,17 +36,24 @@ export function openNextResolvePlugin({
       logger.debug(`OpenNext Resolve plugin for ${fnName}`);
       build.onLoad({ filter: /core\/resolve.js/g }, async (args) => {
         let contents = readFileSync(args.path, "utf-8");
-        if (overrides?.wrapper) {
+        if (overrides?.wrapper && typeof overrides.wrapper === "string") {
           contents = contents.replace(
             "../wrappers/aws-lambda.js",
             `../wrappers/${overrides.wrapper}.js`,
           );
         }
         if (overrides?.converter) {
-          contents = contents.replace(
-            "../converters/aws-apigw-v2.js",
-            `../converters/${overrides.converter}.js`,
-          );
+          if (typeof overrides.converter === "function") {
+            contents = contents.replace(
+              "../converters/aws-apigw-v2.js",
+              `../converters/dummy.js`,
+            );
+          } else {
+            contents = contents.replace(
+              "../converters/aws-apigw-v2.js",
+              `../converters/${overrides.converter}.js`,
+            );
+          }
         }
         return {
           contents,

From ae46f82a45277f10a9d060098fb5b204e9e0a365 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 23 Mar 2024 15:22:28 +0100
Subject: [PATCH 079/102] add better error when edge runtime is used inside
 node

---
 .../open-next/src/build/copyTracedFiles.ts    | 26 ++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 979285046..d34e73911 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -70,12 +70,26 @@ export async function copyTracedFiles(
 
   const computeCopyFilesForPage = (pagePath: string) => {
     const fullFilePath = `server/${pagePath}.js`;
-    const requiredFiles = JSON.parse(
-      readFileSync(
-        path.join(standaloneNextDir, `${fullFilePath}.nft.json`),
-        "utf8",
-      ),
-    );
+    let requiredFiles;
+    try {
+      requiredFiles = JSON.parse(
+        readFileSync(
+          path.join(standaloneNextDir, `${fullFilePath}.nft.json`),
+          "utf8",
+        ),
+      );
+    } catch (e) {
+      //TODO: add a link to the docs
+      throw new Error(
+        `
+--------------------------------------------------------------------------------
+${pagePath} cannot use the edge runtime.
+OpenNext requires edge runtime function to be defined in a separate function. 
+See the docs for more information on how to bundle edge runtime functions.
+--------------------------------------------------------------------------------
+        `,
+      );
+    }
     const dir = path.dirname(fullFilePath);
     extractFiles(
       requiredFiles.files,

From 1992b3f57ea5ce313dc0a6620345f07efbbd6721 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sat, 23 Mar 2024 15:43:54 +0100
Subject: [PATCH 080/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 752df776a..447e7bf5d 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.8",
+  "version": "3.0.0-rc.9",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 52a51276d185e6a5a85de6d6bbaa6efa7e98ef45 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 24 Mar 2024 11:14:03 +0100
Subject: [PATCH 081/102] fix null error on lambda hopefully

---
 packages/open-next/src/wrappers/aws-lambda.ts | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index 3477e9ff0..8b26c25d0 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -6,6 +6,8 @@ import type {
   CloudFrontRequestEvent,
   CloudFrontRequestResult,
 } from "aws-lambda";
+import { StreamCreator } from "http/openNextResponse";
+import { Writable } from "stream";
 import type { WrapperHandler } from "types/open-next";
 
 import { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
@@ -40,7 +42,21 @@ const handler: WrapperHandler =
 
     const internalEvent = await converter.convertFrom(event);
 
-    const response = await handler(internalEvent);
+    //TODO: create a simple reproduction and open an issue in the node repo
+    //This is a workaround, there is an issue in node that causes node to crash silently if the OpenNextNodeResponse stream is not consumed
+    //This does not happen everytime, it's probably caused by suspended component in ssr (either via <Suspense> or loading.tsx)
+    //Everyone that wish to create their own wrapper without a StreamCreator should implement this workaround
+    //This is not necessary if the underlying handler does not use OpenNextNodeResponse (At the moment, OpenNextNodeResponse is used by the node runtime servers and the image server)
+    const fakeStream: StreamCreator = {
+      writeHeaders: () => {
+        return new Writable();
+      },
+      onFinish: () => {
+        // Do nothing
+      },
+    };
+
+    const response = await handler(internalEvent, fakeStream);
 
     return converter.convertTo(response, event);
   };

From 790881dce43e3fc2bdb20742b5d9b62d7fea6d56 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 24 Mar 2024 11:16:30 +0100
Subject: [PATCH 082/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 447e7bf5d..b09290c9d 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.9",
+  "version": "3.0.0-rc.10",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 9cff08a9ff9c33775631f5f22601d108bb053f8d Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 24 Mar 2024 16:31:31 +0100
Subject: [PATCH 083/102] fix 500 on aws-lambda wrapper

---
 packages/open-next/src/wrappers/aws-lambda.ts | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/packages/open-next/src/wrappers/aws-lambda.ts b/packages/open-next/src/wrappers/aws-lambda.ts
index 8b26c25d0..8e5785d5a 100644
--- a/packages/open-next/src/wrappers/aws-lambda.ts
+++ b/packages/open-next/src/wrappers/aws-lambda.ts
@@ -49,7 +49,11 @@ const handler: WrapperHandler =
     //This is not necessary if the underlying handler does not use OpenNextNodeResponse (At the moment, OpenNextNodeResponse is used by the node runtime servers and the image server)
     const fakeStream: StreamCreator = {
       writeHeaders: () => {
-        return new Writable();
+        return new Writable({
+          write: (_chunk, _encoding, callback) => {
+            callback();
+          },
+        });
       },
       onFinish: () => {
         // Do nothing

From 7f5ba6f58a9c0df94152cfa9c5518cb4fea7710e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Sun, 24 Mar 2024 16:32:03 +0100
Subject: [PATCH 084/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index b09290c9d..a55b5226f 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.10",
+  "version": "3.0.0-rc.11",
   "bin": {
     "open-next": "./dist/index.js"
   },

From daaeb3863be754e9f697c83c66056fc6b44a8d86 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 27 Mar 2024 15:01:57 +0100
Subject: [PATCH 085/102] fix duplex for request in node

---
 packages/open-next/src/plugins/edge.ts | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index 7243028f6..b05ac4b15 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -109,9 +109,24 @@ import {AsyncLocalStorage} from "node:async_hooks";
 globalThis.AsyncLocalStorage = AsyncLocalStorage;
 ${
   isInCloudfare
-    ? ""
-    : `import {readFileSync} from "node:fs";
-import path from "node:path";`
+    ? ``
+    : `
+import {readFileSync} from "node:fs";
+import path from "node:path";
+function addDuplexToInit(init) {
+  return typeof init === 'undefined' ||
+    (typeof init === 'object' && init.duplex === undefined)
+    ? { duplex: 'half', ...init }
+    : init
+}
+// We need to override Request to add duplex to the init, it seems Next expects it to work like this
+class OverrideRequest extends Request {
+  constructor(input, init) {
+    super(input, addDuplexToInit(init))
+  }
+}
+globalThis.Request = OverrideRequest;
+`
 }
 ${wasmFiles
   .map((file) =>

From 90c59d9dfdd5f4a8305dede4364e19bd9061a98d Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 27 Mar 2024 16:16:51 +0100
Subject: [PATCH 086/102] fix & refactor middleware response headers

---
 packages/open-next/src/adapters/middleware.ts |  1 -
 .../plugins/without-routing/requestHandler.ts | 10 -------
 .../src/converters/aws-cloudfront.ts          |  7 -----
 packages/open-next/src/core/requestHandler.ts | 26 ++++++++++++++++---
 .../open-next/src/core/routing/middleware.ts  |  3 ++-
 packages/open-next/src/core/routingHandler.ts | 20 +++++++++-----
 6 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/packages/open-next/src/adapters/middleware.ts b/packages/open-next/src/adapters/middleware.ts
index b47e8d451..8f6ca05bd 100644
--- a/packages/open-next/src/adapters/middleware.ts
+++ b/packages/open-next/src/adapters/middleware.ts
@@ -63,7 +63,6 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
     return {
       type: "middleware",
       internalEvent: result.internalEvent,
-      headers: result.headers,
       isExternalRewrite: result.isExternalRewrite,
       origin,
     };
diff --git a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
index 8c7e9a020..5d2cbc511 100644
--- a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
+++ b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts
@@ -5,19 +5,9 @@ import { MiddlewareOutputEvent } from "../../../core/routingHandler";
 declare const internalEvent: InternalEvent;
 
 //#override withRouting
-const overwrittenResponseHeaders = Object.entries(internalEvent.headers).reduce(
-  (acc, [key, value]) => {
-    if (!key.startsWith("x-middleware-response-")) {
-      return acc;
-    }
-    return { ...acc, [key.replace("x-middleware-response-", "")]: value };
-  },
-  {},
-);
 const preprocessResult: MiddlewareOutputEvent = {
   internalEvent: internalEvent,
   isExternalRewrite: false,
-  headers: overwrittenResponseHeaders,
   origin: false,
 };
 //#endOverride
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index 00ba3ca2b..8f021dc8b 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -101,12 +101,6 @@ async function convertToCloudFrontRequestResult(
       : result.headers;
   if (result.type === "middleware") {
     const { method, clientIp, origin } = originalRequest.Records[0].cf.request;
-    const overwrittenResponseHeaders: Record<string, OutgoingHttpHeader> = {};
-    Object.entries(result.headers).forEach(([key, value]) => {
-      //TODO: handle those headers inside plugin
-      if (value)
-        overwrittenResponseHeaders[`x-middleware-response-${key}`] = value;
-    });
 
     // Handle external rewrite
     if (result.isExternalRewrite) {
@@ -151,7 +145,6 @@ async function convertToCloudFrontRequestResult(
       ),
       headers: convertToCloudfrontHeaders({
         ...responseHeaders,
-        ...overwrittenResponseHeaders,
         host,
       }),
       origin: origin?.custom
diff --git a/packages/open-next/src/core/requestHandler.ts b/packages/open-next/src/core/requestHandler.ts
index 7efc7ee8b..f48912831 100644
--- a/packages/open-next/src/core/requestHandler.ts
+++ b/packages/open-next/src/core/requestHandler.ts
@@ -28,7 +28,6 @@ export async function openNextHandler(
   let preprocessResult: InternalResult | MiddlewareOutputEvent = {
     internalEvent: internalEvent,
     isExternalRewrite: false,
-    headers: {},
     origin: false,
   };
   try {
@@ -38,9 +37,28 @@ export async function openNextHandler(
   }
   //#endOverride
 
+  const headers =
+    "type" in preprocessResult
+      ? preprocessResult.headers
+      : preprocessResult.internalEvent.headers;
+
+  const overwrittenResponseHeaders = Object.entries(
+    "type" in preprocessResult
+      ? preprocessResult.headers
+      : preprocessResult.internalEvent.headers,
+  ).reduce((acc, [key, value]) => {
+    if (!key.startsWith("x-middleware-response-")) {
+      return acc;
+    } else {
+      const newKey = key.replace("x-middleware-response-", "");
+      delete headers[key];
+      headers[newKey] = value;
+      return { ...acc, [newKey]: value };
+    }
+  }, {});
+
   if ("type" in preprocessResult) {
     // res is used only in the streaming case
-    const headers = preprocessResult.headers;
     const res = createServerResponse(internalEvent, headers, responseStreaming);
     res.statusCode = preprocessResult.statusCode;
     res.flushHeaders();
@@ -58,7 +76,7 @@ export async function openNextHandler(
       // 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
       // 2. We could alter the lastModified time of our cache to make next believe that the cache is fresh, but this could cause issues with stale data since the cdn will cache the stale data as if it was fresh
       // 3. OUR CHOICE: We could pass a purpose prefetch header to the serverless function to make next believe that the request is a prefetch request and not trigger revalidation (This could potentially break in the future if next changes the behavior of prefetch requests)
-      headers: { ...preprocessedEvent.headers, purpose: "prefetch" },
+      headers: { ...headers, purpose: "prefetch" },
       body: preprocessedEvent.body,
       remoteAddress: preprocessedEvent.remoteAddress,
     };
@@ -68,7 +86,7 @@ export async function openNextHandler(
       const req = new IncomingMessage(reqProps);
       const res = createServerResponse(
         preprocessedEvent,
-        preprocessedResult.headers as Record<string, string | string[]>,
+        overwrittenResponseHeaders,
         responseStreaming,
       );
 
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index d52c4a4ae..10da8351b 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -147,7 +147,8 @@ export async function handleMiddleware(
   let middlewareQueryString = internalEvent.query;
   let newUrl = internalEvent.url;
   if (rewriteUrl) {
-    if (isExternal(rewriteUrl, internalEvent.headers.host)) {
+    // If not a string, it should probably throw
+    if (isExternal(rewriteUrl, internalEvent.headers.host as string)) {
       newUrl = rewriteUrl;
       rewritten = true;
       externalRewrite = true;
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index bb5a1358c..b43453179 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -4,7 +4,6 @@ import {
   PrerenderManifest,
   RoutesManifest,
 } from "config/index";
-import type { OutgoingHttpHeaders } from "http";
 import { InternalEvent, InternalResult, Origin } from "types/open-next";
 
 import { debug } from "../adapters/logger";
@@ -19,7 +18,6 @@ import { handleMiddleware } from "./routing/middleware";
 
 export interface MiddlewareOutputEvent {
   internalEvent: InternalEvent;
-  headers: OutgoingHttpHeaders;
   isExternalRewrite: boolean;
   origin: Origin | false;
 }
@@ -86,12 +84,22 @@ export default async function routingHandler(
     isExternalRewrite = fallbackRewrites.isExternalRewrite;
   }
 
+  // We apply the headers from the middleware response last
+  Object.entries({
+    ...middlewareResponseHeaders,
+    ...nextHeaders,
+  }).forEach(([key, value]) => {
+    if (value) {
+      internalEvent.headers[`x-middleware-response-${key}`] = Array.isArray(
+        value,
+      )
+        ? value.join(",")
+        : value;
+    }
+  });
+
   return {
     internalEvent,
-    headers: {
-      ...nextHeaders,
-      ...middlewareResponseHeaders,
-    },
     isExternalRewrite,
     origin: false,
   };

From 93ee01f839aaf1918e6623b95812bce9d5eddad7 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 27 Mar 2024 16:31:41 +0100
Subject: [PATCH 087/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index a55b5226f..26f48310f 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.11",
+  "version": "3.0.0-rc.12",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 245c13962aa24c0700c5ac0e323c1fa875256a63 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 4 Apr 2024 12:29:44 +0200
Subject: [PATCH 088/102] Sync

---
 packages/open-next/CHANGELOG.md               |  8 +++
 .../adapters/image-optimization-adapter.ts    | 68 ++++++++++++-------
 packages/open-next/src/build.ts               |  6 +-
 .../src/converters/aws-cloudfront.ts          | 48 ++++++++++++-
 .../open-next/src/core/routing/matcher.ts     | 17 ++++-
 .../open-next/src/core/routing/middleware.ts  |  1 -
 packages/open-next/src/core/routingHandler.ts |  3 +
 packages/tests-unit/CHANGELOG.md              |  9 +++
 .../tests-unit/tests/event-mapper.test.ts     | 58 ++++++++++++++++
 9 files changed, 191 insertions(+), 27 deletions(-)

diff --git a/packages/open-next/CHANGELOG.md b/packages/open-next/CHANGELOG.md
index 0d3f871c2..e7e7602e9 100644
--- a/packages/open-next/CHANGELOG.md
+++ b/packages/open-next/CHANGELOG.md
@@ -1,5 +1,13 @@
 # open-next
 
+## 2.3.9
+
+### Patch Changes
+
+- 5c80192: Fix incorrect 200 with wrong buildId for page router
+- 2118ba2: Feat add a static etag for Image Optimization
+- 6a3c69a: fix(edge): remove read-only and blacklisted headers from cloudfront response
+
 ## 2.3.8
 
 ### Patch Changes
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index a4d8aa647..cfdedcc38 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -1,3 +1,4 @@
+import { createHash } from "node:crypto";
 import {
   IncomingMessage,
   OutgoingHttpHeaders,
@@ -8,7 +9,7 @@ import path from "node:path";
 import { Writable } from "node:stream";
 
 import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
-import { loadConfig } from "config/util.js";
+import { loadBuildId, loadConfig } from "config/util.js";
 import { OpenNextNodeResponse, StreamCreator } from "http/openNextResponse.js";
 // @ts-ignore
 import { defaultConfig } from "next/dist/server/config-shared";
@@ -31,6 +32,7 @@ const { BUCKET_NAME, BUCKET_KEY_PREFIX } = process.env;
 setNodeEnv();
 const nextDir = path.join(__dirname, ".next");
 const config = loadConfig(nextDir);
+const buildId = loadBuildId(nextDir);
 const nextConfig = {
   ...defaultConfig,
   images: {
@@ -68,6 +70,20 @@ export async function defaultHandler(
       headers,
       queryString === null ? undefined : queryString,
     );
+    let etag: string | undefined;
+    // We don't cache any images, so in order to be able to return 304 responses, we compute an ETag from what is assumed to be static
+    if (process.env.OPENNEXT_STATIC_ETAG) {
+      etag = computeEtag(imageParams);
+    }
+    if (etag && headers["if-none-match"] === etag) {
+      return {
+        statusCode: 304,
+        headers: {},
+        body: "",
+        isBase64Encoded: false,
+        type: "core",
+      };
+    }
     const result = await optimizeImage(
       headers,
       imageParams,
@@ -75,7 +91,7 @@ export async function defaultHandler(
       downloadHandler,
     );
 
-    return buildSuccessResponse(result, streamCreator);
+    return buildSuccessResponse(result, etag);
   } catch (e: any) {
     return buildFailureResponse(e, streamCreator);
   }
@@ -119,33 +135,39 @@ function validateImageParams(
   return imageParams;
 }
 
-function buildSuccessResponse(
-  result: any,
-  streamCreator?: StreamCreator,
-): InternalResult {
-  if (streamCreator) {
-    const response = new OpenNextNodeResponse(
-      () => void 0,
-      async () => void 0,
-      streamCreator,
-    );
-    response.writeHead(200, {
-      Vary: "Accept",
-      "Cache-Control": `public,max-age=${result.maxAge},immutable`,
-      "Content-Type": result.contentType,
-    });
-    response.end(result.buffer);
+function computeEtag(imageParams: {
+  href: string;
+  width: number;
+  quality: number;
+}) {
+  return createHash("sha1")
+    .update(
+      JSON.stringify({
+        href: imageParams.href,
+        width: imageParams.width,
+        quality: imageParams.quality,
+        buildId,
+      }),
+    )
+    .digest("base64");
+}
+
+function buildSuccessResponse(result: any, etag?: string): InternalResult {
+  const headers: Record<string, string> = {
+    Vary: "Accept",
+    "Content-Type": result.contentType,
+    "Cache-Control": `public,max-age=${result.maxAge},immutable`,
+  };
+  if (etag) {
+    headers["ETag"] = etag;
   }
+
   return {
     type: "core",
     statusCode: 200,
     body: result.buffer.toString("base64"),
     isBase64Encoded: true,
-    headers: {
-      Vary: "Accept",
-      "Cache-Control": `public,max-age=${result.maxAge},immutable`,
-      "Content-Type": result.contentType,
-    },
+    headers,
   };
 }
 
diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 22da479f4..5131d0c53 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -400,12 +400,16 @@ async function createImageOptimizationBundle(config: OpenNextConfig) {
     options,
   );
 
-  // Copy over .next/required-server-files.json file
+  // Copy over .next/required-server-files.json file and BUILD_ID
   fs.mkdirSync(path.join(outputPath, ".next"));
   fs.copyFileSync(
     path.join(appBuildOutputPath, ".next/required-server-files.json"),
     path.join(outputPath, ".next/required-server-files.json"),
   );
+  fs.copyFileSync(
+    path.join(appBuildOutputPath, ".next/BUILD_ID"),
+    path.join(outputPath, ".next/BUILD_ID"),
+  );
 
   // Sharp provides pre-build binaries for all platforms. https://github.com/lovell/sharp/blob/main/docs/install.md#cross-platform
   // Target should be same as used by Lambda, see https://github.com/sst/sst/blob/ca6f763fdfddd099ce2260202d0ce48c72e211ea/packages/sst/src/constructs/NextjsSite.ts#L114
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index 8f021dc8b..216cbee28 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -19,6 +19,47 @@ import {
 } from "../core/routing/util";
 import { MiddlewareOutputEvent } from "../core/routingHandler";
 
+const CloudFrontBlacklistedHeaders = [
+  // Disallowed headers, see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-function-restrictions-all.html#function-restrictions-disallowed-headers
+  "connection",
+  "expect",
+  "keep-alive",
+  "proxy-authenticate",
+  "proxy-authorization",
+  "proxy-connection",
+  "trailer",
+  "upgrade",
+  "x-accel-buffering",
+  "x-accel-charset",
+  "x-accel-limit-rate",
+  "x-accel-redirect",
+  /x-amz-cf-(.*)/,
+  "x-amzn-auth",
+  "x-amzn-cf-billing",
+  "x-amzn-cf-id",
+  "x-amzn-cf-xff",
+  "x-amzn-errortype",
+  "x-amzn-fle-profile",
+  "x-amzn-header-count",
+  "x-amzn-header-order",
+  "x-amzn-lambda-integration-tag",
+  "x-amzn-requestid",
+  /x-edge-(.*)/,
+  "x-cache",
+  "x-forwarded-proto",
+  "x-real-ip",
+
+  // Read-only headers, see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-function-restrictions-all.html#function-restrictions-read-only-headers
+  "accept-encoding",
+  "content-length",
+  "if-modified-since",
+  "if-none-match",
+  "if-range",
+  "if-unmodified-since",
+  "transfer-encoding",
+  "via",
+];
+
 function normalizeCloudFrontRequestEventHeaders(
   rawHeaders: CloudFrontHeaders,
 ): Record<string, string> {
@@ -69,7 +110,12 @@ function convertToCloudfrontHeaders(
 ) {
   const cloudfrontHeaders: CloudFrontHeaders = {};
   Object.entries(headers)
-    .filter(([key]) => key.toLowerCase() !== "content-length")
+    .filter(
+      ([key]) =>
+        !CloudFrontBlacklistedHeaders.some((header) =>
+          typeof header === "string" ? header === key : header.test(key),
+        ),
+    )
     .forEach(([key, value]) => {
       if (key === "set-cookie") {
         const cookies = parseCookies(`${value}`);
diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts
index 95a308a1e..20073a378 100644
--- a/packages/open-next/src/core/routing/matcher.ts
+++ b/packages/open-next/src/core/routing/matcher.ts
@@ -256,9 +256,24 @@ export function handleRedirects(
   }
 }
 
-export function fixDataPage(internalEvent: InternalEvent, buildId: string) {
+export function fixDataPage(
+  internalEvent: InternalEvent,
+  buildId: string,
+): InternalEvent | InternalResult {
   const { rawPath, query } = internalEvent;
   const dataPattern = `/_next/data/${buildId}`;
+  // Return 404 for data requests that don't match the buildId
+  if (rawPath.startsWith("/_next/data") && !rawPath.startsWith(dataPattern)) {
+    return {
+      type: internalEvent.type,
+      statusCode: 404,
+      body: "{}",
+      headers: {
+        "Content-Type": "application/json",
+      },
+      isBase64Encoded: false,
+    };
+  }
 
   if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) {
     let newPath = rawPath.replace(dataPattern, "").replace(/\.json$/, "");
diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 10da8351b..9cb9c67f3 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -50,7 +50,6 @@ function normalizeLocalePath(pathname: string) {
 
   return locales && !pathname.endsWith("/") ? `${pathname}/` : pathname;
 }
-
 //    if res.end() is return, the parent needs to return and not process next server
 export async function handleMiddleware(
   internalEvent: InternalEvent,
diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts
index b43453179..9b71fcdd7 100644
--- a/packages/open-next/src/core/routingHandler.ts
+++ b/packages/open-next/src/core/routingHandler.ts
@@ -28,6 +28,9 @@ export default async function routingHandler(
   const nextHeaders = addNextConfigHeaders(event, ConfigHeaders) ?? {};
 
   let internalEvent = fixDataPage(event, BuildId);
+  if ("statusCode" in internalEvent) {
+    return internalEvent;
+  }
 
   const redirect = handleRedirects(internalEvent, RoutesManifest.redirects);
   if (redirect) {
diff --git a/packages/tests-unit/CHANGELOG.md b/packages/tests-unit/CHANGELOG.md
index c4a4ffbee..d6d113798 100644
--- a/packages/tests-unit/CHANGELOG.md
+++ b/packages/tests-unit/CHANGELOG.md
@@ -4,6 +4,15 @@
 
 ### Patch Changes
 
+- Updated dependencies [5c80192]
+- Updated dependencies [2118ba2]
+- Updated dependencies [6a3c69a]
+  - open-next@2.3.9
+
+## null
+
+### Patch Changes
+
 - Updated dependencies [8cfb801]
   - open-next@2.3.8
 
diff --git a/packages/tests-unit/tests/event-mapper.test.ts b/packages/tests-unit/tests/event-mapper.test.ts
index 312a2c1bc..c85df4093 100644
--- a/packages/tests-unit/tests/event-mapper.test.ts
+++ b/packages/tests-unit/tests/event-mapper.test.ts
@@ -111,5 +111,63 @@ describe("convertTo", () => {
         ],
       });
     });
+
+    describe("blacklisted headers", () => {
+      it("should remove all blacklisted or read-only headers from the response", () => {
+        const response = convertTo({
+          body: "",
+          headers: {
+            Connection: "keep-alive",
+            expect: "100-continue",
+            "keep-Alive": "timeout=5, max=100",
+            "Proxy-Authenticate": "Basic",
+            "proxy-authorization": "Basic",
+            "proxy-connection": "keep-alive",
+            trailer: "Max-Forwards",
+            Upgrade: "HTTP/2.0",
+            "X-accel-buffering": "no",
+            "X-accel-charset": "UTF-8",
+            "x-accel-limit-rate": "1000",
+            "X-accel-redirect": "http://example.com",
+            "X-amz-cf-id": "example",
+            "x-amzn-auth": "example",
+            "x-Amzn-cf-billing": "example",
+            "x-Amzn-cf-id": "example",
+            "x-Amzn-Cf-xff": "example",
+            "x-amzn-Errortype": "example",
+            "x-amzn-fle-Profile": "example",
+            "x-amzn-header-Count": "example",
+            "x-amzn-Header-order": "example",
+            "X-Amzn-Lambda-Integration-tag": "example",
+            "x-amzn-Requestid": "example",
+            "x-edge-Location": "example",
+            "X-Cache": "Hit from cloudfront",
+            "X-Forwarded-proto": "https",
+            "x-Real-ip": "example",
+            "Accept-encoding": "gzip",
+            "content-length": "100",
+            "if-modified-Since": "example",
+            "if-none-match": "example",
+            "if-range": "example",
+            "if-unmodified-since": "example",
+            "transfer-encoding": "example",
+            via: "1.1 abc123.cloudfront.net (CloudFront)",
+            "x-powered-by": "Next.js",
+          },
+          isBase64Encoded: false,
+          statusCode: 200,
+          type: "cf",
+        }) as CloudFrontRequestResult;
+
+        expect(response?.headers).toStrictEqual({
+          "x-powered-by": [
+            {
+              key: "x-powered-by",
+              value: "Next.js",
+            },
+          ],
+        });
+      });
+    });
   });
 });

From e498e28088edc976bf15cc8a19f93c8a76d73a1c Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 4 Apr 2024 12:32:39 +0200
Subject: [PATCH 089/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 26f48310f..c1a38a051 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.12",
+  "version": "3.0.0-rc.13",
   "bin": {
     "open-next": "./dist/index.js"
   },

From c78211707b7c6ef286ff4151788b08f3992c2bac Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 10 Apr 2024 10:27:47 +0200
Subject: [PATCH 090/102] removed specific lamda streaming hack It's been fixed
 upstream

---
 .../open-next/src/http/openNextResponse.ts    | 17 ++------
 .../src/wrappers/aws-lambda-streaming.ts      | 42 ++-----------------
 2 files changed, 7 insertions(+), 52 deletions(-)

diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts
index ba5648ef4..72501202e 100644
--- a/packages/open-next/src/http/openNextResponse.ts
+++ b/packages/open-next/src/http/openNextResponse.ts
@@ -14,7 +14,6 @@ export interface StreamCreator {
   // Just to fix an issue with aws lambda streaming with empty body
   onWrite?: () => void;
   onFinish: () => void;
-  waitForFirstWrite?: boolean;
 }
 
 // We only need to implement the methods that are used by next.js
@@ -146,18 +145,8 @@ export class OpenNextNodeResponse extends Transform {
     if (!this.headersSent) {
       this.flushHeaders();
     }
-    if (this.streamCreator?.waitForFirstWrite && !this.hasDoneFirstWrite) {
-      const waitTime = parseInt(
-        process.env.STREAMING_INITIAL_WRITE_WAIT_TIME ?? "25",
-      );
-      new Promise((resolve) => setTimeout(resolve, waitTime)).then(() => {
-        this._internalWrite(chunk, encoding);
-        this.hasDoneFirstWrite = true;
-        callback();
-      });
-    } else {
-      this._internalWrite(chunk, encoding);
-      callback();
-    }
+
+    this._internalWrite(chunk, encoding);
+    callback();
   }
 }
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 8e3a2fdd3..0d6ca4f0f 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -32,7 +32,6 @@ const handler: WrapperHandler = async (handler, converter) =>
 
       const internalEvent = await converter.convertFrom(event);
       let _hasWriten = false;
-      let _headersSent = false;
 
       //Handle compression
       const acceptEncoding = internalEvent.headers?.["accept-encoding"] ?? "";
@@ -70,56 +69,23 @@ const handler: WrapperHandler = async (handler, converter) =>
 
       const streamCreator: StreamCreator = {
         writeHeaders: (_prelude) => {
-          responseStream.cork();
-          // FIXME: This is extracted from the docker lambda node 18 runtime
-          // https://gist.github.com/conico974/13afd708af20711b97df439b910ceb53#file-index-mjs-L921-L932
-          // We replace their write with ours which are inside a setImmediate
-          // This way it seems to work all the time
-          // I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
-
-          responseStream.setContentType(
-            "application/vnd.awslambda.http-integration-response",
-          );
           _prelude.headers["content-encoding"] = contentEncoding;
-          const prelude = JSON.stringify(_prelude);
-
-          // Try to flush the buffer to the client to invoke
-          // the streaming. This does not work 100% of the time.
-
-          responseStream.write("\n\n");
-
-          responseStream.write(prelude);
-
-          responseStream.write(new Uint8Array(8));
 
-          if (responseStream.writableCorked) {
-            for (let i = 0; i < responseStream.writableCorked; i++) {
-              // For some reason, putting this in a setImmediate makes it work more often
-              // process.nextTick does not, which should be what we should use
-              setImmediate(() => {
-                responseStream.uncork();
-              });
-            }
-          }
-
-          _headersSent = true;
+          responseStream = awslambda.HttpResponseStream.from(
+            responseStream,
+            _prelude,
+          );
 
           return compressedStream ?? responseStream;
         },
         onWrite: () => {
           _hasWriten = true;
-          // Force flushing data, seems necessary for aws lambda streaming to work reliably
-          // We need to reevaluate this if it causes issues on other platforms
-          if (compressedStream?.writableCorked) {
-            compressedStream?.uncork();
-          }
         },
         onFinish: () => {
           if (!_hasWriten) {
             compressedStream?.end(new Uint8Array(8));
           }
         },
-        waitForFirstWrite: true,
       };
 
       const response = await handler(internalEvent, streamCreator);

From 9f7e1084b667eaf533adc941e4c0a950a70977fd Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 10 Apr 2024 11:27:57 +0200
Subject: [PATCH 091/102] add geo in middleware

---
 packages/open-next/src/core/routing/middleware.ts | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/packages/open-next/src/core/routing/middleware.ts b/packages/open-next/src/core/routing/middleware.ts
index 9cb9c67f3..895a4d04e 100644
--- a/packages/open-next/src/core/routing/middleware.ts
+++ b/packages/open-next/src/core/routing/middleware.ts
@@ -73,6 +73,13 @@ export async function handleMiddleware(
   const middleware = await import("./middleware.mjs");
 
   const result: Response = await middleware.default({
+    geo: {
+      city: internalEvent.headers["x-open-next-city"],
+      country: internalEvent.headers["x-open-next-country"],
+      region: internalEvent.headers["x-open-next-region"],
+      latitude: internalEvent.headers["x-open-next-latitude"],
+      longitude: internalEvent.headers["x-open-next-longitude"],
+    },
     headers: internalEvent.headers,
     method: internalEvent.method || "GET",
     nextConfig: {

From 3751bf750b3802359d714fa0ce911d2f2510ac6e Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Wed, 10 Apr 2024 15:49:00 +0200
Subject: [PATCH 092/102] added helpers function for config file Better typing
 as well

---
 .../open-next/src/build/createServerBundle.ts |   1 +
 .../src/build/edge/createEdgeBundle.ts        |   3 +-
 .../open-next/src/helpers/withCloudflare.ts   | 118 ++++++++++++++++++
 packages/open-next/src/helpers/withSST.ts     |  65 ++++++++++
 packages/open-next/src/types/open-next.ts     |  28 ++++-
 5 files changed, 209 insertions(+), 6 deletions(-)
 create mode 100644 packages/open-next/src/helpers/withCloudflare.ts
 create mode 100644 packages/open-next/src/helpers/withSST.ts

diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index ed2233dc3..e1e0474c1 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -107,6 +107,7 @@ export async function createServerBundle(
   // Generate default function
   await generateBundle("default", config, options, {
     ...defaultFn,
+    // @ts-expect-error - Those string are RouteTemplate
     routes: Array.from(remainingRoutes),
     patterns: ["*"],
   });
diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts
index 367e1de0e..03e6af27f 100644
--- a/packages/open-next/src/build/edge/createEdgeBundle.ts
+++ b/packages/open-next/src/build/edge/createEdgeBundle.ts
@@ -7,6 +7,7 @@ import { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
 import {
   DefaultOverrideOptions,
   IncludedConverter,
+  RouteTemplate,
   SplittedFunctionOptions,
 } from "types/open-next";
 
@@ -125,7 +126,7 @@ export async function generateEdgeBundle(
 
   // Find functions
   const functions = Object.values(middlewareManifest.functions).filter((fn) =>
-    fnOptions.routes.includes(fn.name),
+    fnOptions.routes.includes(fn.name as RouteTemplate),
   );
 
   if (functions.length > 1) {
diff --git a/packages/open-next/src/helpers/withCloudflare.ts b/packages/open-next/src/helpers/withCloudflare.ts
new file mode 100644
index 000000000..37f486d28
--- /dev/null
+++ b/packages/open-next/src/helpers/withCloudflare.ts
@@ -0,0 +1,118 @@
+import {
+  FunctionOptions,
+  OpenNextConfig,
+  RouteTemplate,
+  SplittedFunctionOptions,
+} from "types/open-next";
+
+type CloudflareCompatibleFunction<Placement extends "regional" | "global"> =
+  Placement extends "regional"
+    ? FunctionOptions & {
+        placement: "regional";
+      }
+    : { placement: "global" };
+
+type CloudflareCompatibleRoutes<Placement extends "regional" | "global"> =
+  Placement extends "regional"
+    ? {
+        placement: "regional";
+        routes: RouteTemplate[];
+        patterns: string[];
+      }
+    : {
+        placement: "global";
+        routes: `app/${string}/route`;
+        patterns: string;
+      };
+
+type CloudflareCompatibleSplittedFunction<
+  Placement extends "regional" | "global" = "regional",
+> = CloudflareCompatibleRoutes<Placement> &
+  CloudflareCompatibleFunction<Placement>;
+
+type CloudflareConfig<
+  Fn extends Record<
+    string,
+    CloudflareCompatibleSplittedFunction<"global" | "regional">
+  >,
+> = {
+  default: CloudflareCompatibleFunction<"regional">;
+  functions?: Fn;
+} & Omit<OpenNextConfig, "default" | "functions" | "middleware">;
+
+type InterpolatedSplittedFunctionOptions<
+  Fn extends Record<
+    string,
+    CloudflareCompatibleSplittedFunction<"global" | "regional">
+  >,
+> = {
+  [K in keyof Fn]: SplittedFunctionOptions;
+};
+
+/**
+ * This function makes it easier to use Cloudflare with OpenNext.
+ * All options are already restricted to Cloudflare compatible options.
+ * @example
+ * ```ts
+ export default withCloudflare({
+  default: {
+    placement: "regional",
+    runtime: "node",
+  },
+  functions: {
+    api: {
+      placement: "regional",
+      runtime: "node",
+      routes: ["app/api/test/route", "page/api/otherApi"],
+      patterns: ["/api/*"],
+    },
+    global: {
+      placement: "global",
+      runtime: "edge",
+      routes: "app/test/page",
+      patterns: "/page",
+    },
+  },
+});
+ * ```
+ */
+export function withCloudflare<
+  Fn extends Record<
+    string,
+    CloudflareCompatibleSplittedFunction<"global" | "regional">
+  >,
+  Key extends keyof Fn,
+>(config: CloudflareConfig<Fn>) {
+  const functions = Object.entries(config.functions ?? {}).reduce(
+    (acc, [name, fn]) => {
+      const _name = name as Key;
+      acc[_name] =
+        fn.placement === "global"
+          ? {
+              placement: "global",
+              runtime: "edge",
+              routes: [fn.routes],
+              patterns: [fn.patterns],
+              override: {
+                wrapper: "cloudflare",
+                converter: "edge",
+              },
+            }
+          : { ...fn, placement: "regional" };
+      return acc;
+    },
+    {} as InterpolatedSplittedFunctionOptions<Fn>,
+  );
+  return {
+    default: config.default,
+    functions: functions,
+    middleware: {
+      external: true,
+      originResolver: "pattern-env",
+      override: {
+        wrapper: "cloudflare",
+        converter: "edge",
+      },
+    },
+  } satisfies OpenNextConfig;
+}
diff --git a/packages/open-next/src/helpers/withSST.ts b/packages/open-next/src/helpers/withSST.ts
new file mode 100644
index 000000000..afe11b33e
--- /dev/null
+++ b/packages/open-next/src/helpers/withSST.ts
@@ -0,0 +1,65 @@
+import {
+  FunctionOptions,
+  OpenNextConfig,
+  RouteTemplate,
+} from "types/open-next";
+
+type SSTCompatibleFunction = FunctionOptions & {
+  override?: {
+    wrapper?: "aws-lambda-streaming" | "aws-lambda";
+    converter?: "aws-apigw-v2" | "aws-apigw-v1" | "aws-cloudfront";
+  };
+};
+
+type SSTCompatibleSplittedFunction = {
+  routes: RouteTemplate[];
+  patterns: string[];
+} & SSTCompatibleFunction;
+
+type SSTCompatibleConfig<
+  Fn extends Record<string, SSTCompatibleSplittedFunction>,
+> = {
+  default: SSTCompatibleFunction;
+  functions?: Fn;
+  middleware?: {
+    external: true;
+  };
+} & Pick<
+  OpenNextConfig,
+  | "dangerous"
+  | "appPath"
+  | "buildCommand"
+  | "buildOutputPath"
+  | "packageJsonPath"
+>;
+
+/**
+ * This function makes it more straightforward to use SST with OpenNext.
+ * All options are already restricted to SST compatible options only.
+ * Some options not present here can be used in SST, but it's an advanced use case that
+ * can easily break the deployment. If you need to use those options, you should just provide a
+ * compatible OpenNextConfig inside your `open-next.config.ts` file.
+ * @example
+ * ```ts
+  export default withSST({
+    default: {
+      override: {
+        wrapper: "aws-lambda-streaming",
+      },
+    },
+    functions: {
+      "api/*": {
+        routes: ["app/api/test/route", "page/api/otherApi"],
+        patterns: ["/api/*"],
+      },
+    },
+  });
+ * ```
+ */
+export function withSST<
+  Fn extends Record<string, SSTCompatibleSplittedFunction>,
+>(config: SSTCompatibleConfig<Fn>) {
+  return {
+    ...config,
+  } satisfies OpenNextConfig;
+}
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index 323d534f7..dc074ed5b 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -206,13 +206,24 @@ export interface FunctionOptions extends DefaultFunctionOptions {
   experimentalBundledNextServer?: boolean;
 }
 
+export type RouteTemplate =
+  | `app/${string}/route`
+  | `app/${string}/page`
+  | `app/page`
+  | `app/route`
+  | `page/${string}`;
+
 export interface SplittedFunctionOptions extends FunctionOptions {
   /**
    * Here you should specify all the routes you want to use.
-   * If not provided, all the routes will be used.
-   * @default []
+   * For app routes, you should use the `app/${name}/route` format or `app/${name}/page` for pages.
+   * For pages, you should use the `page/${name}` format.
+   * @example
+   * ```ts
+   * routes: ["app/api/test/route", "app/page", "page/admin"]
+   * ```
    */
-  routes: string[];
+  routes: RouteTemplate[];
 
   /**
    * Cloudfront compatible patterns.
@@ -273,8 +284,15 @@ export interface OpenNextConfig {
    */
   imageOptimization?: DefaultFunctionOptions & {
     loader?: "s3" | LazyLoadedOverride<ImageLoader>;
-    arch: "x64" | "arm64";
-    nodeVersion: "18" | "20";
+    /**
+     * @default "arm64"
+     */
+    arch?: "x64" | "arm64";
+    /**
+     * @default "18"
+     */
+
+    nodeVersion?: "18" | "20";
   };
 
   /**

From d0c87c3870fe8d758ee7ca262c177831166c632c Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 11 Apr 2024 09:37:18 +0200
Subject: [PATCH 093/102] fix for 14.2

---
 packages/open-next/src/build/copyTracedFiles.ts | 7 ++++++-
 packages/open-next/src/types/open-next.ts       | 6 ++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index d34e73911..979b4b123 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -120,7 +120,12 @@ See the docs for more information on how to bundle edge runtime functions.
 
   if (hasAppDir) {
     //App dir
-    computeCopyFilesForPage("app/_not-found");
+    try {
+      computeCopyFilesForPage("app/_not-found");
+    } catch (e) {
+      // In next 14.2.0, _not-found is at 'app/_not-found/page'
+      computeCopyFilesForPage("app/_not-found/page");
+    }
   }
 
   //Files we actually want to include
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index dc074ed5b..b93b9e84e 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -201,6 +201,8 @@ export interface FunctionOptions extends DefaultFunctionOptions {
   /**
    * Bundle Next server into a single file.
    * This results in a way smaller bundle but it might break for some cases.
+   * This option will probably break on every new Next.js version.
+   * Broken in 14.2+
    * @default false
    */
   experimentalBundledNextServer?: boolean;
@@ -211,7 +213,7 @@ export type RouteTemplate =
   | `app/${string}/page`
   | `app/page`
   | `app/route`
-  | `page/${string}`;
+  | `pages/${string}`;
 
 export interface SplittedFunctionOptions extends FunctionOptions {
   /**
@@ -220,7 +222,7 @@ export interface SplittedFunctionOptions extends FunctionOptions {
    * For pages, you should use the `page/${name}` format.
    * @example
    * ```ts
-   * routes: ["app/api/test/route", "app/page", "page/admin"]
+   * routes: ["app/api/test/route", "app/page", "pages/admin"]
    * ```
    */
   routes: RouteTemplate[];

From 370547bf49dd7cd534e58cdf1854e8e92df9a739 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 11 Apr 2024 09:38:48 +0200
Subject: [PATCH 094/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index c1a38a051..610799a4d 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.13",
+  "version": "3.0.0-rc.14",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 905a6e4ef9927563a9af05dce94a071ae22c377a Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 11 Apr 2024 11:08:02 +0200
Subject: [PATCH 095/102] fix redirect lambda streaming

---
 .../open-next/src/wrappers/aws-lambda-streaming.ts  | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 0d6ca4f0f..432e19bbf 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -34,7 +34,7 @@ const handler: WrapperHandler = async (handler, converter) =>
       let _hasWriten = false;
 
       //Handle compression
-      const acceptEncoding = internalEvent.headers?.["accept-encoding"] ?? "";
+      const acceptEncoding = internalEvent.headers?.["Accept-Encoding"] ?? "";
       let contentEncoding: string;
       let compressedStream: Writable | undefined;
 
@@ -71,10 +71,15 @@ const handler: WrapperHandler = async (handler, converter) =>
         writeHeaders: (_prelude) => {
           _prelude.headers["content-encoding"] = contentEncoding;
 
-          responseStream = awslambda.HttpResponseStream.from(
-            responseStream,
-            _prelude,
+          responseStream.setContentType(
+            "application/vnd.awslambda.http-integration-response",
           );
+          _prelude.headers["content-encoding"] = contentEncoding;
+          const prelude = JSON.stringify(_prelude);
+
+          responseStream.write(prelude);
+
+          responseStream.write(new Uint8Array(8));
 
           return compressedStream ?? responseStream;
         },

From 8a822cb5959cee418aa0b3477a40dea927177ca9 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 11 Apr 2024 12:34:10 +0200
Subject: [PATCH 096/102] fix e2e tests

---
 examples/app-router/app/page.tsx              |   3 -
 examples/app-router/package.json              |   2 +-
 .../stacks/OpenNextReferenceImplementation.ts |   4 +-
 .../adapters/image-optimization-adapter.ts    |  19 ++-
 pnpm-lock.yaml                                | 132 +++++++++++++++++-
 5 files changed, 151 insertions(+), 9 deletions(-)

diff --git a/examples/app-router/app/page.tsx b/examples/app-router/app/page.tsx
index e07d65a31..f2084c5d3 100644
--- a/examples/app-router/app/page.tsx
+++ b/examples/app-router/app/page.tsx
@@ -44,9 +44,6 @@ export default function Home() {
         <Nav href={"/sse"} title="Server Sent Events">
           Server Sent Events via Streaming
         </Nav>
-        <Nav href={"/revalidate-tag"} title="Revalidate Tag">
-          Revalidate Tag
-        </Nav>
         <Nav href={"/image-optimization"} title="Image Optimization">
           Image Optimization with next/image
         </Nav>
diff --git a/examples/app-router/package.json b/examples/app-router/package.json
index 8775b4ea3..87ce8c576 100644
--- a/examples/app-router/package.json
+++ b/examples/app-router/package.json
@@ -13,7 +13,7 @@
   "dependencies": {
     "@example/shared": "workspace:*",
     "@open-next/utils": "workspace:*",
-    "next": "^14.0.3",
+    "next": "^14.1.4",
     "open-next": "workspace:*",
     "react": "latest",
     "react-dom": "latest"
diff --git a/examples/sst/stacks/OpenNextReferenceImplementation.ts b/examples/sst/stacks/OpenNextReferenceImplementation.ts
index 2e29fe861..66c1fdb5e 100644
--- a/examples/sst/stacks/OpenNextReferenceImplementation.ts
+++ b/examples/sst/stacks/OpenNextReferenceImplementation.ts
@@ -24,6 +24,7 @@ import {
 } from "aws-cdk-lib/aws-dynamodb";
 import { IGrantable, Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
 import {
+  Architecture,
   Code,
   Function as CdkFunction,
   FunctionUrlAuthType,
@@ -207,7 +208,7 @@ export class OpenNextCdkReferenceImplementation extends Construct {
           service: "Lambda",
           action: "invoke",
           parameters: {
-            FunctionName: insertFn.functionArn,
+            FunctionName: insertFn.functionName,
           },
           physicalResourceId: PhysicalResourceId.of("dynamodb-cache"),
         },
@@ -313,6 +314,7 @@ export class OpenNextCdkReferenceImplementation extends Construct {
   private createFunctionOrigin(key: string, origin: OpenNextFunctionOrigin) {
     const environment = this.getEnvironment();
     const fn = new CdkFunction(this, `${key}Function`, {
+      architecture: Architecture.ARM_64,
       runtime: Runtime.NODEJS_18_X,
       handler: origin.handler,
       code: Code.fromAsset(path.join(this.openNextBasePath, origin.bundle)),
diff --git a/packages/open-next/src/adapters/image-optimization-adapter.ts b/packages/open-next/src/adapters/image-optimization-adapter.ts
index cfdedcc38..e4f5974ea 100644
--- a/packages/open-next/src/adapters/image-optimization-adapter.ts
+++ b/packages/open-next/src/adapters/image-optimization-adapter.ts
@@ -91,7 +91,7 @@ export async function defaultHandler(
       downloadHandler,
     );
 
-    return buildSuccessResponse(result, etag);
+    return buildSuccessResponse(result, streamCreator, etag);
   } catch (e: any) {
     return buildFailureResponse(e, streamCreator);
   }
@@ -152,16 +152,31 @@ function computeEtag(imageParams: {
     .digest("base64");
 }
 
-function buildSuccessResponse(result: any, etag?: string): InternalResult {
+function buildSuccessResponse(
+  result: any,
+  streamCreator?: StreamCreator,
+  etag?: string,
+): InternalResult {
   const headers: Record<string, string> = {
     Vary: "Accept",
     "Content-Type": result.contentType,
     "Cache-Control": `public,max-age=${result.maxAge},immutable`,
   };
+  debug("result", result);
   if (etag) {
     headers["ETag"] = etag;
   }
 
+  if (streamCreator) {
+    const response = new OpenNextNodeResponse(
+      () => void 0,
+      async () => void 0,
+      streamCreator,
+    );
+    response.writeHead(200, headers);
+    response.end(result.buffer);
+  }
+
   return {
     type: "core",
     statusCode: 200,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c3a70d179..7d9ba4b3e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -146,8 +146,8 @@ importers:
         specifier: workspace:*
         version: link:../../packages/utils
       next:
-        specifier: ^14.0.3
-        version: 14.0.3(react-dom@18.2.0)(react@18.2.0)
+        specifier: ^14.1.4
+        version: 14.1.4(react-dom@18.2.0)(react@18.2.0)
       open-next:
         specifier: workspace:*
         version: link:../../packages/open-next
@@ -4741,6 +4741,10 @@ packages:
     resolution: {integrity: sha512-7xRqh9nMvP5xrW4/+L0jgRRX+HoNRGnfJpD+5Wq6/13j3dsdzxO3BCXn7D3hMqsDb+vjZnJq+vI7+EtgrYZTeA==}
     dev: false
 
+  /@next/env@14.1.4:
+    resolution: {integrity: sha512-e7X7bbn3Z6DWnDi75UWn+REgAbLEqxI8Tq2pkFOFAMpWAWApz/YCUhtWMWn410h8Q2fYiYL7Yg5OlxMOCfFjJQ==}
+    dev: false
+
   /@next/eslint-plugin-next@13.4.19:
     resolution: {integrity: sha512-N/O+zGb6wZQdwu6atMZHbR7T9Np5SUFUjZqCbj0sXm+MwQO35M8TazVB4otm87GkXYs2l6OPwARd3/PUWhZBVQ==}
     dependencies:
@@ -4765,6 +4769,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-darwin-arm64@14.1.4:
+    resolution: {integrity: sha512-ubmUkbmW65nIAOmoxT1IROZdmmJMmdYvXIe8211send9ZYJu+SqxSnJM4TrPj9wmL6g9Atvj0S/2cFmMSS99jg==}
+    engines: {node: '>= 10'}
+    cpu: [arm64]
+    os: [darwin]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-darwin-x64@13.4.12:
     resolution: {integrity: sha512-WRvH7RxgRHlC1yb5oG0ZLx8F7uci9AivM5/HGGv9ZyG2Als8Ij64GC3d+mQ5sJhWjusyU6T6V1WKTUoTmOB0zQ==}
     engines: {node: '>= 10'}
@@ -4783,6 +4796,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-darwin-x64@14.1.4:
+    resolution: {integrity: sha512-b0Xo1ELj3u7IkZWAKcJPJEhBop117U78l70nfoQGo4xUSvv0PJSTaV4U9xQBLvZlnjsYkc8RwQN1HoH/oQmLlQ==}
+    engines: {node: '>= 10'}
+    cpu: [x64]
+    os: [darwin]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-linux-arm64-gnu@13.4.12:
     resolution: {integrity: sha512-YEKracAWuxp54tKiAvvq73PUs9lok57cc8meYRibTWe/VdPB2vLgkTVWFcw31YDuRXdEhdX0fWS6Q+ESBhnEig==}
     engines: {node: '>= 10'}
@@ -4801,6 +4823,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-linux-arm64-gnu@14.1.4:
+    resolution: {integrity: sha512-457G0hcLrdYA/u1O2XkRMsDKId5VKe3uKPvrKVOyuARa6nXrdhJOOYU9hkKKyQTMru1B8qEP78IAhf/1XnVqKA==}
+    engines: {node: '>= 10'}
+    cpu: [arm64]
+    os: [linux]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-linux-arm64-musl@13.4.12:
     resolution: {integrity: sha512-LhJR7/RAjdHJ2Isl2pgc/JaoxNk0KtBgkVpiDJPVExVWA1c6gzY57+3zWuxuyWzTG+fhLZo2Y80pLXgIJv7g3g==}
     engines: {node: '>= 10'}
@@ -4819,6 +4850,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-linux-arm64-musl@14.1.4:
+    resolution: {integrity: sha512-l/kMG+z6MB+fKA9KdtyprkTQ1ihlJcBh66cf0HvqGP+rXBbOXX0dpJatjZbHeunvEHoBBS69GYQG5ry78JMy3g==}
+    engines: {node: '>= 10'}
+    cpu: [arm64]
+    os: [linux]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-linux-x64-gnu@13.4.12:
     resolution: {integrity: sha512-1DWLL/B9nBNiQRng+1aqs3OaZcxC16Nf+mOnpcrZZSdyKHek3WQh6j/fkbukObgNGwmCoVevLUa/p3UFTTqgqg==}
     engines: {node: '>= 10'}
@@ -4837,6 +4877,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-linux-x64-gnu@14.1.4:
+    resolution: {integrity: sha512-BapIFZ3ZRnvQ1uWbmqEGJuPT9cgLwvKtxhK/L2t4QYO7l+/DxXuIGjvp1x8rvfa/x1FFSsipERZK70pewbtJtw==}
+    engines: {node: '>= 10'}
+    cpu: [x64]
+    os: [linux]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-linux-x64-musl@13.4.12:
     resolution: {integrity: sha512-kEAJmgYFhp0VL+eRWmUkVxLVunn7oL9Mdue/FS8yzRBVj7Z0AnIrHpTIeIUl1bbdQq1VaoOztnKicAjfkLTRCQ==}
     engines: {node: '>= 10'}
@@ -4855,6 +4904,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-linux-x64-musl@14.1.4:
+    resolution: {integrity: sha512-mqVxTwk4XuBl49qn2A5UmzFImoL1iLm0KQQwtdRJRKl21ylQwwGCxJtIYo2rbfkZHoSKlh/YgztY0qH3wG1xIg==}
+    engines: {node: '>= 10'}
+    cpu: [x64]
+    os: [linux]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-win32-arm64-msvc@13.4.12:
     resolution: {integrity: sha512-GMLuL/loR6yIIRTnPRY6UGbLL9MBdw2anxkOnANxvLvsml4F0HNIgvnU3Ej4BjbqMTNjD4hcPFdlEow4XHPdZA==}
     engines: {node: '>= 10'}
@@ -4873,6 +4931,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-win32-arm64-msvc@14.1.4:
+    resolution: {integrity: sha512-xzxF4ErcumXjO2Pvg/wVGrtr9QQJLk3IyQX1ddAC/fi6/5jZCZ9xpuL9Tzc4KPWMFq8GGWFVDMshZOdHGdkvag==}
+    engines: {node: '>= 10'}
+    cpu: [arm64]
+    os: [win32]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-win32-ia32-msvc@13.4.12:
     resolution: {integrity: sha512-PhgNqN2Vnkm7XaMdRmmX0ZSwZXQAtamBVSa9A/V1dfKQCV1rjIZeiy/dbBnVYGdj63ANfsOR/30XpxP71W0eww==}
     engines: {node: '>= 10'}
@@ -4891,6 +4958,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-win32-ia32-msvc@14.1.4:
+    resolution: {integrity: sha512-WZiz8OdbkpRw6/IU/lredZWKKZopUMhcI2F+XiMAcPja0uZYdMTZQRoQ0WZcvinn9xZAidimE7tN9W5v9Yyfyw==}
+    engines: {node: '>= 10'}
+    cpu: [ia32]
+    os: [win32]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@next/swc-win32-x64-msvc@13.4.12:
     resolution: {integrity: sha512-Z+56e/Ljt0bUs+T+jPjhFyxYBcdY2RIq9ELFU+qAMQMteHo7ymbV7CKmlcX59RI9C4YzN8PgMgLyAoi916b5HA==}
     engines: {node: '>= 10'}
@@ -4909,6 +4985,15 @@ packages:
     dev: false
     optional: true
 
+  /@next/swc-win32-x64-msvc@14.1.4:
+    resolution: {integrity: sha512-4Rto21sPfw555sZ/XNLqfxDUNeLhNYGO2dlPqsnuCg8N8a2a9u1ltqBOPQ4vj1Gf7eJC0W2hHG2eYUHuiXgY2w==}
+    engines: {node: '>= 10'}
+    cpu: [x64]
+    os: [win32]
+    requiresBuild: true
+    dev: false
+    optional: true
+
   /@node-minify/core@8.0.6:
     resolution: {integrity: sha512-/vxN46ieWDLU67CmgbArEvOb41zlYFOkOtr9QW9CnTrBLuTyGgkyNWC2y5+khvRw3Br58p2B5ZVSx/PxCTru6g==}
     engines: {node: '>=16.0.0'}
@@ -7509,6 +7594,10 @@ packages:
   /caniuse-lite@1.0.30001525:
     resolution: {integrity: sha512-/3z+wB4icFt3r0USMwxujAqRvaD/B7rvGTsKhbhSQErVrJvkZCLhgNLJxU8MevahQVH6hCU9FsHdNUFbiwmE7Q==}
 
+  /caniuse-lite@1.0.30001608:
+    resolution: {integrity: sha512-cjUJTQkk9fQlJR2s4HMuPMvTiRggl0rAVMtthQuyOlDWuqHXqN8azLq+pi8B2TjwKJ32diHjUqRIKeFX4z1FoA==}
+    dev: false
+
   /case@1.6.3:
     resolution: {integrity: sha512-mzDSXIPaFwVDvZAHqZ9VlbyF4yyXRuX6IvB06WvPYkqJVO24kX1PPhv9bfpKNFZyxYFmmgo03HUiD8iklmJYRQ==}
     engines: {node: '>= 0.8.0'}
@@ -12648,6 +12737,45 @@ packages:
       - babel-plugin-macros
     dev: false
 
+  /next@14.1.4(react-dom@18.2.0)(react@18.2.0):
+    resolution: {integrity: sha512-1WTaXeSrUwlz/XcnhGTY7+8eiaFvdet5z9u3V2jb+Ek1vFo0VhHKSAIJvDWfQpttWjnyw14kBeq28TPq7bTeEQ==}
+    engines: {node: '>=18.17.0'}
+    hasBin: true
+    peerDependencies:
+      '@opentelemetry/api': ^1.1.0
+      react: ^18.2.0
+      react-dom: ^18.2.0
+      sass: ^1.3.0
+    peerDependenciesMeta:
+      '@opentelemetry/api':
+        optional: true
+      sass:
+        optional: true
+    dependencies:
+      '@next/env': 14.1.4
+      '@swc/helpers': 0.5.2
+      busboy: 1.6.0
+      caniuse-lite: 1.0.30001608
+      graceful-fs: 4.2.11
+      postcss: 8.4.31
+      react: 18.2.0
+      react-dom: 18.2.0(react@18.2.0)
+      styled-jsx: 5.1.1(@babel/core@7.22.11)(react@18.2.0)
+    optionalDependencies:
+      '@next/swc-darwin-arm64': 14.1.4
+      '@next/swc-darwin-x64': 14.1.4
+      '@next/swc-linux-arm64-gnu': 14.1.4
+      '@next/swc-linux-arm64-musl': 14.1.4
+      '@next/swc-linux-x64-gnu': 14.1.4
+      '@next/swc-linux-x64-musl': 14.1.4
+      '@next/swc-win32-arm64-msvc': 14.1.4
+      '@next/swc-win32-ia32-msvc': 14.1.4
+      '@next/swc-win32-x64-msvc': 14.1.4
+    transitivePeerDependencies:
+      - '@babel/core'
+      - babel-plugin-macros
+    dev: false
+
   /nextra-theme-docs@2.13.1(next@13.4.12)(nextra@2.13.1)(react-dom@18.2.0)(react@18.2.0):
     resolution: {integrity: sha512-mckNuKa0AmBbRdPCJ/OQ55KZx5MGH8moomMHYB3XVGXQqmXimOq1/2WZQiBdFx9u43KtfEvqZbQE8oGDIrfIsQ==}
     peerDependencies:

From fbfca4394f4cc24cedc40e7797bb4a57a8f02837 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 11 Apr 2024 13:49:25 +0200
Subject: [PATCH 097/102] test: improve reliability of test for revalidateTag

---
 .../stacks/OpenNextReferenceImplementation.ts | 35 +++++++++++--------
 .../tests/appRouter/revalidateTag.test.ts     | 10 ++++++
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/examples/sst/stacks/OpenNextReferenceImplementation.ts b/examples/sst/stacks/OpenNextReferenceImplementation.ts
index 66c1fdb5e..d005929d9 100644
--- a/examples/sst/stacks/OpenNextReferenceImplementation.ts
+++ b/examples/sst/stacks/OpenNextReferenceImplementation.ts
@@ -203,23 +203,28 @@ export class OpenNextCdkReferenceImplementation extends Construct {
         ],
       });
 
-      new AwsCustomResource(this, "RevalidationInitResource", {
-        onUpdate: {
-          service: "Lambda",
-          action: "invoke",
-          parameters: {
-            FunctionName: insertFn.functionName,
+      const customResource = new AwsCustomResource(
+        this,
+        "RevalidationInitResource",
+        {
+          onUpdate: {
+            service: "Lambda",
+            action: "invoke",
+            parameters: {
+              FunctionName: insertFn.functionName,
+            },
+            physicalResourceId: PhysicalResourceId.of(Date.now().toString()),
           },
-          physicalResourceId: PhysicalResourceId.of("dynamodb-cache"),
-        },
 
-        policy: AwsCustomResourcePolicy.fromStatements([
-          new PolicyStatement({
-            actions: ["lambda:InvokeFunction"],
-            resources: [insertFn.functionArn],
-          }),
-        ]),
-      });
+          policy: AwsCustomResourcePolicy.fromStatements([
+            new PolicyStatement({
+              actions: ["lambda:InvokeFunction"],
+              resources: [insertFn.functionArn],
+            }),
+          ]),
+        },
+      );
+      customResource.node.addDependency(insertFn);
     }
 
     return table;
diff --git a/packages/tests-e2e/tests/appRouter/revalidateTag.test.ts b/packages/tests-e2e/tests/appRouter/revalidateTag.test.ts
index ae3d1aa4c..b0fc3c49c 100644
--- a/packages/tests-e2e/tests/appRouter/revalidateTag.test.ts
+++ b/packages/tests-e2e/tests/appRouter/revalidateTag.test.ts
@@ -2,10 +2,20 @@ import { expect, test } from "@playwright/test";
 
 test("Revalidate tag", async ({ page, request }) => {
   test.setTimeout(45000);
+  // We need to hit the page twice to make sure it's properly cached
+  // Turbo might cache next build result, resulting in the tag being newer than the page
+  // This can lead to the cache thinking that revalidate tag has been called when it hasn't
+  // This is because S3 cache files are not uploaded if they have the same BuildId
   let responsePromise = page.waitForResponse((response) => {
     return response.status() === 200;
   });
   await page.goto("/revalidate-tag");
+  await responsePromise;
+
+  responsePromise = page.waitForResponse((response) => {
+    return response.status() === 200;
+  });
+  await page.goto("/revalidate-tag");
   let elLayout = page.getByText("Fetched time:");
   let time = await elLayout.textContent();
   let newTime;

From dae9d98b0519c1ecb640b8c80d7fe0acca5b3886 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 11 Apr 2024 14:00:23 +0200
Subject: [PATCH 098/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index 610799a4d..8d0b4c1bf 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "3.0.0-rc.14",
+  "version": "3.0.0-rc.15",
   "bin": {
     "open-next": "./dist/index.js"
   },

From 1df7d788b81f81065fd507d4057694217897a2cc Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Fri, 12 Apr 2024 09:58:55 +0200
Subject: [PATCH 099/102] review fix

---
 packages/open-next/src/build.ts               | 36 -------------------
 .../open-next/src/build/copyTracedFiles.ts    | 25 +++++++++----
 packages/open-next/src/build/helper.ts        |  7 ++++
 .../src/converters/aws-cloudfront.ts          | 15 ++++----
 packages/open-next/src/core/routing/util.ts   |  2 +-
 packages/open-next/src/plugins/edge.ts        |  3 +-
 packages/open-next/src/types/open-next.ts     |  2 +-
 7 files changed, 35 insertions(+), 55 deletions(-)

diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts
index 5131d0c53..5aed56f1d 100755
--- a/packages/open-next/src/build.ts
+++ b/packages/open-next/src/build.ts
@@ -763,39 +763,3 @@ async function createMiddleware() {
     });
   }
 }
-
-//TODO: Why do we need this? People have access to the headers in the middleware
-function injectMiddlewareGeolocation(outputPath: string, packagePath: string) {
-  // WORKAROUND: Set `NextRequest` geolocation data — https://github.com/serverless-stack/open-next#workaround-set-nextrequest-geolocation-data
-
-  const basePath = path.join(outputPath, packagePath, ".next", "server");
-  const rootMiddlewarePath = path.join(basePath, "middleware.js");
-  const srcMiddlewarePath = path.join(basePath, "src", "middleware.js");
-  if (fs.existsSync(rootMiddlewarePath)) {
-    inject(rootMiddlewarePath);
-  } else if (fs.existsSync(srcMiddlewarePath)) {
-    inject(srcMiddlewarePath);
-  }
-
-  function inject(middlewarePath: string) {
-    const content = fs.readFileSync(middlewarePath, "utf-8");
-    fs.writeFileSync(
-      middlewarePath,
-      content.replace(
-        "geo: init.geo || {}",
-        `geo: init.geo || {
-        country: this.headers.get("cloudfront-viewer-country"),
-        countryName: this.headers.get("cloudfront-viewer-country-name"),
-        region: this.headers.get("cloudfront-viewer-country-region"),
-        regionName: this.headers.get("cloudfront-viewer-country-region-name"),
-        city: this.headers.get("cloudfront-viewer-city"),
-        postalCode: this.headers.get("cloudfront-viewer-postal-code"),
-        timeZone: this.headers.get("cloudfront-viewer-time-zone"),
-        latitude: this.headers.get("cloudfront-viewer-latitude"),
-        longitude: this.headers.get("cloudfront-viewer-longitude"),
-        metroCode: this.headers.get("cloudfront-viewer-metro-code"),
-      }`,
-      ),
-    );
-  }
-}
diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts
index 979b4b123..bf7f2fe02 100644
--- a/packages/open-next/src/build/copyTracedFiles.ts
+++ b/packages/open-next/src/build/copyTracedFiles.ts
@@ -104,6 +104,19 @@ See the docs for more information on how to bundle edge runtime functions.
     );
   };
 
+  const safeComputeCopyFilesForPage = (
+    pagePath: string,
+    alternativePath?: string,
+  ) => {
+    try {
+      computeCopyFilesForPage(pagePath);
+    } catch (e) {
+      if (alternativePath) {
+        computeCopyFilesForPage(alternativePath);
+      }
+    }
+  };
+
   const hasPageDir = routes.some((route) => route.startsWith("pages/"));
   const hasAppDir = routes.some((route) => route.startsWith("app/"));
 
@@ -116,16 +129,16 @@ See the docs for more information on how to bundle edge runtime functions.
     computeCopyFilesForPage("pages/_app");
     computeCopyFilesForPage("pages/_document");
     computeCopyFilesForPage("pages/_error");
+
+    // These files can be present or not depending on if the user uses getStaticProps
+    safeComputeCopyFilesForPage("pages/404");
+    safeComputeCopyFilesForPage("pages/500");
   }
 
   if (hasAppDir) {
     //App dir
-    try {
-      computeCopyFilesForPage("app/_not-found");
-    } catch (e) {
-      // In next 14.2.0, _not-found is at 'app/_not-found/page'
-      computeCopyFilesForPage("app/_not-found/page");
-    }
+    // In next 14.2.0, _not-found is at 'app/_not-found/page'
+    safeComputeCopyFilesForPage("app/_not-found", "app/_not-found/page");
   }
 
   //Files we actually want to include
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
index 43d1584ea..9dfab8d84 100644
--- a/packages/open-next/src/build/helper.ts
+++ b/packages/open-next/src/build/helper.ts
@@ -144,6 +144,13 @@ export function removeFiles(
   );
 }
 
+/**
+ * Recursively traverse files in a directory and call `callbackFn` when `conditionFn` returns true
+ * @param root - Root directory to search
+ * @param conditionFn - Called to determine if `callbackFn` should be called
+ * @param callbackFn - Called when `conditionFn` returns true
+ * @param searchingDir - Directory to search (used for recursion)
+ */
 export function traverseFiles(
   root: string,
   conditionFn: (file: string) => boolean,
diff --git a/packages/open-next/src/converters/aws-cloudfront.ts b/packages/open-next/src/converters/aws-cloudfront.ts
index 216cbee28..ba969e5cf 100644
--- a/packages/open-next/src/converters/aws-cloudfront.ts
+++ b/packages/open-next/src/converters/aws-cloudfront.ts
@@ -153,20 +153,17 @@ async function convertToCloudFrontRequestResult(
       const serverResponse = createServerResponse(result.internalEvent, {});
       await proxyRequest(result.internalEvent, serverResponse);
       const externalResult = convertRes(serverResponse);
-      debug("externalResult", {
+      const cloudfrontResult = {
         status: externalResult.statusCode.toString(),
         statusDescription: "OK",
         headers: convertToCloudfrontHeaders(externalResult.headers),
-        bodyEncoding: externalResult.isBase64Encoded ? "base64" : "text",
-        body: externalResult.body,
-      });
-      return {
-        status: externalResult.statusCode.toString(),
-        statusDescription: "OK",
-        headers: convertToCloudfrontHeaders(externalResult.headers),
-        bodyEncoding: externalResult.isBase64Encoded ? "base64" : "text",
+        bodyEncoding: externalResult.isBase64Encoded
+          ? ("base64" as const)
+          : ("text" as const),
         body: externalResult.body,
       };
+      debug("externalResult", cloudfrontResult);
+      return cloudfrontResult;
     }
     let customOrigin = origin?.custom as CloudFrontCustomOrigin;
     let host = responseHeaders["host"] ?? responseHeaders["Host"];
diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts
index 83ceae17e..48d562d3c 100644
--- a/packages/open-next/src/core/routing/util.ts
+++ b/packages/open-next/src/core/routing/util.ts
@@ -278,7 +278,7 @@ export function fixCacheHeaderForHtmlPages(
  * @__PURE__
  */
 export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
-  // WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
+  // WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/sst/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
   let cacheControl = headers[CommonHeaders.CACHE_CONTROL];
   if (!cacheControl) return;
   if (Array.isArray(cacheControl)) {
diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts
index b05ac4b15..e22e8d1d4 100644
--- a/packages/open-next/src/plugins/edge.ts
+++ b/packages/open-next/src/plugins/edge.ts
@@ -23,7 +23,6 @@ export interface IPluginSettings {
 }
 
 /**
- * TODO: Handle wasm import
  * @param opts.nextDir - The path to the .next directory
  * @param opts.edgeFunctionHandlerPath - The path to the edgeFunctionHandler.js file that we'll use to bundle the routing
  * @param opts.entryFiles - The entry files that we'll inject into the edgeFunctionHandler.js file
@@ -64,7 +63,7 @@ export function openNextEdgePlugins({
         };
       });
 
-      //COpied from https://github.com/cloudflare/next-on-pages/blob/7a18efb5cab4d86c8e3e222fc94ea88ac05baffd/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts#L86-L112
+      //Copied from https://github.com/cloudflare/next-on-pages/blob/7a18efb5cab4d86c8e3e222fc94ea88ac05baffd/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts#L86-L112
 
       build.onResolve({ filter: /^node:/ }, ({ kind, path }) => {
         // this plugin converts `require("node:*")` calls, those are the only ones that
diff --git a/packages/open-next/src/types/open-next.ts b/packages/open-next/src/types/open-next.ts
index b93b9e84e..89b5fcd21 100644
--- a/packages/open-next/src/types/open-next.ts
+++ b/packages/open-next/src/types/open-next.ts
@@ -202,8 +202,8 @@ export interface FunctionOptions extends DefaultFunctionOptions {
    * Bundle Next server into a single file.
    * This results in a way smaller bundle but it might break for some cases.
    * This option will probably break on every new Next.js version.
-   * Broken in 14.2+
    * @default false
+   * @deprecated This is not supported in 14.2+
    */
   experimentalBundledNextServer?: boolean;
 }

From 050b8f5014f6e73f95d0617069498c4f4c28d7a9 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 18 Apr 2024 12:18:46 +0200
Subject: [PATCH 100/102] fix cookies in streaming also fix an issue when both
 middleware and page try to set cookies OpenNextNodeResponse also implements
 ServerResponse

---
 .../open-next/src/http/openNextResponse.ts    | 161 ++++++++++++++++--
 .../src/wrappers/aws-lambda-streaming.ts      |   7 +-
 2 files changed, 153 insertions(+), 15 deletions(-)

diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts
index 72501202e..c2ca5aaed 100644
--- a/packages/open-next/src/http/openNextResponse.ts
+++ b/packages/open-next/src/http/openNextResponse.ts
@@ -1,9 +1,16 @@
-import { OutgoingHttpHeader, OutgoingHttpHeaders } from "http";
+import type {
+  IncomingMessage,
+  OutgoingHttpHeader,
+  OutgoingHttpHeaders,
+  ServerResponse,
+} from "http";
+import { Socket } from "net";
 import { Transform, TransformCallback, Writable } from "stream";
 
-import { convertHeader, parseCookies, parseHeaders } from "./util";
+import { parseCookies, parseHeaders } from "./util";
 
 const SET_COOKIE_HEADER = "set-cookie";
+const CANNOT_BE_USED = "This cannot be used in OpenNext";
 
 export interface StreamCreator {
   writeHeaders(prelude: {
@@ -17,16 +24,56 @@ export interface StreamCreator {
 }
 
 // We only need to implement the methods that are used by next.js
-export class OpenNextNodeResponse extends Transform {
-  statusCode: number | undefined;
-  statusMessage: string | undefined;
+export class OpenNextNodeResponse extends Transform implements ServerResponse {
+  statusCode!: number;
+  statusMessage: string = "";
   headers: OutgoingHttpHeaders = {};
   private _cookies: string[] = [];
   private responseStream?: Writable;
-  private hasDoneFirstWrite: boolean = false;
   headersSent: boolean = false;
   _chunks: Buffer[] = [];
 
+  // To comply with the ServerResponse interface :
+  strictContentLength: boolean = false;
+  assignSocket(_socket: Socket): void {
+    throw new Error(CANNOT_BE_USED);
+  }
+  detachSocket(_socket: Socket): void {
+    throw new Error(CANNOT_BE_USED);
+  }
+  // We might have to revisit those 3 in the future
+  writeContinue(_callback?: (() => void) | undefined): void {
+    throw new Error(CANNOT_BE_USED);
+  }
+  writeEarlyHints(
+    _hints: Record<string, string | string[]>,
+    _callback?: (() => void) | undefined,
+  ): void {
+    throw new Error(CANNOT_BE_USED);
+  }
+  writeProcessing(): void {
+    throw new Error(CANNOT_BE_USED);
+  }
+  /**
+   * This is a dummy request object to comply with the ServerResponse interface
+   * It will never be defined
+   */
+  req!: IncomingMessage;
+  chunkedEncoding: boolean = false;
+  shouldKeepAlive: boolean = true;
+  useChunkedEncodingByDefault: boolean = true;
+  sendDate: boolean = false;
+  connection: Socket | null = null;
+  socket: Socket | null = null;
+  setTimeout(_msecs: number, _callback?: (() => void) | undefined): this {
+    throw new Error(CANNOT_BE_USED);
+  }
+  addTrailers(
+    _headers: OutgoingHttpHeaders | readonly [string, string][],
+  ): void {
+    throw new Error(CANNOT_BE_USED);
+  }
+
   constructor(
     private fixHeaders: (headers: OutgoingHttpHeaders) => void,
     onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
@@ -55,17 +102,23 @@ export class OpenNextNodeResponse extends Transform {
   }
 
   get finished() {
-    return this.writableFinished && this.responseStream?.writableFinished;
+    return Boolean(
+      this.writableFinished && this.responseStream?.writableFinished,
+    );
   }
 
   setHeader(name: string, value: string | string[]): this {
     const key = name.toLowerCase();
     if (key === SET_COOKIE_HEADER) {
-      this._cookies.push(convertHeader(value));
-      this.headers[key] = this._cookies;
-    } else {
-      this.headers[key] = value;
+      if (Array.isArray(value)) {
+        this._cookies = value;
+      } else {
+        this._cookies = [value];
+      }
     }
+    // We should always replace the header
+    // See https://nodejs.org/docs/latest-v18.x/api/http.html#responsesetheadername-value
+    this.headers[key] = value;
 
     return this;
   }
@@ -96,16 +149,40 @@ export class OpenNextNodeResponse extends Transform {
     return this.headers[name.toLowerCase()];
   }
 
+  getHeaderNames(): string[] {
+    return Object.keys(this.headers);
+  }
+
   // Only used directly in next@14+
   flushHeaders() {
     this.headersSent = true;
     this.fixHeaders(this.headers);
+    // Initial headers should be merged with the new headers
+    // These initial headers are the one created either in the middleware or in next.config.js
+    // We choose to override response headers with middleware headers
+    // This is different than the default behavior in next.js, but it allows more customization
+    // TODO: We probably want to change this behavior in the future to follow next
+    // We could add a prefix header that would allow to force the middleware headers
+    // Something like open-next-force-cache-control would override the cache-control header
     if (this.initialHeaders) {
       this.headers = {
         ...this.headers,
         ...this.initialHeaders,
       };
     }
+    if (this._cookies.length > 0) {
+      // For cookies we cannot do the same as for other headers
+      // We need to merge the cookies, and in this case, cookies generated by the routes or pages
+      // should be added after the ones generated by the middleware
+      // This prevents the middleware from overriding the cookies, especially for server actions
+      // which uses the same pathnames as the pages they're being called on
+      this.headers[SET_COOKIE_HEADER] = [
+        ...(parseCookies(
+          this.initialHeaders?.[SET_COOKIE_HEADER] as string | string[],
+        ) ?? []),
+        ...this._cookies,
+      ];
+    }
 
     if (this.streamCreator) {
       this.responseStream = this.streamCreator?.writeHeaders({
@@ -117,11 +194,67 @@ export class OpenNextNodeResponse extends Transform {
     }
   }
 
+  appendHeader(name: string, value: string | string[]): this {
+    const key = name.toLowerCase();
+    if (!this.hasHeader(key)) {
+      return this.setHeader(key, value);
+    } else {
+      const existingHeader = this.getHeader(key) as string | string[];
+      const toAppend = Array.isArray(value) ? value : [value];
+      const newValue = Array.isArray(existingHeader)
+        ? [...existingHeader, ...toAppend]
+        : [existingHeader, ...toAppend];
+      return this.setHeader(key, newValue);
+    }
+  }
+
   // Might be used in next page api routes
-  writeHead(statusCode: number, headers?: OutgoingHttpHeaders): this {
-    this.statusCode = statusCode;
+  writeHead(
+    statusCode: number,
+    statusMessage?: string | undefined,
+    headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined,
+  ): this;
+  writeHead(
+    statusCode: number,
+    headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined,
+  ): this;
+  writeHead(
+    statusCode: unknown,
+    statusMessage?: unknown,
+    headers?: unknown,
+  ): this {
+    let _headers = headers as
+      | OutgoingHttpHeaders
+      | OutgoingHttpHeader[]
+      | undefined;
+    let _statusMessage: string | undefined;
+    if (typeof statusMessage === "string") {
+      _statusMessage = statusMessage;
+    } else {
+      _headers = statusMessage as
+        | OutgoingHttpHeaders
+        | OutgoingHttpHeader[]
+        | undefined;
+    }
+    const finalHeaders: OutgoingHttpHeaders = this.headers;
+    if (_headers) {
+      if (Array.isArray(_headers)) {
+        // headers may be an Array where the keys and values are in the same list. It is not a list of tuples. So, the even-numbered offsets are key values, and the odd-numbered offsets are the associated values.
+        for (let i = 0; i < _headers.length; i += 2) {
+          finalHeaders[_headers[i] as string] = _headers[i + 1] as
+            | string
+            | string[];
+        }
+      } else {
+        for (const key of Object.keys(_headers)) {
+          finalHeaders[key] = _headers[key];
+        }
+      }
+    }
+
+    this.statusCode = statusCode as number;
     if (headers) {
-      this.headers = headers;
+      this.headers = finalHeaders;
     }
     this.flushHeaders();
     return this;
diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
index 432e19bbf..6b0ad02c1 100644
--- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts
+++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts
@@ -34,7 +34,10 @@ const handler: WrapperHandler = async (handler, converter) =>
       let _hasWriten = false;
 
       //Handle compression
-      const acceptEncoding = internalEvent.headers?.["Accept-Encoding"] ?? "";
+      const acceptEncoding =
+        internalEvent.headers["Accept-Encoding"] ??
+        internalEvent.headers["accept-encoding"] ??
+        "";
       let contentEncoding: string;
       let compressedStream: Writable | undefined;
 
@@ -75,6 +78,8 @@ const handler: WrapperHandler = async (handler, converter) =>
             "application/vnd.awslambda.http-integration-response",
           );
           _prelude.headers["content-encoding"] = contentEncoding;
+          // We need to remove the set-cookie header as otherwise it will be set twice, once with the cookies in the prelude, and a second time with the set-cookie headers
+          delete _prelude.headers["set-cookie"];
           const prelude = JSON.stringify(_prelude);
 
           responseStream.write(prelude);

From 9cb86cff1dac0ef498c36d10e7f61efa73f04050 Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 18 Apr 2024 13:03:48 +0200
Subject: [PATCH 101/102] make all write to ddb chunked

---
 examples/app-router/open-next.config.ts       |  1 -
 examples/pages-router/open-next.config.ts     |  4 +-
 .../open-next/src/adapters/dynamo-provider.ts | 16 +------
 .../src/{adapters => cache/tag}/constants.ts  |  0
 packages/open-next/src/cache/tag/dynamoDb.ts  | 44 +++++++++++--------
 5 files changed, 27 insertions(+), 38 deletions(-)
 rename packages/open-next/src/{adapters => cache/tag}/constants.ts (100%)

diff --git a/examples/app-router/open-next.config.ts b/examples/app-router/open-next.config.ts
index 65c916ac1..f5f7c73b9 100644
--- a/examples/app-router/open-next.config.ts
+++ b/examples/app-router/open-next.config.ts
@@ -3,7 +3,6 @@ const config = {
     override: {
       wrapper: "aws-lambda-streaming",
     },
-    experimentalBundledNextServer: true,
   },
   functions: {},
   buildCommand: "npx turbo build",
diff --git a/examples/pages-router/open-next.config.ts b/examples/pages-router/open-next.config.ts
index a03cdc05d..54e09d87d 100644
--- a/examples/pages-router/open-next.config.ts
+++ b/examples/pages-router/open-next.config.ts
@@ -1,7 +1,5 @@
 const config = {
-  default: {
-    experimentalBundledNextServer: true,
-  },
+  default: {},
   functions: {},
   buildCommand: "npx turbo build",
 };
diff --git a/packages/open-next/src/adapters/dynamo-provider.ts b/packages/open-next/src/adapters/dynamo-provider.ts
index 76d924b5a..d45572544 100644
--- a/packages/open-next/src/adapters/dynamo-provider.ts
+++ b/packages/open-next/src/adapters/dynamo-provider.ts
@@ -2,11 +2,6 @@ import { readFileSync } from "fs";
 
 import { createGenericHandler } from "../core/createGenericHandler.js";
 import { resolveTagCache } from "../core/resolve.js";
-import {
-  getDynamoBatchWriteCommandConcurrency,
-  MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT,
-} from "./constants.js";
-import { chunk } from "./util.js";
 
 const PHYSICAL_RESOURCE_ID = "dynamodb-cache" as const;
 
@@ -64,16 +59,7 @@ async function insert(
     revalidatedAt: parseInt(item.revalidatedAt.N),
   }));
 
-  const dataChunks = chunk(parsedData, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT);
-
-  const paramsChunks = chunk(
-    dataChunks,
-    getDynamoBatchWriteCommandConcurrency(),
-  );
-
-  for (const paramsChunk of paramsChunks) {
-    await Promise.all(paramsChunk.map((params) => tagCache.writeTags(params)));
-  }
+  await tagCache.writeTags(parsedData);
 
   return {
     type: "initializationFunction",
diff --git a/packages/open-next/src/adapters/constants.ts b/packages/open-next/src/cache/tag/constants.ts
similarity index 100%
rename from packages/open-next/src/adapters/constants.ts
rename to packages/open-next/src/cache/tag/constants.ts
diff --git a/packages/open-next/src/cache/tag/dynamoDb.ts b/packages/open-next/src/cache/tag/dynamoDb.ts
index 8eccf433f..dc2a61a2c 100644
--- a/packages/open-next/src/cache/tag/dynamoDb.ts
+++ b/packages/open-next/src/cache/tag/dynamoDb.ts
@@ -6,9 +6,12 @@ import {
 } from "@aws-sdk/client-dynamodb";
 import path from "path";
 
-import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "../../adapters/constants";
 import { awsLogger, debug, error } from "../../adapters/logger";
 import { chunk, parseNumberFromEnv } from "../../adapters/util";
+import {
+  getDynamoBatchWriteCommandConcurrency,
+  MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT,
+} from "./constants";
 import { TagCache } from "./types";
 
 const { CACHE_BUCKET_REGION, CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
@@ -119,27 +122,30 @@ const tagCache: TagCache = {
   async writeTags(tags) {
     try {
       if (globalThis.disableDynamoDBCache) return;
-      await Promise.all(
-        chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map((Items) => {
-          return dynamoClient.send(
-            new BatchWriteItemCommand({
-              RequestItems: {
-                [CACHE_DYNAMO_TABLE ?? ""]: Items.map((Item) => ({
-                  PutRequest: {
-                    Item: {
-                      ...buildDynamoObject(
-                        Item.path,
-                        Item.tag,
-                        Item.revalidatedAt,
-                      ),
-                    },
-                  },
-                })),
+      const dataChunks = chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map(
+        (Items) => ({
+          RequestItems: {
+            [CACHE_DYNAMO_TABLE ?? ""]: Items.map((Item) => ({
+              PutRequest: {
+                Item: {
+                  ...buildDynamoObject(Item.path, Item.tag, Item.revalidatedAt),
+                },
               },
-            }),
-          );
+            })),
+          },
         }),
       );
+      const toInsert = chunk(
+        dataChunks,
+        getDynamoBatchWriteCommandConcurrency(),
+      );
+      for (const paramsChunk of toInsert) {
+        await Promise.all(
+          paramsChunk.map(async (params) =>
+            dynamoClient.send(new BatchWriteItemCommand(params)),
+          ),
+        );
+      }
     } catch (e) {
       error("Failed to batch write dynamo item", e);
     }

From dd4d9fe935c7fd12875afdf4c64cbafcbd71b7cd Mon Sep 17 00:00:00 2001
From: Dorseuil Nicolas <nicodorseuil@yahoo.fr>
Date: Thu, 18 Apr 2024 13:19:26 +0200
Subject: [PATCH 102/102] update version

---
 packages/open-next/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/open-next/package.json b/packages/open-next/package.json
index c768493d3..e705ab025 100644
--- a/packages/open-next/package.json
+++ b/packages/open-next/package.json
@@ -3,7 +3,7 @@
     "access": "public"
   },
   "name": "open-next",
-  "version": "2.3.9",
+  "version": "3.0.0-rc.16",
   "bin": {
     "open-next": "./dist/index.js"
   },
