Skip to content

Commit f559b3c

Browse files
committed
feat(runtime): make a file module's query part of its identity
As in browsers and Node, ./x.mjs?v=1, ./x.mjs?v=2, ./x.mjs? and ./x.mjs are four instances of one file, each evaluated once. A file module's registry key is its canonical path plus the specifier's query (after import-map rewriting) as the URL parser serializes it; the fragment is not part of it. A static import and an import() of one queried specifier share an instance, and an import-map target's query names its instance. import.meta.url carries the query. The file read, the code cache (one .mcache per file), import.meta.dirname, relative resolution, import-map scopes, a relative Worker's directory and the inspector's file URL reads use the path. FileModulePath turns a key or a reported file:// URL into that path, and every place that read one as a path goes through it. The entry module, a worker's script and require() of an ES module load the instance without a query. getLoadedModuleUrls keeps skipping file modules whatever their query holds, and invalidateModules given a file:// URL with a query drops only that instance.
1 parent 9bd822c commit f559b3c

17 files changed

Lines changed: 340 additions & 55 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const counts = (globalThis.__queryIdentityEvaluations ??= {});
2+
counts[import.meta.url] = (counts[import.meta.url] || 0) + 1;
3+
4+
export const url = import.meta.url;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function load(spec) {
2+
return import(spec);
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createRequire } from "ns:module";
2+
import { name as siblingName } from "./sibling.mjs";
3+
4+
export const url = import.meta.url;
5+
export const search = new URL(import.meta.url).search;
6+
export const dirname = import.meta.dirname;
7+
export const staticSibling = siblingName;
8+
export const required = createRequire(import.meta.url)("./sibling-cjs.js").name;
9+
10+
export function dynamicSibling() {
11+
return import("./sibling.mjs").then((m) => m.name);
12+
}
13+
14+
export function scopedImport() {
15+
return import("ns-query-identity-scoped").then((m) => m.name);
16+
}
17+
18+
export function startWorker() {
19+
return new Worker("./query-worker.js");
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postMessage("query-worker-ready");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = "scoped-target";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.name = "sibling-cjs";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = "sibling";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as dep from "ns-query-identity-mapped";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as dep from "./counted.mjs?v=1";

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

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,180 @@ describe("import() resolution table", function () {
288288
});
289289
});
290290
});
291+
292+
// A file module's query is part of its identity, as in browsers and Node; its
293+
// fragment is not, as for an http(s) module here.
294+
describe("file module instances per query", function () {
295+
var nsModule = require("ns:module");
296+
var dir = "~/esm/query-identity/";
297+
var appRoot = __dirname.slice(0, __dirname.length - "/tests".length);
298+
var urlBase = "file://" + appRoot + "/esm/query-identity/";
299+
300+
function evaluations(url) {
301+
var counts = globalThis.__queryIdentityEvaluations || {};
302+
return counts[url] || 0;
303+
}
304+
305+
function load(spec) {
306+
return import(dir + "importer.mjs").then(function (m) { return m.load(spec); });
307+
}
308+
309+
function fail(done) {
310+
return function (e) {
311+
expect("rejected: " + String((e && e.message) || e)).toBe("resolved");
312+
done();
313+
};
314+
}
315+
316+
afterEach(function () {
317+
nsModule.configureLoader({ importMap: { imports: {} } });
318+
});
319+
320+
it("gives a static import and an import() with one query one instance", function (done) {
321+
Promise.all([import(dir + "static-v1.mjs"), load("./counted.mjs?v=1")]).then(function (r) {
322+
expect(r[0].dep).toBe(r[1]);
323+
expect(r[1].url).toBe(urlBase + "counted.mjs?v=1");
324+
expect(evaluations(urlBase + "counted.mjs?v=1")).toBe(1);
325+
done();
326+
}, fail(done));
327+
});
328+
329+
it("gives two queries, an empty query and none four instances, each evaluated once",
330+
function (done) {
331+
Promise.all([load("./counted.mjs?v=1"), load("./counted.mjs?v=2"), load("./counted.mjs?"),
332+
load("./counted.mjs")]).then(function (r) {
333+
expect(r.map(function (ns) { return ns.url; })).toEqual([
334+
urlBase + "counted.mjs?v=1",
335+
urlBase + "counted.mjs?v=2",
336+
urlBase + "counted.mjs?",
337+
urlBase + "counted.mjs",
338+
]);
339+
expect([r[0] === r[1], r[0] === r[3], r[1] === r[3], r[2] === r[3]])
340+
.toEqual([false, false, false, false]);
341+
expect(r.map(function (ns) { return evaluations(ns.url); })).toEqual([1, 1, 1, 1]);
342+
done();
343+
}, fail(done));
344+
});
345+
346+
it("keeps the query of an import-map target in the module's identity", function (done) {
347+
nsModule.configureLoader({
348+
importMap: { imports: { "ns-query-identity-mapped": dir + "counted.mjs?v=map" } },
349+
});
350+
Promise.all([import(dir + "static-mapped.mjs"), load("ns-query-identity-mapped"),
351+
load("./counted.mjs?v=map")]).then(function (r) {
352+
expect([r[0].dep === r[1], r[1] === r[2]]).toEqual([true, true]);
353+
expect(r[1].url).toBe(urlBase + "counted.mjs?v=map");
354+
expect(evaluations(urlBase + "counted.mjs?v=map")).toBe(1);
355+
done();
356+
}, fail(done));
357+
});
358+
359+
it("does not split instances by fragment", function (done) {
360+
Promise.all([load("./counted.mjs#a"), load("./counted.mjs#b"), load("./counted.mjs"),
361+
load("./counted.mjs?v=1#c"), load("./counted.mjs?v=1")]).then(function (r) {
362+
expect([r[0] === r[2], r[1] === r[2], r[3] === r[4]]).toEqual([true, true, true]);
363+
expect([r[0].url, r[3].url]).toEqual([urlBase + "counted.mjs", urlBase + "counted.mjs?v=1"]);
364+
done();
365+
}, fail(done));
366+
});
367+
368+
it("gives require() of the file the instance without a query", function (done) {
369+
Promise.all([load("./counted.mjs"), load("./counted.mjs?v=1")]).then(function (r) {
370+
var required = require("../esm/query-identity/counted.mjs");
371+
expect([required === r[0], required === r[1]]).toEqual([true, false]);
372+
done();
373+
}, fail(done));
374+
});
375+
376+
it("invalidates the instance a file:// URL's query names and no other", function (done) {
377+
var invalidated = urlBase + "counted.mjs?v=inv";
378+
Promise.all([load("./counted.mjs?v=inv"), load("./counted.mjs")]).then(function (first) {
379+
nsModule.invalidateModules([invalidated]);
380+
return Promise.all([load("./counted.mjs?v=inv"), load("./counted.mjs")]).then(function (second) {
381+
expect([first[0] === second[0], first[1] === second[1]]).toEqual([false, true]);
382+
expect(evaluations(invalidated)).toBe(2);
383+
done();
384+
});
385+
}).catch(fail(done));
386+
});
387+
388+
describe("inside a module imported with a query containing '/', '..' and '://'", function () {
389+
var query = "?a=/../../y&u=http://h/z";
390+
var probe;
391+
392+
function scopeOnFixtureDir() {
393+
var scopes = {};
394+
scopes[appRoot + "/esm/query-identity/"] = {
395+
"ns-query-identity-scoped": dir + "scoped-target.mjs",
396+
};
397+
nsModule.configureLoader({ importMap: { imports: {}, scopes: scopes } });
398+
}
399+
400+
beforeAll(function (done) {
401+
scopeOnFixtureDir();
402+
load("./probe.mjs" + query).then(function (ns) {
403+
probe = ns;
404+
done();
405+
}, function (e) {
406+
probe = { error: String((e && e.message) || e) };
407+
done();
408+
});
409+
});
410+
411+
it("loads", function () {
412+
expect(probe.error).toBeUndefined();
413+
});
414+
415+
it("reports the query in import.meta.url and not in import.meta.dirname", function () {
416+
expect(probe.url).toBe(urlBase + "probe.mjs" + query);
417+
expect(probe.search).toBe(query);
418+
expect(probe.dirname).toBe(appRoot + "/esm/query-identity");
419+
});
420+
421+
it("resolves relative imports against the module's directory", function (done) {
422+
expect(probe.staticSibling).toBe("sibling");
423+
expect(probe.required).toBe("sibling-cjs");
424+
probe.dynamicSibling().then(function (name) {
425+
expect(name).toBe("sibling");
426+
done();
427+
}, fail(done));
428+
});
429+
430+
it("matches an import-map scope on the module's directory", function (done) {
431+
scopeOnFixtureDir();
432+
probe.scopedImport().then(function (name) {
433+
expect(name).toBe("scoped-target");
434+
done();
435+
}, fail(done));
436+
});
437+
438+
it("starts a relative worker from the module's directory", function (done) {
439+
var worker = probe.startWorker();
440+
var timer = setTimeout(function () {
441+
worker.terminate();
442+
expect("no message").toBe("query-worker-ready");
443+
done();
444+
}, 10000);
445+
worker.onerror = function (e) {
446+
clearTimeout(timer);
447+
worker.terminate();
448+
expect("error: " + String((e && e.message) || e)).toBe("query-worker-ready");
449+
done();
450+
};
451+
worker.onmessage = function (msg) {
452+
clearTimeout(timer);
453+
worker.terminate();
454+
expect(msg.data).toBe("query-worker-ready");
455+
done();
456+
};
457+
}, 15000);
458+
459+
it("is not listed by getLoadedModuleUrls, like any file module", function () {
460+
var listed = nsModule.getLoadedModuleUrls().filter(function (u) {
461+
return u.indexOf("query-identity") >= 0;
462+
});
463+
expect(listed).toEqual([]);
464+
});
465+
});
466+
});
467+

0 commit comments

Comments
 (0)