Skip to content

Commit 0d28268

Browse files
committed
fix(runtime): spell http(s) import-map scope keys like the referrer keys they match
A scope key matches the importing module's registry key by plain prefix, and http(s) registry keys are spelled by the URL parser, so a scope written with dot segments or a non-ASCII segment matched nothing. An http(s) scope key is now spelled the same way when the map is installed, ending in '/' exactly when it was written so; other scope keys (app paths) and imports keys stay as written.
1 parent 92bf295 commit 0d28268

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎NativeScript/runtime/ModuleInternalCallbacks.mm‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,21 @@ static bool ParseImportMapEntries(NSDictionary* source, const std::string& secti
937937
return true;
938938
}
939939
940+
// A scope key matches referrer registry keys by plain prefix, and an http(s)
941+
// referrer key is spelled by NormalizeHttpModuleUrl, so an http(s) scope key
942+
// is spelled the same way. Whether it ends in '/' stays as written: the parser
943+
// gives a path-less URL the path "/", which would narrow the prefix.
944+
static std::string ImportMapScopePrefix(const std::string& scopeKey) {
945+
std::string prefix = NormalizeHttpModuleUrl(scopeKey);
946+
if (prefix.empty()) {
947+
return scopeKey;
948+
}
949+
if (scopeKey.back() != '/' && prefix.back() == '/') {
950+
prefix.pop_back();
951+
}
952+
return prefix;
953+
}
954+
940955
// Parse without touching the live map. On any failure `error` explains what is
941956
// wrong and `out` is meaningless — the caller keeps whatever it already had.
942957
static bool ParseImportMap(const std::string& json, ParsedImportMap* out, std::string* error) {
@@ -1009,7 +1024,7 @@ static bool ParseImportMap(const std::string& json, ParsedImportMap* out, std::s
10091024
error)) {
10101025
return false;
10111026
}
1012-
out->scopes.emplace_back(scopePrefix, std::move(entries));
1027+
out->scopes.emplace_back(ImportMapScopePrefix(scopePrefix), std::move(entries));
10131028
}
10141029
}
10151030
}

‎TestRunner/app/tests/HttpEsmLoaderTests.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,41 @@ describe("HTTP ESM Loader", function() {
19351935
});
19361936
});
19371937

1938+
it("matches an http(s) scope the way the URL parser spells it", function (done) {
1939+
var origin = getHostOrigin();
1940+
if (!origin) {
1941+
pending("REPORT_BASEURL not set; skipping host HTTP tests");
1942+
done();
1943+
return;
1944+
}
1945+
var base = origin + "/esm/resolve/scope-norm";
1946+
var scopes = {};
1947+
scopes[base + "/x/../dots/"] = { "ns-scope-norm": base + "/target.mjs?k=dots" };
1948+
scopes[base + "/mödir/"] = { "ns-scope-norm": base + "/target.mjs?k=nonascii" };
1949+
// No trailing slash: still a plain prefix, here inside a segment.
1950+
scopes[base + "/pla"] = { "ns-scope-norm": base + "/target.mjs?k=plain" };
1951+
scopes[origin] = { "ns-scope-norm-origin": base + "/target.mjs?k=origin" };
1952+
nsModule.configureLoader({ importMap: { imports: {}, scopes: scopes } });
1953+
function load(dir, spec) {
1954+
return importer(origin, "scope-norm/" + dir).then(function (mod) {
1955+
return mod.load(spec);
1956+
});
1957+
}
1958+
Promise.all([
1959+
load("dots", "ns-scope-norm"),
1960+
load("mödir", "ns-scope-norm"),
1961+
load("plain", "ns-scope-norm"),
1962+
load("dots", "ns-scope-norm-origin"),
1963+
]).then(function (mods) {
1964+
expect(mods.map(function (m) { return m.query; }))
1965+
.toEqual(["k=dots", "k=nonascii", "k=plain", "k=origin"]);
1966+
done();
1967+
}).catch(function (error) {
1968+
expect("rejected: " + formatError(error)).toBe("resolved");
1969+
done();
1970+
});
1971+
});
1972+
19381973
it("rejects an import() whose import-map target resolves to nothing", function (done) {
19391974
var origin = getHostOrigin();
19401975
if (!origin) {

‎docs/ns-builtin-modules.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ A **scope key is matched as a plain prefix of the importing module's canonical
660660
registry key** — an absolute `http(s)` URL for a served module, or a canonical
661661
absolute path for a file on disk. That key is this runtime's analogue of the
662662
web's resolved referrer URL, which is what scope prefixes match in a browser.
663-
End a scope key with
663+
An `http(s)` scope key is spelled the way registry keys are (see
664+
[Registry canonicalization](#registry-canonicalization)) when the map is
665+
installed, ending in `/` exactly when it is written so. End a scope key with
664666
`/` to keep it on a directory boundary. Resolution
665667
consults the most specific matching scope first (longest prefix wins), then
666668
progressively less specific ones, then `imports` — so a scope can override a

0 commit comments

Comments
 (0)