Open
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
Open
yanPengAdd on 9 hours ago
yanPeng
yanPeng
9 hours 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
Merge blocked
cpf-managercpf-managerCollaborators
9 hours ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
9 hours ago Comments:

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

likedislike
openharmony_ciopenharmony_ciCollaborators
9 hours ago Added label: dco检查成功
cpf-manager
cpf-managerCollaborators8 hours agoStart code review1
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-manager8 hours 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
cpf-manager
cpf-managerCollaborators8 hours agoStart code review1
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-manager8 hours 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
cpf-managercpf-managerCollaborators
8 hours ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
8 hours ago Added label: AI已检视
yanPengyanPeng
8 hours 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
8 hours ago Removed label: AI已检视
cpf-managercpf-managerCollaborators
8 hours ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
8 hours ago Comments:

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

likedislike
cpf-managercpf-managerCollaborators
8 hours ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
8 hours ago Added label: AI已检视
yanPengyanPeng
2 hours ago Modify description of pull request