inital commit

This commit is contained in:
2026-01-01 15:25:19 +05:30
commit f0ae49465a
36361 changed files with 4894111 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import { DEFAULT_SEGMENT_KEY } from '../../shared/lib/segment';
export async function getLayoutOrPageModule(loaderTree) {
const { layout, page, defaultPage } = loaderTree[2];
const isLayout = typeof layout !== 'undefined';
const isPage = typeof page !== 'undefined';
const isDefaultPage = typeof defaultPage !== 'undefined' && loaderTree[0] === DEFAULT_SEGMENT_KEY;
let mod = undefined;
let modType = undefined;
let filePath = undefined;
if (isLayout) {
mod = await layout[0]();
modType = 'layout';
filePath = layout[1];
} else if (isPage) {
mod = await page[0]();
modType = 'page';
filePath = page[1];
} else if (isDefaultPage) {
mod = await defaultPage[0]();
modType = 'page';
filePath = defaultPage[1];
}
return {
mod,
modType,
filePath
};
}
export async function getComponentTypeModule(loaderTree, moduleType) {
const { [moduleType]: module } = loaderTree[2];
if (typeof module !== 'undefined') {
return await module[0]();
}
return undefined;
}
//# sourceMappingURL=app-dir-module.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/app-dir-module.ts"],"sourcesContent":["import type { AppDirModules } from '../../build/webpack/loaders/next-app-loader'\nimport { DEFAULT_SEGMENT_KEY } from '../../shared/lib/segment'\n\n/**\n * LoaderTree is generated in next-app-loader.\n */\nexport type LoaderTree = [\n segment: string,\n parallelRoutes: { [parallelRouterKey: string]: LoaderTree },\n modules: AppDirModules,\n]\n\nexport async function getLayoutOrPageModule(loaderTree: LoaderTree) {\n const { layout, page, defaultPage } = loaderTree[2]\n const isLayout = typeof layout !== 'undefined'\n const isPage = typeof page !== 'undefined'\n const isDefaultPage =\n typeof defaultPage !== 'undefined' && loaderTree[0] === DEFAULT_SEGMENT_KEY\n\n let mod = undefined\n let modType: 'layout' | 'page' | undefined = undefined\n let filePath = undefined\n\n if (isLayout) {\n mod = await layout[0]()\n modType = 'layout'\n filePath = layout[1]\n } else if (isPage) {\n mod = await page[0]()\n modType = 'page'\n filePath = page[1]\n } else if (isDefaultPage) {\n mod = await defaultPage[0]()\n modType = 'page'\n filePath = defaultPage[1]\n }\n\n return { mod, modType, filePath }\n}\n\nexport async function getComponentTypeModule(\n loaderTree: LoaderTree,\n moduleType: 'layout' | 'not-found' | 'forbidden' | 'unauthorized'\n) {\n const { [moduleType]: module } = loaderTree[2]\n if (typeof module !== 'undefined') {\n return await module[0]()\n }\n return undefined\n}\n"],"names":["DEFAULT_SEGMENT_KEY","getLayoutOrPageModule","loaderTree","layout","page","defaultPage","isLayout","isPage","isDefaultPage","mod","undefined","modType","filePath","getComponentTypeModule","moduleType","module"],"mappings":"AACA,SAASA,mBAAmB,QAAQ,2BAA0B;AAW9D,OAAO,eAAeC,sBAAsBC,UAAsB;IAChE,MAAM,EAAEC,MAAM,EAAEC,IAAI,EAAEC,WAAW,EAAE,GAAGH,UAAU,CAAC,EAAE;IACnD,MAAMI,WAAW,OAAOH,WAAW;IACnC,MAAMI,SAAS,OAAOH,SAAS;IAC/B,MAAMI,gBACJ,OAAOH,gBAAgB,eAAeH,UAAU,CAAC,EAAE,KAAKF;IAE1D,IAAIS,MAAMC;IACV,IAAIC,UAAyCD;IAC7C,IAAIE,WAAWF;IAEf,IAAIJ,UAAU;QACZG,MAAM,MAAMN,MAAM,CAAC,EAAE;QACrBQ,UAAU;QACVC,WAAWT,MAAM,CAAC,EAAE;IACtB,OAAO,IAAII,QAAQ;QACjBE,MAAM,MAAML,IAAI,CAAC,EAAE;QACnBO,UAAU;QACVC,WAAWR,IAAI,CAAC,EAAE;IACpB,OAAO,IAAII,eAAe;QACxBC,MAAM,MAAMJ,WAAW,CAAC,EAAE;QAC1BM,UAAU;QACVC,WAAWP,WAAW,CAAC,EAAE;IAC3B;IAEA,OAAO;QAAEI;QAAKE;QAASC;IAAS;AAClC;AAEA,OAAO,eAAeC,uBACpBX,UAAsB,EACtBY,UAAiE;IAEjE,MAAM,EAAE,CAACA,WAAW,EAAEC,MAAM,EAAE,GAAGb,UAAU,CAAC,EAAE;IAC9C,IAAI,OAAOa,WAAW,aAAa;QACjC,OAAO,MAAMA,MAAM,CAAC,EAAE;IACxB;IACA,OAAOL;AACT"}

52
node_modules/next/dist/esm/server/lib/app-info-log.js generated vendored Normal file
View File

@@ -0,0 +1,52 @@
import { loadEnvConfig } from '@next/env';
import * as Log from '../../build/output/log';
import { bold, purple } from '../../lib/picocolors';
import { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants';
import loadConfig, { getConfiguredExperimentalFeatures } from '../config';
export function logStartInfo({ networkUrl, appUrl, envInfo, experimentalFeatures, maxExperimentalFeatures = Infinity }) {
Log.bootstrap(`${bold(purple(`${Log.prefixes.ready} Next.js ${"15.2.4"}`))}${process.env.TURBOPACK ? ' (Turbopack)' : ''}`);
if (appUrl) {
Log.bootstrap(`- Local: ${appUrl}`);
}
if (networkUrl) {
Log.bootstrap(`- Network: ${networkUrl}`);
}
if (envInfo == null ? void 0 : envInfo.length) Log.bootstrap(`- Environments: ${envInfo.join(', ')}`);
if (experimentalFeatures == null ? void 0 : experimentalFeatures.length) {
Log.bootstrap(`- Experiments (use with caution):`);
// only show a maximum number of flags
for (const exp of experimentalFeatures.slice(0, maxExperimentalFeatures)){
const symbol = exp.type === 'boolean' ? exp.value === true ? bold('✓') : bold('') : '·';
const suffix = exp.type === 'number' ? `: ${exp.value}` : '';
Log.bootstrap(` ${symbol} ${exp.name}${suffix}`);
}
/* indicate if there are more than the maximum shown no. flags */ if (experimentalFeatures.length > maxExperimentalFeatures) {
Log.bootstrap(` · ...`);
}
}
// New line after the bootstrap info
Log.info('');
}
export async function getStartServerInfo(dir, dev) {
let experimentalFeatures = [];
await loadConfig(dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD, dir, {
onLoadUserConfig (userConfig) {
const configuredExperimentalFeatures = getConfiguredExperimentalFeatures(userConfig.experimental);
experimentalFeatures = configuredExperimentalFeatures.sort(({ name: a }, { name: b })=>a.length - b.length);
}
});
// we need to reset env if we are going to create
// the worker process with the esm loader so that the
// initial env state is correct
let envInfo = [];
const { loadedEnvFiles } = loadEnvConfig(dir, true, console, false);
if (loadedEnvFiles.length > 0) {
envInfo = loadedEnvFiles.map((f)=>f.path);
}
return {
envInfo,
experimentalFeatures
};
}
//# sourceMappingURL=app-info-log.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
export class AsyncCallbackSet {
add(callback) {
this.callbacks.push(callback);
}
async runAll() {
if (!this.callbacks.length) {
return;
}
const callbacks = this.callbacks;
this.callbacks = [];
await Promise.allSettled(callbacks.map(// NOTE: wrapped in an async function to protect against synchronous exceptions
async (f)=>f()));
}
constructor(){
this.callbacks = [];
}
}
//# sourceMappingURL=async-callback-set.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/async-callback-set.ts"],"sourcesContent":["export class AsyncCallbackSet {\n private callbacks: (() => Promise<void>)[] = []\n\n public add(callback: () => Promise<void>) {\n this.callbacks.push(callback)\n }\n\n public async runAll(): Promise<void> {\n if (!this.callbacks.length) {\n return\n }\n const callbacks = this.callbacks\n this.callbacks = []\n await Promise.allSettled(\n callbacks.map(\n // NOTE: wrapped in an async function to protect against synchronous exceptions\n async (f) => f()\n )\n )\n }\n}\n"],"names":["AsyncCallbackSet","add","callback","callbacks","push","runAll","length","Promise","allSettled","map","f"],"mappings":"AAAA,OAAO,MAAMA;IAGJC,IAAIC,QAA6B,EAAE;QACxC,IAAI,CAACC,SAAS,CAACC,IAAI,CAACF;IACtB;IAEA,MAAaG,SAAwB;QACnC,IAAI,CAAC,IAAI,CAACF,SAAS,CAACG,MAAM,EAAE;YAC1B;QACF;QACA,MAAMH,YAAY,IAAI,CAACA,SAAS;QAChC,IAAI,CAACA,SAAS,GAAG,EAAE;QACnB,MAAMI,QAAQC,UAAU,CACtBL,UAAUM,GAAG,CACX,+EAA+E;QAC/E,OAAOC,IAAMA;IAGnB;;aAlBQP,YAAqC,EAAE;;AAmBjD"}

12
node_modules/next/dist/esm/server/lib/cache-control.js generated vendored Normal file
View File

@@ -0,0 +1,12 @@
import { CACHE_ONE_YEAR } from '../../lib/constants';
export function getCacheControlHeader({ revalidate, expire }) {
const swrHeader = typeof revalidate === 'number' && expire !== undefined && revalidate < expire ? `, stale-while-revalidate=${expire - revalidate}` : '';
if (revalidate === 0) {
return 'private, no-cache, no-store, max-age=0, must-revalidate';
} else if (typeof revalidate === 'number') {
return `s-maxage=${revalidate}${swrHeader}`;
}
return `s-maxage=${CACHE_ONE_YEAR}${swrHeader}`;
}
//# sourceMappingURL=cache-control.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/cache-control.ts"],"sourcesContent":["import { CACHE_ONE_YEAR } from '../../lib/constants'\n\n/**\n * The revalidate option used internally for pages. A value of `false` means\n * that the page should not be revalidated. A number means that the page\n * should be revalidated after the given number of seconds (this also includes\n * `1` which means to revalidate after 1 second). A value of `0` is not a valid\n * value for this option.\n */\nexport type Revalidate = number | false\n\nexport interface CacheControl {\n revalidate: Revalidate\n expire: number | undefined\n}\n\nexport function getCacheControlHeader({\n revalidate,\n expire,\n}: CacheControl): string {\n const swrHeader =\n typeof revalidate === 'number' &&\n expire !== undefined &&\n revalidate < expire\n ? `, stale-while-revalidate=${expire - revalidate}`\n : ''\n\n if (revalidate === 0) {\n return 'private, no-cache, no-store, max-age=0, must-revalidate'\n } else if (typeof revalidate === 'number') {\n return `s-maxage=${revalidate}${swrHeader}`\n }\n\n return `s-maxage=${CACHE_ONE_YEAR}${swrHeader}`\n}\n"],"names":["CACHE_ONE_YEAR","getCacheControlHeader","revalidate","expire","swrHeader","undefined"],"mappings":"AAAA,SAASA,cAAc,QAAQ,sBAAqB;AAgBpD,OAAO,SAASC,sBAAsB,EACpCC,UAAU,EACVC,MAAM,EACO;IACb,MAAMC,YACJ,OAAOF,eAAe,YACtBC,WAAWE,aACXH,aAAaC,SACT,CAAC,yBAAyB,EAAEA,SAASD,YAAY,GACjD;IAEN,IAAIA,eAAe,GAAG;QACpB,OAAO;IACT,OAAO,IAAI,OAAOA,eAAe,UAAU;QACzC,OAAO,CAAC,SAAS,EAAEA,aAAaE,WAAW;IAC7C;IAEA,OAAO,CAAC,SAAS,EAAEJ,iBAAiBI,WAAW;AACjD"}

View File

@@ -0,0 +1,78 @@
/*
This is the default "use cache" handler it defaults
to an in memory store
*/ import { LRUCache } from '../lru-cache';
import { isTagStale, tagsManifest } from '../incremental-cache/tags-manifest.external';
// LRU cache default to max 50 MB but in future track
const memoryCache = new LRUCache(50 * 1024 * 1024, (entry)=>entry.size);
const pendingSets = new Map();
const DefaultCacheHandler = {
async get (cacheKey, softTags) {
await pendingSets.get(cacheKey);
const privateEntry = memoryCache.get(cacheKey);
if (!privateEntry) {
return undefined;
}
const entry = privateEntry.entry;
if (performance.timeOrigin + performance.now() > entry.timestamp + entry.revalidate * 1000) {
// In memory caches should expire after revalidate time because it is unlikely that
// a new entry will be able to be used before it is dropped from the cache.
return undefined;
}
if (isTagStale(entry.tags, entry.timestamp) || isTagStale(softTags, entry.timestamp)) {
return undefined;
}
const [returnStream, newSaved] = entry.value.tee();
entry.value = newSaved;
return {
...entry,
value: returnStream
};
},
async set (cacheKey, pendingEntry) {
let resolvePending = ()=>{};
const pendingPromise = new Promise((resolve)=>{
resolvePending = resolve;
});
pendingSets.set(cacheKey, pendingPromise);
const entry = await pendingEntry;
let size = 0;
try {
const [value, clonedValue] = entry.value.tee();
entry.value = value;
const reader = clonedValue.getReader();
for(let chunk; !(chunk = await reader.read()).done;){
size += Buffer.from(chunk.value).byteLength;
}
memoryCache.set(cacheKey, {
entry,
isErrored: false,
errorRetryCount: 0,
size
});
} catch {
// TODO: store partial buffer with error after we retry 3 times
} finally{
resolvePending();
pendingSets.delete(cacheKey);
}
},
async expireTags (...tags) {
for (const tag of tags){
if (!tagsManifest.items[tag]) {
tagsManifest.items[tag] = {};
}
// TODO: use performance.now and update file-system-cache?
tagsManifest.items[tag].revalidatedAt = Date.now();
}
},
// This is only meant to invalidate in memory tags
// not meant to be propagated like expireTags would
// in multi-instance scenario
async receiveExpiredTags (...tags) {
return this.expireTags(...tags);
}
};
export default DefaultCacheHandler;
//# sourceMappingURL=default.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
// In-memory caches are fragile and should not use stale-while-revalidate
// semantics on the caches because it's not worth warming up an entry that's
// likely going to get evicted before we get to use it anyway. However,
// we also don't want to reuse a stale entry for too long so stale entries
// should be considered expired/missing in such CacheHandlers.
// This is the entry we store
export { };
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/cache-handlers/types.ts"],"sourcesContent":["// In-memory caches are fragile and should not use stale-while-revalidate\n// semantics on the caches because it's not worth warming up an entry that's\n// likely going to get evicted before we get to use it anyway. However,\n// we also don't want to reuse a stale entry for too long so stale entries\n// should be considered expired/missing in such CacheHandlers.\n\n// This is the entry we store\nexport interface CacheEntry {\n /**\n * The ReadableStream can error and only have partial\n * data so any cache handlers need to handle this case\n * and decide to keep the partial cache around or not\n */\n value: ReadableStream<Uint8Array>\n\n /**\n * The tags configured for the entry excluding soft tags\n */\n tags: string[]\n\n /**\n * This is for the client not used to calculate cache entry expiration\n */\n stale: number\n\n /**\n * When the cache entry was created\n */\n timestamp: number\n\n /**\n * How long the entry can last (should be longer than revalidate)\n */\n expire: number\n\n /**\n * How long until the entry should revalidate\n */\n revalidate: number\n}\n\nexport interface CacheHandler {\n get(cacheKey: string, softTags: string[]): Promise<undefined | CacheEntry>\n\n set(cacheKey: string, entry: Promise<CacheEntry>): Promise<void>\n\n // This is called when expireTags('') is called\n // and should update tags manifest accordingly\n expireTags(...tags: string[]): Promise<void>\n\n // This is called when an action request sends\n // NEXT_CACHE_REVALIDATED_TAGS_HEADER and tells\n // us these tags are expired and the manifest\n // should be updated this differs since in a multi\n // instance environment you don't propagate these\n // as they are request specific\n receiveExpiredTags(...tags: string[]): Promise<void>\n}\n"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,4EAA4E;AAC5E,uEAAuE;AACvE,0EAA0E;AAC1E,8DAA8D;AAE9D,6BAA6B;AAmC7B,WAgBC"}

View File

@@ -0,0 +1,44 @@
/**
* Clones a response by teeing the body so we can return two independent
* ReadableStreams from it. This avoids the bug in the undici library around
* response cloning.
*
* After cloning, the original response's body will be consumed and closed.
*
* @see https://github.com/vercel/next.js/pull/73274
*
* @param original - The original response to clone.
* @returns A tuple containing two independent clones of the original response.
*/ export function cloneResponse(original) {
// If the response has no body, then we can just return the original response
// twice because it's immutable.
if (!original.body) {
return [
original,
original
];
}
const [body1, body2] = original.body.tee();
const cloned1 = new Response(body1, {
status: original.status,
statusText: original.statusText,
headers: original.headers
});
Object.defineProperty(cloned1, 'url', {
value: original.url
});
const cloned2 = new Response(body2, {
status: original.status,
statusText: original.statusText,
headers: original.headers
});
Object.defineProperty(cloned2, 'url', {
value: original.url
});
return [
cloned1,
cloned2
];
}
//# sourceMappingURL=clone-response.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/clone-response.ts"],"sourcesContent":["/**\n * Clones a response by teeing the body so we can return two independent\n * ReadableStreams from it. This avoids the bug in the undici library around\n * response cloning.\n *\n * After cloning, the original response's body will be consumed and closed.\n *\n * @see https://github.com/vercel/next.js/pull/73274\n *\n * @param original - The original response to clone.\n * @returns A tuple containing two independent clones of the original response.\n */\nexport function cloneResponse(original: Response): [Response, Response] {\n // If the response has no body, then we can just return the original response\n // twice because it's immutable.\n if (!original.body) {\n return [original, original]\n }\n\n const [body1, body2] = original.body.tee()\n\n const cloned1 = new Response(body1, {\n status: original.status,\n statusText: original.statusText,\n headers: original.headers,\n })\n\n Object.defineProperty(cloned1, 'url', {\n value: original.url,\n })\n\n const cloned2 = new Response(body2, {\n status: original.status,\n statusText: original.statusText,\n headers: original.headers,\n })\n\n Object.defineProperty(cloned2, 'url', {\n value: original.url,\n })\n\n return [cloned1, cloned2]\n}\n"],"names":["cloneResponse","original","body","body1","body2","tee","cloned1","Response","status","statusText","headers","Object","defineProperty","value","url","cloned2"],"mappings":"AAAA;;;;;;;;;;;CAWC,GACD,OAAO,SAASA,cAAcC,QAAkB;IAC9C,6EAA6E;IAC7E,gCAAgC;IAChC,IAAI,CAACA,SAASC,IAAI,EAAE;QAClB,OAAO;YAACD;YAAUA;SAAS;IAC7B;IAEA,MAAM,CAACE,OAAOC,MAAM,GAAGH,SAASC,IAAI,CAACG,GAAG;IAExC,MAAMC,UAAU,IAAIC,SAASJ,OAAO;QAClCK,QAAQP,SAASO,MAAM;QACvBC,YAAYR,SAASQ,UAAU;QAC/BC,SAAST,SAASS,OAAO;IAC3B;IAEAC,OAAOC,cAAc,CAACN,SAAS,OAAO;QACpCO,OAAOZ,SAASa,GAAG;IACrB;IAEA,MAAMC,UAAU,IAAIR,SAASH,OAAO;QAClCI,QAAQP,SAASO,MAAM;QACvBC,YAAYR,SAASQ,UAAU;QAC/BC,SAAST,SAASS,OAAO;IAC3B;IAEAC,OAAOC,cAAc,CAACG,SAAS,OAAO;QACpCF,OAAOZ,SAASa,GAAG;IACrB;IAEA,OAAO;QAACR;QAASS;KAAQ;AAC3B"}

27
node_modules/next/dist/esm/server/lib/cpu-profile.js generated vendored Normal file
View File

@@ -0,0 +1,27 @@
const privateCpuProfileName = process.env.__NEXT_PRIVATE_CPU_PROFILE;
const isCpuProfileEnabled = process.env.NEXT_CPU_PROF || privateCpuProfileName;
if (isCpuProfileEnabled) {
const { Session } = require('inspector');
const fs = require('fs');
const session = new Session();
session.connect();
session.post('Profiler.enable');
session.post('Profiler.start');
function saveProfile() {
session.post('Profiler.stop', (error, param)=>{
if (error) {
console.error('Cannot generate CPU profiling:', error);
return;
}
// Write profile to disk
const filename = `${privateCpuProfileName || 'CPU.main'}.${Date.now()}.cpuprofile`;
fs.writeFileSync(`./${filename}`, JSON.stringify(param.profile));
process.exit(0);
});
}
process.on('SIGINT', saveProfile);
process.on('SIGTERM', saveProfile);
process.on('exit', saveProfile);
}
//# sourceMappingURL=cpu-profile.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/cpu-profile.ts"],"sourcesContent":["const privateCpuProfileName = process.env.__NEXT_PRIVATE_CPU_PROFILE\nconst isCpuProfileEnabled = process.env.NEXT_CPU_PROF || privateCpuProfileName\n\nif (isCpuProfileEnabled) {\n const { Session } = require('inspector') as typeof import('inspector')\n const fs = require('fs')\n\n const session = new Session()\n session.connect()\n\n session.post('Profiler.enable')\n session.post('Profiler.start')\n\n function saveProfile() {\n session.post('Profiler.stop', (error, param) => {\n if (error) {\n console.error('Cannot generate CPU profiling:', error)\n return\n }\n\n // Write profile to disk\n const filename = `${\n privateCpuProfileName || 'CPU.main'\n }.${Date.now()}.cpuprofile`\n fs.writeFileSync(`./${filename}`, JSON.stringify(param.profile))\n process.exit(0)\n })\n }\n process.on('SIGINT', saveProfile)\n process.on('SIGTERM', saveProfile)\n process.on('exit', saveProfile)\n}\n"],"names":["privateCpuProfileName","process","env","__NEXT_PRIVATE_CPU_PROFILE","isCpuProfileEnabled","NEXT_CPU_PROF","Session","require","fs","session","connect","post","saveProfile","error","param","console","filename","Date","now","writeFileSync","JSON","stringify","profile","exit","on"],"mappings":"AAAA,MAAMA,wBAAwBC,QAAQC,GAAG,CAACC,0BAA0B;AACpE,MAAMC,sBAAsBH,QAAQC,GAAG,CAACG,aAAa,IAAIL;AAEzD,IAAII,qBAAqB;IACvB,MAAM,EAAEE,OAAO,EAAE,GAAGC,QAAQ;IAC5B,MAAMC,KAAKD,QAAQ;IAEnB,MAAME,UAAU,IAAIH;IACpBG,QAAQC,OAAO;IAEfD,QAAQE,IAAI,CAAC;IACbF,QAAQE,IAAI,CAAC;IAEb,SAASC;QACPH,QAAQE,IAAI,CAAC,iBAAiB,CAACE,OAAOC;YACpC,IAAID,OAAO;gBACTE,QAAQF,KAAK,CAAC,kCAAkCA;gBAChD;YACF;YAEA,wBAAwB;YACxB,MAAMG,WAAW,GACfhB,yBAAyB,WAC1B,CAAC,EAAEiB,KAAKC,GAAG,GAAG,WAAW,CAAC;YAC3BV,GAAGW,aAAa,CAAC,CAAC,EAAE,EAAEH,UAAU,EAAEI,KAAKC,SAAS,CAACP,MAAMQ,OAAO;YAC9DrB,QAAQsB,IAAI,CAAC;QACf;IACF;IACAtB,QAAQuB,EAAE,CAAC,UAAUZ;IACrBX,QAAQuB,EAAE,CAAC,WAAWZ;IACtBX,QAAQuB,EAAE,CAAC,QAAQZ;AACrB"}

105
node_modules/next/dist/esm/server/lib/dedupe-fetch.js generated vendored Normal file
View File

@@ -0,0 +1,105 @@
/**
* Based on https://github.com/facebook/react/blob/d4e78c42a94be027b4dc7ed2659a5fddfbf9bd4e/packages/react/src/ReactFetch.js
*/ import * as React from 'react';
import { cloneResponse } from './clone-response';
import { InvariantError } from '../../shared/lib/invariant-error';
const simpleCacheKey = '["GET",[],null,"follow",null,null,null,null]' // generateCacheKey(new Request('https://blank'));
;
function generateCacheKey(request) {
// We pick the fields that goes into the key used to dedupe requests.
// We don't include the `cache` field, because we end up using whatever
// caching resulted from the first request.
// Notably we currently don't consider non-standard (or future) options.
// This might not be safe. TODO: warn for non-standard extensions differing.
// IF YOU CHANGE THIS UPDATE THE simpleCacheKey ABOVE.
return JSON.stringify([
request.method,
Array.from(request.headers.entries()),
request.mode,
request.redirect,
request.credentials,
request.referrer,
request.referrerPolicy,
request.integrity
]);
}
export function createDedupeFetch(originalFetch) {
const getCacheEntries = React.cache(// eslint-disable-next-line @typescript-eslint/no-unused-vars -- url is the cache key
(url)=>[]);
return function dedupeFetch(resource, options) {
if (options && options.signal) {
// If we're passed a signal, then we assume that
// someone else controls the lifetime of this object and opts out of
// caching. It's effectively the opt-out mechanism.
// Ideally we should be able to check this on the Request but
// it always gets initialized with its own signal so we don't
// know if it's supposed to override - unless we also override the
// Request constructor.
return originalFetch(resource, options);
}
// Normalize the Request
let url;
let cacheKey;
if (typeof resource === 'string' && !options) {
// Fast path.
cacheKey = simpleCacheKey;
url = resource;
} else {
// Normalize the request.
// if resource is not a string or a URL (its an instance of Request)
// then do not instantiate a new Request but instead
// reuse the request as to not disturb the body in the event it's a ReadableStream.
const request = typeof resource === 'string' || resource instanceof URL ? new Request(resource, options) : resource;
if (request.method !== 'GET' && request.method !== 'HEAD' || request.keepalive) {
// We currently don't dedupe requests that might have side-effects. Those
// have to be explicitly cached. We assume that the request doesn't have a
// body if it's GET or HEAD.
// keepalive gets treated the same as if you passed a custom cache signal.
return originalFetch(resource, options);
}
cacheKey = generateCacheKey(request);
url = request.url;
}
const cacheEntries = getCacheEntries(url);
for(let i = 0, j = cacheEntries.length; i < j; i += 1){
const [key, promise] = cacheEntries[i];
if (key === cacheKey) {
return promise.then(()=>{
const response = cacheEntries[i][2];
if (!response) throw Object.defineProperty(new InvariantError('No cached response'), "__NEXT_ERROR_CODE", {
value: "E579",
enumerable: false,
configurable: true
});
// We're cloning the response using this utility because there exists
// a bug in the undici library around response cloning. See the
// following pull request for more details:
// https://github.com/vercel/next.js/pull/73274
const [cloned1, cloned2] = cloneResponse(response);
cacheEntries[i][2] = cloned2;
return cloned1;
});
}
}
// We pass the original arguments here in case normalizing the Request
// doesn't include all the options in this environment.
const promise = originalFetch(resource, options);
const entry = [
cacheKey,
promise,
null
];
cacheEntries.push(entry);
return promise.then((response)=>{
// We're cloning the response using this utility because there exists
// a bug in the undici library around response cloning. See the
// following pull request for more details:
// https://github.com/vercel/next.js/pull/73274
const [cloned1, cloned2] = cloneResponse(response);
entry[2] = cloned2;
return cloned1;
});
};
}
//# sourceMappingURL=dedupe-fetch.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,77 @@
import { LRUCache } from './lru-cache';
import { createRequestResponseMocks } from './mock-request';
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../dev/hot-reloader-types';
/**
* The DevBundlerService provides an interface to perform tasks with the
* bundler while in development.
*/ export class DevBundlerService {
constructor(bundler, handler){
this.bundler = bundler;
this.handler = handler;
this.ensurePage = async (definition)=>{
// TODO: remove after ensure is pulled out of server
return await this.bundler.hotReloader.ensurePage(definition);
};
this.logErrorWithOriginalStack = this.bundler.logErrorWithOriginalStack.bind(this.bundler);
this.appIsrManifestInner = new LRUCache(8000, function length() {
return 16;
});
}
async getFallbackErrorComponents(url) {
await this.bundler.hotReloader.buildFallbackError();
// Build the error page to ensure the fallback is built too.
// TODO: See if this can be moved into hotReloader or removed.
await this.bundler.hotReloader.ensurePage({
page: '/_error',
clientOnly: false,
definition: undefined,
url
});
}
async getCompilationError(page) {
const errors = await this.bundler.hotReloader.getCompilationErrors(page);
if (!errors) return;
// Return the very first error we found.
return errors[0];
}
async revalidate({ urlPath, revalidateHeaders, opts: revalidateOpts }) {
const mocked = createRequestResponseMocks({
url: urlPath,
headers: revalidateHeaders
});
await this.handler(mocked.req, mocked.res);
await mocked.res.hasStreamed;
if (mocked.res.getHeader('x-nextjs-cache') !== 'REVALIDATED' && mocked.res.statusCode !== 200 && !(mocked.res.statusCode === 404 && revalidateOpts.unstable_onlyGenerated)) {
throw Object.defineProperty(new Error(`Invalid response ${mocked.res.statusCode}`), "__NEXT_ERROR_CODE", {
value: "E175",
enumerable: false,
configurable: true
});
}
return {};
}
get appIsrManifest() {
const serializableManifest = {};
for (const key of this.appIsrManifestInner.keys()){
serializableManifest[key] = this.appIsrManifestInner.get(key);
}
return serializableManifest;
}
setIsrStatus(key, value) {
var _this_bundler_hotReloader, _this_bundler;
if (value === null) {
this.appIsrManifestInner.remove(key);
} else {
this.appIsrManifestInner.set(key, value);
}
(_this_bundler = this.bundler) == null ? void 0 : (_this_bundler_hotReloader = _this_bundler.hotReloader) == null ? void 0 : _this_bundler_hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.ISR_MANIFEST,
data: this.appIsrManifest
});
}
close() {
this.bundler.hotReloader.close();
}
}
//# sourceMappingURL=dev-bundler-service.js.map

File diff suppressed because one or more lines are too long

34
node_modules/next/dist/esm/server/lib/etag.js generated vendored Normal file
View File

@@ -0,0 +1,34 @@
/**
* FNV-1a Hash implementation
* @author Travis Webb (tjwebb) <me@traviswebb.com>
*
* Ported from https://github.com/tjwebb/fnv-plus/blob/master/index.js
*
* Simplified, optimized and add modified for 52 bit, which provides a larger hash space
* and still making use of Javascript's 53-bit integer space.
*/ export const fnv1a52 = (str)=>{
const len = str.length;
let i = 0, t0 = 0, v0 = 0x2325, t1 = 0, v1 = 0x8422, t2 = 0, v2 = 0x9ce4, t3 = 0, v3 = 0xcbf2;
while(i < len){
v0 ^= str.charCodeAt(i++);
t0 = v0 * 435;
t1 = v1 * 435;
t2 = v2 * 435;
t3 = v3 * 435;
t2 += v0 << 8;
t3 += v1 << 8;
t1 += t0 >>> 16;
v0 = t0 & 65535;
t2 += t1 >>> 16;
v1 = t1 & 65535;
v3 = t3 + (t2 >>> 16) & 65535;
v2 = t2 & 65535;
}
return (v3 & 15) * 281474976710656 + v2 * 4294967296 + v1 * 65536 + (v0 ^ v3 >> 4);
};
export const generateETag = (payload, weak = false)=>{
const prefix = weak ? 'W/"' : '"';
return prefix + fnv1a52(payload).toString(36) + payload.length.toString(36) + '"';
};
//# sourceMappingURL=etag.js.map

1
node_modules/next/dist/esm/server/lib/etag.js.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/etag.ts"],"sourcesContent":["/**\n * FNV-1a Hash implementation\n * @author Travis Webb (tjwebb) <me@traviswebb.com>\n *\n * Ported from https://github.com/tjwebb/fnv-plus/blob/master/index.js\n *\n * Simplified, optimized and add modified for 52 bit, which provides a larger hash space\n * and still making use of Javascript's 53-bit integer space.\n */\nexport const fnv1a52 = (str: string) => {\n const len = str.length\n let i = 0,\n t0 = 0,\n v0 = 0x2325,\n t1 = 0,\n v1 = 0x8422,\n t2 = 0,\n v2 = 0x9ce4,\n t3 = 0,\n v3 = 0xcbf2\n\n while (i < len) {\n v0 ^= str.charCodeAt(i++)\n t0 = v0 * 435\n t1 = v1 * 435\n t2 = v2 * 435\n t3 = v3 * 435\n t2 += v0 << 8\n t3 += v1 << 8\n t1 += t0 >>> 16\n v0 = t0 & 65535\n t2 += t1 >>> 16\n v1 = t1 & 65535\n v3 = (t3 + (t2 >>> 16)) & 65535\n v2 = t2 & 65535\n }\n\n return (\n (v3 & 15) * 281474976710656 +\n v2 * 4294967296 +\n v1 * 65536 +\n (v0 ^ (v3 >> 4))\n )\n}\n\nexport const generateETag = (payload: string, weak = false) => {\n const prefix = weak ? 'W/\"' : '\"'\n return (\n prefix + fnv1a52(payload).toString(36) + payload.length.toString(36) + '\"'\n )\n}\n"],"names":["fnv1a52","str","len","length","i","t0","v0","t1","v1","t2","v2","t3","v3","charCodeAt","generateETag","payload","weak","prefix","toString"],"mappings":"AAAA;;;;;;;;CAQC,GACD,OAAO,MAAMA,UAAU,CAACC;IACtB,MAAMC,MAAMD,IAAIE,MAAM;IACtB,IAAIC,IAAI,GACNC,KAAK,GACLC,KAAK,QACLC,KAAK,GACLC,KAAK,QACLC,KAAK,GACLC,KAAK,QACLC,KAAK,GACLC,KAAK;IAEP,MAAOR,IAAIF,IAAK;QACdI,MAAML,IAAIY,UAAU,CAACT;QACrBC,KAAKC,KAAK;QACVC,KAAKC,KAAK;QACVC,KAAKC,KAAK;QACVC,KAAKC,KAAK;QACVH,MAAMH,MAAM;QACZK,MAAMH,MAAM;QACZD,MAAMF,OAAO;QACbC,KAAKD,KAAK;QACVI,MAAMF,OAAO;QACbC,KAAKD,KAAK;QACVK,KAAK,AAACD,KAAMF,CAAAA,OAAO,EAAC,IAAM;QAC1BC,KAAKD,KAAK;IACZ;IAEA,OACE,AAACG,CAAAA,KAAK,EAAC,IAAK,kBACZF,KAAK,aACLF,KAAK,QACJF,CAAAA,KAAMM,MAAM,CAAC;AAElB,EAAC;AAED,OAAO,MAAME,eAAe,CAACC,SAAiBC,OAAO,KAAK;IACxD,MAAMC,SAASD,OAAO,QAAQ;IAC9B,OACEC,SAASjB,QAAQe,SAASG,QAAQ,CAAC,MAAMH,QAAQZ,MAAM,CAACe,QAAQ,CAAC,MAAM;AAE3E,EAAC"}

