Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

atomic-file-rw

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

atomic-file-rw - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

2

browser.js

@@ -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)

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))
}
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc