resolve-symlink.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 = build;
  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. function isRecursive(path, resolved, state) {
  46. if (state.options.useRealPaths)
  47. return isRecursiveUsingRealPaths(resolved, state);
  48. let parent = (0, path_1.dirname)(path);
  49. let depth = 1;
  50. while (parent !== state.root && depth < 2) {
  51. const resolvedPath = state.symlinks.get(parent);
  52. const isSameRoot = !!resolvedPath &&
  53. (resolvedPath === resolved ||
  54. resolvedPath.startsWith(resolved) ||
  55. resolved.startsWith(resolvedPath));
  56. if (isSameRoot)
  57. depth++;
  58. else
  59. parent = (0, path_1.dirname)(parent);
  60. }
  61. state.symlinks.set(path, resolved);
  62. return depth > 1;
  63. }
  64. function isRecursiveUsingRealPaths(resolved, state) {
  65. return state.visited.includes(resolved + state.options.pathSeparator);
  66. }