View File

@@ -0,0 +1,39 @@
import { join } from 'node:path';
import { writeFile } from 'node:fs/promises';
export async function createEnvDefinitions({ distDir, loadedEnvFiles }) {
const envLines = [];
const seenKeys = new Set();
// env files are in order of priority
for (const { path, env } of loadedEnvFiles){
for(const key in env){
if (!seenKeys.has(key)) {
envLines.push(` /** Loaded from \`${path}\` */`);
envLines.push(` ${key}?: string`);
seenKeys.add(key);
}
}
}
const envStr = envLines.join('\n');
const definitionStr = `// Type definitions for Next.js environment variables
declare global {
namespace NodeJS {
interface ProcessEnv {
${envStr}
}
}
}
export {}`;
if (process.env.NODE_ENV === 'test') {
return definitionStr;
}
try {
// we expect the types directory to already exist
const envDtsPath = join(distDir, 'types', 'env.d.ts');
// do not await, this is not essential for further process
writeFile(envDtsPath, definitionStr, 'utf-8');
} catch (e) {
console.error('Failed to write env.d.ts:', e);
}
}
//# sourceMappingURL=create-env-definitions.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/experimental/create-env-definitions.ts"],"sourcesContent":["import type { LoadedEnvFiles } from '@next/env'\nimport { join } from 'node:path'\nimport { writeFile } from 'node:fs/promises'\n\nexport async function createEnvDefinitions({\n distDir,\n loadedEnvFiles,\n}: {\n distDir: string\n loadedEnvFiles: LoadedEnvFiles\n}) {\n const envLines = []\n const seenKeys = new Set()\n // env files are in order of priority\n for (const { path, env } of loadedEnvFiles) {\n for (const key in env) {\n if (!seenKeys.has(key)) {\n envLines.push(` /** Loaded from \\`${path}\\` */`)\n envLines.push(` ${key}?: string`)\n seenKeys.add(key)\n }\n }\n }\n const envStr = envLines.join('\\n')\n\n const definitionStr = `// Type definitions for Next.js environment variables\ndeclare global {\n namespace NodeJS {\n interface ProcessEnv {\n${envStr}\n }\n }\n}\nexport {}`\n\n if (process.env.NODE_ENV === 'test') {\n return definitionStr\n }\n\n try {\n // we expect the types directory to already exist\n const envDtsPath = join(distDir, 'types', 'env.d.ts')\n // do not await, this is not essential for further process\n writeFile(envDtsPath, definitionStr, 'utf-8')\n } catch (e) {\n console.error('Failed to write env.d.ts:', e)\n }\n}\n"],"names":["join","writeFile","createEnvDefinitions","distDir","loadedEnvFiles","envLines","seenKeys","Set","path","env","key","has","push","add","envStr","definitionStr","process","NODE_ENV","envDtsPath","e","console","error"],"mappings":"AACA,SAASA,IAAI,QAAQ,YAAW;AAChC,SAASC,SAAS,QAAQ,mBAAkB;AAE5C,OAAO,eAAeC,qBAAqB,EACzCC,OAAO,EACPC,cAAc,EAIf;IACC,MAAMC,WAAW,EAAE;IACnB,MAAMC,WAAW,IAAIC;IACrB,qCAAqC;IACrC,KAAK,MAAM,EAAEC,IAAI,EAAEC,GAAG,EAAE,IAAIL,eAAgB;QAC1C,IAAK,MAAMM,OAAOD,IAAK;YACrB,IAAI,CAACH,SAASK,GAAG,CAACD,MAAM;gBACtBL,SAASO,IAAI,CAAC,CAAC,wBAAwB,EAAEJ,KAAK,KAAK,CAAC;gBACpDH,SAASO,IAAI,CAAC,CAAC,MAAM,EAAEF,IAAI,SAAS,CAAC;gBACrCJ,SAASO,GAAG,CAACH;YACf;QACF;IACF;IACA,MAAMI,SAAST,SAASL,IAAI,CAAC;IAE7B,MAAMe,gBAAgB,CAAC;;;;AAIzB,EAAED,OAAO;;;;SAIA,CAAC;IAER,IAAIE,QAAQP,GAAG,CAACQ,QAAQ,KAAK,QAAQ;QACnC,OAAOF;IACT;IAEA,IAAI;QACF,iDAAiD;QACjD,MAAMG,aAAalB,KAAKG,SAAS,SAAS;QAC1C,0DAA0D;QAC1DF,UAAUiB,YAAYH,eAAe;IACvC,EAAE,OAAOI,GAAG;QACVC,QAAQC,KAAK,CAAC,6BAA6BF;IAC7C;AACF"}

View File

@@ -0,0 +1,43 @@
/**
* If set to `incremental`, only those leaf pages that export
* `experimental_ppr = true` will have partial prerendering enabled. If any
* page exports this value as `false` or does not export it at all will not
* have partial prerendering enabled. If set to a boolean, it the options for
* `experimental_ppr` will be ignored.
*/ /**
* Returns true if partial prerendering is enabled for the application. It does
* not tell you if a given route has PPR enabled, as that requires analysis of
* the route's configuration.
*
* @see {@link checkIsRoutePPREnabled} - for checking if a specific route has PPR enabled.
*/ export function checkIsAppPPREnabled(config) {
// If the config is undefined, partial prerendering is disabled.
if (typeof config === 'undefined') return false;
// If the config is a boolean, use it directly.
if (typeof config === 'boolean') return config;
// If the config is a string, it must be 'incremental' to enable partial
// prerendering.
if (config === 'incremental') return true;
return false;
}
/**
* Returns true if partial prerendering is supported for the current page with
* the provided app configuration. If the application doesn't have partial
* prerendering enabled, this function will always return false. If you want to
* check if the application has partial prerendering enabled
*
* @see {@link checkIsAppPPREnabled} for checking if the application has PPR enabled.
*/ export function checkIsRoutePPREnabled(config, appConfig) {
// If the config is undefined, partial prerendering is disabled.
if (typeof config === 'undefined') return false;
// If the config is a boolean, use it directly.
if (typeof config === 'boolean') return config;
// If the config is a string, it must be 'incremental' to enable partial
// prerendering.
if (config === 'incremental' && appConfig.experimental_ppr === true) {
return true;
}
return false;
}
//# sourceMappingURL=ppr.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/experimental/ppr.ts"],"sourcesContent":["/**\n * If set to `incremental`, only those leaf pages that export\n * `experimental_ppr = true` will have partial prerendering enabled. If any\n * page exports this value as `false` or does not export it at all will not\n * have partial prerendering enabled. If set to a boolean, it the options for\n * `experimental_ppr` will be ignored.\n */\n\nexport type ExperimentalPPRConfig = boolean | 'incremental'\n\n/**\n * Returns true if partial prerendering is enabled for the application. It does\n * not tell you if a given route has PPR enabled, as that requires analysis of\n * the route's configuration.\n *\n * @see {@link checkIsRoutePPREnabled} - for checking if a specific route has PPR enabled.\n */\nexport function checkIsAppPPREnabled(\n config: ExperimentalPPRConfig | undefined\n): boolean {\n // If the config is undefined, partial prerendering is disabled.\n if (typeof config === 'undefined') return false\n\n // If the config is a boolean, use it directly.\n if (typeof config === 'boolean') return config\n\n // If the config is a string, it must be 'incremental' to enable partial\n // prerendering.\n if (config === 'incremental') return true\n\n return false\n}\n\n/**\n * Returns true if partial prerendering is supported for the current page with\n * the provided app configuration. If the application doesn't have partial\n * prerendering enabled, this function will always return false. If you want to\n * check if the application has partial prerendering enabled\n *\n * @see {@link checkIsAppPPREnabled} for checking if the application has PPR enabled.\n */\nexport function checkIsRoutePPREnabled(\n config: ExperimentalPPRConfig | undefined,\n appConfig: {\n experimental_ppr?: boolean\n }\n): boolean {\n // If the config is undefined, partial prerendering is disabled.\n if (typeof config === 'undefined') return false\n\n // If the config is a boolean, use it directly.\n if (typeof config === 'boolean') return config\n\n // If the config is a string, it must be 'incremental' to enable partial\n // prerendering.\n if (config === 'incremental' && appConfig.experimental_ppr === true) {\n return true\n }\n\n return false\n}\n"],"names":["checkIsAppPPREnabled","config","checkIsRoutePPREnabled","appConfig","experimental_ppr"],"mappings":"AAAA;;;;;;CAMC,GAID;;;;;;CAMC,GACD,OAAO,SAASA,qBACdC,MAAyC;IAEzC,gEAAgE;IAChE,IAAI,OAAOA,WAAW,aAAa,OAAO;IAE1C,+CAA+C;IAC/C,IAAI,OAAOA,WAAW,WAAW,OAAOA;IAExC,wEAAwE;IACxE,gBAAgB;IAChB,IAAIA,WAAW,eAAe,OAAO;IAErC,OAAO;AACT;AAEA;;;;;;;CAOC,GACD,OAAO,SAASC,uBACdD,MAAyC,EACzCE,SAEC;IAED,gEAAgE;IAChE,IAAI,OAAOF,WAAW,aAAa,OAAO;IAE1C,+CAA+C;IAC/C,IAAI,OAAOA,WAAW,WAAW,OAAOA;IAExC,wEAAwE;IACxE,gBAAgB;IAChB,IAAIA,WAAW,iBAAiBE,UAAUC,gBAAgB,KAAK,MAAM;QACnE,OAAO;IACT;IAEA,OAAO;AACT"}

103
node_modules/next/dist/esm/server/lib/find-page-file.js generated vendored Normal file
View File

@@ -0,0 +1,103 @@
import { fileExists } from '../../lib/file-exists';
import { getPagePaths } from '../../shared/lib/page-path/get-page-paths';
import { nonNullable } from '../../lib/non-nullable';
import { join, sep, normalize } from 'path';
import { promises as fsPromises } from 'fs';
import { warn } from '../../build/output/log';
import { cyan } from '../../lib/picocolors';
import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route';
async function isTrueCasePagePath(pagePath, pagesDir) {
const pageSegments = normalize(pagePath).split(sep).filter(Boolean);
const segmentExistsPromises = pageSegments.map(async (segment, i)=>{
const segmentParentDir = join(pagesDir, ...pageSegments.slice(0, i));
const parentDirEntries = await fsPromises.readdir(segmentParentDir);
return parentDirEntries.includes(segment);
});
return (await Promise.all(segmentExistsPromises)).every(Boolean);
}
/**
* Finds a page file with the given parameters. If the page is duplicated with
* multiple extensions it will throw, otherwise it will return the *relative*
* path to the page file or null if it is not found.
*
* @param pagesDir Absolute path to the pages folder with trailing `/pages`.
* @param normalizedPagePath The page normalized (it will be denormalized).
* @param pageExtensions Array of page extensions.
*/ export async function findPageFile(pagesDir, normalizedPagePath, pageExtensions, isAppDir) {
const pagePaths = getPagePaths(normalizedPagePath, pageExtensions, isAppDir);
const [existingPath, ...others] = (await Promise.all(pagePaths.map(async (path)=>{
const filePath = join(pagesDir, path);
try {
return await fileExists(filePath) ? path : null;
} catch (err) {
var _err_code;
if (!(err == null ? void 0 : (_err_code = err.code) == null ? void 0 : _err_code.includes('ENOTDIR'))) throw err;
}
return null;
}))).filter(nonNullable);
if (!existingPath) {
return null;
}
if (!await isTrueCasePagePath(existingPath, pagesDir)) {
return null;
}
if (others.length > 0) {
warn(`Duplicate page detected. ${cyan(join('pages', existingPath))} and ${cyan(join('pages', others[0]))} both resolve to ${cyan(normalizedPagePath)}.`);
}
return existingPath;
}
/**
*
* createValidFileMatcher receives configured page extensions and return helpers to determine:
* `isLayoutsLeafPage`: if a file is a valid page file or routes file under app directory
* `isTrackedFiles`: if it's a tracked file for webpack watcher
*
*/ export function createValidFileMatcher(pageExtensions, appDirPath) {
const getExtensionRegexString = (extensions)=>`(?:${extensions.join('|')})`;
const validExtensionFileRegex = new RegExp('\\.' + getExtensionRegexString(pageExtensions) + '$');
const leafOnlyPageFileRegex = new RegExp(`(^(page|route)|[\\\\/](page|route))\\.${getExtensionRegexString(pageExtensions)}$`);
const rootNotFoundFileRegex = new RegExp(`^not-found\\.${getExtensionRegexString(pageExtensions)}$`);
/** TODO-METADATA: support other metadata routes
* regex for:
*
* /robots.txt|<ext>
* /sitemap.xml|<ext>
* /favicon.ico
* /manifest.json|<ext>
* <route>/icon.png|jpg|<ext>
* <route>/apple-touch-icon.png|jpg|<ext>
*
*/ /**
* Match the file if it's a metadata route file, static: if the file is a static metadata file.
* It needs to be a file which doesn't match the custom metadata routes e.g. `app/robots.txt/route.js`
*/ function isMetadataFile(filePath) {
const appDirRelativePath = appDirPath ? filePath.replace(appDirPath, '') : filePath;
return isMetadataRouteFile(appDirRelativePath, pageExtensions, true);
}
// Determine if the file is leaf node page file or route file under layouts,
// 'page.<extension>' | 'route.<extension>'
function isAppRouterPage(filePath) {
return leafOnlyPageFileRegex.test(filePath) || isMetadataFile(filePath);
}
function isPageFile(filePath) {
return validExtensionFileRegex.test(filePath) || isMetadataFile(filePath);
}
function isRootNotFound(filePath) {
if (!appDirPath) {
return false;
}
if (!filePath.startsWith(appDirPath + sep)) {
return false;
}
const rest = filePath.slice(appDirPath.length + 1);
return rootNotFoundFileRegex.test(rest);
}
return {
isPageFile,
isAppRouterPage,
isMetadataFile,
isRootNotFound
};
}
//# sourceMappingURL=find-page-file.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,11 @@
import { isIPv6 } from './is-ipv6';
/**
* Formats a hostname so that it is a valid host that can be fetched by wrapping
* IPv6 hosts with brackets.
* @param hostname
* @returns
*/ export function formatHostname(hostname) {
return isIPv6(hostname) ? `[${hostname}]` : hostname;
}
//# sourceMappingURL=format-hostname.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/format-hostname.ts"],"sourcesContent":["import { isIPv6 } from './is-ipv6'\n\n/**\n * Formats a hostname so that it is a valid host that can be fetched by wrapping\n * IPv6 hosts with brackets.\n * @param hostname\n * @returns\n */\nexport function formatHostname(hostname: string): string {\n return isIPv6(hostname) ? `[${hostname}]` : hostname\n}\n"],"names":["isIPv6","formatHostname","hostname"],"mappings":"AAAA,SAASA,MAAM,QAAQ,YAAW;AAElC;;;;;CAKC,GACD,OAAO,SAASC,eAAeC,QAAgB;IAC7C,OAAOF,OAAOE,YAAY,CAAC,CAAC,EAAEA,SAAS,CAAC,CAAC,GAAGA;AAC9C"}

127
node_modules/next/dist/esm/server/lib/i18n-provider.js generated vendored Normal file
View File

@@ -0,0 +1,127 @@
import { getRequestMeta } from '../request-meta';
/**
* The I18NProvider is used to match locale aware routes, detect the locale from
* the pathname and hostname and normalize the pathname by removing the locale
* prefix.
*/ export class I18NProvider {
constructor(config){
var _config_domains;
this.config = config;
if (!config.locales.length) {
throw Object.defineProperty(new Error('Invariant: No locales provided'), "__NEXT_ERROR_CODE", {
value: "E510",
enumerable: false,
configurable: true
});
}
this.lowerCaseLocales = config.locales.map((locale)=>locale.toLowerCase());
this.lowerCaseDomains = (_config_domains = config.domains) == null ? void 0 : _config_domains.map((domainLocale)=>{
var _domainLocale_locales;
const domain = domainLocale.domain.toLowerCase();
return {
defaultLocale: domainLocale.defaultLocale.toLowerCase(),
hostname: domain.split(':', 1)[0],
domain,
locales: (_domainLocale_locales = domainLocale.locales) == null ? void 0 : _domainLocale_locales.map((locale)=>locale.toLowerCase()),
http: domainLocale.http
};
});
}
/**
* Detects the domain locale from the hostname and the detected locale if
* provided.
*
* @param hostname The hostname to detect the domain locale from, this must be lowercased.
* @param detectedLocale The detected locale to use if the hostname does not match.
* @returns The domain locale if found, `undefined` otherwise.
*/ detectDomainLocale(hostname, detectedLocale) {
if (!hostname || !this.lowerCaseDomains || !this.config.domains) return;
if (detectedLocale) detectedLocale = detectedLocale.toLowerCase();
for(let i = 0; i < this.lowerCaseDomains.length; i++){
var // Configuration validation ensures that the locale is not repeated in
// other domains locales.
_domainLocale_locales;
const domainLocale = this.lowerCaseDomains[i];
if (// We assume that the hostname is already lowercased.
domainLocale.hostname === hostname || ((_domainLocale_locales = domainLocale.locales) == null ? void 0 : _domainLocale_locales.some((locale)=>locale === detectedLocale))) {
return this.config.domains[i];
}
}
return;
}
/**
* Pulls the pre-computed locale and inference results from the query
* object.
*
* @param req the request object
* @param pathname the pathname that could contain a locale prefix
* @returns the locale analysis result
*/ fromRequest(req, pathname) {
const detectedLocale = getRequestMeta(req, 'locale');
// If a locale was detected on the query, analyze the pathname to ensure
// that the locale matches.
if (detectedLocale) {
const analysis = this.analyze(pathname);
// If the analysis contained a locale we should validate it against the
// query and strip it from the pathname.
if (analysis.detectedLocale) {
if (analysis.detectedLocale !== detectedLocale) {
throw Object.defineProperty(new Error(`Invariant: The detected locale does not match the locale in the query. Expected to find '${detectedLocale}' in '${pathname}' but found '${analysis.detectedLocale}'}`), "__NEXT_ERROR_CODE", {
value: "E517",
enumerable: false,
configurable: true
});
}
pathname = analysis.pathname;
}
}
return {
pathname,
detectedLocale,
inferredFromDefault: getRequestMeta(req, 'localeInferredFromDefault') ?? false
};
}
/**
* Analyzes the pathname for a locale and returns the pathname without it.
*
* @param pathname The pathname that could contain a locale prefix.
* @param options The options to use when matching the locale.
* @returns The matched locale and the pathname without the locale prefix
* (if any).
*/ analyze(pathname, options = {}) {
let detectedLocale = options.defaultLocale;
// By default, we assume that the default locale was inferred if there was
// no detected locale.
let inferredFromDefault = typeof detectedLocale === 'string';
// The first segment will be empty, because it has a leading `/`. If
// there is no further segment, there is no locale (or it's the default).
const segments = pathname.split('/', 2);
if (!segments[1]) return {
detectedLocale,
pathname,
inferredFromDefault
};
// The second segment will contain the locale part if any.
const segment = segments[1].toLowerCase();
// See if the segment matches one of the locales. If it doesn't, there is
// no locale (or it's the default).
const index = this.lowerCaseLocales.indexOf(segment);
if (index < 0) return {
detectedLocale,
pathname,
inferredFromDefault
};
// Return the case-sensitive locale.
detectedLocale = this.config.locales[index];
inferredFromDefault = false;
// Remove the `/${locale}` part of the pathname.
pathname = pathname.slice(detectedLocale.length + 1) || '/';
return {
detectedLocale,
pathname,
inferredFromDefault
};
}
}
//# sourceMappingURL=i18n-provider.js.map

File diff suppressed because one or more lines are too long

42
node_modules/next/dist/esm/server/lib/implicit-tags.js generated vendored Normal file
View File

@@ -0,0 +1,42 @@
import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants';
const getDerivedTags = (pathname)=>{
const derivedTags = [
`/layout`
];
// we automatically add the current path segments as tags
// for revalidatePath handling
if (pathname.startsWith('/')) {
const pathnameParts = pathname.split('/');
for(let i = 1; i < pathnameParts.length + 1; i++){
let curPathname = pathnameParts.slice(0, i).join('/');
if (curPathname) {
// all derived tags other than the page are layout tags
if (!curPathname.endsWith('/page') && !curPathname.endsWith('/route')) {
curPathname = `${curPathname}${!curPathname.endsWith('/') ? '/' : ''}layout`;
}
derivedTags.push(curPathname);
}
}
}
return derivedTags;
};
export function getImplicitTags(page, url, fallbackRouteParams) {
// TODO: Cache the result
const newTags = [];
const hasFallbackRouteParams = fallbackRouteParams && fallbackRouteParams.size > 0;
// Add the derived tags from the page.
const derivedTags = getDerivedTags(page);
for (let tag of derivedTags){
tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${tag}`;
newTags.push(tag);
}
// Add the tags from the pathname. If the route has unknown params, we don't
// want to add the pathname as a tag, as it will be invalid.
if (url.pathname && !hasFallbackRouteParams) {
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${url.pathname}`;
newTags.push(tag);
}
return newTags;
}
//# sourceMappingURL=implicit-tags.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/implicit-tags.ts"],"sourcesContent":["import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants'\nimport type { FallbackRouteParams } from '../request/fallback-params'\n\nconst getDerivedTags = (pathname: string): string[] => {\n const derivedTags: string[] = [`/layout`]\n\n // we automatically add the current path segments as tags\n // for revalidatePath handling\n if (pathname.startsWith('/')) {\n const pathnameParts = pathname.split('/')\n\n for (let i = 1; i < pathnameParts.length + 1; i++) {\n let curPathname = pathnameParts.slice(0, i).join('/')\n\n if (curPathname) {\n // all derived tags other than the page are layout tags\n if (!curPathname.endsWith('/page') && !curPathname.endsWith('/route')) {\n curPathname = `${curPathname}${\n !curPathname.endsWith('/') ? '/' : ''\n }layout`\n }\n derivedTags.push(curPathname)\n }\n }\n }\n return derivedTags\n}\n\nexport function getImplicitTags(\n page: string,\n url: {\n pathname: string\n search?: string\n },\n fallbackRouteParams: null | FallbackRouteParams\n) {\n // TODO: Cache the result\n const newTags: string[] = []\n const hasFallbackRouteParams =\n fallbackRouteParams && fallbackRouteParams.size > 0\n\n // Add the derived tags from the page.\n const derivedTags = getDerivedTags(page)\n for (let tag of derivedTags) {\n tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${tag}`\n newTags.push(tag)\n }\n\n // Add the tags from the pathname. If the route has unknown params, we don't\n // want to add the pathname as a tag, as it will be invalid.\n if (url.pathname && !hasFallbackRouteParams) {\n const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${url.pathname}`\n newTags.push(tag)\n }\n\n return newTags\n}\n"],"names":["NEXT_CACHE_IMPLICIT_TAG_ID","getDerivedTags","pathname","derivedTags","startsWith","pathnameParts","split","i","length","curPathname","slice","join","endsWith","push","getImplicitTags","page","url","fallbackRouteParams","newTags","hasFallbackRouteParams","size","tag"],"mappings":"AAAA,SAASA,0BAA0B,QAAQ,sBAAqB;AAGhE,MAAMC,iBAAiB,CAACC;IACtB,MAAMC,cAAwB;QAAC,CAAC,OAAO,CAAC;KAAC;IAEzC,yDAAyD;IACzD,8BAA8B;IAC9B,IAAID,SAASE,UAAU,CAAC,MAAM;QAC5B,MAAMC,gBAAgBH,SAASI,KAAK,CAAC;QAErC,IAAK,IAAIC,IAAI,GAAGA,IAAIF,cAAcG,MAAM,GAAG,GAAGD,IAAK;YACjD,IAAIE,cAAcJ,cAAcK,KAAK,CAAC,GAAGH,GAAGI,IAAI,CAAC;YAEjD,IAAIF,aAAa;gBACf,uDAAuD;gBACvD,IAAI,CAACA,YAAYG,QAAQ,CAAC,YAAY,CAACH,YAAYG,QAAQ,CAAC,WAAW;oBACrEH,cAAc,GAAGA,cACf,CAACA,YAAYG,QAAQ,CAAC,OAAO,MAAM,GACpC,MAAM,CAAC;gBACV;gBACAT,YAAYU,IAAI,CAACJ;YACnB;QACF;IACF;IACA,OAAON;AACT;AAEA,OAAO,SAASW,gBACdC,IAAY,EACZC,GAGC,EACDC,mBAA+C;IAE/C,yBAAyB;IACzB,MAAMC,UAAoB,EAAE;IAC5B,MAAMC,yBACJF,uBAAuBA,oBAAoBG,IAAI,GAAG;IAEpD,sCAAsC;IACtC,MAAMjB,cAAcF,eAAec;IACnC,KAAK,IAAIM,OAAOlB,YAAa;QAC3BkB,MAAM,GAAGrB,6BAA6BqB,KAAK;QAC3CH,QAAQL,IAAI,CAACQ;IACf;IAEA,4EAA4E;IAC5E,4DAA4D;IAC5D,IAAIL,IAAId,QAAQ,IAAI,CAACiB,wBAAwB;QAC3C,MAAME,MAAM,GAAGrB,6BAA6BgB,IAAId,QAAQ,EAAE;QAC1DgB,QAAQL,IAAI,CAACQ;IACf;IAEA,OAAOH;AACT"}

View File

