main repo

This commit is contained in:
Basilosaurusrex
2025-11-24 18:09:40 +01:00
parent b636ee5e70
commit f027651f9b
34146 changed files with 4436636 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
export declare function buildDataRoute(page: string, buildId: string): {
page: string;
routeKeys: {
[named: string]: string;
} | undefined;
dataRouteRegex: string;
namedDataRouteRegex: string | undefined;
};

View File

@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "buildDataRoute", {
enumerable: true,
get: function() {
return buildDataRoute;
}
});
const _path = /*#__PURE__*/ _interop_require_default(require("../../../shared/lib/isomorphic/path"));
const _normalizepagepath = require("../../../shared/lib/page-path/normalize-page-path");
const _isdynamic = require("../../../shared/lib/router/utils/is-dynamic");
const _routeregex = require("../../../shared/lib/router/utils/route-regex");
const _loadcustomroutes = require("../../../lib/load-custom-routes");
const _escaperegexp = require("../../../shared/lib/escape-regexp");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function buildDataRoute(page, buildId) {
const pagePath = (0, _normalizepagepath.normalizePagePath)(page);
const dataRoute = _path.default.posix.join("/_next/data", buildId, `${pagePath}.json`);
let dataRouteRegex;
let namedDataRouteRegex;
let routeKeys;
if ((0, _isdynamic.isDynamicRoute)(page)) {
const routeRegex = (0, _routeregex.getNamedRouteRegex)(dataRoute.replace(/\.json$/, ""), true);
dataRouteRegex = (0, _loadcustomroutes.normalizeRouteRegex)(routeRegex.re.source.replace(/\(\?:\\\/\)\?\$$/, `\\.json$`));
namedDataRouteRegex = routeRegex.namedRegex.replace(/\(\?:\/\)\?\$$/, `\\.json$`);
routeKeys = routeRegex.routeKeys;
} else {
dataRouteRegex = (0, _loadcustomroutes.normalizeRouteRegex)(new RegExp(`^${_path.default.posix.join("/_next/data", (0, _escaperegexp.escapeStringRegexp)(buildId), `${pagePath}.json`)}$`).source);
}
return {
page,
routeKeys,
dataRouteRegex,
namedDataRouteRegex
};
}
//# sourceMappingURL=build-data-route.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/build-data-route.ts"],"names":["buildDataRoute","page","buildId","pagePath","normalizePagePath","dataRoute","path","posix","join","dataRouteRegex","namedDataRouteRegex","routeKeys","isDynamicRoute","routeRegex","getNamedRouteRegex","replace","normalizeRouteRegex","re","source","namedRegex","RegExp","escapeStringRegexp"],"mappings":";;;;+BAOgBA;;;eAAAA;;;6DAPC;mCACiB;2BACH;4BACI;kCACC;8BACD;;;;;;AAE5B,SAASA,eAAeC,IAAY,EAAEC,OAAe;IAC1D,MAAMC,WAAWC,IAAAA,oCAAiB,EAACH;IACnC,MAAMI,YAAYC,aAAI,CAACC,KAAK,CAACC,IAAI,CAAC,eAAeN,SAAS,CAAC,EAAEC,SAAS,KAAK,CAAC;IAE5E,IAAIM;IACJ,IAAIC;IACJ,IAAIC;IAEJ,IAAIC,IAAAA,yBAAc,EAACX,OAAO;QACxB,MAAMY,aAAaC,IAAAA,8BAAkB,EACnCT,UAAUU,OAAO,CAAC,WAAW,KAC7B;QAGFN,iBAAiBO,IAAAA,qCAAmB,EAClCH,WAAWI,EAAE,CAACC,MAAM,CAACH,OAAO,CAAC,oBAAoB,CAAC,QAAQ,CAAC;QAE7DL,sBAAsBG,WAAWM,UAAU,CAAEJ,OAAO,CAClD,kBACA,CAAC,QAAQ,CAAC;QAEZJ,YAAYE,WAAWF,SAAS;IAClC,OAAO;QACLF,iBAAiBO,IAAAA,qCAAmB,EAClC,IAAII,OACF,CAAC,CAAC,EAAEd,aAAI,CAACC,KAAK,CAACC,IAAI,CACjB,eACAa,IAAAA,gCAAkB,EAACnB,UACnB,CAAC,EAAEC,SAAS,KAAK,CAAC,EAClB,CAAC,CAAC,EACJe,MAAM;IAEZ;IAEA,OAAO;QACLjB;QACAU;QACAF;QACAC;IACF;AACF"}

View File

@@ -0,0 +1,75 @@
import type { ManifestRoute, PrerenderManifest } from '../../../build';
import type { NextConfigComplete } from '../../config-shared';
import { PatchMatcher } from '../../../shared/lib/router/utils/path-match';
import { MiddlewareRouteMatch } from '../../../shared/lib/router/utils/middleware-route-matcher';
export type FsOutput = {
type: 'appFile' | 'pageFile' | 'nextImage' | 'publicFolder' | 'nextStaticFolder' | 'legacyStaticFolder' | 'devVirtualFsItem';
itemPath: string;
fsPath?: string;
itemsRoot?: string;
locale?: string;
};
export type FilesystemDynamicRoute = ManifestRoute & {
/**
* The path matcher that can be used to match paths against this route.
*/
match: PatchMatcher;
};
export declare const buildCustomRoute: <T>(type: 'redirect' | 'header' | 'rewrite' | 'before_files_rewrite', item: T & {
source: string;
}, basePath?: string, caseSensitive?: boolean) => T & {
match: PatchMatcher;
check?: boolean | undefined;
};
export declare function setupFsCheck(opts: {
dir: string;
dev: boolean;
minimalMode?: boolean;
config: NextConfigComplete;
addDevWatcherCallback?: (arg: (files: Map<string, {
timestamp: number;
}>) => void) => void;
}): Promise<{
headers: (import("../../../lib/load-custom-routes").Header & {
match: PatchMatcher;
check?: boolean | undefined;
})[];
rewrites: {
beforeFiles: (import("../../../lib/load-custom-routes").Rewrite & {
match: PatchMatcher;
check?: boolean | undefined;
})[];
afterFiles: (import("../../../lib/load-custom-routes").Rewrite & {
match: PatchMatcher;
check?: boolean | undefined;
})[];
fallback: (import("../../../lib/load-custom-routes").Rewrite & {
match: PatchMatcher;
check?: boolean | undefined;
})[];
};
redirects: (import("../../../lib/load-custom-routes").Redirect & {
match: PatchMatcher;
check?: boolean | undefined;
})[];
buildId: string;
handleLocale: (pathname: string, locales?: string[]) => {
locale: string | undefined;
pathname: string;
};
appFiles: Set<string>;
pageFiles: Set<string>;
dynamicRoutes: FilesystemDynamicRoute[];
nextDataRoutes: Set<string>;
interceptionRoutes: {
match: PatchMatcher;
check?: boolean | undefined;
}[] | undefined;
devVirtualFsItems: Set<string>;
prerenderManifest: PrerenderManifest;
middlewareMatcher: MiddlewareRouteMatch | undefined;
ensureCallback(fn: (item: FsOutput) => Promise<void> | undefined): void;
getItem(itemPath: string): Promise<FsOutput | null>;
getDynamicRoutes(): FilesystemDynamicRoute[];
getMiddlewareMatchers(): MiddlewareRouteMatch | undefined;
}>;

