@@ -22,12 +22,6 @@ const rimraf = require('rimraf');
22
22
* The working inner variables.
23
23
*/
24
24
const
25
- /**
26
- * The temporary directory.
27
- * @type {string }
28
- */
29
- tmpDir = os . tmpdir ( ) ,
30
-
31
25
// the random characters to choose from
32
26
RANDOM_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ,
33
27
@@ -123,12 +117,21 @@ function _parseArguments(options, callback) {
123
117
* @private
124
118
*/
125
119
function _generateTmpName ( opts ) {
120
+
121
+ const tmpDir = _getTmpDir ( ) ;
122
+
123
+ // fail early on missing tmp dir
124
+ if ( isBlank ( opts . dir ) && isBlank ( tmpDir ) ) {
125
+ throw new Error ( 'No tmp dir specified' ) ;
126
+ }
127
+
126
128
/* istanbul ignore else */
127
- if ( opts . name ) {
129
+ if ( ! isBlank ( opts . name ) ) {
128
130
return path . join ( opts . dir || tmpDir , opts . name ) ;
129
131
}
130
132
131
133
// mkstemps like template
134
+ // opts.template has already been guarded in tmpName() below
132
135
/* istanbul ignore else */
133
136
if ( opts . template ) {
134
137
var template = opts . template ;
@@ -141,10 +144,10 @@ function _generateTmpName(opts) {
141
144
142
145
// prefix and postfix
143
146
const name = [
144
- opts . prefix || 'tmp-' ,
147
+ ( isBlank ( opts . prefix ) ? 'tmp-' : opts . prefix ) ,
145
148
process . pid ,
146
149
_randomChars ( 12 ) ,
147
- opts . postfix || ''
150
+ ( isBlank ( opts . postfix ) ? '' : opts . postfix )
148
151
] . join ( '' ) ;
149
152
150
153
return path . join ( opts . dir || tmpDir , name ) ;
@@ -161,7 +164,7 @@ function tmpName(options, callback) {
161
164
args = _parseArguments ( options , callback ) ,
162
165
opts = args [ 0 ] ,
163
166
cb = args [ 1 ] ,
164
- tries = opts . name ? 1 : opts . tries || DEFAULT_TRIES ;
167
+ tries = ! isBlank ( opts . name ) ? 1 : opts . tries || DEFAULT_TRIES ;
165
168
166
169
/* istanbul ignore else */
167
170
if ( isNaN ( tries ) || tries < 0 )
@@ -172,20 +175,24 @@ function tmpName(options, callback) {
172
175
return cb ( new Error ( 'Invalid template provided' ) ) ;
173
176
174
177
( function _getUniqueName ( ) {
175
- const name = _generateTmpName ( opts ) ;
178
+ try {
179
+ const name = _generateTmpName ( opts ) ;
176
180
177
- // check whether the path exists then retry if needed
178
- fs . stat ( name , function ( err ) {
179
- /* istanbul ignore else */
180
- if ( ! err ) {
181
+ // check whether the path exists then retry if needed
182
+ fs . stat ( name , function ( err ) {
181
183
/* istanbul ignore else */
182
- if ( tries -- > 0 ) return _getUniqueName ( ) ;
184
+ if ( ! err ) {
185
+ /* istanbul ignore else */
186
+ if ( tries -- > 0 ) return _getUniqueName ( ) ;
183
187
184
- return cb ( new Error ( 'Could not get a unique tmp filename, max tries reached ' + name ) ) ;
185
- }
188
+ return cb ( new Error ( 'Could not get a unique tmp filename, max tries reached ' + name ) ) ;
189
+ }
186
190
187
- cb ( null , name ) ;
188
- } ) ;
191
+ cb ( null , name ) ;
192
+ } ) ;
193
+ } catch ( err ) {
194
+ cb ( err ) ;
195
+ }
189
196
} ( ) ) ;
190
197
}
191
198
@@ -200,7 +207,7 @@ function tmpNameSync(options) {
200
207
var
201
208
args = _parseArguments ( options ) ,
202
209
opts = args [ 0 ] ,
203
- tries = opts . name ? 1 : opts . tries || DEFAULT_TRIES ;
210
+ tries = ! isBlank ( opts . name ) ? 1 : opts . tries || DEFAULT_TRIES ;
204
211
205
212
/* istanbul ignore else */
206
213
if ( isNaN ( tries ) || tries < 0 )
@@ -234,7 +241,7 @@ function file(options, callback) {
234
241
opts = args [ 0 ] ,
235
242
cb = args [ 1 ] ;
236
243
237
- opts . postfix = ( _isUndefined ( opts . postfix ) ) ? '.tmp' : opts . postfix ;
244
+ opts . postfix = isBlank ( opts . postfix ) ? '.tmp' : opts . postfix ;
238
245
239
246
// gets a temporary filename
240
247
tmpName ( opts , function _tmpNameCreated ( err , name ) {
@@ -287,7 +294,7 @@ function fileSync(options) {
287
294
args = _parseArguments ( options ) ,
288
295
opts = args [ 0 ] ;
289
296
290
- opts . postfix = ( _isUndefined ( opts . postfix ) ) ? '.tmp' : opts . postfix ;
297
+ opts . postfix = isBlank ( opts . postfix ) ? '.tmp' : opts . postfix ;
291
298
292
299
const discardOrDetachDescriptor = opts . discardDescriptor || opts . detachDescriptor ;
293
300
const name = tmpNameSync ( opts ) ;
@@ -536,32 +543,53 @@ function isENOENT(error) {
536
543
* which will differ between the supported node versions.
537
544
*
538
545
* - Node >= 7.0:
539
- * error.code {String }
540
- * error.errno {String|Number } any numerical value will be negated
546
+ * error.code {string }
547
+ * error.errno {string|number } any numerical value will be negated
541
548
*
542
549
* - Node >= 6.0 < 7.0:
543
- * error.code {String }
544
- * error.errno {Number } negated
550
+ * error.code {string }
551
+ * error.errno {number } negated
545
552
*
546
553
* - Node >= 4.0 < 6.0: introduces SystemError
547
- * error.code {String }
548
- * error.errno {Number } negated
554
+ * error.code {string }
555
+ * error.errno {number } negated
549
556
*
550
557
* - Node >= 0.10 < 4.0:
551
- * error.code {Number } negated
558
+ * error.code {number } negated
552
559
* error.errno n/a
553
560
*/
554
561
function isExpectedError ( error , code , errno ) {
555
562
return error . code === code || error . code === errno ;
556
563
}
557
564
565
+ /**
566
+ * Helper which determines whether a string s is blank, that is undefined, or empty or null.
567
+ *
568
+ * @private
569
+ * @param {string } s
570
+ * @returns {Boolean } true whether the string s is blank, false otherwise
571
+ */
572
+ function isBlank ( s ) {
573
+ return s === null || s === undefined || ! s . trim ( ) ;
574
+ }
575
+
558
576
/**
559
577
* Sets the graceful cleanup.
560
578
*/
561
579
function setGracefulCleanup ( ) {
562
580
_gracefulCleanup = true ;
563
581
}
564
582
583
+ /**
584
+ * Returns the currently configured tmp dir from os.tmpdir().
585
+ *
586
+ * @private
587
+ * @returns {string } the currently configured tmp dir
588
+ */
589
+ function _getTmpDir ( ) {
590
+ return os . tmpdir ( ) ;
591
+ }
592
+
565
593
/**
566
594
* If there are multiple different versions of tmp in place, make sure that
567
595
* we recognize the old listeners.
@@ -715,7 +743,16 @@ _safely_install_sigint_listener();
715
743
*/
716
744
717
745
// exporting all the needed methods
718
- module . exports . tmpdir = tmpDir ;
746
+
747
+ // evaluate os.tmpdir() lazily, mainly for simplifying testing but it also will
748
+ // allow users to reconfigure the temporary directory
749
+ Object . defineProperty ( module . exports , 'tmpdir' , {
750
+ enumerable : true ,
751
+ configurable : false ,
752
+ get : function ( ) {
753
+ return _getTmpDir ( ) ;
754
+ }
755
+ } ) ;
719
756
720
757
module . exports . dir = dir ;
721
758
module . exports . dirSync = dirSync ;
0 commit comments