|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +const isWindows = /^win/.test(process.platform); |
| 4 | + |
| 5 | +// On Windows, Node (still as of v8.1.3) has an issue whereby, when locating JavaScript modules |
| 6 | +// on disk, it walks up the directory hierarchy to the disk root, testing whether each directory |
| 7 | +// is a symlink or not. This fails with an exception if the process doesn't have permission to |
| 8 | +// read those directories. This is a problem when hosting in full IIS, because in typical cases |
| 9 | +// the process does not have read permission for higher-level directories. |
| 10 | +// |
| 11 | +// NodeServices itself works around this by injecting a patched version of Node's 'lstat' API that |
| 12 | +// suppresses these irrelevant errors during module loads. This covers most scenarios, but isn't |
| 13 | +// enough to make Webpack dev middleware work, because typical Webpack configs use loaders such as |
| 14 | +// 'awesome-typescript-loader', which works by forking a child process to do some of its work. The |
| 15 | +// child process does not get the patched 'lstat', and hence fails. It's an especially bad failure, |
| 16 | +// because the Webpack compiler doesn't even surface the exception - it just never completes the |
| 17 | +// compilation process, causing the application to hang indefinitely. |
| 18 | +// |
| 19 | +// Additionally, Webpack dev middleware will want to write its output to disk, which is also going |
| 20 | +// to fail in a typical IIS process, because you won't have 'write' permission to the app dir by |
| 21 | +// default. We have to actually write the build output to disk (and not purely keep it in the in- |
| 22 | +// memory file system) because the server-side prerendering Node instance is a separate process |
| 23 | +// that only knows about code changes when it sees the compiled files on disk change. |
| 24 | +// |
| 25 | +// In the future, we'll hopefully get Node to fix its underlying issue, and figure out whether VS |
| 26 | +// could give 'write' access to the app dir when launching sites in IIS. But until then, disable |
| 27 | +// Webpack dev middleware if we detect the server process doesn't have the necessary permissions. |
| 28 | + |
| 29 | +export function hasSufficientPermissions() { |
| 30 | + if (isWindows) { |
| 31 | + return canReadDirectoryAndAllAncestors(process.cwd()); |
| 32 | + } else { |
| 33 | + return true; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function canReadDirectoryAndAllAncestors(dir: string): boolean { |
| 38 | + if (!canReadDirectory(dir)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + const parentDir = path.resolve(dir, '..'); |
| 43 | + if (parentDir === dir) { |
| 44 | + // There are no more parent directories - we've reached the disk root |
| 45 | + return true; |
| 46 | + } else { |
| 47 | + return canReadDirectoryAndAllAncestors(parentDir); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function canReadDirectory(dir: string): boolean { |
| 52 | + try { |
| 53 | + fs.statSync(dir); |
| 54 | + return true; |
| 55 | + } catch(ex) { |
| 56 | + return false; |
| 57 | + } |
| 58 | +} |
0 commit comments