Simple file writer with atomic writing and race condition prevention.
Can be used as a drop-in replacement to
npm install steno --save
const steno =steno
The problem it solves
Let's say you have a server and want to save data to disk:
var data = counter: 0server
Now if you have many requests, for example
1000, there's a risk that you end up with:
// In your serverdatacounter === 1000// In data.jsondatacounter === 865 // ... or any other value
fs.write doesn't guarantee that the call order will be kept. Also, if the server is killed while
data.json is being written, the file can get corrupted.
With steno you'll always have the same data in your server and file. And in case of a crash, file integrity will be preserved.
if needed, you can also use
steno.writeFileSync() which offers atomic writing too.
Important: works only in a single instance of Node.
MIT - Typicode