Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
esm: implement flagged resolution algo
Browse files Browse the repository at this point in the history
  • Loading branch information
MylesBorins committed Mar 5, 2019
1 parent f604ca4 commit f94cc2c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> object,
Local<Module> module,
Expand Down Expand Up @@ -683,6 +685,36 @@ Maybe<URL> FinalizeResolution(Environment* env,
return Just(resolved);
}

enum ResolveExtensionsOptions {
TRY_EXACT_NAME,
ONLY_VIA_EXTENSIONS
};

template <ResolveExtensionsOptions options>
Maybe<URL> ResolveExtensions(const URL& search) {
if (options == TRY_EXACT_NAME) {
std::string filePath = search.ToFilePath();
Maybe<uv_file> fd = OpenDescriptor(filePath);
if (!fd.IsNothing()) {
return Just(search);
}
}

for (const char* extension : EXTENSIONS) {
URL guess(search.path() + extension, &search);
Maybe<uv_file> fd = OpenDescriptor(guess.ToFilePath());
if (!fd.IsNothing()) {
return Just(guess);
}
}

return Nothing<URL>();
}

inline Maybe<URL> ResolveIndex(const URL& search) {
return ResolveExtensions<ONLY_VIA_EXTENSIONS>(URL("index", search));
}

Maybe<URL> PackageMainResolve(Environment* env,
const URL& pjson_url,
const PackageConfig& pcfg,
Expand Down Expand Up @@ -778,6 +810,16 @@ Maybe<URL> Resolve(Environment* env,
URL resolved;
if (ShouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
resolved = URL(specifier, base);
if (env->options()->legacy_resolution) {
Maybe<URL> file = ResolveExtensions<TRY_EXACT_NAME>(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)) {
Expand Down

0 comments on commit f94cc2c

Please sign in to comment.