JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 500253
  • Score
    100M100P100Q182876F
  • License MIT

Simple file writer with race condition prevention and atomic writing

Package Exports

  • steno

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (steno) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

steno

Simple file writer with race condition prevention and atomic writing.

Built on graceful-fs and used in lowdb.

Without steno

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

var data = { counter: 0 };

server.post('/', function (req, res) {
  ++data.counter;

  fs.writeFile('data.json', JSON.stringify(obj), function (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', function (req, res) {
  ++obj.counter
  steno.writeFile('data.json', JSON.stringify(obj), function (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.

Important: works only in a single instance of Node.

License

MIT - Typicode