🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

crash-safe-write-file

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crash-safe-write-file

Write a file atomically and use fsync to flush contents

0.0.3
latest
Source
npm
Version published
Weekly downloads
3.1K
-20.05%
Maintainers
1
Weekly downloads
 
Created
Source

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 game. This library borrows graciously from the storage methods in nedb.

Install

npm install crash-safe-write-file --save

Usage

const writeFile = require('crash-safe-write-file').writeFile;
writeFile(filename, data, callback);
import {writeFile} from 'crash-safe-write-file';
writeFile(filename, data, callback);

Signature

/**
 * 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);

Keywords

write file

FAQs

Package last updated on 19 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts