Repository navigation
Expand file tree
/
Copy pathhandler.ts
More file actions
244 lines (222 loc) · 8.25 KB
/
Copy pathhandler.ts
File metadata and controls
244 lines (222 loc) · 8.25 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* @file handler.ts
* @description
* [EN] Core Logic Entry Point.
* This module acts as the controller that coordinates config loading,
* route matching (regex/prefix), and response handling. It is platform-agnostic.
*
* [CN] 核心逻辑入口。
* 该模块作为控制器,负责协调配置加载、路由匹配(正则/前缀)以及响应处理。
* 它与具体部署平台(Cloudflare/Vercel)解耦,通用性强。
*
* @see {@link https://github.com/Cedarflake/Cedarflake-Nami} for repository info.
*/
import {
ANALYTICS_ATTRIBUTION_QUERY_PARAM,
clearAttributionCookie,
finalizeMatchedAnalytics,
finalizeRuntimeAnalytics,
prepareAnalyticsRequest,
readAttributionCookie
} from "@handlers/analytics";
import { HTTPS_REDIRECT_STATUS } from "@handlers/core/constants";
import { dispatchRouteRequest } from "@handlers/routing/dispatcher";
import { serveFavicon } from "@handlers/resources/favicon-serve";
import { interLatinVariableFontPath, serveInterFont } from "@handlers/resources/font-serve";
import {
loadDataConfig,
loadRedirects,
resolveRuntimeOptions
} from "@handlers/configuration/loader";
import {
flattenSlots,
getCompiledList,
getSlotSource
} from "@handlers/routing/matcher";
import { generateRobots, generateSitemapXml, isRobotsAllowed } from "@handlers/resources/seo";
import { notFoundPageHtml } from "@handlers/resources/templates";
import { createRuntimeFeaturePipeline } from "@/plugins/features";
import type { AnalyticsRequestContext } from "@handlers/analytics";
import type { HandlerOptions, RouteValueEntry } from "@handlers/core/types";
import { inferEffectivePath, isLikelyStaticAssetPath, normalisePath, safeDecode } from "@handlers/core/utils";
export async function handleRedirectRequest(request: Request, options: HandlerOptions = {}): Promise<Response> {
let runtime = resolveRuntimeOptions(options);
const startedAt = runtime.now();
let analytics: AnalyticsRequestContext | undefined;
let effectivePath = "/";
try {
const initialUrl = new URL(request.url);
if (initialUrl.protocol !== "https:") {
const containsAttributionToken = initialUrl.searchParams.has(ANALYTICS_ATTRIBUTION_QUERY_PARAM);
const destination = `https://${initialUrl.host}${initialUrl.pathname}${initialUrl.search}`;
return createCanonicalRedirect(destination, containsAttributionToken);
}
const initialPath = normalisePath(initialUrl.pathname);
if (!initialUrl.searchParams.has(ANALYTICS_ATTRIBUTION_QUERY_PARAM)) {
const hasAttributionCookie = Boolean(readAttributionCookie(request));
if (initialPath === "/favicon.ico") {
return clearAttributionCookie(serveFavicon(), hasAttributionCookie);
}
if (initialPath === interLatinVariableFontPath) {
return clearAttributionCookie(serveInterFont(), hasAttributionCookie);
}
}
const needsRedirects = initialPath !== "/favicon.ico" && initialPath !== interLatinVariableFontPath;
const [dataConfig, redirectsConfig] = await Promise.all([
loadDataConfig(runtime),
needsRedirects ? loadRedirects(runtime) : Promise.resolve(null)
]);
runtime = {
...runtime,
dataConfig,
featurePipeline: createRuntimeFeaturePipeline(
dataConfig,
{
platformPluginId: runtime.platformPluginId,
pluginInstallations: runtime.pluginInstallations,
runtimePlatformManifests: runtime.runtimePlatformManifests
},
runtime.runtimeFeatures
)
};
analytics = await prepareAnalyticsRequest(request, runtime);
if (analytics.cleanupResponse) {
return analytics.cleanupResponse;
}
const url = analytics.sanitizedUrl;
const path = normalisePath(url.pathname);
effectivePath = safeDecode(path);
if (path === "/favicon.ico") {
return clearAttributionCookie(serveFavicon(), analytics.hasAttributionCookie);
}
if (path === interLatinVariableFontPath) {
return clearAttributionCookie(serveInterFont(), analytics.hasAttributionCookie);
}
const slotSource = getSlotSource(redirectsConfig);
if (!slotSource) {
console.warn("[Handler] No slots configured.");
const response = new Response("503 No Slots configured", {
status: 503,
headers: { "Content-Type": "text/plain; charset=utf-8" }
});
return finalizeRuntimeAnalytics({
request,
response,
outcome: "config_unavailable",
effectivePath,
startedAt,
runtime,
analytics
});
}
if (path === "/robots.txt" || path === "/sitemap.xml") {
const rawRules: Record<string, RouteValueEntry> = {};
flattenSlots(slotSource, rawRules);
const origin = url.origin;
if (path === "/robots.txt") {
const robots = generateRobots(origin, runtime.dataConfig.runtime.robotsPolicy);
const response = new Response(robots, {
status: 200,
headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "public, max-age=3600" }
});
return clearAttributionCookie(response, analytics.hasAttributionCookie);
}
if (!isRobotsAllowed(runtime.dataConfig.runtime.robotsPolicy)) {
const response = new Response("Sitemap disabled", {
status: 404,
headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "public, max-age=300" }
});
return clearAttributionCookie(response, analytics.hasAttributionCookie);
}
const sitemap = generateSitemapXml(origin, rawRules);
const response = new Response(sitemap, {
status: 200,
headers: { "Content-Type": "application/xml; charset=utf-8", "Cache-Control": "public, max-age=3600" }
});
return clearAttributionCookie(response, analytics.hasAttributionCookie);
}
const compiledList = getCompiledList(slotSource);
const decodedPath = safeDecode(path);
effectivePath = inferEffectivePath(decodedPath, request.headers, url.origin, compiledList);
const isStaticAssetPath = isLikelyStaticAssetPath(effectivePath);
const dispatch = await dispatchRouteRequest({
request,
runtime,
compiledList,
effectivePath,
search: url.search,
isStaticAssetPath
});
if (dispatch.match) {
return await finalizeMatchedAnalytics({
request,
response: dispatch.match.response,
rule: dispatch.match.rule,
routePath: dispatch.match.routePath,
matchKind: dispatch.match.matchKind,
effectivePath,
startedAt,
runtime,
analytics
});
}
const isProxyUnavailable = dispatch.proxyFailureReason === "unavailable";
const response = isProxyUnavailable
? new Response("Bad Gateway: All upstream proxies failed.", {
status: 502,
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "no-store"
}
})
: new Response(notFoundPageHtml, {
status: 404,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "public, max-age=60"
}
});
return finalizeRuntimeAnalytics({
request,
response,
outcome: isProxyUnavailable ? "proxy_exhausted" : "not_found",
effectivePath,
startedAt,
runtime,
analytics
});
} catch (error) {
console.error("[Handler Critical Error]", error);
const response = new Response("Internal Server Error", { status: 500 });
return analytics
? finalizeRuntimeAnalytics({
request,
response,
outcome: "internal_error",
effectivePath,
startedAt,
runtime,
analytics
})
: response;
}
}
function createCanonicalRedirect(destination: string, containsAttributionToken: boolean): Response {
if (!containsAttributionToken) {
return Response.redirect(destination, HTTPS_REDIRECT_STATUS);
}
return new Response(null, {
status: HTTPS_REDIRECT_STATUS,
headers: {
"Cache-Control": "private, no-store",
Location: destination,
"Referrer-Policy": "no-referrer"
}
});
}
export {
DEFAULT_CONFIG_URL,
DEFAULT_DATA_CONFIG_URL,
DEFAULT_REDIRECTS_CONFIG_URL
} from "@handlers/configuration/config";
export type { RedirectsConfig, RouteConfig, HandlerOptions } from "@handlers/core/types";