lmaccherone / node-localstorage

A drop-in substitute for the browser native localStorage API that runs on node.js.
MIT License
450 stars 36 forks source link

get and set to not appear to be atomic #6

Closed jcalfee closed 8 years ago

jcalfee commented 9 years ago

Please add a warning or fix (I prefer the later)...

http://www.w3.org/TR/webstorage/#the-storage-interface

"The setItem() and removeItem() methods must be atomic with respect to failure. In the case of failure, the method does nothing. That is, changes to the data storage area must either be successful, or the data storage area must not be changed at all."

jcalfee commented 9 years ago

http://stackoverflow.com/questions/17047994/transactionally-writing-files-in-node-js

lmaccherone commented 9 years ago

Good point. I use this library for either: 1) testing code that will have to operate in a browser, or (much less often) 2) code that has to operate in both the browser and server. If the latter, the lack of atomicity would not be critical for my applications.

I'm busy with some other stuff right now, but I would gladly consider a pull request that is either atomic or add some warning to the docs. I suspect it could be easily done by using a temporary file and renaming (as suggested in the SO post you also provided).

On another note, I have thoughts of upgrading this to support associative array syntax (e.g. localStorage['myKey'] = 'myValue') using the harmony support for Proxies.

I could be swayed to work on both depending upon how you are using node-localstorage. I want to support its users needs. Can you tell me more about your use case?

jcalfee commented 9 years ago

It will be testing and development only for now. However, it is for crypto so if anyone sets up a server that will be important. I was thinking a simple file rename too.

lmaccherone commented 8 years ago

With the new atomic file operations, this is more atomic now. There still may be some issues if more than one file operation is necessary for the method, but the probability is greatly reduced now.