Skip to content

Commit

Permalink
Use fs.existsSync to check for cancellation (microsoft#36190)
Browse files Browse the repository at this point in the history
Unlike statSync, it doesn't throw if the file doesn't exist, saving both
time and allocations.
  • Loading branch information
amcasey authored and Kingwl committed Mar 4, 2020
1 parent 7e223f2 commit 1eae316
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/cancellationToken/cancellationToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ interface ServerCancellationToken {
}

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

function createCancellationToken(args: string[]): ServerCancellationToken {
Expand Down

0 comments on commit 1eae316

Please sign in to comment.