resolve-symlink.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.build = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = require("path");
  9. const resolveSymlinksAsync = function (path, state, callback) {
  10. const { queue, options: { suppressErrors }, } = state;
  11. queue.enqueue();
  12. fs_1.default.realpath(path, (error, resolvedPath) => {
  13. if (error)
  14. return queue.dequeue(suppressErrors ? null : error, state);
  15. fs_1.default.stat(resolvedPath, (error, stat) => {
  16. if (error)
  17. return queue.dequeue(suppressErrors ? null : error, state);
  18. if (stat.isDirectory() && isRecursive(path, resolvedPath, state))
  19. return queue.dequeue(null, state);
  20. callback(stat, resolvedPath);
  21. queue.dequeue(null, state);
  22. });
  23. });
  24. };
  25. const resolveSymlinks = function (path, state, callback) {
  26. const { queue, options: { suppressErrors }, } = state;
  27. queue.enqueue();
  28. try {
  29. const resolvedPath = fs_1.default.realpathSync(path);
  30. const stat = fs_1.default.statSync(resolvedPath);
  31. if (stat.isDirectory() && isRecursive(path, resolvedPath, state))
  32. return;
  33. callback(stat, resolvedPath);
  34. }
  35. catch (e) {
  36. if (!suppressErrors)
  37. throw e;
  38. }
  39. };
  40. function build(options, isSynchronous) {
  41. if (!options.resolveSymlinks || options.excludeSymlinks)
  42. return null;
  43. return isSynchronous ? resolveSymlinks : resolveSymlinksAsync;
  44. }
  45. exports.build = build;
  46. function isRecursive(path, resolved, state) {
  47. if (state.options.useRealPaths)
  48. return isRecursiveUsingRealPaths(resolved, state);
  49. let parent = (0, path_1.dirname)(path);
  50. let depth = 1;
  51. while (parent !== state.root && depth < 2) {
  52. const resolvedPath = state.symlinks.get(parent);
  53. const isSameRoot = !!resolvedPath &&
  54. (resolvedPath === resolved ||
  55. resolvedPath.startsWith(resolved) ||
  56. resolved.startsWith(resolvedPath));
  57. if (isSameRoot)
  58. depth++;
  59. else
  60. parent = (0, path_1.dirname)(parent);
  61. }
  62. state.symlinks.set(path, resolved);
  63. return depth > 1;
  64. }
  65. function isRecursiveUsingRealPaths(resolved, state) {
  66. return state.visited.includes(resolved + state.options.pathSeparator);
  67. }