write-file-atomic
Advanced tools
Comparing version 2.3.0 to 2.4.0
36
index.js
@@ -13,2 +13,16 @@ 'use strict' | ||
// if we run inside of a worker_thread, `process.pid` is not unique | ||
/* istanbul ignore next */ | ||
var threadId = (function getId () { | ||
try { | ||
var workerThreads = require('worker_threads') | ||
/// if we are in main thread, this is set to `0` | ||
return workerThreads.threadId | ||
} catch (e) { | ||
// worker_threads are not available, fallback to 0 | ||
return 0 | ||
} | ||
})() | ||
var invocations = 0 | ||
@@ -19,2 +33,3 @@ function getTmpname (filename) { | ||
.hash(String(process.pid)) | ||
.hash(String(threadId)) | ||
.hash(String(++invocations)) | ||
@@ -33,7 +48,12 @@ .result() | ||
function writeFile (filename, data, options, callback) { | ||
if (options instanceof Function) { | ||
callback = options | ||
options = null | ||
if (options) { | ||
if (options instanceof Function) { | ||
callback = options | ||
options = {} | ||
} else if (typeof options === 'string') { | ||
options = { encoding: options } | ||
} | ||
} else { | ||
options = {} | ||
} | ||
if (!options) options = {} | ||
@@ -72,6 +92,6 @@ var Promise = options.Promise || global.Promise | ||
if (!options.mode) { | ||
if (options.mode == null) { | ||
options.mode = stats.mode | ||
} | ||
if (!options.chown && process.getuid) { | ||
if (options.chown == null && process.getuid) { | ||
options.chown = { uid: stats.uid, gid: stats.gid } | ||
@@ -141,3 +161,2 @@ } | ||
}).then(function success () { | ||
removeOnExit() | ||
callback() | ||
@@ -158,3 +177,4 @@ }).catch(function fail (err) { | ||
function writeFileSync (filename, data, options) { | ||
if (!options) options = {} | ||
if (typeof options === 'string') options = { encoding: options } | ||
else if (!options) options = {} | ||
try { | ||
@@ -161,0 +181,0 @@ filename = fs.realpathSync(filename) |
{ | ||
"name": "write-file-atomic", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Write files in an atomic fashion w/configurable ownership", | ||
@@ -32,4 +32,4 @@ "main": "index.js", | ||
"rimraf": "^2.5.4", | ||
"standard": "^10.0.2", | ||
"tap": "^10.3.2" | ||
"standard": "^12.0.1", | ||
"tap": "^12.1.3" | ||
}, | ||
@@ -36,0 +36,0 @@ "files": [ |
@@ -11,4 +11,4 @@ write-file-atomic | ||
* data **String** | **Buffer** | ||
* options **Object** | ||
* chown **Object** | ||
* options **Object** | **String** | ||
* chown **Object** default, uid & gid of existing file, if any | ||
* uid **Number** | ||
@@ -18,5 +18,5 @@ * gid **Number** | ||
* fsync **Boolean** default = true | ||
* mode **Number** default = 438 (aka 0666 in Octal) | ||
* mode **Number** default, from existing file, if any | ||
* Promise **Object** default = native Promise object | ||
callback **Function** | ||
* callback **Function** | ||
@@ -27,2 +27,3 @@ Atomically and asynchronously writes data to a file, replacing the file if it already | ||
The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`. | ||
Note that `require('worker_threads').threadId` is used in addition to `process.pid` if running inside of a worker thread. | ||
If writeFile completes successfully then, if passed the **chown** option it will change | ||
@@ -35,6 +36,12 @@ the ownership of the file. Finally it renames the file back to the filename you specified. If | ||
If provided, the **chown** option requires both **uid** and **gid** properties or else | ||
you'll get an error. | ||
you'll get an error. If **chown** is not specified it will default to using | ||
the owner of the previous file. To prevent chown from being ran you can | ||
also pass `false`, in which case the file will be created with the current user's credentials. | ||
The **encoding** option is ignored if **data** is a buffer. It defaults to 'utf8'. | ||
If **mode** is not specified, it will default to using the permissions from | ||
an existing file, if any. Expicitly setting this to `false` remove this default, resulting | ||
in a file created with the system default permissions. | ||
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'. | ||
If the **fsync** option is **false**, writeFile will skip the final fsync call. | ||
@@ -41,0 +48,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11293
5
206
57