Skip to content

Commit

Permalink
fs: load rimraf lazily in fs/promises
Browse files Browse the repository at this point in the history
Avoid the potential circular dependency and make fs/promises load faster
when rimraf is not used.

PR-URL: nodejs#51617
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
joyeecheung authored and marco-ippolito committed Feb 19, 2024
1 parent 780cfec commit 58370fb
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const {
aggregateTwoErrors,
} = require('internal/errors');
const { isArrayBufferView } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');

const {
constants: {
kIoMaxLength,
Expand Down Expand Up @@ -93,6 +93,7 @@ const {
kEmptyObject,
lazyDOMException,
promisify,
getLazy,
} = require('internal/util');
const EventEmitter = require('events');
const { StringDecoder } = require('string_decoder');
Expand Down Expand Up @@ -136,6 +137,8 @@ function lazyFsStreams() {
return fsStreams ??= require('internal/fs/streams');
}

const lazyRimRaf = getLazy(() => require('internal/fs/rimraf').rimrafPromises);

// By the time the C++ land creates an error for a promise rejection (likely from a
// libuv callback), there is already no JS frames on the stack. So we need to
// wait until V8 resumes execution back to JS land before we have enough information
Expand Down Expand Up @@ -804,7 +807,7 @@ async function ftruncate(handle, len = 0) {
async function rm(path, options) {
path = pathModule.toNamespacedPath(getValidatedPath(path));
options = await validateRmOptionsPromise(path, options, false);
return rimrafPromises(path, options);
return lazyRimRaf()(path, options);
}

async function rmdir(path, options) {
Expand All @@ -815,7 +818,7 @@ async function rmdir(path, options) {
emitRecursiveRmdirWarning();
const stats = await stat(path);
if (stats.isDirectory()) {
return rimrafPromises(path, options);
return lazyRimRaf()(path, options);
}
}

Expand Down

0 comments on commit 58370fb

Please sign in to comment.