Skip to content
Simple file writer with atomic writing and race condition prevention
JavaScript
Branch: master
Clone or download

Latest commit

Latest commit 9ff61b0 Mar 18, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Refactor API Jun 2, 2015
.npmignore Add .npmignore Feb 26, 2016
.travis.yml Update .travis.yml Jun 2, 2015
LICENSE Initial commit Oct 20, 2014
README.md Fix callback issue, thanks to @mcheshkov Feb 26, 2016
index.js Symlink support - use `fs.realpathSync(file)` inside `Writer` Mar 10, 2020
package.json 0.5.0 Mar 18, 2020
test.js Fix callback issue, thanks to @mcheshkov Feb 26, 2016

README.md

steno

Simple file writer with atomic writing and race condition prevention.

Can be used as a drop-in replacement to fs.writeFile().

Built on graceful-fs and used in lowdb.

Install

npm install steno --save

Usage

const steno = require('steno')

steno.writeFile('file.json', data, err => {
  if (err) throw err
})

The problem it solves

Without steno

Let's say you have a server and want to save data to disk:

var data = { counter: 0 }

server.post('/', (req, res) => {
  // Increment counter
  ++data.counter

  // Save data asynchronously
  fs.writeFile('data.json', JSON.stringify(data), err => {
    if (err) throw err
    res.end()
  })
})

Now if you have many requests, for example 1000, there's a risk that you end up with:

// In your server
data.counter === 1000

// In data.json
data.counter === 865 // ... or any other value

Why? Because, 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

server.post('/increment', (req, res) => {
  ++data.counter

  steno.writeFile('data.json', JSON.stringify(data), err => {
    if (err) throw err
    res.end()
  })
})

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.

License

MIT - Typicode

You can’t perform that action at this time.