Skip to content

Commit 5fe75b0

Browse files
aduh95dygabo
authored andcommitted
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 c75d953 commit 5fe75b0

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/api/fs.md

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

35203520
<!-- YAML
35213521
added: v14.14.0
3522+
changes:
3523+
- version: REPLACEME
3524+
pr-url: https://github.com/nodejs/node/pull/41132
3525+
description: The `path` parameter can be a WHATWG `URL` object using `file:`
3526+
protocol.
35223527
-->
35233528

35243529
* `path` {string|Buffer|URL}
@@ -5261,6 +5266,11 @@ with options `{ recursive: true, force: true }`.
52615266
52625267
<!-- YAML
52635268
added: v14.14.0
5269+
changes:
5270+
- version: REPLACEME
5271+
pr-url: https://github.com/nodejs/node/pull/41132
5272+
description: The `path` parameter can be a WHATWG `URL` object using `file:`
5273+
protocol.
52645274
-->
52655275
52665276
* `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

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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');
9+
810
const { validateRmOptionsSync } = require('internal/fs/utils');
911

1012
tmpdir.refresh();
@@ -95,6 +97,11 @@ function removeAsync(dir) {
9597
makeNonEmptyDirectory(2, 10, 2, dir, false);
9698
removeAsync(dir);
9799

100+
// Same test using URL instead of a path
101+
dir = nextDirPath();
102+
makeNonEmptyDirectory(2, 10, 2, dir, false);
103+
removeAsync(pathToFileURL(dir));
104+
98105
// Create a flat folder including symlinks
99106
dir = nextDirPath();
100107
makeNonEmptyDirectory(1, 10, 2, dir, true);
@@ -154,6 +161,16 @@ function removeAsync(dir) {
154161
fs.rmSync(filePath, { force: true });
155162
}
156163

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

@@ -200,6 +217,16 @@ function removeAsync(dir) {
200217
} finally {
201218
fs.rmSync(filePath, { force: true });
202219
}
220+
221+
// Should accept URL
222+
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt'));
223+
fs.writeFileSync(fileURL, '');
224+
225+
try {
226+
await fs.promises.rm(fileURL, { recursive: true });
227+
} finally {
228+
fs.rmSync(fileURL, { force: true });
229+
}
203230
})().then(common.mustCall());
204231

205232
// Test input validation.

0 commit comments

Comments
 (0)