Skip to content

Commit a3bbfdf

Browse files
committed
fix: worker url resolution with ~ beginning
1 parent 60d353d commit a3bbfdf

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

NativeScript/runtime/ModuleInternal.mm

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ bool IsLikelyOptionalModule(const std::string& moduleName) {
2929
return false;
3030
}
3131

32+
// Helper function to check if a file path is an ES module (.mjs) but not a source map (.mjs.map)
33+
bool IsESModule(const std::string& path) {
34+
return path.size() >= 4 && path.compare(path.size() - 4, 4, ".mjs") == 0 &&
35+
!(path.size() >= 8 && path.compare(path.size() - 8, 8, ".mjs.map") == 0);
36+
}
37+
3238
// Helper function to resolve main entry from package.json with proper extension handling
3339
std::string ResolveMainEntryFromPackageJson(const std::string& baseDir) {
3440
// Get the main value from package.json
@@ -139,7 +145,7 @@ bool IsLikelyOptionalModule(const std::string& moduleName) {
139145
}
140146

141147
// Check if this is an ES module (.mjs) and handle it directly
142-
if (path.size() >= 4 && path.compare(path.size() - 4, 4, ".mjs") == 0) {
148+
if (IsESModule(path)) {
143149
// For ES modules, use LoadESModule directly instead of require()
144150
TryCatch tc(isolate);
145151
Local<Value> moduleNamespace;
@@ -508,7 +514,7 @@ bool IsLikelyOptionalModule(const std::string& moduleName) {
508514
}
509515

510516
// Check if this is an ES module
511-
bool isESM = modulePath.size() >= 4 && modulePath.compare(modulePath.size() - 4, 4, ".mjs") == 0;
517+
bool isESM = IsESModule(modulePath);
512518
std::shared_ptr<Caches> cache = Caches::Get(isolate);
513519

514520
if (isESM) {
@@ -662,7 +668,7 @@ throw NativeScriptException(isolate,
662668
}
663669

664670
Local<Value> ModuleInternal::LoadScript(Isolate* isolate, const std::string& path) {
665-
if (path.size() >= 4 && path.compare(path.size() - 4, 4, ".mjs") == 0) {
671+
if (IsESModule(path)) {
666672
// Treat all .mjs files as standard ES modules.
667673
return ModuleInternal::LoadESModule(isolate, path);
668674
}
@@ -1070,7 +1076,8 @@ ScriptOrigin origin(isolate, urlString, 0, 0, false, -1, Local<Value>(), false,
10701076
std::shared_ptr<Caches> cache = Caches::Get(isolate);
10711077
bool isWorkerContext = cache && cache->isWorker;
10721078

1073-
if (path.size() >= 4 && path.compare(path.size() - 4, 4, ".mjs") == 0) {
1079+
// Check if this is an .mjs file but NOT a .mjs.map file
1080+
if (IsESModule(path)) {
10741081
// Read raw text without wrapping.
10751082
std::string sourceText = tns::ReadText(path);
10761083

NativeScript/runtime/Worker.mm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "NativeScriptException.h"
88
#include "ObjectManager.h"
99
#include "Runtime.h"
10+
#include "RuntimeConfig.h"
1011

1112
using namespace v8;
1213

@@ -110,6 +111,15 @@ throw NativeScriptException(
110111
std::shared_ptr<Persistent<Value>> poWorker = ObjectManager::Register(context, thiz);
111112

112113
std::function<Isolate*()> func([worker, workerPath]() {
114+
// Resolve tilde paths before creating the runtime
115+
std::string resolvedPath = workerPath;
116+
if (!workerPath.empty() && workerPath[0] == '~') {
117+
// Convert ~/path to ApplicationPath/path
118+
std::string tail = workerPath.size() >= 2 && workerPath[1] == '/' ? workerPath.substr(2)
119+
: workerPath.substr(1);
120+
resolvedPath = RuntimeConfig.ApplicationPath + "/" + tail;
121+
}
122+
113123
tns::Runtime* runtime = new tns::Runtime();
114124
Isolate* isolate = runtime->CreateIsolate();
115125
v8::Locker locker(isolate);
@@ -121,7 +131,7 @@ throw NativeScriptException(
121131
TryCatch tc(isolate);
122132

123133
// Debug: Log worker execution
124-
// printf("Worker: About to run module: %s\n", workerPath.c_str());
134+
// printf("Worker: About to run module: %s\n", resolvedPath.c_str());
125135

126136
// Debug: Check if console exists in worker context
127137
// {
@@ -136,7 +146,7 @@ throw NativeScriptException(
136146
// }
137147
// }
138148

139-
runtime->RunModule(workerPath);
149+
runtime->RunModule(resolvedPath);
140150

141151
if (tc.HasCaught()) {
142152
Isolate::Scope isolate_scope(isolate);

0 commit comments

Comments
 (0)