Skip to content

Commit ae45c1f

Browse files
committed
refactor(runtime): one shared unit for the loader's module URL helpers
IsHttpRegistryKey, ParseModuleUrl and the written-authority rule move out of ModuleInternalCallbacks.mm into ModuleUrl, which also gains NormalizeHttpModuleUrl: the spelling the loader gives an absolute http(s) module URL, falling back to the input itself for an HTTP-prefixed string the URL parser rejects. ParseModuleUrl now rebuilds the written-authority href by replacing only the authority inside the parser's href. Rebuilding from the pathname, search and hash getters lost a bare trailing '?' or '#', so the re-parse check failed and the parser's lowercased host replaced the written one.
1 parent 3fcad40 commit ae45c1f

5 files changed

Lines changed: 171 additions & 95 deletions

File tree

‎NativeScript/runtime/ModuleInternalCallbacks.mm‎

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
#include "Helpers.h" // for tns::Exists
2929
#include "HttpLoader.h"
3030
#include "ModuleInternal.h" // for CompileFileEsModule / EvaluateModuleGraph
31+
#include "ModuleUrl.h"
3132
#include "NativeScriptException.h"
3233
#include "NativeScriptPlatform.h"
3334
#include "NsBuiltinModules.h"
3435
#include "Runtime.h" // for GetAppConfigValue
3536
#include "RuntimeConfig.h"
36-
#include "ada/ada.h"
3737

