Skip to content

Commit 5a8af3d

Browse files
joyeecheungtargos
authored andcommitted
fs: load rimraf lazily in fs/promises
Avoid the potential circular dependency and make fs/promises load faster when rimraf is not used. PR-URL: #51617 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 8235c26 commit 5a8af3d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/internal/fs/promises.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const {
4343
aggregateTwoErrors,
4444
} = require('internal/errors');
4545
const { isArrayBufferView } = require('internal/util/types');
46-
const { rimrafPromises } = require('internal/fs/rimraf');
46+
4747
const {
4848
constants: {
4949
kIoMaxLength,
@@ -93,6 +93,7 @@ const {
9393
kEmptyObject,
9494
lazyDOMException,
9595
promisify,
96+
getLazy,
9697
} = require('internal/util');
9798
const EventEmitter = require('events');
9899
const { StringDecoder } = require('string_decoder');
@@ -136,6 +137,8 @@ function lazyFsStreams() {
136137
return fsStreams ??= require('internal/fs/streams');
137138
}
138139

140+
const lazyRimRaf = getLazy(() => require('internal/fs/rimraf').rimrafPromises);
141+
139142
// By the time the C++ land creates an error for a promise rejection (likely from a
140143
// libuv callback), there is already no JS frames on the stack. So we need to
141144
// wait until V8 resumes execution back to JS land before we have enough information
@@ -804,7 +807,7 @@ async function ftruncate(handle, len = 0) {
804807
async function rm(path, options) {
805808
path = pathModule.toNamespacedPath(getValidatedPath(path));
806809
options = await validateRmOptionsPromise(path, options, false);
807-
return rimrafPromises(path, options);
810+
return lazyRimRaf()(path, options);
808811
}
809812

810813
async function rmdir(path, options) {
@@ -815,7 +818,7 @@ async function rmdir(path, options) {
815818
emitRecursiveRmdirWarning();
816819
const stats = await stat(path);
817820
if (stats.isDirectory()) {
818-
return rimrafPromises(path, options);
821+
return lazyRimRaf()(path, options);
819822
}
820823
}
821824

0 commit comments

Comments
 (0)