diff --git a/lib/async.js b/lib/async.js index 8ab32a73..85a467fe 100644 --- a/lib/async.js +++ b/lib/async.js @@ -25,6 +25,17 @@ var defaultIsDir = function isDirectory(dir, cb) { }); }; +var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts, cb) { + if (opts && opts.preserveSymlinks === false) { + fs.realpath(x, function (realPathErr, realPath) { + if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr); + else cb(null, realPathErr ? x : realPath); + }); + } else { + cb(null, x); + } +}; + module.exports = function resolve(x, options, callback) { var cb = callback; var opts = options; @@ -54,14 +65,14 @@ module.exports = function resolve(x, options, callback) { // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory var absoluteStart = path.resolve(basedir); - if (opts.preserveSymlinks === false) { - fs.realpath(absoluteStart, function (realPathErr, realStart) { - if (realPathErr && realPathErr.code !== 'ENOENT') cb(err); - else init(realPathErr ? absoluteStart : realStart); - }); - } else { - init(absoluteStart); - } + maybeUnwrapSymlink( + absoluteStart, + opts, + function (err, realStart) { + if (err) cb(err); + else init(realStart); + } + ); var res; function init(basedir) { diff --git a/lib/sync.js b/lib/sync.js index 7c315861..61f68139 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -25,6 +25,19 @@ var defaultIsDir = function isDirectory(dir) { return stat.isDirectory(); }; +var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts) { + if (opts && opts.preserveSymlinks === false) { + try { + return fs.realpathSync(x); + } catch (realPathErr) { + if (realPathErr.code !== 'ENOENT') { + throw realPathErr; + } + } + } + return x; +}; + module.exports = function (x, options) { if (typeof x !== 'string') { throw new TypeError('Path must be a string.'); @@ -42,17 +55,7 @@ module.exports = function (x, options) { opts.paths = opts.paths || []; // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory - var absoluteStart = path.resolve(basedir); - - if (opts.preserveSymlinks === false) { - try { - absoluteStart = fs.realpathSync(absoluteStart); - } catch (realPathErr) { - if (realPathErr.code !== 'ENOENT') { - throw realPathErr; - } - } - } + var absoluteStart = maybeUnwrapSymlink(path.resolve(basedir), opts); if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) { var res = path.resolve(absoluteStart, x);