Skip to content

Commit

Permalink
Merge pull request #252 from raszi/gh-250
Browse files Browse the repository at this point in the history
fix #250: introduce tmpdir option for overriding the system tmp dir
  • Loading branch information
silkentrance authored Apr 28, 2020
2 parents 3fde7ec + 4b51e90 commit 598a36f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ All options are optional :)
absolute paths are fine as long as they point to a location under the system's default temporary directory.
Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access,
as tmp will not check the availability of the path, nor will it establish the requested path for you.
* `tmpdir`: allows you to override the system's root tmp directory
* `tries`: how many times should the function try 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 `false`
* In order to clean up, you will have to call the provided `cleanupCallback` function manually.
Expand Down
59 changes: 45 additions & 14 deletions docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ <h4 class="name" id="setGracefulCleanup"><span class="type-signature"></span>set

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line630">line 630</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line654">line 654</a>
</li></ul></dd>


Expand Down Expand Up @@ -1434,7 +1434,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line713">line 713</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line739">line 739</a>
</li></ul></dd>


Expand Down Expand Up @@ -1522,7 +1522,7 @@ <h4 class="name" id="cleanupCallbackSync"><span class="type-signature"></span>cl

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line720">line 720</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line746">line 746</a>
</li></ul></dd>


Expand Down Expand Up @@ -1729,7 +1729,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line699">line 699</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line725">line 725</a>
</li></ul></dd>


Expand Down Expand Up @@ -1936,7 +1936,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line706">line 706</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line732">line 732</a>
</li></ul></dd>


Expand Down Expand Up @@ -2093,7 +2093,7 @@ <h5 class="subsection-title">Properties:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line671">line 671</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line697">line 697</a>
</li></ul></dd>


Expand Down Expand Up @@ -2317,7 +2317,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line683">line 683</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line709">line 709</a>
</li></ul></dd>


Expand Down Expand Up @@ -2555,7 +2555,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line691">line 691</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line717">line 717</a>
</li></ul></dd>


Expand Down Expand Up @@ -2735,7 +2735,7 @@ <h5 class="subsection-title">Properties:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line664">line 664</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line690">line 690</a>
</li></ul></dd>


Expand Down Expand Up @@ -2978,7 +2978,7 @@ <h5 class="subsection-title">Properties:</h5>



<td class="description last">tmp directory relative to the system tmp directory to use</td>
<td class="description last">tmp directory relative to the root tmp directory in use</td>
</tr>


Expand Down Expand Up @@ -3045,6 +3045,37 @@ <h5 class="subsection-title">Properties:</h5>



<tr>

<td class="name"><code>tmpdir</code></td>


<td class="type">


<span class="param-type">string</span>



</td>


<td class="attributes">



&lt;nullable><br>

</td>




<td class="description last">the root tmp directory which overrides the os tmpdir</td>
</tr>



<tr>

<td class="name"><code>unsafeCleanup</code></td>
Expand Down Expand Up @@ -3172,7 +3203,7 @@ <h5 class="subsection-title">Properties:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line647">line 647</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line672">line 672</a>
</li></ul></dd>


Expand Down Expand Up @@ -3246,7 +3277,7 @@ <h4 class="name" id="simpleCallback"><span class="type-signature"></span>simpleC

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line726">line 726</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line752">line 752</a>
</li></ul></dd>


Expand Down Expand Up @@ -3429,7 +3460,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line677">line 677</a>
<a href="tmp.js.html">tmp.js</a>, <a href="tmp.js.html#line703">line 703</a>
</li></ul></dd>


Expand Down Expand Up @@ -3479,7 +3510,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sat Feb 08 2020 01:04:48 GMT+0100 (GMT+01:00)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Tue Apr 28 2020 21:12:13 GMT+0200 (GMT+02:00)
</footer>

<script> prettyPrint(); </script>
Expand Down
3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ <h2>Options</h2>
absolute paths are fine as long as they point to a location under the system's default temporary directory.
Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access,
as tmp will not check the availability of the path, nor will it establish the requested path for you.</li>
<li><code>tmpdir</code>: allows you to override the system's root tmp directory</li>
<li><code>tries</code>: how many times should the function try to get a unique filename before giving up, default <code>3</code></li>
<li><code>keep</code>: signals that the temporary file or directory should not be deleted on exit, default is <code>false</code>
<ul>
Expand All @@ -322,7 +323,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sat Feb 08 2020 01:04:48 GMT+0100 (GMT+01:00)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Tue Apr 28 2020 21:12:13 GMT+0200 (GMT+02:00)
</footer>

<script> prettyPrint(); </script>
Expand Down
56 changes: 41 additions & 15 deletions docs/tmp.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ <h1 class="page-title">Source: tmp.js</h1>
if (index >= 0) _removeObjects.splice(index, 1);

