Comparing version 0.0.7 to 0.0.8
100
lib/tmp.js
@@ -0,1 +1,10 @@ | ||
/*! | ||
* Tmp | ||
* Copyright (c) 2011 KARASZI Istvan <github@spam.raszi.hu> | ||
* GPLv2 Licensed | ||
*/ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var | ||
@@ -6,5 +15,22 @@ fs = require('fs'), | ||
/** | ||
* The working inner variables. | ||
*/ | ||
var | ||
// store the actual TMP directory | ||
_TMP = _getTMPDir(), | ||
// the random characters to choose from | ||
randomChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz", | ||
randomCharsLength = randomChars.length, | ||
// this will hold the objects need to be removed on exit | ||
_removeObjects = []; | ||
/** | ||
* Gets the temp directory. | ||
* | ||
* @return {String} | ||
* @api private | ||
*/ | ||
var _TMP = (function _getTMPDir() { | ||
function _getTMPDir() { | ||
var tmpNames = [ 'TMPDIR', 'TMP', 'TEMP' ]; | ||
@@ -20,10 +46,11 @@ | ||
return '/tmp'; | ||
}()); | ||
} | ||
var | ||
randomChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz", | ||
randomCharsLength = randomChars.length, | ||
_removeObjects = []; | ||
/** | ||
* Checks whether the `obj` parameter is defined or not. | ||
* | ||
* @param {Object} obj | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
function _isUndefined(obj) { | ||
@@ -33,3 +60,12 @@ return obj === void 0; | ||
function _parseOptions(options, callback) { | ||
/** | ||
* Parses the function arguments. | ||
* | ||
* This function helps to have optional arguments. | ||
* | ||
* @param {Object} options | ||
* @param {Function} callback | ||
* @api private | ||
*/ | ||
function _parseArguments(options, callback) { | ||
if (!callback || typeof callback != "function") { | ||
@@ -44,3 +80,7 @@ callback = options; | ||
/** | ||
* Gets the temporary file name. | ||
* Gets a temporary file name. | ||
* | ||
* @param {Object} opts | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
@@ -60,3 +100,3 @@ function _getTmpName(opts, cb) { | ||
function _getName() { | ||
// prefix and postfix | ||
@@ -87,2 +127,4 @@ if (!templateDefined) { | ||
var name = _getName(); | ||
// check whether the path exists then retry if needed | ||
path.exists(name, function _pathExists(exists) { | ||
@@ -102,6 +144,10 @@ if (exists) { | ||
* Creates and opens a temporary file. | ||
* | ||
* @param {Object} options | ||
* @param {Function} callback | ||
* @api public | ||
*/ | ||
function _createTmpFile(options, callback) { | ||
var | ||
args = _parseOptions(options, callback), | ||
args = _parseArguments(options, callback), | ||
opts = args[0], | ||
@@ -112,3 +158,3 @@ cb = args[1]; | ||
// gets the temporary filename | ||
// gets a temporary filename | ||
_getTmpName(opts, function _tmpNameCreated(err, name) { | ||
@@ -121,4 +167,3 @@ if (err) return cb(err); | ||
if (_isUndefined(opts.unlink) || opts.unlink) | ||
_removeObjects.push([ fs.unlinkSync, name ]); | ||
if (!opts.keep) _removeObjects.push([ fs.unlinkSync, name ]); | ||
@@ -132,10 +177,14 @@ cb(null, name, fd); | ||
* Creates a temporary directory. | ||
* | ||
* @param {Object} options | ||
* @param {Function} callback | ||
* @api public | ||
*/ | ||
function _createTmpDir(options, callback) { | ||
var | ||
args = _parseOptions(options, callback), | ||
args = _parseArguments(options, callback), | ||
opts = args[0], | ||
cb = args[1]; | ||
// gets the temporary filename | ||
// gets a temporary filename | ||
_getTmpName(opts, function _tmpNameCreated(err, name) { | ||
@@ -148,4 +197,3 @@ if (err) return cb(err); | ||
if (_isUndefined(opts.unlink) || opts.unlink) | ||
_removeObjects.push([ fs.rmdirSync, name ]); | ||
if (!opts.keep) _removeObjects.push([ fs.rmdirSync, name ]); | ||
@@ -158,5 +206,7 @@ cb(null, name); | ||
/** | ||
* Garbage collection. | ||
* The garbage collector. | ||
* | ||
* @api private | ||
*/ | ||
process.addListener('exit', function _garbageCollection() { | ||
function _garbageCollector() { | ||
for (var i = 0, length = _removeObjects.length; i < length; i++) { | ||
@@ -166,9 +216,13 @@ try { | ||
} catch (e) { | ||
/* already removed */ | ||
// already removed? | ||
} | ||
}; | ||
}); | ||
} | ||
// adding to the exit listener | ||
process.addListener('exit', _garbageCollector); | ||
// exporting all the needed methods | ||
module.exports.tmpdir = _TMP; | ||
module.exports.dir = _createTmpDir; | ||
module.exports.file = _createTmpFile; |
{ | ||
"name": "tmp", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Temporary file and directory creator", | ||
@@ -5,0 +5,0 @@ "author": "KARASZI István <github@spam.raszi.hu> (http://raszi.hu/)", |
@@ -81,4 +81,5 @@ # Tmp | ||
* `tries`: how many times should the function tries to get a unique filename before giving up, default `3` | ||
* `keep`: signals that the temporary file or directory should not be deleted on exit, default is to delete | ||
[1]: https://github.com/bruce/node-temp | ||
[2]: http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html |
var | ||
assert = require('assert'), | ||
path = require('path'); | ||
path = require('path'), | ||
spawn = require('child_process').spawn; | ||
@@ -23,4 +24,27 @@ function _testStat(stat, mode) { | ||
function _testKeep(type, keep, cb) { | ||
var | ||
filename, | ||
cbCalled, | ||
keepTest = spawn('node', [ path.join(__dirname, 'keep.js'), type, keep ]); | ||
keepTest.stdout.on('data', function (data) { | ||
filename = data.toString().replace(/\n/, ""); | ||
}); | ||
keepTest.stderr.on('data', function (data) { | ||
cbCalled = true; | ||
cb(new Error(data.toString())); | ||
}); | ||
keepTest.on('exit', function _exited(code) { | ||
if (cbCalled) return; | ||
if (code !== 0) return cb(new Error('Exited with error code: ' + code)); | ||
cb(null, filename); | ||
}); | ||
} | ||
module.exports.testStat = _testStat; | ||
module.exports.testPrefix = _testPrefix; | ||
module.exports.testPostfix = _testPostfix; | ||
module.exports.testKeep = _testKeep; |
@@ -14,3 +14,3 @@ var | ||
return function _testDirGenerated(err, name) { | ||
assert.ok(path.existsSync(name), 'Should exists'); | ||
assert.ok(path.existsSync(name), 'Should exist'); | ||
@@ -92,3 +92,24 @@ var stat = fs.statSync(name); | ||
} | ||
}, | ||
'keep testing': { | ||
topic: function () { | ||
Test.testKeep('dir', '1', this.callback); | ||
}, | ||
'should be a dir': function(err, name) { | ||
_testDir(040700)(err, name); | ||
fs.rmdirSync(name); | ||
} | ||
}, | ||
'unlink testing': { | ||
topic: function () { | ||
Test.testKeep('dir', '0', this.callback); | ||
}, | ||
'should not exist': function(err, name) { | ||
assert.ok(!path.existsSync(name), "Directory should be removed"); | ||
} | ||
} | ||
}).export(module); |
@@ -12,5 +12,5 @@ var | ||
function _testFile(mode) { | ||
function _testFile(mode, fdTest) { | ||
return function _testFileGenerated(err, name, fd) { | ||
assert.ok(path.existsSync(name), 'Should exists'); | ||
assert.ok(path.existsSync(name), 'Should exist'); | ||
@@ -24,4 +24,6 @@ var stat = fs.statSync(name); | ||
// check with fstat as well (fd checking) | ||
var fstat = fs.fstatSync(fd); | ||
assert.deepEqual(fstat, stat, 'fstat results should be the same'); | ||
if (fdTest) { | ||
var fstat = fs.fstatSync(fd); | ||
assert.deepEqual(fstat, stat, 'fstat results should be the same'); | ||
} | ||
}; | ||
@@ -36,3 +38,3 @@ } | ||
'should be a file': _testFile(0100600), | ||
'should be a file': _testFile(0100600, true), | ||
'should have the default prefix': Test.testPrefix('tmp-'), | ||
@@ -47,3 +49,3 @@ 'should have the default postfix': Test.testPostfix('.tmp') | ||
'should be a file': _testFile(0100600), | ||
'should be a file': _testFile(0100600, true), | ||
'should have the provided prefix': Test.testPrefix('something') | ||
@@ -57,3 +59,3 @@ }, | ||
'should be a file': _testFile(0100600), | ||
'should be a file': _testFile(0100600, true), | ||
'should have the provided postfix': Test.testPostfix('.txt') | ||
@@ -68,3 +70,3 @@ | ||
'should be a file': _testFile(0100600), | ||
'should be a file': _testFile(0100600, true), | ||
'should have the provided prefix': Test.testPrefix('clike-'), | ||
@@ -79,3 +81,3 @@ 'should have the provided postfix': Test.testPostfix('-postfix') | ||
'should be a file': _testFile(0100640), | ||
'should be a file': _testFile(0100640, true), | ||
'should have the provided prefix': Test.testPrefix('foo'), | ||
@@ -90,3 +92,3 @@ 'should have the provided postfix': Test.testPostfix('bar') | ||
'should be a file': _testFile(0100644), | ||
'should be a file': _testFile(0100644, true), | ||
'should have the provided prefix': Test.testPrefix('complicated'), | ||
@@ -104,3 +106,25 @@ 'should have the provided postfix': Test.testPostfix('options') | ||
} | ||
}, | ||
'keep testing': { | ||
topic: function () { | ||
Test.testKeep('file', '1', this.callback); | ||
}, | ||
'should be a file': function(err, name) { | ||
_testFile(0100600, false)(err, name, null); | ||
fs.unlinkSync(name); | ||
} | ||
}, | ||
'unlink testing': { | ||
topic: function () { | ||
Test.testKeep('file', '0', this.callback); | ||
}, | ||
'should not exist': function(err, name) { | ||
assert.ok(!path.existsSync(name), "File should be removed"); | ||
} | ||
} | ||
}).export(module); |
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
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
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
15864
8
426
85
1