Skip to content

Commit d9f3f98

Browse files
committed
fix(runtime): keep a '#' in a script path read from a file URL
A script's reported file:// URL spells its path verbatim and appends only a module key's query, so a '#' in it is part of a directory or file name, never a fragment. The import() referrer key and createRequire() cut such URLs at the first '#', which resolved a relative import() from a script or module under a directory named with '#' against its parent, and pointed createRequire(import.meta.url) there too. Both now cut at the query only, as FileModulePath does.
1 parent 0a20ae1 commit d9f3f98

3 files changed

Lines changed: 126 additions & 25 deletions

File tree

‎test-app/app/src/main/assets/app/tests/testDynamicImport.js‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,120 @@ describe("file module instances per query", function () {
465465
});
466466
});
467467

468+
469+
// A script's URL spells its path verbatim, so a '#' in a directory name is part
470+
// of the path there, not a fragment. A specifier is a URL, so the fixtures are
471+
// reached through require(), which takes a path. Written at run time: the
472+
// fixture lives in the app's files directory only for the duration of the
473+
// specs.
474+
describe("a script in a directory whose name contains '#'", function () {
475+
var appRoot = __dirname.slice(0, __dirname.length - "/tests".length);
476+
var fixtureDir = appRoot + "/esm/hash#dir";
477+
478+
function writeFile(path, body) {
479+
var writer = new java.io.OutputStreamWriter(new java.io.FileOutputStream(path), "UTF-8");
480+
try {
481+
writer.write(body);
482+
} finally {
483+
writer.close();
484+
}
485+
}
486+
487+
beforeAll(function () {
488+
new java.io.File(fixtureDir).mkdirs();
489+
writeFile(fixtureDir + "/spawner.js",
490+
"exports.start = function () { return new Worker(\"./w.js\"); };\n");
491+
writeFile(fixtureDir + "/w.js", "postMessage(\"hash-dir-worker\");\n");
492+
writeFile(fixtureDir + "/loader.js",
493+
"exports.loadSibling = function () {\n" +
494+
" return import(\"./sibling.mjs\").then(function (m) { return m.name; });\n" +
495+
"};\n" +
496+
"exports.loadModule = function () { return import(\"./module.mjs\"); };\n");
497+
writeFile(fixtureDir + "/module.mjs",
498+
"import { createRequire } from \"ns:module\";\n" +
499+
"export function loadSibling() {\n" +
500+
" return import(\"./sibling.mjs\").then((m) => m.name);\n" +
501+
"}\n" +
502+
"export function requireSibling() {\n" +
503+
" return createRequire(import.meta.url)(\"./sibling-cjs.js\").name;\n" +
504+
"}\n");
505+
writeFile(fixtureDir + "/sibling.mjs", "export const name = \"hash-dir-sibling\";\n");
506+
writeFile(fixtureDir + "/sibling-cjs.js", "exports.name = \"hash-dir-sibling-cjs\";\n");
507+
});
508+
509+
function loader() {
510+
return require(fixtureDir + "/loader.js");
511+
}
512+
513+
function fail(done) {
514+
return function (e) {
515+
expect("rejected: " + String((e && e.message) || e)).toBe("resolved");
516+
done();
517+
};
518+
}
519+
520+
it("resolves a relative import() from a script there", function (done) {
521+
loader().loadSibling().then(function (name) {
522+
expect(name).toBe("hash-dir-sibling");
523+
done();
524+
}, fail(done));
525+
});
526+
527+
it("resolves a relative import() from a module there", function (done) {
528+
loader().loadModule().then(function (m) {
529+
return m.loadSibling();
530+
}).then(function (name) {
531+
expect(name).toBe("hash-dir-sibling");
532+
done();
533+
}, fail(done));
534+
});
535+
536+
it("creates a require from a module's import.meta.url there", function (done) {
537+
loader().loadModule().then(function (m) {
538+
expect(m.requireSibling()).toBe("hash-dir-sibling-cjs");
539+
done();
540+
}, fail(done));
541+
});
542+
543+
afterAll(function () {
544+
var dirFile = new java.io.File(fixtureDir);
545+
var names = dirFile.list();
546+
if (names !== null) {
547+
for (var i = 0; i < names.length; i++) {
548+
new java.io.File(dirFile, names[i]).delete();
549+
}
550+
}
551+
dirFile.delete();
552+
});
553+
554+
it("starts a relative worker from that directory", function (done) {
555+
var worker;
556+
var timer = setTimeout(function () {
557+
if (worker) {
558+
worker.terminate();
559+
}
560+
expect("no message").toBe("hash-dir-worker");
561+
done();
562+
}, 10000);
563+
function finish(actual) {
564+
clearTimeout(timer);
565+
if (worker) {
566+
worker.terminate();
567+
}
568+
expect(actual).toBe("hash-dir-worker");
569+
done();
570+
}
571+
try {
572+
worker = require(fixtureDir + "/spawner.js").start();
573+
} catch (e) {
574+
finish("threw: " + String((e && e.message) || e));
575+
return;
576+
}
577+
worker.onerror = function (e) {
578+
finish("error: " + String((e && e.message) || e));
579+
};
580+
worker.onmessage = function (msg) {
581+
finish(msg.data);
582+
};
583+
}, 15000);
584+
});

