11
11
*/
12
12
const asyncHooks = require ( "async_hooks" ) ;
13
13
const fs = require ( 'fs' ) ;
14
+ const fsp = require ( 'fs/promises' ) ;
14
15
const os = require ( 'os' ) ;
15
16
const path = require ( 'path' ) ;
16
17
const crypto = require ( 'crypto' ) ;
@@ -385,6 +386,8 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall
385
386
} ;
386
387
}
387
388
389
+ const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
390
+
388
391
/**
389
392
* The garbage collector.
390
393
*
@@ -394,7 +397,7 @@ function _garbageCollector() {
394
397
/* istanbul ignore else */
395
398
if ( ! _gracefulCleanup ) return ;
396
399
397
- _logger . info ( "lib-tmp.gc.start" ) ;
400
+ _logger . info ( "lib-tmp.gc.global. start" ) ;
398
401
399
402
const attemptedObjects = _removeObjects . length ;
400
403
let removedObjects = 0 ;
@@ -414,7 +417,7 @@ function _garbageCollector() {
414
417
415
418
_logger . info (
416
419
{ expected : attemptedObjects , actual : removedObjects , count : removedObjects , duration : dt } ,
417
- "lib-tmp.gc.end"
420
+ "lib-tmp.gc.global. end"
418
421
) ;
419
422
}
420
423
@@ -506,7 +509,8 @@ function _parseArguments(options, callback) {
506
509
*/
507
510
function _generateTmpName ( opts ) {
508
511
509
- const tmpDir = opts . tmpdir ;
512
+ const ctx = prefixStackStorage . getStore ( ) ;
513
+ const tmpDir = ctx && ctx . value ? ctx . value : opts . tmpdir ;
510
514
511
515
/* istanbul ignore else */
512
516
if ( ! _isUndefined ( opts . name ) )
@@ -685,13 +689,6 @@ function setGracefulCleanup() {
685
689
* @returns {string } the currently configured tmp dir
686
690
*/
687
691
function _getTmpDir ( options ) {
688
- if ( _defaultDir ) {
689
- return path . resolve (
690
- options && options . tmpdir ,
691
- _defaultDir || os . tmpdir ( )
692
- )
693
- }
694
- if ( options && options . tmpdir ) { }
695
692
return path . resolve ( options && options . tmpdir || os . tmpdir ( ) ) ;
696
693
}
697
694
@@ -700,22 +697,26 @@ function setLogger(logger) {
700
697
}
701
698
702
699
async function withSubdir ( prefix , fn , options ) {
700
+ const ctx = prefixStackStorage . getStore ( ) ;
701
+ const tmpDir = path . resolve (
702
+ ctx ? ctx . value : _getTmpDir ( ) ,
703
+ prefix
704
+ ) ;
705
+ await fsp . mkdir ( tmpDir , { recursive : true } ) ;
706
+
707
+ const logTags = {
708
+ ...( options && options . logTags ? options . logTags : { } ) ,
709
+ path : tmpDir
710
+ } ;
711
+
703
712
return new Promise ( ( resolve , reject ) => {
704
- const currentValue = prefixStackStorage . getStore ( ) ;
705
- const tmpDir = path . resolve (
706
- currentValue ? currentValue . value : os . tmpdir ( ) ,
707
- prefix
708
- ) ;
709
- fs . mkdirSync ( tmpDir , { recursive : true } ) ;
710
- const cleanup = ( cb ) => {
711
- if ( cb ) {
712
- fs . rmdir ( tmpDir , { recursive : true } , cb ) ;
713
- } else {
714
- return new Promise ( ( resolve , reject ) => fs . rmdir (
715
- tmpDir ,
716
- { recursive : true } ,
717
- ( err ) => err ? reject ( err ) : resolve )
718
- ) ;
713
+ const cleanup = async ( ) => {
714
+ _logger . info ( logTags , "lib-tmp.gc.start" ) ;
715
+ try {
716
+ await fsp . rmdir ( tmpDir , { recursive : true , maxRetries : 5 } ) ;
717
+ _logger . info ( { ...logTags , duration : Date . now ( ) - t0 } , "lib-tmp.gc.end" ) ;
718
+ } catch ( error ) {
719
+ _logger . info ( { ...logTags , duration : Date . now ( ) - t0 , error } , "lib-tmp.gc.error" ) ;
719
720
}
720
721
}
721
722
prefixStackStorage . run ( { value : tmpDir } , ( ) => {
@@ -842,6 +843,5 @@ module.exports.tmpNameSync = tmpNameSync;
842
843
module . exports . setGracefulCleanup = setGracefulCleanup ;
843
844
844
845
module . exports . setLogger = setLogger ;
845
- module . exports . setDefaultDir = setDefaultDir ;
846
846
module . exports . gc = _garbageCollector ;
847
- module . exports . withSubdir = withSubdir ;
847
+ module . exports . withSubdir = withSubdir ;
0 commit comments