| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1287d5b commit 7c2060c
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,7 @@ const experimentalNetworkImports = | |||
| 37 | 37 | getOptionValue('--experimental-network-imports'); | |
| 38 | 38 | const typeFlag = getOptionValue('--input-type'); | |
| 39 | 39 | const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url'); | |
| 40 | + const { getCWDURL } = require('internal/util'); | ||
| 40 | 41 | const { canParse: URLCanParse } = internalBinding('url'); | |
| 41 | 42 | const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs'); | |
| 42 | 43 | const { | |
@@ -1017,7 +1018,7 @@ function defaultResolve(specifier, context = {}) { | |||
| 1017 | 1018 | ||
| 1018 | 1019 | const isMain = parentURL === undefined; | |
| 1019 | 1020 | if (isMain) { | |
| 1020 | - parentURL = pathToFileURL(`${process.cwd()}/`).href; | ||
| 1021 | + parentURL = getCWDURL().href; | ||
| 1021 | 1022 | ||
| 1022 | 1023 | // This is the initial entry point to the program, and --input-type has | |
| 1023 | 1024 | // been passed as an option; but --input-type can only be used with | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ const { | |||
| 16 | 16 | loadPreloadModules, | |
| 17 | 17 | initializeFrozenIntrinsics, | |
| 18 | 18 | } = require('internal/process/pre_execution'); | |
| 19 | - const { pathToFileURL } = require('internal/url'); | ||
| 19 | + const { getCWDURL } = require('internal/util'); | ||
| 20 | 20 | const { | |
| 21 | 21 | setImportModuleDynamicallyCallback, | |
| 22 | 22 | setInitializeImportMetaObjectCallback, | |
@@ -112,15 +112,6 @@ function isLoaderWorker() { | |||
| 112 | 112 | async function initializeHooks() { | |
| 113 | 113 | const customLoaderURLs = getOptionValue('--experimental-loader'); | |
| 114 | 114 | ||
| 115 | - let cwd; | ||
| 116 | - try { | ||
| 117 | - // `process.cwd()` can fail if the parent directory is deleted while the process runs. | ||
| 118 | - cwd = process.cwd() + '/'; | ||
| 119 | - } catch { | ||
| 120 | - cwd = '/'; | ||
| 121 | - } | ||
| 122 | - | ||
| 123 | - | ||
| 124 | 115 | const { Hooks } = require('internal/modules/esm/hooks'); | |
| 125 | 116 | const esmLoader = require('internal/process/esm_loader').esmLoader; | |
| 126 | 117 | ||
@@ -137,7 +128,7 @@ async function initializeHooks() { | |||
| 137 | 128 | loadPreloadModules(); | |
| 138 | 129 | initializeFrozenIntrinsics(); | |
| 139 | 130 | ||
| 140 | - const parentURL = pathToFileURL(cwd).href; | ||
| 131 | + const parentURL = getCWDURL().href; | ||
| 141 | 132 | for (let i = 0; i < customLoaderURLs.length; i++) { | |
| 142 | 133 | await hooks.register( | |
| 143 | 134 | customLoaderURLs[i], | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,7 @@ const { getOptionValue } = require('internal/options'); | |||
| 9 | 9 | const { | |
| 10 | 10 | hasUncaughtExceptionCaptureCallback, | |
| 11 | 11 | } = require('internal/process/execution'); | |
| 12 | - const { pathToFileURL } = require('internal/url'); | ||
| 13 | - const { kEmptyObject } = require('internal/util'); | ||
| 12 | + const { kEmptyObject, getCWDURL } = require('internal/util'); | ||
| 14 | 13 | ||
| 15 | 14 | let esmLoader; | |
| 16 | 15 | ||
@@ -23,14 +22,7 @@ module.exports = { | |||
| 23 | 22 | try { | |
| 24 | 23 | const userImports = getOptionValue('--import'); | |
| 25 | 24 | if (userImports.length > 0) { | |
| 26 | - let cwd; | ||
| 27 | - try { | ||
| 28 | - // `process.cwd()` can fail if the parent directory is deleted while the process runs. | ||
| 29 | - cwd = process.cwd() + '/'; | ||
| 30 | - } catch { | ||
| 31 | - cwd = '/'; | ||
| 32 | - } | ||
| 33 | - const parentURL = pathToFileURL(cwd).href; | ||
| 25 | + const parentURL = getCWDURL().href; | ||
| 34 | 26 | await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import( | |
| 35 | 27 | specifier, | |
| 36 | 28 | parentURL, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -358,6 +358,36 @@ function getConstructorOf(obj) { | |||
| 358 | 358 | return null; | |
| 359 | 359 | } | |
| 360 | 360 | ||
| 361 | + let cachedURL; | ||
| 362 | + let cachedCWD; | ||
| 363 | + | ||
| 364 | + /** | ||
| 365 | + * Get the current working directory while accounting for the possibility that it has been deleted. | ||
| 366 | + * `process.cwd()` can fail if the parent directory is deleted while the process runs. | ||
| 367 | + * @returns {URL} The current working directory or the volume root if it cannot be determined. | ||
| 368 | + */ | ||
| 369 | + function getCWDURL() { | ||
| 370 | + const { sep } = require('path'); | ||
| 371 | + const { pathToFileURL } = require('internal/url'); | ||
| 372 | + | ||
| 373 | + let cwd; | ||
| 374 | + | ||
| 375 | + try { | ||
| 376 | + // The implementation of `process.cwd()` already uses proper cache when it can. | ||
| 377 | + // It's a relatively cheap call performance-wise for the most common use case. | ||
| 378 | + cwd = process.cwd(); | ||
| 379 | + } catch { | ||
| 380 | + cachedURL ??= pathToFileURL(sep); | ||
| 381 | + } | ||
| 382 | + | ||
| 383 | + if (cwd != null && cwd !== cachedCWD) { | ||
| 384 | + cachedURL = pathToFileURL(cwd + sep); | ||
| 385 | + cachedCWD = cwd; | ||
| 386 | + } | ||
| 387 | + | ||
| 388 | + return cachedURL; | ||
| 389 | + } | ||
| 390 | + | ||
| 361 | 391 | function getSystemErrorName(err) { | |
| 362 | 392 | const entry = uvErrmapGet(err); | |
| 363 | 393 | return entry ? entry[0] : `Unknown system error ${err}`; | |
@@ -850,6 +880,7 @@ module.exports = { | |||
| 850 | 880 | filterDuplicateStrings, | |
| 851 | 881 | filterOwnProperties, | |
| 852 | 882 | getConstructorOf, | |
| 883 | + getCWDURL, | ||
| 853 | 884 | getInternalGlobal, | |
| 854 | 885 | getSystemErrorMap, | |
| 855 | 886 | getSystemErrorName, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments