Socket
Socket
Sign inDemoInstall

tmp

Package Overview
Dependencies
0
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.5 to 0.0.6

README.md

131

lib/tmp.js

@@ -8,5 +8,3 @@ var

*/
var MAX_REMOVE_LISTENERS = 100;
var TMP = (function _getTMPDir() {
var _TMP = (function _getTMPDir() {
var tmpNames = [ 'TMPDIR', 'TMP', 'TEMP' ];

@@ -24,2 +22,4 @@

var _removeObjects = [];
function _isUndefined(obj) {

@@ -29,8 +29,4 @@ return obj === void 0;

function _parseOptions(options, maximalTries, callback) {
function _parseOptions(options, callback) {
if (!callback || typeof callback != "function") {
callback = maximalTries;
maximalTries = 3;
}
if (!callback || typeof callback != "function") {
callback = options;

@@ -40,3 +36,3 @@ options = {};

return [ options, maximalTries, callback ];
return [ options, callback ];
}

@@ -47,12 +43,31 @@

*/
function _getTmpName(tmp, prefix, postfix) {
var name =
[
(_isUndefined(prefix)) ? 'tmp-' : prefix,
process.pid,
(Math.random() * 0x1000000000).toString(36),
postfix
].join('');
function _getTmpName(dir, prefix, postfix, maxTries, cb) {
var tries = maxTries || 3;
return path.join(tmp || TMP, name);
if (tries < 0) return cb(new Error('Invalid tries'));
function _getName() {
var name =
[
(_isUndefined(prefix)) ? 'tmp-' : prefix,
process.pid,
(Math.random() * 0x1000000000).toString(36),
postfix
].join('');
return path.join(dir || _TMP, name);
}
(function _getUniqueName() {
var name = _getName();
path.exists(name, function _pathExists(exists) {
if (exists) {
if (tries-- > 0) return _getUniqueName();
return cb(new Error('Could not get a unique tmp filename, max tries reached'));
}
cb(null, name);
});
}());
}

@@ -63,28 +78,25 @@

*/
function _createTmpFile(options, maximalTries, callback) {
function _createTmpFile(options, callback) {
var
args = _parseOptions(options, maximalTries, callback),
args = _parseOptions(options, callback),
opts = args[0],
maxTries = args[1],
cb = args[2],
cb = args[1],
filePostFix = (_isUndefined(opts.postfix)) ? '.tmp' : opts.postfix,
remainingTries = (_isUndefined(maxTries)) ? 3 : maxTries;
filePostFix = (_isUndefined(opts.postfix)) ? '.tmp' : opts.postfix;
if (remainingTries <= 0) return cb(new Error("Could not create tmp file, max tries reached"));
// gets the temporary filename
var name = _getTmpName(opts.tmp, opts.prefix, filePostFix);
_getTmpName(opts.dir, opts.prefix, filePostFix, opts.tries,
function _tmpNameCreated(err, name) {
if (err) return cb(err);
// check for it and create it
path.exists(name, function _pathExists(exists) {
if (exists) return _createTmpFile(--remainingTries, opts, cb);
fs.open(name, 'w+', opts.mode || 0600, function _fileCreated(err, fd) {
if (err) return cb(err);
fs.open(name, 'w', opts.mode || 0600, function _fileCreated(err, fd) {
if (err) return cb(err);
if (_isUndefined(opts.unlink) || opts.unlink)
_removeObjects.push([ fs.unlinkSync, name ]);
if (_isUndefined(opts.unlink) || opts.unlink) _onExit(fs.unlinkSync, name);
cb(null, name, fd);
});
});
cb(null, name, fd);
});
}
);
}

@@ -95,45 +107,42 @@

*/
function _createTmpDir(options, maximalTries, callback) {
function _createTmpDir(options, callback) {
var
args = _parseOptions(options, maximalTries, callback),
args = _parseOptions(options, callback),
opts = args[0],
maxTries = args[1],
cb = args[2],
cb = args[1],
dirPostfix = (_isUndefined(opts.postfix)) ? '' : opts.postfix;
remainingTries = (_isUndefined(maxTries)) ? 3 : maxTries;
if (remainingTries <= 0) return cb(new Error("Could not create tmp directory, max tries reached"));
// gets the temporary filename
_getTmpName(opts.dir, opts.prefix, dirPostfix, opts.tries,
function _tmpNameCreated(err, name) {
if (err) return cb(err);
var name = _getTmpName(opts.tmp, opts.prefix, dirPostfix);
fs.mkdir(name, opts.mode || 0700, function _dirCreated(err) {
if (err) return cb(err);
// check for it and create it
path.exists(name, function _pathExists(exists) {
if (exists) return _createTmpDir(opts, --remainingTries, cb);
if (_isUndefined(opts.unlink) || opts.unlink)
_removeObjects.push([ fs.rmdirSync, name ]);
fs.mkdir(name, opts.mode || 0700, function _dirCreated(err) {
if (err) return cb(err);
if (_isUndefined(opts.unlink) || opts.unlink) _onExit(fs.rmdirSync, name);
cb(null, name);
});
});
cb(null, name);
});
}
);
}
/**
* Removes the created items.
* Garbage collection.
*/
function _onExit(func, path) {
process.setMaxListeners(MAX_REMOVE_LISTENERS);
process.addListener('exit', function() {
process.addListener('exit', function _garbageCollection() {
for (var i = 0, length = _removeObjects.length; i < length; i++) {
try {
func(path);
_removeObjects[i][0].call(null, _removeObjects[i][1]);
} catch (e) {
/* already removed */
}
});
}
};
});
module.exports.tmpdir = _TMP;
module.exports.dir = _createTmpDir;
module.exports.file = _createTmpFile;
{
"name": "tmp",
"version": "0.0.5",
"version": "0.0.6",
"description": "Temporary file and directory creator",

@@ -5,0 +5,0 @@ "author": "KARASZI István <github@spam.raszi.hu> (http://raszi.hu/)",

@@ -71,3 +71,13 @@ var

'should have the provided postfix': Test.testPostfix('options')
},
'no tries': {
topic: function () {
tmp.dir({ tries: -1 }, this.callback);
},
'should not be created': function (err, name) {
assert.isObject(err);
}
}
}).export(module);

@@ -76,3 +76,13 @@ var

'should have the provided postfix': Test.testPostfix('options')
},
'no tries': {
topic: function () {
tmp.file({ tries: -1 }, this.callback);
},
'should not be created': function (err, name) {
assert.isObject(err);
}
}
}).export(module);
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