atomic-file-rw
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -17,3 +17,3 @@ const IdbKvStore = require('idb-kv-store') | ||
readFile: function(filename, opts, cb) { | ||
if (!cb) opts = cb | ||
if (!cb) cb = opts | ||
const { store, key } = getStoreAndKey(filename) | ||
@@ -20,0 +20,0 @@ const storeGet = store.get.bind(store) |
37
fs.js
const fs = require('fs') | ||
const path = require('path') | ||
const mutexify = require('mutexify') | ||
@@ -7,2 +9,4 @@ function getEncoding(opts) { | ||
const locks = new Map() | ||
module.exports = { | ||
@@ -15,14 +19,23 @@ readFile: function(filename, opts, cb) { | ||
if (!cb) cb = opts | ||
const tempFile = filename + '~' | ||
fs.open(tempFile, 'w', (err, fd) => { | ||
if (err) return cb(err) | ||
fs.writeFile(fd, value, getEncoding(opts), (err) => { | ||
if (err) return cb(err) | ||
fs.fsync(fd, (err) => { | ||
if (err) return cb(err) | ||
fs.close(fd, (err) => { | ||
if (err) return cb(err) | ||
fs.rename(tempFile, filename, function (err) { | ||
if(err) cb(err) | ||
else cb(null, value) | ||
if (!locks.has(filename)) | ||
locks.set(filename, mutexify()) | ||
const lock = locks.get(filename) | ||
lock((unlock) => { | ||
const tempFile = filename + '~' | ||
// make sure dir exists | ||
fs.mkdirSync(path.dirname(tempFile), { recursive: true }) | ||
fs.open(tempFile, 'w', (err, fd) => { | ||
if (err) return unlock(cb, err) | ||
fs.writeFile(fd, value, getEncoding(opts), (err) => { | ||
if (err) return unlock(cb, err) | ||
fs.fsync(fd, (err) => { | ||
if (err) return unlock(cb, err) | ||
fs.close(fd, (err) => { | ||
if (err) return unlock(cb, err) | ||
fs.rename(tempFile, filename, () => unlock(cb, null, value)) | ||
}) | ||
@@ -29,0 +42,0 @@ }) |
{ | ||
"name": "atomic-file-rw", | ||
"description": "", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/ssb-ngi-pointer/atomic-file-rw", | ||
@@ -11,3 +11,4 @@ "repository": { | ||
"dependencies": { | ||
"idb-kv-store": "^4.5.0" | ||
"idb-kv-store": "^4.5.0", | ||
"mutexify": "^1.3.1" | ||
}, | ||
@@ -14,0 +15,0 @@ "devDependencies": { |
@@ -7,2 +7,6 @@ # Atomic-file-rw | ||
The library is written to handle writing to a few number of files in a | ||
safe manor. Concurrent writes to the same file are completed in the | ||
order they are started. | ||
## Example | ||
@@ -9,0 +13,0 @@ |
@@ -21,1 +21,15 @@ const tape = require('tape') | ||
}) | ||
tape('Concurrency', (t) => { | ||
let completedWrites = 0 | ||
const concurency = Buffer.from('concurrency!') | ||
for (var i = 0; i < 100; ++i) { | ||
setTimeout(() => { | ||
writeFile("test.txt", concurency, (err, x) => { | ||
t.error(err, "no error") | ||
if (++completedWrites === 100) | ||
t.end() | ||
}) | ||
}, Math.floor(Math.random() * 10)) | ||
} | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5450
118
24
2
+ Addedmutexify@^1.3.1
+ Addedmutexify@1.4.0(transitive)
+ Addedqueue-tick@1.0.1(transitive)