Skip to content

Commit e6b5bf0

Browse files
committed
refactor(runtime): build the URL a module is reported under in one place
Script origins of classic scripts, file ES modules and JSON modules, import.meta.url and the worker inspector URL each built their own file:// URL from a path. ReportedModuleUrl in ModuleUrl now does it for all of them: an http(s) URL in the loader's spelling, any other string that carries a scheme unchanged, and otherwise file:// plus the path with the base directory removed (file:///app/ when empty). The outputs are unchanged except the worker inspector URL, which for an http(s) script is now the normalized URL and for any other scheme-carrying script is the script string itself rather than a file:// prefix glued onto it. The docs no longer describe the file://http… string, which is not a form the loader accepts.
1 parent 02e939d commit e6b5bf0

7 files changed

Lines changed: 62 additions & 39 deletions

File tree

‎NativeScript/runtime/ModuleInternal.mm‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,7 @@ throw NativeScriptException("Module evaluation interrupted by isolate terminatio
963963
}
964964
965965
auto context = isolate->GetCurrentContext();
966-
// build URL
967-
std::string base = ReplaceAll(canonicalPath, RuntimeConfig.BaseDir, "");
968-
std::string url = "file://" + base;
966+
const std::string url = ReportedModuleUrl(canonicalPath);
969967
970968
// wrap & cache lookup
971969
Local<v8::String> sourceText = ModuleInternal::WrapModuleContent(isolate, canonicalPath);
@@ -1019,8 +1017,7 @@ ScriptOrigin origin(urlString,
10191017
throw NativeScriptException("Cannot find module " + canonicalPath);
10201018
}
10211019
1022-
std::string base = ReplaceAll(canonicalPath, RuntimeConfig.BaseDir, "");
1023-
std::string url = "file://" + base;
1020+
const std::string url = ReportedModuleUrl(canonicalPath);
10241021
10251022
Local<v8::String> sourceText = ModuleInternal::WrapModuleContent(isolate, canonicalPath);
10261023
auto* cacheData = ModuleInternal::LoadScriptCache(canonicalPath, ScriptCacheKind::kEsModule);

‎NativeScript/runtime/ModuleInternalCallbacks.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ static void RejectAndClearInvalidatedModuleState(v8::Isolate* isolate,
27382738
const std::string& absPath,
27392739
const std::string& registryAbsPath) {
27402740
const std::string jsonText = tns::ReadText(absPath);
2741-
const std::string url = "file://" + ReplaceAll(absPath, RuntimeConfig.BaseDir, "");
2741+
const std::string url = ReportedModuleUrl(absPath);
27422742
return CompileJsonTextAsEsModule(isolate, context, jsonText, registryAbsPath, url);
27432743
}
27442744

‎NativeScript/runtime/ModuleUrl.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string_view>
44

5+
#include "RuntimeConfig.h"
56
#include "ada/ada.h"
67

78
namespace tns {
@@ -116,4 +117,44 @@ std::string NormalizeHttpModuleUrl(const std::string& input) {
116117
return IsHttpRegistryKey(input) ? input : std::string();
117118
}
118119

120+
bool HasUrlScheme(const std::string& s) {
121+
const size_t colon = s.find(':');
122+
if (colon == 0 || colon == std::string::npos) {
123+
return false;
124+
}
125+
const size_t slash = s.find('/');
126+
if (slash != std::string::npos && slash < colon) {
127+
return false;
128+
}
129+
for (size_t i = 0; i < colon; i++) {
130+
const char c = s[i];
131+
const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
132+
(c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.';
133+
if (!ok) {
134+
return false;
135+
}
136+
}
137+
return true;
138+
}
139+
140+
std::string ReportedModuleUrl(const std::string& keyOrPath) {
141+
if (keyOrPath.empty()) {
142+
return "file:///app/";
143+
}
144+
if (HasUrlScheme(keyOrPath)) {
145+
const std::string http = NormalizeHttpModuleUrl(keyOrPath);
146+
return http.empty() ? keyOrPath : http;
147+
}
148+
// Every occurrence, as tns::ReplaceAll removes it everywhere else.
149+
std::string path = keyOrPath;
150+
const std::string& baseDir = RuntimeConfig.BaseDir;
151+
if (!baseDir.empty()) {
152+
for (size_t pos = path.find(baseDir); pos != std::string::npos;
153+
pos = path.find(baseDir, pos)) {
154+
path.erase(pos, baseDir.size());
155+
}
156+
}
157+
return "file://" + path;
158+
}
159+
119160
} // namespace tns

‎NativeScript/runtime/ModuleUrl.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@ ModuleUrl ParseModuleUrl(const std::string& input, const std::string& base);
3434
// not an http(s) URL.
3535
std::string NormalizeHttpModuleUrl(const std::string& input);
3636

37+
// Whether `s` starts with a URL scheme and ':' (`node:fs`, `blob:…`,
38+
// `http://…`) rather than being a filesystem path.
39+
bool HasUrlScheme(const std::string& s);
40+
41+
// The URL a module or script is reported under (script origin,
42+
// import.meta.url, inspector target), from its registry key or path: an
43+
// http(s) URL as NormalizeHttpModuleUrl spells it; any other string that
44+
// carries a URL scheme unchanged; a filesystem path as `file://` plus the path
45+
// with RuntimeConfig.BaseDir removed, `file:///app/` for an empty one. The only
46+
// place a `file://` URL is built from a path.
47+
std::string ReportedModuleUrl(const std::string& keyOrPath);
48+
3749
} // namespace tns