3838
// Do NOT pull all v8 symbols into namespace here; String would clash with
3939
// other typedefs inside the NativeScript codebase. We refer to v8 symbols
@@ -90,19 +90,6 @@ static inline bool StartsWith(const std::string& s, const char* prefix) {
9090
return s.size() >= n && s.compare(0, n, prefix) == 0;
9191
}
9292
93-
// Whether `key` is spelled the way the loader keys an HTTP module: a lowercase
94-
// `http://` or `https://` prefix, file modules being keyed by absolute paths.
95-
// Deliberately not a URL parse: it gates the rule that an HTTP importer never
96-
// reaches the filesystem implicitly, so a key the URL parser would reject (an
97-
// IPv6 zone id, say) must still count as HTTP.
98-
static bool IsHttpRegistryKey(const std::string& key) {
99-
if (key.compare(0, 4, "http") != 0) {
100-
return false;
101-
}
102-
const size_t schemeEnd = key.size() > 4 && key[4] == 's' ? 5 : 4;
103-
return key.compare(schemeEnd, 3, "://") == 0;
104-
}
105-
10693
static bool ShouldTraceRegistryKey(const std::string& rawKey, const std::string& registryKey);
10794
static std::string CanonicalizeRegistryKey(const std::string& key);
10895
static const char* ModuleStatusToString(v8::Module::Status status);
@@ -1560,85 +1547,6 @@ static void DrainStreamedCompilesForIsolate(v8::Isolate* isolate) {
15601547
return "Cannot find module '" + resolution.specifier + "' (tried " + resolution.attempted + ")";
15611548
}
15621549
1563-
static bool HasHttpScheme(const ada::url_aggregator& url) {
1564-
return url.type == ada::scheme::type::HTTP || url.type == ada::scheme::type::HTTPS;
1565-
}
1566-
1567-
// The authority (userinfo, host, port) of an absolute or scheme-relative URL
1568-
// exactly as written, or empty when `url` does not spell one plainly.
1569-
static std::string WrittenAuthority(const std::string& url) {
1570-
size_t start = 0;
1571-
if (StartsWith(url, "//")) {
1572-
start = 2;
1573-
} else {
1574-
const size_t colon = url.find(':');
1575-
if (colon == std::string::npos || url.compare(colon, 3, "://") != 0) {
1576-
return std::string();
1577-
}
1578-
start = colon + 3;
1579-
}
1580-
const size_t end = url.find_first_of("/?#", start);
1581-
std::string authority = url.substr(start, end == std::string::npos ? end : end - start);
1582-
for (const char c : authority) {
1583-
if (static_cast<unsigned char>(c) <= 0x20 || c == '\\') {
1584-
return std::string();
1585-
}
1586-
}
1587-
return authority;
1588-
}
1589-
1590-
struct ModuleUrl {
1591-
bool parsed = false;
1592-
bool http = false; // the scheme is http or https
1593-
std::string href;
1594-
};
1595-
1596-
// `input` parsed with the WHATWG URL parser (the one behind JS `new URL()`):
1597-
// as an absolute URL when `base` is empty, otherwise as a path- or
1598-
// scheme-relative reference against `base`.
1599-
//
1600-
// An http(s) result keeps its authority as written by whichever string
1601-
// supplied it: tooling compares module URLs against the origin string it was
1602-
// configured with, so that spelling is part of a module's identity until every
1603-
// loader entry point canonicalizes. The parser's own href is used whenever the
1604-
// written authority would not name the same URL.
1605-
static ModuleUrl ParseModuleUrl(const std::string& input, const std::string& base) {
1606-
ModuleUrl result;
1607-
ada::result<ada::url_aggregator> url;
1608-
if (base.empty()) {
1609-
url = ada::parse<ada::url_aggregator>(input, nullptr);
1610-
} else {
1611-
auto baseUrl = ada::parse<ada::url_aggregator>(base, nullptr);
1612-
if (!baseUrl) {
1613-
return result;
1614-
}
1615-
url = ada::parse<ada::url_aggregator>(input, &baseUrl.value());
1616-
}
1617-
if (!url) {
1618-
return result;
1619-
}
1620-
result.parsed = true;
1621-
result.http = HasHttpScheme(*url);
1622-
result.href = std::string(url->get_href());
1623-
if (!result.http) {
1624-
return result;
1625-
}
1626-
1627-
const std::string authority =
1628-
WrittenAuthority(base.empty() || StartsWith(input, "//") ? input : base);
1629-
if (authority.empty()) {
1630-
return result;
1631-
}
1632-
const std::string written = std::string(url->get_protocol()) + "//" + authority +
1633-
std::string(url->get_pathname()) + std::string(url->get_search()) +
1634-
std::string(url->get_hash());
1635-
auto reparsed = ada::parse<ada::url_aggregator>(written, nullptr);
1636-
if (reparsed && reparsed->get_href() == url->get_href()) {
1637-
result.href = written;
1638-
}
1639-
return result;
1640-
}
1641-
16421550
struct UrlSpecifier {
16431551
enum class Kind {
16441552
kHttpUrl, // an http(s) URL
@@ -3163,8 +3071,7 @@ static void FinishHttpDynamicImport(v8::Isolate* isolate, v8::Local<v8::Context>
31633071
// app path the registry keys by, or no scope would match it.
31643072
const std::string resourceName(*resourceUtf8);
31653073
const std::string resourceUrl = RepairCollapsedUrlScheme(resourceName);
3166-
const auto parsedResource = ada::parse<ada::url_aggregator>(resourceUrl, nullptr);
3167-
if (parsedResource && HasHttpScheme(*parsedResource)) {
3074+
if (ParseModuleUrl(resourceUrl, std::string()).http) {
31683075
dynamicReferrerKey = CanonicalizeRegistryKey(resourceUrl);
31693076
} else {
31703077
const ModuleResolution referrer =

‎NativeScript/runtime/ModuleUrl.cpp‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "ModuleUrl.h"
2+
3+
#include <string_view>
4+
5+
#include "ada/ada.h"
6+
7+
namespace tns {
8+
9+
namespace {
10+
11+
bool StartsWith(const std::string& s, const char* prefix) {
12+
const size_t n = std::char_traits<char>::length(prefix);
13+
return s.size() >= n && s.compare(0, n, prefix) == 0;
14+
}
15+
16+
bool HasHttpScheme(const ada::url_aggregator& url) {
17+
return url.type == ada::scheme::type::HTTP ||
18+
url.type == ada::scheme::type::HTTPS;
19+
}
20+
21+
// The authority (userinfo, host, port) of an absolute or scheme-relative URL
22+
// exactly as written, or empty when `url` does not spell one plainly.
23+
std::string WrittenAuthority(const std::string& url) {
24+
size_t start = 0;
25+
if (StartsWith(url, "//")) {
26+
start = 2;
27+
} else {
28+
const size_t colon = url.find(':');
29+
if (colon == std::string::npos || url.compare(colon, 3, "://") != 0) {
30+
return std::string();
31+
}
32+
start = colon + 3;
33+
}
34+
const size_t end = url.find_first_of("/?#", start);
35+
std::string authority =
36+
url.substr(start, end == std::string::npos ? end : end - start);
37+
for (const char c : authority) {
38+
if (static_cast<unsigned char>(c) <= 0x20 || c == '\\') {
39+
return std::string();
40+
}
41+
}
42+
return authority;
43+
}
44+
45+
} // namespace
46+
47+
bool IsHttpRegistryKey(const std::string& key) {
48+
if (key.compare(0, 4, "http") != 0) {
49+
return false;
50+
}
51+
const size_t schemeEnd = key.size() > 4 && key[4] == 's' ? 5 : 4;
52+
return key.compare(schemeEnd, 3, "://") == 0;
53+
}
54+
55+
ModuleUrl ParseModuleUrl(const std::string& input, const std::string& base) {
56+
ModuleUrl result;
57+
ada::result<ada::url_aggregator> url;
58+
if (base.empty()) {
59+
url = ada::parse<ada::url_aggregator>(input, nullptr);
60+
} else {
61+
auto baseUrl = ada::parse<ada::url_aggregator>(base, nullptr);
62+
if (!baseUrl) {
63+
return result;
64+
}
65+
url = ada::parse<ada::url_aggregator>(input, &baseUrl.value());
66+
}
67+
if (!url) {
68+
return result;
69+
}
70+
result.parsed = true;
71+
result.http = HasHttpScheme(*url);
72+
result.href = std::string(url->get_href());
73+
if (!result.http) {
74+
return result;
75+
}
76+
77+
const std::string authority =
78+
WrittenAuthority(base.empty() || StartsWith(input, "//") ? input : base);
79+
if (authority.empty()) {
80+
return result;
81+
}
82+
// Everything after the authority comes from the parser's own href: its
83+
// getters drop an empty query's '?' and an empty fragment's '#'.
84+
const std::string_view href = url->get_href();
85+
const std::string written =
86+
std::string(url->get_protocol()) + "//" + authority +
87+
std::string(href.substr(url->get_components().pathname_start));
88+
auto reparsed = ada::parse<ada::url_aggregator>(written, nullptr);
89+
if (reparsed && reparsed->get_href() == href) {
90+
result.href = written;
91+
}
92+
return result;
93+
}
94+
95+
std::string NormalizeHttpModuleUrl(const std::string& input) {
96+
const ModuleUrl url = ParseModuleUrl(input, std::string());
97+
if (url.parsed) {
98+
return url.http ? url.href : std::string();
99+
}
100+
return IsHttpRegistryKey(input) ? input : std::string();
101+
}
102+
103+
} // namespace tns

‎NativeScript/runtime/ModuleUrl.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace tns {
6+
7+
// Whether `key` is spelled the way the loader keys an HTTP module: a lowercase
8+
// `http://` or `https://` prefix, file modules being keyed by absolute paths.
9+
// Deliberately not a URL parse: it gates the rule that an HTTP importer never
10+
// reaches the filesystem implicitly, so a key the URL parser would reject (an
11+
// IPv6 zone id, say) must still count as HTTP.
12+
bool IsHttpRegistryKey(const std::string& key);
13+
14+
struct ModuleUrl {
15+
bool parsed = false;
16+
bool http = false; // the scheme is http or https
17+
std::string href;
18+
};
19+
20+
// `input` parsed with the WHATWG URL parser (the one behind JS `new URL()`):
21+
// as an absolute URL when `base` is empty, otherwise as a path- or
22+
// scheme-relative reference against `base`.
23+
//
24+
// An http(s) result keeps its authority as written by whichever string
25+
// supplied it: tooling compares module URLs against the origin string it was
26+
// configured with, so that spelling is part of a module's identity. The
27+
// parser's own href is used whenever the written authority would not name the
28+
// same URL.
29+
ModuleUrl ParseModuleUrl(const std::string& input, const std::string& base);
30+
31+
// The spelling the loader gives `input` as an absolute http(s) module URL:
32+
// ParseModuleUrl's href; `input` itself when the parser rejects it but it is
33+
// spelled as an HTTP key (fail closed: it stays HTTP); empty when `input` is
34+
// not an http(s) URL.
35+
std::string NormalizeHttpModuleUrl(const std::string& input);
36+
37+
} // namespace tns

‎TestRunner/app/tests/HttpEsmLoaderTests.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,27 @@ describe("HTTP ESM Loader", function() {
19341934
});
19351935
});
19361936

1937+
it("keeps the importer's origin as written when the query or fragment is empty",
1938+
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 written = origin.replace("127.0.0.1", "LOCALHOST");
1946+
importer(written, "authority-empty").then(function (mod) {
1947+
return Promise.all([mod.load("./query.mjs?"), mod.load("./hash.mjs#")]);
1948+
}).then(function (mods) {
1949+
expect(mods[0].url).toBe(written + "/esm/resolve/authority-empty/query.mjs?");
1950+
expect(mods[1].url).toBe(written + "/esm/resolve/authority-empty/hash.mjs");
1951+
done();
1952+
}).catch(function (error) {
1953+
expect("rejected: " + formatError(error)).toBe("resolved");
1954+
done();
1955+
});
1956+
});
1957+
19371958
it("normalizes a specifier the way the WHATWG URL parser does", function (done) {
19381959
var origin = getHostOrigin();
19391960
if (!origin) {

‎v8ios.xcodeproj/project.pbxproj‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
AA5DBFD42EC191F8008D12F9 /* HttpLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = AA5DBFD12EC191F8008D12F9 /* HttpLoader.h */; };
7171
AA8C47B12E27114300649BF5 /* ModuleInternalCallbacks.h in Headers */ = {isa = PBXBuildFile; fileRef = AA8C47AF2E27114300649BF5 /* ModuleInternalCallbacks.h */; };
7272
AA8C47B22E27114300649BF5 /* ModuleInternalCallbacks.mm in Sources */ = {isa = PBXBuildFile; fileRef = AA8C47B02E27114300649BF5 /* ModuleInternalCallbacks.mm */; };
73+
B0E1A0032F0A000100000001 /* ModuleUrl.h in Headers */ = {isa = PBXBuildFile; fileRef = B0E1A0012F0A000100000001 /* ModuleUrl.h */; };
74+
B0E1A0042F0A000100000001 /* ModuleUrl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0E1A0022F0A000100000001 /* ModuleUrl.cpp */; };
7375
C205257F2577D6F900C12A5C /* NativeScript.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C2DDEB32229EAB3B00345BFE /* NativeScript.framework */; };
7476
C20525802577D6F900C12A5C /* NativeScript.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C2DDEB32229EAB3B00345BFE /* NativeScript.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
7577
C20AB5E626E1015300E2B41D /* OneByteStringResource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C20AB5E426E1015200E2B41D /* OneByteStringResource.cpp */; };
@@ -550,6 +552,8 @@
550552
AA5DBFD22EC191F8008D12F9 /* HttpLoader.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = HttpLoader.mm; sourceTree = "<group>"; };
551553
AA8C47AF2E27114300649BF5 /* ModuleInternalCallbacks.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ModuleInternalCallbacks.h; sourceTree = "<group>"; };
552554
AA8C47B02E27114300649BF5 /* ModuleInternalCallbacks.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ModuleInternalCallbacks.mm; sourceTree = "<group>"; };
555+
B0E1A0012F0A000100000001 /* ModuleUrl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ModuleUrl.h; sourceTree = "<group>"; };
556+
B0E1A0022F0A000100000001 /* ModuleUrl.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ModuleUrl.cpp; sourceTree = "<group>"; };
553557
C2003F9E23FA78CD0043B815 /* TNSDerivedClass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TNSDerivedClass.h; sourceTree = "<group>"; };
554558
C20525A72577D86600C12A5C /* TNSWidgets.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = TNSWidgets.xcframework; sourceTree = "<group>"; };
555559
C20AB5E426E1015200E2B41D /* OneByteStringResource.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = OneByteStringResource.cpp; sourceTree = "<group>"; };
@@ -1561,6 +1565,8 @@
15611565
C2DDEB89229EAC8300345BFE /* ModuleInternal.h */,
15621566
AA8C47AF2E27114300649BF5 /* ModuleInternalCallbacks.h */,
15631567
AA8C47B02E27114300649BF5 /* ModuleInternalCallbacks.mm */,
1568+
B0E1A0012F0A000100000001 /* ModuleUrl.h */,
1569+
B0E1A0022F0A000100000001 /* ModuleUrl.cpp */,
15641570
C2DDEB81229EAC8200345BFE /* ModuleInternal.mm */,
15651571
2B7EA6AE2353477000E5184E /* NativeScriptException.h */,
15661572
2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */,
@@ -1820,6 +1826,7 @@
18201826
C247C16822F82842001D2CA2 /* libplatform-export.h in Headers */,
18211827
F1F30E762B58FC74006A62C0 /* URLSearchParamsImpl.h in Headers */,
18221828
AA8C47B12E27114300649BF5 /* ModuleInternalCallbacks.h in Headers */,
1829+
B0E1A0032F0A000100000001 /* ModuleUrl.h in Headers */,
18231830
C2DDEBA7229EAC8300345BFE /* ClassBuilder.h in Headers */,
18241831
6573B9ED291FE5B700B0ED7C /* JSIRuntime.h in Headers */,
18251832
AA5DBFD42EC191F8008D12F9 /* HttpLoader.h in Headers */,
@@ -2484,6 +2491,7 @@
24842491
4A5C201A2E2B000100000007 /* RuntimeBuiltins.cpp in Sources */,
24852492
C2A5F86A2359AEB600074AFA /* ExtVector.cpp in Sources */,
24862493
AA8C47B22E27114300649BF5 /* ModuleInternalCallbacks.mm in Sources */,
2494+
B0E1A0042F0A000100000001 /* ModuleUrl.cpp in Sources */,
24872495
C2DDEBAF229EAC8300345BFE /* ObjectManager.mm in Sources */,
24882496
1DE238EF3BDD0EF221E13C44 /* WrapperLifetime.mm in Sources */,
24892497
2A9D21FFAFE7419B7E69B585 /* WrapperLifetimeCanary.mm in Sources */,

0 commit comments

Comments
 (0)