Secure writing
Hi,
Let's say we run in an electrically unstable environment, and the system we are running on may be shut down unexpectedly, i.e. power loss. AND we were in the middle of the writeFile/writeFileSync. As far as I understand, the resulting storage file, on the next reboot, will consist in an incomplete JSON file. And so, all its content is lost. OK, chances are small, but thanks to Murphy, it WILL happen. So, would you consider securing the writing step of the file (in persistKey* functions). It could be an additional option, disabled by default.
IMO, it would be as simple as writing the ${md5(key)}.tmp file instead of md5(key), then renaming the temporary file with the actual file name.
In that case, previous content would still be safe.
Thanks!
I actually had this happen to me. json file was wiped. Nothing critical in there, but now I've got a corrupt file!
Look like what is needed is enabling atomic writing (safe write). Means you write to the temp file first, and then rename it to the destination file. On Unix systems renaming is atomic operation, and Win OS has ReplaceFile() method.
This module can be a start point https://github.com/npm/write-file-atomic But it has a flaw with EPERM errors handling on Win OS that is going to be handled here https://github.com/isaacs/node-graceful-fs/pull/119
https://github.com/simonlast/node-persist/issues/31 is somehow related issue.
I actually had this happen to me. json file was wiped. Nothing critical in there, but now I've got a corrupt file!
it is happen to me too!