-
-
Notifications
You must be signed in to change notification settings - Fork 813
Expand file tree
/
Copy pathroute-rules.ts
More file actions
96 lines (89 loc) · 3.49 KB
/
route-rules.ts
File metadata and controls
96 lines (89 loc) · 3.49 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
import { proxyRequest, redirect as sendRedirect, requireBasicAuth } from "h3";
import type { BasicAuthOptions, 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/build/virtual/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 (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 (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) {
// Sanitize the name to avoid filesystem path conflicts.
// Route paths contain "/" which unstorage's fs drivers interpret as
// directory separators, causing ENOTDIR when a path like "/foo" is
// cached as a file but "/foo/bar" needs it to be a directory.
const safeName = key.replace(/\//g, "_");
cachedHandler = defineCachedHandler(handler, {
group: "nitro-route-rules",
name: safeName,
...m.options,
});
cachedHandlers.set(key, cachedHandler);
}
return cachedHandler(event);
}) satisfies RouteRuleCtor<"cache">;
// basicAuth auth route rule
export const basicAuth: RouteRuleCtor<"auth"> = ((m) =>
async function authRouteRule(event, next) {
if (!m.options) {
return;
}
await requireBasicAuth(event, m.options as BasicAuthOptions);
return next();
}) satisfies RouteRuleCtor<"auth">;