Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and supports automatic removal (if asked)
Node.js Compatibility
---------------------

Supports v6.0.0+.
Supports Node.js 20.x and 22+.

[![Build Status](https://travis-ci.org/bruce/node-temp.png)](https://travis-ci.org/bruce/node-temp)

Expand Down
87 changes: 34 additions & 53 deletions lib/temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ let path = require('path');
let cnst = require('constants');

let os = require('os');
let rimraf = require('rimraf');
let mkdirp = require('mkdirp');
let { rimraf, rimrafSync } = require('rimraf');
let { mkdirp, mkdirpSync } = require('mkdirp');
let osTmpdir = require('os').tmpdir();

const rimrafSync = rimraf.sync;

//== helpers
//
let dir = path.resolve(os.tmpdir());
let cleanupOptions = { maxRetries: 6 };

let RDWR_EXCL = cnst.O_CREAT | cnst.O_TRUNC | cnst.O_RDWR | cnst.O_EXCL;

Expand Down Expand Up @@ -123,7 +122,7 @@ function cleanupFilesSync() {
var count = 0;
var toDelete;
while ((toDelete = filesToDelete.shift()) !== undefined) {
rimrafSync(toDelete, { maxBusyTries: 6 });
rimrafSync(toDelete, cleanupOptions);
count++;
}
return count;
Expand All @@ -144,29 +143,20 @@ function cleanupFiles(callback) {
callback(null, count);
return promise;
}
var toDelete;
var rimrafCallback = function(err) {
if (!left) {
// Prevent processing if aborted
return;
}
if (err) {
// This shouldn't happen; pass error to callback and abort
// processing
callback(err);
left = 0;
return;
} else {
count++;
}
left--;
if (!left) {
var toDelete = filesToDelete.splice(0);
(async function() {
try {
for (const filePath of toDelete) {
await rimraf(filePath, cleanupOptions);
count++;
left--;
}
callback(null, count);
} catch (err) {
left = 0;
callback(err, count);
}
};
while ((toDelete = filesToDelete.shift()) !== undefined) {
rimraf(toDelete, { maxBusyTries: 6 }, rimrafCallback);
}
})();
return promise;
}

Expand All @@ -177,7 +167,7 @@ function cleanupDirsSync() {
var count = 0;
var toDelete;
while ((toDelete = dirsToDelete.shift()) !== undefined) {
rimrafSync(toDelete, { maxBusyTries: 6 });
rimrafSync(toDelete, cleanupOptions);
count++;
}
return count;
Expand All @@ -198,29 +188,20 @@ function cleanupDirs(callback) {
callback(null, count);
return promise;
}
var toDelete;
var rimrafCallback = function (err) {
if (!left) {
// Prevent processing if aborted
return;
}
if (err) {
// rimraf handles most "normal" errors; pass the error to the
// callback and abort processing
callback(err, count);
left = 0;
return;
} else {
count++;
}
left--;
if (!left) {
var toDelete = dirsToDelete.splice(0);
(async function() {
try {
for (const dirPath of toDelete) {
await rimraf(dirPath, cleanupOptions);
count++;
left--;
}
callback(null, count);
} catch (err) {
left = 0;
callback(err, count);
}
};
while ((toDelete = dirsToDelete.shift()) !== undefined) {
rimraf(toDelete, { maxBusyTries: 6 }, rimrafCallback);
}
})();
return promise;
}

Expand Down Expand Up @@ -262,18 +243,18 @@ const mkdir = (affixes, callback) => {
callback = p[1];

let dirPath = generateName(affixes, 'd-');
mkdirp(dirPath, 0o700, (err) => {
if (!err) {
deleteDirOnExit(dirPath);
}
mkdirp(dirPath, { mode: 0o700 }).then(function() {
deleteDirOnExit(dirPath);
callback(null, dirPath);
}, function(err) {
callback(err, dirPath);
});
return promise;
}

const mkdirSync = (affixes) => {
let dirPath = generateName(affixes, 'd-');
mkdirp.sync(dirPath, 0o700);
mkdirpSync(dirPath, { mode: 0o700 });
deleteDirOnExit(dirPath);
return dirPath;
}
Expand Down
Loading