called = true;
if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction == FN_RIMRAF_SYNC) {
if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction === FN_RIMRAF_SYNC) {
return removeFunction(fileOrDirName);
} else {
return removeFunction(fileOrDirName, next || function() {});
Expand Down Expand Up @@ -479,7 +479,13 @@ <h1 class="page-title">Source: tmp.js</h1>
return [{}, callback];
}

return [options, callback];
// copy options so we do not leak the changes we make internally
const actualOptions = {};
for (const key of Object.getOwnPropertyNames(options)) {
actualOptions[key] = options[key];
}

return [actualOptions, callback];
}

/**
Expand All @@ -491,7 +497,7 @@ <h1 class="page-title">Source: tmp.js</h1>
*/
function _generateTmpName(opts) {

const tmpDir = _getTmpDir();
const tmpDir = opts.tmpdir;

/* istanbul ignore else */
if (!_isUndefined(opts.name))
Expand All @@ -508,8 +514,7 @@ <h1 class="page-title">Source: tmp.js</h1>
process.pid,
'-',
_randomChars(12),
'-',
opts.postfix ? opts.postfix : ''
opts.postfix ? '-' + opts.postfix : ''
].join('');

return path.join(tmpDir, opts.dir, name);
Expand All @@ -523,7 +528,11 @@ <h1 class="page-title">Source: tmp.js</h1>
* @private
*/
function _assertAndSanitizeOptions(options) {
const tmpDir = _getTmpDir();

options.tmpdir = _getTmpDir(options);

const tmpDir = options.tmpdir;

/* istanbul ignore else */
if (!_isUndefined(options.name))
_assertIsRelative(options.name, 'name', tmpDir);
Expand Down Expand Up @@ -554,7 +563,7 @@ <h1 class="page-title">Source: tmp.js</h1>
options.template = _isBlank(options.template) ? undefined : path.relative(options.dir, options.template);

// for completeness' sake only, also keep (multiple) blanks if the user, purportedly sane, requests us to
options.name = _isUndefined(options.name) ? undefined : options.name;
options.name = _isUndefined(options.name) ? undefined : _sanitizeName(options.name);
options.prefix = _isUndefined(options.prefix) ? '' : options.prefix;
options.postfix = _isUndefined(options.postfix) ? '' : options.postfix;
}
Expand All @@ -571,11 +580,26 @@ <h1 class="page-title">Source: tmp.js</h1>
* @private
*/
function _resolvePath(name, tmpDir) {
if (name.startsWith(tmpDir)) {
return path.resolve(name);
const sanitizedName = _sanitizeName(name);
if (sanitizedName.startsWith(tmpDir)) {
return path.resolve(sanitizedName);
} else {
return path.resolve(path.join(tmpDir, name));
return path.resolve(path.join(tmpDir, sanitizedName));
}
}

/**
* Sanitize the specified path name by removing all quote characters.
*
* @param name
* @returns {string}
* @private
*/
function _sanitizeName(name) {
if (_isBlank(name)) {
return name;
}
return name.replace(/["']/g, '');
}

/**
Expand Down Expand Up @@ -663,10 +687,11 @@ <h1 class="page-title">Source: tmp.js</h1>
* Returns the currently configured tmp dir from os.tmpdir().
*
* @private
* @param {?Options} options
* @returns {string} the currently configured tmp dir
*/
function _getTmpDir() {
return path.resolve(os.tmpdir());
function _getTmpDir(options) {
return path.resolve(_sanitizeName(options &amp;&amp; options.tmpdir || os.tmpdir()));
}

// Install process exit listener
Expand All @@ -681,9 +706,10 @@ <h1 class="page-title">Source: tmp.js</h1>
* @property (?int) mode the access mode, defaults are 0o700 for directories and 0o600 for files
* @property {?string} template the "mkstemp" like filename template
* @property {?string} name fixed name relative to tmpdir or the specified dir option
* @property {?string} dir tmp directory relative to the system tmp directory to use
* @property {?string} dir tmp directory relative to the root tmp directory in use
* @property {?string} prefix prefix for the generated name
* @property {?string} postfix postfix for the generated name
* @property {?string} tmpdir the root tmp directory which overrides the os tmpdir
* @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty
* @property {?boolean} detachDescriptor detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection
* @property {?boolean} discardDescriptor discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection
Expand Down Expand Up @@ -760,7 +786,7 @@ <h1 class="page-title">Source: tmp.js</h1>

// exporting all the needed methods

// evaluate os.tmpdir() lazily, mainly for simplifying testing but it also will
// evaluate _getTmpDir() lazily, mainly for simplifying testing but it also will
// allow users to reconfigure the temporary directory
Object.defineProperty(module.exports, 'tmpdir', {
enumerable: true,
Expand Down Expand Up @@ -796,7 +822,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sat Feb 08 2020 01:04:48 GMT+0100 (GMT+01:00)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Tue Apr 28 2020 21:12:13 GMT+0200 (GMT+02:00)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit 598a36f

Please sign in to comment.