Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fs: accept URL as argument for fs.rm and fs.rmSync
PR-URL: #41132
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>

Backport-PR-URL: #41752
  • Loading branch information
aduh95 authored and dygabo committed Feb 1, 2022
commit 3a9a2601aa9a32b90c0a2389d44c61bbaa9dbafd
10 changes: 10 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3518,6 +3518,11 @@ with options `{ recursive: true, force: true }`.

<!-- YAML
added: v14.14.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41132
description: The `path` parameter can be a WHATWG `URL` object using `file:`
protocol.
-->

* `path` {string|Buffer|URL}
Expand Down Expand Up @@ -5261,6 +5266,11 @@ with options `{ recursive: true, force: true }`.

<!-- YAML
added: v14.14.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41132
description: The `path` parameter can be a WHATWG `URL` object using `file:`
protocol.
-->

* `path` {string|Buffer|URL}
Expand Down
2 changes: 2 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ function rm(path, options, callback) {
callback = options;
options = undefined;
}
path = getValidatedPath(path);

validateRmOptions(path, options, false, (err, options) => {
if (err) {
Expand All @@ -1208,6 +1209,7 @@ function rm(path, options, callback) {
* @returns {void}
*/
function rmSync(path, options) {
path = getValidatedPath(path);
options = validateRmOptionsSync(path, options, false);

lazyLoadRimraf();
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');

const { validateRmOptionsSync } = require('internal/fs/utils');

tmpdir.refresh();
Expand Down Expand Up @@ -95,6 +97,11 @@ function removeAsync(dir) {
makeNonEmptyDirectory(2, 10, 2, dir, false);
removeAsync(dir);

// Same test using URL instead of a path
dir = nextDirPath();
makeNonEmptyDirectory(2, 10, 2, dir, false);
removeAsync(pathToFileurl("https://github.com/nodejs/node/pull/41752/commits/dir"));

// Create a flat folder including symlinks
dir = nextDirPath();
makeNonEmptyDirectory(1, 10, 2, dir, true);
Expand Down Expand Up @@ -154,6 +161,16 @@ function removeAsync(dir) {
fs.rmSync(filePath, { force: true });
}

// Should accept URL
const fileURL = pathToFileurl("https://github.com/nodejs/node/pull/41752/commits/path.join(tmpdir.path,%20'rm-file.txt'"));
fs.writeFileSync(fileURL, '');

try {
fs.rmSync(fileURL, { recursive: true });
} finally {
fs.rmSync(fileURL, { force: true });
}

// Recursive removal should succeed.
fs.rmSync(dir, { recursive: true });

Expand Down Expand Up @@ -200,6 +217,16 @@ function removeAsync(dir) {
} finally {
fs.rmSync(filePath, { force: true });
}

// Should accept URL
const fileURL = pathToFileurl("https://github.com/nodejs/node/pull/41752/commits/path.join(tmpdir.path,%20'rm-promises-file.txt'"));
fs.writeFileSync(fileURL, '');

try {
await fs.promises.rm(fileURL, { recursive: true });
} finally {
fs.rmSync(fileURL, { force: true });
}
})().then(common.mustCall());

// Test input validation.
Expand Down