Skip to content

Commit fcf74c5

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 26e4dc8 commit fcf74c5

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

lib/fs.js

Lines changed: 22 additions & 22 deletions
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,
@@ -2472,8 +2472,8 @@ function realpathSync(p, options) {
24722472
return maybeCachedResult;
24732473
}
24742474

2475-
const seenLinks = ObjectCreate(null);
2476-
const knownHard = ObjectCreate(null);
2475+
const seenLinks = new SafeMap();
2476+
const knownHard = new SafeSet();
24772477
const original = p;
24782478

24792479
// Current character position in p
@@ -2494,7 +2494,7 @@ function realpathSync(p, options) {
24942494
const ctx = { path: base };
24952495
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
24962496
handleErrorFromBinding(ctx);
2497-
knownHard[base] = true;
2497+
knownHard.add(base);
24982498
}
24992499

25002500
// Walk down the path, swapping out linked path parts for their real
@@ -2516,7 +2516,7 @@ function realpathSync(p, options) {
25162516
}
25172517

25182518
// Continue if not a symlink, break if a pipe/socket
2519-
if (knownHard[base] || cache?.get(base) === base) {
2519+
if (knownHard.has(base) || cache?.get(base) === base) {
25202520
if (isFileType(binding.statValues, S_IFIFO) ||
25212521
isFileType(binding.statValues, S_IFSOCK)) {
25222522
break;
@@ -2538,7 +2538,7 @@ function realpathSync(p, options) {
25382538
handleErrorFromBinding(ctx);
25392539

25402540
if (!isFileType(stats, S_IFLNK)) {
2541-
knownHard[base] = true;
2541+
knownHard.add(base);
25422542
cache?.set(base, base);
25432543
continue;
25442544
}
@@ -2551,8 +2551,8 @@ function realpathSync(p, options) {
25512551
const dev = BigIntPrototypeToString(stats[0], 32);
25522552
const ino = BigIntPrototypeToString(stats[7], 32);
25532553
id = `${dev}:${ino}`;
2554-
if (seenLinks[id]) {
2555-
linkTarget = seenLinks[id];
2554+
if (seenLinks.has(id)) {
2555+
linkTarget = seenLinks.get(id);
25562556
}
25572557
}
25582558
if (linkTarget === null) {
@@ -2565,7 +2565,7 @@ function realpathSync(p, options) {
25652565
resolvedLink = pathModule.resolve(previous, linkTarget);
25662566

25672567
cache?.set(base, resolvedLink);
2568-
if (!isWindows) seenLinks[id] = linkTarget;
2568+
if (!isWindows) seenLinks.set(id, linkTarget);
25692569
}
25702570

25712571
// Resolve the link, then start over
@@ -2576,11 +2576,11 @@ function realpathSync(p, options) {
25762576
pos = current.length;
25772577

25782578
// On windows, check that the root exists. On unix there is no need.
2579-
if (isWindows && !knownHard[base]) {
2579+
if (isWindows && !knownHard.has(base)) {
25802580
const ctx = { path: base };
25812581
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
25822582
handleErrorFromBinding(ctx);
2583-
knownHard[base] = true;
2583+
knownHard.add(base);
25842584
}
25852585
}
25862586

@@ -2625,8 +2625,8 @@ function realpath(p, options, callback) {
26252625
validatePath(p);
26262626
p = pathModule.resolve(p);
26272627

2628-
const seenLinks = ObjectCreate(null);
2629-
const knownHard = ObjectCreate(null);
2628+
const seenLinks = new SafeMap();
2629+
const knownHard = new SafeSet();
26302630

26312631
// Current character position in p
26322632
let pos;
@@ -2641,10 +2641,10 @@ function realpath(p, options, callback) {
26412641
pos = current.length;
26422642

26432643
// On windows, check that the root exists. On unix there is no need.
2644-
if (isWindows && !knownHard[base]) {
2644+
if (isWindows && !knownHard.has(base)) {
26452645
fs.lstat(base, (err, stats) => {
26462646
if (err) return callback(err);
2647-
knownHard[base] = true;
2647+
knownHard.add(base);
26482648
LOOP();
26492649
});
26502650
} else {
@@ -2674,7 +2674,7 @@ function realpath(p, options, callback) {
26742674
}
26752675

26762676
// Continue if not a symlink, break if a pipe/socket
2677-
if (knownHard[base]) {
2677+
if (knownHard.has(base)) {
26782678
if (isFileType(binding.statValues, S_IFIFO) ||
26792679
isFileType(binding.statValues, S_IFSOCK)) {
26802680
return callback(null, encodeRealpathResult(p, options));
@@ -2690,7 +2690,7 @@ function realpath(p, options, callback) {
26902690

26912691
// If not a symlink, skip to the next path part
26922692
if (!stats.isSymbolicLink()) {
2693-
knownHard[base] = true;
2693+
knownHard.add(base);
26942694
return process.nextTick(LOOP);
26952695
}
26962696

@@ -2702,15 +2702,15 @@ function realpath(p, options, callback) {
27022702
const dev = BigIntPrototypeToString(stats.dev, 32);
27032703
const ino = BigIntPrototypeToString(stats.ino, 32);
27042704
id = `${dev}:${ino}`;
2705-
if (seenLinks[id]) {
2706-
return gotTarget(null, seenLinks[id]);
2705+
if (seenLinks.has(id)) {
2706+
return gotTarget(null, seenLinks.get(id));
27072707
}
27082708
}
27092709
fs.stat(base, (err) => {
27102710
if (err) return callback(err);
27112711

27122712
fs.readlink(base, (err, target) => {
2713-
if (!isWindows) seenLinks[id] = target;
2713+
if (!isWindows) seenLinks.set(id, target);
27142714
gotTarget(err, target);
27152715
});
27162716
});
@@ -2729,10 +2729,10 @@ function realpath(p, options, callback) {
27292729
pos = current.length;
27302730

27312731
// On windows, check that the root exists. On unix there is no need.
2732-
if (isWindows && !knownHard[base]) {
2732+
if (isWindows && !knownHard.has(base)) {
27332733
fs.lstat(base, (err) => {
27342734
if (err) return callback(err);
2735-
knownHard[base] = true;
2735+
knownHard.add(base);
27362736
LOOP();
27372737
});
27382738
} else {

0 commit comments

Comments
 (0)