Skip to content

fix(h5): 处理上传前 Blob 转换的错误与超时 - #19588

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/h5-blob-conversion-errors
Nov 2, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/h5-blob-conversion-errors

Conversation

@Jarvis636431

@Jarvis636431 Jarvis636431 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

H5 uploadFile 发送上传 POST 前,会先通过 GET XMLHttpRequest 读取 Blob。转换函数只处理 load,读取错误或中止后 Promise 不结算,uploadFile 的 fail/complete 和 Promise 因而一直等待;上传超时计时器此时尚未启动。

为转换请求补齐 error/abort/timeout 结算与有限超时,所有终态和同步初始化/发送异常统一释放处理器。保留非 200 HTTP 状态信息,上传入口向转换请求传递现有 timeout 配置(未提供时默认 60000ms)。转换和上传分别使用该超时值,不新增跨两个阶段的总超时策略,也不改变 uploadTask.abort 的既有行为。

Closes #19587

这个 PR 是什么类型?

  • 错误修复 (Bugfix)

这个 PR 涉及以下平台:

  • Web 端(H5)

验证

  • 新增 16 个回归用例,原实现 15 个失败、1 个通过;修复后全部通过。
  • 覆盖转换 error/abort/timeout、默认/配置超时、成功返回 Blob、403/404/500 状态、open/send 同步异常、终态清理与晚到事件。
  • 公开 uploadFile 路径使用真实转换函数,验证失败 Promise、fail/complete 只通知一次、失败前不发送 POST、abort 后不重复通知,以及正常转换后成功上传。
  • Blob 与既有上传测试:2 个套件、26 个用例通过。
  • pnpm --filter @tarojs/taro-h5 run test:ci --watchman=false:30 个套件、259 个用例通过,退出码 0。
  • pnpm --filter @tarojs/taro-h5 run build:通过,退出码 0;存在未修改的 previewImage/Swiper 类型及 Sass 弃用告警。
  • 全工作区 pnpm build:通过,退出码 0;存在未修改文件的 TypeScript、组件及循环依赖等构建告警。
  • 修改源码及新测试 ESLint(--no-ignore)、差异空白检查(保留原文件 CRLF)通过。

Node.js 22.18.0 / pnpm 10.0.0。分支独立基于上游 main 0493b5251da,不包含滚动和 CallbackManager 修复。测试使用 JSDOM 与受控 XHR 事件,未进行真实网络、真实浏览器或真机验证。不宣称解决 vconsole 代理兼容性 #18187。未修改依赖及锁文件。

Summary by CodeRabbit

  • 问题修复
    • 文件上传前将对象 URL 转换为 Blob 时,现会遵循网络请求超时设置;转换超时、失败或中止时会正确结束处理,避免重复触发回调或继续发送上传请求。
    • 转换成功后,上传流程会继续使用转换后的 Blob,并正常返回上传结果。

@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 54ea2a6b-0639-414e-b504-2817988a9811
Commits

Reviewing files that changed from the base of the PR and between ed99dcc and ce07e96.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d02156b1-fba6-4cbb-984e-7dce37b4ce66
Commits

Reviewing files that changed from the base of the PR and between 0493b52 and 733917a.

Files selected for processing: 3
  • packages/taro-h5/__tests__/network/blob-conversion.test.ts
  • packages/taro-h5/src/api/network/upload.ts
  • packages/taro-h5/src/api/network/utils.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Walkthrough

H5 uploadFile 现在将超时配置传给上传前的 Blob URL 转换请求。转换请求会处理成功、HTTP 错误、网络错误、中止、超时及同步异常,并在结算后清理事件处理器。

Changes

H5 Blob 转换与上传

Layer / File(s) Summary
转换请求结算与测试
packages/taro-h5/src/api/network/utils.ts, packages/taro-h5/__tests__/network/blob-conversion.test.ts
convertObjectUrlToBlob 新增默认超时,并统一处理请求结算。成功状态仍为 200;其他 HTTP 状态保留状态码。新增对网络错误、中止、超时和同步异常的处理,并测试处理器清理及重复终止事件。
上传转换超时传递与生命周期测试
packages/taro-h5/src/api/network/upload.ts, packages/taro-h5/__tests__/network/blob-conversion.test.ts
uploadFile 将 `timeout

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • NervJS/taro#19537:同样涉及 H5 uploadFile 上传前的 Blob 转换生命周期,并处理取消后的迟到结果和清理。

Suggested reviewers: zakarycode, single-dancer

Merge Risk: Minimal · previous review 73391

The change addresses stalled Blob conversion requests; no merge-blocking issue is established.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix(h5): 处理上传前 Blob 转换的错误与超时”与变更内容相符。变更为 convertObjectUrlToBlob 增加 timeout 参数,并处理 error、abort、timeout 事件及同步异常,且 createUploadTask 将 timeout 传给转换函数。标题指出了主要变更,属于完全相关。标题使用 conventional commit 格式,简洁明确。
Linked Issues check Passed [#19587] 相关需求及代码证据如下。

1. 转换失败、中止、超时后结算 Promise 并传递失败:packages/taro-h5/src/api/network/utils.ts 中,convertObjectUrlToBlob 为 onerror、onabort、ontimeout 绑定 settle(false, ...)。upload.ts 的 .catch …
Out of Scope Changes check Passed 变更范围包括三部分:packages/taro-h5/src/api/network/utils.ts(convertObjectUrlToBlob 的错误、中止、超时、终态清理及默认超时)、packages/taro-h5/src/api/network/upload.ts(向转换请求传递 timeout || NETWORK_TIMEOUT,仅一行)、以及新增的 `packag…
Full details: Linked Issues check

