• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..20-Sep-2020-

CHANGELOG.mdH A D26-Oct-1985679 2615

LICENSEH A D26-Oct-1985734 73

README.mdH A D26-Oct-19852.8 KiB5743

package.jsonH A D20-Sep-20201.9 KiB7271

README.md

1write-file-atomic
2-----------------
3
4This is an extension for node's `fs.writeFile` that makes its operation
5atomic and allows you set ownership (uid/gid of the file).
6
7### var writeFileAtomic = require('write-file-atomic')<br>writeFileAtomic(filename, data, [options], callback)
8
9* filename **String**
10* data **String** | **Buffer**
11* options **Object** | **String**
12  * chown **Object** default, uid & gid of existing file, if any
13    * uid **Number**
14    * gid **Number**
15  * encoding **String** | **Null** default = 'utf8'
16  * fsync **Boolean** default = true
17  * mode **Number** default, from existing file, if any
18  * Promise **Object** default = native Promise object
19* callback **Function**
20
21Atomically and asynchronously writes data to a file, replacing the file if it already
22exists.  data can be a string or a buffer.
23
24The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`.
25Note that `require('worker_threads').threadId` is used in addition to `process.pid` if running inside of a worker thread.
26If writeFile completes successfully then, if passed the **chown** option it will change
27the ownership of the file. Finally it renames the file back to the filename you specified. If
28it encounters errors at any of these steps it will attempt to unlink the temporary file and then
29pass the error back to the caller.
30If multiple writes are concurrently issued to the same file, the write operations are put into a queue and serialized in the order they were called, using Promises. Native promises are used by default, but you can inject your own promise-like object with the **Promise** option. Writes to different files are still executed in parallel.
31
32If provided, the **chown** option requires both **uid** and **gid** properties or else
33you'll get an error.  If **chown** is not specified it will default to using
34the owner of the previous file.  To prevent chown from being ran you can
35also pass `false`, in which case the file will be created with the current user's credentials.
36
37If **mode** is not specified, it will default to using the permissions from
38an existing file, if any.  Expicitly setting this to `false` remove this default, resulting
39in a file created with the system default permissions.
40
41If options is a String, it's assumed to be the **encoding** option. The **encoding** option is ignored if **data** is a buffer. It defaults to 'utf8'.
42
43If the **fsync** option is **false**, writeFile will skip the final fsync call.
44
45Example:
46
47```javascript
48writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}}, function (err) {
49  if (err) throw err;
50  console.log('It\'s saved!');
51});
52```
53
54### var writeFileAtomicSync = require('write-file-atomic').sync<br>writeFileAtomicSync(filename, data, [options])
55
56The synchronous version of **writeFileAtomic**.
57