-
-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathroute-rules.ts
More file actions
117 lines (111 loc) · 4.31 KB
/
route-rules.ts
File metadata and controls
117 lines (111 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { proxyRequest, redirect as sendRedirect } from "h3";
import type { EventHandler, Middleware } from "h3";
import type { MatchedRouteRule, NitroRouteRules } from "nitro/types";
import { joinURL, withQuery, withoutBase } from "ufo";
import { defineCachedHandler } from "./cache.ts";
// Note: Remember to update RuntimeRouteRules in src/routing.ts when adding new route rules
type RouteRuleCtor<T extends keyof NitroRouteRules> = (m: MatchedRouteRule<T>) => Middleware;
// Headers route rule
export const headers: RouteRuleCtor<"headers"> = ((m) =>
function headersRouteRule(event) {
for (const [key, value] of Object.entries(m.options || {})) {
event.res.headers.set(key, value);
}
}) satisfies RouteRuleCtor<"headers">;
// Redirect route rule
export const redirect: RouteRuleCtor<"redirect"> = ((m) =>
function redirectRouteRule(event) {
let target = m.options?.to;
if (!target) {
return;
}
if (target.endsWith("/**")) {
let targetPath = event.url.pathname + event.url.search;
const strpBase = (m.options as any)._redirectStripBase;
if (strpBase) {
targetPath = withoutBase(targetPath, strpBase);
}
target = joinURL(target.slice(0, -3), targetPath);
} else if (target.includes("**")) {
// Wildcard ** in a non-trailing position (e.g., "/target?param=**")
// Use only pathname (not search) to avoid redirect loops
let targetPath = event.url.pathname;
const strpBase = (m.options as any)._redirectStripBase;
if (strpBase) {
targetPath = withoutBase(targetPath, strpBase);
}
targetPath = targetPath.replace(/^\//, "");
if (!targetPath) {
// No captured path -> skip redirect to avoid infinite loop
return;
}
target = target.replace("**", targetPath);
// Merge any original query params into the target
if (event.url.search) {
target = withQuery(target, Object.fromEntries(event.url.searchParams));
}
} else if (event.url.search) {
target = withQuery(target, Object.fromEntries(event.url.searchParams));
}
return sendRedirect(target, m.options?.status);
}) satisfies RouteRuleCtor<"redirect">;
// Proxy route rule
export const proxy: RouteRuleCtor<"proxy"> = ((m) =>
function proxyRouteRule(event) {
let target = m.options?.to;
if (!target) {
return;
}
if (target.endsWith("/**")) {
let targetPath = event.url.pathname + event.url.search;
const strpBase = (m.options as any)._proxyStripBase;
if (strpBase) {
targetPath = withoutBase(targetPath, strpBase);
}
target = joinURL(target.slice(0, -3), targetPath);
} else if (target.includes("**")) {
// Wildcard ** in a non-trailing position (e.g., "/target?param=**")
// Use only pathname (not search) to avoid redirect loops
let targetPath = event.url.pathname;
const strpBase = (m.options as any)._proxyStripBase;
if (strpBase) {
targetPath = withoutBase(targetPath, strpBase);
}
targetPath = targetPath.replace(/^\//, "");
if (!targetPath) {
// No captured path -> skip proxy to avoid infinite loop
return;
}
target = target.replace("**", targetPath);
// Merge any original query params into the target
if (event.url.search) {
target = withQuery(target, Object.fromEntries(event.url.searchParams));
}
} else if (event.url.search) {
target = withQuery(target, Object.fromEntries(event.url.searchParams));
}
return proxyRequest(event, target, {
...m.options,
});
}) satisfies RouteRuleCtor<"proxy">;
// Cache route rule
export const cache: RouteRuleCtor<"cache"> = ((m) =>
function cacheRouteRule(event, next) {
if (!event.context.matchedRoute) {
return next();
}
const cachedHandlers: Map<string, EventHandler> = ((globalThis as any).__nitroCachedHandlers ??=
new Map());
const { handler, route } = event.context.matchedRoute;
const key = `${m.route}:${route}`;
let cachedHandler = cachedHandlers.get(key);
if (!cachedHandler) {
cachedHandler = defineCachedHandler(handler, {
group: "nitro/route-rules",
name: key,
...m.options,
});
cachedHandlers.set(key, cachedHandler);
}
return cachedHandler(event);
}) satisfies RouteRuleCtor<"cache">;