f69759d4bf23cfb22766db8fc1e151489d92e56bbad1dfdffb2b4746c709ce3c0fffb792dd59fe79f3e78680b2464a8ff701f90dc716fe7aa9028013169b0e 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # fs-write-stream-atomic
  2. Like `fs.createWriteStream(...)`, but atomic.
  3. Writes to a tmp file and does an atomic `fs.rename` to move it into
  4. place when it's done.
  5. First rule of debugging: **It's always a race condition.**
  6. ## USAGE
  7. ```javascript
  8. var fsWriteStreamAtomic = require('fs-write-stream-atomic')
  9. // options are optional.
  10. var write = fsWriteStreamAtomic('output.txt', options)
  11. var read = fs.createReadStream('input.txt')
  12. read.pipe(write)
  13. // When the write stream emits a 'finish' or 'close' event,
  14. // you can be sure that it is moved into place, and contains
  15. // all the bytes that were written to it, even if something else
  16. // was writing to `output.txt` at the same time.
  17. ```
  18. ### `fsWriteStreamAtomic(filename, [options])`
  19. * `filename` {String} The file we want to write to
  20. * `options` {Object}
  21. * `chown` {Object} User and group to set ownership after write
  22. * `uid` {Number}
  23. * `gid` {Number}
  24. * `encoding` {String} default = 'utf8'
  25. * `mode` {Number} default = `0666`
  26. * `flags` {String} default = `'w'`