Skip to content

Commit 2e62837

Browse files
committed
Use fs.rm() instead of rimraf
A regression occurred in 0.2.2 which can be reproduced using: ```js const assert = require('assert'); const tmp = require('tmp'); tmp.dir({ unsafeCleanup: true }, (err, path, cleanup) => { assert.ifError(err); cleanup(assert.ifError); }); ``` This works with 0.2.1. With 0.2.2 it fails with: /path/to/tmp/node-tmp/lib/tmp.js:358 return removeFunction(fileOrDirName, next || function() {}); ^ TypeError: removeFunction is not a function at _cleanupCallback (/path/to/tmp/node-tmp/lib/tmp.js:358:16) at /path/to/tmp/node-tmp/repro.js:5:3 at _dirCreated (/path/to/tmp/node-tmp/lib/tmp.js:207:7) at FSReqCallback.oncomplete (node:fs:192:23) This occurs because 00bb5b2 upgraded the rimraf dependency from ^3.0.0 to ^5.0.5 without handling the change to a `Promise`-based API in 4.0.0 (isaacs/rimraf@a71e7f9) or the removal of the default export in 5.0.0 (isaacs/rimraf@c7a3fd4). This commit fixes the issue by dropping the `rimraf` dependency in favor of `fs.rm({recursive: true})`. Fixes: 00bb5b2 ("Update rimraf and drop old Node compatibility") Fixes: #295 Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
1 parent 9fcf3ce commit 2e62837

11 files changed

+646
-4253
lines changed

docs/global.html

+519-519
Large diffs are not rendered by default.

docs/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ <h3> </h3>
4545
<section>
4646
<article><h1>Tmp</h1>
4747
<p>A simple temporary file and directory creator for <a href="http://nodejs.org/">node.js.</a></p>
48-
<p><a href="https://travis-ci.org/raszi/node-tmp"><img src="https://travis-ci.org/raszi/node-tmp.svg?branch=master" alt="Build Status"></a>
49-
<a href="https://david-dm.org/raszi/node-tmp"><img src="https://david-dm.org/raszi/node-tmp.svg" alt="Dependencies"></a>
48+
<p><a href="https://github.com/raszi/node-tmp/actions/workflows/node.js.yml"><img src="https://img.shields.io/github/actions/workflow/status/raszi/node-tmp/node.js.yml?branch=master" alt="Build Status"></a>
49+
<a href="https://libraries.io/github/raszi/node-tmp"><img src="https://img.shields.io/librariesio/github/raszi/node-tmp" alt="Dependencies"></a>
5050
<a href="https://badge.fury.io/js/tmp"><img src="https://badge.fury.io/js/tmp.svg" alt="npm version"></a>
5151
<a href="https://raszi.github.io/node-tmp/"><img src="https://img.shields.io/badge/API-documented-brightgreen.svg" alt="API documented"></a>
5252
<a href="https://snyk.io/test/npm/tmp"><img src="https://snyk.io/test/npm/tmp/badge.svg" alt="Known Vulnerabilities"></a></p>
@@ -84,7 +84,7 @@ <h2>An Important Note on Previously Undocumented Breaking Changes</h2>
8484
<h2>An Important Note on Compatibility</h2>
8585
<p>See the <a href="./CHANGELOG.md">CHANGELOG</a> for more information.</p>
8686
<h3>Version 0.2.2</h3>
87-
<p>Since version 0.2.2, all support for node version &lt; 12 has been dropped.</p>
87+
<p>Since version 0.2.2, all support for node version &lt;= 12 has been dropped.</p>
8888
<h3>Version 0.1.0</h3>
8989
<p>Since version 0.1.0, all support for node versions &lt; 0.10.0 has been dropped.</p>
9090
<p>Most importantly, any support for earlier versions of node-tmp was also dropped.</p>
@@ -338,7 +338,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
338338
<br class="clear">
339339

340340
<footer>
341-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri Aug 26 2022 22:37:40 GMT+0200 (Mitteleuropäische Sommerzeit)
341+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Feb 28 2024 16:13:00 GMT-0700 (Mountain Standard Time)
342342
</footer>
343343

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