@@ -0,0 +1,323 @@
import { CachedRouteKind, IncrementalCacheKind } from '../../response-cache';
import { LRUCache } from '../lru-cache';
import path from '../../../shared/lib/isomorphic/path';
import { NEXT_CACHE_TAGS_HEADER, NEXT_DATA_SUFFIX, NEXT_META_SUFFIX, RSC_PREFETCH_SUFFIX, RSC_SEGMENT_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX, RSC_SUFFIX } from '../../../lib/constants';
import { tagsManifest } from './tags-manifest.external';
import { MultiFileWriter } from '../../../lib/multi-file-writer';
let memoryCache;
export default class FileSystemCache {
constructor(ctx){
this.fs = ctx.fs;
this.flushToDisk = ctx.flushToDisk;
this.serverDistDir = ctx.serverDistDir;
this.revalidatedTags = ctx.revalidatedTags;
this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE;
if (ctx.maxMemoryCacheSize) {
if (!memoryCache) {
if (this.debug) {
console.log('using memory store for fetch cache');
}
memoryCache = new LRUCache(ctx.maxMemoryCacheSize, function length({ value }) {
var _JSON_stringify;
if (!value) {
return 25;
} else if (value.kind === CachedRouteKind.REDIRECT) {
return JSON.stringify(value.props).length;
} else if (value.kind === CachedRouteKind.IMAGE) {
throw Object.defineProperty(new Error('invariant image should not be incremental-cache'), "__NEXT_ERROR_CODE", {
value: "E501",
enumerable: false,
configurable: true
});
} else if (value.kind === CachedRouteKind.FETCH) {
return JSON.stringify(value.data || '').length;
} else if (value.kind === CachedRouteKind.APP_ROUTE) {
return value.body.length;
}
// rough estimate of size of cache value
return value.html.length + (((_JSON_stringify = JSON.stringify(value.kind === CachedRouteKind.APP_PAGE ? value.rscData : value.pageData)) == null ? void 0 : _JSON_stringify.length) || 0);
});
}
} else if (this.debug) {
console.log('not using memory store for fetch cache');
}
}
resetRequestCache() {}
async revalidateTag(...args) {
let [tags] = args;
tags = typeof tags === 'string' ? [
tags
] : tags;
if (this.debug) {
console.log('revalidateTag', tags);
}
if (tags.length === 0) {
return;
}
for (const tag of tags){
const data = tagsManifest.items[tag] || {};
data.revalidatedAt = Date.now();
tagsManifest.items[tag] = data;
}
}
async get(...args) {
var _data_value, _data_value1, _data_value2;
const [key, ctx] = args;
const { kind } = ctx;
let data = memoryCache == null ? void 0 : memoryCache.get(key);
if (this.debug) {
if (kind === IncrementalCacheKind.FETCH) {
console.log('get', key, ctx.tags, kind, !!data);
} else {
console.log('get', key, kind, !!data);
}
}
// let's check the disk for seed data
if (!data && process.env.NEXT_RUNTIME !== 'edge') {
if (kind === IncrementalCacheKind.APP_ROUTE) {
try {
const filePath = this.getFilePath(`${key}.body`, IncrementalCacheKind.APP_ROUTE);
const fileData = await this.fs.readFile(filePath);
const { mtime } = await this.fs.stat(filePath);
const meta = JSON.parse(await this.fs.readFile(filePath.replace(/\.body$/, NEXT_META_SUFFIX), 'utf8'));
const cacheEntry = {
lastModified: mtime.getTime(),
value: {
kind: CachedRouteKind.APP_ROUTE,
body: fileData,
headers: meta.headers,
status: meta.status
}
};
return cacheEntry;
} catch {
return null;
}
}
try {
const filePath = this.getFilePath(kind === IncrementalCacheKind.FETCH ? key : `${key}.html`, kind);
const fileData = await this.fs.readFile(filePath, 'utf8');
const { mtime } = await this.fs.stat(filePath);
if (kind === IncrementalCacheKind.FETCH) {
var _data_value3;
const { tags, fetchIdx, fetchUrl } = ctx;
if (!this.flushToDisk) return null;
const lastModified = mtime.getTime();
const parsedData = JSON.parse(fileData);
data = {
lastModified,
value: parsedData
};
if (((_data_value3 = data.value) == null ? void 0 : _data_value3.kind) === CachedRouteKind.FETCH) {
var _data_value4;
const storedTags = (_data_value4 = data.value) == null ? void 0 : _data_value4.tags;
// update stored tags if a new one is being added
// TODO: remove this when we can send the tags
// via header on GET same as SET
if (!(tags == null ? void 0 : tags.every((tag)=>storedTags == null ? void 0 : storedTags.includes(tag)))) {
if (this.debug) {
console.log('tags vs storedTags mismatch', tags, storedTags);
}
await this.set(key, data.value, {
fetchCache: true,
tags,
fetchIdx,
fetchUrl
});
}
}
} else if (kind === IncrementalCacheKind.APP_PAGE) {
// We try to load the metadata file, but if it fails, we don't
// error. We also don't load it if this is a fallback.
let meta;
try {
meta = JSON.parse(await this.fs.readFile(filePath.replace(/\.html$/, NEXT_META_SUFFIX), 'utf8'));
} catch {}
let maybeSegmentData;
if (meta == null ? void 0 : meta.segmentPaths) {
// Collect all the segment data for this page.
// TODO: To optimize file system reads, we should consider creating
// separate cache entries for each segment, rather than storing them
// all on the page's entry. Though the behavior is
// identical regardless.
const segmentData = new Map();
maybeSegmentData = segmentData;
const segmentsDir = key + RSC_SEGMENTS_DIR_SUFFIX;
await Promise.all(meta.segmentPaths.map(async (segmentPath)=>{
const segmentDataFilePath = this.getFilePath(segmentsDir + segmentPath + RSC_SEGMENT_SUFFIX, IncrementalCacheKind.APP_PAGE);
try {
segmentData.set(segmentPath, await this.fs.readFile(segmentDataFilePath));
} catch {
// This shouldn't happen, but if for some reason we fail to
// load a segment from the filesystem, treat it the same as if
// the segment is dynamic and does not have a prefetch.
}
}));
}
let rscData;
if (!ctx.isFallback) {
rscData = await this.fs.readFile(this.getFilePath(`${key}${ctx.isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`, IncrementalCacheKind.APP_PAGE));
}
data = {
lastModified: mtime.getTime(),
value: {
kind: CachedRouteKind.APP_PAGE,
html: fileData,
rscData,
postponed: meta == null ? void 0 : meta.postponed,
headers: meta == null ? void 0 : meta.headers,
status: meta == null ? void 0 : meta.status,
segmentData: maybeSegmentData
}
};
} else if (kind === IncrementalCacheKind.PAGES) {
let meta;
let pageData = {};
if (!ctx.isFallback) {
pageData = JSON.parse(await this.fs.readFile(this.getFilePath(`${key}${NEXT_DATA_SUFFIX}`, IncrementalCacheKind.PAGES), 'utf8'));
}
data = {
lastModified: mtime.getTime(),
value: {
kind: CachedRouteKind.PAGES,
html: fileData,
pageData,
headers: meta == null ? void 0 : meta.headers,
status: meta == null ? void 0 : meta.status
}
};
} else {
throw Object.defineProperty(new Error(`Invariant: Unexpected route kind ${kind} in file system cache.`), "__NEXT_ERROR_CODE", {
value: "E445",
enumerable: false,
configurable: true
});
}
if (data) {
memoryCache == null ? void 0 : memoryCache.set(key, data);
}
} catch {
return null;
}
}
if ((data == null ? void 0 : (_data_value = data.value) == null ? void 0 : _data_value.kind) === CachedRouteKind.APP_PAGE || (data == null ? void 0 : (_data_value1 = data.value) == null ? void 0 : _data_value1.kind) === CachedRouteKind.PAGES) {
var _data_value_headers;
let cacheTags;
const tagsHeader = (_data_value_headers = data.value.headers) == null ? void 0 : _data_value_headers[NEXT_CACHE_TAGS_HEADER];
if (typeof tagsHeader === 'string') {
cacheTags = tagsHeader.split(',');
}
if (cacheTags == null ? void 0 : cacheTags.length) {
const isStale = cacheTags.some((tag)=>{
var _tagsManifest_items_tag;
return (tagsManifest == null ? void 0 : (_tagsManifest_items_tag = tagsManifest.items[tag]) == null ? void 0 : _tagsManifest_items_tag.revalidatedAt) && (tagsManifest == null ? void 0 : tagsManifest.items[tag].revalidatedAt) >= ((data == null ? void 0 : data.lastModified) || Date.now());
});
// we trigger a blocking validation if an ISR page
// had a tag revalidated, if we want to be a background
// revalidation instead we return data.lastModified = -1
if (isStale) {
return null;
}
}
} else if ((data == null ? void 0 : (_data_value2 = data.value) == null ? void 0 : _data_value2.kind) === CachedRouteKind.FETCH) {
const combinedTags = ctx.kind === IncrementalCacheKind.FETCH ? [
...ctx.tags || [],
...ctx.softTags || []
] : [];
const wasRevalidated = combinedTags.some((tag)=>{
var _tagsManifest_items_tag;
if (this.revalidatedTags.includes(tag)) {
return true;
}
return (tagsManifest == null ? void 0 : (_tagsManifest_items_tag = tagsManifest.items[tag]) == null ? void 0 : _tagsManifest_items_tag.revalidatedAt) && (tagsManifest == null ? void 0 : tagsManifest.items[tag].revalidatedAt) >= ((data == null ? void 0 : data.lastModified) || Date.now());
});
// When revalidate tag is called we don't return
// stale data so it's updated right away
if (wasRevalidated) {
data = undefined;
}
}
return data ?? null;
}
async set(key, data, ctx) {
memoryCache == null ? void 0 : memoryCache.set(key, {
value: data,
lastModified: Date.now()
});
if (this.debug) {
console.log('set', key);
}
if (!this.flushToDisk || !data) return;
// Create a new writer that will prepare to write all the files to disk
// after their containing directory is created.
const writer = new MultiFileWriter(this.fs);
if (data.kind === CachedRouteKind.APP_ROUTE) {
const filePath = this.getFilePath(`${key}.body`, IncrementalCacheKind.APP_ROUTE);
writer.append(filePath, data.body);
const meta = {
headers: data.headers,
status: data.status,
postponed: undefined,
segmentPaths: undefined
};
writer.append(filePath.replace(/\.body$/, NEXT_META_SUFFIX), JSON.stringify(meta, null, 2));
} else if (data.kind === CachedRouteKind.PAGES || data.kind === CachedRouteKind.APP_PAGE) {
const isAppPath = data.kind === CachedRouteKind.APP_PAGE;
const htmlPath = this.getFilePath(`${key}.html`, isAppPath ? IncrementalCacheKind.APP_PAGE : IncrementalCacheKind.PAGES);
writer.append(htmlPath, data.html);
// Fallbacks don't generate a data file.
if (!ctx.fetchCache && !ctx.isFallback) {
writer.append(this.getFilePath(`${key}${isAppPath ? ctx.isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX : NEXT_DATA_SUFFIX}`, isAppPath ? IncrementalCacheKind.APP_PAGE : IncrementalCacheKind.PAGES), isAppPath ? data.rscData : JSON.stringify(data.pageData));
}
if ((data == null ? void 0 : data.kind) === CachedRouteKind.APP_PAGE) {
let segmentPaths;
if (data.segmentData) {
segmentPaths = [];
const segmentsDir = htmlPath.replace(/\.html$/, RSC_SEGMENTS_DIR_SUFFIX);
for (const [segmentPath, buffer] of data.segmentData){
segmentPaths.push(segmentPath);
const segmentDataFilePath = segmentsDir + segmentPath + RSC_SEGMENT_SUFFIX;
writer.append(segmentDataFilePath, buffer);
}
}
const meta = {
headers: data.headers,
status: data.status,
postponed: data.postponed,
segmentPaths
};
writer.append(htmlPath.replace(/\.html$/, NEXT_META_SUFFIX), JSON.stringify(meta));
}
} else if (data.kind === CachedRouteKind.FETCH) {
const filePath = this.getFilePath(key, IncrementalCacheKind.FETCH);
writer.append(filePath, JSON.stringify({
...data,
tags: ctx.fetchCache ? ctx.tags : []
}));
}
// Wait for all FS operations to complete.
await writer.wait();
}
getFilePath(pathname, kind) {
switch(kind){
case IncrementalCacheKind.FETCH:
// we store in .next/cache/fetch-cache so it can be persisted
// across deploys
return path.join(this.serverDistDir, '..', 'cache', 'fetch-cache', pathname);
case IncrementalCacheKind.PAGES:
return path.join(this.serverDistDir, 'pages', pathname);
case IncrementalCacheKind.IMAGE:
case IncrementalCacheKind.APP_PAGE:
case IncrementalCacheKind.APP_ROUTE:
return path.join(this.serverDistDir, 'app', pathname);
default:
throw Object.defineProperty(new Error(`Unexpected file path kind: ${kind}`), "__NEXT_ERROR_CODE", {
value: "E479",
enumerable: false,
configurable: true
});
}
}
}
//# sourceMappingURL=file-system-cache.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,385 @@
import { IncrementalCacheKind, CachedRouteKind } from '../../response-cache';
import FileSystemCache from './file-system-cache';
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path';
import { CACHE_ONE_YEAR, NEXT_CACHE_REVALIDATED_TAGS_HEADER, NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER, PRERENDER_REVALIDATE_HEADER } from '../../../lib/constants';
import { toRoute } from '../to-route';
import { SharedCacheControls } from './shared-cache-controls';
import { workUnitAsyncStorageInstance } from '../../app-render/work-unit-async-storage-instance';
import { getPrerenderResumeDataCache, getRenderResumeDataCache } from '../../app-render/work-unit-async-storage.external';
import { getCacheHandlers } from '../../use-cache/handlers';
import { InvariantError } from '../../../shared/lib/invariant-error';
export class CacheHandler {
// eslint-disable-next-line
constructor(_ctx){}
async get(_cacheKey, _ctx) {
return {};
}
async set(_cacheKey, _data, _ctx) {}
async revalidateTag(..._args) {}
resetRequestCache() {}
}
export class IncrementalCache {
constructor({ fs, dev, flushToDisk, minimalMode, serverDistDir, requestHeaders, requestProtocol, maxMemoryCacheSize, getPrerenderManifest, fetchCacheKeyPrefix, CurCacheHandler, allowedRevalidateHeaderKeys }){
var _this_prerenderManifest_preview, _this_prerenderManifest, _this_prerenderManifest_preview1, _this_prerenderManifest1;
this.locks = new Map();
const debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE;
this.hasCustomCacheHandler = Boolean(CurCacheHandler);
const cacheHandlersSymbol = Symbol.for('@next/cache-handlers');
const _globalThis = globalThis;
if (!CurCacheHandler) {
// if we have a global cache handler available leverage it
const globalCacheHandler = _globalThis[cacheHandlersSymbol];
if (globalCacheHandler == null ? void 0 : globalCacheHandler.FetchCache) {
CurCacheHandler = globalCacheHandler.FetchCache;
} else {
if (fs && serverDistDir) {
if (debug) {
console.log('using filesystem cache handler');
}
CurCacheHandler = FileSystemCache;
}
}
} else if (debug) {
console.log('using custom cache handler', CurCacheHandler.name);
}
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
// Allow cache size to be overridden for testing purposes
maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10);
}
this.dev = dev;
this.disableForTestmode = process.env.NEXT_PRIVATE_TEST_PROXY === 'true';
// this is a hack to avoid Webpack knowing this is equal to this.minimalMode
// because we replace this.minimalMode to true in production bundles.
const minimalModeKey = 'minimalMode';
this[minimalModeKey] = minimalMode;
this.requestHeaders = requestHeaders;
this.requestProtocol = requestProtocol;
this.allowedRevalidateHeaderKeys = allowedRevalidateHeaderKeys;
this.prerenderManifest = getPrerenderManifest();
this.cacheControls = new SharedCacheControls(this.prerenderManifest);
this.fetchCacheKeyPrefix = fetchCacheKeyPrefix;
let revalidatedTags = [];
if (requestHeaders[PRERENDER_REVALIDATE_HEADER] === ((_this_prerenderManifest = this.prerenderManifest) == null ? void 0 : (_this_prerenderManifest_preview = _this_prerenderManifest.preview) == null ? void 0 : _this_prerenderManifest_preview.previewModeId)) {
this.isOnDemandRevalidate = true;
}
if (minimalMode && typeof requestHeaders[NEXT_CACHE_REVALIDATED_TAGS_HEADER] === 'string' && requestHeaders[NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER] === ((_this_prerenderManifest1 = this.prerenderManifest) == null ? void 0 : (_this_prerenderManifest_preview1 = _this_prerenderManifest1.preview) == null ? void 0 : _this_prerenderManifest_preview1.previewModeId)) {
revalidatedTags = requestHeaders[NEXT_CACHE_REVALIDATED_TAGS_HEADER].split(',');
}
if (CurCacheHandler) {
this.cacheHandler = new CurCacheHandler({
dev,
fs,
flushToDisk,
serverDistDir,
revalidatedTags,
maxMemoryCacheSize,
_requestHeaders: requestHeaders,
fetchCacheKeyPrefix
});
}
}
calculateRevalidate(pathname, fromTime, dev, isFallback) {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
if (dev) return Math.floor(performance.timeOrigin + performance.now() - 1000);
const cacheControl = this.cacheControls.get(toRoute(pathname));
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second unless it's a fallback request.
const initialRevalidateSeconds = cacheControl ? cacheControl.revalidate : isFallback ? false : 1;
const revalidateAfter = typeof initialRevalidateSeconds === 'number' ? initialRevalidateSeconds * 1000 + fromTime : initialRevalidateSeconds;
return revalidateAfter;
}
_getPathname(pathname, fetchCache) {
return fetchCache ? pathname : normalizePagePath(pathname);
}
resetRequestCache() {
var _this_cacheHandler_resetRequestCache, _this_cacheHandler;
(_this_cacheHandler = this.cacheHandler) == null ? void 0 : (_this_cacheHandler_resetRequestCache = _this_cacheHandler.resetRequestCache) == null ? void 0 : _this_cacheHandler_resetRequestCache.call(_this_cacheHandler);
}
async lock(cacheKey) {
let unlockNext = ()=>Promise.resolve();
const existingLock = this.locks.get(cacheKey);
if (existingLock) {
await existingLock;
}
const newLock = new Promise((resolve)=>{
unlockNext = async ()=>{
resolve();
this.locks.delete(cacheKey) // Remove the lock upon release
;
};
});
this.locks.set(cacheKey, newLock);
return unlockNext;
}
async revalidateTag(tags) {
var _this_cacheHandler;
const promises = [];
if ((_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.revalidateTag) {
promises.push(this.cacheHandler.revalidateTag(tags));
}
const handlers = getCacheHandlers();
if (handlers) {
tags = Array.isArray(tags) ? tags : [
tags
];
for (const handler of handlers){
promises.push(handler.expireTags(...tags));
}
}
await Promise.all(promises);
}
// x-ref: https://github.com/facebook/react/blob/2655c9354d8e1c54ba888444220f63e836925caa/packages/react/src/ReactFetch.js#L23
async generateCacheKey(url, init = {}) {
// this should be bumped anytime a fix is made to cache entries
// that should bust the cache
const MAIN_KEY_PREFIX = 'v3';
const bodyChunks = [];
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (init.body) {
// handle ReadableStream body
if (typeof init.body.getReader === 'function') {
const readableBody = init.body;
const chunks = [];
try {
await readableBody.pipeTo(new WritableStream({
write (chunk) {
if (typeof chunk === 'string') {
chunks.push(encoder.encode(chunk));
bodyChunks.push(chunk);
} else {
chunks.push(chunk);
bodyChunks.push(decoder.decode(chunk, {
stream: true
}));
}
}
}));
// Flush the decoder.
bodyChunks.push(decoder.decode());
// Create a new buffer with all the chunks.
const length = chunks.reduce((total, arr)=>total + arr.length, 0);
const arrayBuffer = new Uint8Array(length);
// Push each of the chunks into the new array buffer.
let offset = 0;
for (const chunk of chunks){
arrayBuffer.set(chunk, offset);
offset += chunk.length;
}
;
init._ogBody = arrayBuffer;
} catch (err) {
console.error('Problem reading body', err);
}
} else if (typeof init.body.keys === 'function') {
const formData = init.body;
init._ogBody = init.body;
for (const key of new Set([
...formData.keys()
])){
const values = formData.getAll(key);
bodyChunks.push(`${key}=${(await Promise.all(values.map(async (val)=>{
if (typeof val === 'string') {
return val;
} else {
return await val.text();
}
}))).join(',')}`);
}
// handle blob body
} else if (typeof init.body.arrayBuffer === 'function') {
const blob = init.body;
const arrayBuffer = await blob.arrayBuffer();
bodyChunks.push(await blob.text());
init._ogBody = new Blob([
arrayBuffer
], {
type: blob.type
});
} else if (typeof init.body === 'string') {
bodyChunks.push(init.body);
init._ogBody = init.body;
}
}
const headers = typeof (init.headers || {}).keys === 'function' ? Object.fromEntries(init.headers) : Object.assign({}, init.headers);
// w3c trace context headers can break request caching and deduplication
// so we remove them from the cache key
if ('traceparent' in headers) delete headers['traceparent'];
if ('tracestate' in headers) delete headers['tracestate'];
const cacheString = JSON.stringify([
MAIN_KEY_PREFIX,
this.fetchCacheKeyPrefix || '',
url,
init.method,
headers,
init.mode,
init.redirect,
init.credentials,
init.referrer,
init.referrerPolicy,
init.integrity,
init.cache,
bodyChunks
]);
if (process.env.NEXT_RUNTIME === 'edge') {
function bufferToHex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), (b)=>b.toString(16).padStart(2, '0')).join('');
}
const buffer = encoder.encode(cacheString);
return bufferToHex(await crypto.subtle.digest('SHA-256', buffer));
} else {
const crypto1 = require('crypto');
return crypto1.createHash('sha256').update(cacheString).digest('hex');
}
}
async get(cacheKey, ctx) {
var _this_cacheHandler, _cacheData_value;
// Unlike other caches if we have a resume data cache, we use it even if
// testmode would normally disable it or if requestHeaders say 'no-cache'.
if (ctx.kind === IncrementalCacheKind.FETCH) {
const workUnitStore = workUnitAsyncStorageInstance.getStore();
const resumeDataCache = workUnitStore ? getRenderResumeDataCache(workUnitStore) : null;
if (resumeDataCache) {
const memoryCacheData = resumeDataCache.fetch.get(cacheKey);
if ((memoryCacheData == null ? void 0 : memoryCacheData.kind) === CachedRouteKind.FETCH) {
return {
isStale: false,
value: memoryCacheData
};
}
}
}
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if (this.disableForTestmode || this.dev && (ctx.kind !== IncrementalCacheKind.FETCH || this.requestHeaders['cache-control'] === 'no-cache')) {
return null;
}
cacheKey = this._getPathname(cacheKey, ctx.kind === IncrementalCacheKind.FETCH);
const cacheData = await ((_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.get(cacheKey, ctx));
if (ctx.kind === IncrementalCacheKind.FETCH) {
var _cacheData_value1;
if (!cacheData) {
return null;
}
if (((_cacheData_value1 = cacheData.value) == null ? void 0 : _cacheData_value1.kind) !== CachedRouteKind.FETCH) {
var _cacheData_value2;
throw Object.defineProperty(new InvariantError(`Expected cached value for cache key ${JSON.stringify(cacheKey)} to be a "FETCH" kind, got ${JSON.stringify((_cacheData_value2 = cacheData.value) == null ? void 0 : _cacheData_value2.kind)} instead.`), "__NEXT_ERROR_CODE", {
value: "E653",
enumerable: false,
configurable: true
});
}
const combinedTags = [
...ctx.tags || [],
...ctx.softTags || []
];
// if a tag was revalidated we don't return stale data
if (combinedTags.some((tag)=>{
var _this_revalidatedTags;
return (_this_revalidatedTags = this.revalidatedTags) == null ? void 0 : _this_revalidatedTags.includes(tag);
})) {
return null;
}
const revalidate = ctx.revalidate || cacheData.value.revalidate;
const age = (performance.timeOrigin + performance.now() - (cacheData.lastModified || 0)) / 1000;
const isStale = age > revalidate;
const data = cacheData.value.data;
return {
isStale,
value: {
kind: CachedRouteKind.FETCH,
data,
revalidate
}
};
} else if ((cacheData == null ? void 0 : (_cacheData_value = cacheData.value) == null ? void 0 : _cacheData_value.kind) === CachedRouteKind.FETCH) {
throw Object.defineProperty(new InvariantError(`Expected cached value for cache key ${JSON.stringify(cacheKey)} not to be a ${JSON.stringify(ctx.kind)} kind, got "FETCH" instead.`), "__NEXT_ERROR_CODE", {
value: "E652",
enumerable: false,
configurable: true
});
}
let entry = null;
const { isFallback } = ctx;
const cacheControl = this.cacheControls.get(toRoute(cacheKey));
let isStale;
let revalidateAfter;
if ((cacheData == null ? void 0 : cacheData.lastModified) === -1) {
isStale = -1;
revalidateAfter = -1 * CACHE_ONE_YEAR;
} else {
revalidateAfter = this.calculateRevalidate(cacheKey, (cacheData == null ? void 0 : cacheData.lastModified) || performance.timeOrigin + performance.now(), this.dev ?? false, ctx.isFallback);
isStale = revalidateAfter !== false && revalidateAfter < performance.timeOrigin + performance.now() ? true : undefined;
}
if (cacheData) {
entry = {
isStale,
cacheControl,
revalidateAfter,
value: cacheData.value,
isFallback
};
}
if (!cacheData && this.prerenderManifest.notFoundRoutes.includes(cacheKey)) {
// for the first hit after starting the server the cache
// may not have a way to save notFound: true so if
// the prerender-manifest marks this as notFound then we
// return that entry and trigger a cache set to give it a
// chance to update in-memory entries
entry = {
isStale,
value: null,
cacheControl,
revalidateAfter,
isFallback
};
this.set(cacheKey, entry.value, {
...ctx,
cacheControl
});
}
return entry;
}
async set(pathname, data, ctx) {
// Even if we otherwise disable caching for testMode or if no fetchCache is
// configured we still always stash results in the resume data cache if one
// exists. This is because this is a transient in memory cache that
// populates caches ahead of a dynamic render in dev mode to allow the RSC
// debug info to have the right environment associated to it.
if ((data == null ? void 0 : data.kind) === CachedRouteKind.FETCH) {
const workUnitStore = workUnitAsyncStorageInstance.getStore();
const prerenderResumeDataCache = workUnitStore ? getPrerenderResumeDataCache(workUnitStore) : null;
if (prerenderResumeDataCache) {
prerenderResumeDataCache.fetch.set(pathname, data);
}
}
if (this.disableForTestmode || this.dev && !ctx.fetchCache) return;
pathname = this._getPathname(pathname, ctx.fetchCache);
// FetchCache has upper limit of 2MB per-entry currently
const itemSize = JSON.stringify(data).length;
if (ctx.fetchCache && // we don't show this error/warning when a custom cache handler is being used
// as it might not have this limit
!this.hasCustomCacheHandler && itemSize > 2 * 1024 * 1024) {
if (this.dev) {
throw Object.defineProperty(new Error(`Failed to set Next.js data cache, items over 2MB can not be cached (${itemSize} bytes)`), "__NEXT_ERROR_CODE", {
value: "E86",
enumerable: false,
configurable: true
});
}
return;
}
try {
var _this_cacheHandler;
if (!ctx.fetchCache && ctx.cacheControl) {
this.cacheControls.set(toRoute(pathname), ctx.cacheControl);
}
await ((_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.set(pathname, data, ctx));
} catch (error) {
console.warn('Failed to update prerender cache for', pathname, error);
}
}
}
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,67 @@
/**
* A shared cache of cache controls for routes. This cache is used so we don't
* have to modify the prerender manifest when we want to update the cache
* control for a route.
*/ export class SharedCacheControls {
static #_ = /**
* The in-memory cache of cache lives for routes. This cache is populated when
* the cache is updated with new cache lives.
*/ this.cacheControls = new Map();
constructor(/**
* The prerender manifest that contains the initial cache controls for
* routes.
*/ prerenderManifest){
this.prerenderManifest = prerenderManifest;
}
/**
* Try to get the cache control value for a route. This will first try to get
* the value from the in-memory cache. If the value is not present in the
* in-memory cache, it will be sourced from the prerender manifest.
*
* @param route the route to get the cache control for
* @returns the cache control for the route, or undefined if the values
* are not present in the in-memory cache or the prerender manifest
*/ get(route) {
// This is a copy on write cache that is updated when the cache is updated.
// If the cache is never written to, then the values will be sourced from
// the prerender manifest.
let cacheControl = SharedCacheControls.cacheControls.get(route);
if (cacheControl) return cacheControl;
let prerenderData = this.prerenderManifest.routes[route];
if (prerenderData) {
const { initialRevalidateSeconds, initialExpireSeconds } = prerenderData;
if (typeof initialRevalidateSeconds !== 'undefined') {
return {
revalidate: initialRevalidateSeconds,
expire: initialExpireSeconds
};
}
}
const dynamicPrerenderData = this.prerenderManifest.dynamicRoutes[route];
if (dynamicPrerenderData) {
const { fallbackRevalidate, fallbackExpire } = dynamicPrerenderData;
if (typeof fallbackRevalidate !== 'undefined') {
return {
revalidate: fallbackRevalidate,
expire: fallbackExpire
};
}
}
return undefined;
}
/**
* Set the cache control for a route.
*
* @param route the route to set the cache control for
* @param cacheControl the cache control for the route
*/ set(route, cacheControl) {
SharedCacheControls.cacheControls.set(route, cacheControl);
}
/**
* Clear the in-memory cache of cache controls for routes.
*/ clear() {
SharedCacheControls.cacheControls.clear();
}
}
//# sourceMappingURL=shared-cache-controls.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/incremental-cache/shared-cache-controls.ts"],"sourcesContent":["import type { PrerenderManifest } from '../../../build'\nimport type { DeepReadonly } from '../../../shared/lib/deep-readonly'\nimport type { CacheControl } from '../cache-control'\n\n/**\n * A shared cache of cache controls for routes. This cache is used so we don't\n * have to modify the prerender manifest when we want to update the cache\n * control for a route.\n */\nexport class SharedCacheControls {\n /**\n * The in-memory cache of cache lives for routes. This cache is populated when\n * the cache is updated with new cache lives.\n */\n private static readonly cacheControls = new Map<string, CacheControl>()\n\n constructor(\n /**\n * The prerender manifest that contains the initial cache controls for\n * routes.\n */\n private readonly prerenderManifest: DeepReadonly<\n Pick<PrerenderManifest, 'routes' | 'dynamicRoutes'>\n >\n ) {}\n\n /**\n * Try to get the cache control value for a route. This will first try to get\n * the value from the in-memory cache. If the value is not present in the\n * in-memory cache, it will be sourced from the prerender manifest.\n *\n * @param route the route to get the cache control for\n * @returns the cache control for the route, or undefined if the values\n * are not present in the in-memory cache or the prerender manifest\n */\n public get(route: string): CacheControl | undefined {\n // This is a copy on write cache that is updated when the cache is updated.\n // If the cache is never written to, then the values will be sourced from\n // the prerender manifest.\n let cacheControl = SharedCacheControls.cacheControls.get(route)\n if (cacheControl) return cacheControl\n\n let prerenderData = this.prerenderManifest.routes[route]\n\n if (prerenderData) {\n const { initialRevalidateSeconds, initialExpireSeconds } = prerenderData\n\n if (typeof initialRevalidateSeconds !== 'undefined') {\n return {\n revalidate: initialRevalidateSeconds,\n expire: initialExpireSeconds,\n }\n }\n }\n\n const dynamicPrerenderData = this.prerenderManifest.dynamicRoutes[route]\n\n if (dynamicPrerenderData) {\n const { fallbackRevalidate, fallbackExpire } = dynamicPrerenderData\n\n if (typeof fallbackRevalidate !== 'undefined') {\n return { revalidate: fallbackRevalidate, expire: fallbackExpire }\n }\n }\n\n return undefined\n }\n\n /**\n * Set the cache control for a route.\n *\n * @param route the route to set the cache control for\n * @param cacheControl the cache control for the route\n */\n public set(route: string, cacheControl: CacheControl) {\n SharedCacheControls.cacheControls.set(route, cacheControl)\n }\n\n /**\n * Clear the in-memory cache of cache controls for routes.\n */\n public clear() {\n SharedCacheControls.cacheControls.clear()\n }\n}\n"],"names":["SharedCacheControls","cacheControls","Map","constructor","prerenderManifest","get","route","cacheControl","prerenderData","routes","initialRevalidateSeconds","initialExpireSeconds","revalidate","expire","dynamicPrerenderData","dynamicRoutes","fallbackRevalidate","fallbackExpire","undefined","set","clear"],"mappings":"AAIA;;;;CAIC,GACD,OAAO,MAAMA;gBACX;;;GAGC,QACuBC,gBAAgB,IAAIC;IAE5CC,YACE;;;KAGC,GACD,AAAiBC,iBAEhB,CACD;aAHiBA,oBAAAA;IAGhB;IAEH;;;;;;;;GAQC,GACD,AAAOC,IAAIC,KAAa,EAA4B;QAClD,2EAA2E;QAC3E,yEAAyE;QACzE,0BAA0B;QAC1B,IAAIC,eAAeP,oBAAoBC,aAAa,CAACI,GAAG,CAACC;QACzD,IAAIC,cAAc,OAAOA;QAEzB,IAAIC,gBAAgB,IAAI,CAACJ,iBAAiB,CAACK,MAAM,CAACH,MAAM;QAExD,IAAIE,eAAe;YACjB,MAAM,EAAEE,wBAAwB,EAAEC,oBAAoB,EAAE,GAAGH;YAE3D,IAAI,OAAOE,6BAA6B,aAAa;gBACnD,OAAO;oBACLE,YAAYF;oBACZG,QAAQF;gBACV;YACF;QACF;QAEA,MAAMG,uBAAuB,IAAI,CAACV,iBAAiB,CAACW,aAAa,CAACT,MAAM;QAExE,IAAIQ,sBAAsB;YACxB,MAAM,EAAEE,kBAAkB,EAAEC,cAAc,EAAE,GAAGH;YAE/C,IAAI,OAAOE,uBAAuB,aAAa;gBAC7C,OAAO;oBAAEJ,YAAYI;oBAAoBH,QAAQI;gBAAe;YAClE;QACF;QAEA,OAAOC;IACT;IAEA;;;;;GAKC,GACD,AAAOC,IAAIb,KAAa,EAAEC,YAA0B,EAAE;QACpDP,oBAAoBC,aAAa,CAACkB,GAAG,CAACb,OAAOC;IAC/C;IAEA;;GAEC,GACD,AAAOa,QAAQ;QACbpB,oBAAoBC,aAAa,CAACmB,KAAK;IACzC;AACF"}

View File

@@ -0,0 +1,17 @@
// we share tags manifest between "use cache" handlers and
// previous file-system-cache
export const tagsManifest = {
items: {}
};
export const isTagStale = (tags, timestamp)=>{
for (const tag of tags){
const tagEntry = tagsManifest.items[tag];
if (typeof (tagEntry == null ? void 0 : tagEntry.revalidatedAt) === 'number' && // TODO: use performance.now and update file-system-cache?
tagEntry.revalidatedAt >= timestamp) {
return true;
}
}
return false;
};
//# sourceMappingURL=tags-manifest.external.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/incremental-cache/tags-manifest.external.ts"],"sourcesContent":["type TagsManifest = {\n items: { [tag: string]: { revalidatedAt?: number } }\n}\n\n// we share tags manifest between \"use cache\" handlers and\n// previous file-system-cache\nexport const tagsManifest: TagsManifest = {\n items: {},\n}\n\nexport const isTagStale = (tags: string[], timestamp: number) => {\n for (const tag of tags) {\n const tagEntry = tagsManifest.items[tag]\n if (\n typeof tagEntry?.revalidatedAt === 'number' &&\n // TODO: use performance.now and update file-system-cache?\n tagEntry.revalidatedAt >= timestamp\n ) {\n return true\n }\n }\n return false\n}\n"],"names":["tagsManifest","items","isTagStale","tags","timestamp","tag","tagEntry","revalidatedAt"],"mappings":"AAIA,0DAA0D;AAC1D,6BAA6B;AAC7B,OAAO,MAAMA,eAA6B;IACxCC,OAAO,CAAC;AACV,EAAC;AAED,OAAO,MAAMC,aAAa,CAACC,MAAgBC;IACzC,KAAK,MAAMC,OAAOF,KAAM;QACtB,MAAMG,WAAWN,aAAaC,KAAK,CAACI,IAAI;QACxC,IACE,QAAOC,4BAAAA,SAAUC,aAAa,MAAK,YACnC,0DAA0D;QAC1DD,SAASC,aAAa,IAAIH,WAC1B;YACA,OAAO;QACT;IACF;IACA,OAAO;AACT,EAAC"}

31
node_modules/next/dist/esm/server/lib/is-ipv6.js generated vendored Normal file
View File

@@ -0,0 +1,31 @@
// Regex from `node/lib/internal/net.js`: https://github.com/nodejs/node/blob/9fc57006c27564ed7f75eee090eca86786508f51/lib/internal/net.js#L19-L29
// License included below:
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const v4Str = `(${v4Seg}[.]){3}${v4Seg}`;
const v6Seg = '(?:[0-9a-fA-F]{1,4})';
const IPv6Reg = new RegExp('^(' + `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` + `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` + `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` + `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` + `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` + `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` + `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` + `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + ')(%[0-9a-zA-Z-.:]{1,})?$');
export function isIPv6(s) {
return IPv6Reg.test(s);
}
//# sourceMappingURL=is-ipv6.js.map

1
node_modules/next/dist/esm/server/lib/is-ipv6.js.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/is-ipv6.ts"],"sourcesContent":["// Regex from `node/lib/internal/net.js`: https://github.com/nodejs/node/blob/9fc57006c27564ed7f75eee090eca86786508f51/lib/internal/net.js#L19-L29\n// License included below:\n// Copyright Joyent, Inc. and other Node contributors.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a\n// copy of this software and associated documentation files (the\n// \"Software\"), to deal in the Software without restriction, including\n// without limitation the rights to use, copy, modify, merge, publish,\n// distribute, sublicense, and/or sell copies of the Software, and to permit\n// persons to whom the Software is furnished to do so, subject to the\n// following conditions:\n//\n// The above copyright notice and this permission notice shall be included\n// in all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\n// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\n// USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nconst v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'\nconst v4Str = `(${v4Seg}[.]){3}${v4Seg}`\nconst v6Seg = '(?:[0-9a-fA-F]{1,4})'\nconst IPv6Reg = new RegExp(\n '^(' +\n `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +\n `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +\n `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +\n `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +\n `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +\n `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +\n `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +\n `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +\n ')(%[0-9a-zA-Z-.:]{1,})?$'\n)\n\nexport function isIPv6(s: string) {\n return IPv6Reg.test(s)\n}\n"],"names":["v4Seg","v4Str","v6Seg","IPv6Reg","RegExp","isIPv6","s","test"],"mappings":"AAAA,kJAAkJ;AAClJ,0BAA0B;AAC1B,sDAAsD;AACtD,EAAE;AACF,0EAA0E;AAC1E,gEAAgE;AAChE,sEAAsE;AACtE,sEAAsE;AACtE,4EAA4E;AAC5E,qEAAqE;AACrE,wBAAwB;AACxB,EAAE;AACF,0EAA0E;AAC1E,yDAAyD;AACzD,EAAE;AACF,0EAA0E;AAC1E,6DAA6D;AAC7D,4EAA4E;AAC5E,2EAA2E;AAC3E,wEAAwE;AACxE,4EAA4E;AAC5E,yCAAyC;AAEzC,MAAMA,QAAQ;AACd,MAAMC,QAAQ,CAAC,CAAC,EAAED,MAAM,OAAO,EAAEA,OAAO;AACxC,MAAME,QAAQ;AACd,MAAMC,UAAU,IAAIC,OAClB,OACE,CAAC,GAAG,EAAEF,MAAM,QAAQ,EAAEA,MAAM,IAAI,CAAC,GACjC,CAAC,GAAG,EAAEA,MAAM,QAAQ,EAAED,MAAM,EAAE,EAAEC,MAAM,IAAI,CAAC,GAC3C,CAAC,GAAG,EAAEA,MAAM,SAAS,EAAED,MAAM,GAAG,EAAEC,MAAM,UAAU,CAAC,GACnD,CAAC,GAAG,EAAEA,MAAM,UAAU,EAAEA,MAAM,OAAO,EAAED,MAAM,GAAG,EAAEC,MAAM,UAAU,CAAC,GACnE,CAAC,GAAG,EAAEA,MAAM,UAAU,EAAEA,MAAM,OAAO,EAAED,MAAM,GAAG,EAAEC,MAAM,UAAU,CAAC,GACnE,CAAC,GAAG,EAAEA,MAAM,UAAU,EAAEA,MAAM,OAAO,EAAED,MAAM,GAAG,EAAEC,MAAM,UAAU,CAAC,GACnE,CAAC,GAAG,EAAEA,MAAM,UAAU,EAAEA,MAAM,OAAO,EAAED,MAAM,GAAG,EAAEC,MAAM,UAAU,CAAC,GACnE,CAAC,SAAS,EAAEA,MAAM,OAAO,EAAED,MAAM,KAAK,EAAEC,MAAM,UAAU,CAAC,GACzD;AAGJ,OAAO,SAASG,OAAOC,CAAS;IAC9B,OAAOH,QAAQI,IAAI,CAACD;AACtB"}

90
node_modules/next/dist/esm/server/lib/lru-cache.js generated vendored Normal file
View File

@@ -0,0 +1,90 @@
export class LRUCache {
constructor(maxSize, calculateSize){
this.cache = new Map();
this.sizes = new Map();
this.totalSize = 0;
this.maxSize = maxSize;
this.calculateSize = calculateSize || (()=>1);
}
set(key, value) {
if (!key || !value) return;
const size = this.calculateSize(value);
if (size > this.maxSize) {
console.warn('Single item size exceeds maxSize');
return;
}
if (this.cache.has(key)) {
this.totalSize -= this.sizes.get(key) || 0;
}
this.cache.set(key, value);
this.sizes.set(key, size);
this.totalSize += size;
this.touch(key);
}
has(key) {
if (!key) return false;
this.touch(key);
return Boolean(this.cache.get(key));
}
get(key) {
if (!key) return;
const value = this.cache.get(key);
if (value === undefined) {
return undefined;
}
this.touch(key);
return value;
}
touch(key) {
const value = this.cache.get(key);
if (value !== undefined) {
this.cache.delete(key);
this.cache.set(key, value);
this.evictIfNecessary();
}
}
evictIfNecessary() {
while(this.totalSize > this.maxSize && this.cache.size > 0){
this.evictLeastRecentlyUsed();
}
}
evictLeastRecentlyUsed() {
const lruKey = this.cache.keys().next().value;
if (lruKey !== undefined) {
const lruSize = this.sizes.get(lruKey) || 0;
this.totalSize -= lruSize;
this.cache.delete(lruKey);
this.sizes.delete(lruKey);
}
}
reset() {
this.cache.clear();
this.sizes.clear();
this.totalSize = 0;
}
keys() {
return [
...this.cache.keys()
];
}
remove(key) {
if (this.cache.has(key)) {
this.totalSize -= this.sizes.get(key) || 0;
this.cache.delete(key);
this.sizes.delete(key);
}
}
clear() {
this.cache.clear();
this.sizes.clear();
this.totalSize = 0;
}
get size() {
return this.cache.size;
}
get currentSize() {
return this.totalSize;
}
}
//# sourceMappingURL=lru-cache.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,8 @@
import { getPathMatch } from '../../shared/lib/router/utils/path-match';
const matcher = getPathMatch('/_next/data/:path*');
export function matchNextDataPathname(pathname) {
if (typeof pathname !== 'string') return false;
return matcher(pathname);
}
//# sourceMappingURL=match-next-data-pathname.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/match-next-data-pathname.ts"],"sourcesContent":["import { getPathMatch } from '../../shared/lib/router/utils/path-match'\n\nconst matcher = getPathMatch('/_next/data/:path*')\n\nexport function matchNextDataPathname(pathname: string | null | undefined) {\n if (typeof pathname !== 'string') return false\n\n return matcher(pathname)\n}\n"],"names":["getPathMatch","matcher","matchNextDataPathname","pathname"],"mappings":"AAAA,SAASA,YAAY,QAAQ,2CAA0C;AAEvE,MAAMC,UAAUD,aAAa;AAE7B,OAAO,SAASE,sBAAsBC,QAAmC;IACvE,IAAI,OAAOA,aAAa,UAAU,OAAO;IAEzC,OAAOF,QAAQE;AACjB"}

385
node_modules/next/dist/esm/server/lib/mock-request.js generated vendored Normal file
View File

@@ -0,0 +1,385 @@
import Stream from 'stream';
import { fromNodeOutgoingHttpHeaders, toNodeOutgoingHttpHeaders } from '../web/utils';
export class MockedRequest extends Stream.Readable {
constructor({ url, headers, method, socket = null, readable }){
super(), // This is hardcoded for now, but can be updated to be configurable if needed.
this.httpVersion = '1.0', this.httpVersionMajor = 1, this.httpVersionMinor = 0, // If we don't actually have a socket, we'll just use a mock one that
// always returns false for the `encrypted` property and undefined for the
// `remoteAddress` property.
this.socket = new Proxy({}, {
get: (_target, prop)=>{
if (prop !== 'encrypted' && prop !== 'remoteAddress') {
throw Object.defineProperty(new Error('Method not implemented'), "__NEXT_ERROR_CODE", {
value: "E52",
enumerable: false,
configurable: true
});
}
if (prop === 'remoteAddress') return undefined;
// For this mock request, always ensure we just respond with the encrypted
// set to false to ensure there's no odd leakages.
return false;
}
});
this.url = url;
this.headers = headers;
this.method = method;
if (readable) {
this.bodyReadable = readable;
this.bodyReadable.on('end', ()=>this.emit('end'));
this.bodyReadable.on('close', ()=>this.emit('close'));
}
if (socket) {
this.socket = socket;
}
}
get headersDistinct() {
const headers = {};
for (const [key, value] of Object.entries(this.headers)){
if (!value) continue;
headers[key] = Array.isArray(value) ? value : [
value
];
}
return headers;
}
_read(size) {
if (this.bodyReadable) {
return this.bodyReadable._read(size);
} else {
this.emit('end');
this.emit('close');
}
}
/**
* The `connection` property is just an alias for the `socket` property.
*
* @deprecated — since v13.0.0 - Use socket instead.
*/ get connection() {
return this.socket;
}
// The following methods are not implemented as they are not used in the
// Next.js codebase.
get aborted() {
throw Object.defineProperty(new Error('Method not implemented'), "__NEXT_ERROR_CODE", {
value: "E52",
enumerable: false,
configurable: true
});
}
get complete() {
throw Object.defineProperty(new Error('Method not implemented'), "__NEXT_ERROR_CODE", {
value: "E52",
enumerable: false,
configurable: true
});
}
get trailers() {
throw Object.defineProperty(new Error('Method not implemented'), "__NEXT_ERROR_CODE", {
value: "E52",
enumerable: false,
configurable: true
});
}
get trailersDistinct() {
throw Object.defineProperty(new Error('Method not implemented'), "__NEXT_ERROR_CODE", {
value: "E52",
enumerable: false,
configurable: true
});
}
get rawTrailers() {
throw Object.defineProperty(new Error('Method not implemented'), "__NEXT_ERROR_CODE", {
value: "E52",
enumerable: false,
configurable: true
});
}
get rawHeaders() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
setTimeout() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
}
export class MockedResponse extends Stream.Writable {
constructor(res = {}){
super(), this.statusMessage = '', this.finished = false, this.headersSent = false, /**
* A list of buffers that have been written to the response.
*
* @internal - used internally by Next.js
*/ this.buffers = [];
this.statusCode = res.statusCode ?? 200;
this.socket = res.socket ?? null;
this.headers = res.headers ? fromNodeOutgoingHttpHeaders(res.headers) : new Headers();
this.headPromise = new Promise((resolve)=>{
this.headPromiseResolve = resolve;
});
// Attach listeners for the `finish`, `end`, and `error` events to the
// `MockedResponse` instance.
this.hasStreamed = new Promise((resolve, reject)=>{
this.on('finish', ()=>resolve(true));
this.on('end', ()=>resolve(true));
this.on('error', (err)=>reject(err));
}).then((val)=>{
this.headPromiseResolve == null ? void 0 : this.headPromiseResolve.call(this);
return val;
});
if (res.resWriter) {
this.resWriter = res.resWriter;
}
}
appendHeader(name, value) {
const values = Array.isArray(value) ? value : [
value
];
for (const v of values){
this.headers.append(name, v);
}
return this;
}
/**
* Returns true if the response has been sent, false otherwise.
*
* @internal - used internally by Next.js
*/ get isSent() {
return this.finished || this.headersSent;
}
/**
* The `connection` property is just an alias for the `socket` property.
*
* @deprecated — since v13.0.0 - Use socket instead.
*/ get connection() {
return this.socket;
}
write(chunk) {
if (this.resWriter) {
return this.resWriter(chunk);
}
this.buffers.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
return true;
}
end() {
this.finished = true;
return super.end(...arguments);
}
/**
* This method is a no-op because the `MockedResponse` instance is not
* actually connected to a socket. This method is not specified on the
* interface type for `ServerResponse` but is called by Node.js.
*
* @see https://github.com/nodejs/node/pull/7949
*/ _implicitHeader() {}
_write(chunk, _encoding, callback) {
this.write(chunk);
// According to Node.js documentation, the callback MUST be invoked to
// signal that the write completed successfully. If this callback is not
// invoked, the 'finish' event will not be emitted.
//
// https://nodejs.org/docs/latest-v16.x/api/stream.html#writable_writechunk-encoding-callback
callback();
}
writeHead(statusCode, statusMessage, headers) {
if (!headers && typeof statusMessage !== 'string') {
headers = statusMessage;
} else if (typeof statusMessage === 'string' && statusMessage.length > 0) {
this.statusMessage = statusMessage;
}
if (headers) {
// When headers have been set with response.setHeader(), they will be
// merged with any headers passed to response.writeHead(), with the
// headers passed to response.writeHead() given precedence.
//
// https://nodejs.org/api/http.html#responsewriteheadstatuscode-statusmessage-headers
//
// For this reason, we need to only call `set` to ensure that this will
// overwrite any existing 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. The
// array is in the same format as request.rawHeaders.
for(let i = 0; i < headers.length; i += 2){
// The header key is always a string according to the spec.
this.setHeader(headers[i], headers[i + 1]);
}
} else {
for (const [key, value] of Object.entries(headers)){
// Skip undefined values
if (typeof value === 'undefined') continue;
this.setHeader(key, value);
}
}
}
this.statusCode = statusCode;
this.headersSent = true;
this.headPromiseResolve == null ? void 0 : this.headPromiseResolve.call(this);
return this;
}
hasHeader(name) {
return this.headers.has(name);
}
getHeader(name) {
return this.headers.get(name) ?? undefined;
}
getHeaders() {
return toNodeOutgoingHttpHeaders(this.headers);
}
getHeaderNames() {
return Array.from(this.headers.keys());
}
setHeader(name, value) {
if (Array.isArray(value)) {
// Because `set` here should override any existing values, we need to
// delete the existing values before setting the new ones via `append`.
this.headers.delete(name);
for (const v of value){
this.headers.append(name, v);
}
} else if (typeof value === 'number') {
this.headers.set(name, value.toString());
} else {
this.headers.set(name, value);
}
return this;
}
removeHeader(name) {
this.headers.delete(name);
}
flushHeaders() {
// This is a no-op because we don't actually have a socket to flush the
// headers to.
}
// The following methods are not implemented as they are not used in the
// Next.js codebase.
get strictContentLength() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
writeEarlyHints() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
get req() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
assignSocket() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
detachSocket() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
writeContinue() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
writeProcessing() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
get upgrading() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
get chunkedEncoding() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
get shouldKeepAlive() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
get useChunkedEncodingByDefault() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
get sendDate() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
setTimeout() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
addTrailers() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
setHeaders() {
throw Object.defineProperty(new Error('Method not implemented.'), "__NEXT_ERROR_CODE", {
value: "E41",
enumerable: false,
configurable: true
});
}
}
export function createRequestResponseMocks({ url, headers = {}, method = 'GET', bodyReadable, resWriter, socket = null }) {
return {
req: new MockedRequest({
url,
headers,
method,
socket,
readable: bodyReadable
}),
res: new MockedResponse({
socket,
resWriter
})
};
}
//# sourceMappingURL=mock-request.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,5 @@
/**
* Loads a given module for a given ID.
*/ export { };
//# sourceMappingURL=module-loader.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/module-loader/module-loader.ts"],"sourcesContent":["/**\n * Loads a given module for a given ID.\n */\nexport interface ModuleLoader {\n load<M = any>(id: string): Promise<M>\n}\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAEC"}

View File

@@ -0,0 +1,17 @@
/**
* Loads a module using `await require(id)`.
*/ export class NodeModuleLoader {
async load(id) {
if (process.env.NEXT_RUNTIME !== 'edge') {
// Need to `await` to cover the case that route is marked ESM modules by ESM escalation.
return await (process.env.NEXT_MINIMAL ? __non_webpack_require__(id) : require(id));
}
throw Object.defineProperty(new Error('NodeModuleLoader is not supported in edge runtime.'), "__NEXT_ERROR_CODE", {
value: "E25",
enumerable: false,
configurable: true
});
}
}
//# sourceMappingURL=node-module-loader.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/module-loader/node-module-loader.ts"],"sourcesContent":["import type { ModuleLoader } from './module-loader'\n\n/**\n * Loads a module using `await require(id)`.\n */\nexport class NodeModuleLoader implements ModuleLoader {\n public async load<M>(id: string): Promise<M> {\n if (process.env.NEXT_RUNTIME !== 'edge') {\n // Need to `await` to cover the case that route is marked ESM modules by ESM escalation.\n return await (process.env.NEXT_MINIMAL\n ? // @ts-ignore\n __non_webpack_require__(id)\n : require(id))\n }\n\n throw new Error('NodeModuleLoader is not supported in edge runtime.')\n }\n}\n"],"names":["NodeModuleLoader","load","id","process","env","NEXT_RUNTIME","NEXT_MINIMAL","__non_webpack_require__","require","Error"],"mappings":"AAEA;;CAEC,GACD,OAAO,MAAMA;IACX,MAAaC,KAAQC,EAAU,EAAc;QAC3C,IAAIC,QAAQC,GAAG,CAACC,YAAY,KAAK,QAAQ;YACvC,wFAAwF;YACxF,OAAO,MAAOF,CAAAA,QAAQC,GAAG,CAACE,YAAY,GAElCC,wBAAwBL,MACxBM,QAAQN,GAAE;QAChB;QAEA,MAAM,qBAA+D,CAA/D,IAAIO,MAAM,uDAAV,qBAAA;mBAAA;wBAAA;0BAAA;QAA8D;IACtE;AACF"}

View File

@@ -0,0 +1,16 @@
import { NodeModuleLoader } from './node-module-loader';
export class RouteModuleLoader {
static async load(id, loader = new NodeModuleLoader()) {
const module = await loader.load(id);
if ('routeModule' in module) {
return module.routeModule;
}
throw Object.defineProperty(new Error(`Module "${id}" does not export a routeModule.`), "__NEXT_ERROR_CODE", {
value: "E53",
enumerable: false,
configurable: true
});
}
}
//# sourceMappingURL=route-module-loader.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/module-loader/route-module-loader.ts"],"sourcesContent":["import type { RouteModule } from '../../route-modules/route-module'\nimport type { ModuleLoader } from './module-loader'\n\nimport { NodeModuleLoader } from './node-module-loader'\n\nexport interface AppLoaderModule<M extends RouteModule = RouteModule> {\n routeModule: M\n}\n\nexport class RouteModuleLoader {\n static async load<M extends RouteModule>(\n id: string,\n loader: ModuleLoader = new NodeModuleLoader()\n ): Promise<M> {\n const module: AppLoaderModule<M> = await loader.load(id)\n if ('routeModule' in module) {\n return module.routeModule\n }\n\n throw new Error(`Module \"${id}\" does not export a routeModule.`)\n }\n}\n"],"names":["NodeModuleLoader","RouteModuleLoader","load","id","loader","module","routeModule","Error"],"mappings":"AAGA,SAASA,gBAAgB,QAAQ,uBAAsB;AAMvD,OAAO,MAAMC;IACX,aAAaC,KACXC,EAAU,EACVC,SAAuB,IAAIJ,kBAAkB,EACjC;QACZ,MAAMK,SAA6B,MAAMD,OAAOF,IAAI,CAACC;QACrD,IAAI,iBAAiBE,QAAQ;YAC3B,OAAOA,OAAOC,WAAW;QAC3B;QAEA,MAAM,qBAA0D,CAA1D,IAAIC,MAAM,CAAC,QAAQ,EAAEJ,GAAG,gCAAgC,CAAC,GAAzD,qBAAA;mBAAA;wBAAA;0BAAA;QAAyD;IACjE;AACF"}

