Skip to content

Commit

Permalink
Atomic write of targets file (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote authored Jul 19, 2024
1 parent 461d277 commit 1515f55
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/prom-targets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fsProm = require('fs/promises')
const fs = require('fs')
const fsProm = fs.promises
const idEnc = require('hypercore-id-encoding')

async function writePromTargets (location, aliases) {
Expand All @@ -20,7 +21,14 @@ async function writePromTargets (location, aliases) {
}
]

await fsProm.writeFile(location, JSON.stringify(content, null, 1), { encoding: 'utf-8' })
const tmpLocation = `${location}.tmp`
await fsProm.writeFile(
tmpLocation,
JSON.stringify(content, null, 1),
{ encoding: 'utf-8' }
)

await fsProm.rename(tmpLocation, location) // Atomic
}

async function readPromTargets (location) {
Expand Down

0 comments on commit 1515f55

Please sign in to comment.