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.
The problem
Let's say you have a server and want to save data to disk:
var data = { counter: 0 };
server.post('/', function (req, res) {
// Increment counter
++data.counter;
// Save data asynchronously
fs.writeFile('data.json', JSON.stringify(data), 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.
steno solves that.
Usage
server.post('/increment', function (req, res) {
++data.counter
steno.writeFile('data.json', JSON.stringify(data), 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.
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