fix(amsg-server): 单用户 Worker 的错误响应补上 CORS 头 - #46
Merged
Conversation
跨域部署里,Worker 抛异常回的 500 之前不带 Access-Control-*,浏览器整条丢掉、 fetch reject 成网络错误,服务端故障和「Worker 连不上」在前端完全无法区分。 - 异常 500 带上与正常响应同一份 CORS 头 - buildConfig 自身抛错时单独兜底:预检回 204、真实请求回能读的 500;该降级路径 的 CORS 头回显来访 Origin 而非 '*',同源调用仍不加头,Max-Age 0 不进缓存 - 没配 cors 的部署行为不变(OPTIONS 仍走 404,响应仍不带 Access-Control-*) 回归测试钉住两条:处理器抛异常的 500 带头;配置构建失败时预检为 2xx 且 500 带头、 同源不加头。 Claude-Session: https://claude.ai/code/session_01GEKR7ivPYPq59A1cJVs14u
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么
配了
cors的部署里,之前只有正常响应带Access-Control-*,Worker 内部抛异常时回的那条 500 是裸的。跨域前端拿到没有Access-Control-Allow-Origin的响应,浏览器会把整条丢掉,fetch直接 reject 成 TypeError(Safari 显示Load failed)——服务端故障在前端长得和「Worker 连不上」一模一样,错误码和错误信息一概读不到。真实排查里踩到过:某个自部署用户所有请求都报
Load failed,从外部探测该 Worker 却完全正常(预检 204 带 CORS、401 也带 CORS),因为只有过了鉴权的请求才会走到抛异常那段。改后:
{ success: false, error: { code: 'INTERNAL_ERROR' } }buildConfig自身抛错时(少绑 binding、环境变量被重新部署刷掉)单独兜底:预检回 204、真实请求回能读的 500。之前这种情况预检也拿裸 500,浏览器根本不会发真正那条请求,整个部署看起来就是彻底离线且零报错cors的部署行为不变:OPTIONS 仍走 404,响应仍不带任何Access-Control-*降级路径的 CORS 策略取舍
buildConfig炸了时部署配置的 CORS 策略无从得知,但错误响应仍然需要头,否则跨域调用方什么都看不到。选的是回显来访的Origin,绝不退化成*:cfg.cors管辖,没配 CORS 的部署不会因为一次故障变成开放的Origin头)依然一个头都不加Access-Control-Max-Age: 0,故障期间答复的预检不进浏览器缓存,配置修好即刻失效测试
packages/rei-standard-amsg/server/test/single-user-worker.test.mjs新增两条回归守卫:Access-Control-Allow-Origin红绿都实跑过:撤掉修复 15 个测试挂 2 个(正是这两条),放回后 15/15 过。server 包完整测试 328 pass / 0 fail,
npm run build通过。备注
docs/reviews/2026-07-26-code-organization-review.md的 A9 记过这个坑,本次一并清掉。https://claude.ai/code/session_01GEKR7ivPYPq59A1cJVs14u