fix: /docs 交互式文档改为完全离线,去掉 CDN 依赖

- scripts/build/build-docs.mjs:固定 @scalar/api-reference@1.25.101,
  下载后按 SHA-256 校验,生成 src/packaging/generated/
  scalar-standalone.js.txt(2.7 MiB,随 git 提交)。
- embedded-docs.ts 用 with { type: text } 内嵌;后缀 .txt 避开 Bun
  对 .js / .html 的非字符串类型推断。
- renderDocsHtml() 把 script src 从 cdn.jsdelivr.net 换成
  /static/scalar.js;routes.ts 新增该路由返回内嵌 JS 并带 24h 缓存。
  保留原 <script id="api-reference"> 结构,Scalar 定位逻辑不变。
- 加 CSP(connect-src 'self' 等):Scalar 内置的「加载示例规范」
  分支会 fetch jsdelivr 的 galaxy.yaml,本页 data-url 指向本地
  /openapi.json 不走该分支,CSP 再兜底确保任何情况下无外网请求。
- 新增 docs.test.ts 3 例;更新 README、packaging README、
  build README;package.json 加 docs:fetch 脚本。

二进制体积 +2.9 MB(67.5 → 70.4 MB)。
校验:bun test 1114 pass / 0 fail,check、check:readme 36/36;
编译产物实测 GET /docs 200 且零 CDN,GET /static/scalar.js
200 / application/javascript / 2685520 字节且 SHA-256 与生成物一致,
/openapi.json 200。

本次只合入 main,不打 tag、不发版。
This commit is contained in:
gitea committed 2026-11-05 20:55:10 +08:00
1 parent f9de8337b2
commit 7666db9f1d
10 files changed
+30237 -2

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
import { describe, expect, test } from "bun:test";
import { renderDocsHtml } from "./docs.js";
import { handleHttpRequest } from "./routes.js";
import { getScalarStandaloneJs } from "../packaging/embedded-docs.js";
describe("离线 API 文档", () => {
test("HTML 不引用任何 CDN,指向本地路由", () => {
const html = renderDocsHtml();
expect(html).toContain('src="/static/scalar.js"');
expect(html).toContain('data-url="/openapi.json"');
expect(html).toContain('id="api-reference"');
expect(html).not.toContain("cdn.jsdelivr.net");
expect(html).not.toMatch(/src="https?:\/\//);
expect(html).toContain("connect-src 'self'");
expect(html).toContain("Content-Security-Policy");
});
test("内嵌运行时是完整的 Scalar standalone", () => {
const js = getScalarStandaloneJs();
expect(js.length).toBeGreaterThan(1_000_000);
expect(js).toContain("Scalar");
expect(js).not.toContain("</script>");
});
test("GET /static/scalar.js 返回 JS 且与内嵌产物一致", async () => {
let status = 0;
let headers: Record<string, string> = {};
let body = "";
const res = {
setHeader: (key: string, value: unknown) => { headers[key] = String(value); },
writeHead: (code: number, next: Record<string, string>) => {
status = code;
headers = { ...headers, ...next };
},
end: (value?: string) => { body = value ?? ""; },
headersSent: false,
};
await handleHttpRequest(
{ method: "GET", url: "/static/scalar.js", headers: { host: "localhost" } } as never,
res as never,
{ app: "hose-use" }
);
expect(status).toBe(200);
expect(headers["Content-Type"]).toContain("application/javascript");
expect(body).toBe(getScalarStandaloneJs());
});
});
+3 -1
View File
@@ -6,6 +6,8 @@ export function renderDocsHtml(): string {
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- 运行时全部资源来自本地:即使 Scalar 内置的"示例规范"分支被触发也发不出外网请求 -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: blob:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; worker-src 'self' blob:;" />
<title>hose-use API Reference (v${VERSION})</title>
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>⚡</text></svg>" />
<style>
@@ -22,7 +24,7 @@ export function renderDocsHtml(): string {
id="api-reference"
data-url="/openapi.json"
data-configuration='{"theme":"purple","layout":"modern","showSidebar":true}'
src="https://cdn.jsdelivr.net/npm/@scalar/api-reference@1.25.101/dist/browser/standalone.js"
src="/static/scalar.js"
></script>
</body>
</html>`;
+17
View File
@@ -5,6 +5,7 @@ import { runAiProCli } from "../aipro-cli.js";
import { loadCorpConfigs, loadGlobalConfig } from "../core/config/loader.js";
import { getOpenApiSchema } from "../packaging/embedded-openapi.js";
import { getAiProWebUiHtml } from "../packaging/embedded-webui.js";
import { getScalarStandaloneJs } from "../packaging/embedded-docs.js";
import { tokenizeCommand, normalizeCliArgs } from "../server.js";
import { VERSION } from "../version.js";
import { renderDocsHtml } from "./docs.js";
@@ -125,6 +126,12 @@ export async function handleHttpRequest(
return;
}
// /docs 的 Scalar 运行时由本地提供,彻底去掉 CDN 依赖
if (method === "GET" && pathname === "/static/scalar.js") {
sendJavascript(res, 200, getScalarStandaloneJs());
return;
}
if (method === "GET" && pathname === "/docs") {
sendHtml(res, 200, renderDocsHtml());
return;
@@ -235,6 +242,16 @@ function sendHtml(res: ServerResponse, status: number, html: string): void {
res.end(html);
}
function sendJavascript(res: ServerResponse, status: number, source: string): void {
if (res.headersSent) return;
res.writeHead(status, {
"Content-Type": "application/javascript; charset=utf-8",
"Content-Length": Buffer.byteLength(source, "utf-8"),
"Cache-Control": "public, max-age=86400",
});
res.end(source);
}
function sendError(res: ServerResponse, status: number, message: string): void {
sendJson(res, status, { error: true, message });
}