src,lib: reducing C++ calls of esm legacy main resolve

Instead of many C++ calls, now we make only one C++ call
to return a enum number that represents the selected state.

PR-URL: https://github.com/nodejs/node/pull/48325
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Vinicius Lourenço authored and Juan José Arboleda committed 2023-07-13 16:44:14 -05:00
1 parent 332e480b46
commit 9cf2e1f55b
10 files changed
+715 -45

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
// Tests the impact on eager operations required for policies affecting
// general startup, does not test lazy operations
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const common = require('../common.js');
const tmpdir = require('../../test/common/tmpdir.js');
const { pathToFileURL } = require('node:url');
const benchmarkDirectory =
path.resolve(tmpdir.path, 'benchmark-import-meta-resolve');
const configs = {
n: [1e4],
packageJsonUrl: [
'node_modules/test/package.json',
],
packageConfigMain: ['', './index.js'],
resolvedFile: [
'node_modules/test/index.js',
'node_modules/test/index.json',
'node_modules/test/index.node',
'node_modules/non-exist',
],
};
const options = {
flags: ['--expose-internals'],
};
const bench = common.createBenchmark(main, configs, options);
function main(conf) {
const { legacyMainResolve } = require('internal/modules/esm/resolve');
tmpdir.refresh();
fs.mkdirSync(path.join(benchmarkDirectory, 'node_modules', 'test'), { recursive: true });
fs.writeFileSync(path.join(benchmarkDirectory, conf.resolvedFile), '\n');
const packageJsonUrl = pathToFileURL(conf.packageJsonUrl);
const packageConfigMain = { main: conf.packageConfigMain };
bench.start();
for (let i = 0; i < conf.n; i++) {
try {
legacyMainResolve(packageJsonUrl, packageConfigMain, undefined);
} catch { /* empty */ }
}
bench.end(conf.n);
}
+46 -45
View File
@@ -36,8 +36,9 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
const typeFlag = getOptionValue('--input-type');
const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url');
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { canParse: URLCanParse } = internalBinding('url');
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
const {
ERR_INPUT_TYPE_NOT_ALLOWED,
ERR_INVALID_ARG_TYPE,
@@ -133,13 +134,34 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
const realpathCache = new SafeMap();
/**
* @param {string | URL} url
* @returns {boolean}
*/
function fileExists(url) {
return internalModuleStat(toNamespacedPath(toPathIfFileURL(url))) === 0;
}
const legacyMainResolveExtensions = [
'',
'.js',
'.json',
'.node',
'/index.js',
'/index.json',
'/index.node',
'./index.js',
'./index.json',
'./index.node',
];
const legacyMainResolveExtensionsIndexes = {
// 0-6: when packageConfig.main is defined
kResolvedByMain: 0,
kResolvedByMainJs: 1,
kResolvedByMainJson: 2,
kResolvedByMainNode: 3,
kResolvedByMainIndexJs: 4,
kResolvedByMainIndexJson: 5,
kResolvedByMainIndexNode: 6,
// 7-9: when packageConfig.main is NOT defined,
// or when the previous case didn't found the file
kResolvedByPackageAndJs: 7,
kResolvedByPackageAndJson: 8,
kResolvedByPackageAndNode: 9,
};
/**
* Legacy CommonJS main resolution:
@@ -154,44 +176,22 @@ function fileExists(url) {
* @returns {URL}
*/
function legacyMainResolve(packageJSONUrl, packageConfig, base) {
let guess;
if (packageConfig.main !== undefined) {
// Note: fs check redundances will be handled by Descriptor cache here.
if (fileExists(guess = new URL(`./${packageConfig.main}`,
packageJSONUrl))) {
return guess;
} else if (fileExists(guess = new URL(`./${packageConfig.main}.js`,
packageJSONUrl)));
else if (fileExists(guess = new URL(`./${packageConfig.main}.json`,
packageJSONUrl)));
else if (fileExists(guess = new URL(`./${packageConfig.main}.node`,
packageJSONUrl)));
else if (fileExists(guess = new URL(`./${packageConfig.main}/index.js`,
packageJSONUrl)));
else if (fileExists(guess = new URL(`./${packageConfig.main}/index.json`,
packageJSONUrl)));
else if (fileExists(guess = new URL(`./${packageConfig.main}/index.node`,
packageJSONUrl)));
else guess = undefined;
if (guess) {
emitLegacyIndexDeprecation(guess, packageJSONUrl, base,
packageConfig.main);
return guess;
}
// Fallthrough.
const packageJsonUrlString = packageJSONUrl.href;
if (typeof packageJsonUrlString !== 'string') {
throw new ERR_INVALID_ARG_TYPE('packageJSONUrl', ['URL'], packageJSONUrl);
}
if (fileExists(guess = new URL('./index.js', packageJSONUrl)));
// So fs.
else if (fileExists(guess = new URL('./index.json', packageJSONUrl)));
else if (fileExists(guess = new URL('./index.node', packageJSONUrl)));
else guess = undefined;
if (guess) {
emitLegacyIndexDeprecation(guess, packageJSONUrl, base, packageConfig.main);
return guess;
}
// Not found.
throw new ERR_MODULE_NOT_FOUND(
fileURLToPath(new URL('.', packageJSONUrl)), fileURLToPath(base));
const baseStringified = isURL(base) ? base.href : base;
const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);
const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes.kResolvedByMainIndexNode ? `./${packageConfig.main}` : '';
const resolvedUrl = new URL(baseUrl + legacyMainResolveExtensions[resolvedOption], packageJSONUrl);
emitLegacyIndexDeprecation(resolvedUrl, packageJSONUrl, base, packageConfig.main);
return resolvedUrl;
}
const encodedSepRegEx = /%2F|%5C/i;
@@ -1078,6 +1078,7 @@ module.exports = {
packageExportsResolve,
packageImportsResolve,
throwIfInvalidParentURL,
legacyMainResolve,
};
// cycle
+5
View File
@@ -69,18 +69,22 @@ void AppendExceptionLine(Environment* env,
V(ERR_INVALID_ARG_VALUE, TypeError) \
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
V(ERR_INVALID_ARG_TYPE, TypeError) \
V(ERR_INVALID_FILE_URL_HOST, TypeError) \
V(ERR_INVALID_FILE_URL_PATH, TypeError) \
V(ERR_INVALID_OBJECT_DEFINE_PROPERTY, TypeError) \
V(ERR_INVALID_MODULE, Error) \
V(ERR_INVALID_STATE, Error) \
V(ERR_INVALID_THIS, TypeError) \
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
V(ERR_INVALID_URL, TypeError) \
V(ERR_INVALID_URL_SCHEME, TypeError) \
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
V(ERR_MISSING_ARGS, TypeError) \
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError) \
V(ERR_MISSING_PASSPHRASE, TypeError) \
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
V(ERR_MODULE_NOT_FOUND, Error) \
V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \
V(ERR_OUT_OF_RANGE, RangeError) \
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
@@ -165,6 +169,7 @@ ERRORS_WITH_CODE(V)
V(ERR_INVALID_STATE, "Invalid state") \
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
V(ERR_INVALID_URL_SCHEME, "The URL must be of scheme file:") \
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \
+304
View File
@@ -19,11 +19,14 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node_file.h" // NOLINT(build/include_inline)
#include "ada.h"
#include "aliased_buffer-inl.h"
#include "memory_tracker-inl.h"
#include "node_buffer.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_file-inl.h"
#include "node_metadata.h"
#include "node_process-inl.h"
#include "node_stat_watcher.h"
#include "permission/permission.h"
@@ -2726,6 +2729,292 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& 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<const uv_stat_t*>(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<std::string, 10> BindingData::legacy_main_extensions = {
"",
".js",
".json",
".node",
"/index.js",
"/index.json",
"/index.node",
".js",
".json",
".node"};
void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
Environment* env = Environment::GetCurrent(args);
Utf8Value utf8_package_json_url(env->isolate(), args[0].As<String>());
auto package_json_url =
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView());
if (!package_json_url) {
env->isolate()->ThrowException(
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
return;
}
ada::result<ada::url_aggregator> file_path_url;
std::string initial_file_path;
std::string file_path;
if (args.Length() >= 2 && !args[1]->IsNullOrUndefined() &&
args[1]->IsString()) {
std::string package_config_main =
Utf8Value(env->isolate(), args[1].As<String>()).ToString();
file_path_url = ada::parse<ada::url_aggregator>(
std::string("./") + package_config_main, &package_json_url.value());
if (!file_path_url) {
env->isolate()->ThrowException(
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
return;
}
if (!FileURLToPath(env, file_path_url.value(), initial_file_path)) return;
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:
return args.GetReturnValue().Set(i);
case BindingData::FilePathIsFileReturnType::kIsNotFile:
continue;
case BindingData::FilePathIsFileReturnType::
kThrowInsufficientPermissions:
// the default behavior when do not have permission is to return
// and exit the execution of the method as soon as possible
// the internal function will throw the exception
return;
default:
UNREACHABLE();
}
}
}
file_path_url =
ada::parse<ada::url_aggregator>("./index", &package_json_url.value());
if (!file_path_url) {
env->isolate()->ThrowException(
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
return;
}
if (!FileURLToPath(env, file_path_url.value(), initial_file_path)) return;
FromNamespacedPath(&initial_file_path);
for (int i = BindingData::legacy_main_extensions_with_main_end;
i < BindingData::legacy_main_extensions_package_fallback_end;
i++) {
file_path = initial_file_path + BindingData::legacy_main_extensions[i];
switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
return args.GetReturnValue().Set(i);
case BindingData::FilePathIsFileReturnType::kIsNotFile:
continue;
case BindingData::FilePathIsFileReturnType::kThrowInsufficientPermissions:
// the default behavior when do not have permission is to return
// and exit the execution of the method as soon as possible
// the internal function will throw the exception
return;
default:
UNREACHABLE();
}
}
std::string module_path;
std::string module_base;
if (!FileURLToPath(env, package_json_url.value(), module_path)) return;
if (args.Length() >= 3 && !args[2]->IsNullOrUndefined() &&
args[2]->IsString()) {
Utf8Value utf8_base_path(env->isolate(), args[2].As<String>());
auto base_url =
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView());
if (!base_url) {
env->isolate()->ThrowException(
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
return;
}
if (!FileURLToPath(env, base_url.value(), module_base)) return;
} else {
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;
}
std::string err_module_message =
"Cannot find package '" + module_path + "' imported from " + module_base;
env->isolate()->ThrowException(
ERR_MODULE_NOT_FOUND(env->isolate(), err_module_message.c_str()));
}
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("stats_field_array", stats_field_array);
tracker->TrackField("stats_field_bigint_array", stats_field_bigint_array);
@@ -2825,6 +3114,19 @@ InternalFieldInfoBase* BindingData::Serialize(int index) {
return info;
}
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
SetMethod(
isolate, target, "legacyMainResolve", BindingData::LegacyMainResolve);
}
void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(BindingData::LegacyMainResolve);
}
static void CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
@@ -2872,6 +3174,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "mkdtemp", Mkdtemp);
StatWatcher::CreatePerIsolateProperties(isolate_data, target);
BindingData::CreatePerIsolateProperties(isolate_data, target);
target->Set(
FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"),
@@ -2944,6 +3247,7 @@ BindingData* FSReqBase::binding_data() {
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Access);
StatWatcher::RegisterExternalReferences(registry);
BindingData::RegisterExternalReferences(registry);
registry->Register(Close);
registry->Register(Open);
+25
View File
@@ -63,6 +63,13 @@ 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<v8::Object> wrap,
InternalFieldInfo* info = nullptr);
@@ -79,12 +86,30 @@ class BindingData : public SnapshotableObject {
SERIALIZABLE_OBJECT_METHODS()
SET_BINDING_ID(fs_binding_data)
static void LegacyMainResolve(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void CreatePerIsolateProperties(IsolateData* isolate_data,
v8::Local<v8::ObjectTemplate> ctor);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
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<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.
@@ -0,0 +1,132 @@
'use strict';
// Flags: --expose-internals --experimental-permission --allow-fs-read=* --allow-child-process
require('../common');
const { describe, it } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const fixtures = require('../common/fixtures.js');
function escapeWhenSepIsBackSlash(filePath) {
return path.sep === '\\' ? filePath.replace(/\\/g, '\\\\') : filePath;
}
describe('legacyMainResolve', () => {
it('should ensure permission model when resolving by packageConfig.main', () => {
const fixtextureFolder = fixtures.path('/es-modules/legacy-main-resolver');
const paths = [
['./index-js/index.js', []],
['./index-js/index', ['./index-js/index.js']],
['./index-json/index', ['./index-json/index.js']],
['./index-node/index', ['./index-node/index.js', './index-node/index.json']],
['./index-js', []],
['./index-json', ['./index-json/index.js']],
['./index-node', ['./index-node/index.js', './index-node/index.json']],
];
for (const [mainOrFolder, allowReads] of paths) {
const allowReadFilePaths = allowReads.map((filepath) => path.resolve(fixtextureFolder, filepath));
const allowReadFiles = allowReads?.length > 0 ? ['--allow-fs-read', allowReadFilePaths.join(',')] : [];
const fixtextureFolderEscaped = escapeWhenSepIsBackSlash(fixtextureFolder);
const { status, stderr } = spawnSync(
process.execPath,
[
'--expose-internals',
'--experimental-permission',
...allowReadFiles,
'-e',
`
const { legacyMainResolve } = require('node:internal/modules/esm/resolve');
const { pathToFileURL } = require('node:url');
const path = require('node:path');
const assert = require('node:assert');
const packageJsonUrl = pathToFileURL(
path.resolve(
'${fixtextureFolderEscaped}',
'package.json'
)
);
const packageConfig = { main: '${mainOrFolder}' };
const base = path.resolve(
'${fixtextureFolderEscaped}'
);
assert.throws(() => legacyMainResolve(packageJsonUrl, packageConfig, base), {
code: 'ERR_ACCESS_DENIED',
resource: path.resolve(
'${fixtextureFolderEscaped}',
'${mainOrFolder}'
)
});
`,
]
);
assert.strictEqual(status, 0, stderr.toString());
}
});
it('should ensure permission model when resolving by packageJsonUrl', () => {
const fixtextureFolder = fixtures.path('/es-modules/legacy-main-resolver');
const paths = [
['./index-js', './index.js', []],
['./index-json', './index.json', ['./index.js']],
['./index-node', './index.node', ['./index.js', './index.json']],
];
for (const [folder, expectedFile, allowReads] of paths) {
const allowReadFilePaths = allowReads.map((filepath) => path.resolve(fixtextureFolder, folder, filepath));
const allowReadFiles = allowReads?.length > 0 ? ['--allow-fs-read', allowReadFilePaths.join(',')] : [];
const fixtextureFolderEscaped = escapeWhenSepIsBackSlash(fixtextureFolder);
const { status, stderr } = spawnSync(
process.execPath,
[
'--expose-internals',
'--experimental-permission',
...allowReadFiles,
'-e',
`
const { legacyMainResolve } = require('node:internal/modules/esm/resolve');
const { pathToFileURL } = require('node:url');
const path = require('node:path');
const assert = require('node:assert');
const packageJsonUrl = pathToFileURL(
path.resolve(
'${fixtextureFolderEscaped}',
'${folder}',
'package.json'
)
);
const packageConfig = { main: undefined };
const base = path.resolve(
'${fixtextureFolderEscaped}'
);
assert.throws(() => legacyMainResolve(packageJsonUrl, packageConfig, base), {
code: 'ERR_ACCESS_DENIED',
resource: path.resolve(
'${fixtextureFolderEscaped}',
'${folder}',
'${expectedFile}'
)
});
`,
]
);
assert.strictEqual(status, 0, stderr.toString());
}
});
});
@@ -0,0 +1,148 @@
'use strict';
// Flags: --expose-internals
require('../common');
const { describe, it } = require('node:test');
const path = require('node:path');
const assert = require('node:assert');
const { pathToFileURL } = require('node:url');
const { legacyMainResolve } = require('node:internal/modules/esm/resolve');
const fixtures = require('../common/fixtures.js');
describe('legacyMainResolve', () => {
it('should resolve using packageConfig.main', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
const paths = [
['./index-js/index.js', './index-js/index.js'],
['./index-js/index', './index-js/index.js'],
['./index-json/index', './index-json/index.json'],
['./index-node/index', './index-node/index.node'],
['./index-js', './index-js/index.js'],
['./index-json', './index-json/index.json'],
['./index-node', './index-node/index.node'],
];
for (const [main, expected] of paths) {
const packageConfig = { main };
const base = path.resolve(
fixtures.path('/es-modules/legacy-main-resolver')
);
assert.strictEqual(
legacyMainResolve(packageJsonUrl, packageConfig, base).href,
pathToFileURL(path.join(base, expected)).href
);
}
});
it('should resolve using packageJsonUrl', () => {
const paths = [
['index-js', './index-js/index.js'],
['index-json', './index-json/index.json'],
['index-node', './index-node/index.node'],
];
for (const [folder, expected] of paths) {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
folder,
'package.json'
)
);
const packageConfig = { main: undefined };
const base = path.resolve(
fixtures.path('/es-modules/legacy-main-resolver')
);
assert.strictEqual(
legacyMainResolve(packageJsonUrl, packageConfig, base).href,
pathToFileURL(path.join(base, expected)).href
);
}
});
it('should throw when packageJsonUrl is not URL', () => {
assert.throws(
() =>
legacyMainResolve(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver/index-node'),
'package.json'
),
{},
''
),
{ message: /instance of URL/ }
);
});
it('should throw when packageConfigMain is invalid URL', () => {
assert.throws(
() =>
legacyMainResolve(
pathToFileURL(
path.resolve(
// Is invalid because this path does not point to a file
fixtures.path('/es-modules/legacy-main-resolver/index-node')
)
),
{ main: './invalid/index.js' },
''
),
{ message: /Invalid URL/ }
);
});
it('should throw when packageJsonUrl is invalid URL', () => {
assert.throws(
() =>
legacyMainResolve(
pathToFileURL(
path.resolve(
// Is invalid because this path does not point to a file
fixtures.path('/es-modules/legacy-main-resolver/index-node')
)
),
{ main: undefined },
''
),
{ message: /Invalid URL/ }
);
});
it('should throw when cannot resolve to a file', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
{ message: /Cannot find package/i }
);
});
it('should throw when cannot resolve to a file (base not defined)', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: null }, undefined),
{ message: /"base" argument must be/ }
);
});
});
@@ -0,0 +1 @@
module.exports = {};
@@ -0,0 +1 @@
{}
Whitespace-only changes.