vinext 0.0.0 → 0.0.2
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/build/static-export.d.ts +78 -0
- package/dist/build/static-export.d.ts.map +1 -0
- package/dist/build/static-export.js +553 -0
- package/dist/build/static-export.js.map +1 -0
- package/dist/check.d.ts +52 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +483 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +565 -0
- package/dist/cli.js.map +1 -0
- package/dist/client/entry.d.ts +2 -0
- package/dist/client/entry.d.ts.map +1 -0
- package/dist/client/entry.js +85 -0
- package/dist/client/entry.js.map +1 -0
- package/dist/cloudflare/index.d.ts +8 -0
- package/dist/cloudflare/index.d.ts.map +1 -0
- package/dist/cloudflare/index.js +8 -0
- package/dist/cloudflare/index.js.map +1 -0
- package/dist/cloudflare/kv-cache-handler.d.ts +68 -0
- package/dist/cloudflare/kv-cache-handler.d.ts.map +1 -0
- package/dist/cloudflare/kv-cache-handler.js +304 -0
- package/dist/cloudflare/kv-cache-handler.js.map +1 -0
- package/dist/cloudflare/tpr.d.ts +78 -0
- package/dist/cloudflare/tpr.d.ts.map +1 -0
- package/dist/cloudflare/tpr.js +672 -0
- package/dist/cloudflare/tpr.js.map +1 -0
- package/dist/config/config-matchers.d.ts +106 -0
- package/dist/config/config-matchers.d.ts.map +1 -0
- package/dist/config/config-matchers.js +499 -0
- package/dist/config/config-matchers.js.map +1 -0
- package/dist/config/next-config.d.ts +153 -0
- package/dist/config/next-config.d.ts.map +1 -0
- package/dist/config/next-config.js +274 -0
- package/dist/config/next-config.js.map +1 -0
- package/dist/deploy.d.ts +87 -0
- package/dist/deploy.d.ts.map +1 -0
- package/dist/deploy.js +644 -0
- package/dist/deploy.js.map +1 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3296 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +55 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +201 -0
- package/dist/init.js.map +1 -0
- package/dist/routing/app-router.d.ts +96 -0
- package/dist/routing/app-router.d.ts.map +1 -0
- package/dist/routing/app-router.js +815 -0
- package/dist/routing/app-router.js.map +1 -0
- package/dist/routing/pages-router.d.ts +52 -0
- package/dist/routing/pages-router.d.ts.map +1 -0
- package/dist/routing/pages-router.js +239 -0
- package/dist/routing/pages-router.js.map +1 -0
- package/dist/server/api-handler.d.ts +18 -0
- package/dist/server/api-handler.d.ts.map +1 -0
- package/dist/server/api-handler.js +169 -0
- package/dist/server/api-handler.js.map +1 -0
- package/dist/server/app-dev-server.d.ts +42 -0
- package/dist/server/app-dev-server.d.ts.map +1 -0
- package/dist/server/app-dev-server.js +2718 -0
- package/dist/server/app-dev-server.js.map +1 -0
- package/dist/server/app-router-entry.d.ts +18 -0
- package/dist/server/app-router-entry.d.ts.map +1 -0
- package/dist/server/app-router-entry.js +34 -0
- package/dist/server/app-router-entry.js.map +1 -0
- package/dist/server/dev-server.d.ts +40 -0
- package/dist/server/dev-server.d.ts.map +1 -0
- package/dist/server/dev-server.js +758 -0
- package/dist/server/dev-server.js.map +1 -0
- package/dist/server/html.d.ts +22 -0
- package/dist/server/html.d.ts.map +1 -0
- package/dist/server/html.js +29 -0
- package/dist/server/html.js.map +1 -0
- package/dist/server/image-optimization.d.ts +56 -0
- package/dist/server/image-optimization.d.ts.map +1 -0
- package/dist/server/image-optimization.js +103 -0
- package/dist/server/image-optimization.js.map +1 -0
- package/dist/server/instrumentation.d.ts +68 -0
- package/dist/server/instrumentation.d.ts.map +1 -0
- package/dist/server/instrumentation.js +90 -0
- package/dist/server/instrumentation.js.map +1 -0
- package/dist/server/isr-cache.d.ts +61 -0
- package/dist/server/isr-cache.d.ts.map +1 -0
- package/dist/server/isr-cache.js +134 -0
- package/dist/server/isr-cache.js.map +1 -0
- package/dist/server/metadata-routes.d.ts +103 -0
- package/dist/server/metadata-routes.d.ts.map +1 -0
- package/dist/server/metadata-routes.js +270 -0
- package/dist/server/metadata-routes.js.map +1 -0
- package/dist/server/middleware.d.ts +77 -0
- package/dist/server/middleware.d.ts.map +1 -0
- package/dist/server/middleware.js +228 -0
- package/dist/server/middleware.js.map +1 -0
- package/dist/server/prod-server.d.ts +78 -0
- package/dist/server/prod-server.d.ts.map +1 -0
- package/dist/server/prod-server.js +712 -0
- package/dist/server/prod-server.js.map +1 -0
- package/dist/shims/amp.d.ts +17 -0
- package/dist/shims/amp.d.ts.map +1 -0
- package/dist/shims/amp.js +21 -0
- package/dist/shims/amp.js.map +1 -0
- package/dist/shims/app.d.ts +12 -0
- package/dist/shims/app.d.ts.map +1 -0
- package/dist/shims/app.js +2 -0
- package/dist/shims/app.js.map +1 -0
- package/dist/shims/cache-runtime.d.ts +68 -0
- package/dist/shims/cache-runtime.d.ts.map +1 -0
- package/dist/shims/cache-runtime.js +437 -0
- package/dist/shims/cache-runtime.js.map +1 -0
- package/dist/shims/cache.d.ts +243 -0
- package/dist/shims/cache.d.ts.map +1 -0
- package/dist/shims/cache.js +415 -0
- package/dist/shims/cache.js.map +1 -0
- package/dist/shims/client-only.d.ts +18 -0
- package/dist/shims/client-only.d.ts.map +1 -0
- package/dist/shims/client-only.js +18 -0
- package/dist/shims/client-only.js.map +1 -0
- package/dist/shims/config.d.ts +27 -0
- package/dist/shims/config.d.ts.map +1 -0
- package/dist/shims/config.js +30 -0
- package/dist/shims/config.js.map +1 -0
- package/dist/shims/constants.d.ts +13 -0
- package/dist/shims/constants.d.ts.map +1 -0
- package/dist/shims/constants.js +13 -0
- package/dist/shims/constants.js.map +1 -0
- package/dist/shims/document.d.ts +33 -0
- package/dist/shims/document.d.ts.map +1 -0
- package/dist/shims/document.js +32 -0
- package/dist/shims/document.js.map +1 -0
- package/dist/shims/dynamic.d.ts +33 -0
- package/dist/shims/dynamic.d.ts.map +1 -0
- package/dist/shims/dynamic.js +149 -0
- package/dist/shims/dynamic.js.map +1 -0
- package/dist/shims/error-boundary.d.ts +33 -0
- package/dist/shims/error-boundary.d.ts.map +1 -0
- package/dist/shims/error-boundary.js +88 -0
- package/dist/shims/error-boundary.js.map +1 -0
- package/dist/shims/error.d.ts +16 -0
- package/dist/shims/error.d.ts.map +1 -0
- package/dist/shims/error.js +45 -0
- package/dist/shims/error.js.map +1 -0
- package/dist/shims/fetch-cache.d.ts +61 -0
- package/dist/shims/fetch-cache.d.ts.map +1 -0
- package/dist/shims/fetch-cache.js +307 -0
- package/dist/shims/fetch-cache.js.map +1 -0
- package/dist/shims/font-google.d.ts +122 -0
- package/dist/shims/font-google.d.ts.map +1 -0
- package/dist/shims/font-google.js +387 -0
- package/dist/shims/font-google.js.map +1 -0
- package/dist/shims/font-local.d.ts +61 -0
- package/dist/shims/font-local.d.ts.map +1 -0
- package/dist/shims/font-local.js +303 -0
- package/dist/shims/font-local.js.map +1 -0
- package/dist/shims/form.d.ts +30 -0
- package/dist/shims/form.d.ts.map +1 -0
- package/dist/shims/form.js +78 -0
- package/dist/shims/form.js.map +1 -0
- package/dist/shims/head-state.d.ts +11 -0
- package/dist/shims/head-state.d.ts.map +1 -0
- package/dist/shims/head-state.js +47 -0
- package/dist/shims/head-state.js.map +1 -0
- package/dist/shims/head.d.ts +28 -0
- package/dist/shims/head.d.ts.map +1 -0
- package/dist/shims/head.js +148 -0
- package/dist/shims/head.js.map +1 -0
- package/dist/shims/headers.d.ts +150 -0
- package/dist/shims/headers.d.ts.map +1 -0
- package/dist/shims/headers.js +412 -0
- package/dist/shims/headers.js.map +1 -0
- package/dist/shims/image-config.d.ts +30 -0
- package/dist/shims/image-config.d.ts.map +1 -0
- package/dist/shims/image-config.js +91 -0
- package/dist/shims/image-config.js.map +1 -0
- package/dist/shims/image.d.ts +63 -0
- package/dist/shims/image.d.ts.map +1 -0
- package/dist/shims/image.js +284 -0
- package/dist/shims/image.js.map +1 -0
- package/dist/shims/internal/api-utils.d.ts +12 -0
- package/dist/shims/internal/api-utils.d.ts.map +1 -0
- package/dist/shims/internal/api-utils.js +7 -0
- package/dist/shims/internal/api-utils.js.map +1 -0
- package/dist/shims/internal/app-router-context.d.ts +21 -0
- package/dist/shims/internal/app-router-context.d.ts.map +1 -0
- package/dist/shims/internal/app-router-context.js +15 -0
- package/dist/shims/internal/app-router-context.js.map +1 -0
- package/dist/shims/internal/cookies.d.ts +9 -0
- package/dist/shims/internal/cookies.d.ts.map +1 -0
- package/dist/shims/internal/cookies.js +9 -0
- package/dist/shims/internal/cookies.js.map +1 -0
- package/dist/shims/internal/router-context.d.ts +2 -0
- package/dist/shims/internal/router-context.d.ts.map +1 -0
- package/dist/shims/internal/router-context.js +9 -0
- package/dist/shims/internal/router-context.js.map +1 -0
- package/dist/shims/internal/utils.d.ts +48 -0
- package/dist/shims/internal/utils.d.ts.map +1 -0
- package/dist/shims/internal/utils.js +35 -0
- package/dist/shims/internal/utils.js.map +1 -0
- package/dist/shims/internal/work-unit-async-storage.d.ts +12 -0
- package/dist/shims/internal/work-unit-async-storage.d.ts.map +1 -0
- package/dist/shims/internal/work-unit-async-storage.js +13 -0
- package/dist/shims/internal/work-unit-async-storage.js.map +1 -0
- package/dist/shims/layout-segment-context.d.ts +21 -0
- package/dist/shims/layout-segment-context.d.ts.map +1 -0
- package/dist/shims/layout-segment-context.js +27 -0
- package/dist/shims/layout-segment-context.js.map +1 -0
- package/dist/shims/legacy-image.d.ts +52 -0
- package/dist/shims/legacy-image.d.ts.map +1 -0
- package/dist/shims/legacy-image.js +46 -0
- package/dist/shims/legacy-image.js.map +1 -0
- package/dist/shims/link.d.ts +48 -0
- package/dist/shims/link.d.ts.map +1 -0
- package/dist/shims/link.js +395 -0
- package/dist/shims/link.js.map +1 -0
- package/dist/shims/metadata.d.ts +184 -0
- package/dist/shims/metadata.d.ts.map +1 -0
- package/dist/shims/metadata.js +472 -0
- package/dist/shims/metadata.js.map +1 -0
- package/dist/shims/navigation-state.d.ts +14 -0
- package/dist/shims/navigation-state.d.ts.map +1 -0
- package/dist/shims/navigation-state.js +77 -0
- package/dist/shims/navigation-state.js.map +1 -0
- package/dist/shims/navigation.d.ts +201 -0
- package/dist/shims/navigation.d.ts.map +1 -0
- package/dist/shims/navigation.js +672 -0
- package/dist/shims/navigation.js.map +1 -0
- package/dist/shims/og.d.ts +20 -0
- package/dist/shims/og.d.ts.map +1 -0
- package/dist/shims/og.js +19 -0
- package/dist/shims/og.js.map +1 -0
- package/dist/shims/router-state.d.ts +11 -0
- package/dist/shims/router-state.d.ts.map +1 -0
- package/dist/shims/router-state.js +56 -0
- package/dist/shims/router-state.js.map +1 -0
- package/dist/shims/router.d.ts +103 -0
- package/dist/shims/router.d.ts.map +1 -0
- package/dist/shims/router.js +536 -0
- package/dist/shims/router.js.map +1 -0
- package/dist/shims/script.d.ts +58 -0
- package/dist/shims/script.d.ts.map +1 -0
- package/dist/shims/script.js +163 -0
- package/dist/shims/script.js.map +1 -0
- package/dist/shims/server-only.d.ts +19 -0
- package/dist/shims/server-only.d.ts.map +1 -0
- package/dist/shims/server-only.js +19 -0
- package/dist/shims/server-only.js.map +1 -0
- package/dist/shims/server.d.ts +178 -0
- package/dist/shims/server.d.ts.map +1 -0
- package/dist/shims/server.js +377 -0
- package/dist/shims/server.js.map +1 -0
- package/dist/shims/web-vitals.d.ts +24 -0
- package/dist/shims/web-vitals.d.ts.map +1 -0
- package/dist/shims/web-vitals.js +17 -0
- package/dist/shims/web-vitals.js.map +1 -0
- package/dist/utils/hash.d.ts +6 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +20 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/project.d.ts +36 -0
- package/dist/utils/project.d.ts.map +1 -0
- package/dist/utils/project.js +112 -0
- package/dist/utils/project.js.map +1 -0
- package/dist/utils/query.d.ts +10 -0
- package/dist/utils/query.d.ts.map +1 -0
- package/dist/utils/query.js +27 -0
- package/dist/utils/query.js.map +1 -0
- package/package.json +65 -7
- package/index.js +0 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* proxy.ts / middleware.ts runner
|
|
3
|
+
*
|
|
4
|
+
* Loads and executes the user's proxy.ts (Next.js 16) or middleware.ts file
|
|
5
|
+
* before routing. Runs in Node (not Edge Runtime), per the vinext design.
|
|
6
|
+
*
|
|
7
|
+
* In Next.js 16, proxy.ts replaces middleware.ts:
|
|
8
|
+
* - proxy.ts: default export function, runs on Node.js runtime
|
|
9
|
+
* - middleware.ts: deprecated but still supported for Edge runtime use cases
|
|
10
|
+
*
|
|
11
|
+
* The proxy/middleware receives a NextRequest and can:
|
|
12
|
+
* - Return NextResponse.next() to continue to the route
|
|
13
|
+
* - Return NextResponse.redirect() to redirect
|
|
14
|
+
* - Return NextResponse.rewrite() to rewrite the URL
|
|
15
|
+
* - Set/modify headers and cookies
|
|
16
|
+
* - Return a Response directly (e.g., for auth guards)
|
|
17
|
+
*
|
|
18
|
+
* Supports the `config.matcher` export for path filtering.
|
|
19
|
+
*/
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import { NextRequest } from "../shims/server.js";
|
|
23
|
+
import { safeRegExp } from "../config/config-matchers.js";
|
|
24
|
+
/**
|
|
25
|
+
* Possible proxy/middleware file names.
|
|
26
|
+
* proxy.ts (Next.js 16) is checked first, then middleware.ts (deprecated).
|
|
27
|
+
*/
|
|
28
|
+
const PROXY_FILES = [
|
|
29
|
+
"proxy.ts",
|
|
30
|
+
"proxy.js",
|
|
31
|
+
"proxy.mjs",
|
|
32
|
+
"src/proxy.ts",
|
|
33
|
+
"src/proxy.js",
|
|
34
|
+
"src/proxy.mjs",
|
|
35
|
+
];
|
|
36
|
+
const MIDDLEWARE_FILES = [
|
|
37
|
+
"middleware.ts",
|
|
38
|
+
"middleware.tsx",
|
|
39
|
+
"middleware.js",
|
|
40
|
+
"middleware.mjs",
|
|
41
|
+
"src/middleware.ts",
|
|
42
|
+
"src/middleware.tsx",
|
|
43
|
+
"src/middleware.js",
|
|
44
|
+
"src/middleware.mjs",
|
|
45
|
+
];
|
|
46
|
+
/**
|
|
47
|
+
* Find the proxy or middleware file in the project root.
|
|
48
|
+
* Checks for proxy.ts (Next.js 16) first, then falls back to middleware.ts.
|
|
49
|
+
* If middleware.ts is found, logs a deprecation warning.
|
|
50
|
+
*/
|
|
51
|
+
export function findMiddlewareFile(root) {
|
|
52
|
+
// Check proxy.ts first (Next.js 16 replacement for middleware.ts)
|
|
53
|
+
for (const file of PROXY_FILES) {
|
|
54
|
+
const fullPath = path.join(root, file);
|
|
55
|
+
if (fs.existsSync(fullPath)) {
|
|
56
|
+
return fullPath;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Fall back to middleware.ts (deprecated in Next.js 16)
|
|
60
|
+
for (const file of MIDDLEWARE_FILES) {
|
|
61
|
+
const fullPath = path.join(root, file);
|
|
62
|
+
if (fs.existsSync(fullPath)) {
|
|
63
|
+
console.warn("[vinext] middleware.ts is deprecated in Next.js 16. " +
|
|
64
|
+
"Rename to proxy.ts and export a default function.");
|
|
65
|
+
return fullPath;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Check if a pathname matches the middleware matcher config.
|
|
72
|
+
* If no matcher is configured, middleware runs on all paths
|
|
73
|
+
* except static files and internal Next.js paths.
|
|
74
|
+
*/
|
|
75
|
+
export function matchesMiddleware(pathname, matcher) {
|
|
76
|
+
if (!matcher) {
|
|
77
|
+
// Default: match all paths except static files, _next, and favicon
|
|
78
|
+
return (!pathname.startsWith("/_next") &&
|
|
79
|
+
!pathname.startsWith("/api") &&
|
|
80
|
+
!pathname.includes(".") &&
|
|
81
|
+
pathname !== "/favicon.ico");
|
|
82
|
+
}
|
|
83
|
+
const patterns = [];
|
|
84
|
+
if (typeof matcher === "string") {
|
|
85
|
+
patterns.push(matcher);
|
|
86
|
+
}
|
|
87
|
+
else if (Array.isArray(matcher)) {
|
|
88
|
+
for (const m of matcher) {
|
|
89
|
+
if (typeof m === "string") {
|
|
90
|
+
patterns.push(m);
|
|
91
|
+
}
|
|
92
|
+
else if (m && typeof m === "object" && "source" in m) {
|
|
93
|
+
patterns.push(m.source);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return patterns.some((pattern) => matchPattern(pathname, pattern));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Match a single pattern against a pathname.
|
|
101
|
+
* Supports Next.js matcher patterns:
|
|
102
|
+
* /about -> exact match
|
|
103
|
+
* /dashboard/:path* -> prefix match with params
|
|
104
|
+
* /api/:path+ -> one or more segments
|
|
105
|
+
* /((?!api|_next).*) -> regex patterns
|
|
106
|
+
*/
|
|
107
|
+
export function matchPattern(pathname, pattern) {
|
|
108
|
+
// Handle regex patterns (starts with /)
|
|
109
|
+
if (pattern.includes("(") || pattern.includes("\\")) {
|
|
110
|
+
const re = safeRegExp("^" + pattern + "$");
|
|
111
|
+
if (re)
|
|
112
|
+
return re.test(pathname);
|
|
113
|
+
// Fall through to simple matching
|
|
114
|
+
}
|
|
115
|
+
// Convert Next.js path patterns to regex.
|
|
116
|
+
// Escape dots FIRST (before replacements that produce regex metacharacters
|
|
117
|
+
// like .* and .+ which must not be escaped).
|
|
118
|
+
const regexStr = pattern
|
|
119
|
+
// Escape dots in the literal path segments
|
|
120
|
+
.replace(/\./g, "\\.")
|
|
121
|
+
// /:path* -> optionally match slash + zero or more segments
|
|
122
|
+
.replace(/\/:(\w+)\*/g, "(?:/.*)?")
|
|
123
|
+
// /:path+ -> match slash + one or more segments
|
|
124
|
+
.replace(/\/:(\w+)\+/g, "(?:/.+)")
|
|
125
|
+
// :param -> match one segment
|
|
126
|
+
.replace(/:(\w+)/g, "([^/]+)");
|
|
127
|
+
const re = safeRegExp("^" + regexStr + "$");
|
|
128
|
+
if (re)
|
|
129
|
+
return re.test(pathname);
|
|
130
|
+
return pathname === pattern;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Load and execute middleware for a given request.
|
|
134
|
+
*
|
|
135
|
+
* @param server - Vite dev server (for SSR module loading)
|
|
136
|
+
* @param middlewarePath - Absolute path to the middleware file
|
|
137
|
+
* @param request - The incoming Request object
|
|
138
|
+
* @returns Middleware result describing what action to take
|
|
139
|
+
*/
|
|
140
|
+
export async function runMiddleware(server, middlewarePath, request) {
|
|
141
|
+
// Load the middleware module via Vite's SSR module loader
|
|
142
|
+
const mod = await server.ssrLoadModule(middlewarePath);
|
|
143
|
+
// Accept: default export, named "proxy" (Next.js 16), or named "middleware"
|
|
144
|
+
const middlewareFn = mod.default ?? mod.proxy ?? mod.middleware;
|
|
145
|
+
if (typeof middlewareFn !== "function") {
|
|
146
|
+
// No proxy/middleware function exported — continue as normal
|
|
147
|
+
return { continue: true };
|
|
148
|
+
}
|
|
149
|
+
// Check matcher config
|
|
150
|
+
const config = mod.config;
|
|
151
|
+
const matcher = config?.matcher;
|
|
152
|
+
const url = new URL(request.url);
|
|
153
|
+
if (!matchesMiddleware(url.pathname, matcher)) {
|
|
154
|
+
return { continue: true };
|
|
155
|
+
}
|
|
156
|
+
// Wrap in NextRequest so middleware gets .nextUrl, .cookies, .geo, .ip, etc.
|
|
157
|
+
const nextRequest = request instanceof NextRequest ? request : new NextRequest(request);
|
|
158
|
+
// Execute the middleware
|
|
159
|
+
let response;
|
|
160
|
+
try {
|
|
161
|
+
response = await middlewareFn(nextRequest);
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
console.error("[vinext] Middleware error:", e);
|
|
165
|
+
return {
|
|
166
|
+
continue: false,
|
|
167
|
+
response: new Response("Middleware Error: " + (e?.message ?? String(e)), {
|
|
168
|
+
status: 500,
|
|
169
|
+
}),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
// No response = continue
|
|
173
|
+
if (!response) {
|
|
174
|
+
return { continue: true };
|
|
175
|
+
}
|
|
176
|
+
// Check for x-middleware-next header (NextResponse.next())
|
|
177
|
+
if (response.headers.get("x-middleware-next") === "1") {
|
|
178
|
+
// Continue to the route, but apply any headers the middleware set
|
|
179
|
+
const responseHeaders = new Headers();
|
|
180
|
+
for (const [key, value] of response.headers) {
|
|
181
|
+
if (key !== "x-middleware-next" &&
|
|
182
|
+
key !== "x-middleware-rewrite") {
|
|
183
|
+
responseHeaders.set(key, value);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { continue: true, responseHeaders };
|
|
187
|
+
}
|
|
188
|
+
// Check for redirect (3xx status)
|
|
189
|
+
if (response.status >= 300 && response.status < 400) {
|
|
190
|
+
const location = response.headers.get("Location") ?? response.headers.get("location");
|
|
191
|
+
if (location) {
|
|
192
|
+
return {
|
|
193
|
+
continue: false,
|
|
194
|
+
redirectUrl: location,
|
|
195
|
+
redirectStatus: response.status,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// Check for rewrite (x-middleware-rewrite header)
|
|
200
|
+
const rewriteUrl = response.headers.get("x-middleware-rewrite");
|
|
201
|
+
if (rewriteUrl) {
|
|
202
|
+
// Continue to the route but with a rewritten URL
|
|
203
|
+
const responseHeaders = new Headers();
|
|
204
|
+
for (const [key, value] of response.headers) {
|
|
205
|
+
if (key !== "x-middleware-rewrite") {
|
|
206
|
+
responseHeaders.set(key, value);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Parse the rewrite URL — may be absolute or relative
|
|
210
|
+
let rewritePath;
|
|
211
|
+
try {
|
|
212
|
+
const rewriteParsed = new URL(rewriteUrl, request.url);
|
|
213
|
+
rewritePath = rewriteParsed.pathname + rewriteParsed.search;
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
rewritePath = rewriteUrl;
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
continue: true,
|
|
220
|
+
rewriteUrl: rewritePath,
|
|
221
|
+
rewriteStatus: response.status !== 200 ? response.status : undefined,
|
|
222
|
+
responseHeaders,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
// Middleware returned a full Response (e.g., blocking, custom body)
|
|
226
|
+
return { continue: false, response };
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/server/middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE1D;;;GAGG;AACH,MAAM,WAAW,GAAG;IAClB,UAAU;IACV,UAAU;IACV,WAAW;IACX,cAAc;IACd,cAAc;IACd,eAAe;CAChB,CAAC;AAEF,MAAM,gBAAgB,GAAG;IACvB,eAAe;IACf,gBAAgB;IAChB,eAAe;IACf,gBAAgB;IAChB,mBAAmB;IACnB,oBAAoB;IACpB,mBAAmB;IACnB,oBAAoB;CACrB,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,kEAAkE;IAClE,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE,CAAC;QAC/B,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACvC,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC5B,OAAO,QAAQ,CAAC;QAClB,CAAC;IACH,CAAC;IAED,wDAAwD;IACxD,KAAK,MAAM,IAAI,IAAI,gBAAgB,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAA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AC,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;YACvD,WAAW,GAAG,aAAa,CAAC,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC;QAC9D,CAAC;QAAC,MAAM,CAAC;YACP,WAAW,GAAG,UAAU,CAAC;QAC3B,CAAC;QACD,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,UAAU,EAAE,WAAW;YACvB,aAAa,EAAE,QAAQ,CAAC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;YACpE,eAAe;SAChB,CAAC;IACJ,CAAC;IAED,oEAAoE;IACpE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;AACvC,CAAC","sourcesContent":["/**\n * proxy.ts / middleware.ts runner\n *\n * Loads and executes the user's proxy.ts (Next.js 16) or middleware.ts file\n * before routing. Runs in Node (not Edge Runtime), per the vinext design.\n *\n * In Next.js 16, proxy.ts replaces middleware.ts:\n * - proxy.ts: default export function, runs on Node.js runtime\n * - middleware.ts: deprecated but still supported for Edge runtime use cases\n *\n * The proxy/middleware receives a NextRequest and can:\n * - Return NextResponse.next() to continue to the route\n * - Return NextResponse.redirect() to redirect\n * - Return NextResponse.rewrite() to rewrite the URL\n * - Set/modify headers and cookies\n * - Return a Response directly (e.g., for auth guards)\n *\n * Supports the `config.matcher` export for path filtering.\n */\n\nimport type { ViteDevServer } from \"vite\";\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport { NextRequest } from \"../shims/server.js\";\nimport { safeRegExp } from \"../config/config-matchers.js\";\n\n/**\n * Possible proxy/middleware file names.\n * proxy.ts (Next.js 16) is checked first, then middleware.ts (deprecated).\n */\nconst PROXY_FILES = [\n \"proxy.ts\",\n \"proxy.js\",\n \"proxy.mjs\",\n \"src/proxy.ts\",\n \"src/proxy.js\",\n \"src/proxy.mjs\",\n];\n\nconst MIDDLEWARE_FILES = [\n \"middleware.ts\",\n \"middleware.tsx\",\n \"middleware.js\",\n \"middleware.mjs\",\n \"src/middleware.ts\",\n \"src/middleware.tsx\",\n \"src/middleware.js\",\n \"src/middleware.mjs\",\n];\n\n/**\n * Find the proxy or middleware file in the project root.\n * Checks for proxy.ts (Next.js 16) first, then falls back to middleware.ts.\n * If middleware.ts is found, logs a deprecation warning.\n */\nexport function findMiddlewareFile(root: string): string | null {\n // Check proxy.ts first (Next.js 16 replacement for middleware.ts)\n for (const file of PROXY_FILES) {\n const fullPath = path.join(root, file);\n if (fs.existsSync(fullPath)) {\n return fullPath;\n }\n }\n\n // Fall back to middleware.ts (deprecated in Next.js 16)\n for (const file of MIDDLEWARE_FILES) {\n const fullPath = path.join(root, file);\n if (fs.existsSync(fullPath)) {\n console.warn(\n \"[vinext] middleware.ts is deprecated in Next.js 16. \" +\n \"Rename to proxy.ts and export a default function.\",\n );\n return fullPath;\n }\n }\n return null;\n}\n\n/** Matcher pattern from middleware config export. */\ntype MatcherConfig =\n | string\n | string[]\n | { source: string; regexp?: string; locale?: boolean; has?: any[]; missing?: any[] }[];\n\n/**\n * Check if a pathname matches the middleware matcher config.\n * If no matcher is configured, middleware runs on all paths\n * except static files and internal Next.js paths.\n */\nexport function matchesMiddleware(\n pathname: string,\n matcher: MatcherConfig | undefined,\n): boolean {\n if (!matcher) {\n // Default: match all paths except static files, _next, and favicon\n return (\n !pathname.startsWith(\"/_next\") &&\n !pathname.startsWith(\"/api\") &&\n !pathname.includes(\".\") &&\n pathname !== \"/favicon.ico\"\n );\n }\n\n const patterns: string[] = [];\n if (typeof matcher === \"string\") {\n patterns.push(matcher);\n } else if (Array.isArray(matcher)) {\n for (const m of matcher) {\n if (typeof m === \"string\") {\n patterns.push(m);\n } else if (m && typeof m === \"object\" && \"source\" in m) {\n patterns.push(m.source);\n }\n }\n }\n\n return patterns.some((pattern) => matchPattern(pathname, pattern));\n}\n\n/**\n * Match a single pattern against a pathname.\n * Supports Next.js matcher patterns:\n * /about -> exact match\n * /dashboard/:path* -> prefix match with params\n * /api/:path+ -> one or more segments\n * /((?!api|_next).*) -> regex patterns\n */\nexport function matchPattern(pathname: string, pattern: string): boolean {\n // Handle regex patterns (starts with /)\n if (pattern.includes(\"(\") || pattern.includes(\"\\\\\")) {\n const re = safeRegExp(\"^\" + pattern + \"$\");\n if (re) return re.test(pathname);\n // Fall through to simple matching\n }\n\n // Convert Next.js path patterns to regex.\n // Escape dots FIRST (before replacements that produce regex metacharacters\n // like .* and .+ which must not be escaped).\n const regexStr = pattern\n // Escape dots in the literal path segments\n .replace(/\\./g, \"\\\\.\")\n // /:path* -> optionally match slash + zero or more segments\n .replace(/\\/:(\\w+)\\*/g, \"(?:/.*)?\")\n // /:path+ -> match slash + one or more segments\n .replace(/\\/:(\\w+)\\+/g, \"(?:/.+)\")\n // :param -> match one segment\n .replace(/:(\\w+)/g, \"([^/]+)\");\n\n const re = safeRegExp(\"^\" + regexStr + \"$\");\n if (re) return re.test(pathname);\n return pathname === pattern;\n}\n\n/** Result of running middleware. */\nexport interface MiddlewareResult {\n /** Whether to continue to the route handler. */\n continue: boolean;\n /** If set, redirect to this URL. */\n redirectUrl?: string;\n /** HTTP status for redirect (default 307). */\n redirectStatus?: number;\n /** If set, rewrite to this URL (internal). */\n rewriteUrl?: string;\n /** HTTP status for rewrite (e.g. 403 from NextResponse.rewrite(url, { status: 403 })). */\n rewriteStatus?: number;\n /** Headers to set on the response. */\n responseHeaders?: Headers;\n /** If the middleware returned a full Response, use it directly. */\n response?: Response;\n}\n\n/**\n * Load and execute middleware for a given request.\n *\n * @param server - Vite dev server (for SSR module loading)\n * @param middlewarePath - Absolute path to the middleware file\n * @param request - The incoming Request object\n * @returns Middleware result describing what action to take\n */\nexport async function runMiddleware(\n server: ViteDevServer,\n middlewarePath: string,\n request: Request,\n): Promise<MiddlewareResult> {\n // Load the middleware module via Vite's SSR module loader\n const mod = await server.ssrLoadModule(middlewarePath);\n\n // Accept: default export, named \"proxy\" (Next.js 16), or named \"middleware\"\n const middlewareFn = mod.default ?? mod.proxy ?? mod.middleware;\n if (typeof middlewareFn !== \"function\") {\n // No proxy/middleware function exported — continue as normal\n return { continue: true };\n }\n\n // Check matcher config\n const config = mod.config;\n const matcher = config?.matcher;\n const url = new URL(request.url);\n\n if (!matchesMiddleware(url.pathname, matcher)) {\n return { continue: true };\n }\n\n // Wrap in NextRequest so middleware gets .nextUrl, .cookies, .geo, .ip, etc.\n const nextRequest = request instanceof NextRequest ? request : new NextRequest(request);\n\n // Execute the middleware\n let response: Response | undefined;\n try {\n response = await middlewareFn(nextRequest);\n } catch (e: any) {\n console.error(\"[vinext] Middleware error:\", e);\n return {\n continue: false,\n response: new Response(\"Middleware Error: \" + (e?.message ?? String(e)), {\n status: 500,\n }),\n };\n }\n\n // No response = continue\n if (!response) {\n return { continue: true };\n }\n\n // Check for x-middleware-next header (NextResponse.next())\n if (response.headers.get(\"x-middleware-next\") === \"1\") {\n // Continue to the route, but apply any headers the middleware set\n const responseHeaders = new Headers();\n for (const [key, value] of response.headers) {\n if (\n key !== \"x-middleware-next\" &&\n key !== \"x-middleware-rewrite\"\n ) {\n responseHeaders.set(key, value);\n }\n }\n return { continue: true, responseHeaders };\n }\n\n // Check for redirect (3xx status)\n if (response.status >= 300 && response.status < 400) {\n const location = response.headers.get(\"Location\") ?? response.headers.get(\"location\");\n if (location) {\n return {\n continue: false,\n redirectUrl: location,\n redirectStatus: response.status,\n };\n }\n }\n\n // Check for rewrite (x-middleware-rewrite header)\n const rewriteUrl = response.headers.get(\"x-middleware-rewrite\");\n if (rewriteUrl) {\n // Continue to the route but with a rewritten URL\n const responseHeaders = new Headers();\n for (const [key, value] of response.headers) {\n if (key !== \"x-middleware-rewrite\") {\n responseHeaders.set(key, value);\n }\n }\n // Parse the rewrite URL — may be absolute or relative\n let rewritePath: string;\n try {\n const rewriteParsed = new URL(rewriteUrl, request.url);\n rewritePath = rewriteParsed.pathname + rewriteParsed.search;\n } catch {\n rewritePath = rewriteUrl;\n }\n return {\n continue: true,\n rewriteUrl: rewritePath,\n rewriteStatus: response.status !== 200 ? response.status : undefined,\n responseHeaders,\n };\n }\n\n // Middleware returned a full Response (e.g., blocking, custom body)\n return { continue: false, response };\n}\n"]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production server for vinext.
|
|
3
|
+
*
|
|
4
|
+
* Serves the built output from `vinext build`. Handles:
|
|
5
|
+
* - Static asset serving from client build output
|
|
6
|
+
* - Pages Router: SSR rendering + API route handling
|
|
7
|
+
* - App Router: RSC/SSR rendering, route handlers, server actions
|
|
8
|
+
* - Gzip/Brotli compression for text-based responses
|
|
9
|
+
* - Streaming SSR for App Router
|
|
10
|
+
*
|
|
11
|
+
* Build output for Pages Router:
|
|
12
|
+
* - dist/client/ — static assets (JS, CSS, images) + .vite/ssr-manifest.json
|
|
13
|
+
* - dist/server/entry.js — SSR entry point (virtual:vinext-server-entry)
|
|
14
|
+
*
|
|
15
|
+
* Build output for App Router:
|
|
16
|
+
* - dist/client/ — static assets (JS, CSS, images)
|
|
17
|
+
* - dist/server/index.js — RSC entry (default export: handler(Request) → Response)
|
|
18
|
+
* - dist/server/ssr/index.js — SSR entry (imported by RSC entry at runtime)
|
|
19
|
+
*/
|
|
20
|
+
import { type IncomingMessage, type ServerResponse } from "node:http";
|
|
21
|
+
export interface ProdServerOptions {
|
|
22
|
+
/** Port to listen on */
|
|
23
|
+
port?: number;
|
|
24
|
+
/** Host to bind to */
|
|
25
|
+
host?: string;
|
|
26
|
+
/** Path to the build output directory */
|
|
27
|
+
outDir?: string;
|
|
28
|
+
/** Disable compression (default: false) */
|
|
29
|
+
noCompression?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Content types that benefit from compression. */
|
|
32
|
+
declare const COMPRESSIBLE_TYPES: Set<string>;
|
|
33
|
+
/** Minimum size threshold for compression (in bytes). Below this, compression overhead isn't worth it. */
|
|
34
|
+
declare const COMPRESS_THRESHOLD = 1024;
|
|
35
|
+
/**
|
|
36
|
+
* Parse the Accept-Encoding header and return the best supported encoding.
|
|
37
|
+
* Preference order: br > gzip > deflate > identity.
|
|
38
|
+
*/
|
|
39
|
+
declare function negotiateEncoding(req: IncomingMessage): "br" | "gzip" | "deflate" | null;
|
|
40
|
+
/**
|
|
41
|
+
* Send a compressed response if the content type is compressible and the
|
|
42
|
+
* client supports compression. Otherwise send uncompressed.
|
|
43
|
+
*/
|
|
44
|
+
declare function sendCompressed(req: IncomingMessage, res: ServerResponse, body: string | Buffer, contentType: string, statusCode: number, extraHeaders?: Record<string, string>, compress?: boolean): void;
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the host for a request, ignoring X-Forwarded-Host to prevent
|
|
47
|
+
* host header poisoning attacks (open redirects, cache poisoning).
|
|
48
|
+
*
|
|
49
|
+
* X-Forwarded-Host is only trusted when the VINEXT_TRUSTED_HOSTS env var
|
|
50
|
+
* lists the forwarded host value. Without this, an attacker can send
|
|
51
|
+
* X-Forwarded-Host: evil.com and poison any redirect that resolves
|
|
52
|
+
* against request.url.
|
|
53
|
+
*
|
|
54
|
+
* On Cloudflare Workers, X-Forwarded-Host is always set by Cloudflare
|
|
55
|
+
* itself, so this is only a concern for the Node.js prod-server.
|
|
56
|
+
*/
|
|
57
|
+
declare function resolveHost(req: IncomingMessage, fallback: string): string;
|
|
58
|
+
/** Hosts that are allowed as X-Forwarded-Host values (stored lowercase). */
|
|
59
|
+
declare const trustedHosts: Set<string>;
|
|
60
|
+
/**
|
|
61
|
+
* Whether to trust X-Forwarded-Proto from upstream proxies.
|
|
62
|
+
* Enabled when VINEXT_TRUST_PROXY=1 or when VINEXT_TRUSTED_HOSTS is set
|
|
63
|
+
* (having trusted hosts implies a trusted proxy).
|
|
64
|
+
*/
|
|
65
|
+
declare const trustProxy: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Convert a Node.js IncomingMessage to a Web Request object.
|
|
68
|
+
*/
|
|
69
|
+
declare function nodeToWebRequest(req: IncomingMessage): Request;
|
|
70
|
+
/**
|
|
71
|
+
* Start the production server.
|
|
72
|
+
*
|
|
73
|
+
* Automatically detects whether the build is App Router (dist/server/index.js) or
|
|
74
|
+
* Pages Router (dist/server/entry.js) and configures the appropriate handler.
|
|
75
|
+
*/
|
|
76
|
+
export declare function startProdServer(options?: ProdServerOptions): Promise<import("node:http").Server<typeof IncomingMessage, typeof ServerResponse>>;
|
|
77
|
+
export { sendCompressed, negotiateEncoding, COMPRESSIBLE_TYPES, COMPRESS_THRESHOLD, resolveHost, trustedHosts, trustProxy, nodeToWebRequest };
|
|
78
|
+
//# sourceMappingURL=prod-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prod-server.d.ts","sourceRoot":"","sources":["../../src/server/prod-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAgB,KAAK,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAsBpF,MAAM,WAAW,iBAAiB;IAChC,wBAAwB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,sBAAsB;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2CAA2C;IAC3C,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,mDAAmD;AACnD,QAAA,MAAM,kBAAkB,aAetB,CAAC;AAEH,0GAA0G;AAC1G,QAAA,MAAM,kBAAkB,OAAO,CAAC;AAEhC;;;GAGG;AACH,iBAAS,iBAAiB,CAAC,GAAG,EAAE,eAAe,GAAG,IAAI,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAQjF;AAoBD;;;GAGG;AACH,iBAAS,cAAc,CACrB,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,YAAY,GAAE,MAA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