概述FSA 不支持原子替换,可通过临时文件与备份策略提高安全性。临时写入与替换async function safeReplace(updater) {

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

const src = await handle.getFile();

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

const bws = await backup.createWritable();

await bws.write(await src.arrayBuffer());

await bws.close();

const tmp = await window.showSaveFilePicker({ suggestedName: `${src.name}.tmp` });

const tws = await tmp.createWritable();

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

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

await tws.close();

const ws = await handle.createWritable();

await ws.write(await (await tmp.getFile()).arrayBuffer());

await ws.close();

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部