Socket
Socket
Sign inDemoInstall

tmp

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tmp - npm Package Compare versions

Comparing version 0.0.31 to 0.0.33

334

lib/tmp.js
/*!
* Tmp
*
* Copyright (c) 2011-2015 KARASZI Istvan <github@spam.raszi.hu>
* Copyright (c) 2011-2017 KARASZI Istvan <github@spam.raszi.hu>
*

@@ -9,19 +9,20 @@ * MIT Licensed

/**
/*
* Module dependencies.
*/
var
fs = require('fs'),
path = require('path'),
crypto = require('crypto'),
tmpDir = require('os-tmpdir'),
_c = process.binding('constants');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const osTmpDir = require('os-tmpdir');
const _c = process.binding('constants');
/**
/*
* The working inner variables.
*/
var
// store the actual TMP directory
_TMP = tmpDir(),
const
/**
* The temporary directory.
* @type {string}
*/
tmpDir = osTmpDir(),

@@ -40,8 +41,9 @@ // the random characters to choose from

DIR_MODE = 448 /* 0700 */,
FILE_MODE = 384 /* 0600 */,
DIR_MODE = 448 /* 0o700 */,
FILE_MODE = 384 /* 0o600 */,
// this will hold the objects need to be removed on exit
_removeObjects = [],
_removeObjects = [];
var
_gracefulCleanup = false,

@@ -54,5 +56,5 @@ _uncaughtException = false;

*
* @param {Number} howMany
* @return {String}
* @api private
* @param {number} howMany
* @returns {string} the generated random name
* @private
*/

