| [9af201e] | 1 | write-file-atomic
|
|---|
| 2 | -----------------
|
|---|
| 3 |
|
|---|
| 4 | This is an extension for node's `fs.writeFile` that makes its operation
|
|---|
| 5 | atomic 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 | * tmpfileCreated **Function** called when the tmpfile is created
|
|---|
| 19 | * callback **Function**
|
|---|
| 20 |
|
|---|
| 21 | Atomically and asynchronously writes data to a file, replacing the file if it already
|
|---|
| 22 | exists. data can be a string or a buffer.
|
|---|
| 23 |
|
|---|
| 24 | The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`.
|
|---|
| 25 | Note that `require('worker_threads').threadId` is used in addition to `process.pid` if running inside of a worker thread.
|
|---|
| 26 | If writeFile completes successfully then, if passed the **chown** option it will change
|
|---|
| 27 | the ownership of the file. Finally it renames the file back to the filename you specified. If
|
|---|
| 28 | it encounters errors at any of these steps it will attempt to unlink the temporary file and then
|
|---|
| 29 | pass the error back to the caller.
|
|---|
| 30 | If 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. Writes to different files are still executed in parallel.
|
|---|
| 31 |
|
|---|
| 32 | If provided, the **chown** option requires both **uid** and **gid** properties or else
|
|---|
| 33 | you'll get an error. If **chown** is not specified it will default to using
|
|---|
| 34 | the owner of the previous file. To prevent chown from being ran you can
|
|---|
| 35 | also pass `false`, in which case the file will be created with the current user's credentials.
|
|---|
| 36 |
|
|---|
| 37 | If **mode** is not specified, it will default to using the permissions from
|
|---|
| 38 | an existing file, if any. Expicitly setting this to `false` remove this default, resulting
|
|---|
| 39 | in a file created with the system default permissions.
|
|---|
| 40 |
|
|---|
| 41 | If 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 |
|
|---|
| 43 | If the **fsync** option is **false**, writeFile will skip the final fsync call.
|
|---|
| 44 |
|
|---|
| 45 | If the **tmpfileCreated** option is specified it will be called with the name of the tmpfile when created.
|
|---|
| 46 |
|
|---|
| 47 | Example:
|
|---|
| 48 |
|
|---|
| 49 | ```javascript
|
|---|
| 50 | writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}}, function (err) {
|
|---|
| 51 | if (err) throw err;
|
|---|
| 52 | console.log('It\'s saved!');
|
|---|
| 53 | });
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | This function also supports async/await:
|
|---|
| 57 |
|
|---|
| 58 | ```javascript
|
|---|
| 59 | (async () => {
|
|---|
| 60 | try {
|
|---|
| 61 | await writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}});
|
|---|
| 62 | console.log('It\'s saved!');
|
|---|
| 63 | } catch (err) {
|
|---|
| 64 | console.error(err);
|
|---|
| 65 | process.exit(1);
|
|---|
| 66 | }
|
|---|
| 67 | })();
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | ### var writeFileAtomicSync = require('write-file-atomic').sync<br>writeFileAtomicSync(filename, data, [options])
|
|---|
| 71 |
|
|---|
| 72 | The synchronous version of **writeFileAtomic**.
|
|---|