Skip to content

Commit 12a591a

Browse files
LiviaMedeirostargos
authored andcommitted
fs: refactor realpath with Map and Set
PR-URL: #43569 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5ae30bf commit 12a591a

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

lib/fs.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ const {
3333
BigIntPrototypeToString,
3434
MathMax,
3535
Number,
36-
ObjectCreate,
3736
ObjectDefineProperties,
3837
ObjectDefineProperty,
3938
Promise,
4039
ReflectApply,
4140
RegExpPrototypeExec,
4241
SafeMap,
42+
SafeSet,
4343
String,
4444
StringPrototypeCharCodeAt,
4545
StringPrototypeIndexOf,
@@ -2486,8 +2486,8 @@ function realpathSync(p, options) {
24862486
return maybeCachedResult;
24872487
}
24882488

2489-
const seenLinks = ObjectCreate(null);
2490-
const knownHard = ObjectCreate(null);
2489+
const seenLinks = new SafeMap();
2490+
const knownHard = new SafeSet();
24912491
const original = p;
24922492

24932493
// Current character position in p
@@ -2508,7 +2508,7 @@ function realpathSync(p, options) {
25082508
const ctx = { path: base };
25092509
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
25102510
handleErrorFromBinding(ctx);
2511-
knownHard[base] = true;
2511+
knownHard.add(base);
25122512
}
25132513

25142514
// Walk down the path, swapping out linked path parts for their real
@@ -2530,7 +2530,7 @@ function realpathSync(p, options) {
25302530
}
25312531

25322532
// Continue if not a symlink, break if a pipe/socket
2533-
if (knownHard[base] || cache?.get(base) === base) {
2533+
if (knownHard.has(base) || cache?.get(base) === base) {
25342534
if (isFileType(binding.statValues, S_IFIFO) ||
25352535
isFileType(binding.statValues, S_IFSOCK)) {
25362536
break;
@@ -2552,7 +2552,7 @@ function realpathSync(p, options) {
25522552
handleErrorFromBinding(ctx);
25532553

25542554
if (!isFileType(stats, S_IFLNK)) {
2555-
knownHard[base] = true;
2555+
knownHard.add(base);
25562556
cache?.set(base, base);
25572557
continue;
25582558
}
@@ -2565,8 +2565,8 @@ function realpathSync(p, options) {
25652565
const dev = BigIntPrototypeToString(stats[0], 32);
25662566
const ino = BigIntPrototypeToString(stats[7], 32);
25672567
id = `${dev}:${ino}`;
2568-
if (seenLinks[id]) {
2569-
linkTarget = seenLinks[id];
2568+
if (seenLinks.has(id)) {
2569+
linkTarget = seenLinks.get(id);
25702570
}
25712571
}
25722572
if (linkTarget === null) {
@@ -2579,7 +2579,7 @@ function realpathSync(p, options) {
25792579
resolvedLink = pathModule.resolve(previous, linkTarget);
25802580

25812581
cache?.set(base, resolvedLink);
2582-
if (!isWindows) seenLinks[id] = linkTarget;
2582+
if (!isWindows) seenLinks.set(id, linkTarget);
25832583
}
25842584

25852585
// Resolve the link, then start over
@@ -2590,11 +2590,11 @@ function realpathSync(p, options) {
25902590
pos = current.length;
25912591

25922592
// On windows, check that the root exists. On unix there is no need.
2593-
if (isWindows && !knownHard[base]) {
2593+
if (isWindows && !knownHard.has(base)) {
25942594
const ctx = { path: base };
25952595
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
25962596
handleErrorFromBinding(ctx);
2597-
knownHard[base] = true;
2597+
knownHard.add(base);
25982598
}
25992599
}
26002600

@@ -2639,8 +2639,8 @@ function realpath(p, options, callback) {
26392639
validatePath(p);
26402640
p = pathModule.resolve(p);
26412641

2642-
const seenLinks = ObjectCreate(null);
2643-
const knownHard = ObjectCreate(null);
2642+
const seenLinks = new SafeMap();
2643+
const knownHard = new SafeSet();
26442644

26452645
// Current character position in p
26462646
let pos;
@@ -2655,10 +2655,10 @@ function realpath(p, options, callback) {
26552655
pos = current.length;
26562656

26572657
// On windows, check that the root exists. On unix there is no need.
2658-
if (isWindows && !knownHard[base]) {
2658+
if (isWindows && !knownHard.has(base)) {
26592659
fs.lstat(base, (err, stats) => {
26602660
if (err) return callback(err);
2661-
knownHard[base] = true;
2661+
knownHard.add(base);
26622662
LOOP();
26632663
});
26642664
} else {
@@ -2688,7 +2688,7 @@ function realpath(p, options, callback) {
26882688
}
26892689

26902690
// Continue if not a symlink, break if a pipe/socket
2691-
if (knownHard[base]) {
2691+
if (knownHard.has(base)) {
26922692
if (isFileType(binding.statValues, S_IFIFO) ||
26932693
isFileType(binding.statValues, S_IFSOCK)) {
26942694
return callback(null, encodeRealpathResult(p, options));
@@ -2704,7 +2704,7 @@ function realpath(p, options, callback) {
27042704

27052705
// If not a symlink, skip to the next path part
27062706
if (!stats.isSymbolicLink()) {
2707-
knownHard[base] = true;
2707+
knownHard.add(base);
27082708
return process.nextTick(LOOP);
27092709
}
27102710

@@ -2716,15 +2716,15 @@ function realpath(p, options, callback) {
27162716
const dev = BigIntPrototypeToString(stats.dev, 32);
27172717
const ino = BigIntPrototypeToString(stats.ino, 32);
27182718
id = `${dev}:${ino}`;
2719-
if (seenLinks[id]) {
2720-
return gotTarget(null, seenLinks[id]);
2719+
if (seenLinks.has(id)) {
2720+
return gotTarget(null, seenLinks.get(id));
27212721
}
27222722
}
27232723
fs.stat(base, (err) => {
27242724
if (err) return callback(err);
27252725

27262726
fs.readlink(base, (err, target) => {
2727-
if (!isWindows) seenLinks[id] = target;
2727+
if (!isWindows) seenLinks.set(id, target);
27282728
gotTarget(err, target);
27292729
});
27302730
});
@@ -2743,10 +2743,10 @@ function realpath(p, options, callback) {
27432743
pos = current.length;
27442744

27452745
// On windows, check that the root exists. On unix there is no need.
2746-
if (isWindows && !knownHard[base]) {
2746+
if (isWindows && !knownHard.has(base)) {
27472747
fs.lstat(base, (err) => {
27482748
if (err) return callback(err);
2749-
knownHard[base] = true;
2749+
knownHard.add(base);
27502750
LOOP();
27512751
});
27522752
} else {

0 commit comments

Comments
 (0)