View File

@@ -0,0 +1,13 @@
import fs from 'fs';
export const nodeFs = {
existsSync: fs.existsSync,
readFile: fs.promises.readFile,
readFileSync: fs.readFileSync,
writeFile: (f, d)=>fs.promises.writeFile(f, d),
mkdir: (dir)=>fs.promises.mkdir(dir, {
recursive: true
}),
stat: (f)=>fs.promises.stat(f)
};
//# sourceMappingURL=node-fs-methods.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/node-fs-methods.ts"],"sourcesContent":["import type { CacheFs } from '../../shared/lib/utils'\n\nimport fs from 'fs'\n\nexport const nodeFs: CacheFs = {\n existsSync: fs.existsSync,\n readFile: fs.promises.readFile,\n readFileSync: fs.readFileSync,\n writeFile: (f, d) => fs.promises.writeFile(f, d),\n mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }),\n stat: (f) => fs.promises.stat(f),\n}\n"],"names":["fs","nodeFs","existsSync","readFile","promises","readFileSync","writeFile","f","d","mkdir","dir","recursive","stat"],"mappings":"AAEA,OAAOA,QAAQ,KAAI;AAEnB,OAAO,MAAMC,SAAkB;IAC7BC,YAAYF,GAAGE,UAAU;IACzBC,UAAUH,GAAGI,QAAQ,CAACD,QAAQ;IAC9BE,cAAcL,GAAGK,YAAY;IAC7BC,WAAW,CAACC,GAAGC,IAAMR,GAAGI,QAAQ,CAACE,SAAS,CAACC,GAAGC;IAC9CC,OAAO,CAACC,MAAQV,GAAGI,QAAQ,CAACK,KAAK,CAACC,KAAK;YAAEC,WAAW;QAAK;IACzDC,MAAM,CAACL,IAAMP,GAAGI,QAAQ,CAACQ,IAAI,CAACL;AAChC,EAAC"}

693
node_modules/next/dist/esm/server/lib/patch-fetch.js generated vendored Normal file
View File

@@ -0,0 +1,693 @@
import { AppRenderSpan, NextNodeServerSpan } from './trace/constants';
import { getTracer, SpanKind } from './trace/tracer';
import { CACHE_ONE_YEAR, INFINITE_CACHE, NEXT_CACHE_TAG_MAX_ITEMS, NEXT_CACHE_TAG_MAX_LENGTH } from '../../lib/constants';
import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering';
import { makeHangingPromise } from '../dynamic-rendering-utils';
import { createDedupeFetch } from './dedupe-fetch';
import { CachedRouteKind, IncrementalCacheKind } from '../response-cache';
import { waitAtLeastOneReactRenderTask } from '../../lib/scheduler';
import { cloneResponse } from './clone-response';
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge';
export const NEXT_PATCH_SYMBOL = Symbol.for('next-patch');
function isFetchPatched() {
return globalThis[NEXT_PATCH_SYMBOL] === true;
}
export function validateRevalidate(revalidateVal, route) {
try {
let normalizedRevalidate = undefined;
if (revalidateVal === false) {
normalizedRevalidate = INFINITE_CACHE;
} else if (typeof revalidateVal === 'number' && !isNaN(revalidateVal) && revalidateVal > -1) {
normalizedRevalidate = revalidateVal;
} else if (typeof revalidateVal !== 'undefined') {
throw Object.defineProperty(new Error(`Invalid revalidate value "${revalidateVal}" on "${route}", must be a non-negative number or false`), "__NEXT_ERROR_CODE", {
value: "E179",
enumerable: false,
configurable: true
});
}
return normalizedRevalidate;
} catch (err) {
// handle client component error from attempting to check revalidate value
if (err instanceof Error && err.message.includes('Invalid revalidate')) {
throw err;
}
return undefined;
}
}
export function validateTags(tags, description) {
const validTags = [];
const invalidTags = [];
for(let i = 0; i < tags.length; i++){
const tag = tags[i];
if (typeof tag !== 'string') {
invalidTags.push({
tag,
reason: 'invalid type, must be a string'
});
} else if (tag.length > NEXT_CACHE_TAG_MAX_LENGTH) {
invalidTags.push({
tag,
reason: `exceeded max length of ${NEXT_CACHE_TAG_MAX_LENGTH}`
});
} else {
validTags.push(tag);
}
if (validTags.length > NEXT_CACHE_TAG_MAX_ITEMS) {
console.warn(`Warning: exceeded max tag count for ${description}, dropped tags:`, tags.slice(i).join(', '));
break;
}
}
if (invalidTags.length > 0) {
console.warn(`Warning: invalid tags passed to ${description}: `);
for (const { tag, reason } of invalidTags){
console.log(`tag: "${tag}" ${reason}`);
}
}
return validTags;
}
function trackFetchMetric(workStore, ctx) {
var _workStore_requestEndedState;
// If the static generation store is not available, we can't track the fetch
if (!workStore) return;
if ((_workStore_requestEndedState = workStore.requestEndedState) == null ? void 0 : _workStore_requestEndedState.ended) return;
const isDebugBuild = (!!process.env.NEXT_DEBUG_BUILD || process.env.NEXT_SSG_FETCH_METRICS === '1') && workStore.isStaticGeneration;
const isDevelopment = process.env.NODE_ENV === 'development';
if (// The only time we want to track fetch metrics outside of development is when
// we are performing a static generation & we are in debug mode.
!isDebugBuild && !isDevelopment) {
return;
}
workStore.fetchMetrics ??= [];
workStore.fetchMetrics.push({
...ctx,
end: performance.timeOrigin + performance.now(),
idx: workStore.nextFetchId || 0
});
}
export function createPatchedFetcher(originFetch, { workAsyncStorage, workUnitAsyncStorage }) {
// Create the patched fetch function. We don't set the type here, as it's
// verified as the return value of this function.
const patched = async (input, init)=>{
var _init_method, _init_next;
let url;
try {
url = new URL(input instanceof Request ? input.url : input);
url.username = '';
url.password = '';
} catch {
// Error caused by malformed URL should be handled by native fetch
url = undefined;
}
const fetchUrl = (url == null ? void 0 : url.href) ?? '';
const method = (init == null ? void 0 : (_init_method = init.method) == null ? void 0 : _init_method.toUpperCase()) || 'GET';
// Do create a new span trace for internal fetches in the
// non-verbose mode.
const isInternal = (init == null ? void 0 : (_init_next = init.next) == null ? void 0 : _init_next.internal) === true;
const hideSpan = process.env.NEXT_OTEL_FETCH_DISABLED === '1';
// We don't track fetch metrics for internal fetches
// so it's not critical that we have a start time, as it won't be recorded.
// This is to workaround a flaky issue where performance APIs might
// not be available and will require follow-up investigation.
const fetchStart = isInternal ? undefined : performance.timeOrigin + performance.now();
const workStore = workAsyncStorage.getStore();
const workUnitStore = workUnitAsyncStorage.getStore();
// During static generation we track cache reads so we can reason about when they fill
let cacheSignal = workUnitStore && workUnitStore.type === 'prerender' ? workUnitStore.cacheSignal : null;
if (cacheSignal) {
cacheSignal.beginRead();
}
const result = getTracer().trace(isInternal ? NextNodeServerSpan.internalFetch : AppRenderSpan.fetch, {
hideSpan,
kind: SpanKind.CLIENT,
spanName: [
'fetch',
method,
fetchUrl
].filter(Boolean).join(' '),
attributes: {
'http.url': fetchUrl,
'http.method': method,
'net.peer.name': url == null ? void 0 : url.hostname,
'net.peer.port': (url == null ? void 0 : url.port) || undefined
}
}, async ()=>{
var _getRequestMeta;
// If this is an internal fetch, we should not do any special treatment.
if (isInternal) {
return originFetch(input, init);
}
// If the workStore is not available, we can't do any
// special treatment of fetch, therefore fallback to the original
// fetch implementation.
if (!workStore) {
return originFetch(input, init);
}
// We should also fallback to the original fetch implementation if we
// are in draft mode, it does not constitute a static generation.
if (workStore.isDraftMode) {
return originFetch(input, init);
}
const isRequestInput = input && typeof input === 'object' && typeof input.method === 'string';
const getRequestMeta = (field)=>{
// If request input is present but init is not, retrieve from input first.
const value = init == null ? void 0 : init[field];
return value || (isRequestInput ? input[field] : null);
};
let finalRevalidate = undefined;
const getNextField = (field)=>{
var _init_next, _init_next1, _input_next;
return typeof (init == null ? void 0 : (_init_next = init.next) == null ? void 0 : _init_next[field]) !== 'undefined' ? init == null ? void 0 : (_init_next1 = init.next) == null ? void 0 : _init_next1[field] : isRequestInput ? (_input_next = input.next) == null ? void 0 : _input_next[field] : undefined;
};
// RequestInit doesn't keep extra fields e.g. next so it's
// only available if init is used separate
let currentFetchRevalidate = getNextField('revalidate');
const tags = validateTags(getNextField('tags') || [], `fetch ${input.toString()}`);
const revalidateStore = workUnitStore && (workUnitStore.type === 'cache' || workUnitStore.type === 'prerender' || workUnitStore.type === 'prerender-ppr' || workUnitStore.type === 'prerender-legacy') ? workUnitStore : undefined;
if (revalidateStore) {
if (Array.isArray(tags)) {
// Collect tags onto parent caches or parent prerenders.
const collectedTags = revalidateStore.tags ?? (revalidateStore.tags = []);
for (const tag of tags){
if (!collectedTags.includes(tag)) {
collectedTags.push(tag);
}
}
}
}
const implicitTags = !workUnitStore || workUnitStore.type === 'unstable-cache' ? [] : workUnitStore.implicitTags;
// Inside unstable-cache we treat it the same as force-no-store on the
// page.
const pageFetchCacheMode = workUnitStore && workUnitStore.type === 'unstable-cache' ? 'force-no-store' : workStore.fetchCache;
const isUsingNoStore = !!workStore.isUnstableNoStore;
let currentFetchCacheConfig = getRequestMeta('cache');
let cacheReason = '';
let cacheWarning;
if (typeof currentFetchCacheConfig === 'string' && typeof currentFetchRevalidate !== 'undefined') {
// If the revalidate value conflicts with the cache value, we should warn the user and unset the conflicting values.
const isConflictingRevalidate = // revalidate: 0 and cache: force-cache
currentFetchCacheConfig === 'force-cache' && currentFetchRevalidate === 0 || // revalidate: >0 or revalidate: false and cache: no-store
currentFetchCacheConfig === 'no-store' && (currentFetchRevalidate > 0 || currentFetchRevalidate === false);
if (isConflictingRevalidate) {
cacheWarning = `Specified "cache: ${currentFetchCacheConfig}" and "revalidate: ${currentFetchRevalidate}", only one should be specified.`;
currentFetchCacheConfig = undefined;
currentFetchRevalidate = undefined;
}
}
const hasExplicitFetchCacheOptOut = // fetch config itself signals not to cache
currentFetchCacheConfig === 'no-cache' || currentFetchCacheConfig === 'no-store' || // the fetch isn't explicitly caching and the segment level cache config signals not to cache
// note: `pageFetchCacheMode` is also set by being in an unstable_cache context.
pageFetchCacheMode === 'force-no-store' || pageFetchCacheMode === 'only-no-store';
// If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set,
// we shouldn't consider caching the fetch. This is because the `dynamic` cache
// is considered a "top-level" cache mode, whereas something like `fetchCache` is more
// fine-grained. Top-level modes are responsible for setting reasonable defaults for the
// other configurations.
const noFetchConfigAndForceDynamic = !pageFetchCacheMode && !currentFetchCacheConfig && !currentFetchRevalidate && workStore.forceDynamic;
if (// force-cache was specified without a revalidate value. We set the revalidate value to false
// which will signal the cache to not revalidate
currentFetchCacheConfig === 'force-cache' && typeof currentFetchRevalidate === 'undefined') {
currentFetchRevalidate = false;
} else if (// if we are inside of "use cache"/"unstable_cache"
// we shouldn't set the revalidate to 0 as it's overridden
// by the cache context
(workUnitStore == null ? void 0 : workUnitStore.type) !== 'cache' && (hasExplicitFetchCacheOptOut || noFetchConfigAndForceDynamic)) {
currentFetchRevalidate = 0;
}
if (currentFetchCacheConfig === 'no-cache' || currentFetchCacheConfig === 'no-store') {
cacheReason = `cache: ${currentFetchCacheConfig}`;
}
finalRevalidate = validateRevalidate(currentFetchRevalidate, workStore.route);
const _headers = getRequestMeta('headers');
const initHeaders = typeof (_headers == null ? void 0 : _headers.get) === 'function' ? _headers : new Headers(_headers || {});
const hasUnCacheableHeader = initHeaders.get('authorization') || initHeaders.get('cookie');
const isUnCacheableMethod = ![
'get',
'head'
].includes(((_getRequestMeta = getRequestMeta('method')) == null ? void 0 : _getRequestMeta.toLowerCase()) || 'get');
/**
* We automatically disable fetch caching under the following conditions:
* - Fetch cache configs are not set. Specifically:
* - A page fetch cache mode is not set (export const fetchCache=...)
* - A fetch cache mode is not set in the fetch call (fetch(url, { cache: ... }))
* or the fetch cache mode is set to 'default'
* - A fetch revalidate value is not set in the fetch call (fetch(url, { revalidate: ... }))
* - OR the fetch comes after a configuration that triggered dynamic rendering (e.g., reading cookies())
* and the fetch was considered uncacheable (e.g., POST method or has authorization headers)
*/ const hasNoExplicitCacheConfig = // eslint-disable-next-line eqeqeq
pageFetchCacheMode == undefined && // eslint-disable-next-line eqeqeq
(currentFetchCacheConfig == undefined || // when considering whether to opt into the default "no-cache" fetch semantics,
// a "default" cache config should be treated the same as no cache config
currentFetchCacheConfig === 'default') && // eslint-disable-next-line eqeqeq
currentFetchRevalidate == undefined;
const autoNoCache = // this condition is hit for null/undefined
// eslint-disable-next-line eqeqeq
hasNoExplicitCacheConfig && // we disable automatic no caching behavior during build time SSG so that we can still
// leverage the fetch cache between SSG workers
!workStore.isPrerendering || (hasUnCacheableHeader || isUnCacheableMethod) && revalidateStore && revalidateStore.revalidate === 0;
if (hasNoExplicitCacheConfig && workUnitStore !== undefined && workUnitStore.type === 'prerender') {
// If we have no cache config, and we're in Dynamic I/O prerendering, it'll be a dynamic call.
// We don't have to issue that dynamic call.
if (cacheSignal) {
cacheSignal.endRead();
cacheSignal = null;
}
return makeHangingPromise(workUnitStore.renderSignal, 'fetch()');
}
switch(pageFetchCacheMode){
case 'force-no-store':
{
cacheReason = 'fetchCache = force-no-store';
break;
}
case 'only-no-store':
{
if (currentFetchCacheConfig === 'force-cache' || typeof finalRevalidate !== 'undefined' && finalRevalidate > 0) {
throw Object.defineProperty(new Error(`cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'`), "__NEXT_ERROR_CODE", {
value: "E448",
enumerable: false,
configurable: true
});
}
cacheReason = 'fetchCache = only-no-store';
break;
}
case 'only-cache':
{
if (currentFetchCacheConfig === 'no-store') {
throw Object.defineProperty(new Error(`cache: 'no-store' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-cache'`), "__NEXT_ERROR_CODE", {
value: "E521",
enumerable: false,
configurable: true
});
}
break;
}
case 'force-cache':
{
if (typeof currentFetchRevalidate === 'undefined' || currentFetchRevalidate === 0) {
cacheReason = 'fetchCache = force-cache';
finalRevalidate = INFINITE_CACHE;
}
break;
}
default:
}
if (typeof finalRevalidate === 'undefined') {
if (pageFetchCacheMode === 'default-cache' && !isUsingNoStore) {
finalRevalidate = INFINITE_CACHE;
cacheReason = 'fetchCache = default-cache';
} else if (pageFetchCacheMode === 'default-no-store') {
finalRevalidate = 0;
cacheReason = 'fetchCache = default-no-store';
} else if (isUsingNoStore) {
finalRevalidate = 0;
cacheReason = 'noStore call';
} else if (autoNoCache) {
finalRevalidate = 0;
cacheReason = 'auto no cache';
} else {
// TODO: should we consider this case an invariant?
cacheReason = 'auto cache';
finalRevalidate = revalidateStore ? revalidateStore.revalidate : INFINITE_CACHE;
}
} else if (!cacheReason) {
cacheReason = `revalidate: ${finalRevalidate}`;
}
if (// when force static is configured we don't bail from
// `revalidate: 0` values
!(workStore.forceStatic && finalRevalidate === 0) && // we don't consider autoNoCache to switch to dynamic for ISR
!autoNoCache && // If the revalidate value isn't currently set or the value is less
// than the current revalidate value, we should update the revalidate
// value.
revalidateStore && finalRevalidate < revalidateStore.revalidate) {
// If we were setting the revalidate value to 0, we should try to
// postpone instead first.
if (finalRevalidate === 0) {
if (workUnitStore && workUnitStore.type === 'prerender') {
if (cacheSignal) {
cacheSignal.endRead();
cacheSignal = null;
}
return makeHangingPromise(workUnitStore.renderSignal, 'fetch()');
} else {
markCurrentScopeAsDynamic(workStore, workUnitStore, `revalidate: 0 fetch ${input} ${workStore.route}`);
}
}
// We only want to set the revalidate store's revalidate time if it
// was explicitly set for the fetch call, i.e. currentFetchRevalidate.
if (revalidateStore && currentFetchRevalidate === finalRevalidate) {
revalidateStore.revalidate = finalRevalidate;
}
}
const isCacheableRevalidate = typeof finalRevalidate === 'number' && finalRevalidate > 0;
let cacheKey;
const { incrementalCache } = workStore;
const useCacheOrRequestStore = (workUnitStore == null ? void 0 : workUnitStore.type) === 'request' || (workUnitStore == null ? void 0 : workUnitStore.type) === 'cache' ? workUnitStore : undefined;
if (incrementalCache && (isCacheableRevalidate || (useCacheOrRequestStore == null ? void 0 : useCacheOrRequestStore.serverComponentsHmrCache))) {
try {
cacheKey = await incrementalCache.generateCacheKey(fetchUrl, isRequestInput ? input : init);
} catch (err) {
console.error(`Failed to generate cache key for`, input);
}
}
const fetchIdx = workStore.nextFetchId ?? 1;
workStore.nextFetchId = fetchIdx + 1;
let handleUnlock = ()=>Promise.resolve();
const doOriginalFetch = async (isStale, cacheReasonOverride)=>{
const requestInputFields = [
'cache',
'credentials',
'headers',
'integrity',
'keepalive',
'method',
'mode',
'redirect',
'referrer',
'referrerPolicy',
'window',
'duplex',
// don't pass through signal when revalidating
...isStale ? [] : [
'signal'
]
];
if (isRequestInput) {
const reqInput = input;
const reqOptions = {
body: reqInput._ogBody || reqInput.body
};
for (const field of requestInputFields){
// @ts-expect-error custom fields
reqOptions[field] = reqInput[field];
}
input = new Request(reqInput.url, reqOptions);
} else if (init) {
const { _ogBody, body, signal, ...otherInput } = init;
init = {
...otherInput,
body: _ogBody || body,
signal: isStale ? undefined : signal
};
}
// add metadata to init without editing the original
const clonedInit = {
...init,
next: {
...init == null ? void 0 : init.next,
fetchType: 'origin',
fetchIdx
}
};
return originFetch(input, clonedInit).then(async (res)=>{
if (!isStale && fetchStart) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
cacheReason: cacheReasonOverride || cacheReason,
cacheStatus: finalRevalidate === 0 || cacheReasonOverride ? 'skip' : 'miss',
cacheWarning,
status: res.status,
method: clonedInit.method || 'GET'
});
}
if (res.status === 200 && incrementalCache && cacheKey && (isCacheableRevalidate || (useCacheOrRequestStore == null ? void 0 : useCacheOrRequestStore.serverComponentsHmrCache))) {
const normalizedRevalidate = finalRevalidate >= INFINITE_CACHE ? CACHE_ONE_YEAR : finalRevalidate;
if (workUnitStore && workUnitStore.type === 'prerender') {
// We are prerendering at build time or revalidate time with dynamicIO so we need to
// buffer the response so we can guarantee it can be read in a microtask
const bodyBuffer = await res.arrayBuffer();
const fetchedData = {
headers: Object.fromEntries(res.headers.entries()),
body: Buffer.from(bodyBuffer).toString('base64'),
status: res.status,
url: res.url
};
// We can skip checking the serverComponentsHmrCache because we aren't in
// dev mode.
await incrementalCache.set(cacheKey, {
kind: CachedRouteKind.FETCH,
data: fetchedData,
revalidate: normalizedRevalidate
}, {
fetchCache: true,
fetchUrl,
fetchIdx,
tags
});
await handleUnlock();
// We return a new Response to the caller.
return new Response(bodyBuffer, {
headers: res.headers,
status: res.status,
statusText: res.statusText
});
} else {
// We're cloning the response using this utility because there
// exists a bug in the undici library around response cloning.
// See the following pull request for more details:
// https://github.com/vercel/next.js/pull/73274
const [cloned1, cloned2] = cloneResponse(res);
// We are dynamically rendering including dev mode. We want to return
// the response to the caller as soon as possible because it might stream
// over a very long time.
cloned1.arrayBuffer().then(async (arrayBuffer)=>{
var _useCacheOrRequestStore_serverComponentsHmrCache;
const bodyBuffer = Buffer.from(arrayBuffer);
const fetchedData = {
headers: Object.fromEntries(cloned1.headers.entries()),
body: bodyBuffer.toString('base64'),
status: cloned1.status,
url: cloned1.url
};
useCacheOrRequestStore == null ? void 0 : (_useCacheOrRequestStore_serverComponentsHmrCache = useCacheOrRequestStore.serverComponentsHmrCache) == null ? void 0 : _useCacheOrRequestStore_serverComponentsHmrCache.set(cacheKey, fetchedData);
if (isCacheableRevalidate) {
await incrementalCache.set(cacheKey, {
kind: CachedRouteKind.FETCH,
data: fetchedData,
revalidate: normalizedRevalidate
}, {
fetchCache: true,
fetchUrl,
fetchIdx,
tags
});
}
}).catch((error)=>console.warn(`Failed to set fetch cache`, input, error)).finally(handleUnlock);
return cloned2;
}
}
// we had response that we determined shouldn't be cached so we return it
// and don't cache it. This also needs to unlock the cache lock we acquired.
await handleUnlock();
return res;
}).catch((error)=>{
handleUnlock();
throw error;
});
};
let cacheReasonOverride;
let isForegroundRevalidate = false;
let isHmrRefreshCache = false;
if (cacheKey && incrementalCache) {
let cachedFetchData;
if ((useCacheOrRequestStore == null ? void 0 : useCacheOrRequestStore.isHmrRefresh) && useCacheOrRequestStore.serverComponentsHmrCache) {
cachedFetchData = useCacheOrRequestStore.serverComponentsHmrCache.get(cacheKey);
isHmrRefreshCache = true;
}
if (isCacheableRevalidate && !cachedFetchData) {
handleUnlock = await incrementalCache.lock(cacheKey);
const entry = workStore.isOnDemandRevalidate ? null : await incrementalCache.get(cacheKey, {
kind: IncrementalCacheKind.FETCH,
revalidate: finalRevalidate,
fetchUrl,
fetchIdx,
tags,
softTags: implicitTags
});
if (hasNoExplicitCacheConfig) {
// We sometimes use the cache to dedupe fetches that do not specify a cache configuration
// In these cases we want to make sure we still exclude them from prerenders if dynamicIO is on
// so we introduce an artificial Task boundary here.
if (workUnitStore && workUnitStore.type === 'prerender') {
await waitAtLeastOneReactRenderTask();
}
}
if (entry) {
await handleUnlock();
} else {
// in dev, incremental cache response will be null in case the browser adds `cache-control: no-cache` in the request headers
cacheReasonOverride = 'cache-control: no-cache (hard refresh)';
}
if ((entry == null ? void 0 : entry.value) && entry.value.kind === CachedRouteKind.FETCH) {
// when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data
if (workStore.isRevalidate && entry.isStale) {
isForegroundRevalidate = true;
} else {
if (entry.isStale) {
workStore.pendingRevalidates ??= {};
if (!workStore.pendingRevalidates[cacheKey]) {
const pendingRevalidate = doOriginalFetch(true).then(async (response)=>({
body: await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText
})).finally(()=>{
workStore.pendingRevalidates ??= {};
delete workStore.pendingRevalidates[cacheKey || ''];
});
// Attach the empty catch here so we don't get a "unhandled
// promise rejection" warning.
pendingRevalidate.catch(console.error);
workStore.pendingRevalidates[cacheKey] = pendingRevalidate;
}
}
cachedFetchData = entry.value.data;
}
}
}
if (cachedFetchData) {
if (fetchStart) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
cacheReason,
cacheStatus: isHmrRefreshCache ? 'hmr' : 'hit',
cacheWarning,
status: cachedFetchData.status || 200,
method: (init == null ? void 0 : init.method) || 'GET'
});
}
const response = new Response(Buffer.from(cachedFetchData.body, 'base64'), {
headers: cachedFetchData.headers,
status: cachedFetchData.status
});
Object.defineProperty(response, 'url', {
value: cachedFetchData.url
});
return response;
}
}
if (workStore.isStaticGeneration && init && typeof init === 'object') {
const { cache } = init;
// Delete `cache` property as Cloudflare Workers will throw an error
if (isEdgeRuntime) delete init.cache;
if (cache === 'no-store') {
// If enabled, we should bail out of static generation.
if (workUnitStore && workUnitStore.type === 'prerender') {
if (cacheSignal) {
cacheSignal.endRead();
cacheSignal = null;
}
return makeHangingPromise(workUnitStore.renderSignal, 'fetch()');
} else {
markCurrentScopeAsDynamic(workStore, workUnitStore, `no-store fetch ${input} ${workStore.route}`);
}
}
const hasNextConfig = 'next' in init;
const { next = {} } = init;
if (typeof next.revalidate === 'number' && revalidateStore && next.revalidate < revalidateStore.revalidate) {
if (next.revalidate === 0) {
// If enabled, we should bail out of static generation.
if (workUnitStore && workUnitStore.type === 'prerender') {
return makeHangingPromise(workUnitStore.renderSignal, 'fetch()');
} else {
markCurrentScopeAsDynamic(workStore, workUnitStore, `revalidate: 0 fetch ${input} ${workStore.route}`);
}
}
if (!workStore.forceStatic || next.revalidate !== 0) {
revalidateStore.revalidate = next.revalidate;
}
}
if (hasNextConfig) delete init.next;
}
// if we are revalidating the whole page via time or on-demand and
// the fetch cache entry is stale we should still de-dupe the
// origin hit if it's a cache-able entry
if (cacheKey && isForegroundRevalidate) {
const pendingRevalidateKey = cacheKey;
workStore.pendingRevalidates ??= {};
let pendingRevalidate = workStore.pendingRevalidates[pendingRevalidateKey];
if (pendingRevalidate) {
const revalidatedResult = await pendingRevalidate;
return new Response(revalidatedResult.body, {
headers: revalidatedResult.headers,
status: revalidatedResult.status,
statusText: revalidatedResult.statusText
});
}
// We used to just resolve the Response and clone it however for
// static generation with dynamicIO we need the response to be able to
// be resolved in a microtask and cloning the response will never have
// a body that can resolve in a microtask in node (as observed through
// experimentation) So instead we await the body and then when it is
// available we construct manually cloned Response objects with the
// body as an ArrayBuffer. This will be resolvable in a microtask
// making it compatible with dynamicIO.
const pendingResponse = doOriginalFetch(true, cacheReasonOverride)// We're cloning the response using this utility because there
// exists a bug in the undici library around response cloning.
// See the following pull request for more details:
// https://github.com/vercel/next.js/pull/73274
.then(cloneResponse);
pendingRevalidate = pendingResponse.then(async (responses)=>{
const response = responses[0];
return {
body: await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText
};
}).finally(()=>{
var _workStore_pendingRevalidates;
// If the pending revalidate is not present in the store, then
// we have nothing to delete.
if (!((_workStore_pendingRevalidates = workStore.pendingRevalidates) == null ? void 0 : _workStore_pendingRevalidates[pendingRevalidateKey])) {
return;
}
delete workStore.pendingRevalidates[pendingRevalidateKey];
});
// Attach the empty catch here so we don't get a "unhandled promise
// rejection" warning
pendingRevalidate.catch(()=>{});
workStore.pendingRevalidates[pendingRevalidateKey] = pendingRevalidate;
return pendingResponse.then((responses)=>responses[1]);
} else {
return doOriginalFetch(false, cacheReasonOverride);
}
});
if (cacheSignal) {
try {
return await result;
} finally{
if (cacheSignal) {
cacheSignal.endRead();
}
}
}
return result;
};
// Attach the necessary properties to the patched fetch function.
// We don't use this to determine if the fetch function has been patched,
// but for external consumers to determine if the fetch function has been
// patched.
patched.__nextPatched = true;
patched.__nextGetStaticStore = ()=>workAsyncStorage;
patched._nextOriginalFetch = originFetch;
globalThis[NEXT_PATCH_SYMBOL] = true;
return patched;
}
// we patch fetch to collect cache information used for
// determining if a page is static or not
export function patchFetch(options) {
// If we've already patched fetch, we should not patch it again.
if (isFetchPatched()) return;
// Grab the original fetch function. We'll attach this so we can use it in
// the patched fetch function.
const original = createDedupeFetch(globalThis.fetch);
// Set the global fetch to the patched fetch.
globalThis.fetch = createPatchedFetcher(original, options);
}
//# sourceMappingURL=patch-fetch.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,34 @@
import { getRequestMeta } from '../request-meta';
/**
* Ensure cookies set in middleware are merged and not overridden by API
* routes/getServerSideProps.
*
* @param req Incoming request
* @param res Outgoing response
*/ export function patchSetHeaderWithCookieSupport(req, res) {
const setHeader = res.setHeader.bind(res);
res.setHeader = (name, value)=>{
// When renders /_error after page is failed, it could attempt to set
// headers after headers.
if ('headersSent' in res && res.headersSent) {
return res;
}
if (name.toLowerCase() === 'set-cookie') {
const middlewareValue = getRequestMeta(req, 'middlewareCookie');
if (!middlewareValue || !Array.isArray(value) || !value.every((item, idx)=>item === middlewareValue[idx])) {
value = [
// TODO: (wyattjoh) find out why this is called multiple times resulting in duplicate cookies being added
...new Set([
...middlewareValue || [],
...typeof value === 'string' ? [
value
] : Array.isArray(value) ? value : []
])
];
}
}
return setHeader(name, value);
};
}
//# sourceMappingURL=patch-set-header.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/patch-set-header.ts"],"sourcesContent":["import { getRequestMeta, type NextIncomingMessage } from '../request-meta'\n\ntype PatchableResponse = {\n setHeader(key: string, value: string | string[]): PatchableResponse\n}\n\n/**\n * Ensure cookies set in middleware are merged and not overridden by API\n * routes/getServerSideProps.\n *\n * @param req Incoming request\n * @param res Outgoing response\n */\nexport function patchSetHeaderWithCookieSupport(\n req: NextIncomingMessage,\n res: PatchableResponse\n) {\n const setHeader = res.setHeader.bind(res)\n res.setHeader = (\n name: string,\n value: string | string[]\n ): PatchableResponse => {\n // When renders /_error after page is failed, it could attempt to set\n // headers after headers.\n if ('headersSent' in res && res.headersSent) {\n return res\n }\n\n if (name.toLowerCase() === 'set-cookie') {\n const middlewareValue = getRequestMeta(req, 'middlewareCookie')\n\n if (\n !middlewareValue ||\n !Array.isArray(value) ||\n !value.every((item, idx) => item === middlewareValue[idx])\n ) {\n value = [\n // TODO: (wyattjoh) find out why this is called multiple times resulting in duplicate cookies being added\n ...new Set([\n ...(middlewareValue || []),\n ...(typeof value === 'string'\n ? [value]\n : Array.isArray(value)\n ? value\n : []),\n ]),\n ]\n }\n }\n\n return setHeader(name, value)\n }\n}\n"],"names":["getRequestMeta","patchSetHeaderWithCookieSupport","req","res","setHeader","bind","name","value","headersSent","toLowerCase","middlewareValue","Array","isArray","every","item","idx","Set"],"mappings":"AAAA,SAASA,cAAc,QAAkC,kBAAiB;AAM1E;;;;;;CAMC,GACD,OAAO,SAASC,gCACdC,GAAwB,EACxBC,GAAsB;IAEtB,MAAMC,YAAYD,IAAIC,SAAS,CAACC,IAAI,CAACF;IACrCA,IAAIC,SAAS,GAAG,CACdE,MACAC;QAEA,qEAAqE;QACrE,yBAAyB;QACzB,IAAI,iBAAiBJ,OAAOA,IAAIK,WAAW,EAAE;YAC3C,OAAOL;QACT;QAEA,IAAIG,KAAKG,WAAW,OAAO,cAAc;YACvC,MAAMC,kBAAkBV,eAAeE,KAAK;YAE5C,IACE,CAACQ,mBACD,CAACC,MAAMC,OAAO,CAACL,UACf,CAACA,MAAMM,KAAK,CAAC,CAACC,MAAMC,MAAQD,SAASJ,eAAe,CAACK,IAAI,GACzD;gBACAR,QAAQ;oBACN,yGAAyG;uBACtG,IAAIS,IAAI;2BACLN,mBAAmB,EAAE;2BACrB,OAAOH,UAAU,WACjB;4BAACA;yBAAM,GACPI,MAAMC,OAAO,CAACL,SACZA,QACA,EAAE;qBACT;iBACF;YACH;QACF;QAEA,OAAOH,UAAUE,MAAMC;IACzB;AACF"}

