What is steno?
The steno npm package is a simple, fast, and lightweight library for writing JSON files atomically. It ensures that data is written to disk safely and without corruption, making it ideal for applications that need to frequently update JSON files.
What are steno's main functionalities?
Atomic Write
This feature allows you to write JSON data to a file atomically. The `writeFile` method ensures that the data is written safely, preventing file corruption.
const steno = require('steno');
const fs = require('fs');
const data = { key: 'value' };
steno.writeFile('data.json', JSON.stringify(data), (err) => {
if (err) throw err;
console.log('Data written successfully');
});
Synchronous Write
This feature allows you to write JSON data to a file synchronously. The `writeFileSync` method ensures that the data is written safely and the operation is completed before moving on to the next line of code.
const steno = require('steno');
const fs = require('fs');
const data = { key: 'value' };
try {
steno.writeFileSync('data.json', JSON.stringify(data));
console.log('Data written successfully');
} catch (err) {
console.error('Error writing data:', err);
}
Other packages similar to steno
write-file-atomic
The write-file-atomic package provides similar functionality to steno by ensuring that files are written atomically. It is designed to handle the same use cases, such as preventing file corruption during write operations. However, write-file-atomic offers more configuration options and is more widely used in the community.
fs-extra
The fs-extra package extends the native Node.js fs module with additional methods, including atomic write operations. While it offers a broader range of file system utilities compared to steno, it is also heavier and includes more features than just atomic writes.
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) => {
++data.counter
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:
data.counter === 1000
data.counter === 865
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