@vercel/next 4.7.0 → 4.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +142 -3
- package/dist/middleware-launcher.js +138 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -12806,6 +12806,127 @@ function normalizeEdgeFunctionPath(shortPath, appPathRoutesManifest) {
|
|
12806
12806
|
}
|
12807
12807
|
return shortPath;
|
12808
12808
|
}
|
12809
|
+
async function getNodeMiddleware({
|
12810
|
+
config,
|
12811
|
+
baseDir,
|
12812
|
+
projectDir,
|
12813
|
+
entryPath,
|
12814
|
+
nextVersion,
|
12815
|
+
nodeVersion,
|
12816
|
+
lstatSema,
|
12817
|
+
lstatResults,
|
12818
|
+
pageExtensions,
|
12819
|
+
routesManifest,
|
12820
|
+
outputDirectory,
|
12821
|
+
prerenderBypassToken,
|
12822
|
+
isCorrectMiddlewareOrder,
|
12823
|
+
functionsConfigManifest,
|
12824
|
+
requiredServerFilesManifest
|
12825
|
+
}) {
|
12826
|
+
const middlewareFunctionConfig = functionsConfigManifest?.functions["/_middleware"];
|
12827
|
+
if (!middlewareFunctionConfig || !middlewareFunctionConfig.matchers) {
|
12828
|
+
return null;
|
12829
|
+
}
|
12830
|
+
const routes = [];
|
12831
|
+
const routeMatchers = getRouteMatchers(
|
12832
|
+
{ matchers: middlewareFunctionConfig.matchers },
|
12833
|
+
routesManifest
|
12834
|
+
);
|
12835
|
+
for (const matcher of routeMatchers) {
|
12836
|
+
const route = {
|
12837
|
+
continue: true,
|
12838
|
+
src: matcher.regexp,
|
12839
|
+
has: matcher.has,
|
12840
|
+
missing: [
|
12841
|
+
{
|
12842
|
+
type: "header",
|
12843
|
+
key: "x-prerender-revalidate",
|
12844
|
+
value: prerenderBypassToken
|
12845
|
+
},
|
12846
|
+
...matcher.missing || []
|
12847
|
+
]
|
12848
|
+
};
|
12849
|
+
route.middlewarePath = "/_middleware";
|
12850
|
+
route.middlewareRawSrc = matcher.originalSource ? [matcher.originalSource] : [];
|
12851
|
+
if (isCorrectMiddlewareOrder) {
|
12852
|
+
route.override = true;
|
12853
|
+
}
|
12854
|
+
routes.push(route);
|
12855
|
+
}
|
12856
|
+
const sourceFile = await getSourceFilePathFromPage({
|
12857
|
+
workPath: entryPath,
|
12858
|
+
page: normalizeSourceFilePageFromManifest("/middleware", "middleware", {}),
|
12859
|
+
pageExtensions
|
12860
|
+
});
|
12861
|
+
const vercelConfigOpts = await (0, import_build_utils.getLambdaOptionsFromFunction)({
|
12862
|
+
sourceFile,
|
12863
|
+
config
|
12864
|
+
});
|
12865
|
+
const middlewareFile = import_path2.default.join(
|
12866
|
+
entryPath,
|
12867
|
+
outputDirectory,
|
12868
|
+
"server",
|
12869
|
+
"middleware.js"
|
12870
|
+
);
|
12871
|
+
const middlewareTrace = `${middlewareFile}.nft.json`;
|
12872
|
+
const middlewareTraceDir = import_path2.default.dirname(middlewareTrace);
|
12873
|
+
const { files } = JSON.parse(await import_fs_extra3.default.readFile(middlewareTrace, "utf8"));
|
12874
|
+
const fileList = [];
|
12875
|
+
const normalizedBaseDir = `${baseDir}${baseDir.endsWith(import_path2.default.sep) ? "" : import_path2.default.sep}`;
|
12876
|
+
files.forEach((file) => {
|
12877
|
+
const absolutePath = import_path2.default.join(middlewareTraceDir, file);
|
12878
|
+
if (absolutePath.startsWith(normalizedBaseDir)) {
|
12879
|
+
fileList.push(import_path2.default.relative(baseDir, absolutePath));
|
12880
|
+
} else {
|
12881
|
+
console.log("outside base dir", absolutePath);
|
12882
|
+
}
|
12883
|
+
});
|
12884
|
+
const reasons = /* @__PURE__ */ new Map();
|
12885
|
+
const tracedFiles = {};
|
12886
|
+
await Promise.all(
|
12887
|
+
fileList.map(
|
12888
|
+
collectTracedFiles(baseDir, lstatResults, lstatSema, reasons, tracedFiles)
|
12889
|
+
)
|
12890
|
+
);
|
12891
|
+
const launcherData = (await import_fs_extra3.default.readFile(import_path2.default.join(__dirname, "middleware-launcher.js"), "utf8")).replace(
|
12892
|
+
/(?:var|const) conf = __NEXT_CONFIG__/,
|
12893
|
+
`const conf = ${JSON.stringify({
|
12894
|
+
...requiredServerFilesManifest.config,
|
12895
|
+
distDir: import_path2.default.relative(
|
12896
|
+
projectDir,
|
12897
|
+
import_path2.default.join(entryPath, outputDirectory)
|
12898
|
+
)
|
12899
|
+
})}`
|
12900
|
+
).replace("__NEXT_MIDDLEWARE_PATH__", `./.next/server/middleware.js`);
|
12901
|
+
const lambda = new import_build_utils.NodejsLambda({
|
12902
|
+
...vercelConfigOpts,
|
12903
|
+
runtime: nodeVersion,
|
12904
|
+
handler: import_path2.default.join(
|
12905
|
+
import_path2.default.relative(baseDir, projectDir),
|
12906
|
+
"___next_launcher.cjs"
|
12907
|
+
),
|
12908
|
+
useWebApi: true,
|
12909
|
+
shouldAddHelpers: false,
|
12910
|
+
shouldAddSourcemapSupport: false,
|
12911
|
+
framework: {
|
12912
|
+
slug: "nextjs",
|
12913
|
+
version: nextVersion
|
12914
|
+
},
|
12915
|
+
files: {
|
12916
|
+
...tracedFiles,
|
12917
|
+
[import_path2.default.relative(baseDir, middlewareFile)]: new import_build_utils.FileFsRef({
|
12918
|
+
fsPath: middlewareFile
|
12919
|
+
}),
|
12920
|
+
[import_path2.default.join(import_path2.default.relative(baseDir, projectDir), "___next_launcher.cjs")]: new import_build_utils.FileBlob({ data: launcherData })
|
12921
|
+
}
|
12922
|
+
});
|
12923
|
+
return {
|
12924
|
+
routes,
|
12925
|
+
lambdas: {
|
12926
|
+
_middleware: lambda
|
12927
|
+
}
|
12928
|
+
};
|
12929
|
+
}
|
12809
12930
|
async function getMiddlewareBundle({
|
12810
12931
|
entryPath,
|
12811
12932
|
outputDirectory,
|
@@ -14732,6 +14853,23 @@ ${JSON.stringify(
|
|
14732
14853
|
true
|
14733
14854
|
)];
|
14734
14855
|
});
|
14856
|
+
const nodeMiddleware = await getNodeMiddleware({
|
14857
|
+
config,
|
14858
|
+
baseDir,
|
14859
|
+
projectDir,
|
14860
|
+
entryPath,
|
14861
|
+
nextVersion,
|
14862
|
+
nodeVersion: nodeVersion.runtime,
|
14863
|
+
lstatSema,
|
14864
|
+
lstatResults,
|
14865
|
+
pageExtensions: requiredServerFilesManifest.config.pageExtensions,
|
14866
|
+
routesManifest,
|
14867
|
+
outputDirectory,
|
14868
|
+
prerenderBypassToken: prerenderManifest.bypassToken,
|
14869
|
+
isCorrectMiddlewareOrder,
|
14870
|
+
functionsConfigManifest,
|
14871
|
+
requiredServerFilesManifest
|
14872
|
+
});
|
14735
14873
|
const middleware = await getMiddlewareBundle({
|
14736
14874
|
config,
|
14737
14875
|
entryPath,
|
@@ -14742,7 +14880,7 @@ ${JSON.stringify(
|
|
14742
14880
|
nextVersion,
|
14743
14881
|
appPathRoutesManifest: appPathRoutesManifest || {}
|
14744
14882
|
});
|
14745
|
-
const isNextDataServerResolving = middleware.staticRoutes.length > 0 && import_semver3.default.gte(nextVersion, NEXT_DATA_MIDDLEWARE_RESOLVING_VERSION);
|
14883
|
+
const isNextDataServerResolving = (middleware.staticRoutes.length > 0 || nodeMiddleware) && import_semver3.default.gte(nextVersion, NEXT_DATA_MIDDLEWARE_RESOLVING_VERSION);
|
14746
14884
|
const dynamicRoutes = await getDynamicRoutes({
|
14747
14885
|
entryPath,
|
14748
14886
|
entryDirectory,
|
@@ -14961,6 +15099,7 @@ ${JSON.stringify(
|
|
14961
15099
|
...staticDirectoryFiles,
|
14962
15100
|
...privateOutputs.files,
|
14963
15101
|
...middleware.edgeFunctions,
|
15102
|
+
...nodeMiddleware?.lambdas,
|
14964
15103
|
...isNextDataServerResolving ? {
|
14965
15104
|
__next_data_catchall: nextDataCatchallOutput
|
14966
15105
|
} : {}
|
@@ -15093,7 +15232,7 @@ ${JSON.stringify(
|
|
15093
15232
|
// middleware comes directly after redirects but before
|
15094
15233
|
// beforeFiles rewrites as middleware is not a "file" route
|
15095
15234
|
...routesManifest?.skipMiddlewareUrlNormalize ? denormalizeNextDataRoute(true) : [],
|
15096
|
-
...isCorrectMiddlewareOrder ? middleware.staticRoutes : [],
|
15235
|
+
...isCorrectMiddlewareOrder ? [...middleware.staticRoutes, ...nodeMiddleware?.routes || []] : [],
|
15097
15236
|
...routesManifest?.skipMiddlewareUrlNormalize ? normalizeNextDataRoute(true) : [],
|
15098
15237
|
...beforeFilesRewrites,
|
15099
15238
|
// Make sure to 404 for the /404 path itself
|
@@ -15149,7 +15288,7 @@ ${JSON.stringify(
|
|
15149
15288
|
// while middleware was in beta the order came right before
|
15150
15289
|
// handle: 'filesystem' we maintain this for older versions
|
15151
15290
|
// to prevent a local/deploy mismatch
|
15152
|
-
...!isCorrectMiddlewareOrder ? middleware.staticRoutes : [],
|
15291
|
+
...!isCorrectMiddlewareOrder ? [...middleware.staticRoutes, ...nodeMiddleware?.routes || []] : [],
|
15153
15292
|
...appDir ? [
|
15154
15293
|
...isAppClientSegmentCacheEnabled && rscPrefetchHeader && prefetchSegmentHeader && prefetchSegmentDirSuffix && prefetchSegmentSuffix ? [
|
15155
15294
|
{
|
@@ -0,0 +1,138 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
// src/vercel-request-context.ts
|
4
|
+
var SYMBOL_FOR_REQ_CONTEXT = Symbol.for("@vercel/request-context");
|
5
|
+
function getContext() {
|
6
|
+
const fromSymbol = globalThis;
|
7
|
+
return fromSymbol[SYMBOL_FOR_REQ_CONTEXT]?.get?.() ?? {};
|
8
|
+
}
|
9
|
+
|
10
|
+
// src/next-request-context.ts
|
11
|
+
var import_async_hooks = require("async_hooks");
|
12
|
+
var name = "@next/request-context";
|
13
|
+
var NEXT_REQUEST_CONTEXT_SYMBOL = Symbol.for(name);
|
14
|
+
var INTERNAL_STORAGE_FIELD_SYMBOL = Symbol.for("internal.storage");
|
15
|
+
function getOrCreateContextSingleton() {
|
16
|
+
const _globalThis = globalThis;
|
17
|
+
if (!_globalThis[NEXT_REQUEST_CONTEXT_SYMBOL]) {
|
18
|
+
const storage = new import_async_hooks.AsyncLocalStorage();
|
19
|
+
const Context = {
|
20
|
+
get: () => storage.getStore(),
|
21
|
+
[INTERNAL_STORAGE_FIELD_SYMBOL]: storage
|
22
|
+
};
|
23
|
+
_globalThis[NEXT_REQUEST_CONTEXT_SYMBOL] = Context;
|
24
|
+
}
|
25
|
+
return _globalThis[NEXT_REQUEST_CONTEXT_SYMBOL];
|
26
|
+
}
|
27
|
+
var NextRequestContext = getOrCreateContextSingleton();
|
28
|
+
function withNextRequestContext(value, callback) {
|
29
|
+
const storage = NextRequestContext[INTERNAL_STORAGE_FIELD_SYMBOL];
|
30
|
+
return storage.run(value, callback);
|
31
|
+
}
|
32
|
+
|
33
|
+
// src/edge-function-source/to-plain-headers.ts
|
34
|
+
function toPlainHeaders(headers) {
|
35
|
+
const result = {};
|
36
|
+
if (!headers)
|
37
|
+
return result;
|
38
|
+
headers.forEach((value, key) => {
|
39
|
+
result[key] = value;
|
40
|
+
if (key.toLowerCase() === "set-cookie") {
|
41
|
+
result[key] = splitCookiesString(value);
|
42
|
+
}
|
43
|
+
});
|
44
|
+
return result;
|
45
|
+
}
|
46
|
+
function splitCookiesString(cookiesString) {
|
47
|
+
const cookiesStrings = [];
|
48
|
+
let pos = 0;
|
49
|
+
let start;
|
50
|
+
let ch;
|
51
|
+
let lastComma;
|
52
|
+
let nextStart;
|
53
|
+
let cookiesSeparatorFound;
|
54
|
+
function skipWhitespace() {
|
55
|
+
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos)))
|
56
|
+
pos += 1;
|
57
|
+
return pos < cookiesString.length;
|
58
|
+
}
|
59
|
+
function notSpecialChar() {
|
60
|
+
ch = cookiesString.charAt(pos);
|
61
|
+
return ch !== "=" && ch !== ";" && ch !== ",";
|
62
|
+
}
|
63
|
+
while (pos < cookiesString.length) {
|
64
|
+
start = pos;
|
65
|
+
cookiesSeparatorFound = false;
|
66
|
+
while (skipWhitespace()) {
|
67
|
+
ch = cookiesString.charAt(pos);
|
68
|
+
if (ch === ",") {
|
69
|
+
lastComma = pos;
|
70
|
+
pos += 1;
|
71
|
+
skipWhitespace();
|
72
|
+
nextStart = pos;
|
73
|
+
while (pos < cookiesString.length && notSpecialChar()) {
|
74
|
+
pos += 1;
|
75
|
+
}
|
76
|
+
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
|
77
|
+
cookiesSeparatorFound = true;
|
78
|
+
pos = nextStart;
|
79
|
+
cookiesStrings.push(cookiesString.substring(start, lastComma));
|
80
|
+
start = pos;
|
81
|
+
} else {
|
82
|
+
pos = lastComma + 1;
|
83
|
+
}
|
84
|
+
} else {
|
85
|
+
pos += 1;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
89
|
+
cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
|
90
|
+
}
|
91
|
+
}
|
92
|
+
return cookiesStrings;
|
93
|
+
}
|
94
|
+
|
95
|
+
// src/middleware-launcher.ts
|
96
|
+
process.chdir(__dirname);
|
97
|
+
var region = process.env.VERCEL_REGION || process.env.NOW_REGION;
|
98
|
+
if (!process.env.NODE_ENV) {
|
99
|
+
process.env.NODE_ENV = region === "dev1" ? "development" : "production";
|
100
|
+
}
|
101
|
+
if (process.env.NODE_ENV !== "production" && region !== "dev1") {
|
102
|
+
console.warn(
|
103
|
+
`Warning: NODE_ENV was incorrectly set to "${process.env.NODE_ENV}", this value is being overridden to "production"`
|
104
|
+
);
|
105
|
+
process.env.NODE_ENV = "production";
|
106
|
+
}
|
107
|
+
var conf = __NEXT_CONFIG__;
|
108
|
+
globalThis.AsyncLocalStorage = require("async_hooks").AsyncLocalStorage;
|
109
|
+
var middlewareHandler = require("__NEXT_MIDDLEWARE_PATH__").default;
|
110
|
+
var serve = async (request) => {
|
111
|
+
try {
|
112
|
+
const context = getContext();
|
113
|
+
return await withNextRequestContext(
|
114
|
+
{ waitUntil: context.waitUntil },
|
115
|
+
async () => {
|
116
|
+
const result = await middlewareHandler({
|
117
|
+
request: {
|
118
|
+
url: request.url,
|
119
|
+
method: request.method,
|
120
|
+
headers: toPlainHeaders(request.headers),
|
121
|
+
nextConfig: conf,
|
122
|
+
page: "/middleware",
|
123
|
+
body: request.method !== "GET" && request.method !== "HEAD" ? request.body : void 0,
|
124
|
+
waitUntil: context.waitUntil
|
125
|
+
}
|
126
|
+
});
|
127
|
+
if (result.waitUntil && context.waitUntil) {
|
128
|
+
context.waitUntil(result.waitUntil);
|
129
|
+
}
|
130
|
+
return result.response;
|
131
|
+
}
|
132
|
+
);
|
133
|
+
} catch (err) {
|
134
|
+
console.error(err);
|
135
|
+
process.exit(1);
|
136
|
+
}
|
137
|
+
};
|
138
|
+
module.exports = serve;
|