概述写入后的校验与回滚可降低风险。本文给出最小实现。写入与校验回滚async function writeWithVerify(updater) {

const [handle] = await window.showOpenFilePicker({ multiple: false });

const file = await handle.getFile();

const backup = await window.showSaveFilePicker({ suggestedName: `${file.name}.bak` });

const bws = await backup.createWritable(); await bws.write(await file.arrayBuffer()); await bws.close();

const ws = await handle.createWritable();

const next = await updater(await file.text());

await ws.write(new Blob([next], { type: file.type||'text/plain' })); await ws.close();

const newFile = await handle.getFile();

const ok = newFile.size > 0;

if (!ok) { const rw = await handle.createWritable(); await rw.write(await (await backup.getFile()).arrayBuffer()); await rw.close(); alert('校验失败,已回滚到备份'); }

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部