diff options
author | Derek Lewis <DerekNonGeneric@inf.is> | 2020-08-03 00:27:51 +0300 |
---|---|---|
committer | Node.js GitHub Bot <github-bot@iojs.org> | 2020-08-15 05:22:13 +0300 |
commit | 0347574834ab1a0714c2c97bf6bb07592556abd8 (patch) | |
tree | e29e1a297ef852f88da312e286e84fcb068d71d2 /lib/internal/modules | |
parent | 5f78dea1a42eba373df57beac18a252aad74df65 (diff) |
module: fix check for package.json at volume root
This patch converts the "read package scope" algorithm's while loop
into a do-while loop enabling items at the filesystem root dir to
be considered within the scope of a sibling package.json also at the
filesystem root dir.
Fixes: https://github.com/nodejs/node/issues/33438
Co-authored-by: Guy Bedford <guybedford@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/34595
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Mary Marchini <oss@mmarchini.me>
Diffstat (limited to 'lib/internal/modules')
-rw-r--r-- | lib/internal/modules/cjs/loader.js | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 9aad5318d71..845f8d8f9aa 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -46,6 +46,9 @@ const { SafeMap, SafeWeakMap, String, + StringPrototypeEndsWith, + StringPrototypeIndexOf, + StringPrototypeLastIndexOf, StringPrototypeMatch, StringPrototypeSlice, StringPrototypeStartsWith, @@ -64,6 +67,7 @@ const assert = require('internal/assert'); const fs = require('fs'); const internalFS = require('internal/fs/utils'); const path = require('path'); +const { sep } = path; const { emitWarningSync } = require('internal/process/warning'); const { internalModuleStat } = internalBinding('fs'); const packageJsonReader = require('internal/modules/package_json_reader'); @@ -296,20 +300,19 @@ function readPackage(requestPath) { } function readPackageScope(checkPath) { - const rootSeparatorIndex = checkPath.indexOf(path.sep); + const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep); let separatorIndex; - while ( - (separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex - ) { - checkPath = checkPath.slice(0, separatorIndex); - if (checkPath.endsWith(path.sep + 'node_modules')) + do { + separatorIndex = StringPrototypeLastIndexOf(checkPath, sep); + checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex); + if (StringPrototypeEndsWith(checkPath, sep + 'node_modules')) return false; - const pjson = readPackage(checkPath); + const pjson = readPackage(checkPath + sep); if (pjson) return { + data: pjson, path: checkPath, - data: pjson }; - } + } while (separatorIndex > rootSeparatorIndex); return false; } |