Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow overriding the proxying for external rewrite #630

Merged
merged 12 commits into from
Nov 19, 2024
20 changes: 13 additions & 7 deletions packages/open-next/src/adapters/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createGenericHandler } from "../core/createGenericHandler";
import {
resolveIncrementalCache,
resolveOriginResolver,
resolveProxyRequest,
resolveQueue,
resolveTagCache,
} from "../core/resolve";
Expand All @@ -19,6 +20,10 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
globalThis.openNextConfig.middleware?.originResolver,
);

const externalRequestProxy = await resolveProxyRequest(
globalThis.openNextConfig.middleware?.override?.proxyExternalRequest,
);

//#override includeCacheInMiddleware
globalThis.tagCache = await resolveTagCache(
globalThis.openNextConfig.middleware?.override?.tagCache,
Expand All @@ -43,14 +48,15 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
let origin: Origin | false = false;
if (!result.isExternalRewrite) {
origin = await originResolver.resolve(result.internalEvent.rawPath);
conico974 marked this conversation as resolved.
Show resolved Hide resolved
return {
type: "middleware",
internalEvent: result.internalEvent,
isExternalRewrite: result.isExternalRewrite,
origin,
isISR: result.isISR,
};
}
return {
type: "middleware",
internalEvent: result.internalEvent,
isExternalRewrite: result.isExternalRewrite,
origin,
isISR: result.isISR,
};
return externalRequestProxy.proxy(result.internalEvent);
}

