|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import type { HistoryItem } from "@/app/types/docs-history"; |
| 3 | + |
| 4 | +// 响应缓存 1 小时,GitHub API 每小时限额 5000 次(authenticated) |
| 5 | +export const revalidate = 3600; |
| 6 | + |
| 7 | +interface GitHubCommit { |
| 8 | + sha: string; |
| 9 | + commit: { |
| 10 | + author: { |
| 11 | + name: string; |
| 12 | + date: string; |
| 13 | + }; |
| 14 | + message: string; |
| 15 | + }; |
| 16 | + author: { |
| 17 | + login: string; |
| 18 | + avatar_url: string; |
| 19 | + } | null; |
| 20 | + html_url: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * 规范化前端传入的文档路径为仓库根相对路径(GitHub API 要求)。 |
| 25 | + * |
| 26 | + * 接受的输入形态: |
| 27 | + * - `app/docs/ai/...`(仓库根相对)→ 原样返回 |
| 28 | + * - `docs/ai/...` → 前面补 `app/` |
| 29 | + * - `/docs/ai/...`(浏览器 URL 风格)→ 去开头斜杠再补 `app/` |
| 30 | + * |
| 31 | + * 拒绝:含 `..`、反斜杠、null 字节;最终不落在 `app/docs/` 下的路径一律拒绝, |
| 32 | + * 避免用服务端 GITHUB_TOKEN 被动泄露仓库内任意文件的 commit 信息。 |
| 33 | + */ |
| 34 | +function normalizeDocsPath(raw: string): string | null { |
| 35 | + if (!raw) return null; |
| 36 | + // 路径穿越 / 反斜杠 / null 字节 直接拒 |
| 37 | + if (raw.includes("..") || raw.includes("\\") || raw.includes("\0")) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + let normalized = raw; |
| 42 | + // URL 风格 /docs/... → docs/... |
| 43 | + if (normalized.startsWith("/")) { |
| 44 | + normalized = normalized.slice(1); |
| 45 | + } |
| 46 | + // docs/... → app/docs/... |
| 47 | + if (normalized.startsWith("docs/")) { |
| 48 | + normalized = `app/${normalized}`; |
| 49 | + } |
| 50 | + // 必须落在 app/docs/ 下才放行 |
| 51 | + if (!normalized.startsWith("app/docs/")) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + return normalized; |
| 55 | +} |
| 56 | + |
| 57 | +export async function GET(req: NextRequest) { |
| 58 | + const { searchParams } = new URL(req.url); |
| 59 | + const rawPath = searchParams.get("path"); |
| 60 | + |
| 61 | + const path = rawPath ? normalizeDocsPath(rawPath) : null; |
| 62 | + if (!path) { |
| 63 | + return NextResponse.json( |
| 64 | + { |
| 65 | + success: false, |
| 66 | + error: "缺少合法的 path 参数(仅允许 app/docs/ 路径)", |
| 67 | + }, |
| 68 | + { status: 400 }, |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const token = process.env.GITHUB_TOKEN; |
| 73 | + if (!token) { |
| 74 | + return NextResponse.json( |
| 75 | + { success: false, error: "服务端未配置 GITHUB_TOKEN" }, |
| 76 | + { status: 500 }, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const apiUrl = `https://api.github.com/repos/InvolutionHell/involutionhell/commits?path=${encodeURIComponent(path)}&per_page=5`; |
| 81 | + |
| 82 | + let res: Response; |
| 83 | + try { |
| 84 | + res = await fetch(apiUrl, { |
| 85 | + headers: { |
| 86 | + Authorization: `Bearer ${token}`, |
| 87 | + Accept: "application/vnd.github+json", |
| 88 | + "X-GitHub-Api-Version": "2022-11-28", |
| 89 | + }, |
| 90 | + // Next.js fetch 缓存,与 revalidate 配合 |
| 91 | + next: { revalidate: 3600 }, |
| 92 | + }); |
| 93 | + } catch { |
| 94 | + return NextResponse.json( |
| 95 | + { success: false, error: "无法连接 GitHub API" }, |
| 96 | + { status: 502 }, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // 403 可能是限流、也可能是 token 权限不足 / 仓库不可访问;用 x-ratelimit-remaining 区分 |
| 101 | + if (res.status === 403) { |
| 102 | + const rateRemaining = res.headers.get("x-ratelimit-remaining"); |
| 103 | + if (rateRemaining === "0") { |
| 104 | + return NextResponse.json( |
| 105 | + { success: false, error: "GitHub API 限流,请稍后重试" }, |
| 106 | + { status: 429 }, |
| 107 | + ); |
| 108 | + } |
| 109 | + return NextResponse.json( |
| 110 | + { success: false, error: "GitHub API 403(可能 token 权限不足)" }, |
| 111 | + { status: 403 }, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + if (res.status === 401) { |
| 116 | + return NextResponse.json( |
| 117 | + { success: false, error: "GitHub token 无效或过期" }, |
| 118 | + { status: 401 }, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + if (!res.ok) { |
| 123 | + return NextResponse.json( |
| 124 | + { success: false, error: `GitHub API 返回 ${res.status}` }, |
| 125 | + { status: 502 }, |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + const commits: GitHubCommit[] = await res.json(); |
| 130 | + |
| 131 | + const data: HistoryItem[] = commits.map((c) => ({ |
| 132 | + sha: c.sha, |
| 133 | + authorName: c.commit.author.name, |
| 134 | + // author 为 null 时(commit 作者邮箱未关联 GitHub 账号),login 退回展示名 |
| 135 | + authorLogin: c.author?.login ?? c.commit.author.name, |
| 136 | + // commit.author.name 是展示名(可能含中文/空格),拼 github.com/<name>.png 容易 404; |
| 137 | + // 仅在有真实 author 时用其 avatar_url,否则返回空串让前端用占位资源 |
| 138 | + avatarUrl: c.author?.avatar_url ?? "", |
| 139 | + date: c.commit.author.date, |
| 140 | + // 只取 commit message 第一行 |
| 141 | + message: c.commit.message.split("\n")[0], |
| 142 | + htmlUrl: c.html_url, |
| 143 | + })); |
| 144 | + |
| 145 | + return NextResponse.json( |
| 146 | + { success: true, data }, |
| 147 | + { |
| 148 | + headers: { |
| 149 | + "Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400", |
| 150 | + }, |
| 151 | + }, |
| 152 | + ); |
| 153 | +} |
0 commit comments