This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On Windows, patch Node's lstat to work around permissions error. Fixes …
- Loading branch information
1 parent
70d89b9
commit 22ff88a
Showing
3 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Microsoft.AspNetCore.NodeServices/TypeScript/HttpNodeInstanceEntryPoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Microsoft.AspNetCore.NodeServices/TypeScript/Util/PatchModuleResolutionLStat.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as path from 'path'; | ||
const startsWith = (str: string, prefix: string) => str.substring(0, prefix.length) === prefix; | ||
const appRootDir = process.cwd(); | ||
|
||
function patchedLStat(pathToStatLong: string) { | ||
try { | ||
// If the lstat completes without errors, we don't modify its behavior at all | ||
return origLStat.apply(this, arguments); | ||
} catch(ex) { | ||
const shouldOverrideError = | ||
startsWith(ex.message, 'EPERM') // It's a permissions error | ||
&& typeof appRootDirLong === 'string' | ||
&& startsWith(appRootDirLong, pathToStatLong) // ... for an ancestor directory | ||
&& ex.stack.indexOf('Object.realpathSync ') >= 0; // ... during symlink resolution | ||
|
||
if (shouldOverrideError) { | ||
// Fake the result to give the same result as an 'lstat' on the app root dir. | ||
// This stops Node failing to load modules just because it doesn't know whether | ||
// ancestor directories are symlinks or not. If there's a genuine file | ||
// permissions issue, it will still surface later when Node actually | ||
// tries to read the file. | ||
return origLStat.call(this, appRootDir); | ||
} else { | ||
// In any other case, preserve the original error | ||
throw ex; | ||
} | ||
} | ||
}; | ||
|
||
// It's only necessary to apply this workaround on Windows | ||
let appRootDirLong: string = null; | ||
let origLStat: Function = null; | ||
if (/^win/.test(process.platform)) { | ||
try { | ||
// Get the app's root dir in Node's internal "long" format (e.g., \\?\C:\dir\subdir) | ||
appRootDirLong = (path as any)._makeLong(appRootDir); | ||
|
||
// Actually apply the patch, being as defensive as possible | ||
const bindingFs = (process as any).binding('fs'); | ||
origLStat = bindingFs.lstat; | ||
if (typeof origLStat === 'function') { | ||
bindingFs.lstat = patchedLStat; | ||
} | ||
} catch(ex) { | ||
// If some future version of Node throws (e.g., to prevent use of process.binding()), | ||
// don't apply the patch, but still let the application run. | ||
} | ||
} |