‎NativeScript/runtime/Runtime.mm‎

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
#include "HttpLoader.h"
4444
#include "ModuleBinding.hpp"
4545
#include "ModuleInternalCallbacks.h"
46-
#include "napi/NapiEnv.h"
46+
#include "ModuleUrl.h"
4747
#include "URLImpl.h"
4848
#include "URLPatternImpl.h"
4949
#include "URLSearchParamsImpl.h"
50+
#include "napi/NapiEnv.h"
5051

5152
using namespace v8;
5253
using namespace std;
@@ -76,31 +77,7 @@ static void InitializeImportMetaObject(Local<Context> context, Local<Module> mod
7677

7778
std::string modulePath = tns::LookupModuleKeyForModule(isolate, module);
7879

79-
auto hasUrlScheme = [](const std::string& s) -> bool {
80-
if (s.empty()) return false;
81-
size_t colonPos = s.find(':');
82-
if (colonPos == 0 || colonPos == std::string::npos) return false;
83-
size_t slashPos = s.find('/');
84-
if (slashPos != std::string::npos && slashPos < colonPos) return false;
85-
for (size_t i = 0; i < colonPos; i++) {
86-
char c = s[i];
87-
const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
88-
c == '+' || c == '-' || c == '.';
89-
if (!ok) return false;
90-
}
91-
return true;
92-
};
93-
94-
// Compute import.meta.url.
95-
std::string moduleUrl;
96-
if (modulePath.empty()) {
97-
moduleUrl = "file:///app/";
98-
} else if (hasUrlScheme(modulePath)) {
99-
moduleUrl = modulePath;
100-
} else {
101-
std::string base = tns::ReplaceAll(modulePath, RuntimeConfig.BaseDir, "");
102-
moduleUrl = "file://" + base;
103-
}
80+
const std::string moduleUrl = tns::ReportedModuleUrl(modulePath);
10481

10582
Local<String> url =
10683
String::NewFromUtf8(isolate, moduleUrl.c_str(), NewStringType::kNormal).ToLocalChecked();
@@ -127,7 +104,7 @@ static void InitializeImportMetaObject(Local<Context> context, Local<Module> mod
127104
std::string dirname;
128105
if (modulePath.empty()) {
129106
dirname = "/app";
130-
} else if (hasUrlScheme(modulePath)) {
107+
} else if (tns::HasUrlScheme(modulePath)) {
131108
size_t schemeEnd = modulePath.find("://");
132109
size_t pathStart =
133110
(schemeEnd == std::string::npos) ? std::string::npos : modulePath.find('/', schemeEnd + 3);

‎NativeScript/runtime/WorkerWrapper.mm‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,7 @@ static void PostThreadEndedNotification(Isolate* mainIsolate, std::weak_ptr<Even
485485
}
486486
487487
// Same url scheme the module loader reports in Debugger.scriptParsed.
488-
std::string url = NormalizeHttpModuleUrl(scriptPath).empty()
489-
? "file://" + ReplaceAll(scriptPath, RuntimeConfig.BaseDir, "")
490-
: scriptPath;
488+
std::string url = ReportedModuleUrl(scriptPath);
491489
492490
auto* client =
493491
new v8_inspector::WorkerInspectorClient(this->workerId_, isolate, CFRunLoopGetCurrent(), url);

‎docs/ns-builtin-modules.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,7 @@ The registry keys modules by a canonical URL. Before the vocabulary applies,
705705
an absolute http(s) URL is spelled by the WHATWG URL parser with its authority
706706
as written, whichever way it arrives — a specifier from any importer, an
707707
import-map target, or `invalidateModules` — so `HTTP://host/a/../x.js` and
708-
`http:/host/x.js` key as `http://host/x.js`. A `file://http://…` string is not
709-
an http(s) URL: it keys as itself, and `import()` of it fails with
710-
`Cannot find module` without fetching anything. The mechanism — fragment strip,
708+
`http:/host/x.js` key as `http://host/x.js`. The mechanism — fragment strip,
711709
cache-buster param drop, param sort — is the runtime's; the *vocabulary* is
712710
server policy, supplied here:
713711

0 commit comments

Comments
 (0)