LinkResolver.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const fs = require("fs");
  7. const path = require("path");
  8. // macOS, Linux, and Windows all rely on these errors
  9. const EXPECTED_ERRORS = new Set(["EINVAL", "ENOENT"]);
  10. // On Windows there is also this error in some cases
  11. if (process.platform === "win32") EXPECTED_ERRORS.add("UNKNOWN");
  12. class LinkResolver {
  13. constructor() {
  14. /** @type {Map<string, readonly string[]>} */
  15. this.cache = new Map();
  16. }
  17. /**
  18. * @param {string} file path to file or directory
  19. * @returns {readonly string[]} array of file and all symlinks contributed in the resolving process (first item is the resolved file)
  20. */
  21. resolve(file) {
  22. const cacheEntry = this.cache.get(file);
  23. if (cacheEntry !== undefined) {
  24. return cacheEntry;
  25. }
  26. const parent = path.dirname(file);
  27. if (parent === file) {
  28. // At root of filesystem there can't be a link
  29. const result = Object.freeze([file]);
  30. this.cache.set(file, result);
  31. return result;
  32. }
  33. // resolve the parent directory to find links there and get the real path
  34. const parentResolved = this.resolve(parent);
  35. let realFile = file;
  36. // is the parent directory really somewhere else?
  37. if (parentResolved[0] !== parent) {
  38. // get the real location of file
  39. const basename = path.basename(file);
  40. realFile = path.resolve(parentResolved[0], basename);
  41. }
  42. // try to read the link content
  43. try {
  44. const linkContent = fs.readlinkSync(realFile);
  45. // resolve the link content relative to the parent directory
  46. const resolvedLink = path.resolve(parentResolved[0], linkContent);
  47. // recursive resolve the link content for more links in the structure
  48. const linkResolved = this.resolve(resolvedLink);
  49. // merge parent and link resolve results
  50. let result;
  51. if (linkResolved.length > 1 && parentResolved.length > 1) {
  52. // when both contain links we need to duplicate them with a Set
  53. const resultSet = new Set(linkResolved);
  54. // add the link
  55. resultSet.add(realFile);
  56. // add all symlinks of the parent
  57. for (let i = 1; i < parentResolved.length; i++) {
  58. resultSet.add(parentResolved[i]);
  59. }
  60. result = Object.freeze([...resultSet]);
  61. } else if (parentResolved.length > 1) {
  62. // we have links in the parent but not for the link content location
  63. result = [...parentResolved];
  64. // eslint-disable-next-line prefer-destructuring
  65. result[0] = linkResolved[0];
  66. // add the link
  67. result.push(realFile);
  68. Object.freeze(result);
  69. } else if (linkResolved.length > 1) {
  70. // we can return the link content location result
  71. result = [...linkResolved];
  72. // add the link
  73. result.push(realFile);
  74. Object.freeze(result);
  75. } else {
  76. // neither link content location nor parent have links
  77. // this link is the only link here
  78. result = Object.freeze([
  79. // the resolve real location
  80. linkResolved[0],
  81. // add the link
  82. realFile,
  83. ]);
  84. }
  85. this.cache.set(file, result);
  86. return result;
  87. } catch (err) {
  88. if (
  89. /** @type {NodeJS.ErrnoException} */
  90. (err).code &&
  91. !EXPECTED_ERRORS.has(/** @type {NodeJS.ErrnoException} */ (err).code)
  92. ) {
  93. throw err;
  94. }
  95. // no link
  96. const result = [...parentResolved];
  97. result[0] = realFile;
  98. Object.freeze(result);
  99. this.cache.set(file, result);
  100. return result;
  101. }
  102. }
  103. }
  104. module.exports = LinkResolver;