1.0.1 • Published 10 years ago
mkdir-and-mv-stream v1.0.1
mkdir-and-mv-stream
stream into a directory that doesn't exist yet, and fail safely if the process crashes.
writing npmd, I've found my self doing this a lot:
//make a directory for the file to be in.
mkdirp(path.dirname(filename), function () {
//stream file to a temporary location
var tmp = path.join(osenv.tmpdir(), Date.now() + '-' + Math.random())
stream.pipe(fs.createWriteStream(tmp)
.on('close', function () {
//when streaming completes, move the file to where it should be.
fs.rename(tmp, filename, function (err) {
if(err) cb(err)
else cb() //done!
})
})
})
It's important to stream to a safe location and then stream the file.
If the program crashes while writing the file, this depends on your program,
but an incomplete file is probably corrupt. fs.rename
(mv
) is atomic,
so the file will appear in the final location if and only if the program did not crash.
License
MIT
1.0.1
10 years ago