‎test-app/runtime/src/main/cpp/ModuleInternalCallbacks.cpp‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,6 @@ static std::string NormalizePath(const std::string& path) {
139139
return NormalizeDotSegments(path);
140140
}
141141

142-
// Convert a file:// URL to a filesystem path. Handles both file:///a/b and
143-
// file:/a/b variants. Percent-decoding is deliberately omitted — the runtime
144-
// only emits ASCII file:// URLs internally.
145-
static std::string FileURLToPath(const std::string& url) {
146-
if (url.empty()) return url;
147-
if (!StartsWith(url, "file://")) return url;
148-
std::string tail = url.substr(7);
149-
// Strip host component when present (file://host/path → /path). NS never
150-
// emits a host, but be tolerant.
151-
if (!tail.empty() && tail[0] != '/') {
152-
size_t slash = tail.find('/');
153-
tail = (slash == std::string::npos) ? std::string() : tail.substr(slash);
154-
}
155-
// Drop query and fragment — these have no meaning for filesystem paths.
156-
size_t cut = tail.find_first_of("?#");
157-
if (cut != std::string::npos) tail = tail.substr(0, cut);
158-
return NormalizePath(tail);
159-
}
160-
161142
// Forward declarations for helpers referenced before their definitions.
162143
static const char* ModuleStatusToString(v8::Module::Status status);
163144
static void KillAsyncGraphLoadsForIsolate(v8::Isolate* isolate);
@@ -802,7 +783,12 @@ std::string CanonicalizeRegistryKey(const std::string& key) {
802783
registryKey = CanonicalizeNormalizedHttpUrlKey(httpUrl);
803784
classification = "http";
804785
} else if (StartsWith(key, "file://")) {
805-
registryKey = NormalizePath(FileURLToPath(key)) + ModuleQuery(key);
786+
// The path is spelled verbatim, so a '#' before the query is part of it;
787+
// only after the query does one start a fragment.
788+
const size_t queryAt = key.find('?');
789+
registryKey = NormalizePath(FileModulePath(key)) +
790+
(queryAt == std::string::npos ? std::string()
791+
: ModuleQuery(key.substr(queryAt)));
806792
classification = "file-url";
807793
} else if (StartsWith(key, "blob:")) {
808794
registryKey = key;

‎test-app/runtime/src/main/cpp/js/ns-module.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ function fileUrlToPath(href) {
4949
}
5050
rest = StringPrototypeSlice(rest, authorityEnd);
5151

52-
// The query and fragment are URL syntax, never part of the path.
52+
// The query is URL syntax, never part of the path. A '#' is: the runtime
53+
// spells a module's path verbatim after file:// (import.meta.url), so there
54+
// it names a directory or file, and such a URL never carries a fragment.
5355
const queryAt = StringPrototypeIndexOf(rest, "?");
5456
if (queryAt >= 0) {
5557
rest = StringPrototypeSlice(rest, 0, queryAt);
5658
}
57-
const hashAt = StringPrototypeIndexOf(rest, "#");
58-
if (hashAt >= 0) {
59-
rest = StringPrototypeSlice(rest, 0, hashAt);
60-
}
6159

6260
try {
6361
return decodeURIComponent(rest);

0 commit comments

Comments
 (0)