Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/charliehess/crash-safe-write-file
Writes a file atomically, plus fsync to flush contents
https://github.com/charliehess/crash-safe-write-file
Last synced: 17 days ago
JSON representation
Writes a file atomically, plus fsync to flush contents
- Host: GitHub
- URL: https://github.com/charliehess/crash-safe-write-file
- Owner: CharlieHess
- License: mit
- Created: 2017-01-13T20:59:00.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-19T21:09:52.000Z (almost 8 years ago)
- Last Synced: 2024-11-30T00:06:14.894Z (about 1 month ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# crash-safe-write-file
Write a file atomically and use fsync to flush its contents.Node.js file operations, even `writeFileSync`, cannot guarantee that the contents of the file were written.
From http://www.daveeddy.com/2013/03/26/synchronous-file-io-in-nodejs/:
>`fs.writeFileSync` is synchronous in the sense that it blocks the event loop while it executes. It does NOT ask the Kernel to do a synchronous write to the underlying file system.
This is fine for most cases, but if you're trying to write a file that's immune to crashes, you need to get into the [`fsync`](http://blog.httrack.com/blog/2013/11/15/everything-you-always-wanted-to-know-about-fsync/) game. This library borrows graciously from the storage methods in [`nedb`](https://github.com/louischatriot/nedb).
## Install
```
npm install crash-safe-write-file --save
```## Usage
``` js
const writeFile = require('crash-safe-write-file').writeFile;
writeFile(filename, data, callback);
```
``` js
import {writeFile} from 'crash-safe-write-file';
writeFile(filename, data, callback);
```## Signature
``` js
/**
* Fully write or rewrite the datafile, immune to crashes during the write
* operation. Writes to a temporary file like `write-file-atomic`, but flushes
* all buffers using fsync.
*
* Adapted from https://github.com/louischatriot/nedb/blob/master/lib/storage.js.
*
* @param {String} filename The destination file
* @param {String} data The data to write
* @param {Function} callback Optional callback on completion or error
*/
function writeFile(filename, data, callback);
```