86
node_modules/next/dist/esm/server/lib/render-server.js generated vendored Normal file
View File

@@ -0,0 +1,86 @@
import next from '../next';
let initializations = {};
let sandboxContext;
if (process.env.NODE_ENV !== 'production') {
sandboxContext = require('../web/sandbox/context');
}
export function clearAllModuleContexts() {
return sandboxContext == null ? void 0 : sandboxContext.clearAllModuleContexts();
}
export function clearModuleContext(target) {
return sandboxContext == null ? void 0 : sandboxContext.clearModuleContext(target);
}
export async function getServerField(dir, field) {
const initialization = await initializations[dir];
if (!initialization) {
throw Object.defineProperty(new Error('Invariant cant propagate server field, no app initialized'), "__NEXT_ERROR_CODE", {
value: "E116",
enumerable: false,
configurable: true
});
}
const { server } = initialization;
let wrappedServer = server['server']// NextServer.server is private
;
return wrappedServer[field];
}
export async function propagateServerField(dir, field, value) {
const initialization = await initializations[dir];
if (!initialization) {
throw Object.defineProperty(new Error('Invariant cant propagate server field, no app initialized'), "__NEXT_ERROR_CODE", {
value: "E116",
enumerable: false,
configurable: true
});
}
const { server } = initialization;
let wrappedServer = server['server'];
const _field = field;
if (wrappedServer) {
if (typeof wrappedServer[_field] === 'function') {
// @ts-expect-error
await wrappedServer[_field].apply(wrappedServer, Array.isArray(value) ? value : []);
} else {
// @ts-expect-error
wrappedServer[_field] = value;
}
}
}
async function initializeImpl(opts) {
const type = process.env.__NEXT_PRIVATE_RENDER_WORKER;
if (type) {
process.title = 'next-render-worker-' + type;
}
let requestHandler;
let upgradeHandler;
const server = next({
...opts,
hostname: opts.hostname || 'localhost',
customServer: false,
httpServer: opts.server,
port: opts.port
})// should return a NextServer when `customServer: false`
;
requestHandler = server.getRequestHandler();
upgradeHandler = server.getUpgradeHandler();
await server.prepare(opts.serverFields);
return {
requestHandler,
upgradeHandler,
server,
closeUpgraded () {
var _opts_bundlerService;
(_opts_bundlerService = opts.bundlerService) == null ? void 0 : _opts_bundlerService.close();
}
};
}
export async function initialize(opts) {
// if we already setup the server return as we only need to do
// this on first worker boot
if (initializations[opts.dir]) {
return initializations[opts.dir];
}
return initializations[opts.dir] = initializeImpl(opts);
}
//# sourceMappingURL=render-server.js.map

File diff suppressed because one or more lines are too long

542
node_modules/next/dist/esm/server/lib/router-server.js generated vendored Normal file
View File

