Skip to content

refactor(http): 合并三处重复的 HTTP 封装为统一 fetch 客户端 - #2

Merged
wthsama merged 3 commits into
mainfrom
feature/docker-support
Sep 27, 2026
Merged

wthsama merged 3 commits into
mainfrom
feature/docker-support

Conversation

@wthsama

@wthsama wthsama commented Sep 27, 2026

Copy link
Copy Markdown
Owner

背景

auth.js / models.js / cli-login.js 各自拷贝了一份 https.request 封装(代理、超时、JSON 解析),proxy.js 另有一套。四处重复、维护成本高。

改动

  • 新增 src/http.js:基于 undici 的 fetch,按需注入代理 dispatcher,提供 request()(读完响应体)与 rawFetch()(保留响应流,用于 SSE)。
  • auth.js / models.js / cli-login.js / proxy.js 全部改用该模块;依赖 https-proxy-agent → undici。
  • proxy.js 用 Readable.fromWeb 做 SSE 流式转发,重试按 cause 识别网络错误。
  • 支持 CODEX_RESPONSES_URL 覆盖,便于本地测试。

验证

node:22-alpine 下 npm ci 后运行测试脚本,覆盖:普通请求 / 404 / 超时 / 无代理 dispatcher / 首页与 /health、/status、/fast、以及 /v1/responses 的 SSE 流式转发 → ALL TESTS PASSED。

wthsama added 3 commits September 28, 2026 02:41
auth.js / models.js / cli-login.js 各自拷贝了一份 https.request 封装(代理、
超时、JSON 解析),proxy.js 另有一套。现合并为 src/http.js:基于 undici 的
fetch,按需注入代理 dispatcher,提供 request()(读完响应体)与 rawFetch()
(保留响应流,用于 SSE 转发)。依赖 https-proxy-agent 换成 undici。

验证:node:22-alpine 下 npm install 后运行 test.js,覆盖普通请求 / 404 /
超时 / 无代理 dispatcher / 首页与 /health、/status、/fast,以及 /v1/responses
的 SSE 流式转发,全部通过。
新增 test/smoke.js(零依赖断言,失败即非零退出),覆盖 src/http 客户端
(正常/404/超时/无代理)、app 路由(/health、/status、/fast、首页)以及
/v1/responses 的 SSE 流式转发;package.json 增加 test 脚本与 engines。
- ci.yml:pull_request 与 main 推送时跑 npm ci + 语法检查 + npm test。
- release.yml:推 release 分支或 v*.*.* tag 时构建并推送 ghcr.io 镜像,
  打 release/latest 与语义化版本 tag,供本地 docker pull。
@wthsama
wthsama merged commit 3cf9104 into main Sep 27, 2026
1 check passed
@wthsama
wthsama deleted the feature/docker-support branch September 27, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant