feat(native): 图片取字节统一走 Rust(P1 全部 + P2 顺序/320 原生)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 47s

This commit is contained in:
qiqi committed 2026-12-04 10:54:41 +08:00
1 parent 330bd455e4
commit a7430bb5f2
10 files changed
+129 -29

No files matched your search

+10 -4
View File
@@ -59,10 +59,16 @@
// ① 优先路径:字节 → bitmap → 缩放 → WebP Blob → objectURL
const runBitmap = async (key, v) => {
try { window.IWBPerf && window.IWBPerf.tag('thumb') } catch (e) { /* ignore */ }
if (typeof fetch !== 'function' || typeof createImageBitmap !== 'function') return false
const resp = await fetch(v)
if (!resp || !resp.ok) return false
const blob = await resp.blob()
if (typeof createImageBitmap !== 'function') return false
// 2026-12-04(审计 P1):优先宿主注入的 Rust 取字节(远程图不再受 CORS 限制);失败再回退 fetch
let blob = (typeof o.fetchBlob === 'function') ? await o.fetchBlob(v) : null
if (!blob || !blob.size) {
if (typeof fetch !== 'function') return false
const resp = await fetch(v)
if (!resp || !resp.ok) return false
blob = await resp.blob()
}
if (!blob || !blob.size) return false
// v3.26.121(参考 Chromium 的 ImageDecodeCache 与 tldraw 的取图方式):
// 先解码再缩放 = 整个原图(2016×2688 ≈ 21MB)先落地一次;改成在解码阶段就按目标宽度降采样,
// 峰值内存与 CPU 都随 max 走,不随原图走。只给 resizeWidth(等比),纵向超出的再补一次 canvas 缩放。
+14 -6
View File
@@ -27,9 +27,13 @@
try {
const url = String((n.data && n.data.url) || '')
if (!url) continue
const resp = await fetch(url, { credentials: 'omit' })
if (!resp || !resp.ok) continue
const blob = await resp.blob()
// 2026-12-04(审计 P1):优先用宿主注入的 Rust 取字节(不受 CORS/白名单限制)
let blob = (typeof o.fetchBlob === 'function') ? await o.fetchBlob(url) : null
if (!blob || !blob.size) {
const resp = await fetch(url, { credentials: 'omit' })
if (!resp || !resp.ok) continue
blob = await resp.blob()
}
if (!blob || !blob.size) continue
const dataUrl = await new Promise((res, rej) => {
const rd = new FileReader()
@@ -64,9 +68,13 @@
if (!url) return
;(async () => {
try {
const resp = await fetch(url, { credentials: 'omit' })
if (!resp || !resp.ok) return
const blob = await resp.blob()
// 2026-12-04(审计 P1):优先 Rust 取字节;失败再回退 WebView fetch
let blob = (typeof o.fetchBlob === 'function') ? await o.fetchBlob(url) : null
if (!blob || !blob.size) {
const resp = await fetch(url, { credentials: 'omit' })
if (!resp || !resp.ok) return
blob = await resp.blob()
}
if (!blob || !blob.size) return
const dataUrl = await new Promise((res, rej) => {
const rd = new FileReader()
+5 -1
View File
@@ -27,6 +27,8 @@
const isOn = typeof d.isOn === 'function' ? d.isOn : function () { return false }
const resolveSrc = typeof d.resolveSrc === 'function' ? d.resolveSrc : null
const toBlob = typeof d.toBlob === 'function' ? d.toBlob : null
// 2026-12-04(审计 P2-12):宿主可注入「系统网络栈取字节」(Rust),远程兜底不再受 CORS 限制
const fetchBlob = typeof d.fetchBlob === 'function' ? d.fetchBlob : null
const write = typeof d.write === 'function' ? d.write : null
const toast = typeof d.toast === 'function' ? d.toast : function () {}
@@ -52,11 +54,13 @@
// 本地原图(dataURL)直接解码成 Blob,不再 fetch(dataURL);远程 URL 才走 fetch
let blob = null
if (toBlob) { try { blob = await toBlob(src) } catch (e) { blob = null } }
if (!blob) {
if (!blob && fetchBlob) { try { blob = await fetchBlob(src) } catch (e2) { blob = null } }
if (!blob || !blob.size) {
const resp = await fetch(src)
if (!resp || !resp.ok) { failed++; continue }
blob = await resp.blob()
}
if (!blob || !blob.size) { failed++; continue }
const name = genName()
const ok = await write(getDir(), name, blob)
if (ok) saved++
+10 -5
View File
@@ -13,7 +13,7 @@
// ── 注入依赖(app.js 在首次渲染时调用 inject 注入)─────────────────────────
// 输出节点缩略图补取去重(每个 url 本会话只触发一次)
const outThumbPending = new Set()
let ingestView, I, LocalTextField, autoGrow, React, ReactDOM, combineSourcePairs, downloadUrl, wResolveOutSrc, wOutNameOf, wPreviewEntryOf, wOutThumbEnsure, isImageProducer, nodePreviewRefsV2, nodeInheritOf, orderedUniqueRefNodes, outProgUI, outThumbCache, uploadRefToImageHost, wDisplayThumbOf, wImgSrcForNode, wImportImage, wPreviewUrlOf, wRawCfg, wResolveImg, wThumbSrc, el, wPluginNodeVariants, wPluginVariantMeta, isBatchSrc, batchSrcText, compareCanvasTextOrder, GenParams, RATIOS, RES_KEYS, RES_MAP, cachedGenerationTasks, classifyErr, fmtDur, presetDisplayName, ratioOfSize, resOfSize, taskStallEndMs, wDedupeTaskIds, wThumbBump
let ingestView, I, LocalTextField, autoGrow, React, ReactDOM, combineSourcePairs, downloadUrl, wResolveOutSrc, wOutNameOf, wPreviewEntryOf, wOutThumbEnsure, wRustFetchBlob, isImageProducer, nodePreviewRefsV2, nodeInheritOf, orderedUniqueRefNodes, outProgUI, outThumbCache, uploadRefToImageHost, wDisplayThumbOf, wImgSrcForNode, wImportImage, wPreviewUrlOf, wRawCfg, wResolveImg, wThumbSrc, el, wPluginNodeVariants, wPluginVariantMeta, isBatchSrc, batchSrcText, compareCanvasTextOrder, GenParams, RATIOS, RES_KEYS, RES_MAP, cachedGenerationTasks, classifyErr, fmtDur, presetDisplayName, ratioOfSize, resOfSize, taskStallEndMs, wDedupeTaskIds, wThumbBump
// 2026-12-03:输出格「NEW」标记 —— 只认本次启动之后生成的图;已确认的 key 记在内存里,不写画布存档。
const OUT_NEW_SINCE = Date.now()
const outNewSeen = new Set()
@@ -34,6 +34,7 @@
wOutNameOf = D.wOutNameOf
wPreviewEntryOf = D.wPreviewEntryOf
wOutThumbEnsure = D.wOutThumbEnsure
wRustFetchBlob = D.wRustFetchBlob
isImageProducer = D.isImageProducer
nodePreviewRefsV2 = D.nodePreviewRefsV2
nodeInheritOf = D.nodeInheritOf
@@ -774,11 +775,15 @@
props.setUploadingRefId && props.setUploadingRefId(node.id)
try {
let payload = d.data
// 只有 URL 时:先取回字节(同源/允许 CORS 才能成功)
// 只有 URL 时:先取回字节。2026-12-04(审计 P1):优先 Rust(不受 CORS/白名单限制),
// 失败再回退 WebView fetch(同源/允许跨域才成功)。
if (!payload) {
const res = await fetch(d.url, { credentials: 'same-origin' })
if (!res || !res.ok) throw new Error('图片下载失败(HTTP ' + (res && res.status) + ')')
const blob = await res.blob()
let blob = (typeof wRustFetchBlob === 'function') ? await wRustFetchBlob(d.url) : null
if (!blob || !blob.size) {
const res = await fetch(d.url, { credentials: 'same-origin' })
if (!res || !res.ok) throw new Error('图片下载失败(HTTP ' + (res && res.status) + ')')
blob = await res.blob()
}
payload = await new Promise((done, fail) => {
const fr = new FileReader()
fr.onload = () => done(String(fr.result || ''))