@@ -82,4 +84,4 @@ function _randomChars(howMany) {

* @param {Object} obj
* @return {Boolean}
* @api private
* @returns {boolean} true if the object is undefined
* @private
*/

@@ -95,16 +97,16 @@ function _isUndefined(obj) {

*
* @param {Object} options
* @param {(Options|Function)} options
* @param {Function} callback
* @api private
* @returns {Array} parsed arguments
* @private
*/
function _parseArguments(options, callback) {
if (typeof options == 'function') {
var
tmp = options,
options = callback || {},
callback = tmp;
} else if (typeof options == 'undefined') {
options = {};
return [callback || {}, options];
}
if (_isUndefined(options)) {
return [{}, callback];
}
return [options, callback];

@@ -117,8 +119,8 @@ }

* @param {Object} opts
* @returns {String}
* @api private
* @returns {string} the new random name according to opts
* @private
*/
function _generateTmpName(opts) {
if (opts.name) {
return path.join(opts.dir || _TMP, opts.name);
return path.join(opts.dir || tmpDir, opts.name);
}

@@ -132,3 +134,3 @@

// prefix and postfix
var name = [
const name = [
opts.prefix || 'tmp-',

@@ -140,3 +142,3 @@ process.pid,

return path.join(opts.dir || _TMP, name);
return path.join(opts.dir || tmpDir, name);
}

@@ -147,7 +149,6 @@

*
* @param {Object} options
* @param {Function} callback
* @api private
* @param {(Options|tmpNameCallback)} options options or callback
* @param {?tmpNameCallback} callback the callback function
*/
function _getTmpName(options, callback) {
function tmpName(options, callback) {
var

@@ -157,3 +158,3 @@ args = _parseArguments(options, callback),

cb = args[1],
tries = opts.tries || DEFAULT_TRIES;
tries = opts.name ? 1 : opts.tries || DEFAULT_TRIES;

@@ -167,3 +168,3 @@ if (isNaN(tries) || tries < 0)

(function _getUniqueName() {
var name = _generateTmpName(opts);
const name = _generateTmpName(opts);

@@ -184,13 +185,13 @@ // check whether the path exists then retry if needed

/**
* Synchronous version of _getTmpName.
* Synchronous version of tmpName.
*
* @param {Object} options
* @returns {String}
* @api private
* @returns {string} the generated random name
* @throws {Error} if the options are invalid or could not generate a filename
*/
function _getTmpNameSync(options) {
function tmpNameSync(options) {
var
args = _parseArguments(options),
opts = args[0],
tries = opts.tries || DEFAULT_TRIES;
tries = opts.name ? 1 : opts.tries || DEFAULT_TRIES;

@@ -204,3 +205,3 @@ if (isNaN(tries) || tries < 0)

do {
var name = _generateTmpName(opts);
const name = _generateTmpName(opts);
try {

@@ -219,7 +220,6 @@ fs.statSync(name);

*
* @param {Object} options
* @param {Function} callback
* @api public
* @param {(Options|fileCallback)} options the config options or the callback function
* @param {?fileCallback} callback
*/
function _createTmpFile(options, callback) {
function file(options, callback) {
var

@@ -233,3 +233,3 @@ args = _parseArguments(options, callback),

// gets a temporary filename
_getTmpName(opts, function _tmpNameCreated(err, name) {
tmpName(opts, function _tmpNameCreated(err, name) {
if (err) return cb(err);

@@ -251,3 +251,5 @@

} catch (e) {
err = e;
if (!isENOENT(e)) {
err = e;
}
}

@@ -268,9 +270,9 @@ return cb(err);

/**
* Synchronous version of _createTmpFile.
* Synchronous version of file.
*
* @param {Object} options
* @returns {Object} object consists of name, fd and removeCallback
* @api private
* @param {Options} options
* @returns {FileSyncObject} object consists of name, fd and removeCallback
* @throws {Error} if cannot create a file
*/
function _createTmpFileSync(options) {
function fileSync(options) {
var

@@ -282,9 +284,14 @@ args = _parseArguments(options),

var name = _getTmpNameSync(opts);
const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
const name = tmpNameSync(opts);
var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE);
if (opts.discardDescriptor) {
fs.closeSync(fd);
fd = undefined;
}
return {
name : name,
fd : fd,
removeCallback : _prepareTmpFileRemoveCallback(name, fd, opts)
name: name,
fd: fd,
removeCallback: _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts)
};

@@ -296,7 +303,7 @@ }

*
* @param {String} root
* @api private
* @param {string} root
* @private
*/
function _rmdirRecursiveSync(root) {
var dirs = [root];
const dirs = [root];

@@ -334,7 +341,6 @@ do {

*
* @param {Object} options
* @param {Function} callback
* @api public
* @param {(Options|dirCallback)} options the options or the callback function
* @param {?dirCallback} callback
*/
function _createTmpDir(options, callback) {
function dir(options, callback) {
var

@@ -346,3 +352,3 @@ args = _parseArguments(options, callback),

// gets a temporary filename
_getTmpName(opts, function _tmpNameCreated(err, name) {
tmpName(opts, function _tmpNameCreated(err, name) {
if (err) return cb(err);

@@ -360,9 +366,9 @@

/**
* Synchronous version of _createTmpDir.
* Synchronous version of dir.
*
* @param {Object} options
* @returns {Object} object consists of name and removeCallback
* @api private
* @param {Options} options
* @returns {DirSyncObject} object consists of name and removeCallback
* @throws {Error} if it cannot create a directory
*/
function _createTmpDirSync(options) {
function dirSync(options) {
var

@@ -372,8 +378,8 @@ args = _parseArguments(options),

var name = _getTmpNameSync(opts);
const name = tmpNameSync(opts);
fs.mkdirSync(name, opts.mode || DIR_MODE);
return {
name : name,
removeCallback : _prepareTmpDirRemoveCallback(name, opts)
name: name,
removeCallback: _prepareTmpDirRemoveCallback(name, opts)
};

@@ -385,10 +391,10 @@ }

*
* @param {String} name
* @param {int} fd
* @param {string} name the path of the file
* @param {number} fd file descriptor
* @param {Object} opts
* @api private
* @returns {Function} the callback
* @returns {fileCallback}
* @private
*/
function _prepareTmpFileRemoveCallback(name, fd, opts) {
var removeCallback = _prepareRemoveCallback(function _removeCallback(fdPath) {
const removeCallback = _prepareRemoveCallback(function _removeCallback(fdPath) {
try {

@@ -403,3 +409,3 @@ if (0 <= fdPath[0]) {

// by the user, in which case we will simply ignore the error
if (e.errno != -EBADF && e.errno != -ENOENT) {
if (!isEBADF(e) && !isENOENT(e)) {
// reraise any unanticipated error

@@ -409,3 +415,11 @@ throw e;

}
fs.unlinkSync(fdPath[1]);
try {
fs.unlinkSync(fdPath[1]);
}
catch (e) {
if (!isENOENT(e)) {
// reraise any unanticipated error
throw e;
}
}
}, [fd, name]);

@@ -423,10 +437,10 @@

*
* @param {String} name
* @param {string} name
* @param {Object} opts
* @returns {Function} the callback
* @api private
* @private
*/
function _prepareTmpDirRemoveCallback(name, opts) {
var removeFunction = opts.unsafeCleanup ? _rmdirRecursiveSync : fs.rmdirSync.bind(fs);
var removeCallback = _prepareRemoveCallback(removeFunction, name);
const removeFunction = opts.unsafeCleanup ? _rmdirRecursiveSync : fs.rmdirSync.bind(fs);
const removeCallback = _prepareRemoveCallback(removeFunction, name);

@@ -446,3 +460,3 @@ if (!opts.keep) {

* @returns {Function}
* @api private
* @private
*/

@@ -454,10 +468,11 @@ function _prepareRemoveCallback(removeFunction, arg) {

if (!called) {
var index = _removeObjects.indexOf(_cleanupCallback);
if (index >= 0) {
_removeObjects.splice(index, 1);
}
const index = _removeObjects.indexOf(_cleanupCallback);
if (index >= 0) {
_removeObjects.splice(index, 1);
}
called = true;
removeFunction(arg);
called = true;
removeFunction(arg);
}
if (next) next(null);

@@ -470,3 +485,3 @@ };

*
* @api private
* @private
*/

@@ -489,7 +504,50 @@ function _garbageCollector() {

function _setGracefulCleanup() {
/**
* Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
*/
function isEBADF(error) {
return isExpectedError(error, -EBADF, 'EBADF');
}
/**
* Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
*/
function isENOENT(error) {
return isExpectedError(error, -ENOENT, 'ENOENT');
}
/**
* Helper to determine whether the expected error code matches the actual code and errno,
* which will differ between the supported node versions.
*
* - Node >= 7.0:
* error.code {String}
* error.errno {String|Number} any numerical value will be negated
*
* - Node >= 6.0 < 7.0:
* error.code {String}
* error.errno {Number} negated
*
* - Node >= 4.0 < 6.0: introduces SystemError
* error.code {String}
* error.errno {Number} negated
*
* - Node >= 0.10 < 4.0:
* error.code {Number} negated
* error.errno n/a
*/
function isExpectedError(error, code, errno) {
return error.code == code || error.code == errno;
}
/**
* Sets the graceful cleanup.
*
* Also removes the created files and directories when an uncaught exception occurs.
*/
function setGracefulCleanup() {
_gracefulCleanup = true;
}
var version = process.versions.node.split('.').map(function (value) {
const version = process.versions.node.split('.').map(function (value) {
return parseInt(value, 10);

@@ -512,10 +570,74 @@ });

/**
* Configuration options.
*
* @typedef {Object} Options
* @property {?number} tries the number of tries before give up the name generation
* @property {?string} template the "mkstemp" like filename template
* @property {?string} name fix name
* @property {?string} dir the tmp directory to use
* @property {?string} prefix prefix for the generated name
* @property {?string} postfix postfix for the generated name
*/
/**
* @typedef {Object} FileSyncObject
* @property {string} name the name of the file
* @property {string} fd the file descriptor
* @property {fileCallback} removeCallback the callback function to remove the file
*/
/**
* @typedef {Object} DirSyncObject
* @property {string} name the name of the directory
* @property {fileCallback} removeCallback the callback function to remove the directory
*/
/**
* @callback tmpNameCallback
* @param {?Error} err the error object if anything goes wrong
* @param {string} name the temporary file name
*/
/**
* @callback fileCallback
* @param {?Error} err the error object if anything goes wrong
* @param {string} name the temporary file name
* @param {number} fd the file descriptor
* @param {cleanupCallback} fn the cleanup callback function
*/
/**
* @callback dirCallback
* @param {?Error} err the error object if anything goes wrong
* @param {string} name the temporary file name
* @param {cleanupCallback} fn the cleanup callback function
*/
/**
* Removes the temporary created file or directory.
*
* @callback cleanupCallback
* @param {simpleCallback} [next] function to call after entry was removed
*/
/**
* Callback function for function composition.
* @see {@link https://github.com/raszi/node-tmp/issues/57|raszi/node-tmp#57}
*
* @callback simpleCallback
*/
// exporting all the needed methods
module.exports.tmpdir = _TMP;
module.exports.dir = _createTmpDir;
module.exports.dirSync = _createTmpDirSync;
module.exports.file = _createTmpFile;
module.exports.fileSync = _createTmpFileSync;
module.exports.tmpName = _getTmpName;
module.exports.tmpNameSync = _getTmpNameSync;
module.exports.setGracefulCleanup = _setGracefulCleanup;
module.exports.tmpdir = tmpDir;
module.exports.dir = dir;
module.exports.dirSync = dirSync;
module.exports.file = file;
module.exports.fileSync = fileSync;
module.exports.tmpName = tmpName;
module.exports.tmpNameSync = tmpNameSync;
module.exports.setGracefulCleanup = setGracefulCleanup;
{
"name": "tmp",
"version": "0.0.31",
"version": "0.0.33",
"description": "Temporary file and directory creator",

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

"bugs": {
"url": "http://github.com/raszi/node-tmp/issues"
"url": "http://github.com/raszi/node-tmp/issues"
},
"engines": {
"node": ">=0.4.0"
"node": ">=0.6.0"
},
"dependencies": {
"os-tmpdir": "~1.0.1"
"os-tmpdir": "~1.0.2"
},

@@ -32,6 +32,9 @@ "devDependencies": {

"main": "lib/tmp.js",
"files": ["lib/"],
"files": [
"lib/"
],
"scripts": {
"test": "vows test/*-test.js"
"test": "vows test/*-test.js",
"doc": "jsdoc -c .jsdoc.json"
}
}

@@ -8,2 +8,4 @@ # Tmp

[![npm version](https://badge.fury.io/js/tmp.svg)](https://badge.fury.io/js/tmp)
[![API documented](https://img.shields.io/badge/API-documented-brightgreen.svg)](https://raszi.github.io/node-tmp/)
[![Known Vulnerabilities](https://snyk.io/test/npm/tmp/badge.svg)](https://snyk.io/test/npm/tmp)

@@ -16,3 +18,4 @@ ## About

Tmp offers both an asynchronous and a synchronous API. For all API calls, all
the parameters are optional.
the parameters are optional. There also exists a promisified version of the
API, see (5) under references below.

@@ -34,2 +37,4 @@ Tmp uses crypto for determining random file names, or, when using templates,

Please also check [API docs][4].
### Asynchronous file creation

@@ -45,4 +50,4 @@

console.log("File: ", path);
console.log("Filedescriptor: ", fd);
console.log('File: ', path);
console.log('Filedescriptor: ', fd);

@@ -64,4 +69,4 @@ // If we don't need the file anymore we could manually call the cleanupCallback

var tmpobj = tmp.fileSync();
console.log("File: ", tmpobj.name);
console.log("Filedescriptor: ", tmpobj.fd);
console.log('File: ', tmpobj.name);
console.log('Filedescriptor: ', tmpobj.fd);

@@ -90,3 +95,3 @@ // If we don't need the file anymore we could manually call the removeCallback

console.log("Dir: ", path);
console.log('Dir: ', path);

@@ -109,3 +114,3 @@ // Manual cleanup

var tmpobj = tmp.dirSync();
console.log("Dir: ", tmpobj.name);
console.log('Dir: ', tmpobj.name);
// Manual cleanup

@@ -130,3 +135,3 @@ tmpobj.removeCallback();

console.log("Created temporary filename: ", path);
console.log('Created temporary filename: ', path);
});

@@ -143,3 +148,3 @@ ```

var name = tmp.tmpNameSync();
console.log("Created temporary filename: ", name);
console.log('Created temporary filename: ', name);
```

@@ -159,4 +164,4 @@

console.log("File: ", path);
console.log("Filedescriptor: ", fd);
console.log('File: ', path);
console.log('Filedescriptor: ', fd);
});

@@ -173,4 +178,4 @@ ```

var tmpobj = tmp.fileSync({ mode: 0644, prefix: 'prefix-', postfix: '.txt' });
console.log("File: ", tmpobj.name);
console.log("Filedescriptor: ", tmpobj.fd);
console.log('File: ', tmpobj.name);
console.log('Filedescriptor: ', tmpobj.fd);
```

@@ -227,3 +232,3 @@

console.log("Dir: ", path);
console.log('Dir: ', path);
});

@@ -240,3 +245,3 @@ ```

var tmpobj = tmp.dirSync({ mode: 0750, prefix: 'myTmpDir_' });
console.log("Dir: ", tmpobj.name);
console.log('Dir: ', tmpobj.name);
```

@@ -254,3 +259,3 @@

console.log("Dir: ", path);
console.log('Dir: ', path);
});

@@ -267,3 +272,3 @@ ```

var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
console.log("Dir: ", tmpobj.name);
console.log('Dir: ', tmpobj.name);
```

@@ -281,3 +286,3 @@

console.log("Created temporary filename: ", path);
console.log('Created temporary filename: ', path);
});

@@ -293,3 +298,3 @@ ```

var tmpname = tmp.tmpNameSync({ template: '/tmp/tmp-XXXXXX' });
console.log("Created temporary filename: ", tmpname);
console.log('Created temporary filename: ', tmpname);
```

@@ -325,1 +330,3 @@

[3]: http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html
[4]: https://raszi.github.io/node-tmp/
[5]: https://github.com/benjamingr/tmp-promise
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc