From f94cc2c0e77f5a8fe2329e588e78c3df35930d13 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Tue, 5 Mar 2019 04:41:28 -0500 Subject: [PATCH] esm: implement flagged resolution algo --- src/module_wrap.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 1df0319db1..febeca760e 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -43,6 +43,8 @@ using v8::String; using v8::Undefined; using v8::Value; +static const char* const EXTENSIONS[] = {".mjs", ".js", ".json", ".node"}; + ModuleWrap::ModuleWrap(Environment* env, Local object, Local module, @@ -683,6 +685,36 @@ Maybe FinalizeResolution(Environment* env, return Just(resolved); } +enum ResolveExtensionsOptions { + TRY_EXACT_NAME, + ONLY_VIA_EXTENSIONS +}; + +template +Maybe ResolveExtensions(const URL& search) { + if (options == TRY_EXACT_NAME) { + std::string filePath = search.ToFilePath(); + Maybe fd = OpenDescriptor(filePath); + if (!fd.IsNothing()) { + return Just(search); + } + } + + for (const char* extension : EXTENSIONS) { + URL guess(search.path() + extension, &search); + Maybe fd = OpenDescriptor(guess.ToFilePath()); + if (!fd.IsNothing()) { + return Just(guess); + } + } + + return Nothing(); +} + +inline Maybe ResolveIndex(const URL& search) { + return ResolveExtensions(URL("index", search)); +} + Maybe PackageMainResolve(Environment* env, const URL& pjson_url, const PackageConfig& pcfg, @@ -778,6 +810,16 @@ Maybe Resolve(Environment* env, URL resolved; if (ShouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { resolved = URL(specifier, base); + if (env->options()->legacy_resolution) { + Maybe file = ResolveExtensions(resolved); + if (!file.IsNothing()) { + return file; + } + if (specifier.back() != '/') { + resolved = URL(specifier + "/", base); + } + return ResolveIndex(resolved); + } } else { URL pure_url(specifier); if (!(pure_url.flags() & URL_FLAGS_FAILED)) {