Socket
Socket
Sign inDemoInstall

write-file-atomic

Package Overview
Dependencies
3
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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) {

10

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc