From 4f36620ac0aab131f24297b4a58fd87da47a09e7 Mon Sep 17 00:00:00 2001 From: Keeley Hammond Date: Mon, 19 Feb 2024 12:10:05 -0800 Subject: [PATCH] chore: update patch harder --- ...g_c_calls_of_esm_legacy_main_resolve.patch | 320 ++++++++++++++---- 1 file changed, 259 insertions(+), 61 deletions(-) diff --git a/patches/node/fix_revert_src_lb_reducing_c_calls_of_esm_legacy_main_resolve.patch b/patches/node/fix_revert_src_lb_reducing_c_calls_of_esm_legacy_main_resolve.patch index ced296ea53191..9bf0bf6b1a54f 100644 --- a/patches/node/fix_revert_src_lb_reducing_c_calls_of_esm_legacy_main_resolve.patch +++ b/patches/node/fix_revert_src_lb_reducing_c_calls_of_esm_legacy_main_resolve.patch @@ -1,6 +1,6 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: VerteDinde -Date: Sat, 17 Feb 2024 22:17:13 -0800 +From: Keeley Hammond +Date: Mon, 19 Feb 2024 12:05:42 -0800 Subject: fix: revert "src,lb: reducing C++ calls of esm legacy main resolve" This switch to native legacyMainResolve doesn't take asar into account, and can @@ -15,13 +15,13 @@ to recognize asar files. This reverts commit 9cf2e1f55b8446a7cde23699d00a3be73aa0c8f1. diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js -index 102165af37a42ca0394ec0d4efc21d3b03efe5eb..cb8341f7378f16d4c0cc5368047a893704147842 100644 +index ce8092b96aee8d09ff382110db4be62dcd760cce..303f6b57a08e18de1d78a14b19cfba852bdffa47 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -36,10 +36,9 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); - const inputTypeFlag = getOptionValue('--input-type'); + const typeFlag = getOptionValue('--input-type'); -const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url'); +const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url'); const { getCWDURL } = require('internal/util'); @@ -30,7 +30,7 @@ index 102165af37a42ca0394ec0d4efc21d3b03efe5eb..cb8341f7378f16d4c0cc5368047a8937 const { ERR_INPUT_TYPE_NOT_ALLOWED, ERR_INVALID_ARG_TYPE, -@@ -161,34 +160,13 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) { +@@ -136,34 +135,13 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) { const realpathCache = new SafeMap(); @@ -72,7 +72,7 @@ index 102165af37a42ca0394ec0d4efc21d3b03efe5eb..cb8341f7378f16d4c0cc5368047a8937 /** * Legacy CommonJS main resolution: -@@ -203,22 +181,44 @@ const legacyMainResolveExtensionsIndexes = { +@@ -178,22 +156,44 @@ const legacyMainResolveExtensionsIndexes = { * @returns {URL} */ function legacyMainResolve(packageJSONUrl, packageConfig, base) { @@ -132,8 +132,16 @@ index 102165af37a42ca0394ec0d4efc21d3b03efe5eb..cb8341f7378f16d4c0cc5368047a8937 } const encodedSepRegEx = /%2F|%5C/i; +@@ -1093,7 +1093,6 @@ module.exports = { + packageExportsResolve, + packageImportsResolve, + throwIfInvalidParentURL, +- legacyMainResolve, + }; + + // cycle diff --git a/src/node_file.cc b/src/node_file.cc -index f3d8c35d4963d84aef1cb2e15627a959215b8ab2..5b42f671f7a8f1814e086cd9855fff7edaa87851 100644 +index 59780dec1c4b6d157d2b04fea8c57cacce73ec3a..8f8629ed0b8cbc08a544211b63675ea0dcca1828 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -19,14 +19,11 @@ @@ -150,51 +158,212 @@ index f3d8c35d4963d84aef1cb2e15627a959215b8ab2..5b42f671f7a8f1814e086cd9855fff7e -#include "node_metadata.h" #include "node_process-inl.h" #include "node_stat_watcher.h" - #include "node_url.h" -@@ -2971,134 +2968,6 @@ constexpr std::array legacy_main_extensions = { - - } // namespace + #include "permission/permission.h" +@@ -3013,293 +3010,6 @@ static void Mkdtemp(const FunctionCallbackInfo& args) { + } + } +-static bool FileURLToPath( +- Environment* env, +- const ada::url_aggregator& file_url, +- /* The linter can't detect the assign for result_file_path +- So we need to ignore since it suggest to put const */ +- // NOLINTNEXTLINE(runtime/references) +- std::string& result_file_path) { +- if (file_url.type != ada::scheme::FILE) { +- env->isolate()->ThrowException(ERR_INVALID_URL_SCHEME(env->isolate())); +- +- return false; +- } +- +- std::string_view pathname = file_url.get_pathname(); +-#ifdef _WIN32 +- size_t first_percent = std::string::npos; +- size_t pathname_size = pathname.size(); +- std::string pathname_escaped_slash; +- +- for (size_t i = 0; i < pathname_size; i++) { +- if (pathname[i] == '/') { +- pathname_escaped_slash += '\\'; +- } else { +- pathname_escaped_slash += pathname[i]; +- } +- +- if (pathname[i] != '%') continue; +- +- if (first_percent == std::string::npos) { +- first_percent = i; +- } +- +- // just safe-guard against access the pathname +- // outside the bounds +- if ((i + 2) >= pathname_size) continue; +- +- char third = pathname[i + 2] | 0x20; +- +- bool is_slash = pathname[i + 1] == '2' && third == 102; +- bool is_forward_slash = pathname[i + 1] == '5' && third == 99; +- +- if (!is_slash && !is_forward_slash) continue; +- +- env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( +- env->isolate(), +- "File URL path must not include encoded \\ or / characters")); +- +- return false; +- } +- +- std::string_view hostname = file_url.get_hostname(); +- std::string decoded_pathname = ada::unicode::percent_decode( +- std::string_view(pathname_escaped_slash), first_percent); +- +- if (hostname.size() > 0) { +- // If hostname is set, then we have a UNC path +- // Pass the hostname through domainToUnicode just in case +- // it is an IDN using punycode encoding. We do not need to worry +- // about percent encoding because the URL parser will have +- // already taken care of that for us. Note that this only +- // causes IDNs with an appropriate `xn--` prefix to be decoded. +- result_file_path = +- "\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname; +- +- return true; +- } +- +- char letter = decoded_pathname[1] | 0x20; +- char sep = decoded_pathname[2]; +- +- // a..z A..Z +- if (letter < 'a' || letter > 'z' || sep != ':') { +- env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( +- env->isolate(), "File URL path must be absolute")); +- +- return false; +- } +- +- result_file_path = decoded_pathname.substr(1); +- +- return true; +-#else // _WIN32 +- std::string_view hostname = file_url.get_hostname(); +- +- if (hostname.size() > 0) { +- std::string error_message = +- std::string("File URL host must be \"localhost\" or empty on ") + +- std::string(per_process::metadata.platform); +- env->isolate()->ThrowException( +- ERR_INVALID_FILE_URL_HOST(env->isolate(), error_message.c_str())); +- +- return false; +- } +- +- size_t first_percent = std::string::npos; +- for (size_t i = 0; (i + 2) < pathname.size(); i++) { +- if (pathname[i] != '%') continue; +- +- if (first_percent == std::string::npos) { +- first_percent = i; +- } +- +- if (pathname[i + 1] == '2' && (pathname[i + 2] | 0x20) == 102) { +- env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( +- env->isolate(), +- "File URL path must not include encoded / characters")); +- +- return false; +- } +- } +- +- result_file_path = ada::unicode::percent_decode(pathname, first_percent); +- +- return true; +-#endif // _WIN32 +-} +- +-BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile( +- Environment* env, const std::string& file_path) { +- THROW_IF_INSUFFICIENT_PERMISSIONS( +- env, +- permission::PermissionScope::kFileSystemRead, +- file_path, +- BindingData::FilePathIsFileReturnType::kThrowInsufficientPermissions); +- +- uv_fs_t req; +- +- int rc = uv_fs_stat(env->event_loop(), &req, file_path.c_str(), nullptr); +- +- if (rc == 0) { +- const uv_stat_t* const s = static_cast(req.ptr); +- rc = !!(s->st_mode & S_IFDIR); +- } +- +- uv_fs_req_cleanup(&req); +- +- // rc is 0 if the path refers to a file +- if (rc == 0) return BindingData::FilePathIsFileReturnType::kIsFile; +- +- return BindingData::FilePathIsFileReturnType::kIsNotFile; +-} +- +-// the possible file extensions that should be tested +-// 0-6: when packageConfig.main is defined +-// 7-9: when packageConfig.main is NOT defined, +-// or when the previous case didn't found the file +-const std::array BindingData::legacy_main_extensions = { +- "", +- ".js", +- ".json", +- ".node", +- "/index.js", +- "/index.json", +- "/index.node", +- ".js", +- ".json", +- ".node"}; +- -void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { - CHECK_GE(args.Length(), 1); - CHECK(args[0]->IsString()); - - Environment* env = Environment::GetCurrent(args); -- auto isolate = env->isolate(); - -- Utf8Value utf8_package_json_url(isolate, args[0]); +- Utf8Value utf8_package_json_url(env->isolate(), args[0].As()); - auto package_json_url = - ada::parse(utf8_package_json_url.ToStringView()); - - if (!package_json_url) { -- THROW_ERR_INVALID_URL(isolate, "Invalid URL"); +- env->isolate()->ThrowException( +- ERR_INVALID_URL(env->isolate(), "Invalid URL")); +- - return; - } - - ada::result file_path_url; -- std::optional initial_file_path; +- std::string initial_file_path; - std::string file_path; - -- if (args.Length() >= 2 && args[1]->IsString()) { -- auto package_config_main = Utf8Value(isolate, args[1]).ToString(); +- if (args.Length() >= 2 && !args[1]->IsNullOrUndefined() && +- args[1]->IsString()) { +- std::string package_config_main = +- Utf8Value(env->isolate(), args[1].As()).ToString(); - - file_path_url = ada::parse( - std::string("./") + package_config_main, &package_json_url.value()); - - if (!file_path_url) { -- THROW_ERR_INVALID_URL(isolate, "Invalid URL"); -- return; -- } +- env->isolate()->ThrowException( +- ERR_INVALID_URL(env->isolate(), "Invalid URL")); - -- initial_file_path = node::url::FileURLToPath(env, *file_path_url); -- if (!initial_file_path.has_value()) { - return; - } - -- node::url::FromNamespacedPath(&initial_file_path.value()); +- if (!FileURLToPath(env, file_path_url.value(), initial_file_path)) return; - -- for (int i = 0; i < legacy_main_extensions_with_main_end; i++) { -- file_path = *initial_file_path + std::string(legacy_main_extensions[i]); +- FromNamespacedPath(&initial_file_path); +- +- for (int i = 0; i < BindingData::legacy_main_extensions_with_main_end; +- i++) { +- file_path = initial_file_path + BindingData::legacy_main_extensions[i]; - - switch (FilePathIsFile(env, file_path)) { - case BindingData::FilePathIsFileReturnType::kIsFile: @@ -217,21 +386,20 @@ index f3d8c35d4963d84aef1cb2e15627a959215b8ab2..5b42f671f7a8f1814e086cd9855fff7e - ada::parse("./index", &package_json_url.value()); - - if (!file_path_url) { -- THROW_ERR_INVALID_URL(isolate, "Invalid URL"); -- return; -- } +- env->isolate()->ThrowException( +- ERR_INVALID_URL(env->isolate(), "Invalid URL")); - -- initial_file_path = node::url::FileURLToPath(env, *file_path_url); -- if (!initial_file_path.has_value()) { - return; - } - -- node::url::FromNamespacedPath(&initial_file_path.value()); +- if (!FileURLToPath(env, file_path_url.value(), initial_file_path)) return; +- +- FromNamespacedPath(&initial_file_path); - -- for (int i = legacy_main_extensions_with_main_end; -- i < legacy_main_extensions_package_fallback_end; +- for (int i = BindingData::legacy_main_extensions_with_main_end; +- i < BindingData::legacy_main_extensions_package_fallback_end; - i++) { -- file_path = *initial_file_path + std::string(legacy_main_extensions[i]); +- file_path = initial_file_path + BindingData::legacy_main_extensions[i]; - - switch (FilePathIsFile(env, file_path)) { - case BindingData::FilePathIsFileReturnType::kIsFile: @@ -248,45 +416,44 @@ index f3d8c35d4963d84aef1cb2e15627a959215b8ab2..5b42f671f7a8f1814e086cd9855fff7e - } - } - -- std::optional module_path = -- node::url::FileURLToPath(env, *package_json_url); -- std::optional module_base; +- std::string module_path; +- std::string module_base; - -- if (!module_path.has_value()) { -- return; -- } +- if (!FileURLToPath(env, package_json_url.value(), module_path)) return; - -- if (args.Length() >= 3 && args[2]->IsString()) { -- Utf8Value utf8_base_path(isolate, args[2]); +- if (args.Length() >= 3 && !args[2]->IsNullOrUndefined() && +- args[2]->IsString()) { +- Utf8Value utf8_base_path(env->isolate(), args[2].As()); - auto base_url = - ada::parse(utf8_base_path.ToStringView()); - - if (!base_url) { -- THROW_ERR_INVALID_URL(isolate, "Invalid URL"); -- return; -- } +- env->isolate()->ThrowException( +- ERR_INVALID_URL(env->isolate(), "Invalid URL")); - -- module_base = node::url::FileURLToPath(env, *base_url); -- if (!module_base.has_value()) { - return; - } +- +- if (!FileURLToPath(env, base_url.value(), module_base)) return; - } else { -- THROW_ERR_INVALID_ARG_TYPE( -- isolate, -- "The \"base\" argument must be of type string or an instance of URL."); +- std::string err_arg_message = +- "The \"base\" argument must be of type string or an instance of URL."; +- env->isolate()->ThrowException( +- ERR_INVALID_ARG_TYPE(env->isolate(), err_arg_message.c_str())); - return; - } - -- THROW_ERR_MODULE_NOT_FOUND(isolate, -- "Cannot find package '%s' imported from %s", -- *module_path, -- *module_base); +- env->isolate()->ThrowException( +- ERR_MODULE_NOT_FOUND(env->isolate(), +- "Cannot find package '%s' imported from %s", +- module_path, +- module_base)); -} - void BindingData::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackField("stats_field_array", stats_field_array); tracker->TrackField("stats_field_bigint_array", stats_field_bigint_array); -@@ -3198,19 +3067,6 @@ InternalFieldInfoBase* BindingData::Serialize(int index) { +@@ -3399,19 +3109,6 @@ InternalFieldInfoBase* BindingData::Serialize(int index) { return info; } @@ -306,7 +473,7 @@ index f3d8c35d4963d84aef1cb2e15627a959215b8ab2..5b42f671f7a8f1814e086cd9855fff7e static void CreatePerIsolateProperties(IsolateData* isolate_data, Local target) { Isolate* isolate = isolate_data->isolate(); -@@ -3265,7 +3121,6 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, +@@ -3468,7 +3165,6 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, SetMethod(isolate, target, "mkdtemp", Mkdtemp); StatWatcher::CreatePerIsolateProperties(isolate_data, target); @@ -314,19 +481,33 @@ index f3d8c35d4963d84aef1cb2e15627a959215b8ab2..5b42f671f7a8f1814e086cd9855fff7e target->Set( FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"), -@@ -3338,7 +3193,6 @@ BindingData* FSReqBase::binding_data() { - void RegisterExternalReferences(ExternalReferenceRegistry* registry) { +@@ -3542,7 +3238,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(Access); + registry->Register(AccessSync); StatWatcher::RegisterExternalReferences(registry); - BindingData::RegisterExternalReferences(registry); - registry->Register(GetFormatOfExtensionlessFile); registry->Register(Close); + registry->Register(CloseSync); diff --git a/src/node_file.h b/src/node_file.h -index 6f1b55284db0f4f8c70081b4834a074c717f3cc9..a969fff32bd156aa9393c1db9eec474eb7432cea 100644 +index 4599546c5245300346557b68070c60292daaed23..7b43d027a2e6524f3ec6b7bccdb6e49a3c8790ea 100644 --- a/src/node_file.h +++ b/src/node_file.h -@@ -86,13 +86,6 @@ class BindingData : public SnapshotableObject { +@@ -63,13 +63,6 @@ class BindingData : public SnapshotableObject { + AliasedBufferIndex statfs_field_array; + AliasedBufferIndex statfs_field_bigint_array; + }; +- +- enum class FilePathIsFileReturnType { +- kIsFile = 0, +- kIsNotFile, +- kThrowInsufficientPermissions +- }; +- + explicit BindingData(Realm* realm, + v8::Local wrap, + InternalFieldInfo* info = nullptr); +@@ -86,30 +79,12 @@ class BindingData : public SnapshotableObject { SERIALIZABLE_OBJECT_METHODS() SET_BINDING_ID(fs_binding_data) @@ -340,6 +521,23 @@ index 6f1b55284db0f4f8c70081b4834a074c717f3cc9..a969fff32bd156aa9393c1db9eec474e void MemoryInfo(MemoryTracker* tracker) const override; SET_SELF_SIZE(BindingData) SET_MEMORY_INFO_NAME(BindingData) + + private: + InternalFieldInfo* internal_field_info_ = nullptr; +- +- static FilePathIsFileReturnType FilePathIsFile(Environment* env, +- const std::string& file_path); +- +- static const std::array legacy_main_extensions; +- // define the final index of the algorithm resolution +- // when packageConfig.main is defined. +- static const uint8_t legacy_main_extensions_with_main_end = 7; +- // define the final index of the algorithm resolution +- // when packageConfig.main is NOT defined +- static const uint8_t legacy_main_extensions_package_fallback_end = 10; + }; + + // structure used to store state during a complex operation, e.g., mkdirp. diff --git a/test/es-module/test-cjs-legacyMainResolve.js b/test/es-module/test-cjs-legacyMainResolve.js index 1dc7d8faafe6eb5cea7e43e9783041f2a994be0d..d86d501689b2b72f2b964d6e2a91c5d36b6b62f5 100644 --- a/test/es-module/test-cjs-legacyMainResolve.js