Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2909,11 +2909,19 @@ BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
return BindingData::FilePathIsFileReturnType::kIsNotFile;
}

namespace {

// define the final index of the algorithm resolution
// when packageConfig.main is defined.
constexpr uint8_t legacy_main_extensions_with_main_end = 7;
// define the final index of the algorithm resolution
// when packageConfig.main is NOT defined
constexpr uint8_t legacy_main_extensions_package_fallback_end = 10;
// 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<std::string, 10> BindingData::legacy_main_extensions = {
constexpr std::array<std::string_view, 10> legacy_main_extensions = {
"",
".js",
".json",
Expand All @@ -2925,6 +2933,8 @@ const std::array<std::string, 10> BindingData::legacy_main_extensions = {
".json",
".node"};

} // namespace

void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
Expand Down Expand Up @@ -2965,9 +2975,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

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];
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
file_path = initial_file_path + std::string(legacy_main_extensions[i]);

switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
Expand Down Expand Up @@ -3000,10 +3009,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

FromNamespacedPath(&initial_file_path);

for (int i = BindingData::legacy_main_extensions_with_main_end;
i < BindingData::legacy_main_extensions_package_fallback_end;
for (int i = legacy_main_extensions_with_main_end;
i < legacy_main_extensions_package_fallback_end;
i++) {
file_path = initial_file_path + BindingData::legacy_main_extensions[i];
file_path = initial_file_path + std::string(legacy_main_extensions[i]);

switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
Expand Down
8 changes: 0 additions & 8 deletions src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ class BindingData : public SnapshotableObject {

static FilePathIsFileReturnType FilePathIsFile(Environment* env,
const std::string& file_path);

static const std::array<std::string, 10> 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.
Expand Down