Skip to content

Commit 1eae316

Browse files
amcaseyKingwl
authored andcommitted
Use fs.existsSync to check for cancellation (microsoft#36190)
Unlike statSync, it doesn't throw if the file doesn't exist, saving both time and allocations.
1 parent 7e223f2 commit 1eae316

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/cancellationToken/cancellationToken.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ interface ServerCancellationToken {
99
}
1010

1111
function pipeExists(name: string): boolean {
12-
try {
13-
fs.statSync(name);
14-
return true;
15-
}
16-
catch (e) {
17-
return false;
18-
}
12+
// Unlike statSync, existsSync doesn't throw an exception if the target doesn't exist.
13+
// A comment in the node code suggests they're stuck with that decision for back compat
14+
// (https://github.com/nodejs/node/blob/9da241b600182a9ff400f6efc24f11a6303c27f7/lib/fs.js#L222).
15+
// Caveat: If a named pipe does exist, the first call to existsSync will return true, as for
16+
// statSync. Subsequent calls will return false, whereas statSync would throw an exception
17+
// indicating that the pipe was busy. The difference is immaterial, since our statSync
18+
// implementation returned false from its catch block.
19+
return fs.existsSync(name);
1920
}
2021

2122
function createCancellationToken(args: string[]): ServerCancellationToken {

0 commit comments

Comments
 (0)