Skip to content

Commit e5c1fd7

Browse files
authored
fs: accept URL as argument for fs.rm and fs.rmSync
PR-URL: #41132 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent bc6072e commit e5c1fd7

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/api/fs.md

+10
Original file line numberDiff line numberDiff line change
@@ -3580,6 +3580,11 @@ with options `{ recursive: true, force: true }`.
35803580

35813581
<!-- YAML
35823582
added: v14.14.0
3583+
changes:
3584+
- version: REPLACEME
3585+
pr-url: https://github.com/nodejs/node/pull/41132
3586+
description: The `path` parameter can be a WHATWG `URL` object using `file:`
3587+
protocol.
35833588
-->
35843589

35853590
* `path` {string|Buffer|URL}
@@ -5328,6 +5333,11 @@ with options `{ recursive: true, force: true }`.
53285333
53295334
<!-- YAML
53305335
added: v14.14.0
5336+
changes:
5337+
- version: REPLACEME
5338+
pr-url: https://github.com/nodejs/node/pull/41132
5339+
description: The `path` parameter can be a WHATWG `URL` object using `file:`
5340+
protocol.
53315341
-->
53325342
53335343
* `path` {string|Buffer|URL}

lib/fs.js

+2
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ function rm(path, options, callback) {
11851185
callback = options;
11861186
options = undefined;
11871187
}
1188+
path = getValidatedPath(path);
11881189

11891190
validateRmOptions(path, options, false, (err, options) => {
11901191
if (err) {
@@ -1208,6 +1209,7 @@ function rm(path, options, callback) {
12081209
* @returns {void}
12091210
*/
12101211
function rmSync(path, options) {
1212+
path = getValidatedPath(path);
12111213
options = validateRmOptionsSync(path, options, false);
12121214

12131215
lazyLoadRimraf();

test/parallel/test-fs-rm.js

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const tmpdir = require('../common/tmpdir');
55
const assert = require('assert');
66
const fs = require('fs');
77
const path = require('path');
8+
const { pathToFileURL } = require('url');
89
const { execSync } = require('child_process');
910

1011
const { validateRmOptionsSync } = require('internal/fs/utils');
@@ -97,6 +98,11 @@ function removeAsync(dir) {
9798
makeNonEmptyDirectory(2, 10, 2, dir, false);
9899
removeAsync(dir);
99100

101+
// Same test using URL instead of a path
102+
dir = nextDirPath();
103+
makeNonEmptyDirectory(2, 10, 2, dir, false);
104+
removeAsync(pathToFileURL(dir));
105+
100106
// Create a flat folder including symlinks
101107
dir = nextDirPath();
102108
makeNonEmptyDirectory(1, 10, 2, dir, true);
@@ -156,6 +162,16 @@ function removeAsync(dir) {
156162
fs.rmSync(filePath, { force: true });
157163
}
158164

165+
// Should accept URL
166+
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt'));
167+
fs.writeFileSync(fileURL, '');
168+
169+
try {
170+
fs.rmSync(fileURL, { recursive: true });
171+
} finally {
172+
fs.rmSync(fileURL, { force: true });
173+
}
174+
159175
// Recursive removal should succeed.
160176
fs.rmSync(dir, { recursive: true });
161177

@@ -202,6 +218,16 @@ function removeAsync(dir) {
202218
} finally {
203219
fs.rmSync(filePath, { force: true });
204220
}
221+
222+
// Should accept URL
223+
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt'));
224+
fs.writeFileSync(fileURL, '');
225+
226+
try {
227+
await fs.promises.rm(fileURL, { recursive: true });
228+
} finally {
229+
fs.rmSync(fileURL, { force: true });
230+
}
205231
})().then(common.mustCall());
206232

207233
// Test input validation.

0 commit comments

Comments
 (0)