| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9757989 commit 8c55f31
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,7 +56,6 @@ const { | |||
| 56 | 56 | StringPrototypeCharAt, | |
| 57 | 57 | StringPrototypeCharCodeAt, | |
| 58 | 58 | StringPrototypeEndsWith, | |
| 59 | - StringPrototypeLastIndexOf, | ||
| 60 | 59 | StringPrototypeIndexOf, | |
| 61 | 60 | StringPrototypeRepeat, | |
| 62 | 61 | StringPrototypeSlice, | |
@@ -69,7 +68,7 @@ const cjsParseCache = new SafeWeakMap(); | |||
| 69 | 68 | ||
| 70 | 69 | // Set first due to cycle with ESM loader functions. | |
| 71 | 70 | module.exports = { | |
| 72 | - wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache, | ||
| 71 | + wrapSafe, Module, cjsParseCache, | ||
| 73 | 72 | get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }, | |
| 74 | 73 | initializeCJS, | |
| 75 | 74 | }; | |
@@ -89,9 +88,7 @@ const { | |||
| 89 | 88 | const { internalCompileFunction } = require('internal/vm'); | |
| 90 | 89 | const assert = require('internal/assert'); | |
| 91 | 90 | const fs = require('fs'); | |
| 92 | - const internalFS = require('internal/fs/utils'); | ||
| 93 | 91 | const path = require('path'); | |
| 94 | - const { sep } = path; | ||
| 95 | 92 | const { internalModuleStat } = internalBinding('fs'); | |
| 96 | 93 | const { safeGetenv } = internalBinding('credentials'); | |
| 97 | 94 | const { | |
@@ -107,6 +104,7 @@ const { | |||
| 107 | 104 | makeRequireFunction, | |
| 108 | 105 | normalizeReferrerURL, | |
| 109 | 106 | stripBOM, | |
| 107 | + toRealPath, | ||
| 110 | 108 | } = require('internal/modules/helpers'); | |
| 111 | 109 | const packageJsonReader = require('internal/modules/package_json_reader'); | |
| 112 | 110 | const { getOptionValue, getEmbedderOptions } = require('internal/options'); | |
@@ -402,15 +400,7 @@ function initializeCJS() { | |||
| 402 | 400 | // -> a.<ext> | |
| 403 | 401 | // -> a/index.<ext> | |
| 404 | 402 | ||
| 405 | - /** | ||
| 406 | - * @param {string} requestPath | ||
| 407 | - * @return {PackageConfig} | ||
| 408 | - */ | ||
| 409 | - function readPackage(requestPath) { | ||
| 410 | - return packageJsonReader.read(path.resolve(requestPath, 'package.json')); | ||
| 411 | - } | ||
| 412 | - | ||
| 413 | - let _readPackage = readPackage; | ||
| 403 | + let _readPackage = packageJsonReader.readPackage; | ||
| 414 | 404 | ObjectDefineProperty(Module, '_readPackage', { | |
| 415 | 405 | __proto__: null, | |
| 416 | 406 | get() { return _readPackage; }, | |
@@ -422,31 +412,6 @@ ObjectDefineProperty(Module, '_readPackage', { | |||
| 422 | 412 | configurable: true, | |
| 423 | 413 | }); | |
| 424 | 414 | ||
| 425 | - /** | ||
| 426 | - * Get the nearest parent package.json file from a given path. | ||
| 427 | - * Return the package.json data and the path to the package.json file, or false. | ||
| 428 | - * @param {string} checkPath The path to start searching from. | ||
| 429 | - */ | ||
| 430 | - function readPackageScope(checkPath) { | ||
| 431 | - const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep); | ||
| 432 | - let separatorIndex; | ||
| 433 | - do { | ||
| 434 | - separatorIndex = StringPrototypeLastIndexOf(checkPath, sep); | ||
| 435 | - checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex); | ||
| 436 | - if (StringPrototypeEndsWith(checkPath, sep + 'node_modules')) { | ||
| 437 | - return false; | ||
| 438 | - } | ||
| 439 | - const pjson = _readPackage(checkPath + sep); | ||
| 440 | - if (pjson.exists) { | ||
| 441 | - return { | ||
| 442 | - data: pjson, | ||
| 443 | - path: checkPath, | ||
| 444 | - }; | ||
| 445 | - } | ||
| 446 | - } while (separatorIndex > rootSeparatorIndex); | ||
| 447 | - return false; | ||
| 448 | - } | ||
| 449 | - | ||
| 450 | 415 | /** | |
| 451 | 416 | * Try to load a specifier as a package. | |
| 452 | 417 | * @param {string} requestPath The path to what we are trying to load | |
@@ -491,14 +456,6 @@ function tryPackage(requestPath, exts, isMain, originalPath) { | |||
| 491 | 456 | return actual; | |
| 492 | 457 | } | |
| 493 | 458 | ||
| 494 | - /** | ||
| 495 | - * Cache for storing resolved real paths of modules. | ||
| 496 | - * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths. | ||
| 497 | - * Set to an empty Map to reset. | ||
| 498 | - * @type {Map<string, string>} | ||
| 499 | - */ | ||
| 500 | - const realpathCache = new SafeMap(); | ||
| 501 | - | ||
| 502 | 459 | /** | |
| 503 | 460 | * Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false, keep symlinks | |
| 504 | 461 | * intact, otherwise resolve to the absolute realpath. | |
@@ -514,17 +471,6 @@ function tryFile(requestPath, isMain) { | |||
| 514 | 471 | return toRealPath(requestPath); | |
| 515 | 472 | } | |
| 516 | 473 | ||
| 517 | - | ||
| 518 | - /** | ||
| 519 | - * Resolves the path of a given `require` specifier, following symlinks. | ||
| 520 | - * @param {string} requestPath The `require` specifier | ||
| 521 | - */ | ||
| 522 | - function toRealPath(requestPath) { | ||
| 523 | - return fs.realpathSync(requestPath, { | ||
| 524 | - [internalFS.realpathCacheKey]: realpathCache, | ||
| 525 | - }); | ||
| 526 | - } | ||
| 527 | - | ||
| 528 | 474 | /** | |
| 529 | 475 | * Given a path, check if the file exists with any of the set extensions. | |
| 530 | 476 | * @param {string} basePath The path and filename without extension | |
@@ -586,7 +532,7 @@ function trySelfParentPath(parent) { | |||
| 586 | 532 | function trySelf(parentPath, request) { | |
| 587 | 533 | if (!parentPath) { return false; } | |
| 588 | 534 | ||
| 589 | - const { data: pkg, path: pkgPath } = readPackageScope(parentPath); | ||
| 535 | + const { data: pkg, path: pkgPath } = packageJsonReader.readPackageScope(parentPath); | ||
| 590 | 536 | if (!pkg || pkg.exports == null || pkg.name === undefined) { | |
| 591 | 537 | return false; | |
| 592 | 538 | } | |
@@ -1143,7 +1089,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { | |||
| 1143 | 1089 | ||
| 1144 | 1090 | if (request[0] === '#' && (parent?.filename || parent?.id === '<repl>')) { | |
| 1145 | 1091 | const parentPath = parent?.filename ?? process.cwd() + path.sep; | |
| 1146 | - const pkg = readPackageScope(parentPath) || { __proto__: null }; | ||
| 1092 | + const pkg = packageJsonReader.readPackageScope(parentPath) || { __proto__: null }; | ||
| 1147 | 1093 | if (pkg.data?.imports != null) { | |
| 1148 | 1094 | try { | |
| 1149 | 1095 | const { packageImportsResolve } = require('internal/modules/esm/resolve'); | |
@@ -1431,7 +1377,7 @@ Module._extensions['.js'] = function(module, filename) { | |||
| 1431 | 1377 | content = fs.readFileSync(filename, 'utf8'); | |
| 1432 | 1378 | } | |
| 1433 | 1379 | if (StringPrototypeEndsWith(filename, '.js')) { | |
| 1434 | - const pkg = readPackageScope(filename) || { __proto__: null }; | ||
| 1380 | + const pkg = packageJsonReader.readPackageScope(filename) || { __proto__: null }; | ||
| 1435 | 1381 | // Function require shouldn't be used in ES modules. | |
| 1436 | 1382 | if (pkg.data?.type === 'module') { | |
| 1437 | 1383 | const parent = moduleParentCache.get(module); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,8 @@ const { | |||
| 21 | 21 | const { BuiltinModule } = require('internal/bootstrap/realm'); | |
| 22 | 22 | ||
| 23 | 23 | const { validateString } = require('internal/validators'); | |
| 24 | + const fs = require('fs'); // Import all of `fs` so that it can be monkey-patched. | ||
| 25 | + const internalFS = require('internal/fs/utils'); | ||
| 24 | 26 | const path = require('path'); | |
| 25 | 27 | const { pathToFileURL, fileURLToPath, URL } = require('internal/url'); | |
| 26 | 28 | ||
@@ -39,6 +41,23 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | |||
| 39 | 41 | ||
| 40 | 42 | /** @typedef {import('internal/modules/cjs/loader.js').Module} Module */ | |
| 41 | 43 | ||
| 44 | + /** | ||
| 45 | + * Cache for storing resolved real paths of modules. | ||
| 46 | + * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths. | ||
| 47 | + * Set to an empty Map to reset. | ||
| 48 | + * @type {Map<string, string>} | ||
| 49 | + */ | ||
| 50 | + const realpathCache = new SafeMap(); | ||
| 51 | + /** | ||
| 52 | + * Resolves the path of a given `require` specifier, following symlinks. | ||
| 53 | + * @param {string} requestPath The `require` specifier | ||
| 54 | + */ | ||
| 55 | + function toRealPath(requestPath) { | ||
| 56 | + return fs.realpathSync(requestPath, { | ||
| 57 | + [internalFS.realpathCacheKey]: realpathCache, | ||
| 58 | + }); | ||
| 59 | + } | ||
| 60 | + | ||
| 42 | 61 | /** @type {Set<string>} */ | |
| 43 | 62 | let cjsConditions; | |
| 44 | 63 | /** | |
@@ -310,4 +329,5 @@ module.exports = { | |||
| 310 | 329 | makeRequireFunction, | |
| 311 | 330 | normalizeReferrerURL, | |
| 312 | 331 | stripBOM, | |
| 332 | + toRealPath, | ||
| 313 | 333 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,12 +4,16 @@ const { | |||
| 4 | 4 | JSONParse, | |
| 5 | 5 | ObjectPrototypeHasOwnProperty, | |
| 6 | 6 | SafeMap, | |
| 7 | + StringPrototypeEndsWith, | ||
| 8 | + StringPrototypeIndexOf, | ||
| 9 | + StringPrototypeLastIndexOf, | ||
| 10 | + StringPrototypeSlice, | ||
| 7 | 11 | } = primordials; | |
| 8 | 12 | const { | |
| 9 | 13 | ERR_INVALID_PACKAGE_CONFIG, | |
| 10 | 14 | } = require('internal/errors').codes; | |
| 11 | 15 | const { internalModuleReadJSON } = internalBinding('fs'); | |
| 12 | - const { toNamespacedPath } = require('path'); | ||
| 16 | + const { resolve, sep, toNamespacedPath } = require('path'); | ||
| 13 | 17 | const { kEmptyObject } = require('internal/util'); | |
| 14 | 18 | ||
| 15 | 19 | const { fileURLToPath, pathToFileURL } = require('internal/url'); | |
@@ -128,4 +132,41 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) { | |||
| 128 | 132 | return result; | |
| 129 | 133 | } | |
| 130 | 134 | ||
| 131 | - module.exports = { read }; | ||
| 135 | + /** | ||
| 136 | + * @param {string} requestPath | ||
| 137 | + * @return {PackageConfig} | ||
| 138 | + */ | ||
| 139 | + function readPackage(requestPath) { | ||
| 140 | + return read(resolve(requestPath, 'package.json')); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + /** | ||
| 144 | + * Get the nearest parent package.json file from a given path. | ||
| 145 | + * Return the package.json data and the path to the package.json file, or false. | ||
| 146 | + * @param {string} checkPath The path to start searching from. | ||
| 147 | + */ | ||
| 148 | + function readPackageScope(checkPath) { | ||
| 149 | + const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep); | ||
| 150 | + let separatorIndex; | ||
| 151 | + do { | ||
| 152 | + separatorIndex = StringPrototypeLastIndexOf(checkPath, sep); | ||
| 153 | + checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex); | ||
| 154 | + if (StringPrototypeEndsWith(checkPath, sep + 'node_modules')) { | ||
| 155 | + return false; | ||
| 156 | + } | ||
| 157 | + const pjson = readPackage(checkPath + sep); | ||
| 158 | + if (pjson.exists) { | ||
| 159 | + return { | ||
| 160 | + data: pjson, | ||
| 161 | + path: checkPath, | ||
| 162 | + }; | ||
| 163 | + } | ||
| 164 | + } while (separatorIndex > rootSeparatorIndex); | ||
| 165 | + return false; | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + module.exports = { | ||
| 169 | + read, | ||
| 170 | + readPackage, | ||
| 171 | + readPackageScope, | ||
| 172 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,12 +16,13 @@ function resolveMainPath(main) { | |||
| 16 | 16 | // Note extension resolution for the main entry point can be deprecated in a | |
| 17 | 17 | // future major. | |
| 18 | 18 | // Module._findPath is monkey-patchable here. | |
| 19 | - const { Module, toRealPath } = require('internal/modules/cjs/loader'); | ||
| 19 | + const { Module } = require('internal/modules/cjs/loader'); | ||
| 20 | 20 | let mainPath = Module._findPath(path.resolve(main), null, true); | |
| 21 | 21 | if (!mainPath) { return; } | |
| 22 | 22 | ||
| 23 | 23 | const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); | |
| 24 | 24 | if (!preserveSymlinksMain) { | |
| 25 | + const { toRealPath } = require('internal/modules/helpers'); | ||
| 25 | 26 | mainPath = toRealPath(mainPath); | |
| 26 | 27 | } | |
| 27 | 28 | ||
@@ -51,10 +52,11 @@ function shouldUseESMLoader(mainPath) { | |||
| 51 | 52 | if (esModuleSpecifierResolution === 'node') { | |
| 52 | 53 | return true; | |
| 53 | 54 | } | |
| 54 | - const { readPackageScope } = require('internal/modules/cjs/loader'); | ||
| 55 | 55 | // Determine the module format of the main | |
| 56 | 56 | if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs')) { return true; } | |
| 57 | 57 | if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs')) { return false; } | |
| 58 | + | ||
| 59 | + const { readPackageScope } = require('internal/modules/package_json_reader'); | ||
| 58 | 60 | const pkg = readPackageScope(mainPath); | |
| 59 | 61 | return pkg && pkg.data.type === 'module'; | |
| 60 | 62 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,11 +4,11 @@ throw new Error('Should include grayed stack trace') | |||
| 4 | 4 | ||
| 5 | 5 | Error: Should include grayed stack trace | |
| 6 | 6 | at Object.<anonymous> [90m(/[39mtest*force_colors.js:1:7[90m)[39m | |
| 7 | - [90m at Module._compile (node:internal*modules*cjs*loader:1244:14)[39m | ||
| 8 | - [90m at Module._extensions..js (node:internal*modules*cjs*loader:1298:10)[39m | ||
| 9 | - [90m at Module.load (node:internal*modules*cjs*loader:1101:32)[39m | ||
| 10 | - [90m at Module._load (node:internal*modules*cjs*loader:942:12)[39m | ||
| 11 | - [90m at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:88:12)[39m | ||
| 7 | + [90m at Module._compile (node:internal*modules*cjs*loader:1356:14)[39m | ||
| 8 | + [90m at Module._extensions..js (node:internal*modules*cjs*loader:1414:10)[39m | ||
| 9 | + [90m at Module.load (node:internal*modules*cjs*loader:1197:32)[39m | ||
| 10 | + [90m at Module._load (node:internal*modules*cjs*loader:1013:12)[39m | ||
| 11 | + [90m at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:109:12)[39m | ||
| 12 | 12 | [90m at node:internal*main*run_main_module:23:47[39m | |
| 13 | 13 | ||
| 14 | 14 | Node.js * | |
| Back | FazBrowse Home | New Git URL |
0 commit comments