@@ -0,0 +1,542 @@
// this must come first as it includes require hooks
// This is required before other imports to ensure the require hook is setup.
import '../node-environment';
import '../require-hook';
import url from 'url';
import path from 'path';
import loadConfig from '../config';
import { serveStatic } from '../serve-static';
import setupDebug from 'next/dist/compiled/debug';
import * as Log from '../../build/output/log';
import { DecodeError } from '../../shared/lib/utils';
import { findPagesDir } from '../../lib/find-pages-dir';
import { setupFsCheck } from './router-utils/filesystem';
import { proxyRequest } from './router-utils/proxy-request';
import { isAbortError, pipeToNodeResponse } from '../pipe-readable';
import { getResolveRoutes } from './router-utils/resolve-routes';
import { addRequestMeta, getRequestMeta } from '../request-meta';
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix';
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix';
import setupCompression from 'next/dist/compiled/compression';
import { NoFallbackError } from '../base-server';
import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request';
import { isPostpone } from './router-utils/is-postpone';
import { parseUrl as parseUrlUtil } from '../../shared/lib/router/utils/parse-url';
import { PHASE_PRODUCTION_SERVER, PHASE_DEVELOPMENT_SERVER, UNDERSCORE_NOT_FOUND_ROUTE } from '../../shared/lib/constants';
import { RedirectStatusCode } from '../../client/components/redirect-status-code';
import { DevBundlerService } from './dev-bundler-service';
import { trace } from '../../trace';
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash';
import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-pathname-info';
import { getHostname } from '../../shared/lib/get-hostname';
import { detectDomainLocale } from '../../shared/lib/i18n/detect-domain-locale';
import { MockedResponse } from './mock-request';
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../dev/hot-reloader-types';
import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix';
import { NEXT_PATCH_SYMBOL } from './patch-fetch';
import { filterInternalHeaders } from './server-ipc/utils';
import { blockCrossSite } from './router-utils/block-cross-site';
const debug = setupDebug('next:router-server:main');
const isNextFont = (pathname)=>pathname && /\/media\/[^/]+\.(woff|woff2|eot|ttf|otf)$/.test(pathname);
const requestHandlers = {};
export async function initialize(opts) {
if (!process.env.NODE_ENV) {
// @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production';
}
const config = await loadConfig(opts.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER, opts.dir, {
silent: false
});
let compress;
if ((config == null ? void 0 : config.compress) !== false) {
compress = setupCompression();
}
const fsChecker = await setupFsCheck({
dev: opts.dev,
dir: opts.dir,
config,
minimalMode: opts.minimalMode
});
const renderServer = {};
let developmentBundler;
let devBundlerService;
let originalFetch = globalThis.fetch;
if (opts.dev) {
const { Telemetry } = require('../../telemetry/storage');
const telemetry = new Telemetry({
distDir: path.join(opts.dir, config.distDir)
});
const { pagesDir, appDir } = findPagesDir(opts.dir);
const { setupDevBundler } = require('./router-utils/setup-dev-bundler');
const resetFetch = ()=>{
globalThis.fetch = originalFetch;
globalThis[NEXT_PATCH_SYMBOL] = false;
};
const setupDevBundlerSpan = opts.startServerSpan ? opts.startServerSpan.traceChild('setup-dev-bundler') : trace('setup-dev-bundler');
developmentBundler = await setupDevBundlerSpan.traceAsyncFn(()=>setupDevBundler({
// Passed here but the initialization of this object happens below, doing the initialization before the setupDev call breaks.
renderServer,
appDir,
pagesDir,
telemetry,
fsChecker,
dir: opts.dir,
nextConfig: config,
isCustomServer: opts.customServer,
turbo: !!process.env.TURBOPACK,
port: opts.port,
onDevServerCleanup: opts.onDevServerCleanup,
resetFetch
}));
devBundlerService = new DevBundlerService(developmentBundler, // The request handler is assigned below, this allows us to create a lazy
// reference to it.
(req, res)=>{
return requestHandlers[opts.dir](req, res);
});
}
renderServer.instance = require('./render-server');
const requestHandlerImpl = async (req, res)=>{
// internal headers should not be honored by the request handler
if (!process.env.NEXT_PRIVATE_TEST_HEADERS) {
filterInternalHeaders(req.headers);
}
if (!opts.minimalMode && config.i18n && config.i18n.localeDetection !== false) {
var _this;
const urlParts = (req.url || '').split('?', 1);
let urlNoQuery = urlParts[0] || '';
if (config.basePath) {
urlNoQuery = removePathPrefix(urlNoQuery, config.basePath);
}
const pathnameInfo = getNextPathnameInfo(urlNoQuery, {
nextConfig: config
});
const domainLocale = detectDomainLocale(config.i18n.domains, getHostname({
hostname: urlNoQuery
}, req.headers));
const defaultLocale = (domainLocale == null ? void 0 : domainLocale.defaultLocale) || config.i18n.defaultLocale;
const { getLocaleRedirect } = require('../../shared/lib/i18n/get-locale-redirect');
const parsedUrl = parseUrlUtil((_this = req.url || '') == null ? void 0 : _this.replace(/^\/+/, '/'));
const redirect = getLocaleRedirect({
defaultLocale,
domainLocale,
headers: req.headers,
nextConfig: config,
pathLocale: pathnameInfo.locale,
urlParsed: {
...parsedUrl,
pathname: pathnameInfo.locale ? `/${pathnameInfo.locale}${urlNoQuery}` : urlNoQuery
}
});
if (redirect) {
res.setHeader('Location', redirect);
res.statusCode = RedirectStatusCode.TemporaryRedirect;
res.end(redirect);
return;
}
}
if (compress) {
// @ts-expect-error not express req/res
compress(req, res, ()=>{});
}
req.on('error', (_err)=>{
// TODO: log socket errors?
});
res.on('error', (_err)=>{
// TODO: log socket errors?
});
const invokedOutputs = new Set();
async function invokeRender(parsedUrl, invokePath, handleIndex, additionalRequestMeta) {
var _fsChecker_getMiddlewareMatchers;
// invokeRender expects /api routes to not be locale prefixed
// so normalize here before continuing
if (config.i18n && removePathPrefix(invokePath, config.basePath).startsWith(`/${getRequestMeta(req, 'locale')}/api`)) {
invokePath = fsChecker.handleLocale(removePathPrefix(invokePath, config.basePath)).pathname;
}
if (req.headers['x-nextjs-data'] && ((_fsChecker_getMiddlewareMatchers = fsChecker.getMiddlewareMatchers()) == null ? void 0 : _fsChecker_getMiddlewareMatchers.length) && removePathPrefix(invokePath, config.basePath) === '/404') {
res.setHeader('x-nextjs-matched-path', parsedUrl.pathname || '');
res.statusCode = 404;
res.setHeader('content-type', 'application/json');
res.end('{}');
return null;
}
if (!handlers) {
throw Object.defineProperty(new Error('Failed to initialize render server'), "__NEXT_ERROR_CODE", {
value: "E90",
enumerable: false,
configurable: true
});
}
addRequestMeta(req, 'invokePath', invokePath);
addRequestMeta(req, 'invokeQuery', parsedUrl.query);
addRequestMeta(req, 'middlewareInvoke', false);
for(const key in additionalRequestMeta || {}){
addRequestMeta(req, key, additionalRequestMeta[key]);
}
debug('invokeRender', req.url, req.headers);
try {
var _renderServer_instance;
const initResult = await (renderServer == null ? void 0 : (_renderServer_instance = renderServer.instance) == null ? void 0 : _renderServer_instance.initialize(renderServerOpts));
try {
await (initResult == null ? void 0 : initResult.requestHandler(req, res));
} catch (err) {
if (err instanceof NoFallbackError) {
// eslint-disable-next-line
await handleRequest(handleIndex + 1);
return;
}
throw err;
}
return;
} 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 (isAbortError(e)) {
return;
}
throw e;
}
}
const handleRequest = async (handleIndex)=>{
if (handleIndex > 5) {
throw Object.defineProperty(new Error(`Attempted to handle request too many times ${req.url}`), "__NEXT_ERROR_CODE", {
value: "E283",
enumerable: false,
configurable: true
});
}
// handle hot-reloader first
if (developmentBundler) {
if (blockCrossSite(req, res, config.allowedDevOrigins, opts.hostname)) {
return;
}
const origUrl = req.url || '/';
if (config.basePath && pathHasPrefix(origUrl, config.basePath)) {
req.url = removePathPrefix(origUrl, config.basePath);
}
const parsedUrl = url.parse(req.url || '/');
const hotReloaderResult = await developmentBundler.hotReloader.run(req, res, parsedUrl);
if (hotReloaderResult.finished) {
return hotReloaderResult;
}
req.url = origUrl;
}
const { finished, parsedUrl, statusCode, resHeaders, bodyStream, matchedOutput } = await resolveRoutes({
req,
res,
isUpgradeReq: false,
signal: signalFromNodeResponse(res),
invokedOutputs
});
if (res.closed || res.finished) {
return;
}
if (developmentBundler && (matchedOutput == null ? void 0 : matchedOutput.type) === 'devVirtualFsItem') {
const origUrl = req.url || '/';
if (config.basePath && pathHasPrefix(origUrl, config.basePath)) {
req.url = removePathPrefix(origUrl, config.basePath);
}
if (resHeaders) {
for (const key of Object.keys(resHeaders)){
res.setHeader(key, resHeaders[key]);
}
}
const result = await developmentBundler.requestHandler(req, res);
if (result.finished) {
return;
}
// TODO: throw invariant if we resolved to this but it wasn't handled?
req.url = origUrl;
}
debug('requestHandler!', req.url, {
matchedOutput,
statusCode,
resHeaders,
bodyStream: !!bodyStream,
parsedUrl: {
pathname: parsedUrl.pathname,
query: parsedUrl.query
},
finished
});
// apply any response headers from routing
for (const key of Object.keys(resHeaders || {})){
res.setHeader(key, resHeaders[key]);
}
// handle redirect
if (!bodyStream && statusCode && statusCode > 300 && statusCode < 400) {
const destination = url.format(parsedUrl);
res.statusCode = statusCode;
res.setHeader('location', destination);
if (statusCode === RedirectStatusCode.PermanentRedirect) {
res.setHeader('Refresh', `0;url=${destination}`);
}
return res.end(destination);
}
// handle middleware body response
if (bodyStream) {
res.statusCode = statusCode || 200;
return await pipeToNodeResponse(bodyStream, res);
}
if (finished && parsedUrl.protocol) {
var _getRequestMeta;
return await proxyRequest(req, res, parsedUrl, undefined, (_getRequestMeta = getRequestMeta(req, 'clonableBody')) == null ? void 0 : _getRequestMeta.cloneBodyStream(), config.experimental.proxyTimeout);
}
if ((matchedOutput == null ? void 0 : matchedOutput.fsPath) && matchedOutput.itemPath) {
if (opts.dev && (fsChecker.appFiles.has(matchedOutput.itemPath) || fsChecker.pageFiles.has(matchedOutput.itemPath))) {
res.statusCode = 500;
const message = `A conflicting public file and page file was found for path ${matchedOutput.itemPath} https://nextjs.org/docs/messages/conflicting-public-file-page`;
await invokeRender(parsedUrl, '/_error', handleIndex, {
invokeStatus: 500,
invokeError: Object.defineProperty(new Error(message), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
})
});
Log.error(message);
return;
}
if (!res.getHeader('cache-control') && matchedOutput.type === 'nextStaticFolder') {
if (opts.dev && !isNextFont(parsedUrl.pathname)) {
res.setHeader('Cache-Control', 'no-store, must-revalidate');
} else {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
}
if (!(req.method === 'GET' || req.method === 'HEAD')) {
res.setHeader('Allow', [
'GET',
'HEAD'
]);
res.statusCode = 405;
return await invokeRender(url.parse('/405', true), '/405', handleIndex, {
invokeStatus: 405
});
}
try {
return await serveStatic(req, res, matchedOutput.itemPath, {
root: matchedOutput.itemsRoot,
// Ensures that etags are not generated for static files when disabled.
etag: config.generateEtags
});
} catch (err) {
/**
* Hardcoded every possible error status code that could be thrown by "serveStatic" method
* This is done by searching "this.error" inside "send" module's source code:
* https://github.com/pillarjs/send/blob/master/index.js
* https://github.com/pillarjs/send/blob/develop/index.js
*/ const POSSIBLE_ERROR_CODE_FROM_SERVE_STATIC = new Set([
// send module will throw 500 when header is already sent or fs.stat error happens
// https://github.com/pillarjs/send/blob/53f0ab476145670a9bdd3dc722ab2fdc8d358fc6/index.js#L392
// Note: we will use Next.js built-in 500 page to handle 500 errors
// 500,
// send module will throw 404 when file is missing
// https://github.com/pillarjs/send/blob/53f0ab476145670a9bdd3dc722ab2fdc8d358fc6/index.js#L421
// Note: we will use Next.js built-in 404 page to handle 404 errors
// 404,
// send module will throw 403 when redirecting to a directory without enabling directory listing
// https://github.com/pillarjs/send/blob/53f0ab476145670a9bdd3dc722ab2fdc8d358fc6/index.js#L484
// Note: Next.js throws a different error (without status code) for directory listing
// 403,
// send module will throw 400 when fails to normalize the path
// https://github.com/pillarjs/send/blob/53f0ab476145670a9bdd3dc722ab2fdc8d358fc6/index.js#L520
400,
// send module will throw 412 with conditional GET request
// https://github.com/pillarjs/send/blob/53f0ab476145670a9bdd3dc722ab2fdc8d358fc6/index.js#L632
412,
// send module will throw 416 when range is not satisfiable
// https://github.com/pillarjs/send/blob/53f0ab476145670a9bdd3dc722ab2fdc8d358fc6/index.js#L669
416
]);
let validErrorStatus = POSSIBLE_ERROR_CODE_FROM_SERVE_STATIC.has(err.statusCode);
// normalize non-allowed status codes
if (!validErrorStatus) {
;
err.statusCode = 400;
}
if (typeof err.statusCode === 'number') {
const invokePath = `/${err.statusCode}`;
const invokeStatus = err.statusCode;
res.statusCode = err.statusCode;
return await invokeRender(url.parse(invokePath, true), invokePath, handleIndex, {
invokeStatus
});
}
throw err;
}
}
if (matchedOutput) {
invokedOutputs.add(matchedOutput.itemPath);
return await invokeRender(parsedUrl, parsedUrl.pathname || '/', handleIndex, {
invokeOutput: matchedOutput.itemPath
});
}
// 404 case
res.setHeader('Cache-Control', 'private, no-cache, no-store, max-age=0, must-revalidate');
// Short-circuit favicon.ico serving so that the 404 page doesn't get built as favicon is requested by the browser when loading any route.
if (opts.dev && !matchedOutput && parsedUrl.pathname === '/favicon.ico') {
res.statusCode = 404;
res.end('');
return null;
}
const appNotFound = opts.dev ? developmentBundler == null ? void 0 : developmentBundler.serverFields.hasAppNotFound : await fsChecker.getItem(UNDERSCORE_NOT_FOUND_ROUTE);
res.statusCode = 404;
if (appNotFound) {
return await invokeRender(parsedUrl, UNDERSCORE_NOT_FOUND_ROUTE, handleIndex, {
invokeStatus: 404
});
}
await invokeRender(parsedUrl, '/404', handleIndex, {
invokeStatus: 404
});
};
try {
await handleRequest(0);
} catch (err) {
try {
let invokePath = '/500';
let invokeStatus = '500';
if (err instanceof DecodeError) {
invokePath = '/400';
invokeStatus = '400';
} else {
console.error(err);
}
res.statusCode = Number(invokeStatus);
return await invokeRender(url.parse(invokePath, true), invokePath, 0, {
invokeStatus: res.statusCode
});
} catch (err2) {
console.error(err2);
}
res.statusCode = 500;
res.end('Internal Server Error');
}
};
let requestHandler = requestHandlerImpl;
if (config.experimental.testProxy) {
// Intercept fetch and other testmode apis.
const { wrapRequestHandlerWorker, interceptTestApis } = require('next/dist/experimental/testmode/server');
requestHandler = wrapRequestHandlerWorker(requestHandler);
interceptTestApis();
// We treat the intercepted fetch as "original" fetch that should be reset to during HMR.
originalFetch = globalThis.fetch;
}
requestHandlers[opts.dir] = requestHandler;
const renderServerOpts = {
port: opts.port,
dir: opts.dir,
hostname: opts.hostname,
minimalMode: opts.minimalMode,
dev: !!opts.dev,
server: opts.server,
serverFields: {
...(developmentBundler == null ? void 0 : developmentBundler.serverFields) || {},
setIsrStatus: devBundlerService == null ? void 0 : devBundlerService.setIsrStatus.bind(devBundlerService)
},
experimentalTestProxy: !!config.experimental.testProxy,
experimentalHttpsServer: !!opts.experimentalHttpsServer,
bundlerService: devBundlerService,
startServerSpan: opts.startServerSpan,
quiet: opts.quiet,
onDevServerCleanup: opts.onDevServerCleanup
};
renderServerOpts.serverFields.routerServerHandler = requestHandlerImpl;
// pre-initialize workers
const handlers = await renderServer.instance.initialize(renderServerOpts);
const logError = async (type, err)=>{
if (isPostpone(err)) {
// React postpones that are unhandled might end up logged here but they're
// not really errors. They're just part of rendering.
return;
}
if (type === 'unhandledRejection') {
Log.error('unhandledRejection: ', err);
} else if (type === 'uncaughtException') {
Log.error('uncaughtException: ', err);
}
};
process.on('uncaughtException', logError.bind(null, 'uncaughtException'));
process.on('unhandledRejection', logError.bind(null, 'unhandledRejection'));
const resolveRoutes = getResolveRoutes(fsChecker, config, opts, renderServer.instance, renderServerOpts, developmentBundler == null ? void 0 : developmentBundler.ensureMiddleware);
const upgradeHandler = async (req, socket, head)=>{
try {
req.on('error', (_err)=>{
// TODO: log socket errors?
// console.error(_err);
});
socket.on('error', (_err)=>{
// TODO: log socket errors?
// console.error(_err);
});
if (opts.dev && developmentBundler && req.url) {
if (blockCrossSite(req, socket, config.allowedDevOrigins, opts.hostname)) {
return;
}
const { basePath, assetPrefix } = config;
let hmrPrefix = basePath;
// assetPrefix overrides basePath for HMR path
if (assetPrefix) {
hmrPrefix = normalizedAssetPrefix(assetPrefix);
if (URL.canParse(hmrPrefix)) {
// remove trailing slash from pathname
// return empty string if pathname is '/'
// to avoid conflicts with '/_next' below
hmrPrefix = new URL(hmrPrefix).pathname.replace(/\/$/, '');
}
}
const isHMRRequest = req.url.startsWith(ensureLeadingSlash(`${hmrPrefix}/_next/webpack-hmr`));
// only handle HMR requests if the basePath in the request
// matches the basePath for the handler responding to the request
if (isHMRRequest) {
return developmentBundler.hotReloader.onHMR(req, socket, head, (client)=>{
client.send(JSON.stringify({
action: HMR_ACTIONS_SENT_TO_BROWSER.ISR_MANIFEST,
data: (devBundlerService == null ? void 0 : devBundlerService.appIsrManifest) || {}
}));
});
}
}
const res = new MockedResponse({
resWriter: ()=>{
throw Object.defineProperty(new Error('Invariant: did not expect response writer to be written to for upgrade request'), "__NEXT_ERROR_CODE", {
value: "E522",
enumerable: false,
configurable: true
});
}
});
const { matchedOutput, parsedUrl } = await resolveRoutes({
req,
res,
isUpgradeReq: true,
signal: signalFromNodeResponse(socket)
});
// TODO: allow upgrade requests to pages/app paths?
// this was not previously supported
if (matchedOutput) {
return socket.end();
}
if (parsedUrl.protocol) {
return await proxyRequest(req, socket, parsedUrl, head);
}
// If there's no matched output, we don't handle the request as user's
// custom WS server may be listening on the same path.
} catch (err) {
console.error('Error handling upgrade request', err);
socket.end();
}
};
return {
requestHandler,
upgradeHandler,
server: handlers.server,
closeUpgraded () {
var _developmentBundler_hotReloader;
developmentBundler == null ? void 0 : (_developmentBundler_hotReloader = developmentBundler.hotReloader) == null ? void 0 : _developmentBundler_hotReloader.close();
}
};
}
//# sourceMappingURL=router-server.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,66 @@
import { parseUrl } from '../../../lib/url';
import { warnOnce } from '../../../build/output/log';
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection';
function warnOrBlockRequest(res, origin, mode) {
const originString = origin ? `from ${origin}` : '';
if (mode === 'warn') {
warnOnce(`Cross origin request detected ${originString} to /_next/* resource. In a future major version of Next.js, you will need to explicitly configure "allowedDevOrigins" in next.config to allow this.\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins`);
return false;
}
warnOnce(`Blocked cross-origin request ${originString} to /_next/* resource. To allow this, configure "allowedDevOrigins" in next.config\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins`);
if ('statusCode' in res) {
res.statusCode = 403;
}
res.end('Unauthorized');
return true;
}
function isInternalDevEndpoint(req) {
if (!req.url) return false;
try {
// TODO: We should standardize on a single prefix for this
const isMiddlewareRequest = req.url.includes('/__nextjs');
const isInternalAsset = req.url.includes('/_next');
// Static media requests are excluded, as they might be loaded via CSS and would fail
// CORS checks.
const isIgnoredRequest = req.url.includes('/_next/image') || req.url.includes('/_next/static/media');
return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest);
} catch (err) {
return false;
}
}
export const blockCrossSite = (req, res, allowedDevOrigins, hostname)=>{
// in the future, these will be blocked by default when allowed origins aren't configured.
// for now, we warn when allowed origins aren't configured
const mode = typeof allowedDevOrigins === 'undefined' ? 'warn' : 'block';
const allowedOrigins = [
'*.localhost',
'localhost',
...allowedDevOrigins || []
];
if (hostname) {
allowedOrigins.push(hostname);
}
// only process internal URLs/middleware
if (!isInternalDevEndpoint(req)) {
return false;
}
// block non-cors request from cross-site e.g. script tag on
// different host
if (req.headers['sec-fetch-mode'] === 'no-cors' && req.headers['sec-fetch-site'] === 'cross-site') {
return warnOrBlockRequest(res, undefined, mode);
}
// ensure websocket requests from allowed origin
const rawOrigin = req.headers['origin'];
if (rawOrigin) {
const parsedOrigin = parseUrl(rawOrigin);
if (parsedOrigin) {
const originLowerCase = parsedOrigin.hostname.toLowerCase();
if (!isCsrfOriginAllowed(originLowerCase, allowedOrigins)) {
return warnOrBlockRequest(res, originLowerCase, mode);
}
}
}
return false;
};
//# sourceMappingURL=block-cross-site.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,33 @@
import path from '../../../shared/lib/isomorphic/path';
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path';
import { isDynamicRoute } from '../../../shared/lib/router/utils/is-dynamic';
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { normalizeRouteRegex } from '../../../lib/load-custom-routes';
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp';
export function buildDataRoute(page, buildId) {
const pagePath = normalizePagePath(page);
const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`);
let dataRouteRegex;
let namedDataRouteRegex;
let routeKeys;
if (isDynamicRoute(page)) {
const routeRegex = getNamedRouteRegex(dataRoute, {
prefixRouteKeys: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true
});
dataRouteRegex = normalizeRouteRegex(routeRegex.re.source);
namedDataRouteRegex = routeRegex.namedRegex;
routeKeys = routeRegex.routeKeys;
} else {
dataRouteRegex = normalizeRouteRegex(new RegExp(`^${path.posix.join('/_next/data', 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"],"sourcesContent":["import path from '../../../shared/lib/isomorphic/path'\nimport { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'\nimport { isDynamicRoute } from '../../../shared/lib/router/utils/is-dynamic'\nimport { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'\nimport { normalizeRouteRegex } from '../../../lib/load-custom-routes'\nimport { escapeStringRegexp } from '../../../shared/lib/escape-regexp'\n\nexport function buildDataRoute(page: string, buildId: string) {\n const pagePath = normalizePagePath(page)\n const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`)\n\n let dataRouteRegex: string\n let namedDataRouteRegex: string | undefined\n let routeKeys: { [named: string]: string } | undefined\n\n if (isDynamicRoute(page)) {\n const routeRegex = getNamedRouteRegex(dataRoute, {\n prefixRouteKeys: true,\n includeSuffix: true,\n excludeOptionalTrailingSlash: true,\n })\n\n dataRouteRegex = normalizeRouteRegex(routeRegex.re.source)\n namedDataRouteRegex = routeRegex.namedRegex\n routeKeys = routeRegex.routeKeys\n } else {\n dataRouteRegex = normalizeRouteRegex(\n new RegExp(\n `^${path.posix.join(\n '/_next/data',\n escapeStringRegexp(buildId),\n `${pagePath}\\\\.json`\n )}$`\n ).source\n )\n }\n\n return {\n page,\n routeKeys,\n dataRouteRegex,\n namedDataRouteRegex,\n }\n}\n"],"names":["path","normalizePagePath","isDynamicRoute","getNamedRouteRegex","normalizeRouteRegex","escapeStringRegexp","buildDataRoute","page","buildId","pagePath","dataRoute","posix","join","dataRouteRegex","namedDataRouteRegex","routeKeys","routeRegex","prefixRouteKeys","includeSuffix","excludeOptionalTrailingSlash","re","source","namedRegex","RegExp"],"mappings":"AAAA,OAAOA,UAAU,sCAAqC;AACtD,SAASC,iBAAiB,QAAQ,oDAAmD;AACrF,SAASC,cAAc,QAAQ,8CAA6C;AAC5E,SAASC,kBAAkB,QAAQ,+CAA8C;AACjF,SAASC,mBAAmB,QAAQ,kCAAiC;AACrE,SAASC,kBAAkB,QAAQ,oCAAmC;AAEtE,OAAO,SAASC,eAAeC,IAAY,EAAEC,OAAe;IAC1D,MAAMC,WAAWR,kBAAkBM;IACnC,MAAMG,YAAYV,KAAKW,KAAK,CAACC,IAAI,CAAC,eAAeJ,SAAS,GAAGC,SAAS,KAAK,CAAC;IAE5E,IAAII;IACJ,IAAIC;IACJ,IAAIC;IAEJ,IAAIb,eAAeK,OAAO;QACxB,MAAMS,aAAab,mBAAmBO,WAAW;YAC/CO,iBAAiB;YACjBC,eAAe;YACfC,8BAA8B;QAChC;QAEAN,iBAAiBT,oBAAoBY,WAAWI,EAAE,CAACC,MAAM;QACzDP,sBAAsBE,WAAWM,UAAU;QAC3CP,YAAYC,WAAWD,SAAS;IAClC,OAAO;QACLF,iBAAiBT,oBACf,IAAImB,OACF,CAAC,CAAC,EAAEvB,KAAKW,KAAK,CAACC,IAAI,CACjB,eACAP,mBAAmBG,UACnB,GAAGC,SAAS,OAAO,CAAC,EACpB,CAAC,CAAC,EACJY,MAAM;IAEZ;IAEA,OAAO;QACLd;QACAQ;QACAF;QACAC;IACF;AACF"}

View File

@@ -0,0 +1,22 @@
import path from '../../../shared/lib/isomorphic/path';
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path';
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { RSC_SEGMENT_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX } from '../../../lib/constants';
export const SEGMENT_PATH_KEY = 'nextSegmentPath';
export function buildPrefetchSegmentDataRoute(page, segmentPath) {
const pagePath = normalizePagePath(page);
const destination = path.posix.join(`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`, `${segmentPath}${RSC_SEGMENT_SUFFIX}`);
const { namedRegex } = getNamedRouteRegex(destination, {
prefixRouteKeys: true,
includePrefix: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
backreferenceDuplicateKeys: true
});
return {
destination,
source: namedRegex
};
}
//# sourceMappingURL=build-prefetch-segment-data-route.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/build-prefetch-segment-data-route.ts"],"sourcesContent":["import path from '../../../shared/lib/isomorphic/path'\nimport { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'\nimport { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'\nimport {\n RSC_SEGMENT_SUFFIX,\n RSC_SEGMENTS_DIR_SUFFIX,\n} from '../../../lib/constants'\n\nexport const SEGMENT_PATH_KEY = 'nextSegmentPath'\n\nexport type PrefetchSegmentDataRoute = {\n source: string\n destination: string\n}\n\nexport function buildPrefetchSegmentDataRoute(\n page: string,\n segmentPath: string\n): PrefetchSegmentDataRoute {\n const pagePath = normalizePagePath(page)\n\n const destination = path.posix.join(\n `${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,\n `${segmentPath}${RSC_SEGMENT_SUFFIX}`\n )\n\n const { namedRegex } = getNamedRouteRegex(destination, {\n prefixRouteKeys: true,\n includePrefix: true,\n includeSuffix: true,\n excludeOptionalTrailingSlash: true,\n backreferenceDuplicateKeys: true,\n })\n\n return {\n destination,\n source: namedRegex,\n }\n}\n"],"names":["path","normalizePagePath","getNamedRouteRegex","RSC_SEGMENT_SUFFIX","RSC_SEGMENTS_DIR_SUFFIX","SEGMENT_PATH_KEY","buildPrefetchSegmentDataRoute","page","segmentPath","pagePath","destination","posix","join","namedRegex","prefixRouteKeys","includePrefix","includeSuffix","excludeOptionalTrailingSlash","backreferenceDuplicateKeys","source"],"mappings":"AAAA,OAAOA,UAAU,sCAAqC;AACtD,SAASC,iBAAiB,QAAQ,oDAAmD;AACrF,SAASC,kBAAkB,QAAQ,+CAA8C;AACjF,SACEC,kBAAkB,EAClBC,uBAAuB,QAClB,yBAAwB;AAE/B,OAAO,MAAMC,mBAAmB,kBAAiB;AAOjD,OAAO,SAASC,8BACdC,IAAY,EACZC,WAAmB;IAEnB,MAAMC,WAAWR,kBAAkBM;IAEnC,MAAMG,cAAcV,KAAKW,KAAK,CAACC,IAAI,CACjC,GAAGH,WAAWL,yBAAyB,EACvC,GAAGI,cAAcL,oBAAoB;IAGvC,MAAM,EAAEU,UAAU,EAAE,GAAGX,mBAAmBQ,aAAa;QACrDI,iBAAiB;QACjBC,eAAe;QACfC,eAAe;QACfC,8BAA8B;QAC9BC,4BAA4B;IAC9B;IAEA,OAAO;QACLR;QACAS,QAAQN;IACV;AACF"}

View File

@@ -0,0 +1,28 @@
import escapePathDelimiters from '../../../shared/lib/router/utils/escape-path-delimiters';
import { DecodeError } from '../../../shared/lib/utils';
/**
* We only encode path delimiters for path segments from
* getStaticPaths so we need to attempt decoding the URL
* to match against and only escape the path delimiters
* this allows non-ascii values to be handled e.g.
* Japanese characters.
* */ function decodePathParams(pathname) {
// TODO: investigate adding this handling for non-SSG
// pages so non-ascii names also work there.
return pathname.split('/').map((seg)=>{
try {
seg = escapePathDelimiters(decodeURIComponent(seg), true);
} catch (_) {
// An improperly encoded URL was provided
throw Object.defineProperty(new DecodeError('Failed to decode path param(s).'), "__NEXT_ERROR_CODE", {
value: "E539",
enumerable: false,
configurable: true
});
}
return seg;
}).join('/');
}
export { decodePathParams };
//# sourceMappingURL=decode-path-params.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/decode-path-params.ts"],"sourcesContent":["import escapePathDelimiters from '../../../shared/lib/router/utils/escape-path-delimiters'\nimport { DecodeError } from '../../../shared/lib/utils'\n\n/**\n * We only encode path delimiters for path segments from\n * getStaticPaths so we need to attempt decoding the URL\n * to match against and only escape the path delimiters\n * this allows non-ascii values to be handled e.g.\n * Japanese characters.\n * */\nfunction decodePathParams(pathname: string): string {\n // TODO: investigate adding this handling for non-SSG\n // pages so non-ascii names also work there.\n return pathname\n .split('/')\n .map((seg) => {\n try {\n seg = escapePathDelimiters(decodeURIComponent(seg), true)\n } catch (_) {\n // An improperly encoded URL was provided\n throw new DecodeError('Failed to decode path param(s).')\n }\n return seg\n })\n .join('/')\n}\n\nexport { decodePathParams }\n"],"names":["escapePathDelimiters","DecodeError","decodePathParams","pathname","split","map","seg","decodeURIComponent","_","join"],"mappings":"AAAA,OAAOA,0BAA0B,0DAAyD;AAC1F,SAASC,WAAW,QAAQ,4BAA2B;AAEvD;;;;;;GAMG,GACH,SAASC,iBAAiBC,QAAgB;IACxC,qDAAqD;IACrD,4CAA4C;IAC5C,OAAOA,SACJC,KAAK,CAAC,KACNC,GAAG,CAAC,CAACC;QACJ,IAAI;YACFA,MAAMN,qBAAqBO,mBAAmBD,MAAM;QACtD,EAAE,OAAOE,GAAG;YACV,yCAAyC;YACzC,MAAM,qBAAkD,CAAlD,IAAIP,YAAY,oCAAhB,qBAAA;uBAAA;4BAAA;8BAAA;YAAiD;QACzD;QACA,OAAOK;IACT,GACCG,IAAI,CAAC;AACV;AAEA,SAASP,gBAAgB,GAAE"}

View File

@@ -0,0 +1,472 @@
import path from 'path';
import fs from 'fs/promises';
import * as Log from '../../../build/output/log';
import setupDebug from 'next/dist/compiled/debug';
import { LRUCache } from '../lru-cache';
import loadCustomRoutes from '../../../lib/load-custom-routes';
import { modifyRouteRegex } from '../../../lib/redirect-status';
import { FileType, fileExists } from '../../../lib/file-exists';
import { recursiveReadDir } from '../../../lib/recursive-readdir';
import { isDynamicRoute } from '../../../shared/lib/router/utils';
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp';
import { getPathMatch } from '../../../shared/lib/router/utils/path-match';
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher';
import { pathHasPrefix } from '../../../shared/lib/router/utils/path-has-prefix';
import { normalizeLocalePath } from '../../../shared/lib/i18n/normalize-locale-path';
import { removePathPrefix } from '../../../shared/lib/router/utils/remove-path-prefix';
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher';
import { APP_PATH_ROUTES_MANIFEST, BUILD_ID_FILE, FUNCTIONS_CONFIG_MANIFEST, MIDDLEWARE_MANIFEST, PAGES_MANIFEST, PRERENDER_MANIFEST, ROUTES_MANIFEST } from '../../../shared/lib/constants';
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep';
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route';
import { RSCPathnameNormalizer } from '../../normalizers/request/rsc';
import { PrefetchRSCPathnameNormalizer } from '../../normalizers/request/prefetch-rsc';
import { encodeURIPath } from '../../../shared/lib/encode-uri-path';
const debug = setupDebug('next:router-server:filesystem');
export const buildCustomRoute = (type, item, basePath, caseSensitive)=>{
const restrictedRedirectPaths = [
'/_next'
].map((p)=>basePath ? `${basePath}${p}` : p);
const match = getPathMatch(item.source, {
strict: true,
removeUnnamedParams: true,
regexModifier: !item.internal ? (regex)=>modifyRouteRegex(regex, type === 'redirect' ? restrictedRedirectPaths : undefined) : undefined,
sensitive: caseSensitive
});
return {
...item,
...type === 'rewrite' ? {
check: true
} : {},
match
};
};
export async function setupFsCheck(opts) {
const getItemsLru = !opts.dev ? new LRUCache(1024 * 1024, function length(value) {
if (!value) return 0;
return (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.join(opts.dir, opts.config.distDir);
const publicFolderPath = path.join(opts.dir, 'public');
const nextStaticFolderPath = path.join(distDir, 'static');
const legacyStaticFolderPath = path.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.join(opts.dir, opts.config.distDir, BUILD_ID_FILE);
try {
buildId = await fs.readFile(buildIdPath, 'utf8');
} catch (err) {
if (err.code !== 'ENOENT') throw err;
throw Object.defineProperty(new Error(`Could not find a production build in the '${opts.config.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id`), "__NEXT_ERROR_CODE", {
value: "E427",
enumerable: false,
configurable: true
});
}
try {
for (const file of (await recursiveReadDir(publicFolderPath))){
// Ensure filename is encoded and normalized.
publicFolderItems.add(encodeURIPath(normalizePathSep(file)));
}
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
try {
for (const file of (await recursiveReadDir(legacyStaticFolderPath))){
// Ensure filename is encoded and normalized.
legacyStaticFolderItems.add(encodeURIPath(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 recursiveReadDir(nextStaticFolderPath))){
// Ensure filename is encoded and normalized.
nextStaticFolderItems.add(path.posix.join('/_next/static', encodeURIPath(normalizePathSep(file))));
}
} catch (err) {
if (opts.config.output !== 'standalone') throw err;
}
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST);
const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST);
const middlewareManifestPath = path.join(distDir, 'server', MIDDLEWARE_MANIFEST);
const functionsConfigManifestPath = path.join(distDir, 'server', FUNCTIONS_CONFIG_MANIFEST);
const pagesManifestPath = path.join(distDir, 'server', PAGES_MANIFEST);
const appRoutesManifestPath = path.join(distDir, APP_PATH_ROUTES_MANIFEST);
const routesManifest = JSON.parse(await fs.readFile(routesManifestPath, 'utf8'));
prerenderManifest = JSON.parse(await fs.readFile(prerenderManifestPath, 'utf8'));
const middlewareManifest = JSON.parse(await fs.readFile(middlewareManifestPath, 'utf8').catch(()=>'{}'));
const functionsConfigManifest = JSON.parse(await fs.readFile(functionsConfigManifestPath, 'utf8').catch(()=>'{}'));
const pagesManifest = JSON.parse(await fs.readFile(pagesManifestPath, 'utf8'));
const appRoutesManifest = JSON.parse(await fs.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(normalizeLocalePath(key, opts.config.i18n.locales).pathname);
} else {
pageFiles.add(key);
}
}
for (const key of Object.keys(appRoutesManifest)){
appFiles.add(appRoutesManifest[key]);
}
const escapedBuildId = escapeStringRegexp(buildId);
for (const route of routesManifest.dataRoutes){
if (isDynamicRoute(route.page)) {
const routeRegex = getRouteRegex(route.page);
dynamicRoutes.push({
...route,
regex: routeRegex.re.toString(),
match: 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: getRouteMatcher(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 = getMiddlewareRouteMatcher((_middlewareManifest_middleware1 = middlewareManifest.middleware) == null ? void 0 : (_middlewareManifest_middleware_1 = _middlewareManifest_middleware1['/']) == null ? void 0 : _middlewareManifest_middleware_1.matchers);
} else if (functionsConfigManifest == null ? void 0 : functionsConfigManifest.functions['/_middleware']) {
middlewareMatcher = getMiddlewareRouteMatcher(functionsConfigManifest.functions['/_middleware'].matchers ?? [
{
regexp: '.*',
originalSource: '/:path*'
}
]);
}
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 loadCustomRoutes(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 = {
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 = 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;
const normalizers = {
// Because we can't know if the app directory is enabled or not at this
// stage, we assume that it is.
rsc: new RSCPathnameNormalizer(),
prefetchRSC: opts.config.experimental.ppr ? new PrefetchRSCPathnameNormalizer() : undefined
};
return {
headers,
rewrites,
redirects,
buildId,
handleLocale,
appFiles,
pageFiles,
dynamicRoutes,
nextDataRoutes,
exportPathMapRoutes: 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;
}
const { basePath } = opts.config;
const hasBasePath = pathHasPrefix(itemPath, basePath);
// Return null if path doesn't start with basePath
if (basePath && !hasBasePath) {
return null;
}
// Remove basePath if it exists.
if (basePath && hasBasePath) {
itemPath = removePathPrefix(itemPath, basePath) || '/';
}
// Simulate minimal mode requests by normalizing RSC and postponed
// requests.
if (opts.minimalMode) {
var _normalizers_prefetchRSC;
if ((_normalizers_prefetchRSC = normalizers.prefetchRSC) == null ? void 0 : _normalizers_prefetchRSC.match(itemPath)) {
itemPath = normalizers.prefetchRSC.normalize(itemPath, true);
} else if (normalizers.rsc.match(itemPath)) {
itemPath = normalizers.rsc.normalize(itemPath, true);
}
}
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) {
var _i18n_domains;
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,
// default locales from domains need to be matched too
...((_i18n_domains = i18n.domains) == null ? void 0 : _i18n_domains.map((item)=>item.defaultLocale)) || []
]);
if (localeResult.pathname !== curItemPath) {
curItemPath = localeResult.pathname;
locale = localeResult.locale;
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
}
if (type === 'legacyStaticFolder') {
if (!pathHasPrefix(curItemPath, '/static')) {
continue;
}
curItemPath = curItemPath.substring('/static'.length);
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
if (type === 'nextStaticFolder' && !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 {}
}
let matchedItem = items.has(curItemPath);
// check decoded variant as well
if (!matchedItem && !opts.dev) {
matchedItem = items.has(curDecodedItemPath);
if (matchedItem) curItemPath = curDecodedItemPath;
else {
// x-ref: https://github.com/vercel/next.js/issues/54008
// There're cases that urls get decoded before requests, we should support both encoded and decoded ones.
// e.g. nginx could decode the proxy urls, the below ones should be treated as the same:
// decoded version: `/_next/static/chunks/pages/blog/[slug]-d4858831b91b69f6.js`
// encoded version: `/_next/static/chunks/pages/blog/%5Bslug%5D-d4858831b91b69f6.js`
try {
// encode the special characters in the path and retrieve again to determine if path exists.
const encodedCurItemPath = encodeURIPath(curItemPath);
matchedItem = items.has(encodedCurItemPath);
} catch {}
}
}
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.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 fileExists(fsPath, 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.posix.join(itemsRoot, tempItemPath);
found = await fileExists(fsPath, 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 ? 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,6 @@
const REACT_POSTPONE_TYPE = Symbol.for('react.postpone');
export function isPostpone(error) {
return typeof error === 'object' && error !== null && error.$$typeof === REACT_POSTPONE_TYPE;
}
//# sourceMappingURL=is-postpone.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/is-postpone.ts"],"sourcesContent":["const REACT_POSTPONE_TYPE: symbol = Symbol.for('react.postpone')\n\nexport function isPostpone(error: any): boolean {\n return (\n typeof error === 'object' &&\n error !== null &&\n error.$$typeof === REACT_POSTPONE_TYPE\n )\n}\n"],"names":["REACT_POSTPONE_TYPE","Symbol","for","isPostpone","error","$$typeof"],"mappings":"AAAA,MAAMA,sBAA8BC,OAAOC,GAAG,CAAC;AAE/C,OAAO,SAASC,WAAWC,KAAU;IACnC,OACE,OAAOA,UAAU,YACjBA,UAAU,QACVA,MAAMC,QAAQ,KAAKL;AAEvB"}

View File

@@ -0,0 +1,103 @@
import url from 'url';
import { stringifyQuery } from '../../server-route-utils';
import { Duplex } from 'stream';
import { DetachedPromise } from '../../../lib/detached-promise';
export async function proxyRequest(req, res, parsedUrl, upgradeHead, reqBody, proxyTimeout) {
const { query } = parsedUrl;
delete parsedUrl.query;
parsedUrl.search = stringifyQuery(req, query);
const target = url.format(parsedUrl);
const HttpProxy = require('next/dist/compiled/http-proxy');
const proxy = new HttpProxy({
target,
changeOrigin: true,
ignorePath: 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,
headers: {
'x-forwarded-host': req.headers.host || ''
}
});
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);
});
const detached = new DetachedPromise();
proxy.on('error', (err)=>{
console.error(`Failed to proxy ${target}`, err);
if (!finished) {
finished = true;
detached.reject(err);
if (!res.destroyed) {
if (!(res instanceof Duplex)) {
res.statusCode = 500;
}
res.end('Internal Server Error');
}
}
});
// If upgrade head is present or the response is a Duplex stream, treat as
// WebSocket request.
if (upgradeHead || res instanceof Duplex) {
proxy.on('proxyReqWs', (proxyReq)=>{
proxyReq.on('close', ()=>{
if (!finished) {
finished = true;
detached.resolve(true);
}
});
});
proxy.ws(req, res, upgradeHead);
detached.resolve(true);
} else {
proxy.on('proxyReq', (proxyReq)=>{
proxyReq.on('close', ()=>{
if (!finished) {
finished = true;
detached.resolve(true);
}
});
});
proxy.web(req, res, {
buffer: reqBody
});
}
// When the proxy finishes proxying the request, shut down the proxy.
return detached.promise.finally(()=>{
proxy.close();
});
}
//# sourceMappingURL=proxy-request.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,566 @@
import url from 'url';
import path from 'node:path';
import setupDebug from 'next/dist/compiled/debug';
import { getCloneableBody } from '../../body-streams';
import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils';
import { stringifyQuery } from '../../server-route-utils';
import { formatHostname } from '../format-hostname';
import { toNodeOutgoingHttpHeaders } from '../../web/utils';
import { isAbortError } from '../../pipe-readable';
import { getHostname } from '../../../shared/lib/get-hostname';
import { getRedirectStatus } from '../../../lib/redirect-status';
import { normalizeRepeatedSlashes } from '../../../shared/lib/utils';
import { getRelativeURL } from '../../../shared/lib/router/utils/relativize-url';
import { addPathPrefix } from '../../../shared/lib/router/utils/add-path-prefix';
import { pathHasPrefix } from '../../../shared/lib/router/utils/path-has-prefix';
import { detectDomainLocale } from '../../../shared/lib/i18n/detect-domain-locale';
import { normalizeLocalePath } from '../../../shared/lib/i18n/normalize-locale-path';
import { removePathPrefix } from '../../../shared/lib/router/utils/remove-path-prefix';
import { NextDataPathnameNormalizer } from '../../normalizers/request/next-data';
import { BasePathPathnameNormalizer } from '../../normalizers/request/base-path';
import { addRequestMeta } from '../../request-meta';
import { compileNonPath, matchHas, parseDestination, prepareDestination } from '../../../shared/lib/router/utils/prepare-destination';
import { NEXT_REWRITTEN_PATH_HEADER, NEXT_REWRITTEN_QUERY_HEADER, NEXT_ROUTER_STATE_TREE_HEADER, RSC_HEADER } from '../../../client/components/app-router-headers';
import { getSelectedParams } from '../../../client/components/router-reducer/compute-changed-path';
import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites';
import { parseAndValidateFlightRouterState } from '../../app-render/parse-and-validate-flight-router-state';
const debug = setupDebug('next:router-server:resolve-routes');
export function getResolveRoutes(fsChecker, config, opts, renderServer, renderServerOpts, 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 _req_socket, _req_headers_xforwardedproto;
let finished = false;
let resHeaders = {};
let matchedOutput = null;
let parsedUrl = url.parse(req.url || '', true);
let didRewrite = false;
const urlParts = (req.url || '').split('?', 1);
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.parse(normalizeRepeatedSlashes(req.url), true);
return {
parsedUrl,
resHeaders,
finished: true,
statusCode: 308
};
}
// TODO: inherit this from higher up
const protocol = (req == null ? void 0 : (_req_socket = req.socket) == null ? void 0 : _req_socket.encrypted) || ((_req_headers_xforwardedproto = req.headers['x-forwarded-proto']) == null ? void 0 : _req_headers_xforwardedproto.includes('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}://${formatHostname(opts.hostname || 'localhost')}:${opts.port}${req.url}` : req.url || '';
addRequestMeta(req, 'initURL', initUrl);
addRequestMeta(req, 'initQuery', {
...parsedUrl.query
});
addRequestMeta(req, 'initProtocol', protocol);
if (!isUpgradeReq) {
addRequestMeta(req, 'clonableBody', 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 = pathHasPrefix(parsedUrl.pathname || '', config.basePath);
initialLocaleResult = normalizeLocalePath(removePathPrefix(parsedUrl.pathname || '/', config.basePath), config.i18n.locales);
domainLocale = detectDomainLocale(config.i18n.domains, getHostname(parsedUrl, req.headers));
defaultLocale = (domainLocale == null ? void 0 : domainLocale.defaultLocale) || config.i18n.defaultLocale;
addRequestMeta(req, 'defaultLocale', defaultLocale);
addRequestMeta(req, 'locale', initialLocaleResult.detectedLocale || defaultLocale);
// ensure locale is present for resolving routes
if (!initialLocaleResult.detectedLocale && !initialLocaleResult.pathname.startsWith('/_next/')) {
parsedUrl.pathname = addPathPrefix(initialLocaleResult.pathname === '/' ? `/${defaultLocale}` : 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) && 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 (!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(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'))) {
addRequestMeta(req, 'isNextDataReq', true);
}
if (config.useFileSystemPublicRoutes || didRewrite) {
return pageOutput;
}
}
}
}
const normalizers = {
basePath: config.basePath && config.basePath !== '/' ? new BasePathPathnameNormalizer(config.basePath) : undefined,
data: new NextDataPathnameNormalizer(fsChecker.buildId)
};
async function handleRoute(route) {
let curPathname = parsedUrl.pathname || '/';
if (config.i18n && route.internal) {
const hadTrailingSlash = curPathname.endsWith('/');
if (config.basePath) {
curPathname = removePathPrefix(curPathname, config.basePath);
}
const hadBasePath = curPathname !== parsedUrl.pathname;
const localeResult = normalizeLocalePath(curPathname, config.i18n.locales);
const isDefaultLocale = localeResult.detectedLocale === defaultLocale;
if (isDefaultLocale) {
curPathname = localeResult.pathname === '/' && hadBasePath ? config.basePath : addPathPrefix(localeResult.pathname, hadBasePath ? config.basePath : '');
} else if (hadBasePath) {
curPathname = curPathname === '/' ? config.basePath : addPathPrefix(curPathname, config.basePath);
}
if ((isDefaultLocale || hadBasePath) && hadTrailingSlash) {
curPathname = maybeAddTrailingSlash(curPathname);
}
}
let params = route.match(curPathname);
if ((route.has || route.missing) && params) {
const hasParams = matchHas(req, parsedUrl.query, route.has, route.missing);
if (hasParams) {
Object.assign(params, hasParams);
} else {
params = false;
}
}
if (params) {
if (fsChecker.exportPathMapRoutes && route.name === 'before_files_end') {
for (const exportPathMapRoute of fsChecker.exportPathMapRoutes){
const result = await handleRoute(exportPathMapRoute);
if (result) {
return result;
}
}
}
if (route.name === 'middleware_next_data' && parsedUrl.pathname) {
var _fsChecker_getMiddlewareMatchers;
if ((_fsChecker_getMiddlewareMatchers = fsChecker.getMiddlewareMatchers()) == null ? void 0 : _fsChecker_getMiddlewareMatchers.length) {
var _normalizers_basePath;
let normalized = parsedUrl.pathname;
// Remove the base path if it exists.
const hadBasePath = (_normalizers_basePath = normalizers.basePath) == null ? void 0 : _normalizers_basePath.match(parsedUrl.pathname);
if (hadBasePath && normalizers.basePath) {
normalized = normalizers.basePath.normalize(normalized, true);
}
let updated = false;
if (normalizers.data.match(normalized)) {
updated = true;
addRequestMeta(req, 'isNextDataReq', true);
normalized = normalizers.data.normalize(normalized, true);
}
if (config.i18n) {
const curLocaleResult = normalizeLocalePath(normalized, config.i18n.locales);
if (curLocaleResult.detectedLocale) {
addRequestMeta(req, 'locale', curLocaleResult.detectedLocale);
}
}
// If we updated the pathname, and it had a base path, re-add the
// base path.
if (updated) {
if (hadBasePath) {
normalized = path.posix.join(config.basePath, normalized);
}
// Re-add the trailing slash (if required).
normalized = maybeAddTrailingSlash(normalized);
parsedUrl.pathname = normalized;
}
}
}
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) && pathHasPrefix(pathname, '/api'))) {
if (config.useFileSystemPublicRoutes || didRewrite || output.type !== 'appFile' && output.type !== 'pageFile') {
matchedOutput = output;
if (output.locale) {
addRequestMeta(req, 'locale', 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)) {
if (ensureMiddleware) {
await ensureMiddleware(req.url);
}
const serverResult = await (renderServer == null ? void 0 : renderServer.initialize(renderServerOpts));
if (!serverResult) {
throw Object.defineProperty(new Error(`Failed to initialize render server "middleware"`), "__NEXT_ERROR_CODE", {
value: "E222",
enumerable: false,
configurable: true
});
}
addRequestMeta(req, 'invokePath', '');
addRequestMeta(req, 'invokeOutput', '');
addRequestMeta(req, 'invokeQuery', {});
addRequestMeta(req, 'middlewareInvoke', true);
debug('invoking middleware', req.url, req.headers);
let middlewareRes = undefined;
let bodyStream = undefined;
try {
try {
await serverResult.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 (isAbortError(e)) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
throw e;
}
if (res.closed || res.finished || !middlewareRes) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
const middlewareHeaders = 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({
...filterReqHeaders(middlewareHeaders, ipcForbiddenHeaders)
})){
if ([
'content-length',
'x-middleware-rewrite',
'x-middleware-redirect',
'x-middleware-refresh'
].includes(key)) {
continue;
}
// for set-cookie, the header shouldn't be added to the response
// as it's only needed for the request to the middleware function.
if (key === 'x-middleware-set-cookie') {
req.headers[key] = value;
continue;
}
if (value) {
resHeaders[key] = value;
req.headers[key] = value;
}
}
if (middlewareHeaders['x-middleware-rewrite']) {
const value = middlewareHeaders['x-middleware-rewrite'];
const destination = getRelativeURL(value, initUrl);
resHeaders['x-middleware-rewrite'] = destination;
parsedUrl = url.parse(destination, true);
if (parsedUrl.protocol) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
if (config.i18n) {
const curLocaleResult = normalizeLocalePath(parsedUrl.pathname || '', config.i18n.locales);
if (curLocaleResult.detectedLocale) {
addRequestMeta(req, 'locale', curLocaleResult.detectedLocale);
}
}
}
if (middlewareHeaders['location']) {
const value = middlewareHeaders['location'];
const rel = getRelativeURL(value, initUrl);
resHeaders['location'] = rel;
parsedUrl = url.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 } = prepareDestination({
appendParamsToQuery: false,
destination: route.destination,
params: params,
query: parsedUrl.query
});
const { query } = parsedDestination;
delete parsedDestination.query;
parsedDestination.search = stringifyQuery(req, query);
parsedDestination.pathname = normalizeRepeatedSlashes(parsedDestination.pathname);
return {
finished: true,
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
statusCode: 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 = compileNonPath(key, params);
value = 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) {
let rewriteParams = params;
try {
// An interception rewrite might reference a dynamic param for a route the user
// is currently on, which wouldn't be extractable from the matched route params.
// This attempts to extract the dynamic params from the provided router state.
if (isInterceptionRouteRewrite(route)) {
const stateHeader = req.headers[NEXT_ROUTER_STATE_TREE_HEADER.toLowerCase()];
if (stateHeader) {
rewriteParams = {
...getSelectedParams(parseAndValidateFlightRouterState(stateHeader)),
...params
};
}
}
} catch (err) {
// this is a no-op -- we couldn't extract dynamic params from the provided router state,
// so we'll just use the params from the route matcher
}
// We extract the search params of the destination so we can set it on
// the response headers. We don't want to use the following
// `parsedDestination` as the query object is mutated.
const { search: destinationSearch, pathname: destinationPathname } = parseDestination({
destination: route.destination,
params: rewriteParams,
query: parsedUrl.query
});
const { parsedDestination } = prepareDestination({
appendParamsToQuery: true,
destination: route.destination,
params: rewriteParams,
query: parsedUrl.query
});
if (parsedDestination.protocol) {
return {
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
finished: true
};
}
// Set the rewrite headers only if this is a RSC request.
if (req.headers[RSC_HEADER.toLowerCase()] === '1') {
// We set the rewritten path and query headers on the response now
// that we know that the it's not an external rewrite.
if (parsedUrl.pathname !== destinationPathname) {
res.setHeader(NEXT_REWRITTEN_PATH_HEADER, destinationPathname);
}
if (destinationSearch) {
res.setHeader(NEXT_REWRITTEN_QUERY_HEADER, // remove the leading ? from the search
destinationSearch.slice(1));
}
}
if (config.i18n) {
const curLocaleResult = normalizeLocalePath(removePathPrefix(parsedDestination.pathname, config.basePath), config.i18n.locales);
if (curLocaleResult.detectedLocale) {
addRequestMeta(req, 'locale', 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,704 @@
import { getPageStaticInfo } from '../../../build/analysis/get-page-static-info';
import { createDefineEnv } from '../../../build/swc';
import fs from 'fs';
import { mkdir } from 'fs/promises';
import url from 'url';
import path from 'path';
import qs from 'querystring';
import Watchpack from 'next/dist/compiled/watchpack';
import { loadEnvConfig } from '@next/env';
import findUp from 'next/dist/compiled/find-up';
import { buildCustomRoute } from './filesystem';
import * as Log from '../../../build/output/log';
import HotReloaderWebpack from '../../dev/hot-reloader-webpack';
import { setGlobal } from '../../../trace/shared';
import loadJsConfig from '../../../build/load-jsconfig';
import { createValidFileMatcher } from '../find-page-file';
import { eventCliSession } from '../../../telemetry/events';
import { getDefineEnv } from '../../../build/webpack/plugins/define-env-plugin';
import { getSortedRoutes } from '../../../shared/lib/router/utils';
import { getStaticInfoIncludingLayouts, sortByPageExts } from '../../../build/entries';
import { verifyTypeScriptSetup } from '../../../lib/verify-typescript-setup';
import { verifyPartytownSetup } from '../../../lib/verify-partytown-setup';
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths';
import { buildDataRoute } from './build-data-route';
import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher';
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep';
import { createClientRouterFilter } from '../../../lib/create-client-router-filter';
import { absolutePathToPage } from '../../../shared/lib/page-path/absolute-path-to-page';
import { generateInterceptionRoutesRewrites } from '../../../lib/generate-interception-routes-rewrites';
import { CLIENT_STATIC_FILES_PATH, DEV_CLIENT_PAGES_MANIFEST, DEV_CLIENT_MIDDLEWARE_MANIFEST, PHASE_DEVELOPMENT_SERVER, TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST } from '../../../shared/lib/constants';
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher';
import { isMiddlewareFile, NestedMiddlewareError, isInstrumentationHookFile, getPossibleMiddlewareFilenames, getPossibleInstrumentationHookFilenames } from '../../../build/utils';
import { devPageFiles } from '../../../build/webpack/plugins/next-types-plugin/shared';
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../dev/hot-reloader-types';
import { PAGE_TYPES } from '../../../lib/page-types';
import { createHotReloaderTurbopack } from '../../dev/hot-reloader-turbopack';
import { generateEncryptionKeyBase64 } from '../../app-render/encryption-utils-server';
import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route';
import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata-route';
import { createEnvDefinitions } from '../experimental/create-env-definitions';
import { JsConfigPathsPlugin } from '../../../build/webpack/plugins/jsconfig-paths-plugin';
import { store as consoleStore } from '../../../build/output/store';
import { ModuleBuildError, TurbopackInternalError } from '../../../shared/lib/turbopack/utils';
async function verifyTypeScript(opts) {
let usingTypeScript = false;
const verifyResult = await verifyTypeScriptSetup({
dir: opts.dir,
distDir: opts.nextConfig.distDir,
intentDirs: [
opts.pagesDir,
opts.appDir
].filter(Boolean),
typeCheckPreflight: false,
tsconfigPath: opts.nextConfig.typescript.tsconfigPath,
disableStaticImages: opts.nextConfig.images.disableStaticImages,
hasAppDir: !!opts.appDir,
hasPagesDir: !!opts.pagesDir
});
if (verifyResult.version) {
usingTypeScript = true;
}
return usingTypeScript;
}
export async function propagateServerField(opts, field, args) {
var _opts_renderServer_instance, _opts_renderServer;
await ((_opts_renderServer = opts.renderServer) == null ? void 0 : (_opts_renderServer_instance = _opts_renderServer.instance) == null ? void 0 : _opts_renderServer_instance.propagateServerField(opts.dir, field, args));
}
async function startWatcher(opts) {
const { nextConfig, appDir, pagesDir, dir, resetFetch } = opts;
const { useFileSystemPublicRoutes } = nextConfig;
const usingTypeScript = await verifyTypeScript(opts);
const distDir = path.join(opts.dir, opts.nextConfig.distDir);
// we ensure the types directory exists here
if (usingTypeScript) {
const distTypesDir = path.join(distDir, 'types');
if (!fs.existsSync(distTypesDir)) {
await mkdir(distTypesDir, {
recursive: true
});
}
}
setGlobal('distDir', distDir);
setGlobal('phase', PHASE_DEVELOPMENT_SERVER);
const validFileMatcher = createValidFileMatcher(nextConfig.pageExtensions, appDir);
const serverFields = {};
// Update logging state once based on next.config.js when initializing
consoleStore.setState({
logging: nextConfig.logging !== false
});
const hotReloader = opts.turbo ? await createHotReloaderTurbopack(opts, serverFields, distDir, resetFetch) : new HotReloaderWebpack(opts.dir, {
appDir,
pagesDir,
distDir,
config: opts.nextConfig,
buildId: 'development',
encryptionKey: await generateEncryptionKeyBase64({
isBuild: false,
distDir
}),
telemetry: opts.telemetry,
rewrites: opts.fsChecker.rewrites,
previewProps: opts.fsChecker.prerenderManifest.preview,
resetFetch
});
await hotReloader.start();
if (opts.nextConfig.experimental.nextScriptWorkers) {
await verifyPartytownSetup(opts.dir, path.join(distDir, CLIENT_STATIC_FILES_PATH));
}
opts.fsChecker.ensureCallback(async function ensure(item) {
if (item.type === 'appFile' || item.type === 'pageFile') {
await hotReloader.ensurePage({
clientOnly: false,
page: item.itemPath,
isApp: item.type === 'appFile',
definition: undefined
});
}
});
let resolved = false;
let prevSortedRoutes = [];
await new Promise(async (resolve, reject)=>{
if (pagesDir) {
// Watchpack doesn't emit an event for an empty directory
fs.readdir(pagesDir, (_, files)=>{
if (files == null ? void 0 : files.length) {
return;
}
if (!resolved) {
resolve();
resolved = true;
}
});
}
const pages = pagesDir ? [
pagesDir
] : [];
const app = appDir ? [
appDir
] : [];
const directories = [
...pages,
...app
];
const rootDir = pagesDir || appDir;
const files = [
...getPossibleMiddlewareFilenames(path.join(rootDir, '..'), nextConfig.pageExtensions),
...getPossibleInstrumentationHookFilenames(path.join(rootDir, '..'), nextConfig.pageExtensions)
];
let nestedMiddleware = [];
const envFiles = [
'.env.development.local',
'.env.local',
'.env.development',
'.env'
].map((file)=>path.join(dir, file));
files.push(...envFiles);
// tsconfig/jsconfig paths hot-reloading
const tsconfigPaths = [
path.join(dir, 'tsconfig.json'),
path.join(dir, 'jsconfig.json')
];
files.push(...tsconfigPaths);
const wp = new Watchpack({
ignored: (pathname)=>{
return !files.some((file)=>file.startsWith(pathname)) && !directories.some((d)=>pathname.startsWith(d) || d.startsWith(pathname));
}
});
const fileWatchTimes = new Map();
let enabledTypeScript = usingTypeScript;
let previousClientRouterFilters;
let previousConflictingPagePaths = new Set();
wp.on('aggregated', async ()=>{
var _serverFields_middleware, _serverFields_middleware1;
let middlewareMatchers;
const routedPages = [];
const knownFiles = wp.getTimeInfoEntries();
const appPaths = {};
const pageNameSet = new Set();
const conflictingAppPagePaths = new Set();
const appPageFilePaths = new Map();
const pagesPageFilePaths = new Map();
let envChange = false;
let tsconfigChange = false;
let conflictingPageChange = 0;
let hasRootAppNotFound = false;
const { appFiles, pageFiles } = opts.fsChecker;
appFiles.clear();
pageFiles.clear();
devPageFiles.clear();
const sortedKnownFiles = [
...knownFiles.keys()
].sort(sortByPageExts(nextConfig.pageExtensions));
for (const fileName of sortedKnownFiles){
if (!files.includes(fileName) && !directories.some((d)=>fileName.startsWith(d))) {
continue;
}
const meta = knownFiles.get(fileName);
const watchTime = fileWatchTimes.get(fileName);
// If the file is showing up for the first time or the meta.timestamp is changed since last time
const watchTimeChange = watchTime === undefined || watchTime && watchTime !== (meta == null ? void 0 : meta.timestamp);
fileWatchTimes.set(fileName, meta == null ? void 0 : meta.timestamp);
if (envFiles.includes(fileName)) {
if (watchTimeChange) {
envChange = true;
}
continue;
}
if (tsconfigPaths.includes(fileName)) {
if (fileName.endsWith('tsconfig.json')) {
enabledTypeScript = true;
}
if (watchTimeChange) {
tsconfigChange = true;
}
continue;
}
if ((meta == null ? void 0 : meta.accuracy) === undefined || !validFileMatcher.isPageFile(fileName)) {
continue;
}
const isAppPath = Boolean(appDir && normalizePathSep(fileName).startsWith(normalizePathSep(appDir) + '/'));
const isPagePath = Boolean(pagesDir && normalizePathSep(fileName).startsWith(normalizePathSep(pagesDir) + '/'));
const rootFile = absolutePathToPage(fileName, {
dir: dir,
extensions: nextConfig.pageExtensions,
keepIndex: false,
pagesType: PAGE_TYPES.ROOT
});
if (isMiddlewareFile(rootFile)) {
var _staticInfo_middleware;
const staticInfo = await getStaticInfoIncludingLayouts({
pageFilePath: fileName,
config: nextConfig,
appDir: appDir,
page: rootFile,
isDev: true,
isInsideAppDir: isAppPath,
pageExtensions: nextConfig.pageExtensions
});
if (nextConfig.output === 'export') {
Log.error('Middleware cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export');
continue;
}
serverFields.actualMiddlewareFile = rootFile;
await propagateServerField(opts, 'actualMiddlewareFile', serverFields.actualMiddlewareFile);
middlewareMatchers = ((_staticInfo_middleware = staticInfo.middleware) == null ? void 0 : _staticInfo_middleware.matchers) || [
{
regexp: '.*',
originalSource: '/:path*'
}
];
continue;
}
if (isInstrumentationHookFile(rootFile)) {
serverFields.actualInstrumentationHookFile = rootFile;
await propagateServerField(opts, 'actualInstrumentationHookFile', serverFields.actualInstrumentationHookFile);
continue;
}
if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) {
enabledTypeScript = true;
}
if (!(isAppPath || isPagePath)) {
continue;
}
// Collect all current filenames for the TS plugin to use
devPageFiles.add(fileName);
let pageName = absolutePathToPage(fileName, {
dir: isAppPath ? appDir : pagesDir,
extensions: nextConfig.pageExtensions,
keepIndex: isAppPath,
pagesType: isAppPath ? PAGE_TYPES.APP : PAGE_TYPES.PAGES
});
if (isAppPath && appDir && isMetadataRouteFile(fileName.replace(appDir, ''), nextConfig.pageExtensions, true)) {
const staticInfo = await getPageStaticInfo({
pageFilePath: fileName,
nextConfig: {},
page: pageName,
isDev: true,
pageType: PAGE_TYPES.APP
});
pageName = normalizeMetadataPageToRoute(pageName, !!(staticInfo.generateSitemaps || staticInfo.generateImageMetadata));
}
if (!isAppPath && pageName.startsWith('/api/') && nextConfig.output === 'export') {
Log.error('API Routes cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export');
continue;
}
if (isAppPath) {
const isRootNotFound = validFileMatcher.isRootNotFound(fileName);
hasRootAppNotFound = true;
if (isRootNotFound) {
continue;
}
if (!isRootNotFound && !validFileMatcher.isAppRouterPage(fileName)) {
continue;
}
// Ignore files/directories starting with `_` in the app directory
if (normalizePathSep(pageName).includes('/_')) {
continue;
}
const originalPageName = pageName;
pageName = normalizeAppPath(pageName).replace(/%5F/g, '_');
if (!appPaths[pageName]) {
appPaths[pageName] = [];
}
appPaths[pageName].push(originalPageName);
if (useFileSystemPublicRoutes) {
appFiles.add(pageName);
}
if (routedPages.includes(pageName)) {
continue;
}
} else {
if (useFileSystemPublicRoutes) {
pageFiles.add(pageName);
// always add to nextDataRoutes for now but in future only add
// entries that actually use getStaticProps/getServerSideProps
opts.fsChecker.nextDataRoutes.add(pageName);
}
}
;
(isAppPath ? appPageFilePaths : pagesPageFilePaths).set(pageName, fileName);
if (appDir && pageNameSet.has(pageName)) {
conflictingAppPagePaths.add(pageName);
} else {
pageNameSet.add(pageName);
}
/**
* If there is a middleware that is not declared in the root we will
* warn without adding it so it doesn't make its way into the system.
*/ if (/[\\\\/]_middleware$/.test(pageName)) {
nestedMiddleware.push(pageName);
continue;
}
routedPages.push(pageName);
}
const numConflicting = conflictingAppPagePaths.size;
conflictingPageChange = numConflicting - previousConflictingPagePaths.size;
if (conflictingPageChange !== 0) {
if (numConflicting > 0) {
let errorMessage = `Conflicting app and page file${numConflicting === 1 ? ' was' : 's were'} found, please remove the conflicting files to continue:\n`;
for (const p of conflictingAppPagePaths){
const appPath = path.relative(dir, appPageFilePaths.get(p));
const pagesPath = path.relative(dir, pagesPageFilePaths.get(p));
errorMessage += ` "${pagesPath}" - "${appPath}"\n`;
}
hotReloader.setHmrServerError(Object.defineProperty(new Error(errorMessage), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
}));
} else if (numConflicting === 0) {
hotReloader.clearHmrServerError();
await propagateServerField(opts, 'reloadMatchers', undefined);
}
}
previousConflictingPagePaths = conflictingAppPagePaths;
let clientRouterFilters;
if (nextConfig.experimental.clientRouterFilter) {
clientRouterFilters = createClientRouterFilter(Object.keys(appPaths), nextConfig.experimental.clientRouterFilterRedirects ? (nextConfig._originalRedirects || []).filter((r)=>!r.internal) : [], nextConfig.experimental.clientRouterFilterAllowedRate);
if (!previousClientRouterFilters || JSON.stringify(previousClientRouterFilters) !== JSON.stringify(clientRouterFilters)) {
envChange = true;
previousClientRouterFilters = clientRouterFilters;
}
}
if (!usingTypeScript && enabledTypeScript) {
// we tolerate the error here as this is best effort
// and the manual install command will be shown
await verifyTypeScript(opts).then(()=>{
tsconfigChange = true;
}).catch(()=>{});
}
if (envChange || tsconfigChange) {
var _hotReloader_activeWebpackConfigs;
if (envChange) {
var _nextConfig_experimental;
const { loadedEnvFiles } = loadEnvConfig(dir, process.env.NODE_ENV === 'development', Log, true, (envFilePath)=>{
Log.info(`Reload env: ${envFilePath}`);
});
if (usingTypeScript && ((_nextConfig_experimental = nextConfig.experimental) == null ? void 0 : _nextConfig_experimental.typedEnv)) {
// do not await, this is not essential for further process
createEnvDefinitions({
distDir,
loadedEnvFiles: [
...loadedEnvFiles,
{
path: nextConfig.configFileName,
env: nextConfig.env,
contents: ''
}
]
});
}
await propagateServerField(opts, 'loadEnvConfig', [
{
dev: true,
forceReload: true,
silent: true
}
]);
}
let tsconfigResult;
if (tsconfigChange) {
try {
tsconfigResult = await loadJsConfig(dir, nextConfig);
} catch (_) {
/* do we want to log if there are syntax errors in tsconfig while editing? */ }
}
if (hotReloader.turbopackProject) {
const hasRewrites = opts.fsChecker.rewrites.afterFiles.length > 0 || opts.fsChecker.rewrites.beforeFiles.length > 0 || opts.fsChecker.rewrites.fallback.length > 0;
await hotReloader.turbopackProject.update({
defineEnv: createDefineEnv({
isTurbopack: true,
clientRouterFilters,
config: nextConfig,
dev: true,
distDir,
fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix,
hasRewrites,
// TODO: Implement
middlewareMatchers: undefined
})
});
}
(_hotReloader_activeWebpackConfigs = hotReloader.activeWebpackConfigs) == null ? void 0 : _hotReloader_activeWebpackConfigs.forEach((config, idx)=>{
const isClient = idx === 0;
const isNodeServer = idx === 1;
const isEdgeServer = idx === 2;
const hasRewrites = opts.fsChecker.rewrites.afterFiles.length > 0 || opts.fsChecker.rewrites.beforeFiles.length > 0 || opts.fsChecker.rewrites.fallback.length > 0;
if (tsconfigChange) {
var _config_resolve_plugins, _config_resolve;
(_config_resolve = config.resolve) == null ? void 0 : (_config_resolve_plugins = _config_resolve.plugins) == null ? void 0 : _config_resolve_plugins.forEach((plugin)=>{
// look for the JsConfigPathsPlugin and update with
// the latest paths/baseUrl config
if (plugin instanceof JsConfigPathsPlugin && tsconfigResult) {
var _config_resolve_modules, _config_resolve, _jsConfig_compilerOptions;
const { resolvedBaseUrl, jsConfig } = tsconfigResult;
const currentResolvedBaseUrl = plugin.resolvedBaseUrl;
const resolvedUrlIndex = (_config_resolve = config.resolve) == null ? void 0 : (_config_resolve_modules = _config_resolve.modules) == null ? void 0 : _config_resolve_modules.findIndex((item)=>item === (currentResolvedBaseUrl == null ? void 0 : currentResolvedBaseUrl.baseUrl));
if (resolvedBaseUrl) {
if (resolvedBaseUrl.baseUrl !== (currentResolvedBaseUrl == null ? void 0 : currentResolvedBaseUrl.baseUrl)) {
// remove old baseUrl and add new one
if (resolvedUrlIndex && resolvedUrlIndex > -1) {
var _config_resolve_modules1, _config_resolve1;
(_config_resolve1 = config.resolve) == null ? void 0 : (_config_resolve_modules1 = _config_resolve1.modules) == null ? void 0 : _config_resolve_modules1.splice(resolvedUrlIndex, 1);
}
// If the resolvedBaseUrl is implicit we only remove the previous value.
// Only add the baseUrl if it's explicitly set in tsconfig/jsconfig
if (!resolvedBaseUrl.isImplicit) {
var _config_resolve_modules2, _config_resolve2;
(_config_resolve2 = config.resolve) == null ? void 0 : (_config_resolve_modules2 = _config_resolve2.modules) == null ? void 0 : _config_resolve_modules2.push(resolvedBaseUrl.baseUrl);
}
}
}
if ((jsConfig == null ? void 0 : (_jsConfig_compilerOptions = jsConfig.compilerOptions) == null ? void 0 : _jsConfig_compilerOptions.paths) && resolvedBaseUrl) {
Object.keys(plugin.paths).forEach((key)=>{
delete plugin.paths[key];
});
Object.assign(plugin.paths, jsConfig.compilerOptions.paths);
plugin.resolvedBaseUrl = resolvedBaseUrl;
}
}
});
}
if (envChange) {
var _config_plugins;
(_config_plugins = config.plugins) == null ? void 0 : _config_plugins.forEach((plugin)=>{
// we look for the DefinePlugin definitions so we can
// update them on the active compilers
if (plugin && typeof plugin.definitions === 'object' && plugin.definitions.__NEXT_DEFINE_ENV) {
const newDefine = getDefineEnv({
isTurbopack: false,
clientRouterFilters,
config: nextConfig,
dev: true,
distDir,
fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix,
hasRewrites,
isClient,
isEdgeServer,
isNodeOrEdgeCompilation: isNodeServer || isEdgeServer,
isNodeServer,
middlewareMatchers: undefined
});
Object.keys(plugin.definitions).forEach((key)=>{
if (!(key in newDefine)) {
delete plugin.definitions[key];
}
});
Object.assign(plugin.definitions, newDefine);
}
});
}
});
await hotReloader.invalidate({
reloadAfterInvalidation: envChange
});
}
if (nestedMiddleware.length > 0) {
Log.error(Object.defineProperty(new NestedMiddlewareError(nestedMiddleware, dir, pagesDir || appDir), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
}).message);
nestedMiddleware = [];
}
// Make sure to sort parallel routes to make the result deterministic.
serverFields.appPathRoutes = Object.fromEntries(Object.entries(appPaths).map(([k, v])=>[
k,
v.sort()
]));
await propagateServerField(opts, 'appPathRoutes', serverFields.appPathRoutes);
// TODO: pass this to fsChecker/next-dev-server?
serverFields.middleware = middlewareMatchers ? {
match: null,
page: '/',
matchers: middlewareMatchers
} : undefined;
await propagateServerField(opts, 'middleware', serverFields.middleware);
serverFields.hasAppNotFound = hasRootAppNotFound;
opts.fsChecker.middlewareMatcher = ((_serverFields_middleware = serverFields.middleware) == null ? void 0 : _serverFields_middleware.matchers) ? getMiddlewareRouteMatcher((_serverFields_middleware1 = serverFields.middleware) == null ? void 0 : _serverFields_middleware1.matchers) : undefined;
const interceptionRoutes = generateInterceptionRoutesRewrites(Object.keys(appPaths), opts.nextConfig.basePath).map((item)=>buildCustomRoute('before_files_rewrite', item, opts.nextConfig.basePath, opts.nextConfig.experimental.caseSensitiveRoutes));
opts.fsChecker.rewrites.beforeFiles.push(...interceptionRoutes);
const exportPathMap = typeof nextConfig.exportPathMap === 'function' && await (nextConfig.exportPathMap == null ? void 0 : nextConfig.exportPathMap.call(nextConfig, {}, {
dev: true,
dir: opts.dir,
outDir: null,
distDir: distDir,
buildId: 'development'
})) || {};
const exportPathMapEntries = Object.entries(exportPathMap || {});
if (exportPathMapEntries.length > 0) {
opts.fsChecker.exportPathMapRoutes = exportPathMapEntries.map(([key, value])=>buildCustomRoute('before_files_rewrite', {
source: key,
destination: `${value.page}${value.query ? '?' : ''}${qs.stringify(value.query)}`
}, opts.nextConfig.basePath, opts.nextConfig.experimental.caseSensitiveRoutes));
}
try {
// we serve a separate manifest with all pages for the client in
// dev mode so that we can match a page after a rewrite on the client
// before it has been built and is populated in the _buildManifest
const sortedRoutes = getSortedRoutes(routedPages);
opts.fsChecker.dynamicRoutes = sortedRoutes.map((page)=>{
const regex = getRouteRegex(page);
return {
regex: regex.re.toString(),
match: getRouteMatcher(regex),
page
};
});
const dataRoutes = [];
for (const page of sortedRoutes){
const route = buildDataRoute(page, 'development');
const routeRegex = getRouteRegex(route.page);
dataRoutes.push({
...route,
regex: routeRegex.re.toString(),
match: getRouteMatcher({
// TODO: fix this in the manifest itself, must also be fixed in
// upstream builder that relies on this
re: opts.nextConfig.i18n ? new RegExp(route.dataRouteRegex.replace(`/development/`, `/development/(?<nextLocale>[^/]+?)/`)) : new RegExp(route.dataRouteRegex),
groups: routeRegex.groups
})
});
}
opts.fsChecker.dynamicRoutes.unshift(...dataRoutes);
if (!(prevSortedRoutes == null ? void 0 : prevSortedRoutes.every((val, idx)=>val === sortedRoutes[idx]))) {
const addedRoutes = sortedRoutes.filter((route)=>!prevSortedRoutes.includes(route));
const removedRoutes = prevSortedRoutes.filter((route)=>!sortedRoutes.includes(route));
// emit the change so clients fetch the update
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE,
data: [
{
devPagesManifest: true
}
]
});
addedRoutes.forEach((route)=>{
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.ADDED_PAGE,
data: [
route
]
});
});
removedRoutes.forEach((route)=>{
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.REMOVED_PAGE,
data: [
route
]
});
});
}
prevSortedRoutes = sortedRoutes;
if (!resolved) {
resolve();
resolved = true;
}
} catch (e) {
if (!resolved) {
reject(e);
resolved = true;
} else {
Log.warn('Failed to reload dynamic routes:', e);
}
} finally{
// Reload the matchers. The filesystem would have been written to,
// and the matchers need to re-scan it to update the router.
await propagateServerField(opts, 'reloadMatchers', undefined);
}
});
wp.watch({
directories: [
dir
],
startTime: 0
});
});
const clientPagesManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_PAGES_MANIFEST}`;
opts.fsChecker.devVirtualFsItems.add(clientPagesManifestPath);
const devMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_MIDDLEWARE_MANIFEST}`;
opts.fsChecker.devVirtualFsItems.add(devMiddlewareManifestPath);
const devTurbopackMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`;
opts.fsChecker.devVirtualFsItems.add(devTurbopackMiddlewareManifestPath);
async function requestHandler(req, res) {
var _parsedUrl_pathname, _parsedUrl_pathname1, _parsedUrl_pathname2;
const parsedUrl = url.parse(req.url || '/');
if ((_parsedUrl_pathname = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname.includes(clientPagesManifestPath)) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify({
pages: prevSortedRoutes.filter((route)=>!opts.fsChecker.appFiles.has(route))
}));
return {
finished: true
};
}
if (((_parsedUrl_pathname1 = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname1.includes(devMiddlewareManifestPath)) || ((_parsedUrl_pathname2 = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname2.includes(devTurbopackMiddlewareManifestPath))) {
var _serverFields_middleware;
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify(((_serverFields_middleware = serverFields.middleware) == null ? void 0 : _serverFields_middleware.matchers) || []));
return {
finished: true
};
}
return {
finished: false
};
}
function logErrorWithOriginalStack(err, type) {
if (err instanceof ModuleBuildError) {
// Errors that may come from issues from the user's code
Log.error(err.message);
} else if (err instanceof TurbopackInternalError) {
// An internal Turbopack error that has been handled by next-swc, written
// to disk and a simplified message shown to user on the Rust side.
} else if (type === 'warning') {
Log.warn(err);
} else if (type === 'app-dir') {
Log.error(err);
} else if (type) {
Log.error(`${type}:`, err);
} else {
Log.error(err);
}
}
return {
serverFields,
hotReloader,
requestHandler,
logErrorWithOriginalStack,
async ensureMiddleware (requestUrl) {
if (!serverFields.actualMiddlewareFile) return;
return hotReloader.ensurePage({
page: serverFields.actualMiddlewareFile,
clientOnly: false,
definition: undefined,
url: requestUrl
});
}
};
}
export async function setupDevBundler(opts) {
const isSrcDir = path.relative(opts.dir, opts.pagesDir || opts.appDir || '').startsWith('src');
const result = await startWatcher(opts);
opts.telemetry.record(eventCliSession(path.join(opts.dir, opts.nextConfig.distDir), opts.nextConfig, {
webpackVersion: 5,
isSrcDir,
turboFlag: !!opts.turbo,
cliCommand: 'dev',
appDir: !!opts.appDir,
pagesDir: !!opts.pagesDir,
isCustomServer: !!opts.isCustomServer,
hasNowJson: !!await findUp('now.json', {
cwd: opts.dir
})
}));
return result;
}
// Returns a trace rewritten through Turbopack's sourcemaps
//# sourceMappingURL=setup-dev-bundler.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
export { };
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/types.ts"],"sourcesContent":["export type PropagateToWorkersField =\n | 'actualMiddlewareFile'\n | 'actualInstrumentationHookFile'\n | 'reloadMatchers'\n | 'loadEnvConfig'\n | 'appPathRoutes'\n | 'middleware'\n | 'renderOpts'\n"],"names":[],"mappings":"AAAA,WAOgB"}

View File

@@ -0,0 +1,28 @@
import { ACTION_HEADER } from '../../client/components/app-router-headers';
export function getServerActionRequestMetadata(req) {
let actionId;
let contentType;
if (req.headers instanceof Headers) {
actionId = req.headers.get(ACTION_HEADER.toLowerCase()) ?? null;
contentType = req.headers.get('content-type');
} else {
actionId = req.headers[ACTION_HEADER.toLowerCase()] ?? null;
contentType = req.headers['content-type'] ?? null;
}
const isURLEncodedAction = Boolean(req.method === 'POST' && contentType === 'application/x-www-form-urlencoded');
const isMultipartAction = Boolean(req.method === 'POST' && (contentType == null ? void 0 : contentType.startsWith('multipart/form-data')));
const isFetchAction = Boolean(actionId !== undefined && typeof actionId === 'string' && req.method === 'POST');
const isServerAction = Boolean(isFetchAction || isURLEncodedAction || isMultipartAction);
return {
actionId,
isURLEncodedAction,
isMultipartAction,
isFetchAction,
isServerAction
};
}
export function getIsServerAction(req) {
return getServerActionRequestMetadata(req).isServerAction;
}
//# sourceMappingURL=server-action-request-meta.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/server-action-request-meta.ts"],"sourcesContent":["import type { IncomingMessage } from 'http'\nimport type { BaseNextRequest } from '../base-http'\nimport type { NextRequest } from '../web/exports'\nimport { ACTION_HEADER } from '../../client/components/app-router-headers'\n\nexport function getServerActionRequestMetadata(\n req: IncomingMessage | BaseNextRequest | NextRequest\n): {\n actionId: string | null\n isURLEncodedAction: boolean\n isMultipartAction: boolean\n isFetchAction: boolean\n isServerAction: boolean\n} {\n let actionId: string | null\n let contentType: string | null\n\n if (req.headers instanceof Headers) {\n actionId = req.headers.get(ACTION_HEADER.toLowerCase()) ?? null\n contentType = req.headers.get('content-type')\n } else {\n actionId = (req.headers[ACTION_HEADER.toLowerCase()] as string) ?? null\n contentType = req.headers['content-type'] ?? null\n }\n\n const isURLEncodedAction = Boolean(\n req.method === 'POST' && contentType === 'application/x-www-form-urlencoded'\n )\n const isMultipartAction = Boolean(\n req.method === 'POST' && contentType?.startsWith('multipart/form-data')\n )\n const isFetchAction = Boolean(\n actionId !== undefined &&\n typeof actionId === 'string' &&\n req.method === 'POST'\n )\n\n const isServerAction = Boolean(\n isFetchAction || isURLEncodedAction || isMultipartAction\n )\n\n return {\n actionId,\n isURLEncodedAction,\n isMultipartAction,\n isFetchAction,\n isServerAction,\n }\n}\n\nexport function getIsServerAction(\n req: IncomingMessage | BaseNextRequest | NextRequest\n): boolean {\n return getServerActionRequestMetadata(req).isServerAction\n}\n"],"names":["ACTION_HEADER","getServerActionRequestMetadata","req","actionId","contentType","headers","Headers","get","toLowerCase","isURLEncodedAction","Boolean","method","isMultipartAction","startsWith","isFetchAction","undefined","isServerAction","getIsServerAction"],"mappings":"AAGA,SAASA,aAAa,QAAQ,6CAA4C;AAE1E,OAAO,SAASC,+BACdC,GAAoD;IAQpD,IAAIC;IACJ,IAAIC;IAEJ,IAAIF,IAAIG,OAAO,YAAYC,SAAS;QAClCH,WAAWD,IAAIG,OAAO,CAACE,GAAG,CAACP,cAAcQ,WAAW,OAAO;QAC3DJ,cAAcF,IAAIG,OAAO,CAACE,GAAG,CAAC;IAChC,OAAO;QACLJ,WAAW,AAACD,IAAIG,OAAO,CAACL,cAAcQ,WAAW,GAAG,IAAe;QACnEJ,cAAcF,IAAIG,OAAO,CAAC,eAAe,IAAI;IAC/C;IAEA,MAAMI,qBAAqBC,QACzBR,IAAIS,MAAM,KAAK,UAAUP,gBAAgB;IAE3C,MAAMQ,oBAAoBF,QACxBR,IAAIS,MAAM,KAAK,WAAUP,+BAAAA,YAAaS,UAAU,CAAC;IAEnD,MAAMC,gBAAgBJ,QACpBP,aAAaY,aACX,OAAOZ,aAAa,YACpBD,IAAIS,MAAM,KAAK;IAGnB,MAAMK,iBAAiBN,QACrBI,iBAAiBL,sBAAsBG;IAGzC,OAAO;QACLT;QACAM;QACAG;QACAE;QACAE;IACF;AACF;AAEA,OAAO,SAASC,kBACdf,GAAoD;IAEpD,OAAOD,+BAA+BC,KAAKc,cAAc;AAC3D"}

View File

@@ -0,0 +1,50 @@
export const ipcForbiddenHeaders = [
'accept-encoding',
'keepalive',
'keep-alive',
'content-encoding',
'transfer-encoding',
// https://github.com/nodejs/undici/issues/1470
'connection',
// marked as unsupported by undici: https://github.com/nodejs/undici/blob/c83b084879fa0bb8e0469d31ec61428ac68160d5/lib/core/request.js#L354
'expect'
];
export const actionsForbiddenHeaders = [
...ipcForbiddenHeaders,
'content-length',
'set-cookie'
];
export const filterReqHeaders = (headers, forbiddenHeaders)=>{
// Some browsers are not matching spec and sending Content-Length: 0. This causes issues in undici
// https://github.com/nodejs/undici/issues/2046
if (headers['content-length'] && headers['content-length'] === '0') {
delete headers['content-length'];
}
for (const [key, value] of Object.entries(headers)){
if (forbiddenHeaders.includes(key) || !(Array.isArray(value) || typeof value === 'string')) {
delete headers[key];
}
}
return headers;
};
// These are headers that are only used internally and should
// not be honored from the external request
const INTERNAL_HEADERS = [
'x-middleware-rewrite',
'x-middleware-redirect',
'x-middleware-set-cookie',
'x-middleware-skip',
'x-middleware-override-headers',
'x-middleware-next',
'x-now-route-matches',
'x-matched-path'
];
export const filterInternalHeaders = (headers)=>{
for(const header in headers){
if (INTERNAL_HEADERS.includes(header)) {
delete headers[header];
}
}
};
//# sourceMappingURL=utils.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/server-ipc/utils.ts"],"sourcesContent":["export const ipcForbiddenHeaders = [\n 'accept-encoding',\n 'keepalive',\n 'keep-alive',\n 'content-encoding',\n 'transfer-encoding',\n // https://github.com/nodejs/undici/issues/1470\n 'connection',\n // marked as unsupported by undici: https://github.com/nodejs/undici/blob/c83b084879fa0bb8e0469d31ec61428ac68160d5/lib/core/request.js#L354\n 'expect',\n]\n\nexport const actionsForbiddenHeaders = [\n ...ipcForbiddenHeaders,\n 'content-length',\n 'set-cookie',\n]\n\nexport const filterReqHeaders = (\n headers: Record<string, undefined | string | number | string[]>,\n forbiddenHeaders: string[]\n) => {\n // Some browsers are not matching spec and sending Content-Length: 0. This causes issues in undici\n // https://github.com/nodejs/undici/issues/2046\n if (headers['content-length'] && headers['content-length'] === '0') {\n delete headers['content-length']\n }\n\n for (const [key, value] of Object.entries(headers)) {\n if (\n forbiddenHeaders.includes(key) ||\n !(Array.isArray(value) || typeof value === 'string')\n ) {\n delete headers[key]\n }\n }\n return headers as Record<string, undefined | string | string[]>\n}\n\n// These are headers that are only used internally and should\n// not be honored from the external request\nconst INTERNAL_HEADERS = [\n 'x-middleware-rewrite',\n 'x-middleware-redirect',\n 'x-middleware-set-cookie',\n 'x-middleware-skip',\n 'x-middleware-override-headers',\n 'x-middleware-next',\n 'x-now-route-matches',\n 'x-matched-path',\n]\n\nexport const filterInternalHeaders = (\n headers: Record<string, undefined | string | string[]>\n) => {\n for (const header in headers) {\n if (INTERNAL_HEADERS.includes(header)) {\n delete headers[header]\n }\n }\n}\n"],"names":["ipcForbiddenHeaders","actionsForbiddenHeaders","filterReqHeaders","headers","forbiddenHeaders","key","value","Object","entries","includes","Array","isArray","INTERNAL_HEADERS","filterInternalHeaders","header"],"mappings":"AAAA,OAAO,MAAMA,sBAAsB;IACjC;IACA;IACA;IACA;IACA;IACA,+CAA+C;IAC/C;IACA,2IAA2I;IAC3I;CACD,CAAA;AAED,OAAO,MAAMC,0BAA0B;OAClCD;IACH;IACA;CACD,CAAA;AAED,OAAO,MAAME,mBAAmB,CAC9BC,SACAC;IAEA,kGAAkG;IAClG,+CAA+C;IAC/C,IAAID,OAAO,CAAC,iBAAiB,IAAIA,OAAO,CAAC,iBAAiB,KAAK,KAAK;QAClE,OAAOA,OAAO,CAAC,iBAAiB;IAClC;IAEA,KAAK,MAAM,CAACE,KAAKC,MAAM,IAAIC,OAAOC,OAAO,CAACL,SAAU;QAClD,IACEC,iBAAiBK,QAAQ,CAACJ,QAC1B,CAAEK,CAAAA,MAAMC,OAAO,CAACL,UAAU,OAAOA,UAAU,QAAO,GAClD;YACA,OAAOH,OAAO,CAACE,IAAI;QACrB;IACF;IACA,OAAOF;AACT,EAAC;AAED,6DAA6D;AAC7D,2CAA2C;AAC3C,MAAMS,mBAAmB;IACvB;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD;AAED,OAAO,MAAMC,wBAAwB,CACnCV;IAEA,IAAK,MAAMW,UAAUX,QAAS;QAC5B,IAAIS,iBAAiBH,QAAQ,CAACK,SAAS;YACrC,OAAOX,OAAO,CAACW,OAAO;QACxB;IACF;AACF,EAAC"}

312
node_modules/next/dist/esm/server/lib/start-server.js generated vendored Normal file
View File

@@ -0,0 +1,312 @@
import { getNetworkHost } from '../../lib/get-network-host';
if (performance.getEntriesByName('next-start').length === 0) {
performance.mark('next-start');
}
import '../next';
import '../require-hook';
import fs from 'fs';
import v8 from 'v8';
import path from 'path';
import http from 'http';
import https from 'https';
import os from 'os';
import Watchpack from 'next/dist/compiled/watchpack';
import * as Log from '../../build/output/log';
import setupDebug from 'next/dist/compiled/debug';
import { RESTART_EXIT_CODE, getFormattedDebugAddress, getNodeDebugType } from './utils';
import { formatHostname } from './format-hostname';
import { initialize } from './router-server';
import { CONFIG_FILES } from '../../shared/lib/constants';
import { getStartServerInfo, logStartInfo } from './app-info-log';
import { validateTurboNextConfig } from '../../lib/turbopack-warning';
import { trace, flushAllTraces } from '../../trace';
import { isPostpone } from './router-utils/is-postpone';
import { isIPv6 } from './is-ipv6';
import { AsyncCallbackSet } from './async-callback-set';
const debug = setupDebug('next:start-server');
let startServerSpan;
export async function getRequestHandlers({ dir, port, isDev, onDevServerCleanup, server, hostname, minimalMode, keepAliveTimeout, experimentalHttpsServer, quiet }) {
return initialize({
dir,
port,
hostname,
onDevServerCleanup,
dev: isDev,
minimalMode,
server,
keepAliveTimeout,
experimentalHttpsServer,
startServerSpan,
quiet
});
}
export async function startServer(serverOptions) {
const { dir, isDev, hostname, minimalMode, allowRetry, keepAliveTimeout, selfSignedCertificate } = serverOptions;
let { port } = serverOptions;
process.title = `next-server (v${"15.2.4"})`;
let handlersReady = ()=>{};
let handlersError = ()=>{};
let handlersPromise = new Promise((resolve, reject)=>{
handlersReady = resolve;
handlersError = reject;
});
let requestHandler = async (req, res)=>{
if (handlersPromise) {
await handlersPromise;
return requestHandler(req, res);
}
throw Object.defineProperty(new Error('Invariant request handler was not setup'), "__NEXT_ERROR_CODE", {
value: "E287",
enumerable: false,
configurable: true
});
};
let upgradeHandler = async (req, socket, head)=>{
if (handlersPromise) {
await handlersPromise;
return upgradeHandler(req, socket, head);
}
throw Object.defineProperty(new Error('Invariant upgrade handler was not setup'), "__NEXT_ERROR_CODE", {
value: "E290",
enumerable: false,
configurable: true
});
};
let nextServer;
// setup server listener as fast as possible
if (selfSignedCertificate && !isDev) {
throw Object.defineProperty(new Error('Using a self signed certificate is only supported with `next dev`.'), "__NEXT_ERROR_CODE", {
value: "E128",
enumerable: false,
configurable: true
});
}
async function requestListener(req, res) {
try {
if (handlersPromise) {
await handlersPromise;
handlersPromise = undefined;
}
await requestHandler(req, res);
} catch (err) {
res.statusCode = 500;
res.end('Internal Server Error');
Log.error(`Failed to handle request for ${req.url}`);
console.error(err);
} finally{
if (isDev) {
if (v8.getHeapStatistics().used_heap_size > 0.8 * v8.getHeapStatistics().heap_size_limit) {
Log.warn(`Server is approaching the used memory threshold, restarting...`);
trace('server-restart-close-to-memory-threshold', undefined, {
'memory.heapSizeLimit': String(v8.getHeapStatistics().heap_size_limit),
'memory.heapUsed': String(v8.getHeapStatistics().used_heap_size)
}).stop();
await flushAllTraces();
process.exit(RESTART_EXIT_CODE);
}
}
}
}
const server = selfSignedCertificate ? https.createServer({
key: fs.readFileSync(selfSignedCertificate.key),
cert: fs.readFileSync(selfSignedCertificate.cert)
}, requestListener) : http.createServer(requestListener);
if (keepAliveTimeout) {
server.keepAliveTimeout = keepAliveTimeout;
}
server.on('upgrade', async (req, socket, head)=>{
try {
await upgradeHandler(req, socket, head);
} catch (err) {
socket.destroy();
Log.error(`Failed to handle request for ${req.url}`);
console.error(err);
}
});
let portRetryCount = 0;
server.on('error', (err)=>{
if (allowRetry && port && isDev && err.code === 'EADDRINUSE' && portRetryCount < 10) {
Log.warn(`Port ${port} is in use, trying ${port + 1} instead.`);
port += 1;
portRetryCount += 1;
server.listen(port, hostname);
} else {
Log.error(`Failed to start server`);
console.error(err);
process.exit(1);
}
});
let cleanupListeners = isDev ? new AsyncCallbackSet() : undefined;
await new Promise((resolve)=>{
server.on('listening', async ()=>{
const nodeDebugType = getNodeDebugType();
const addr = server.address();
const actualHostname = formatHostname(typeof addr === 'object' ? (addr == null ? void 0 : addr.address) || hostname || 'localhost' : addr);
const formattedHostname = !hostname || actualHostname === '0.0.0.0' ? 'localhost' : actualHostname === '[::]' ? '[::1]' : formatHostname(hostname);
port = typeof addr === 'object' ? (addr == null ? void 0 : addr.port) || port : port;
const networkHostname = hostname ?? getNetworkHost(isIPv6(actualHostname) ? 'IPv6' : 'IPv4');
const protocol = selfSignedCertificate ? 'https' : 'http';
const networkUrl = networkHostname ? `${protocol}://${formatHostname(networkHostname)}:${port}` : null;
const appUrl = `${protocol}://${formattedHostname}:${port}`;
if (nodeDebugType) {
const formattedDebugAddress = getFormattedDebugAddress();
Log.info(`the --${nodeDebugType} option was detected, the Next.js router server should be inspected at ${formattedDebugAddress}.`);
}
// Store the selected port to:
// - expose it to render workers
// - re-use it for automatic dev server restarts with a randomly selected port
process.env.PORT = port + '';
process.env.__NEXT_PRIVATE_ORIGIN = appUrl;
// Only load env and config in dev to for logging purposes
let envInfo;
let experimentalFeatures;
if (isDev) {
const startServerInfo = await getStartServerInfo(dir, isDev);
envInfo = startServerInfo.envInfo;
experimentalFeatures = startServerInfo.experimentalFeatures;
}
logStartInfo({
networkUrl,
appUrl,
envInfo,
experimentalFeatures,
maxExperimentalFeatures: 3
});
Log.event(`Starting...`);
try {
let cleanupStarted = false;
let closeUpgraded = null;
const cleanup = ()=>{
if (cleanupStarted) {
// We can get duplicate signals, e.g. when `ctrl+c` is used in an
// interactive shell (i.e. bash, zsh), the shell will recursively
// send SIGINT to children. The parent `next-dev` process will also
// send us SIGINT.
return;
}
cleanupStarted = true;
(async ()=>{
debug('start-server process cleanup');
// first, stop accepting new connections and finish pending requests,
// because they might affect `nextServer.close()` (e.g. by scheduling an `after`)
await new Promise((res)=>{
server.close((err)=>{
if (err) console.error(err);
res();
});
if (isDev) {
server.closeAllConnections();
closeUpgraded == null ? void 0 : closeUpgraded();
}
});
// now that no new requests can come in, clean up the rest
await Promise.all([
nextServer == null ? void 0 : nextServer.close().catch(console.error),
cleanupListeners == null ? void 0 : cleanupListeners.runAll().catch(console.error)
]);
debug('start-server process cleanup finished');
process.exit(0);
})();
};
const exception = (err)=>{
if (isPostpone(err)) {
// React postpones that are unhandled might end up logged here but they're
// not really errors. They're just part of rendering.
return;
}
// This is the render worker, we keep the process alive
console.error(err);
};
// Make sure commands gracefully respect termination signals (e.g. from Docker)
// Allow the graceful termination to be manually configurable
if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
}
process.on('rejectionHandled', ()=>{
// It is ok to await a Promise late in Next.js as it allows for better
// prefetching patterns to avoid waterfalls. We ignore loggining these.
// We should've already errored in anyway unhandledRejection.
});
process.on('uncaughtException', exception);
process.on('unhandledRejection', exception);
const initResult = await getRequestHandlers({
dir,
port,
isDev,
onDevServerCleanup: cleanupListeners ? cleanupListeners.add.bind(cleanupListeners) : undefined,
server,
hostname,
minimalMode,
keepAliveTimeout,
experimentalHttpsServer: !!selfSignedCertificate
});
requestHandler = initResult.requestHandler;
upgradeHandler = initResult.upgradeHandler;
nextServer = initResult.server;
closeUpgraded = initResult.closeUpgraded;
const startServerProcessDuration = performance.mark('next-start-end') && performance.measure('next-start-duration', 'next-start', 'next-start-end').duration;
handlersReady();
const formatDurationText = startServerProcessDuration > 2000 ? `${Math.round(startServerProcessDuration / 100) / 10}s` : `${Math.round(startServerProcessDuration)}ms`;
Log.event(`Ready in ${formatDurationText}`);
if (process.env.TURBOPACK) {
await validateTurboNextConfig({
dir: serverOptions.dir,
isDev: true
});
}
} catch (err) {
// fatal error if we can't setup
handlersError();
console.error(err);
process.exit(1);
}
resolve();
});
server.listen(port, hostname);
});
if (isDev) {
function watchConfigFiles(dirToWatch, onChange) {
const wp = new Watchpack();
wp.watch({
files: CONFIG_FILES.map((file)=>path.join(dirToWatch, file))
});
wp.on('change', onChange);
}
watchConfigFiles(dir, async (filename)=>{
if (process.env.__NEXT_DISABLE_MEMORY_WATCHER) {
Log.info(`Detected change, manual restart required due to '__NEXT_DISABLE_MEMORY_WATCHER' usage`);
return;
}
Log.warn(`Found a change in ${path.basename(filename)}. Restarting the server to apply the changes...`);
process.exit(RESTART_EXIT_CODE);
});
}
}
if (process.env.NEXT_PRIVATE_WORKER && process.send) {
process.addListener('message', async (msg)=>{
if (msg && typeof msg === 'object' && msg.nextWorkerOptions && process.send) {
startServerSpan = trace('start-dev-server', undefined, {
cpus: String(os.cpus().length),
platform: os.platform(),
'memory.freeMem': String(os.freemem()),
'memory.totalMem': String(os.totalmem()),
'memory.heapSizeLimit': String(v8.getHeapStatistics().heap_size_limit)
});
await startServerSpan.traceAsyncFn(()=>startServer(msg.nextWorkerOptions));
const memoryUsage = process.memoryUsage();
startServerSpan.setAttribute('memory.rss', String(memoryUsage.rss));
startServerSpan.setAttribute('memory.heapTotal', String(memoryUsage.heapTotal));
startServerSpan.setAttribute('memory.heapUsed', String(memoryUsage.heapUsed));
process.send({
nextServerReady: true,
port: process.env.PORT
});
}
});
process.send({
nextWorkerReady: true
});
}
//# sourceMappingURL=start-server.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,15 @@
import { getBotType, HTML_LIMITED_BOT_UA_RE_STRING } from '../../shared/lib/router/utils/is-bot';
export function shouldServeStreamingMetadata(userAgent, htmlLimitedBots) {
const blockingMetadataUARegex = new RegExp(htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING, 'i');
return(// When it's static generation, userAgents are not available - do not serve streaming metadata
!!userAgent && !blockingMetadataUARegex.test(userAgent));
}
// When the request UA is a html-limited bot, we should do a dynamic render.
// In this case, postpone state is not sent.
export function isHtmlBotRequest(req) {
const ua = req.headers['user-agent'] || '';
const botType = getBotType(ua);
return botType === 'html';
}
//# sourceMappingURL=streaming-metadata.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/streaming-metadata.ts"],"sourcesContent":["import {\n getBotType,\n HTML_LIMITED_BOT_UA_RE_STRING,\n} from '../../shared/lib/router/utils/is-bot'\nimport type { BaseNextRequest } from '../base-http'\n\nexport function shouldServeStreamingMetadata(\n userAgent: string,\n htmlLimitedBots: string | undefined\n): boolean {\n const blockingMetadataUARegex = new RegExp(\n htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING,\n 'i'\n )\n return (\n // When it's static generation, userAgents are not available - do not serve streaming metadata\n !!userAgent && !blockingMetadataUARegex.test(userAgent)\n )\n}\n\n// When the request UA is a html-limited bot, we should do a dynamic render.\n// In this case, postpone state is not sent.\nexport function isHtmlBotRequest(req: BaseNextRequest): boolean {\n const ua = req.headers['user-agent'] || ''\n const botType = getBotType(ua)\n\n return botType === 'html'\n}\n"],"names":["getBotType","HTML_LIMITED_BOT_UA_RE_STRING","shouldServeStreamingMetadata","userAgent","htmlLimitedBots","blockingMetadataUARegex","RegExp","test","isHtmlBotRequest","req","ua","headers","botType"],"mappings":"AAAA,SACEA,UAAU,EACVC,6BAA6B,QACxB,uCAAsC;AAG7C,OAAO,SAASC,6BACdC,SAAiB,EACjBC,eAAmC;IAEnC,MAAMC,0BAA0B,IAAIC,OAClCF,mBAAmBH,+BACnB;IAEF,OACE,8FAA8F;IAC9F,CAAC,CAACE,aAAa,CAACE,wBAAwBE,IAAI,CAACJ;AAEjD;AAEA,4EAA4E;AAC5E,4CAA4C;AAC5C,OAAO,SAASK,iBAAiBC,GAAoB;IACnD,MAAMC,KAAKD,IAAIE,OAAO,CAAC,aAAa,IAAI;IACxC,MAAMC,UAAUZ,WAAWU;IAE3B,OAAOE,YAAY;AACrB"}

27
node_modules/next/dist/esm/server/lib/to-route.js generated vendored Normal file
View File

@@ -0,0 +1,27 @@
/**
* This transforms a URL pathname into a route. It removes any trailing slashes
* and the `/index` suffix.
*
* @param {string} pathname - The URL path that needs to be optimized.
* @returns {string} - The route
*
* @example
* // returns '/example'
* toRoute('/example/index/');
*
* @example
* // returns '/example'
* toRoute('/example/');
*
* @example
* // returns '/'
* toRoute('/index/');
*
* @example
* // returns '/'
* toRoute('/');
*/ export function toRoute(pathname) {
return pathname.replace(/(?:\/index)?\/?$/, '') || '/';
}
//# sourceMappingURL=to-route.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/lib/to-route.ts"],"sourcesContent":["/**\n * This transforms a URL pathname into a route. It removes any trailing slashes\n * and the `/index` suffix.\n *\n * @param {string} pathname - The URL path that needs to be optimized.\n * @returns {string} - The route\n *\n * @example\n * // returns '/example'\n * toRoute('/example/index/');\n *\n * @example\n * // returns '/example'\n * toRoute('/example/');\n *\n * @example\n * // returns '/'\n * toRoute('/index/');\n *\n * @example\n * // returns '/'\n * toRoute('/');\n */\nexport function toRoute(pathname: string): string {\n return pathname.replace(/(?:\\/index)?\\/?$/, '') || '/'\n}\n"],"names":["toRoute","pathname","replace"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;CAsBC,GACD,OAAO,SAASA,QAAQC,QAAgB;IACtC,OAAOA,SAASC,OAAO,CAAC,oBAAoB,OAAO;AACrD"}

View File

@@ -0,0 +1,136 @@
/**
* Contains predefined constants for the trace span name in next/server.
*
* Currently, next/server/tracer is internal implementation only for tracking
* next.js's implementation only with known span names defined here.
**/ // eslint typescript has a bug with TS enums
/* eslint-disable no-shadow */ var BaseServerSpan = /*#__PURE__*/ function(BaseServerSpan) {
BaseServerSpan["handleRequest"] = "BaseServer.handleRequest";
BaseServerSpan["run"] = "BaseServer.run";
BaseServerSpan["pipe"] = "BaseServer.pipe";
BaseServerSpan["getStaticHTML"] = "BaseServer.getStaticHTML";
BaseServerSpan["render"] = "BaseServer.render";
BaseServerSpan["renderToResponseWithComponents"] = "BaseServer.renderToResponseWithComponents";
BaseServerSpan["renderToResponse"] = "BaseServer.renderToResponse";
BaseServerSpan["renderToHTML"] = "BaseServer.renderToHTML";
BaseServerSpan["renderError"] = "BaseServer.renderError";
BaseServerSpan["renderErrorToResponse"] = "BaseServer.renderErrorToResponse";
BaseServerSpan["renderErrorToHTML"] = "BaseServer.renderErrorToHTML";
BaseServerSpan["render404"] = "BaseServer.render404";
return BaseServerSpan;
}(BaseServerSpan || {});
var LoadComponentsSpan = /*#__PURE__*/ function(LoadComponentsSpan) {
LoadComponentsSpan["loadDefaultErrorComponents"] = "LoadComponents.loadDefaultErrorComponents";
LoadComponentsSpan["loadComponents"] = "LoadComponents.loadComponents";
return LoadComponentsSpan;
}(LoadComponentsSpan || {});
var NextServerSpan = /*#__PURE__*/ function(NextServerSpan) {
NextServerSpan["getRequestHandler"] = "NextServer.getRequestHandler";
NextServerSpan["getServer"] = "NextServer.getServer";
NextServerSpan["getServerRequestHandler"] = "NextServer.getServerRequestHandler";
NextServerSpan["createServer"] = "createServer.createServer";
return NextServerSpan;
}(NextServerSpan || {});
var NextNodeServerSpan = /*#__PURE__*/ function(NextNodeServerSpan) {
NextNodeServerSpan["compression"] = "NextNodeServer.compression";
NextNodeServerSpan["getBuildId"] = "NextNodeServer.getBuildId";
NextNodeServerSpan["createComponentTree"] = "NextNodeServer.createComponentTree";
NextNodeServerSpan["clientComponentLoading"] = "NextNodeServer.clientComponentLoading";
NextNodeServerSpan["getLayoutOrPageModule"] = "NextNodeServer.getLayoutOrPageModule";
NextNodeServerSpan["generateStaticRoutes"] = "NextNodeServer.generateStaticRoutes";
NextNodeServerSpan["generateFsStaticRoutes"] = "NextNodeServer.generateFsStaticRoutes";
NextNodeServerSpan["generatePublicRoutes"] = "NextNodeServer.generatePublicRoutes";
NextNodeServerSpan["generateImageRoutes"] = "NextNodeServer.generateImageRoutes.route";
NextNodeServerSpan["sendRenderResult"] = "NextNodeServer.sendRenderResult";
NextNodeServerSpan["proxyRequest"] = "NextNodeServer.proxyRequest";
NextNodeServerSpan["runApi"] = "NextNodeServer.runApi";
NextNodeServerSpan["render"] = "NextNodeServer.render";
NextNodeServerSpan["renderHTML"] = "NextNodeServer.renderHTML";
NextNodeServerSpan["imageOptimizer"] = "NextNodeServer.imageOptimizer";
NextNodeServerSpan["getPagePath"] = "NextNodeServer.getPagePath";
NextNodeServerSpan["getRoutesManifest"] = "NextNodeServer.getRoutesManifest";
NextNodeServerSpan["findPageComponents"] = "NextNodeServer.findPageComponents";
NextNodeServerSpan["getFontManifest"] = "NextNodeServer.getFontManifest";
NextNodeServerSpan["getServerComponentManifest"] = "NextNodeServer.getServerComponentManifest";
NextNodeServerSpan["getRequestHandler"] = "NextNodeServer.getRequestHandler";
NextNodeServerSpan["renderToHTML"] = "NextNodeServer.renderToHTML";
NextNodeServerSpan["renderError"] = "NextNodeServer.renderError";
NextNodeServerSpan["renderErrorToHTML"] = "NextNodeServer.renderErrorToHTML";
NextNodeServerSpan["render404"] = "NextNodeServer.render404";
NextNodeServerSpan["startResponse"] = "NextNodeServer.startResponse";
// nested inner span, does not require parent scope name
NextNodeServerSpan["route"] = "route";
NextNodeServerSpan["onProxyReq"] = "onProxyReq";
NextNodeServerSpan["apiResolver"] = "apiResolver";
NextNodeServerSpan["internalFetch"] = "internalFetch";
return NextNodeServerSpan;
}(NextNodeServerSpan || {});
var StartServerSpan = /*#__PURE__*/ function(StartServerSpan) {
StartServerSpan["startServer"] = "startServer.startServer";
return StartServerSpan;
}(StartServerSpan || {});
var RenderSpan = /*#__PURE__*/ function(RenderSpan) {
RenderSpan["getServerSideProps"] = "Render.getServerSideProps";
RenderSpan["getStaticProps"] = "Render.getStaticProps";
RenderSpan["renderToString"] = "Render.renderToString";
RenderSpan["renderDocument"] = "Render.renderDocument";
RenderSpan["createBodyResult"] = "Render.createBodyResult";
return RenderSpan;
}(RenderSpan || {});
var AppRenderSpan = /*#__PURE__*/ function(AppRenderSpan) {
AppRenderSpan["renderToString"] = "AppRender.renderToString";
AppRenderSpan["renderToReadableStream"] = "AppRender.renderToReadableStream";
AppRenderSpan["getBodyResult"] = "AppRender.getBodyResult";
AppRenderSpan["fetch"] = "AppRender.fetch";
return AppRenderSpan;
}(AppRenderSpan || {});
var RouterSpan = /*#__PURE__*/ function(RouterSpan) {
RouterSpan["executeRoute"] = "Router.executeRoute";
return RouterSpan;
}(RouterSpan || {});
var NodeSpan = /*#__PURE__*/ function(NodeSpan) {
NodeSpan["runHandler"] = "Node.runHandler";
return NodeSpan;
}(NodeSpan || {});
var AppRouteRouteHandlersSpan = /*#__PURE__*/ function(AppRouteRouteHandlersSpan) {
AppRouteRouteHandlersSpan["runHandler"] = "AppRouteRouteHandlers.runHandler";
return AppRouteRouteHandlersSpan;
}(AppRouteRouteHandlersSpan || {});
var ResolveMetadataSpan = /*#__PURE__*/ function(ResolveMetadataSpan) {
ResolveMetadataSpan["generateMetadata"] = "ResolveMetadata.generateMetadata";
ResolveMetadataSpan["generateViewport"] = "ResolveMetadata.generateViewport";
return ResolveMetadataSpan;
}(ResolveMetadataSpan || {});
var MiddlewareSpan = /*#__PURE__*/ function(MiddlewareSpan) {
MiddlewareSpan["execute"] = "Middleware.execute";
return MiddlewareSpan;
}(MiddlewareSpan || {});
// This list is used to filter out spans that are not relevant to the user
export const NextVanillaSpanAllowlist = [
"Middleware.execute",
"BaseServer.handleRequest",
"Render.getServerSideProps",
"Render.getStaticProps",
"AppRender.fetch",
"AppRender.getBodyResult",
"Render.renderDocument",
"Node.runHandler",
"AppRouteRouteHandlers.runHandler",
"ResolveMetadata.generateMetadata",
"ResolveMetadata.generateViewport",
"NextNodeServer.createComponentTree",
"NextNodeServer.findPageComponents",
"NextNodeServer.getLayoutOrPageModule",
"NextNodeServer.startResponse",
"NextNodeServer.clientComponentLoading"
];
// These Spans are allowed to be always logged
// when the otel log prefix env is set
export const LogSpanAllowList = [
"NextNodeServer.findPageComponents",
"NextNodeServer.createComponentTree",
"NextNodeServer.clientComponentLoading"
];
export { BaseServerSpan, LoadComponentsSpan, NextServerSpan, NextNodeServerSpan, StartServerSpan, RenderSpan, RouterSpan, AppRenderSpan, NodeSpan, AppRouteRouteHandlersSpan, ResolveMetadataSpan, MiddlewareSpan, };
//# sourceMappingURL=constants.js.map

File diff suppressed because one or more lines are too long

217
node_modules/next/dist/esm/server/lib/trace/tracer.js generated vendored Normal file
View File

@@ -0,0 +1,217 @@
import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants';
import { isThenable } from '../../../shared/lib/is-thenable';
let api;
// we want to allow users to use their own version of @opentelemetry/api if they
// want to, so we try to require it first, and if it fails we fall back to the
// version that is bundled with Next.js
// this is because @opentelemetry/api has to be synced with the version of
// @opentelemetry/tracing that is used, and we don't want to force users to use
// the version that is bundled with Next.js.
// the API is ~stable, so this should be fine
if (process.env.NEXT_RUNTIME === 'edge') {
api = require('@opentelemetry/api');
} else {
try {
api = require('@opentelemetry/api');
} catch (err) {
api = require('next/dist/compiled/@opentelemetry/api');
}
}
const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = api;
export class BubbledError extends Error {
constructor(bubble, result){
super(), this.bubble = bubble, this.result = result;
}
}
export function isBubbledError(error) {
if (typeof error !== 'object' || error === null) return false;
return error instanceof BubbledError;
}
const closeSpanWithError = (span, error)=>{
if (isBubbledError(error) && error.bubble) {
span.setAttribute('next.bubble', true);
} else {
if (error) {
span.recordException(error);
}
span.setStatus({
code: SpanStatusCode.ERROR,
message: error == null ? void 0 : error.message
});
}
span.end();
};
/** we use this map to propagate attributes from nested spans to the top span */ const rootSpanAttributesStore = new Map();
const rootSpanIdKey = api.createContextKey('next.rootSpanId');
let lastSpanId = 0;
const getSpanId = ()=>lastSpanId++;
const clientTraceDataSetter = {
set (carrier, key, value) {
carrier.push({
key,
value
});
}
};
class NextTracerImpl {
/**
* Returns an instance to the trace with configured name.
* Since wrap / trace can be defined in any place prior to actual trace subscriber initialization,
* This should be lazily evaluated.
*/ getTracerInstance() {
return trace.getTracer('next.js', '0.0.1');
}
getContext() {
return context;
}
getTracePropagationData() {
const activeContext = context.active();
const entries = [];
propagation.inject(activeContext, entries, clientTraceDataSetter);
return entries;
}
getActiveScopeSpan() {
return trace.getSpan(context == null ? void 0 : context.active());
}
withPropagatedContext(carrier, fn, getter) {
const activeContext = context.active();
if (trace.getSpanContext(activeContext)) {
// Active span is already set, too late to propagate.
return fn();
}
const remoteContext = propagation.extract(activeContext, carrier, getter);
return context.with(remoteContext, fn);
}
trace(...args) {
var _trace_getSpanContext;
const [type, fnOrOptions, fnOrEmpty] = args;
// coerce options form overload
const { fn, options } = typeof fnOrOptions === 'function' ? {
fn: fnOrOptions,
options: {}
} : {
fn: fnOrEmpty,
options: {
...fnOrOptions
}
};
const spanName = options.spanName ?? type;
if (!NextVanillaSpanAllowlist.includes(type) && process.env.NEXT_OTEL_VERBOSE !== '1' || options.hideSpan) {
return fn();
}
// Trying to get active scoped span to assign parent. If option specifies parent span manually, will try to use it.
let spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan());
let isRootSpan = false;
if (!spanContext) {
spanContext = (context == null ? void 0 : context.active()) ?? ROOT_CONTEXT;
isRootSpan = true;
} else if ((_trace_getSpanContext = trace.getSpanContext(spanContext)) == null ? void 0 : _trace_getSpanContext.isRemote) {
isRootSpan = true;
}
const spanId = getSpanId();
options.attributes = {
'next.span_name': spanName,
'next.span_type': type,
...options.attributes
};
return context.with(spanContext.setValue(rootSpanIdKey, spanId), ()=>this.getTracerInstance().startActiveSpan(spanName, options, (span)=>{
const startTime = 'performance' in globalThis && 'measure' in performance ? globalThis.performance.now() : undefined;
const onCleanup = ()=>{
rootSpanAttributesStore.delete(spanId);
if (startTime && process.env.NEXT_OTEL_PERFORMANCE_PREFIX && LogSpanAllowList.includes(type || '')) {
performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(type.split('.').pop() || '').replace(/[A-Z]/g, (match)=>'-' + match.toLowerCase())}`, {
start: startTime,
end: performance.now()
});
}
};
if (isRootSpan) {
rootSpanAttributesStore.set(spanId, new Map(Object.entries(options.attributes ?? {})));
}
try {
if (fn.length > 1) {
return fn(span, (err)=>closeSpanWithError(span, err));
}
const result = fn(span);
if (isThenable(result)) {
// If there's error make sure it throws
return result.then((res)=>{
span.end();
// Need to pass down the promise result,
// it could be react stream response with error { error, stream }
return res;
}).catch((err)=>{
closeSpanWithError(span, err);
throw err;
}).finally(onCleanup);
} else {
span.end();
onCleanup();
}
return result;
} catch (err) {
closeSpanWithError(span, err);
onCleanup();
throw err;
}
}));
}
wrap(...args) {
const tracer = this;
const [name, options, fn] = args.length === 3 ? args : [
args[0],
{},
args[1]
];
if (!NextVanillaSpanAllowlist.includes(name) && process.env.NEXT_OTEL_VERBOSE !== '1') {
return fn;
}
return function() {
let optionsObj = options;
if (typeof optionsObj === 'function' && typeof fn === 'function') {
optionsObj = optionsObj.apply(this, arguments);
}
const lastArgId = arguments.length - 1;
const cb = arguments[lastArgId];
if (typeof cb === 'function') {
const scopeBoundCb = tracer.getContext().bind(context.active(), cb);
return tracer.trace(name, optionsObj, (_span, done)=>{
arguments[lastArgId] = function(err) {
done == null ? void 0 : done(err);
return scopeBoundCb.apply(this, arguments);
};
return fn.apply(this, arguments);
});
} else {
return tracer.trace(name, optionsObj, ()=>fn.apply(this, arguments));
}
};
}
startSpan(...args) {
const [type, options] = args;
const spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan());
return this.getTracerInstance().startSpan(type, options, spanContext);
}
getSpanContext(parentSpan) {
const spanContext = parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined;
return spanContext;
}
getRootSpanAttributes() {
const spanId = context.active().getValue(rootSpanIdKey);
return rootSpanAttributesStore.get(spanId);
}
setRootSpanAttribute(key, value) {
const spanId = context.active().getValue(rootSpanIdKey);
const attributes = rootSpanAttributesStore.get(spanId);
if (attributes) {
attributes.set(key, value);
}
}
}
const getTracer = (()=>{
const tracer = new NextTracerImpl();
return ()=>tracer;
})();
export { getTracer, SpanStatusCode, SpanKind };
//# sourceMappingURL=tracer.js.map

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More