Skip to content

fix(devtools): 将 wx.request 迁移到主进程并完善 Network 与项目上下文 - #202

Merged
yyninghuang merged 4 commits into
mainfrom
fix/devtools-request
Sep 7, 2026
Merged

fix(devtools): 将 wx.request 迁移到主进程并完善 Network 与项目上下文#202
yyninghuang merged 4 commits into
mainfrom
fix/devtools-request

Conversation

@yyninghuang

Copy link
Copy Markdown
Collaborator

wx.request 原先经过 Chromium fetch,会触发小程序请求不需要的 CORS/OPTIONS 检查;迁移到主进程后,Chromium 原生 Network 又无法直接看到这些请求。本次统一使用 Node http/https 执行 wx.request,并把原生请求 trace 接入开发者工具 Network,补全原始请求头和响应正文展示。

变更

  • service 与 preload 入口共享主进程请求服务,保留相对 URL、项目 Referer、超时、取消、窗口销毁清理、重定向及压缩响应处理。
  • 原生 HTTP trace 经 bridge 和窗口路由合成 CDP Network 事件,支持嵌入式与全局面板;Response/Payload 接入有界缓存,处理重定向缓存淘汰和请求 ID 隔离。
  • 响应及每次重定向携带 Node 实际序列化的请求头,支持 Headers 和 Raw 展示。读取原始头失败时安全降级;尚未收到响应的请求继续显示预配标头。
  • 自定义 API 增加每次调用的项目上下文,支持多项目宿主正确读取 appId;应用级 API 注册保持共享。
  • 修复 UTF-8 BOM 文本/JSON 和 raw DEFLATE 响应兼容,保留 ArrayBuffer 字节与正文缓存语义,损坏压缩流仍返回失败。

验证

以下为本分支开发过程中已完成的验证,本次创建 PR 未重复运行全仓测试:

  • 最后一次原生请求测试:8 个文件、75 项通过;Network 聚焦测试:5 个文件、32 项通过。
  • 项目上下文改动的 52 项相关测试通过;受影响包类型检查、主进程构建及相关 lint 通过。
  • 隔离 Electron 冒烟验证 HTTP GET/POST、HTTPS、302 每一跳原始请求头与服务端实际收到内容一致;嵌入式/全局 Network 的 Headers、Raw、Response/Payload 可用。
  • BOM、raw DEFLATE 与二进制字节保留的构建产物冒烟通过;git diff --check 通过。
  • 初始迁移提交曾运行完整 gate,lint/typecheck/test 通过,但 Pawl 指标超阈值(file-length 55→57、cognitive 0→2、duplication 275→303),未修改基线。后续按受影响范围验证,不能据此视为最终全仓 gate/CI 已通过。

已知限制与配套变更

  • 原生请求与 Chromium 请求使用不同时间基准,混合显示时瀑布图时间轴仍可能偏移,尚未纳入本次修复。
  • 原始请求头依赖 Node 的只读序列化快照;获取不到快照时保留预配标头,不伪造原始数据。
  • qdmp 消费端 MR:frontend/qdmp !155。本地通过 link 联调,发布 qdmp 前需使用包含本 PR 的 dimina-kit 版本。

将 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 套件。

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment on lines +2793 to +2795
if (NATIVE_HTTP_API_NAMES.has(name)) {
await handleNativeHttpApi(state, ap, name, params)
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment on lines +93 to +96
const userFacing = event.type === "redirect" ? previous!.userFacing : isUserFacingRequest(
event.url,
this.options.internalOrigins?.(),
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +1810 to +1811
dispatchToGlobal(message.method, message.params);
if (message.userFacing) enqueueNative(message.method, message.params);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +2748 to +2749
await invokeSimulatorApiAndCallback(ap, name, params, () =>
state.nativeRequest.request(ap.appSessionId, requestId, options),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-07T07:56:42.140959Z 0a593dc PR opened
ℹ️ 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" or "@codex security review".

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 三项检查通过。
@yyninghuang
yyninghuang merged commit 34106da into main Sep 7, 2026
2 checks passed
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