docs/tmp.js.html

+35-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ <h1 class="page-title">Source: tmp.js</h1>
4242
const path = require('path');
4343
const crypto = require('crypto');
4444
const _c = { fs: fs.constants, os: os.constants };
45-
const rimraf = require('rimraf');
4645

4746
/*
4847
* The working inner variables.
@@ -71,12 +70,44 @@ <h1 class="page-title">Source: tmp.js</h1>
7170
_removeObjects = [],
7271

7372
// API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
74-
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs),
75-
FN_RIMRAF_SYNC = rimraf.sync;
73+
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs);
7674

7775
let
7876
_gracefulCleanup = false;
7977

78+
/**
79+
* Recursively remove a directory and its contents.
80+
*
81+
* @param {string} dirPath path of directory to remove
82+
* @param {Function} callback
83+
* @private
84+
*/
85+
function rimraf(dirPath, callback) {
86+
// fs.rm() was added in Node.js 14.14.
87+
// Use deprecated rmdir({recursive: true}) on older versions.
88+
if (!fs.rm) {
89+
return fs.rmdir(dirPath, { recursive: true }, callback);
90+
}
91+
92+
return fs.rm(dirPath, { recursive: true }, callback);
93+
}
94+
95+
/**
96+
* Recursively remove a directory and its contents, synchronously.
97+
*
98+
* @param {string} dirPath path of directory to remove
99+
* @private
100+
*/
101+
function FN_RIMRAF_SYNC(dirPath) {
102+
// fs.rm() was added in Node.js 14.14.
103+
// Use deprecated rmdir({recursive: true}) on older versions.
104+
if (!fs.rm) {
105+
return fs.rmdirSync(dirPath, { recursive: true });
106+
}
107+
108+
return fs.rmSync(dirPath, { recursive: true });
109+
}
110+
80111
/**
81112
* Gets a temporary file name.
82113
*
@@ -807,7 +838,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
807838
<br class="clear">
808839

809840
<footer>
810-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri Aug 26 2022 22:37:40 GMT+0200 (Mitteleuropäische Sommerzeit)
841+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Feb 28 2024 16:13:00 GMT-0700 (Mountain Standard Time)
811842
</footer>
812843

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

lib/tmp.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const os = require('os');
1414
const path = require('path');
1515
const crypto = require('crypto');
1616
const _c = { fs: fs.constants, os: os.constants };
17-
const rimraf = require('rimraf');
1817

1918
/*
2019
* The working inner variables.
@@ -43,12 +42,44 @@ const
4342
_removeObjects = [],
4443

4544
// API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
46-
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs),
47-
FN_RIMRAF_SYNC = rimraf.sync;
45+
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs);
4846

4947
let
5048
_gracefulCleanup = false;
5149

50+
/**
51+
* Recursively remove a directory and its contents.
52+
*
53+
* @param {string} dirPath path of directory to remove
54+
* @param {Function} callback
55+
* @private
56+
*/
57+
function rimraf(dirPath, callback) {
58+
// fs.rm() was added in Node.js 14.14.
59+
// Use deprecated rmdir({recursive: true}) on older versions.
60+
if (!fs.rm) {
61+
return fs.rmdir(dirPath, { recursive: true }, callback);
62+
}
63+
64+
return fs.rm(dirPath, { recursive: true }, callback);
65+
}
66+
67+
/**
68+
* Recursively remove a directory and its contents, synchronously.
69+
*
70+
* @param {string} dirPath path of directory to remove
71+
* @private
72+
*/
73+
function FN_RIMRAF_SYNC(dirPath) {
74+
// fs.rm() was added in Node.js 14.14.
75+
// Use deprecated rmdir({recursive: true}) on older versions.
76+
if (!fs.rm) {
77+
return fs.rmdirSync(dirPath, { recursive: true });
78+
}
79+
80+
return fs.rmSync(dirPath, { recursive: true });
81+
}
82+
5283
/**
5384
* Gets a temporary file name.
5485
*

0 commit comments

Comments
 (0)