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: #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 richardlau committed Mar 25, 2024
1 parent 83d4ecb commit ede4eb9
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 @@ -92,6 +92,7 @@ const {
kEmptyObject,
lazyDOMException,
promisify,
getLazy,
} = require('internal/util');
const { EventEmitterMixin } = require('internal/event_target');
const { StringDecoder } = require('string_decoder');
Expand Down Expand Up @@ -135,6 +136,8 @@ function lazyFsStreams() {
return fsStreams ??= require('internal/fs/streams');
}

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

class FileHandle extends EventEmitterMixin(JSTransferable) {
/**
* @param {InternalFSBinding.FileHandle | undefined} filehandle
Expand Down Expand Up @@ -749,7 +752,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 @@ -760,7 +763,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 ede4eb9

Please sign in to comment.