Skip to content

Commit 4b51e90

Browse files
committed
fix #250: introduce tmpdir option for overriding the system tmp dir
1 parent 3fde7ec commit 4b51e90

File tree

7 files changed

+121
-41
lines changed

7 files changed

+121
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ All options are optional :)
350350
absolute paths are fine as long as they point to a location under the system's default temporary directory.
351351
Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access,
352352
as tmp will not check the availability of the path, nor will it establish the requested path for you.
353+
* `tmpdir`: allows you to override the system's root tmp directory
353354
* `tries`: how many times should the function try to get a unique filename before giving up, default `3`
354355
* `keep`: signals that the temporary file or directory should not be deleted on exit, default is `false`
355356
* In order to clean up, you will have to call the provided `cleanupCallback` function manually.

docs/global.html

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ <h4 class="name" id="setGracefulCleanup"><span class="type-signature"></span>set
908908

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

914914

@@ -1434,7 +1434,7 @@ <h5>Parameters:</h5>
14341434

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

14401440

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

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

15281528

@@ -1729,7 +1729,7 @@ <h5>Parameters:</h5>
17291729

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

17351735

@@ -1936,7 +1936,7 @@ <h5>Parameters:</h5>
19361936

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

19421942

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

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

20992099

@@ -2317,7 +2317,7 @@ <h5>Parameters:</h5>
23172317

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

23232323

@@ -2555,7 +2555,7 @@ <h5>Parameters:</h5>
25552555

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

25612561

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

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

27412741

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

29792979

29802980

2981-
<td class="description last">tmp directory relative to the system tmp directory to use</td>
2981+
<td class="description last">tmp directory relative to the root tmp directory in use</td>
29822982
</tr>
29832983

29842984

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

30463046

30473047

3048+
<tr>
3049+
3050+
<td class="name"><code>tmpdir</code></td>
3051+
3052+
3053+
<td class="type">
3054+
3055+
3056+
<span class="param-type">string</span>
3057+
3058+
3059+
3060+
</td>
3061+
3062+
3063+
<td class="attributes">
3064+
3065+
3066+
3067+
&lt;nullable><br>
3068+
3069+
</td>
3070+
3071+
3072+
3073+
3074+
<td class="description last">the root tmp directory which overrides the os tmpdir</td>
3075+
</tr>
3076+
3077+
3078+
30483079
<tr>
30493080

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

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

31783209

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

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

32523283

@@ -3429,7 +3460,7 @@ <h5>Parameters:</h5>
34293460

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

34353466

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

34813512
<footer>
3482-
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)
3513+
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)
34833514
</footer>
34843515

34853516
<script> prettyPrint(); </script>

docs/index.html

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

324325
<footer>
325-
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)
326+
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)
326327
</footer>
327328

328329
<script> prettyPrint(); </script>

docs/tmp.js.html

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ <h1 class="page-title">Source: tmp.js</h1>
380380
if (index >= 0) _removeObjects.splice(index, 1);
381381

382382
called = true;
383-
if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction == FN_RIMRAF_SYNC) {
383+
if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction === FN_RIMRAF_SYNC) {
384384
return removeFunction(fileOrDirName);
385385
} else {
386386
return removeFunction(fileOrDirName, next || function() {});
@@ -479,7 +479,13 @@ <h1 class="page-title">Source: tmp.js</h1>
479479
return [{}, callback];
480480
}
481481

482-
return [options, callback];
482+
// copy options so we do not leak the changes we make internally
483+
const actualOptions = {};
484+
for (const key of Object.getOwnPropertyNames(options)) {
485+
actualOptions[key] = options[key];
486+
}
487+
488+
return [actualOptions, callback];
483489
}
484490

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

494-
const tmpDir = _getTmpDir();
500+
const tmpDir = opts.tmpdir;
495501

496502
/* istanbul ignore else */
497503
if (!_isUndefined(opts.name))
@@ -508,8 +514,7 @@ <h1 class="page-title">Source: tmp.js</h1>
508514
process.pid,
509515
'-',
510516
_randomChars(12),
511-
'-',
512-
opts.postfix ? opts.postfix : ''
517+
opts.postfix ? '-' + opts.postfix : ''
513518
].join('');
514519

515520
return path.join(tmpDir, opts.dir, name);
@@ -523,7 +528,11 @@ <h1 class="page-title">Source: tmp.js</h1>
523528
* @private
524529
*/
525530
function _assertAndSanitizeOptions(options) {
526-
const tmpDir = _getTmpDir();
531+
532+
options.tmpdir = _getTmpDir(options);
533+
534+
const tmpDir = options.tmpdir;
535+
527536
/* istanbul ignore else */
528537
if (!_isUndefined(options.name))
529538
_assertIsRelative(options.name, 'name', tmpDir);
@@ -554,7 +563,7 @@ <h1 class="page-title">Source: tmp.js</h1>
554563
options.template = _isBlank(options.template) ? undefined : path.relative(options.dir, options.template);
555564

556565
// for completeness' sake only, also keep (multiple) blanks if the user, purportedly sane, requests us to
557-
options.name = _isUndefined(options.name) ? undefined : options.name;
566+
options.name = _isUndefined(options.name) ? undefined : _sanitizeName(options.name);
558567
options.prefix = _isUndefined(options.prefix) ? '' : options.prefix;
559568
options.postfix = _isUndefined(options.postfix) ? '' : options.postfix;
560569
}
@@ -571,11 +580,26 @@ <h1 class="page-title">Source: tmp.js</h1>
571580
* @private
572581
*/
573582
function _resolvePath(name, tmpDir) {
574-
if (name.startsWith(tmpDir)) {
575-
return path.resolve(name);
583+
const sanitizedName = _sanitizeName(name);
584+
if (sanitizedName.startsWith(tmpDir)) {
585+
return path.resolve(sanitizedName);
576586
} else {
577-
return path.resolve(path.join(tmpDir, name));
587+
return path.resolve(path.join(tmpDir, sanitizedName));
588+
}
589+
}
590+
591+
/**
592+
* Sanitize the specified path name by removing all quote characters.
593+
*
594+
* @param name
595+
* @returns {string}
596+
* @private
597+
*/
598+
function _sanitizeName(name) {
599+
if (_isBlank(name)) {
600+
return name;
578601
}
602+
return name.replace(/["']/g, '');
579603
}
580604

581605
/**
@@ -663,10 +687,11 @@ <h1 class="page-title">Source: tmp.js</h1>
663687
* Returns the currently configured tmp dir from os.tmpdir().
664688
*
665689
* @private
690+
* @param {?Options} options
666691
* @returns {string} the currently configured tmp dir
667692
*/
668-
function _getTmpDir() {
669-
return path.resolve(os.tmpdir());
693+
function _getTmpDir(options) {
694+
return path.resolve(_sanitizeName(options &amp;&amp; options.tmpdir || os.tmpdir()));
670695
}
671696

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

761787
// exporting all the needed methods
762788

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

798824
<footer>
799-
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)
825+
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)
800826
</footer>
801827

802828
<script> prettyPrint(); </script>

0 commit comments

Comments
 (0)