write-file-atomic
Advanced tools
Comparing version 1.1.4 to 1.2.0
60
index.js
@@ -5,2 +5,3 @@ 'use strict' | ||
var MurmurHash3 = require('imurmurhash') | ||
var extend = Object.assign || require('util')._extend | ||
@@ -24,10 +25,31 @@ function murmurhex () { | ||
var tmpfile = getTmpname(filename) | ||
chain([ | ||
[fs, fs.writeFile, tmpfile, data, options], | ||
options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid], | ||
[fs, fs.rename, tmpfile, filename] | ||
], function (err) { | ||
err ? fs.unlink(tmpfile, function () { callback(err) }) | ||
: callback() | ||
}) | ||
if (options.mode && options.chmod) { | ||
return thenWriteFile() | ||
} else { | ||
// Either mode or chown is not explicitly set | ||
// Default behavior is to copy it from original file | ||
return fs.stat(filename, function (err, stats) { | ||
options = extend({}, options) | ||
if (!err && stats && !options.mode) { | ||
options.mode = stats.mode | ||
} | ||
if (!err && stats && !options.chown && process.getuid) { | ||
options.chown = { uid: stats.uid, gid: stats.gid } | ||
} | ||
return thenWriteFile() | ||
}) | ||
} | ||
function thenWriteFile () { | ||
chain([ | ||
[fs, fs.writeFile, tmpfile, data, options.encoding || 'utf8'], | ||
options.mode && [fs, fs.chmod, tmpfile, options.mode], | ||
options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid], | ||
[fs, fs.rename, tmpfile, filename] | ||
], function (err) { | ||
err ? fs.unlink(tmpfile, function () { callback(err) }) | ||
: callback() | ||
}) | ||
} | ||
} | ||
@@ -38,5 +60,25 @@ | ||
var tmpfile = getTmpname(filename) | ||
try { | ||
fs.writeFileSync(tmpfile, data, options) | ||
if (!options.mode || !options.chmod) { | ||
// Either mode or chown is not explicitly set | ||
// Default behavior is to copy it from original file | ||
try { | ||
var stats = fs.statSync(filename) | ||
options = extend({}, options) | ||
if (!options.mode) { | ||
options.mode = stats.mode | ||
} | ||
if (!options.chown && process.getuid) { | ||
options.chown = { uid: stats.uid, gid: stats.gid } | ||
} | ||
} catch (ex) { | ||
// ignore stat errors | ||
} | ||
} | ||
fs.writeFileSync(tmpfile, data, options.encoding || 'utf8') | ||
if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid) | ||
if (options.mode) fs.chmodSync(tmpfile, options.mode) | ||
fs.renameSync(tmpfile, filename) | ||
@@ -43,0 +85,0 @@ } catch (err) { |
{ | ||
"name": "write-file-atomic", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "Write files in an atomic fashion w/configurable ownership", | ||
@@ -31,4 +31,8 @@ "main": "index.js", | ||
"standard": "^5.4.1", | ||
"tap": "^2.3.1" | ||
} | ||
"tap": "^2.3.1", | ||
"tmp": "0.0.28" | ||
}, | ||
"files": [ | ||
"index.js" | ||
] | ||
} |
@@ -22,3 +22,3 @@ write-file-atomic | ||
The file is initially named `filename + "." + md5hex(__filename, process.pid, ++invocations)`. | ||
The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`. | ||
If writeFile completes successfully then, if passed the **chown** option it will change | ||
@@ -25,0 +25,0 @@ the ownership of the file. Finally it renames the file back to the filename you specified. If |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
5854
4
4
78