debug("Middleware response", result);
Expand Down
5 changes: 4 additions & 1 deletion packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export async function createMiddleware(
outfile: path.join(outputPath, "handler.mjs"),
middlewareInfo,
options,
overrides: config.middleware?.override,
overrides: {
...config.middleware.override,
originResolver: config.middleware.originResolver,
},
defaultConverter: "aws-cloudfront",
includeCache: config.dangerous?.enableCacheInterception,
additionalExternals: config.edgeExternals,
Expand Down
9 changes: 8 additions & 1 deletion packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { build } from "esbuild";
import type { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
import type {
IncludedConverter,
IncludedOriginResolver,
LazyLoadedOverride,
OverrideOptions,
RouteTemplate,
SplittedFunctionOptions,
} from "types/open-next";

import type { OriginResolver } from "types/overrides.js";
import logger from "../../logger.js";
import { openNextEdgePlugins } from "../../plugins/edge.js";
import { openNextReplacementPlugin } from "../../plugins/replacement.js";
Expand All @@ -23,7 +26,11 @@ interface BuildEdgeBundleOptions {
entrypoint: string;
outfile: string;
options: BuildOptions;
overrides?: OverrideOptions;
overrides?: OverrideOptions & {
originResolver?:
vicb marked this conversation as resolved.
Show resolved Hide resolved
| LazyLoadedOverride<OriginResolver>
| IncludedOriginResolver;
};
defaultConverter?: IncludedConverter;
additionalInject?: string;
includeCache?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions packages/open-next/src/core/createMainHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { openNextHandler } from "./requestHandler";
import {
resolveConverter,
resolveIncrementalCache,
resolveProxyRequest,
resolveQueue,
resolveTagCache,
resolveWrapper,
Expand Down Expand Up @@ -33,6 +34,10 @@ export async function createMainHandler() {

globalThis.tagCache = await resolveTagCache(thisFunction.override?.tagCache);

globalThis.proxyExternalRequest = await resolveProxyRequest(
thisFunction.override?.proxyExternalRequest,
);

globalThis.lastModified = {};

// From the config, we create the converter
Expand Down
48 changes: 32 additions & 16 deletions packages/open-next/src/core/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { runWithOpenNextRequestContext } from "utils/promise";

import { debug, error, warn } from "../adapters/logger";
import { patchAsyncStorage } from "./patchAsyncStorage";
import { convertRes, createServerResponse, proxyRequest } from "./routing/util";
import { resolveProxyRequest } from "./resolve";
import { convertRes, createServerResponse } from "./routing/util";
import type { MiddlewareOutputEvent } from "./routingHandler";
import routingHandler, {
MIDDLEWARE_HEADER_PREFIX,
Expand Down Expand Up @@ -65,6 +66,35 @@ export async function openNextHandler(
delete headers[rawKey];
}

if (
"isExternalRewrite" in preprocessResult &&
preprocessResult.isExternalRewrite
conico974 marked this conversation as resolved.
Show resolved Hide resolved
) {
try {
const proxyResult = await globalThis.proxyExternalRequest.proxy(
conico974 marked this conversation as resolved.
Show resolved Hide resolved
preprocessResult.internalEvent,
);
preprocessResult = proxyResult;
} catch (e) {
error("External request failed.", e);
preprocessResult = {
internalEvent: {
type: "core",
rawPath: "/500",
method: "GET",
headers: {},
url: "/500",
query: {},
cookies: {},
remoteAddress: "",
},
isExternalRewrite: false,
vicb marked this conversation as resolved.
Show resolved Hide resolved
isISR: false,
origin: false,
};
}
}

if ("type" in preprocessResult) {
// response is used only in the streaming case
if (responseStreaming) {
Expand Down Expand Up @@ -110,20 +140,14 @@ export async function openNextHandler(
store.mergeHeadersPriority = mergeHeadersPriority;
}

const preprocessedResult = preprocessResult as MiddlewareOutputEvent;
const req = new IncomingMessage(reqProps);
const res = createServerResponse(
preprocessedEvent,
overwrittenResponseHeaders,
responseStreaming,
);

await processRequest(
req,
res,
preprocessedEvent,
preprocessedResult.isExternalRewrite,
);
await processRequest(req, res, preprocessedEvent);

const {
statusCode,
Expand Down Expand Up @@ -155,21 +179,13 @@ async function processRequest(
req: IncomingMessage,
res: OpenNextNodeResponse,
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.

if (isExternalRewrite) {
return proxyRequest(internalEvent, res);
}

//#override applyNextjsPrebundledReact
setNextjsPrebundledReact(internalEvent.rawPath);
//#endOverride
Expand Down
17 changes: 17 additions & 0 deletions packages/open-next/src/core/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
BaseEventOrResult,
DefaultOverrideOptions,
IncludedProxyExternalRequest,
InternalEvent,
InternalResult,
LazyLoadedOverride,
Expand All @@ -10,6 +11,7 @@ import type {
Converter,
ImageLoader,
OriginResolver,
ProxyExternalRequest,
TagCache,
Warmer,
Wrapper,
Expand Down Expand Up @@ -130,3 +132,18 @@ export async function resolveWarmerInvoke(
const m_1 = await import("../overrides/warmer/aws-lambda.js");
return m_1.default;
}

/**
* @__PURE__
*/
export async function resolveProxyRequest(
proxyRequest?:
| LazyLoadedOverride<ProxyExternalRequest>
| IncludedProxyExternalRequest,
) {
if (typeof proxyRequest === "function") {
return proxyRequest();
}
const m_1 = await import("../overrides/proxyExternalRequest/node.js");
return m_1.default;
}
82 changes: 0 additions & 82 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,6 @@ export function unescapeRegex(str: string) {
.replaceAll("_µ3_", "(...)");
}

/**
*
* @__PURE__
*/
function filterHeadersForProxy(
headers: Record<string, string | string[] | undefined>,
) {
const filteredHeaders: Record<string, string | string[]> = {};
const disallowedHeaders = [
"host",
"connection",
"via",
"x-cache",
"transfer-encoding",
"content-encoding",
"content-length",
];
Object.entries(headers).forEach(([key, value]) => {
const lowerKey = key.toLowerCase();
if (disallowedHeaders.includes(lowerKey) || lowerKey.startsWith("x-amz"))
return;

filteredHeaders[key] = value?.toString() ?? "";
});
return filteredHeaders;
}

/**
* @__PURE__
*/
Expand All @@ -209,61 +182,6 @@ export function convertBodyToReadableStream(
return readable;
}

/**
*
* @__PURE__
*/
export async function proxyRequest(
internalEvent: InternalEvent,
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);
debug("filteredHeaders", filteredHeaders);
const req = request(
url,
{
headers: filteredHeaders,
method,
rejectUnauthorized: false,
},
(_res) => {
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") {
_res.pipe(require("node:zlib").createGunzip()).pipe(res);
} else {
_res.pipe(res);
}

_res.on("error", (e) => {
error("proxyRequest error", e);
res.end();
reject(e);
});
res.on("finish", () => {
resolve();
});
},
);

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());
}

enum CommonHeaders {
CACHE_CONTROL = "cache-control",
NEXT_CACHE = "x-nextjs-cache",
Expand Down
22 changes: 1 addition & 21 deletions packages/open-next/src/overrides/converters/aws-cloudfront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
convertToQuery,
convertToQueryString,
createServerResponse,
proxyRequest,
} from "../../core/routing/util";
import type { MiddlewareOutputEvent } from "../../core/routingHandler";

Expand Down Expand Up @@ -159,26 +158,7 @@ async function convertToCloudFrontRequestResult(
const responseHeaders = result.internalEvent.headers;

// Handle external rewrite
if (result.isExternalRewrite) {
const serverResponse = createServerResponse(result.internalEvent, {});
await proxyRequest(result.internalEvent, serverResponse);
const externalResult = convertRes(serverResponse);
const body = await fromReadableStream(
externalResult.body,
externalResult.isBase64Encoded,
);
const cloudfrontResult = {
status: externalResult.statusCode.toString(),
statusDescription: "OK",
headers: convertToCloudfrontHeaders(externalResult.headers, true),
bodyEncoding: externalResult.isBase64Encoded
? ("base64" as const)
: ("text" as const),
body,
};
debug("externalResult", cloudfrontResult);
return cloudfrontResult;
}

let customOrigin = origin?.custom as CloudFrontCustomOrigin;
let host = responseHeaders.host ?? responseHeaders.Host;
if (result.origin) {
Expand Down
10 changes: 10 additions & 0 deletions packages/open-next/src/overrides/proxyExternalRequest/dummy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ProxyExternalRequest } from "types/overrides";

const DummyProxyExternalRequest: ProxyExternalRequest = {
name: "dummy",
proxy: async (_event) => {
throw new Error("This is a dummy implementation");
},
};

export default DummyProxyExternalRequest;
28 changes: 28 additions & 0 deletions packages/open-next/src/overrides/proxyExternalRequest/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ProxyExternalRequest } from "types/overrides";
import { emptyReadableStream } from "utils/stream";

const fetchProxy: ProxyExternalRequest = {
name: "fetch-proxy",
// @ts-ignore
proxy: async (internalEvent) => {
const { url, headers, method, body } = internalEvent;
const response = await fetch(url, {
vicb marked this conversation as resolved.
Show resolved Hide resolved
method,
headers,
body,
});
const responseHeaders: Record<string, string | string[]> = {};
response.headers.forEach((value, key) => {
responseHeaders[key] = value;
});
return {
type: "core",
headers: responseHeaders,
statusCode: response.status,
isBase64Encoded: true,
body: response.body ?? emptyReadableStream(),
};
},
};

export default fetchProxy;
Loading
Loading