Explanation

[#19587] 相关需求及代码证据如下。

  1. 转换失败、中止、超时后结算 Promise 并传递失败:packages/taro-h5/src/api/network/utils.ts 中,convertObjectUrlToBlob 为 onerror、onabort、ontimeout 绑定 settle(false, ...)。upload.ts 的 .catch 调用 fail(...),从而触发 uploadFile 的 fail、complete 及 Promise reject。

  2. 转换阶段有限超时:新增参数 timeout = NETWORK_TIMEOUT,并设置 xhr.timeout = timeout。createUploadTask 传入 timeout || NETWORK_TIMEOUT。

  3. 终态后释放处理器:settle 只执行一次,并将 onload、onerror、onabort、ontimeout 置为 null。晚到事件不再调用处理器。

  4. 保留 HTTP 非 200 状态:非 200 时以 { status } 拒绝。upload.ts 的 catch 将该 status 转为错误文本。

  5. 同步异常:xhr.open 与 xhr.send 位于 try/catch 中,异常经 settle 拒绝。

  6. 自动化测试:PR 描述及变更摘要列出 blob-conversion.test.ts 中 16 个新增用例。本评估未逐行读取测试文件,结论基于源码 diff 与摘要。

说明:上传超时计时器仍在 send() 时启动。转换与上传各自使用同一超时值。issue 要求的是转换阶段有有限超时,该要求已满足。跨阶段总超时不属于 issue 的明确期望结果。uploadTask.abort 在转换阶段不会中止转换 XHR,PR 明确保留该既有行为,issue 未要求改变。

Full details: Out of Scope Changes check

Explanation

变更范围包括三部分:packages/taro-h5/src/api/network/utils.ts(convertObjectUrlToBlob 的错误、中止、超时、终态清理及默认超时)、packages/taro-h5/src/api/network/upload.ts(向转换请求传递 timeout || NETWORK_TIMEOUT,仅一行)、以及新增的 packages/taro-h5/__tests__/network/blob-conversion.test.ts。三部分均直接服务于 #19587 的转换阶段目标。未发现与 issue 无关的改动。PR 描述称未修改依赖及锁文件,未处理 vconsole 代理兼容性(#18187),该说明与变更摘要一致。变更摘要中未出现 #18187 相关文件。

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

我是小兔,蹦到转换请求旁,
看见超时钟声轻轻响。
成功的 Blob 跳进上传袋,
错误与中止也有了交代。
处理器收好,计时器回家,
我抱着胡萝卜,安心啦。

@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.92%. Comparing base (cead17f) to head (ce07e96).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19588      +/-   ##
==========================================
+ Coverage   57.83%   57.92%   +0.09%     
==========================================
  Files         470      470              
  Lines       25987    26001      +14     
  Branches     6490     6500      +10     
==========================================
+ Hits        15029    15061      +32     
+ Misses       9066     9045      -21     
- Partials     1892     1895       +3     
Flag Coverage Δ
taro-cli 59.69% <ø> (ø)
taro-runtime 66.00% <ø> (ø)
taro-web 67.94% <100.00%> (+0.56%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-h5/src/api/network/upload.ts 93.42% <100.00%> (ø)
packages/taro-h5/src/api/network/utils.ts 96.77% <100.00%> (+67.36%) ⬆️

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer Single-Dancer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

审查结论:可以合并,未发现需要阻止合入的问题。已检查 Blob 转换到 uploadFile 的真实调用链、错误传播、超时配置、终态清理及重复回调防护,改动范围与 #19587 一致。

独立验证(固定 head 733917a):

  • 新增 16 个回归用例在 base 0493b52 上 15 失败、1 通过;修复后全部通过。
  • Blob 转换及既有上传测试:2 套件、26 用例全部通过。
  • H5 整包 Jest 测试:30 套件、259 用例全部通过。

验证复用了本机已安装依赖,在隔离源码快照中执行。未独立运行构建或真实浏览器网络验证;转换和 POST 分别使用 timeout,不宣称提供跨阶段总超时,也不改变既有 uploadTask.abort 行为。

@Single-Dancer Single-Dancer added this to the 4.3.2 milestone Nov 2, 2026
@Single-Dancer
Single-Dancer force-pushed the fix/h5-blob-conversion-errors branch from 733917a to ed99dcc Compare November 2, 2026 06:11
@Single-Dancer
Single-Dancer force-pushed the fix/h5-blob-conversion-errors branch from ed99dcc to ce07e96 Compare November 2, 2026 07:16
@Single-Dancer
Single-Dancer merged commit fa80561 into NervJS:main Nov 2, 2026
25 checks passed
@Jarvis636431
Jarvis636431 deleted the fix/h5-blob-conversion-errors branch November 3, 2026 03:19
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.

H5 uploadFile 在 Blob 转换失败时一直等待,timeout 无法覆盖转换阶段

2 participants