From 37578511a8a86b9cada7ab840823489fb8a6b465 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 10 Dec 2021 10:08:06 +0100 Subject: [PATCH 1/3] fs: accept URL as argument for `fs.rm` and `fs.rmSync` --- lib/fs.js | 2 ++ test/parallel/test-fs-rm.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/fs.js b/lib/fs.js index abdc2f0c39c11b..9884e85d5845ee 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1185,6 +1185,7 @@ function rm(path, options, callback) { callback = options; options = undefined; } + path = getValidatedPath(path); validateRmOptions(path, options, false, (err, options) => { if (err) { @@ -1208,6 +1209,7 @@ function rm(path, options, callback) { * @returns {void} */ function rmSync(path, options) { + path = getValidatedPath(path); options = validateRmOptionsSync(path, options, false); lazyLoadRimraf(); diff --git a/test/parallel/test-fs-rm.js b/test/parallel/test-fs-rm.js index 3fdfc8426248ac..c404be310b4bcd 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -5,6 +5,7 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const fs = require('fs'); const path = require('path'); +const { pathToFileURL }=require('url'); const { execSync } = require('child_process'); const { validateRmOptionsSync } = require('internal/fs/utils'); @@ -97,6 +98,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(dir)); + // Create a flat folder including symlinks dir = nextDirPath(); makeNonEmptyDirectory(1, 10, 2, dir, true); @@ -156,6 +162,16 @@ function removeAsync(dir) { fs.rmSync(filePath, { force: true }); } + // Should accept URL + const fileURL = pathToFileURL(path.join(tmpdir.path, '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 }); @@ -202,6 +218,16 @@ function removeAsync(dir) { } finally { fs.rmSync(filePath, { force: true }); } + + // Should accept URL + const fileURL = pathToFileURL(path.join(tmpdir.path, '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. From 60124934ea8f23175e3c716dd89f7af848fe2f7c Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 10 Dec 2021 10:17:17 +0100 Subject: [PATCH 2/3] fixup! fs: accept URL as argument for `fs.rm` and `fs.rmSync` --- test/parallel/test-fs-rm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-rm.js b/test/parallel/test-fs-rm.js index c404be310b4bcd..5b30599189dec4 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -5,7 +5,7 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const fs = require('fs'); const path = require('path'); -const { pathToFileURL }=require('url'); +const { pathToFileURL } = require('url'); const { execSync } = require('child_process'); const { validateRmOptionsSync } = require('internal/fs/utils'); From b8606dc5e77e49afb80b5f2c041927bcd0752f02 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 12 Dec 2021 18:56:49 +0100 Subject: [PATCH 3/3] fixup! fs: accept URL as argument for `fs.rm` and `fs.rmSync` --- doc/api/fs.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index fb4a625b863240..c8fa23cf7c4228 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3580,6 +3580,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} @@ -5328,6 +5333,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL}