Merged
fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.1.2-beta.2 #31
fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.1.2-beta.2 #31
Merged
yanPengAdd on 2 days ago
yanPeng
yanPeng
2 days ago

变更类型

请在对应类型(请勿多选)前打 √,仅可选择一项:

变更内容

修复问题点

1.网络图旋转不稳定
远程图片每次旋转都重新 http.request,未固定 GET / ARRAY_BUFFER / HTTP1.1;含特殊字符的 URL 可能请求失败。

2.同 URL 重复下载
对同一网络地址多次 rotateImage 会反复拉取整图,耗时长、易失败。

3.PixelMap 未释放
createPixelMap 后主流程未 release(),连续旋转易抬高内存。

4.边界与资源处理不严谨
本地文件用 READ_WRITE 打开,只读 URI 可能失败
用 includes('http') 判断远程,路径含 http 的本地文件会被误判
本地解码失败只打日志返回 undefined,错误信息降级
每次 rotateImage 都清理临时目录,可能误删刚生成文件
入口缺少回调 / 角度有限性校验;模块销毁时无缓存与临时文件清理
5.工程配套不足
缺少 Jest;example 仅有鸿蒙 testerino,未覆盖上游 demo。

解决方案

1.增加HTTP 稳定性,normalizeRemoteRequestUrl;请求指定 GET + ARRAY_BUFFER + HTTP1.1
2.同 URL 复用,静态 LRU(上限 4)缓存远程 ArrayBuffer
3.内存,旋转/打包后 finally 中 pixelMap.release()
4.边界处理:READ_ONLY;^https?😕/ 判定远程;解码失败抛 Cannot decode bitmap;入口校验回调与 Number.isFinite(angle)
5.生命周期:去掉每次调用时的 cleanDirectory;增加 onDestroy 清缓存并清理临时文件;构造清理加 try-catch
6.工程增加 jest/;example 双入口(上游 demo + 鸿蒙 demo);

改动前后代码块

(1)网络下载:规范化 URL + LRU 缓存

// 改动前:每次直连请求,无缓存、无请求选项
const data: http.HttpResponse = await httpRequest.request(src);

// 改动后:规范化 URL + 指定请求参数 + LRU 复用
let imageData = RNImageRotateTurboModule.remoteImageBodyCache.get(src);
if (imageData === undefined) {
  const requestUrl = RNImageRotateTurboModule.normalizeRemoteRequestUrl(src);
  const data = await httpRequest.request(requestUrl, {
    method: http.RequestMethod.GET,
    expectDataType: http.HttpDataType.ARRAY_BUFFER,
    usingProtocol: http.HttpProtocol.HTTP1_1,
  });
  // ... 成功后 cacheRemoteImageBody(src, imageData)
}

(2)PixelMap 统一释放

// 改动前:rotate → pack,无 release
imagePixelMap.rotate(...).then(() => {
  rotateTask.doInBackgroundGuarded(imagePixelMap);
});

// 改动后:try/finally 全路径释放
try {
  await imagePixelMap.rotate(rotateTask.mAngle);
  await rotateTask.doInBackgroundGuarded(imagePixelMap);
} finally {
  await imagePixelMap.release();
}

(3)远程判定 / 本地打开 / 错误上抛

// 改动前
if (muri.includes('http')) { ... }
file = fs.openSync(src, fs.OpenMode.READ_WRITE);
// catch 仅 console,返回 undefined

// 改动后
if (RNImageRotateTurboModule.isRemoteUri(muri)) { ... } // /^https?:\/\//i
file = fs.openSync(src, fs.OpenMode.READ_ONLY);
throw new Error('Cannot decode bitmap: ' + src);

影响评估

  • 是否影响公开接口功能
    • 不影响。 仍为 ImageRotate.rotateImage(uri, angle, success, failure);成功仍回 file://…/ReactNative_rotated_image_*;失败仍走 failure / 空 URI 仍抛 Please specify a URI。
  • 是否产生兼容问题
    • 对正常业务调用无破坏性兼容问题。 行为对齐/增强:只读本地 URI 更稳、远程判定更准、同 URL 更快、内存更稳。可能感知差异:① 非法回调 / 非有限角度会显式报错(原先可能二次抛错);② 本地解码失败信息更接近 Cannot decode bitmap;③ 临时文件改为构造/销毁时清理,而非每次调用清理(更合理,一般不影响业务)。
  • 建议验证test
    • 网络图连续旋转同 URL;本地只读路径;非法 URI / 空 URI;±90° / 45°;观察内存是否平稳。

其他

  • 版本更新2.1.2-beta.1->2.1.2-beta.2
  • 更新readme

测试步骤

