fix(devtools): 将 wx.request 迁移到主进程并完善 Network 与项目上下文 - #202
Conversation
将 service 和 preload 的请求统一交给 Node http/https,避免 Chromium CORS 与 OPTIONS 预检。保留请求编码、相对 URL、项目 Referer、解压和重定向语义,统一超时、取消及 owner 清理。 通过 NativeRequestTrace 合成 CDP Network 事件,接通 Response/Payload 缓存和两个前端查询入口;限制调试正文预算,修复重定向缓存、并发请求 ID 与销毁重入问题。同步协议及架构文档,保留公开 request-core 兼容出口。 验证:194 项聚焦测试和 7 项真实 Electron E2E 通过。本次提交前 gate 的 lint、typecheck、test 通过;Pawl 未通过:file-length 55→57、cognitive-complexity 0→2、code-duplication 275→303。未修改门禁基线。
保持自定义 API 注册及清理归应用所有,由各窗口的调用入口附带所属项目路径,供宿主区分并发项目。上下文与小程序参数分离,不随窗口焦点切换;保留原有单参数回调兼容。 补充多窗口调用来源和元信息透传回归,保留关闭窗口、注册撤销及动态菜单测试。验证:52 项相关测试通过;electron-runtime 代码构建、devtools 主进程构建和改动文件 ESLint 通过。按本次验证约束沿用聚焦验证,未重复运行全仓 gate,未运行 E2E。
采集 Node HTTP/1.1 已发送请求的完整头部快照,按响应及各跳重定向传递最终字段和原始文本,使 Network 支持完整标头及 Raw 查看。采集失败或尚无响应时保留预配提示,观察数据与业务请求隔离。 恢复带 UTF-8 BOM 的 JSON/文本解码,兼容缺少 zlib 包装的 raw DEFLATE;保留二进制和调试正文原始字节,标准压缩流的校验和及截断错误仍失败。补充原始字节对照、跨源重定向、观察者隔离及解码回归。 验证:75 项 native-request 与 32 项 Network 聚焦测试通过,相关类型检查、构建、ESLint 和 diff 检查通过。独立 Electron 验证 HTTP/HTTPS、两个 Network 前端及 Raw 展示,Node 24.18 验证 BOM、raw DEFLATE 和二进制返回。按本次验证约束沿用聚焦结果,未重复全仓 gate 或完整 E2E 套件。
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0a593dcbb8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| throw new Error("unsupported request protocol"); | ||
| } | ||
| const nodeModule = parsed.protocol === "https:" ? https : http; | ||
| req = nodeModule.request(url, { method, headers: nodeHeaders }, (res) => { |
There was a problem hiding this comment.
Preserve the caller session in native requests
When a mini-app uses cookies with same-origin dev-server endpoints, this direct Node request runs outside ap.simulatorWc.session. The existing per-project Electron partition is the authoritative cookie store, but this transport neither sends its cookies nor commits response/redirect Set-Cookie headers, so a login followed by another wx.request becomes unauthenticated and the cookie state no longer survives relaunches. Pass the caller's session into the transport and synchronize its cookie jar, or use a session-aware request implementation.
AGENTS.md reference: AGENTS.md:L12-L14
Useful? React with 👍 / 👎.
| if (NATIVE_HTTP_API_NAMES.has(name)) { | ||
| await handleNativeHttpApi(state, ap, name, params) | ||
| return |
There was a problem hiding this comment.
Honor registered request overrides before native fallback
When a host customizes networking through the public registerSimulatorApi('request', handler) API, this branch handles the call before the later ctx.simulatorApis.has(name) branch, so the registered handler is never invoked. This regresses the last-write-wins contract documented in packages/devtools/src/main/app/app.ts:387-389 and the previous simulator boot order, where custom APIs were registered after the built-in request handler. Check the host registry before selecting the native fallback.
AGENTS.md reference: AGENTS.md:L23-L24
Useful? React with 👍 / 👎.
| const userFacing = event.type === "redirect" ? previous!.userFacing : isUserFacingRequest( | ||
| event.url, | ||
| this.options.internalOrigins?.(), | ||
| ); |
There was a problem hiding this comment.
Reclassify redirected URLs before routing their trace
When an internal-origin initial URL redirects to an external business endpoint, or vice versa, retaining previous.userFacing routes the entire remaining chain according to the first hop. A redirect carries its own URL and produces another Network.requestWillBeSent; the existing debugger-backed path recomputes classification for every such event in network-forward/index.ts:1337-1350. With this cached verdict, the right-side Network panel can hide the final business request or expose an internal redirect, so classify event.url again for redirect events.
AGENTS.md reference: AGENTS.md:L23-L24
Useful? React with 👍 / 👎.
| dispatchToGlobal(message.method, message.params); | ||
| if (message.userFacing) enqueueNative(message.method, message.params); |
There was a problem hiding this comment.
Retain the console fallback for native HTTP traces
When the embedded DevTools host is absent or its frontend probe degrades, enqueueNative drops these synthesized events, but this native HTTP path never records terminal requests in probeConsoleBuffer or calls the existing maybeFallback/forwardToConsole path. Consequently migrated wx.request traffic becomes completely invisible instead of producing the legacy [网络] console line promised by this module and previously emitted by wireNetworkCapture. Track native request completion and feed the same fallback state machine.
AGENTS.md reference: AGENTS.md:L23-L24
Useful? React with 👍 / 👎.
| await invokeSimulatorApiAndCallback(ap, name, params, () => | ||
| state.nativeRequest.request(ap.appSessionId, requestId, options), |
There was a problem hiding this comment.
Suppress callbacks after the request owner is disposed
When an app session is closed or soft-reloaded while a native request is pending, disposeOwner() aborts and resolves the request, after which this awaited helper resumes and sends fail/complete through the captured ap without checking that it is still registered. The service WebContents can still be closing or resetting in the prewarm pool, so an old session's callback can run during teardown or reach a reused owner; forwarded simulator calls avoid this by deleting their pending entries in drainPendingApiCallsForSession. Revalidate the app-session generation before emitting callbacks.
AGENTS.md reference: AGENTS.md:L13-L14
Useful? React with 👍 / 👎.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
复用请求头和正文编码,拆分 CDP 请求事件合成与 HTTP 重定向处理,保留现有请求及终态行为。更新 watchdog 测试中的过时注释并保留全部断言。 验证:148 个聚焦测试通过;electron-runtime build:code、devtools build:main 及修改文件 ESLint 通过;Pawl file-length、cognitive-complexity、code-duplication 三项检查通过。
wx.request 原先经过 Chromium fetch,会触发小程序请求不需要的 CORS/OPTIONS 检查;迁移到主进程后,Chromium 原生 Network 又无法直接看到这些请求。本次统一使用 Node http/https 执行 wx.request,并把原生请求 trace 接入开发者工具 Network,补全原始请求头和响应正文展示。
变更
验证
以下为本分支开发过程中已完成的验证,本次创建 PR 未重复运行全仓测试:
已知限制与配套变更