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