View File

@@ -0,0 +1,489 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
buildCustomRoute: null,
setupFsCheck: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
buildCustomRoute: function() {
return buildCustomRoute;
},
setupFsCheck: function() {
return setupFsCheck;
}
});
const _path = /*#__PURE__*/ _interop_require_default(require("path"));
const _promises = /*#__PURE__*/ _interop_require_default(require("fs/promises"));
const _log = /*#__PURE__*/ _interop_require_wildcard(require("../../../build/output/log"));
const _debug = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/debug"));
const _lrucache = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/lru-cache"));
const _loadcustomroutes = /*#__PURE__*/ _interop_require_default(require("../../../lib/load-custom-routes"));
const _redirectstatus = require("../../../lib/redirect-status");
const _fileexists = require("../../../lib/file-exists");
const _recursivereaddir = require("../../../lib/recursive-readdir");
const _utils = require("../../../shared/lib/router/utils");
const _escaperegexp = require("../../../shared/lib/escape-regexp");
const _pathmatch = require("../../../shared/lib/router/utils/path-match");
const _routeregex = require("../../../shared/lib/router/utils/route-regex");
const _routematcher = require("../../../shared/lib/router/utils/route-matcher");
const _pathhasprefix = require("../../../shared/lib/router/utils/path-has-prefix");
const _normalizelocalepath = require("../../../shared/lib/i18n/normalize-locale-path");
const _removepathprefix = require("../../../shared/lib/router/utils/remove-path-prefix");
const _middlewareroutematcher = require("../../../shared/lib/router/utils/middleware-route-matcher");
const _constants = require("../../../shared/lib/constants");
const _normalizepathsep = require("../../../shared/lib/page-path/normalize-path-sep");
const _getmetadataroute = require("../../../lib/metadata/get-metadata-route");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function(nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
return {
default: obj
};
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for(var key in obj){
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
const debug = (0, _debug.default)("next:router-server:filesystem");
const buildCustomRoute = (type, item, basePath, caseSensitive)=>{
const restrictedRedirectPaths = [
"/_next"
].map((p)=>basePath ? `${basePath}${p}` : p);
const match = (0, _pathmatch.getPathMatch)(item.source, {
strict: true,
removeUnnamedParams: true,
regexModifier: !item.internal ? (regex)=>(0, _redirectstatus.modifyRouteRegex)(regex, type === "redirect" ? restrictedRedirectPaths : undefined) : undefined,
sensitive: caseSensitive
});
return {
...item,
...type === "rewrite" ? {
check: true
} : {},
match
};
};
async function setupFsCheck(opts) {
const getItemsLru = !opts.dev ? new _lrucache.default({
max: 1024 * 1024,
length (value, key) {
if (!value) return (key == null ? void 0 : key.length) || 0;
return (key || "").length + (value.fsPath || "").length + value.itemPath.length + value.type.length;
}
}) : undefined;
// routes that have _next/data endpoints (SSG/SSP)
const nextDataRoutes = new Set();
const publicFolderItems = new Set();
const nextStaticFolderItems = new Set();
const legacyStaticFolderItems = new Set();
const appFiles = new Set();
const pageFiles = new Set();
let dynamicRoutes = [];
let middlewareMatcher = ()=>false;
const distDir = _path.default.join(opts.dir, opts.config.distDir);
const publicFolderPath = _path.default.join(opts.dir, "public");
const nextStaticFolderPath = _path.default.join(distDir, "static");
const legacyStaticFolderPath = _path.default.join(opts.dir, "static");
let customRoutes = {
redirects: [],
rewrites: {
beforeFiles: [],
afterFiles: [],
fallback: []
},
headers: []
};
let buildId = "development";
let prerenderManifest;
if (!opts.dev) {
var _middlewareManifest_middleware_, _middlewareManifest_middleware;
const buildIdPath = _path.default.join(opts.dir, opts.config.distDir, _constants.BUILD_ID_FILE);
buildId = await _promises.default.readFile(buildIdPath, "utf8");
try {
for (const file of (await (0, _recursivereaddir.recursiveReadDir)(publicFolderPath))){
// Ensure filename is encoded and normalized.
publicFolderItems.add(encodeURI((0, _normalizepathsep.normalizePathSep)(file)));
}
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
try {
for (const file of (await (0, _recursivereaddir.recursiveReadDir)(legacyStaticFolderPath))){
// Ensure filename is encoded and normalized.
legacyStaticFolderItems.add(encodeURI((0, _normalizepathsep.normalizePathSep)(file)));
}
_log.warn(`The static directory has been deprecated in favor of the public directory. https://nextjs.org/docs/messages/static-dir-deprecated`);
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
try {
for (const file of (await (0, _recursivereaddir.recursiveReadDir)(nextStaticFolderPath))){
// Ensure filename is encoded and normalized.
nextStaticFolderItems.add(_path.default.posix.join("/_next/static", encodeURI((0, _normalizepathsep.normalizePathSep)(file))));
}
} catch (err) {
if (opts.config.output !== "standalone") throw err;
}
const routesManifestPath = _path.default.join(distDir, _constants.ROUTES_MANIFEST);
const prerenderManifestPath = _path.default.join(distDir, _constants.PRERENDER_MANIFEST);
const middlewareManifestPath = _path.default.join(distDir, "server", _constants.MIDDLEWARE_MANIFEST);
const pagesManifestPath = _path.default.join(distDir, "server", _constants.PAGES_MANIFEST);
const appRoutesManifestPath = _path.default.join(distDir, _constants.APP_PATH_ROUTES_MANIFEST);
const routesManifest = JSON.parse(await _promises.default.readFile(routesManifestPath, "utf8"));
prerenderManifest = JSON.parse(await _promises.default.readFile(prerenderManifestPath, "utf8"));
const middlewareManifest = JSON.parse(await _promises.default.readFile(middlewareManifestPath, "utf8").catch(()=>"{}"));
const pagesManifest = JSON.parse(await _promises.default.readFile(pagesManifestPath, "utf8"));
const appRoutesManifest = JSON.parse(await _promises.default.readFile(appRoutesManifestPath, "utf8").catch(()=>"{}"));
for (const key of Object.keys(pagesManifest)){
// ensure the non-locale version is in the set
if (opts.config.i18n) {
pageFiles.add((0, _normalizelocalepath.normalizeLocalePath)(key, opts.config.i18n.locales).pathname);
} else {
pageFiles.add(key);
}
}
for (const key of Object.keys(appRoutesManifest)){
appFiles.add(appRoutesManifest[key]);
}
const escapedBuildId = (0, _escaperegexp.escapeStringRegexp)(buildId);
for (const route of routesManifest.dataRoutes){
if ((0, _utils.isDynamicRoute)(route.page)) {
const routeRegex = (0, _routeregex.getRouteRegex)(route.page);
dynamicRoutes.push({
...route,
regex: routeRegex.re.toString(),
match: (0, _routematcher.getRouteMatcher)({
// TODO: fix this in the manifest itself, must also be fixed in
// upstream builder that relies on this
re: opts.config.i18n ? new RegExp(route.dataRouteRegex.replace(`/${escapedBuildId}/`, `/${escapedBuildId}/(?<nextLocale>[^/]+?)/`)) : new RegExp(route.dataRouteRegex),
groups: routeRegex.groups
})
});
}
nextDataRoutes.add(route.page);
}
for (const route of routesManifest.dynamicRoutes){
dynamicRoutes.push({
...route,
match: (0, _routematcher.getRouteMatcher)((0, _routeregex.getRouteRegex)(route.page))
});
}
if ((_middlewareManifest_middleware = middlewareManifest.middleware) == null ? void 0 : (_middlewareManifest_middleware_ = _middlewareManifest_middleware["/"]) == null ? void 0 : _middlewareManifest_middleware_.matchers) {
var _middlewareManifest_middleware_1, _middlewareManifest_middleware1;
middlewareMatcher = (0, _middlewareroutematcher.getMiddlewareRouteMatcher)((_middlewareManifest_middleware1 = middlewareManifest.middleware) == null ? void 0 : (_middlewareManifest_middleware_1 = _middlewareManifest_middleware1["/"]) == null ? void 0 : _middlewareManifest_middleware_1.matchers);
}
customRoutes = {
redirects: routesManifest.redirects,
rewrites: routesManifest.rewrites ? Array.isArray(routesManifest.rewrites) ? {
beforeFiles: [],
afterFiles: routesManifest.rewrites,
fallback: []
} : routesManifest.rewrites : {
beforeFiles: [],
afterFiles: [],
fallback: []
},
headers: routesManifest.headers
};
} else {
// dev handling
customRoutes = await (0, _loadcustomroutes.default)(opts.config);
prerenderManifest = {
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: {
previewModeId: require("crypto").randomBytes(16).toString("hex"),
previewModeSigningKey: require("crypto").randomBytes(32).toString("hex"),
previewModeEncryptionKey: require("crypto").randomBytes(32).toString("hex")
}
};
}
const headers = customRoutes.headers.map((item)=>buildCustomRoute("header", item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes));
const redirects = customRoutes.redirects.map((item)=>buildCustomRoute("redirect", item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes));
const rewrites = {
// TODO: add interception routes generateInterceptionRoutesRewrites()
beforeFiles: customRoutes.rewrites.beforeFiles.map((item)=>buildCustomRoute("before_files_rewrite", item)),
afterFiles: customRoutes.rewrites.afterFiles.map((item)=>buildCustomRoute("rewrite", item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes)),
fallback: customRoutes.rewrites.fallback.map((item)=>buildCustomRoute("rewrite", item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes))
};
const { i18n } = opts.config;
const handleLocale = (pathname, locales)=>{
let locale;
if (i18n) {
const i18nResult = (0, _normalizelocalepath.normalizeLocalePath)(pathname, locales || i18n.locales);
pathname = i18nResult.pathname;
locale = i18nResult.detectedLocale;
}
return {
locale,
pathname
};
};
debug("nextDataRoutes", nextDataRoutes);
debug("dynamicRoutes", dynamicRoutes);
debug("pageFiles", pageFiles);
debug("appFiles", appFiles);
let ensureFn;
return {
headers,
rewrites,
redirects,
buildId,
handleLocale,
appFiles,
pageFiles,
dynamicRoutes,
nextDataRoutes,
interceptionRoutes: undefined,
devVirtualFsItems: new Set(),
prerenderManifest,
middlewareMatcher: middlewareMatcher,
ensureCallback (fn) {
ensureFn = fn;
},
async getItem (itemPath) {
const originalItemPath = itemPath;
const itemKey = originalItemPath;
const lruResult = getItemsLru == null ? void 0 : getItemsLru.get(itemKey);
if (lruResult) {
return lruResult;
}
// handle minimal mode case with .rsc output path (this is
// mostly for testings)
if (opts.minimalMode && itemPath.endsWith(".rsc")) {
itemPath = itemPath.substring(0, itemPath.length - ".rsc".length);
}
const { basePath } = opts.config;
if (basePath && !(0, _pathhasprefix.pathHasPrefix)(itemPath, basePath)) {
return null;
}
itemPath = (0, _removepathprefix.removePathPrefix)(itemPath, basePath) || "/";
if (itemPath !== "/" && itemPath.endsWith("/")) {
itemPath = itemPath.substring(0, itemPath.length - 1);
}
let decodedItemPath = itemPath;
try {
decodedItemPath = decodeURIComponent(itemPath);
} catch {}
if (itemPath === "/_next/image") {
return {
itemPath,
type: "nextImage"
};
}
const itemsToCheck = [
[
this.devVirtualFsItems,
"devVirtualFsItem"
],
[
nextStaticFolderItems,
"nextStaticFolder"
],
[
legacyStaticFolderItems,
"legacyStaticFolder"
],
[
publicFolderItems,
"publicFolder"
],
[
appFiles,
"appFile"
],
[
pageFiles,
"pageFile"
]
];
for (let [items, type] of itemsToCheck){
let locale;
let curItemPath = itemPath;
let curDecodedItemPath = decodedItemPath;
const isDynamicOutput = type === "pageFile" || type === "appFile";
if (i18n) {
const localeResult = handleLocale(itemPath, // legacy behavior allows visiting static assets under
// default locale but no other locale
isDynamicOutput ? undefined : [
i18n == null ? void 0 : i18n.defaultLocale
]);
if (localeResult.pathname !== curItemPath) {
curItemPath = localeResult.pathname;
locale = localeResult.locale;
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
}
if (type === "legacyStaticFolder") {
if (!(0, _pathhasprefix.pathHasPrefix)(curItemPath, "/static")) {
continue;
}
curItemPath = curItemPath.substring("/static".length);
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
if (type === "nextStaticFolder" && !(0, _pathhasprefix.pathHasPrefix)(curItemPath, "/_next/static")) {
continue;
}
const nextDataPrefix = `/_next/data/${buildId}/`;
if (type === "pageFile" && curItemPath.startsWith(nextDataPrefix) && curItemPath.endsWith(".json")) {
items = nextDataRoutes;
// remove _next/data/<build-id> prefix
curItemPath = curItemPath.substring(nextDataPrefix.length - 1);
// remove .json postfix
curItemPath = curItemPath.substring(0, curItemPath.length - ".json".length);
const curLocaleResult = handleLocale(curItemPath);
curItemPath = curLocaleResult.pathname === "/index" ? "/" : curLocaleResult.pathname;
locale = curLocaleResult.locale;
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
// check decoded variant as well
if (!items.has(curItemPath) && !opts.dev) {
curItemPath = curDecodedItemPath;
}
const matchedItem = items.has(curItemPath);
if (matchedItem || opts.dev) {
let fsPath;
let itemsRoot;
switch(type){
case "nextStaticFolder":
{
itemsRoot = nextStaticFolderPath;
curItemPath = curItemPath.substring("/_next/static".length);
break;
}
case "legacyStaticFolder":
{
itemsRoot = legacyStaticFolderPath;
break;
}
case "publicFolder":
{
itemsRoot = publicFolderPath;
break;
}
default:
{
break;
}
}
if (itemsRoot && curItemPath) {
fsPath = _path.default.posix.join(itemsRoot, curItemPath);
}
// dynamically check fs in development so we don't
// have to wait on the watcher
if (!matchedItem && opts.dev) {
const isStaticAsset = [
"nextStaticFolder",
"publicFolder",
"legacyStaticFolder"
].includes(type);
if (isStaticAsset && itemsRoot) {
let found = fsPath && await (0, _fileexists.fileExists)(fsPath, _fileexists.FileType.File);
if (!found) {
try {
// In dev, we ensure encoded paths match
// decoded paths on the filesystem so check
// that variation as well
const tempItemPath = decodeURIComponent(curItemPath);
fsPath = _path.default.posix.join(itemsRoot, tempItemPath);
found = await (0, _fileexists.fileExists)(fsPath, _fileexists.FileType.File);
} catch {}
if (!found) {
continue;
}
}
} else if (type === "pageFile" || type === "appFile") {
var _ensureFn;
const isAppFile = type === "appFile";
if (ensureFn && await ((_ensureFn = ensureFn({
type,
itemPath: isAppFile ? (0, _getmetadataroute.normalizeMetadataRoute)(curItemPath) : curItemPath
})) == null ? void 0 : _ensureFn.catch(()=>"ENSURE_FAILED")) === "ENSURE_FAILED") {
continue;
}
} else {
continue;
}
}
// i18n locales aren't matched for app dir
if (type === "appFile" && locale && locale !== (i18n == null ? void 0 : i18n.defaultLocale)) {
continue;
}
const itemResult = {
type,
fsPath,
locale,
itemsRoot,
itemPath: curItemPath
};
getItemsLru == null ? void 0 : getItemsLru.set(itemKey, itemResult);
return itemResult;
}
}
getItemsLru == null ? void 0 : getItemsLru.set(itemKey, null);
return null;
},
getDynamicRoutes () {
// this should include data routes
return this.dynamicRoutes;
},
getMiddlewareMatchers () {
return this.middlewareMatcher;
}
};
}
//# sourceMappingURL=filesystem.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,4 @@
/// <reference types="node" />
import type { IncomingMessage, ServerResponse } from 'http';
import type { NextUrlWithParsedQuery } from '../../request-meta';
export declare function proxyRequest(req: IncomingMessage, res: ServerResponse, parsedUrl: NextUrlWithParsedQuery, upgradeHead?: any, reqBody?: any, proxyTimeout?: number | null): Promise<void>;

View File

@@ -0,0 +1,108 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "proxyRequest", {
enumerable: true,
get: function() {
return proxyRequest;
}
});
const _url = /*#__PURE__*/ _interop_require_default(require("url"));
const _serverrouteutils = require("../../server-route-utils");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
async function proxyRequest(req, res, parsedUrl, upgradeHead, reqBody, proxyTimeout) {
const { query } = parsedUrl;
delete parsedUrl.query;
parsedUrl.search = (0, _serverrouteutils.stringifyQuery)(req, query);
const target = _url.default.format(parsedUrl);
const HttpProxy = require("next/dist/compiled/http-proxy");
const proxy = new HttpProxy({
target,
changeOrigin: true,
ignorePath: true,
xfwd: true,
ws: true,
// we limit proxy requests to 30s by default, in development
// we don't time out WebSocket requests to allow proxying
proxyTimeout: proxyTimeout === null ? undefined : proxyTimeout || 30000
});
await new Promise((proxyResolve, proxyReject)=>{
let finished = false;
// http-proxy does not properly detect a client disconnect in newer
// versions of Node.js. This is caused because it only listens for the
// `aborted` event on the our request object, but it also fully reads
// and closes the request object. Node **will not** fire `aborted` when
// the request is already closed. Listening for `close` on our response
// object will detect the disconnect, and we can abort the proxy's
// connection.
proxy.on("proxyReq", (proxyReq)=>{
res.on("close", ()=>proxyReq.destroy());
});
proxy.on("proxyRes", (proxyRes)=>{
if (res.destroyed) {
proxyRes.destroy();
} else {
res.on("close", ()=>proxyRes.destroy());
}
});
proxy.on("proxyRes", (proxyRes, innerReq, innerRes)=>{
const cleanup = (err)=>{
// cleanup event listeners to allow clean garbage collection
proxyRes.removeListener("error", cleanup);
proxyRes.removeListener("close", cleanup);
innerRes.removeListener("error", cleanup);
innerRes.removeListener("close", cleanup);
// destroy all source streams to propagate the caught event backward
innerReq.destroy(err);
proxyRes.destroy(err);
};
proxyRes.once("error", cleanup);
proxyRes.once("close", cleanup);
innerRes.once("error", cleanup);
innerRes.once("close", cleanup);
});
proxy.on("error", (err)=>{
console.error(`Failed to proxy ${target}`, err);
if (!finished) {
finished = true;
proxyReject(err);
if (!res.destroyed) {
res.statusCode = 500;
res.end("Internal Server Error");
}
}
});
// if upgrade head is present treat as WebSocket request
if (upgradeHead) {
proxy.on("proxyReqWs", (proxyReq)=>{
proxyReq.on("close", ()=>{
if (!finished) {
finished = true;
proxyResolve(true);
}
});
});
proxy.ws(req, res, upgradeHead);
proxyResolve(true);
} else {
proxy.on("proxyReq", (proxyReq)=>{
proxyReq.on("close", ()=>{
if (!finished) {
finished = true;
proxyResolve(true);
}
});
});
proxy.web(req, res, {
buffer: reqBody
});
}
});
}
//# sourceMappingURL=proxy-request.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/proxy-request.ts"],"names":["proxyRequest","req","res","parsedUrl","upgradeHead","reqBody","proxyTimeout","query","search","stringifyQuery","target","url","format","HttpProxy","require","proxy","changeOrigin","ignorePath","xfwd","ws","undefined","Promise","proxyResolve","proxyReject","finished","on","proxyReq","destroy","proxyRes","destroyed","innerReq","innerRes","cleanup","err","removeListener","once","console","error","statusCode","end","web","buffer"],"mappings":";;;;+BAMsBA;;;eAAAA;;;4DAHN;kCACe;;;;;;AAExB,eAAeA,aACpBC,GAAoB,EACpBC,GAAmB,EACnBC,SAAiC,EACjCC,WAAiB,EACjBC,OAAa,EACbC,YAA4B;IAE5B,MAAM,EAAEC,KAAK,EAAE,GAAGJ;IAClB,OAAO,AAACA,UAAkBI,KAAK;IAC/BJ,UAAUK,MAAM,GAAGC,IAAAA,gCAAc,EAACR,KAAYM;IAE9C,MAAMG,SAASC,YAAG,CAACC,MAAM,CAACT;IAC1B,MAAMU,YACJC,QAAQ;IAEV,MAAMC,QAAQ,IAAIF,UAAU;QAC1BH;QACAM,cAAc;QACdC,YAAY;QACZC,MAAM;QACNC,IAAI;QACJ,4DAA4D;QAC5D,yDAAyD;QACzDb,cAAcA,iBAAiB,OAAOc,YAAYd,gBAAgB;IACpE;IAEA,MAAM,IAAIe,QAAQ,CAACC,cAAcC;QAC/B,IAAIC,WAAW;QAEf,mEAAmE;QACnE,sEAAsE;QACtE,qEAAqE;QACrE,uEAAuE;QACvE,uEAAuE;QACvE,kEAAkE;QAClE,cAAc;QACdT,MAAMU,EAAE,CAAC,YAAY,CAACC;YACpBxB,IAAIuB,EAAE,CAAC,SAAS,IAAMC,SAASC,OAAO;QACxC;QACAZ,MAAMU,EAAE,CAAC,YAAY,CAACG;YACpB,IAAI1B,IAAI2B,SAAS,EAAE;gBACjBD,SAASD,OAAO;YAClB,OAAO;gBACLzB,IAAIuB,EAAE,CAAC,SAAS,IAAMG,SAASD,OAAO;YACxC;QACF;QAEAZ,MAAMU,EAAE,CAAC,YAAY,CAACG,UAAUE,UAAUC;YACxC,MAAMC,UAAU,CAACC;gBACf,4DAA4D;gBAC5DL,SAASM,cAAc,CAAC,SAASF;gBACjCJ,SAASM,cAAc,CAAC,SAASF;gBACjCD,SAASG,cAAc,CAAC,SAASF;gBACjCD,SAASG,cAAc,CAAC,SAASF;gBAEjC,oEAAoE;gBACpEF,SAASH,OAAO,CAACM;gBACjBL,SAASD,OAAO,CAACM;YACnB;YAEAL,SAASO,IAAI,CAAC,SAASH;YACvBJ,SAASO,IAAI,CAAC,SAASH;YACvBD,SAASI,IAAI,CAAC,SAASH;YACvBD,SAASI,IAAI,CAAC,SAASH;QACzB;QAEAjB,MAAMU,EAAE,CAAC,SAAS,CAACQ;YACjBG,QAAQC,KAAK,CAAC,CAAC,gBAAgB,EAAE3B,OAAO,CAAC,EAAEuB;YAC3C,IAAI,CAACT,UAAU;gBACbA,WAAW;gBACXD,YAAYU;gBAEZ,IAAI,CAAC/B,IAAI2B,SAAS,EAAE;oBAClB3B,IAAIoC,UAAU,GAAG;oBACjBpC,IAAIqC,GAAG,CAAC;gBACV;YACF;QACF;QAEA,wDAAwD;QACxD,IAAInC,aAAa;YACfW,MAAMU,EAAE,CAAC,cAAc,CAACC;gBACtBA,SAASD,EAAE,CAAC,SAAS;oBACnB,IAAI,CAACD,UAAU;wBACbA,WAAW;wBACXF,aAAa;oBACf;gBACF;YACF;YACAP,MAAMI,EAAE,CAAClB,KAA+BC,KAAKE;YAC7CkB,aAAa;QACf,OAAO;YACLP,MAAMU,EAAE,CAAC,YAAY,CAACC;gBACpBA,SAASD,EAAE,CAAC,SAAS;oBACnB,IAAI,CAACD,UAAU;wBACbA,WAAW;wBACXF,aAAa;oBACf;gBACF;YACF;YACAP,MAAMyB,GAAG,CAACvC,KAAKC,KAAK;gBAClBuC,QAAQpC;YACV;QACF;IACF;AACF"}

View File

@@ -0,0 +1,24 @@
import type { FsOutput } from './filesystem';
import type { IncomingMessage, ServerResponse } from 'http';
import type { NextConfigComplete } from '../../config-shared';
import type { RenderWorker, initialize } from '../router-server';
import { UnwrapPromise } from '../../../lib/coalesced-function';
import { NextUrlWithParsedQuery } from '../../request-meta';
import '../../node-polyfill-web-streams';
export declare function getResolveRoutes(fsChecker: UnwrapPromise<ReturnType<typeof import('./filesystem').setupFsCheck>>, config: NextConfigComplete, opts: Parameters<typeof initialize>[0], renderWorkers: {
app?: RenderWorker;
pages?: RenderWorker;
}, renderWorkerOpts: Parameters<RenderWorker['initialize']>[0], ensureMiddleware?: () => Promise<void>): ({ req, res, isUpgradeReq, invokedOutputs, }: {
req: IncomingMessage;
res: ServerResponse;
isUpgradeReq: boolean;
signal: AbortSignal;
invokedOutputs?: Set<string> | undefined;
}) => Promise<{
finished: boolean;
statusCode?: number | undefined;
bodyStream?: ReadableStream<any> | null | undefined;
resHeaders: Record<string, string | string[]>;
parsedUrl: NextUrlWithParsedQuery;
matchedOutput?: FsOutput | null | undefined;
}>;

View File

@@ -0,0 +1,530 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "getResolveRoutes", {
enumerable: true,
get: function() {
return getResolveRoutes;
}
});
const _url = /*#__PURE__*/ _interop_require_default(require("url"));
const _debug = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/debug"));
const _bodystreams = require("../../body-streams");
const _utils = require("../server-ipc/utils");
const _serverrouteutils = require("../../server-route-utils");
const _formathostname = require("../format-hostname");
const _utils1 = require("../../web/utils");
const _pipereadable = require("../../pipe-readable");
const _gethostname = require("../../../shared/lib/get-hostname");
const _redirectstatus = require("../../../lib/redirect-status");
const _utils2 = require("../../../shared/lib/utils");
const _relativizeurl = require("../../../shared/lib/router/utils/relativize-url");
const _addpathprefix = require("../../../shared/lib/router/utils/add-path-prefix");
const _pathhasprefix = require("../../../shared/lib/router/utils/path-has-prefix");
const _detectdomainlocale = require("../../../shared/lib/i18n/detect-domain-locale");
const _normalizelocalepath = require("../../../shared/lib/i18n/normalize-locale-path");
const _removepathprefix = require("../../../shared/lib/router/utils/remove-path-prefix");
const _requestmeta = require("../../request-meta");
const _preparedestination = require("../../../shared/lib/router/utils/prepare-destination");
const _mockrequest = require("../mock-request");
require("../../node-polyfill-web-streams");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
const debug = (0, _debug.default)("next:router-server:resolve-routes");
function getResolveRoutes(fsChecker, config, opts, renderWorkers, renderWorkerOpts, ensureMiddleware) {
const routes = [
// _next/data with middleware handling
{
match: ()=>({}),
name: "middleware_next_data"
},
...opts.minimalMode ? [] : fsChecker.headers,
...opts.minimalMode ? [] : fsChecker.redirects,
// check middleware (using matchers)
{
match: ()=>({}),
name: "middleware"
},
...opts.minimalMode ? [] : fsChecker.rewrites.beforeFiles,
// check middleware (using matchers)
{
match: ()=>({}),
name: "before_files_end"
},
// we check exact matches on fs before continuing to
// after files rewrites
{
match: ()=>({}),
name: "check_fs"
},
...opts.minimalMode ? [] : fsChecker.rewrites.afterFiles,
// we always do the check: true handling before continuing to
// fallback rewrites
{
check: true,
match: ()=>({}),
name: "after files check: true"
},
...opts.minimalMode ? [] : fsChecker.rewrites.fallback
];
async function resolveRoutes({ req, res, isUpgradeReq, invokedOutputs }) {
var _this;
let finished = false;
let resHeaders = {};
let matchedOutput = null;
let parsedUrl = _url.default.parse(req.url || "", true);
let didRewrite = false;
const urlParts = (req.url || "").split("?");
const urlNoQuery = urlParts[0];
// this normalizes repeated slashes in the path e.g. hello//world ->
// hello/world or backslashes to forward slashes, this does not
// handle trailing slash as that is handled the same as a next.config.js
// redirect
if (urlNoQuery == null ? void 0 : urlNoQuery.match(/(\\|\/\/)/)) {
parsedUrl = _url.default.parse((0, _utils2.normalizeRepeatedSlashes)(req.url), true);
return {
parsedUrl,
resHeaders,
finished: true,
statusCode: 308
};
}
// TODO: inherit this from higher up
const protocol = ((_this = req == null ? void 0 : req.socket) == null ? void 0 : _this.encrypted) || req.headers["x-forwarded-proto"] === "https" ? "https" : "http";
// When there are hostname and port we build an absolute URL
const initUrl = config.experimental.trustHostHeader ? `https://${req.headers.host || "localhost"}${req.url}` : opts.port ? `${protocol}://${(0, _formathostname.formatHostname)(opts.hostname || "localhost")}:${opts.port}${req.url}` : req.url || "";
(0, _requestmeta.addRequestMeta)(req, "__NEXT_INIT_URL", initUrl);
(0, _requestmeta.addRequestMeta)(req, "__NEXT_INIT_QUERY", {
...parsedUrl.query
});
(0, _requestmeta.addRequestMeta)(req, "_protocol", protocol);
if (!isUpgradeReq) {
(0, _requestmeta.addRequestMeta)(req, "__NEXT_CLONABLE_BODY", (0, _bodystreams.getCloneableBody)(req));
}
const maybeAddTrailingSlash = (pathname)=>{
if (config.trailingSlash && !config.skipMiddlewareUrlNormalize && !pathname.endsWith("/")) {
return `${pathname}/`;
}
return pathname;
};
let domainLocale;
let defaultLocale;
let initialLocaleResult = undefined;
if (config.i18n) {
var _parsedUrl_pathname;
const hadTrailingSlash = (_parsedUrl_pathname = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname.endsWith("/");
const hadBasePath = (0, _pathhasprefix.pathHasPrefix)(parsedUrl.pathname || "", config.basePath);
initialLocaleResult = (0, _normalizelocalepath.normalizeLocalePath)((0, _removepathprefix.removePathPrefix)(parsedUrl.pathname || "/", config.basePath), config.i18n.locales);
domainLocale = (0, _detectdomainlocale.detectDomainLocale)(config.i18n.domains, (0, _gethostname.getHostname)(parsedUrl, req.headers));
defaultLocale = (domainLocale == null ? void 0 : domainLocale.defaultLocale) || config.i18n.defaultLocale;
parsedUrl.query.__nextDefaultLocale = defaultLocale;
parsedUrl.query.__nextLocale = initialLocaleResult.detectedLocale || defaultLocale;
// ensure locale is present for resolving routes
if (!initialLocaleResult.detectedLocale && !initialLocaleResult.pathname.startsWith("/_next/")) {
parsedUrl.pathname = (0, _addpathprefix.addPathPrefix)(initialLocaleResult.pathname === "/" ? `/${defaultLocale}` : (0, _addpathprefix.addPathPrefix)(initialLocaleResult.pathname || "", `/${defaultLocale}`), hadBasePath ? config.basePath : "");
if (hadTrailingSlash) {
parsedUrl.pathname = maybeAddTrailingSlash(parsedUrl.pathname);
}
}
}
const checkLocaleApi = (pathname)=>{
if (config.i18n && pathname === urlNoQuery && (initialLocaleResult == null ? void 0 : initialLocaleResult.detectedLocale) && (0, _pathhasprefix.pathHasPrefix)(initialLocaleResult.pathname, "/api")) {
return true;
}
};
async function checkTrue() {
const pathname = parsedUrl.pathname || "";
if (checkLocaleApi(pathname)) {
return;
}
if (!(invokedOutputs == null ? void 0 : invokedOutputs.has(pathname))) {
const output = await fsChecker.getItem(pathname);
if (output) {
if (config.useFileSystemPublicRoutes || didRewrite || output.type !== "appFile" && output.type !== "pageFile") {
return output;
}
}
}
const dynamicRoutes = fsChecker.getDynamicRoutes();
let curPathname = parsedUrl.pathname;
if (config.basePath) {
if (!(0, _pathhasprefix.pathHasPrefix)(curPathname || "", config.basePath)) {
return;
}
curPathname = (curPathname == null ? void 0 : curPathname.substring(config.basePath.length)) || "/";
}
const localeResult = fsChecker.handleLocale(curPathname || "");
for (const route of dynamicRoutes){
// when resolving fallback: false the
// render worker may return a no-fallback response
// which signals we need to continue resolving.
// TODO: optimize this to collect static paths
// to use at the routing layer
if (invokedOutputs == null ? void 0 : invokedOutputs.has(route.page)) {
continue;
}
const params = route.match(localeResult.pathname);
if (params) {
const pageOutput = await fsChecker.getItem((0, _addpathprefix.addPathPrefix)(route.page, config.basePath || ""));
// i18n locales aren't matched for app dir
if ((pageOutput == null ? void 0 : pageOutput.type) === "appFile" && (initialLocaleResult == null ? void 0 : initialLocaleResult.detectedLocale)) {
continue;
}
if (pageOutput && (curPathname == null ? void 0 : curPathname.startsWith("/_next/data"))) {
parsedUrl.query.__nextDataReq = "1";
}
if (config.useFileSystemPublicRoutes || didRewrite) {
return pageOutput;
}
}
}
}
async function handleRoute(route) {
let curPathname = parsedUrl.pathname || "/";
if (config.i18n && route.internal) {
const hadTrailingSlash = curPathname.endsWith("/");
if (config.basePath) {
curPathname = (0, _removepathprefix.removePathPrefix)(curPathname, config.basePath);
}
const hadBasePath = curPathname !== parsedUrl.pathname;
const localeResult = (0, _normalizelocalepath.normalizeLocalePath)(curPathname, config.i18n.locales);
const isDefaultLocale = localeResult.detectedLocale === defaultLocale;
if (isDefaultLocale) {
curPathname = localeResult.pathname === "/" && hadBasePath ? config.basePath : (0, _addpathprefix.addPathPrefix)(localeResult.pathname, hadBasePath ? config.basePath : "");
} else if (hadBasePath) {
curPathname = curPathname === "/" ? config.basePath : (0, _addpathprefix.addPathPrefix)(curPathname, config.basePath);
}
if ((isDefaultLocale || hadBasePath) && hadTrailingSlash) {
curPathname = maybeAddTrailingSlash(curPathname);
}
}
let params = route.match(curPathname);
if ((route.has || route.missing) && params) {
const hasParams = (0, _preparedestination.matchHas)(req, parsedUrl.query, route.has, route.missing);
if (hasParams) {
Object.assign(params, hasParams);
} else {
params = false;
}
}
if (params) {
if (fsChecker.interceptionRoutes && route.name === "before_files_end") {
for (const interceptionRoute of fsChecker.interceptionRoutes){
const result = await handleRoute(interceptionRoute);
if (result) {
return result;
}
}
}
if (route.name === "middleware_next_data") {
var _fsChecker_getMiddlewareMatchers;
if ((_fsChecker_getMiddlewareMatchers = fsChecker.getMiddlewareMatchers()) == null ? void 0 : _fsChecker_getMiddlewareMatchers.length) {
var _parsedUrl_pathname;
const nextDataPrefix = (0, _addpathprefix.addPathPrefix)(`/_next/data/${fsChecker.buildId}/`, config.basePath);
if (((_parsedUrl_pathname = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname.startsWith(nextDataPrefix)) && parsedUrl.pathname.endsWith(".json")) {
parsedUrl.query.__nextDataReq = "1";
parsedUrl.pathname = parsedUrl.pathname.substring(nextDataPrefix.length - 1);
parsedUrl.pathname = parsedUrl.pathname.substring(0, parsedUrl.pathname.length - ".json".length);
parsedUrl.pathname = (0, _addpathprefix.addPathPrefix)(parsedUrl.pathname || "", config.basePath);
parsedUrl.pathname = parsedUrl.pathname === "/index" ? "/" : parsedUrl.pathname;
parsedUrl.pathname = maybeAddTrailingSlash(parsedUrl.pathname);
}
}
}
if (route.name === "check_fs") {
const pathname = parsedUrl.pathname || "";
if ((invokedOutputs == null ? void 0 : invokedOutputs.has(pathname)) || checkLocaleApi(pathname)) {
return;
}
const output = await fsChecker.getItem(pathname);
if (output && !(config.i18n && (initialLocaleResult == null ? void 0 : initialLocaleResult.detectedLocale) && (0, _pathhasprefix.pathHasPrefix)(pathname, "/api"))) {
if (config.useFileSystemPublicRoutes || didRewrite || output.type !== "appFile" && output.type !== "pageFile") {
matchedOutput = output;
if (output.locale) {
parsedUrl.query.__nextLocale = output.locale;
}
return {
parsedUrl,
resHeaders,
finished: true,
matchedOutput
};
}
}
}
if (!opts.minimalMode && route.name === "middleware") {
const match = fsChecker.getMiddlewareMatchers();
if (// @ts-expect-error BaseNextRequest stuff
(match == null ? void 0 : match(parsedUrl.pathname, req, parsedUrl.query)) && (!ensureMiddleware || await (ensureMiddleware == null ? void 0 : ensureMiddleware().then(()=>true).catch(()=>false)))) {
var _this;
const workerResult = await ((_this = renderWorkers.app || renderWorkers.pages) == null ? void 0 : _this.initialize(renderWorkerOpts));
if (!workerResult) {
throw new Error(`Failed to initialize render worker "middleware"`);
}
const invokeHeaders = {
"x-invoke-path": "",
"x-invoke-query": "",
"x-invoke-output": "",
"x-middleware-invoke": "1"
};
Object.assign(req.headers, invokeHeaders);
debug("invoking middleware", req.url, invokeHeaders);
let middlewareRes = undefined;
let bodyStream = undefined;
try {
var _renderWorkers_pages;
let readableController;
const { res: mockedRes } = await (0, _mockrequest.createRequestResponseMocks)({
url: req.url || "/",
method: req.method || "GET",
headers: (0, _utils.filterReqHeaders)(invokeHeaders, _utils.ipcForbiddenHeaders),
resWriter (chunk) {
readableController.enqueue(Buffer.from(chunk));
return true;
}
});
const initResult = await ((_renderWorkers_pages = renderWorkers.pages) == null ? void 0 : _renderWorkers_pages.initialize(renderWorkerOpts));
mockedRes.on("close", ()=>{
readableController.close();
});
try {
await (initResult == null ? void 0 : initResult.requestHandler(req, res, parsedUrl));
} catch (err) {
if (!("result" in err) || !("response" in err.result)) {
throw err;
}
middlewareRes = err.result.response;
res.statusCode = middlewareRes.status;
if (middlewareRes.body) {
bodyStream = middlewareRes.body;
} else if (middlewareRes.status) {
bodyStream = new ReadableStream({
start (controller) {
controller.enqueue("");
controller.close();
}
});
}
}
} catch (e) {
// If the client aborts before we can receive a response object
// (when the headers are flushed), then we can early exit without
// further processing.
if ((0, _pipereadable.isAbortError)(e)) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
throw e;
}
if (res.closed || res.finished || !middlewareRes) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
const middlewareHeaders = (0, _utils1.toNodeOutgoingHttpHeaders)(middlewareRes.headers);
debug("middleware res", middlewareRes.status, middlewareHeaders);
if (middlewareHeaders["x-middleware-override-headers"]) {
const overriddenHeaders = new Set();
let overrideHeaders = middlewareHeaders["x-middleware-override-headers"];
if (typeof overrideHeaders === "string") {
overrideHeaders = overrideHeaders.split(",");
}
for (const key of overrideHeaders){
overriddenHeaders.add(key.trim());
}
delete middlewareHeaders["x-middleware-override-headers"];
// Delete headers.
for (const key of Object.keys(req.headers)){
if (!overriddenHeaders.has(key)) {
delete req.headers[key];
}
}
// Update or add headers.
for (const key of overriddenHeaders.keys()){
const valueKey = "x-middleware-request-" + key;
const newValue = middlewareHeaders[valueKey];
const oldValue = req.headers[key];
if (oldValue !== newValue) {
req.headers[key] = newValue === null ? undefined : newValue;
}
delete middlewareHeaders[valueKey];
}
}
if (!middlewareHeaders["x-middleware-rewrite"] && !middlewareHeaders["x-middleware-next"] && !middlewareHeaders["location"]) {
middlewareHeaders["x-middleware-refresh"] = "1";
}
delete middlewareHeaders["x-middleware-next"];
for (const [key, value] of Object.entries({
...(0, _utils.filterReqHeaders)(middlewareHeaders, _utils.ipcForbiddenHeaders)
})){
if ([
"content-length",
"x-middleware-rewrite",
"x-middleware-redirect",
"x-middleware-refresh",
"x-middleware-invoke",
"x-invoke-path",
"x-invoke-query"
].includes(key)) {
continue;
}
if (value) {
resHeaders[key] = value;
req.headers[key] = value;
}
}
if (middlewareHeaders["x-middleware-rewrite"]) {
const value = middlewareHeaders["x-middleware-rewrite"];
const rel = (0, _relativizeurl.relativizeURL)(value, initUrl);
resHeaders["x-middleware-rewrite"] = rel;
const query = parsedUrl.query;
parsedUrl = _url.default.parse(rel, true);
if (parsedUrl.protocol) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
// keep internal query state
for (const key of Object.keys(query)){
if (key.startsWith("_next") || key.startsWith("__next")) {
parsedUrl.query[key] = query[key];
}
}
if (config.i18n) {
const curLocaleResult = (0, _normalizelocalepath.normalizeLocalePath)(parsedUrl.pathname || "", config.i18n.locales);
if (curLocaleResult.detectedLocale) {
parsedUrl.query.__nextLocale = curLocaleResult.detectedLocale;
}
}
}
if (middlewareHeaders["location"]) {
const value = middlewareHeaders["location"];
const rel = (0, _relativizeurl.relativizeURL)(value, initUrl);
resHeaders["location"] = rel;
parsedUrl = _url.default.parse(rel, true);
return {
parsedUrl,
resHeaders,
finished: true,
statusCode: middlewareRes.status
};
}
if (middlewareHeaders["x-middleware-refresh"]) {
return {
parsedUrl,
resHeaders,
finished: true,
bodyStream,
statusCode: middlewareRes.status
};
}
}
}
// handle redirect
if (("statusCode" in route || "permanent" in route) && route.destination) {
const { parsedDestination } = (0, _preparedestination.prepareDestination)({
appendParamsToQuery: false,
destination: route.destination,
params: params,
query: parsedUrl.query
});
const { query } = parsedDestination;
delete parsedDestination.query;
parsedDestination.search = (0, _serverrouteutils.stringifyQuery)(req, query);
parsedDestination.pathname = (0, _utils2.normalizeRepeatedSlashes)(parsedDestination.pathname);
return {
finished: true,
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
statusCode: (0, _redirectstatus.getRedirectStatus)(route)
};
}
// handle headers
if (route.headers) {
const hasParams = Object.keys(params).length > 0;
for (const header of route.headers){
let { key, value } = header;
if (hasParams) {
key = (0, _preparedestination.compileNonPath)(key, params);
value = (0, _preparedestination.compileNonPath)(value, params);
}
if (key.toLowerCase() === "set-cookie") {
if (!Array.isArray(resHeaders[key])) {
const val = resHeaders[key];
resHeaders[key] = typeof val === "string" ? [
val
] : [];
}
resHeaders[key].push(value);
} else {
resHeaders[key] = value;
}
}
}
// handle rewrite
if (route.destination) {
const { parsedDestination } = (0, _preparedestination.prepareDestination)({
appendParamsToQuery: true,
destination: route.destination,
params: params,
query: parsedUrl.query
});
if (parsedDestination.protocol) {
return {
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
finished: true
};
}
if (config.i18n) {
const curLocaleResult = (0, _normalizelocalepath.normalizeLocalePath)((0, _removepathprefix.removePathPrefix)(parsedDestination.pathname, config.basePath), config.i18n.locales);
if (curLocaleResult.detectedLocale) {
parsedUrl.query.__nextLocale = curLocaleResult.detectedLocale;
}
}
didRewrite = true;
parsedUrl.pathname = parsedDestination.pathname;
Object.assign(parsedUrl.query, parsedDestination.query);
}
// handle check: true
if (route.check) {
const output = await checkTrue();
if (output) {
return {
parsedUrl,
resHeaders,
finished: true,
matchedOutput: output
};
}
}
}
}
for (const route of routes){
const result = await handleRoute(route);
if (result) {
return result;
}
}
return {
finished,
parsedUrl,
resHeaders,
matchedOutput
};
}
return resolveRoutes;
}
//# sourceMappingURL=resolve-routes.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,45 @@
/// <reference types="node" />
import type { NextConfigComplete } from '../../config-shared';
import { Telemetry } from '../../../telemetry/storage';
import { IncomingMessage, ServerResponse } from 'http';
import { UnwrapPromise } from '../../../lib/coalesced-function';
import { MiddlewareMatcher } from '../../../build/analysis/get-page-static-info';
import { MiddlewareRouteMatch } from '../../../shared/lib/router/utils/middleware-route-matcher';
import type { RenderWorkers } from '../router-server';
import { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types';
type SetupOpts = {
renderWorkers: RenderWorkers;
dir: string;
turbo?: boolean;
appDir?: string;
pagesDir?: string;
telemetry: Telemetry;
isCustomServer?: boolean;
fsChecker: UnwrapPromise<ReturnType<typeof import('./filesystem').setupFsCheck>>;
nextConfig: NextConfigComplete;
port: number;
};
export declare function setupDev(opts: SetupOpts): Promise<{
serverFields: {
actualMiddlewareFile?: string | undefined;
actualInstrumentationHookFile?: string | undefined;
appPathRoutes?: Record<string, string | string[]> | undefined;
middleware?: {
page: string;
match: MiddlewareRouteMatch;
matchers?: MiddlewareMatcher[] | undefined;
} | undefined;
hasAppNotFound?: boolean | undefined;
interceptionRoutes?: {
match: import("../../../shared/lib/router/utils/path-match").PatchMatcher;
check?: boolean | undefined;
}[] | undefined;
};
hotReloader: NextJsHotReloaderInterface;
requestHandler: (req: IncomingMessage, res: ServerResponse<IncomingMessage>) => Promise<{
finished: boolean;
}>;
logErrorWithOriginalStack: (err: unknown, type?: "warning" | "uncaughtException" | "unhandledRejection" | "app-dir" | undefined) => Promise<void>;
ensureMiddleware(): Promise<void>;
}>;
export {};

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
export type PropagateToWorkersField = 'actualMiddlewareFile' | 'actualInstrumentationHookFile' | 'reloadMatchers' | 'loadEnvConfig' | 'appPathRoutes' | 'middleware';

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":[],"names":[],"mappings":""}