Code Monkey home page Code Monkey logo

atomic-file's Introduction

atomic-file

store data in a file, but gaurantee there is never a partial write.

This uses a simple trick to make writes to the file system atomic: first write the file to {filename}~, then mv {filename}~ {filename} although it's possible a program to crash while it's part way through writing a file, the rename operation (mv) can only either succeed or fail.

var AtomicFile = require('atomic-file') //default, json encoding
// var AtomicFile = require('atomic-file/buffer') //binary

var af = AtomicFile(filename)
//get the current contents
af.get(function (err, value) {
  //write a new value
  af.set(value, function (err) {

  })
})

note, get stores the read value in memory, so subsequent calls to get will be instant. Successful writes will update the value.

af = AtomicFile(filename, suffix='~', codec=json)

create a new atomic-file instance

af.get(cb(err, value))

read the current value. will callback synchronously if the value is already in memory.

af.set(value, cb)

write value to the file atomically. will callback when it's definitely on disk.

af.destroy(cb)

delete the underlying file. callback when definitely deleted.

License

MIT

atomic-file's People

Contributors

arj03 avatar christianbundy avatar dominictarr avatar tehshrike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

atomic-file's Issues

Travis not running

Hey @christianbundy

I want to create a new release of what is currently in master. Thinking of calling it 2.1.0. I can see that travis is not working, seems like an old version of node, and I know you have been tweaking a bunch of repos for that to work. Could you work your magic here? ๐Ÿฑ

Not that atomic, unfortunately

I've been receiving bug reports for Manyverse (hence we can assume Android is the OS), consistently about the same bug (and crash) related to atomic-file used in ssb-conn (specifically, in ssb-conn-db) which look like:

SyntaxError: Unexpected token , in JSON at position 132
    at JSON.parse (<anonymous>)
    at Object.decode (node_modules/atomic-file/codec/json.js:6:17)
    at /atomic-file/index.js:43:25

I myself have also experienced this on my Android device. I have opened conn.json on my Android device to see how it looks like at those declared positions, and it's usually some extraneous }\n,}}, (or something like that) added to the end of the file.

I don't know how those extra characters get added to the file, but I know that they are getting added, and this happens in a variety of devices. My suspicion is that the fs.rename hack isn't quite working, perhaps because Android is not POSIX compatible or because rename isn't always atomic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.