Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ export async function writeRecord(key, patch) {
await mkdir(BOXES_DIR, { recursive: true })
const prev = (await readRecord(key)) || {}
const next = { ...prev, ...patch, key, updatedAt: new Date().toISOString() }
const tmp = `${recordPath(key)}.tmp.${process.pid}`
await writeFile(tmp, JSON.stringify(next, null, 2))
await rename(tmp, recordPath(key))
const nonce = `${Date.now()}.${Math.random().toString(36).slice(2)}`
const tmp = `${recordPath(key)}.tmp.${process.pid}.${nonce}`
try {
await writeFile(tmp, JSON.stringify(next, null, 2))
await rename(tmp, recordPath(key))
} catch (err) {
try { await unlink(tmp) } catch {}
throw err
}
return next
}

Expand Down