Refactor to Cloudflare Workers #1

Merged
Yushu merged 3 commits from feat/cloudflare-worker into main 2026-08-08 06:00:46 +00:00
Owner
No description provided.
worker: Cloudflare Workers + KV 版
Some checks failed
.NET / build (push) Has been cancelled
5f22376f54
存储从本地文件换成 KV —— 这是 serverless 化唯一必须改的东西:
Workers 的文件系统是临时的,PUT 写完下次调用就没了。

## 与 .NET 版的差异(都是有意的,逐条有用例锁着)

修正三处未严格遵循 HTTP 语义的行为:
  1. GET 空库 500 → 404;2. PUT JSON 坏 500 → 400;
  3. 版本号格式非法 500 → 400 且报到字段级(原来不说哪个字段错)
  原实现里这三种情况全部落进 UseExceptionHandler 变成 5xx,
  服务器没错却报服务器错,客户端据此重试也没有意义。

补齐五项标准件:
  WWW-Authenticate(RFC 9110 §15.5.2 规定 401 必须带)、HEAD、
  Cache-Control、ETag + If-None-Match、405 的 Allow

⚠️另有一项不是"加功能"而是"不加就废":CORS 与 OPTIONS 预检。
线上那几个 access-control-* 头是**源站 nginx 加的**,.NET 代码里没有 UseCors。
Worker route 一拦截请求就不回源,nginx 不参与,头跟着消失;
而 panel 的 PUT 带 Authorization + JSON body,浏览器必发预检 ——
不补的话 panel 保存功能 100% 失效。**这条只有核对客户端源码才发现得了。**

## 验证

37 条行为回归 + tsc --noEmit,均不依赖 CF 账号。
响应格式不是照代码推的:拉了线上真实响应对过字段序、版本号形状、
content-type,以及外链过 URL 规范化后是否变形。

两个客户端逐行核过:panel 受 CORS/预检影响(已解),
func 用 HttpClient 不做缓存不发条件请求、非 2xx 一律静默,完全不受影响。

另含切流量前的对拍脚本 worker/compare.sh:
手动 curl 一眼只会看 body 像不像,而真正会咬人的是没去看的那几项
(字段顺序、预检、401 的头),脚本把"该看什么"固化下来。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Yushu merged commit c7485c2517 into main 2026-08-08 06:00:46 +00:00
Yushu referenced this pull request from a commit 2026-08-08 06:00:47 +00:00
Yushu deleted branch feat/cloudflare-worker 2026-08-08 06:00:47 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
SourceZoneDev/mo4-version-api!1
No description provided.