ESLint 升级到 recommended-type-checked + stylistic-type-checked, 引入 perfectionist 导入排序和 import 插件导入验证。 Prettier 显式声明全部格式化参数,消除跨环境差异。 TypeScript 启用 noUnusedLocals 和 noPropertyAccessFromIndexSignature。 完善 ignore 列表,排除 .agents/、bun.lock、data/ 等。 引入 husky + lint-staged(pre-commit)+ commitlint(commit-msg)。 更新 DEVELOPMENT.md 代码质量章节。 修复所有新增规则检测到的类型和风格违规。
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import type { RuntimeMode } from "../shared/api";
|
|
import type { StaticAssets } from "./app";
|
|
|
|
import { createHeaders } from "./helpers";
|
|
|
|
export function contentTypeFor(pathname: string): string {
|
|
if (pathname.endsWith(".js") || pathname.endsWith(".mjs")) return "text/javascript; charset=utf-8";
|
|
if (pathname.endsWith(".css")) return "text/css; charset=utf-8";
|
|
if (pathname.endsWith(".svg")) return "image/svg+xml";
|
|
if (pathname.endsWith(".json")) return "application/json; charset=utf-8";
|
|
if (pathname.endsWith(".png")) return "image/png";
|
|
if (pathname.endsWith(".jpg") || pathname.endsWith(".jpeg")) return "image/jpeg";
|
|
if (pathname.endsWith(".ico")) return "image/x-icon";
|
|
|
|
return "application/octet-stream";
|
|
}
|
|
|
|
export function hasFileExtension(pathname: string): boolean {
|
|
return /\/[^/]+\.[^/]+$/.test(pathname);
|
|
}
|
|
|
|
export function htmlResponse(indexHtml: Blob, mode: RuntimeMode): Response {
|
|
return new Response(indexHtml, {
|
|
headers: createHeaders(mode, {
|
|
"Cache-Control": "no-cache",
|
|
"Content-Type": "text/html; charset=utf-8",
|
|
}),
|
|
});
|
|
}
|
|
|
|
export function serveStaticAsset(pathname: string, staticAssets: StaticAssets, mode: RuntimeMode): Response {
|
|
if (pathname === "/") {
|
|
return htmlResponse(staticAssets.indexHtml, mode);
|
|
}
|
|
|
|
const asset = staticAssets.files[pathname];
|
|
|
|
if (asset) {
|
|
return new Response(asset, {
|
|
headers: createHeaders(mode, {
|
|
"Cache-Control": "public, max-age=31536000, immutable",
|
|
"Content-Type": contentTypeFor(pathname),
|
|
}),
|
|
});
|
|
}
|
|
|
|
if (pathname.startsWith("/assets/") || hasFileExtension(pathname)) {
|
|
return new Response("Not Found", {
|
|
headers: createHeaders(mode, { "Content-Type": "text/plain; charset=utf-8" }),
|
|
status: 404,
|
|
});
|
|
}
|
|
|
|
return htmlResponse(staticAssets.indexHtml, mode);
|
|
}
|