mkdir-and-mv-stream

1.0.1 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i mkdir-and-mv-stream

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • nopersonsmodules