| Server IP : 159.203.156.69 / Your IP : 216.73.217.172 Web Server : nginx/1.24.0 System : Linux main-ubuntu 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/tanviranik.com/node_modules/vinext/dist/server/ |
Upload File : |
import { normalizePathnameForRouteMatchStrict } from "../routing/utils.js";
import { getRequestExecutionContext, runWithExecutionContext } from "../shims/request-context.js";
import { MIDDLEWARE_REWRITE_HEADER } from "./headers.js";
import { shouldKeepMiddlewareHeader } from "./middleware-request-headers.js";
import { NextFetchEvent, NextRequest } from "../shims/server.js";
import { normalizePath } from "./normalize-path.js";
import { matchesMiddleware } from "./middleware-matcher.js";
import { badRequestResponse, internalServerErrorResponse } from "./http-error-responses.js";
import { processMiddlewareHeaders } from "./request-pipeline.js";
//#region src/server/middleware-runtime.ts
function isMiddlewareHandler(value) {
return typeof value === "function";
}
function isMiddlewareConfigExport(value) {
return !!value && typeof value === "object";
}
function middlewareFileLabel(isProxy) {
return isProxy ? "Proxy" : "Middleware";
}
function middlewareExpectedExport(isProxy) {
return isProxy ? "proxy" : "middleware";
}
function resolveMiddlewareModuleHandler(mod, options) {
const handler = options.isProxy ? mod.proxy ?? mod.default : mod.middleware ?? mod.default;
if (isMiddlewareHandler(handler)) return handler;
const fileLabel = middlewareFileLabel(options.isProxy);
const expectedExport = middlewareExpectedExport(options.isProxy);
const fileSuffix = options.filePath ? ` "${options.filePath}"` : "";
throw new Error(`The ${fileLabel} file${fileSuffix} must export a function named \`${expectedExport}\` or a \`default\` function.`);
}
function middlewareMatcher(mod) {
const config = mod.config;
if (!isMiddlewareConfigExport(config)) return void 0;
return config.matcher;
}
function stripMiddlewareHeadersFromResponse(response) {
const headers = new Headers(response.headers);
processMiddlewareHeaders(headers);
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers
});
}
function collectMiddlewareHeaders(response) {
const responseHeaders = new Headers();
for (const [key, value] of response.headers) if (!key.startsWith("x-middleware-") || shouldKeepMiddlewareHeader(key)) responseHeaders.append(key, value);
return responseHeaders;
}
function drainFetchEvent(fetchEvent) {
const waitUntilPromises = fetchEvent.waitUntilPromises;
const drained = fetchEvent.drainWaitUntil();
const executionContext = getRequestExecutionContext();
if (executionContext) executionContext.waitUntil(drained);
return waitUntilPromises;
}
function resolveMiddlewarePathname(request) {
const url = new URL(request.url);
try {
return normalizePath(normalizePathnameForRouteMatchStrict(url.pathname));
} catch {
return badRequestResponse();
}
}
function createNextRequest(request, normalizedPathname, i18nConfig, basePath) {
const url = new URL(request.url);
let mwRequest = request.body && !request.bodyUsed ? request.clone() : request;
if (normalizedPathname !== url.pathname) {
const mwUrl = new URL(url);
mwUrl.pathname = normalizedPathname;
mwRequest = new Request(mwUrl, mwRequest);
}
const nextConfig = basePath || i18nConfig ? {
basePath: basePath ?? "",
i18n: i18nConfig ?? void 0
} : void 0;
return mwRequest instanceof NextRequest ? mwRequest : new NextRequest(mwRequest, nextConfig ? { nextConfig } : void 0);
}
async function executeMiddleware(options) {
const middlewareFn = resolveMiddlewareModuleHandler(options.module, {
filePath: options.filePath,
isProxy: options.isProxy
});
const normalizedPathname = options.normalizedPathname ?? resolveMiddlewarePathname(options.request);
if (normalizedPathname instanceof Response) return {
continue: false,
response: normalizedPathname
};
if (!matchesMiddleware(normalizedPathname, middlewareMatcher(options.module), options.request, options.i18nConfig)) return { continue: true };
const nextRequest = createNextRequest(options.request, normalizedPathname, options.i18nConfig, options.basePath);
const fetchEvent = new NextFetchEvent({ page: normalizedPathname });
let response;
try {
response = await middlewareFn(nextRequest, fetchEvent);
} catch (e) {
console.error("[vinext] Middleware error:", e);
const waitUntilPromises = drainFetchEvent(fetchEvent);
return {
continue: false,
response: internalServerErrorResponse(options.includeErrorDetails ? "Middleware Error: " + (e instanceof Error ? e.message : String(e)) : "Internal Server Error"),
waitUntilPromises
};
}
const waitUntilPromises = drainFetchEvent(fetchEvent);
if (!response) return {
continue: true,
waitUntilPromises
};
if (response.headers.get("x-middleware-next") === "1") return {
continue: true,
responseHeaders: collectMiddlewareHeaders(response),
status: response.status !== 200 ? response.status : void 0,
waitUntilPromises
};
if (response.status >= 300 && response.status < 400) {
const location = response.headers.get("Location") ?? response.headers.get("location");
if (location) {
const responseHeaders = new Headers();
for (const [key, value] of response.headers) if (!key.startsWith("x-middleware-") && key.toLowerCase() !== "location") responseHeaders.append(key, value);
return {
continue: false,
redirectUrl: location,
redirectStatus: response.status,
response: stripMiddlewareHeadersFromResponse(response),
responseHeaders,
waitUntilPromises
};
}
}
const rewriteUrl = response.headers.get(MIDDLEWARE_REWRITE_HEADER);
if (rewriteUrl) {
let rewritePath;
try {
const rewriteParsed = new URL(rewriteUrl, options.request.url);
const requestOrigin = new URL(options.request.url).origin;
rewritePath = rewriteParsed.origin === requestOrigin ? rewriteParsed.pathname + rewriteParsed.search : rewriteParsed.href;
} catch {
rewritePath = rewriteUrl;
}
return {
continue: true,
rewriteUrl: rewritePath,
rewriteStatus: response.status !== 200 ? response.status : void 0,
responseHeaders: collectMiddlewareHeaders(response),
status: response.status !== 200 ? response.status : void 0,
waitUntilPromises
};
}
return {
continue: false,
response: stripMiddlewareHeadersFromResponse(response),
waitUntilPromises
};
}
async function runGeneratedMiddleware(options) {
const run = () => executeMiddleware(options);
return options.ctx ? runWithExecutionContext(options.ctx, run) : run();
}
//#endregion
export { executeMiddleware, resolveMiddlewareModuleHandler, runGeneratedMiddleware };
//# sourceMappingURL=middleware-runtime.js.map