-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
57 lines (53 loc) · 2.73 KB
/
Copy pathmiddleware.js
File metadata and controls
57 lines (53 loc) · 2.73 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
// Generated by scripts/vercel.mjs. Do not edit.
const redirects = {"/mobile":"/#mobile","/agents":"/setup"};
const markdownByPath = new Map([["/","/index.md"],["/product","/product.md"],["/docs","/docs.md"],["/setup","/setup.md"],["/download","/download.md"],["/overview","/overview.md"],["/machine","/machine.md"],["/mobile","/mobile.md"],["/agents","/agents.md"],["/services","/services.md"],["/people","/people.md"],["/showcase","/showcase.md"],["/faq","/faq.md"]]);
const HOMEPAGE_LINK_HEADER = "</.well-known/api-catalog>; rel=\"api-catalog\"; type=\"application/linkset+json\", <https://localstudio.ai/.well-known/api-catalog>; rel=\"api-catalog\"; type=\"application/linkset+json\", </llms.txt>; rel=\"describedby\"; type=\"text/markdown\", </index.md>; rel=\"alternate\"; type=\"text/markdown\", </docs>; rel=\"service-doc\"; type=\"text/html\", </docs.md>; rel=\"service-doc\"; type=\"text/markdown\", </openapi.json>; rel=\"service-desc\"; type=\"application/vnd.oai.openapi+json;version=3.1\", </.well-known/mcp>; rel=\"service\"; type=\"application/json\", </developers>; rel=\"service-doc\"; type=\"text/html\", </.well-known/agent-card.json>; rel=\"service-desc\"; type=\"application/json\", </sitemap.xml>; rel=\"describedby\"; type=\"application/xml\", <https://localstudio.ai/>; rel=\"canonical\"; type=\"text/html\"";
function wantsMarkdown(acceptHeader) {
return (
acceptHeader.includes("text/markdown") &&
!acceptHeader.includes("text/html")
);
}
function markdownTokenCount(body) {
return String(Math.max(1, Math.ceil(body.length / 4)));
}
// Vercel requires a statically analyzable matcher.
export const config = {
matcher: "/(.*)",
};
export default async function middleware(request) {
let url;
try {
url = new URL(request.url);
} catch {
return new Response("Invalid request URL\n", {
status: 400,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
const pathname =
url.pathname !== "/" && url.pathname.endsWith("/")
? url.pathname.slice(0, -1)
: url.pathname;
const redirect = redirects[pathname];
if (redirect) {
return Response.redirect(new URL(redirect, url.origin), 308);
}
const accept = request.headers.get("accept") ?? "";
if (!wantsMarkdown(accept)) return;
const markdownPath = markdownByPath.get(pathname);
if (markdownPath === undefined) return;
const upstream = await fetch(new URL(markdownPath, url.origin));
if (!upstream.ok) return;
const body = await upstream.text();
const headers = {
"Cache-Control": "public, max-age=0, must-revalidate",
"Content-Type": "text/markdown; charset=utf-8",
Vary: "Accept",
"x-markdown-tokens": markdownTokenCount(body),
};
if (pathname === "/") {
headers.Link = HOMEPAGE_LINK_HEADER;
}
return new Response(body, { status: 200, headers });
}