Skip to content

Commit 9bd822c

Browse files
committed
fix(runtime): look a blob URL up without its fragment
import() of a blob: URL now follows the File API's blob URL resolution from every importer: the store is looked up by the URL without its fragment and with its query, so blobUrl + '#f' loads the blob and blobUrl + '?x' rejects with "Blob not found". The module is registered under that same URL, so a blob URL with and without a fragment is one module.
1 parent fca06cb commit 9bd822c

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe("import() resolution table", function () {
149149
["esm", "ns:import-pin-missing", "rejected:Error:No such built-in module: ns:import-pin-missing"],
150150
["esm", function () { return blobUrl; }, "ok:blob"],
151151
["esm", function () { return blobUrl + "?x"; }, "rejected:Error:Blob not found: <blob>?x"],
152-
["esm", function () { return blobUrl + "#f"; }, "rejected:Error:Blob not found: <blob>#f"],
152+
["esm", function () { return blobUrl + "#f"; }, "ok:blob"],
153153
["esm", "./missing.mjs", "rejected:Error:Cannot find module './missing.mjs' (tried <app>/esm/import-pin/missing.mjs)"],
154154
["esm", "~/esm/import-pin/missing.mjs", "rejected:Error:Cannot find module '~/esm/import-pin/missing.mjs' (tried <app>/app/esm/import-pin/missing.mjs)"],
155155
["esm", "./syntax-error.mjs", "rejected:SyntaxError:Unexpected token ';'"],
@@ -178,7 +178,7 @@ describe("import() resolution table", function () {
178178
["cjs", "ns:module", "ok:createRequire=function"],
179179
["cjs", function () { return blobUrl; }, "ok:blob"],
180180
["cjs", function () { return blobUrl + "?x"; }, "rejected:Error:Blob not found: <blob>?x"],
181-
["cjs", function () { return blobUrl + "#f"; }, "rejected:Error:Blob not found: <blob>#f"],
181+
["cjs", function () { return blobUrl + "#f"; }, "ok:blob"],
182182
["cjs", "./missing.mjs", "rejected:Error:Cannot find module './missing.mjs' (tried <app>/esm/import-pin/missing.mjs)"],
183183
["cjs", "./throws.mjs", "rejected:TypeError:import-pin evaluation error"],
184184

@@ -191,7 +191,7 @@ describe("import() resolution table", function () {
191191
["newFunction", "/esm/import-pin/leaf.mjs", "ok:leaf"],
192192
["newFunction", "file:///app/esm/import-pin/leaf.mjs", "ok:leaf"],
193193
["newFunction", "ns:module", "ok:createRequire=function"],
194-
["newFunction", function () { return blobUrl + "#f"; }, "rejected:Error:Blob not found: <blob>#f"],
194+
["newFunction", function () { return blobUrl + "#f"; }, "ok:blob"],
195195

196196
["indirectEval", "./leaf.mjs", "rejected:Error:Cannot find module './leaf.mjs' (tried <app>/tests/leaf.mjs)"],
197197
["indirectEval", "./esm/import-pin/leaf.mjs", "rejected:Error:Cannot find module './esm/import-pin/leaf.mjs' (tried <app>/tests/esm/import-pin/leaf.mjs)"],
@@ -214,7 +214,7 @@ describe("import() resolution table", function () {
214214
["http", "ns:module", "ok:createRequire=function"],
215215
["http", function () { return blobUrl; }, "ok:blob"],
216216
["http", function () { return blobUrl + "?x"; }, "rejected:Error:Blob not found: <blob>?x"],
217-
["http", function () { return blobUrl + "#f"; }, "rejected:Error:Blob not found: <blob>#f"],
217+
["http", function () { return blobUrl + "#f"; }, "ok:blob"],
218218
];
219219

220220
var rowCount = 0;
@@ -268,6 +268,17 @@ describe("import() resolution table", function () {
268268
});
269269
});
270270

271+
it("from esm: the same namespace for a blob URL with and without a fragment", function (done) {
272+
Promise.all([importers.esm(blobUrl), importers.esm(blobUrl + "#g"),
273+
importers.cjs(blobUrl + "#h")]).then(function (r) {
274+
expect([r[0] === r[1], r[0] === r[2]]).toEqual([true, true]);
275+
done();
276+
}, function (e) {
277+
expect("rejected:" + describeError(e)).toBe("resolved");
278+
done();
279+
});
280+
});
281+
271282
it("from esm: JSON with a type attribute", function (done) {
272283
import(dir + "importer.mjs").then(function (m) {
273284
return outcome(m.load("./data.json", { with: { type: "json" } }));

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,15 @@ v8::MaybeLocal<v8::Promise> ImportModuleDynamicallyCallback(
32383238
normalizedSpec = dynamicResolution.url;
32393239
}
32403240

3241+
// A blob URL is looked up, and keyed, the way the File API resolves it:
3242+
// without its fragment, with its query.
3243+
{
3244+
const std::string blobUrl = BlobStoreUrl(normalizedSpec);
3245+
if (!blobUrl.empty()) {
3246+
normalizedSpec = blobUrl;
3247+
}
3248+
}
3249+
32413250
try {
32423251
// ── Blob URL support (e.g. blob:nativescript/<uuid>) ──
32433252
// Retrieve the blob content from the global BLOB_STORE via

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ std::string NormalizeHttpModuleUrl(const std::string& input) {
116116
return IsHttpRegistryKey(input) ? input : std::string();
117117
}
118118

119+
std::string BlobStoreUrl(const std::string& input) {
120+
// Fast reject only, as in NormalizeHttpModuleUrl.
121+
size_t first = 0;
122+
while (first < input.size() &&
123+
static_cast<unsigned char>(input[first]) <= 0x20) {
124+
first++;
125+
}
126+
if (first == input.size() || (input[first] != 'b' && input[first] != 'B')) {
127+
return std::string();
128+
}
129+
auto url = ada::parse<ada::url_aggregator>(input, nullptr);
130+
if (!url || url->get_protocol() != "blob:") {
131+
return std::string();
132+
}
133+
url->set_hash("");
134+
return std::string(url->get_href());
135+
}
136+
119137
bool HasUrlScheme(const std::string& s) {
120138
const size_t colon = s.find(':');
121139
if (colon == 0 || colon == std::string::npos) {

‎test-app/runtime/src/main/cpp/ModuleUrl.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ 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+
// The key the blob URL store is looked up by for `input`, per the File API's
38+
// blob URL resolution: the URL serialized without its fragment, a query kept.
39+
// Empty when `input` is not an absolute blob: URL.
40+
std::string BlobStoreUrl(const std::string& input);
41+
3742
// Whether `s` starts with a URL scheme and ':' (`node:fs`, `blob:…`,
3843
// `http://…`) rather than being a filesystem path.
3944
bool HasUrlScheme(const std::string& s);

0 commit comments

Comments
 (0)