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