Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite2b24f8

Browse files
committed
util: addgetCwdSafe internal util fn
This function was first implemented in #46826, but at some pointof the PR implementation this fn was no longer related to the PR.Refs:nodejs/node#46826 (comment)PR-URL:nodejs/node#48434Backport-PR-URL:nodejs/node#50669Reviewed-By: Jacob Smith <jacob@frende.me>Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent54d64e0 commite2b24f8

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

‎graal-nodejs/lib/internal/modules/esm/resolve.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const experimentalNetworkImports =
3838
getOptionValue('--experimental-network-imports');
3939
constinputTypeFlag=getOptionValue('--input-type');
4040
const{URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL}=require('internal/url');
41+
const{ getCWDURL}=require('internal/util');
4142
const{canParse:URLCanParse}=internalBinding('url');
4243
const{
4344
ERR_INPUT_TYPE_NOT_ALLOWED,
@@ -1175,7 +1176,7 @@ function defaultResolve(specifier, context = {}) {
11751176

11761177
constisMain=parentURL===undefined;
11771178
if(isMain){
1178-
parentURL=pathToFileURL(`${process.cwd()}/`).href;
1179+
parentURL=getCWDURL().href;
11791180

11801181
// This is the initial entry point to the program, and --input-type has
11811182
// been passed as an option; but --input-type can only be used with

‎graal-nodejs/lib/internal/modules/esm/utils.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616
loadPreloadModules,
1717
initializeFrozenIntrinsics,
1818
}=require('internal/process/pre_execution');
19-
const{pathToFileURL}=require('internal/url');
19+
const{getCWDURL}=require('internal/util');
2020
const{
2121
setImportModuleDynamicallyCallback,
2222
setInitializeImportMetaObjectCallback,
@@ -146,15 +146,6 @@ function isLoaderWorker() {
146146
asyncfunctioninitializeHooks(){
147147
constcustomLoaderURLs=getOptionValue('--experimental-loader');
148148

149-
letcwd;
150-
try{
151-
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
152-
cwd=process.cwd()+'/';
153-
}catch{
154-
cwd='/';
155-
}
156-
157-
158149
const{ Hooks}=require('internal/modules/esm/hooks');
159150
constesmLoader=require('internal/process/esm_loader').esmLoader;
160151

@@ -171,7 +162,7 @@ async function initializeHooks() {
171162
loadPreloadModules();
172163
initializeFrozenIntrinsics();
173164

174-
constparentURL=pathToFileURL(cwd).href;
165+
constparentURL=getCWDURL().href;
175166
for(leti=0;i<customLoaderURLs.length;i++){
176167
awaithooks.register(
177168
customLoaderURLs[i],

‎graal-nodejs/lib/internal/process/esm_loader.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const { getOptionValue } = require('internal/options');
99
const{
1010
hasUncaughtExceptionCaptureCallback,
1111
}=require('internal/process/execution');
12-
const{ pathToFileURL}=require('internal/url');
13-
const{ kEmptyObject}=require('internal/util');
12+
const{ kEmptyObject, getCWDURL}=require('internal/util');
1413

1514
letesmLoader;
1615

@@ -23,14 +22,7 @@ module.exports = {
2322
try{
2423
constuserImports=getOptionValue('--import');
2524
if(userImports.length>0){
26-
letcwd;
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-
constparentURL=pathToFileURL(cwd).href;
25+
constparentURL=getCWDURL().href;
3426
awaitSafePromiseAllReturnVoid(userImports,(specifier)=>esmLoader.import(
3527
specifier,
3628
parentURL,

‎graal-nodejs/lib/internal/util.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,36 @@ function getConstructorOf(obj) {
357357
returnnull;
358358
}
359359

360+
letcachedURL;
361+
letcachedCWD;
362+
363+
/**
364+
* Get the current working directory while accounting for the possibility that it has been deleted.
365+
* `process.cwd()` can fail if the parent directory is deleted while the process runs.
366+
*@returns {URL} The current working directory or the volume root if it cannot be determined.
367+
*/
368+
functiongetCWDURL(){
369+
const{ sep}=require('path');
370+
const{ pathToFileURL}=require('internal/url');
371+
372+
letcwd;
373+
374+
try{
375+
// The implementation of `process.cwd()` already uses proper cache when it can.
376+
// It's a relatively cheap call performance-wise for the most common use case.
377+
cwd=process.cwd();
378+
}catch{
379+
cachedURL??=pathToFileURL(sep);
380+
}
381+
382+
if(cwd!=null&&cwd!==cachedCWD){
383+
cachedURL=pathToFileURL(cwd+sep);
384+
cachedCWD=cwd;
385+
}
386+
387+
returncachedURL;
388+
}
389+
360390
functiongetSystemErrorName(err){
361391
constentry=uvErrmapGet(err);
362392
returnentry ?entry[0] :`Unknown system error${err}`;
@@ -784,6 +814,7 @@ module.exports = {
784814
filterDuplicateStrings,
785815
filterOwnProperties,
786816
getConstructorOf,
817+
getCWDURL,
787818
getInternalGlobal,
788819
getSystemErrorMap,
789820
getSystemErrorName,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp