Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Use realpath() for directory paths in findPath(). [closes #551]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Aug 6, 2018
1 parent c639f51 commit 9adbd37
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/module/internal/find-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright Node.js contributors. Released under MIT license:
// https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js

import { basename, dirname, resolve, sep } from "../../safe/path.js"

import CHAR_CODE from "../../constant/char-code.js"
import ENV from "../../constant/env.js"

Expand All @@ -16,7 +18,6 @@ import isMJS from "../../path/is-mjs.js"
import keys from "../../util/keys.js"
import readPackage from "./read-package.js"
import realpath from "../../fs/realpath.js"
import { resolve } from "../../safe/path.js"
import shared from "../../shared.js"
import statFast from "../../fs/stat-fast.js"
import statSync from "../../fs/stat-sync.js"
Expand All @@ -41,8 +42,6 @@ let resolveSymlinks = ! preserveSymlinks
let resolveSymlinksMain = ! preserveSymlinksMain

function findPath(request, paths, isMain, fields, exts) {
const cache = shared.memoize.moduleInternalFindPath

let cacheKey = request

if (paths) {
Expand All @@ -57,8 +56,11 @@ function findPath(request, paths, isMain, fields, exts) {
cacheKey += "\0" + (exts.length === 1 ? exts[0] : GenericArray.join(exts))
}

if (Reflect.has(cache, cacheKey)) {
return cache[cacheKey]
const cache = shared.memoize.moduleInternalFindPath
const cached = cache[cacheKey]

if (cached !== void 0) {
return cached
}

const isAbs = isAbsolute(request)
Expand Down Expand Up @@ -92,15 +94,30 @@ function findPath(request, paths, isMain, fields, exts) {
? resolveSymlinksMain
: resolveSymlinks

for (const curPath of paths) {
for (let curPath of paths) {
if (useRealpath) {
if (isAbs) {
curPath = dirname(request)
request = basename(request)
}

curPath = realpath(curPath)
}

if (curPath &&
statFast(curPath) !== 1) {
continue
}

const thePath = isAbs
? request
: resolve(curPath, request)
let thePath

if (isAbs) {
thePath = useRealpath
? curPath + sep + request
: request
} else {
thePath = resolve(curPath, request)
}

let isSymLink = false
let rc = -1
Expand Down Expand Up @@ -158,6 +175,10 @@ function findPath(request, paths, isMain, fields, exts) {
fields = mainFields
}

if (useRealpath) {
thePath = realpath(thePath)
}

filename =
tryPackage(thePath, fields, exts, isMain) ||
tryExtensions(resolve(thePath, "index"), exts, isMain)
Expand Down

0 comments on commit 9adbd37

Please sign in to comment.