cd example & npm i & npm run dev
运行工程

selected-area.png

测试情况

合入前自检

likedislike
Pull Request has been successfully merged by openharmony_ci.
(Thank you for yanPeng's contribution.)
cpf-managercpf-managerCollaborators
2 days ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
2 days ago Comments:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
openharmony_ciopenharmony_ciCollaborators
2 days ago Added label: dco检查成功
cpf-manager
cpf-managerCollaborators2 days agoStart code review2
example/src/harmonyTestDemo.tsx
@@ -0,0 +17,4 @@
17+ 
18+import ImageRotate from "react-native-image-rotate";
19+ 
20+const SOURCE_IMAGE = 'https://filesamples.com/samples/image/jpg/sample_640×426.jpg';
cpf-manager
cpf-manager2 days agoComments:

【AI-Review】【严重】【基础代码问题】【字符串问题】
● 问题: example/src/harmonyTestDemo.tsx 第 20 行的 SOURCE_IMAGE 常量 URL 为 'https://filesamples.com/samples/image/jpg/sample_640×426.jpg',其中 640 与 426 之间的分隔符是 Unicode 乘号 ×(U+00D7,UTF-8 字节 c3 97),而非 ASCII 字母 x(U+0078)。该 URL 被 PR 新增的 normalizeRemoteRequestUrl 处理后,encodeURI 会将 × 编码为 %C3%97,最终请求 URL 变为 https://filesamples.com/samples/image/jpg/sample_640%C3%97426.jpg。filesamples.com 上的实际文件名使用 ASCII x(即 sample_640x426.jpg),编码后的 URL 将得到 404 响应。

● 影响: 使用 SOURCE_IMAGE 的三个设备测试用例(rotateImage_cw、rotateImage_ccw、rotateImage_angle_45)在设备上运行时,HTTP 请求会返回 404,触发 failure 回调,setState 将 uri 设为 '__failure__',assert 中的 expect(uri).to.not.contain('__failure__') 断言失败。PR 声明"已通过本地完整测试",但"已在目标环境验证功能"未勾选,说明设备端测试未实际执行,此问题未被捕获,核心旋转功能的设备端验证实际上无效。

● 建议: 将 URL 中的 × 改为 ASCII x:'https://filesamples.com/samples/image/jpg/sample_640x426.jpg'。修改后在设备上运行 rotateImage_cw / rotateImage_ccw / rotateImage_angle_45 三个用例确认通过。

likedislike
yanPeng
yanPeng
1 day ago Comments:

非问题,const SOURCE_IMAGE = 'https://filesamples.com/samples/image/jpg/sample_640×426.jpg'; 这是正常访问的https网址,测试用图不替换

likedislike
cpf-manager
cpf-managerCollaborators2 days agoStart code review2
harmony/imageRotate/src/main/ets/RNImageRotateTurboModule.ts
@@ -68,1 +112,3 @@
68- });
112+ try {
113+ await imagePixelMap.rotate(rotateTask.mAngle);
114+ await rotateTask.doInBackgroundGuarded(imagePixelMap);
cpf-manager
cpf-manager2 days agoComments:

【AI-Review】【一般】【基础代码问题】【代码逻辑错误】
● 问题: RNImageRotateTurboModule.ts 第 114 行将 await rotateTask.doInBackgroundGuarded(imagePixelMap) 放入与 imagePixelMap.rotate() 相同的 try/catch 中。doInBackgroundGuarded(第 293–316 行)内部有自己的 try/catch/finally:try 块成功时调用 this.mSuccess(src)(第 304 行),随后 finally 块执行 imagePackerApi.release()(第 310 行)和 fs.closeSync(tempFile.fd)(第 313 行)。如果 finally 块中任一清理操作抛出异常(如文件系统异常导致 closeSync 失败),该异常会传播到第 115–117 行的外层 catch,调用 rotateTask.mError()。此时 mSuccess 和 mError 都被调用,违反了 rotateImage 的回调契约(成功与失败回调互斥)。同理,若 doInBackgroundGuarded 的 try 块失败、catch 调用了 mError,随后 finally 又抛出异常,外层 catch 会再次调用 mError,导致 failure 回调被调用两次。旧代码中 doInBackgroundGuarded 未被 await,其 finally 抛出的异常是未捕获的 Promise rejection,不会触发外层 mError。

● 影响: 在 imagePackerApi.release() 或 fs.closeSync() 抛出异常的边界条件下,调用方会同时收到 success 和 failure 回调,或收到两次 failure 回调,可能导致调用方状态不一致(如先设置旋转结果 URI 又被错误覆盖)。

● 建议: 将 doInBackgroundGuarded 的 finally 块用 try/catch 包裹,防止清理操作的异常向上传播;或在 rotateImage 中将 doInBackgroundGuarded 的调用与 imagePixelMap.rotate() 分离到不同的 try/catch 中,对 doInBackgroundGuarded 的异常仅记录日志、不再调用 mError(因为 doInBackgroundGuarded 已自行处理了业务错误)。

likedislike
yanPeng
yanPeng
1 day ago Comments:

已修改,doInBackgroundGuarded 的finally中已添加异常捕获处理

likedislike
cpf-managercpf-managerCollaborators
2 days ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
2 days ago Added label: AI已检视
yanPengyanPeng
2 days ago Force-pushed 1 commits:4ea2e78d-fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.1.2-beta.2
cpf-managercpf-managerCollaborators
2 days ago Removed label: AI已检视
cpf-managercpf-managerCollaborators
2 days ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
2 days ago Comments:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
cpf-managercpf-managerCollaborators
2 days ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
2 days ago Added label: AI已检视
yanPengyanPeng
2 days ago Modify description of pull request
yanPengyanPeng
1 day ago Force-pushed 1 commits:ddf7e96d-fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.1.2-beta.2
cpf-managercpf-managerCollaborators
1 day ago Removed label: AI已检视
cpf-managercpf-managerCollaborators
1 day ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
1 day ago Comments:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
yanPengyanPeng
1 day ago Associated issue: 72Branch Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example
yanPeng
yanPeng
1 day ago Comments:

start build

likedislike
openharmony_ci
openharmony_ciCollaborators
1 day ago Comments:

首次触发
门禁构建开始,包含静态检查、代码编译和测试【rntpc_br_rnoh0.72测试, rntpc_br_rnoh0.72编译, rntpc_br_rnoh0.72-jest编译】,预计在60分钟内完成,门禁结果会同步发送到注册邮箱。您可以通过如下链接跟踪门禁进展:http://dcp.openharmony.cn/workbench/cicd/detail/6b10ca1f64650f998b189cd6/runlist

likedislike
openharmony_ciopenharmony_ciCollaborators
1 day ago Added label: 静态检查失败
openharmony_ci
openharmony_ciCollaborators
1 day ago Comments:

代码门禁未通过
您可以通过如下链接查看门禁报告:http://dcp.openharmony.cn/workbench/cicd/detail/6b10ca1f64650f998b189cd6/runlist

静态检查:

# check type result report
1 codeCheck noPass >>>

编译测试:
# Device build result package
1 rntpc_br_rnoh0.72 pending NA
2 rntpc_br_rnoh0.72-jest pending NA

likedislike
cpf-manager
cpf-managerCollaborators1 day agoStart code review2
README.md
@@ -4,13 +4,15 @@
44 <h1 align="center"> <code>react-native-image-rotate</code> </h1>
55</p>
66 
7+[English](./README_en.md) | **简体中文**
cpf-manager
cpf-manager1 day agoComments:

【AI-Review】【一般】【编码规范】【其他编码规范】
● 问题: README.md 第 7 行新增语言切换器 [English](./README_en.md) | **简体中文**,将 简体中文 加粗标记为当前默认页面。根据通用检视规则集规则 5(文档语言与默认入口),根目录 README.md 和文档默认首页/默认语言必须为英文,不得将中文设为默认入口。本次 PR 新增了该语言切换器入口,使中文成为显式默认入口。

● 影响: 仓库根目录 README.md 以中文为默认版本,不满足规则 5 要求的"根目录 README.md 必须为英文"。国际开发者和 npm 包页面默认访问 README.md 时首先看到中文内容。

● 建议: 将 README.md 改为英文版本(与当前 README_en.md 内容交换),在 README.md 中放置英文内容并以 [简体中文](./README_zh.md) | **English** 形式提供中文切换入口;或至少调整切换器标记方式使英文成为默认标记。修改后确认两种语言版本的关键信息保持一致。

likedislike
yanPeng
yanPeng
1 day ago Comments:

非问题,模板是中文入口

likedislike
cpf-managercpf-managerCollaborators
1 day ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
1 day ago Added label: AI已检视
胡枭
胡枭
1 day ago Comments:

已检视,无其他问题

likedislike
yanPeng
yanPeng
1 day ago Comments:

start build

likedislike
胡枭
胡枭
1 day ago Comments:

已检视,无其他问题

likedislike
openharmony_ciopenharmony_ciCollaborators
1 day ago Removed label: 静态检查失败
openharmony_ci
openharmony_ciCollaborators
1 day ago Comments:

部分构建失败,仅触发失败构建
门禁构建开始,包含静态检查、代码编译和测试【rntpc_br_rnoh0.72-jest编译, rntpc_br_rnoh0.72编译, rntpc_br_rnoh0.72测试】,预计在60分钟内完成,门禁结果会同步发送到注册邮箱。您可以通过如下链接跟踪门禁进展:http://dcp.openharmony.cn/workbench/cicd/detail/6b10cd4364650f998b19eb54/runlist

likedislike
openharmony_ciopenharmony_ciCollaborators
1 day ago Added label: 编译成功
openharmony_ciopenharmony_ciCollaborators
1 day ago Added label: 静态检查成功
openharmony_ciopenharmony_ciCollaborators
1 day ago Test passed
openharmony_ci
openharmony_ciCollaborators
1 day ago Comments:

代码门禁通过
您可以通过如下链接查看门禁报告:http://dcp.openharmony.cn/workbench/cicd/detail/6b10cd4364650f998b19eb54/runlist

静态检查:

# check type result report
1 codeCheck pass >>>

编译测试:
# Device build result test result package
1 rntpc_br_rnoh0.72 success success(IGNORE) >>>
2 rntpc_br_rnoh0.72-jest success NA >>>

likedislike
dingchengjiedingchengjieCollaborators
1 day ago Set dingchengjie as approver
dingchengjiedingchengjieCollaborators
1 day ago Resolved final issue
dingchengjiedingchengjieCollaborators
1 day ago Approval passed
cpf-managercpf-managerCollaborators
1 day ago Added label: AI检视复核中
cpf-managercpf-managerCollaborators
1 day ago Reset test status
cpf-manager
cpf-managerCollaborators
1 day ago Comments:

【AI-Review复核报告】

结论: ❌ 不通过

复核基线: ddf7e96d | 意见总数: 3 | 有效处置: 1 | 无效处置: 2

级别分布: 致命 0 / 严重 1 / 一般 2 / 建议 0
总结: 共 2 条意见处置无效——2条“非问题”否认缺少证据,请开发者补充处置。

待闭环清单:

  1. 检视意见:example/src/harmonyTestDemo.tsx:20 —— 【严重】 SOURCE_IMAGE URL 中 640 与 426 之间的分隔符是 Unicode 乘号 ×(U+00D7)而非 ASCII x,normalizeRem…
    问题:无证据否认(敷衍行为第2类):开发者仅口头声称'这是正常访问的https网址',未回应 AI 指出的 encodeURI 将 ×(U+00D7) 编码为 %C3%97 的技术链路,无 HTTP 测试结果/curl 证据/服务器文档佐证经编码后仍返回 200;同时命中认知偏差第5类'想当然安全假设',未经 normalizeRemoteRequestUrl 实际路径验证即假设 URL 可访问。headSha 验证 harmonyTestDemo.tsx:20 仍为 sample_640×426.jpg(分隔符 codepoint 0xd7),问题现象未消失——评论 id 206434283(yp9522 回复);headSha ddf7e96 harmonyTestDemo.tsx:20 分隔符仍为 U+00D7;回复未涉及 encodeURI 编码链路、无 URL 可访问性测试证据

  2. 检视意见:README.md:7 —— 【一般】 README.md 第 7 行新增语言切换器 English | 简体中文,将 简体中文 加粗标记为当前默认…
    问题:无证据否认(敷衍行为第2类):开发者仅口头声称'模板是中文入口',未引用模板来源 URL、未引用模板文档条文、未回应 AI 引用的通用检视规则集规则5;无可验证证据支撑否认。headSha 验证 README.md:7 仍为 English | 简体中文,中文仍加粗标记为默认入口,问题现象未消失——评论 id 206440373(yp9522 回复);headSha ddf7e96 README.md:7 仍为 简体中文 加粗默认入口;回复未提供模板出处与文档佐证


请开发者完成处置后由审查人重新审查通过,或由审查人发布 /ai-review pass 人工放行(也可发布 /ai-review fail 人工否决,命令后可附原因说明)。

likedislike
cpf-managercpf-managerCollaborators
1 day ago Removed label: AI检视复核中
cpf-managercpf-managerCollaborators
1 day ago Added label: AI检视复核不通过
dingchengjie
dingchengjieCollaborators
1 day ago Comments:

/ai-review pass

likedislike
cpf-managercpf-managerCollaborators
1 day ago Removed label: AI检视复核不通过
cpf-managercpf-managerCollaborators
1 day ago Added label: 审查人复核通过
cpf-managercpf-managerCollaborators
1 day ago Test passed
openharmony_ciopenharmony_ciCollaborators
1 day ago Closed associated issue
openharmony_ciopenharmony_ciCollaborators
1 day ago Merged pull request, merge node SHA: 235fad12ad82f76b458068b108ea6ccb9f09b87b