| 1 |
"use strict"; |
| 2 |
|
| 3 |
const isCallable = require("es5-ext/object/is-callable") |
| 4 |
, isValue = require("es5-ext/object/is-value") |
| 5 |
, { dirname } = require("path") |
| 6 |
, { resolve } = require("path") |
| 7 |
, { readlink } = require("./readlink") |
| 8 |
, { realpath } = require("./realpath"); |
| 9 |
|
| 10 |
module.exports = function (path, options = {}, cb = null) { |
| 11 |
path = resolve(String(path)); |
| 12 |
if (!isValue(cb) && isCallable(options)) { |
| 13 |
cb = options; |
| 14 |
options = {}; |
| 15 |
} else { |
| 16 |
options = Object(options); |
| 17 |
} |
| 18 |
const expectedLinkPath = options.linkPath |
| 19 |
? resolve(dirname(path), String(options.linkPath)) |
| 20 |
: null; |
| 21 |
return readlink(path, {})( |
| 22 |
linkPath => { |
| 23 |
if (!linkPath) return null; |
| 24 |
if (!expectedLinkPath) return true; |
| 25 |
if (!options.recursive) return resolve(dirname(path), linkPath) === expectedLinkPath; |
| 26 |
return realpath(path, { loose: true })(finalLinkPath => { |
| 27 |
if (!finalLinkPath) return false; |
| 28 |
return finalLinkPath === expectedLinkPath; |
| 29 |
}); |
| 30 |
}, |
| 31 |
error => { |
| 32 |
if (error.code === "ENOENT") return null; |
| 33 |
if (error.code === "EINVAL") return false; |
| 34 |
if (error.code === "ENOTDIR") return null; |
| 35 |
if (error.code === "UNKNOWN") return null; |
| 36 |
throw error; |
| 37 |
} |
| 38 |
).cb(cb); |
| 39 |
}; |
| 40 |
module.exports.returnsPromise = true; |