fix(replay): link the complete ESM graph before evaluating (#515)
* fix(replay): link the complete ESM graph before evaluating Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com> * fix(replay): rename shadowed module bindings in ESM link graph --------- Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
morluto
parent
9975bd23cd
commit
4200a425d5
@@ -213,7 +213,7 @@ const loadEntry = async (
|
||||
return module.exports;
|
||||
};
|
||||
|
||||
const loadEsm = async (alias: string): Promise<Module> => {
|
||||
const loadEsm = (alias: string): Module => {
|
||||
const cached = esmCache.get(alias);
|
||||
if (cached !== undefined) return cached;
|
||||
const descriptor = requiredModule(modules, alias);
|
||||
@@ -245,13 +245,6 @@ const loadEntry = async (
|
||||
});
|
||||
}
|
||||
esmCache.set(alias, module);
|
||||
await module.link(async (specifier) => {
|
||||
const dependency = descriptor.dependencies[specifier];
|
||||
if (dependency === undefined)
|
||||
throw new ReplayDeniedError(`Undeclared import: ${specifier}`);
|
||||
return loadEsm(dependency);
|
||||
});
|
||||
await module.evaluate();
|
||||
return module;
|
||||
};
|
||||
|
||||
@@ -262,7 +255,19 @@ const loadEntry = async (
|
||||
? (exported.default ?? exported)
|
||||
: exported[side.entryExport];
|
||||
}
|
||||
const namespace = (await loadEsm(side.entryAlias)).namespace;
|
||||
const entryModule = loadEsm(side.entryAlias);
|
||||
await entryModule.link((specifier, referencingModule) => {
|
||||
const descriptor = requiredModule(
|
||||
modules,
|
||||
referencingModule.identifier.slice("rea:".length),
|
||||
);
|
||||
const dependency = descriptor.dependencies[specifier];
|
||||
if (dependency === undefined)
|
||||
throw new ReplayDeniedError(`Undeclared import: ${specifier}`);
|
||||
return loadEsm(dependency);
|
||||
});
|
||||
await entryModule.evaluate();
|
||||
const namespace = entryModule.namespace;
|
||||
return Reflect.get(namespace, side.entryExport);
|
||||
};
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ import { resolve } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { WorkerModule } from "../../../../src/replay/JavaScriptReplayWorkerTypes.js";
|
||||
|
||||
const fixture = async (name: string): Promise<string> =>
|
||||
readFile(resolve("tests/fixtures/replay", name), "utf8");
|
||||
|
||||
@@ -226,6 +228,138 @@ describe("disposable JavaScript replay worker", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("replay ESM graph linking", () => {
|
||||
const esmModule = (
|
||||
alias: string,
|
||||
source: string,
|
||||
dependencies: Record<string, string> = {},
|
||||
): WorkerModule => ({ alias, source, dependencies, format: "esm" });
|
||||
it.each([
|
||||
{
|
||||
name: "two-module cycle",
|
||||
value: 8,
|
||||
modules: [
|
||||
esmModule(
|
||||
"entry",
|
||||
"import { value } from './dep'; export default function(){ return value(); } export function base(){ return 7; }",
|
||||
{ "./dep": "dep" },
|
||||
),
|
||||
esmModule(
|
||||
"dep",
|
||||
"import { base } from './entry'; export function value(){ return base()+1; }",
|
||||
{ "./entry": "entry" },
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "three-module cycle",
|
||||
value: 8,
|
||||
modules: [
|
||||
esmModule(
|
||||
"entry",
|
||||
"import { value } from './dep'; export default function(){ return value(); } export function base(){ return 7; }",
|
||||
{ "./dep": "dep" },
|
||||
),
|
||||
esmModule(
|
||||
"dep",
|
||||
"import { value as next } from './third'; export function value(){ return next(); }",
|
||||
{ "./third": "third" },
|
||||
),
|
||||
esmModule(
|
||||
"third",
|
||||
"import { base } from './entry'; export function value(){ return base()+1; }",
|
||||
{ "./entry": "entry" },
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "acyclic evaluation order",
|
||||
value: 8,
|
||||
modules: [
|
||||
esmModule(
|
||||
"entry",
|
||||
"import { value } from './dep'; const answer=value+1; export default function(){ return answer; }",
|
||||
{ "./dep": "dep" },
|
||||
),
|
||||
esmModule("dep", "export const value=7;"),
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "diamond single initialization",
|
||||
value: [1, 1],
|
||||
modules: [
|
||||
esmModule(
|
||||
"entry",
|
||||
"import { a } from './a'; import { b } from './b'; export default function(){ return [a,b]; }",
|
||||
{ "./a": "a", "./b": "b" },
|
||||
),
|
||||
esmModule(
|
||||
"a",
|
||||
"import { value } from './shared'; export const a=value;",
|
||||
{
|
||||
"./shared": "shared",
|
||||
},
|
||||
),
|
||||
esmModule(
|
||||
"b",
|
||||
"import { value } from './shared'; export const b=value;",
|
||||
{
|
||||
"./shared": "shared",
|
||||
},
|
||||
),
|
||||
esmModule(
|
||||
"shared",
|
||||
"globalThis.initializations=(globalThis.initializations??0)+1; export const value=globalThis.initializations;",
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "mixed CommonJS factory",
|
||||
value: 8,
|
||||
modules: [
|
||||
esmModule(
|
||||
"entry",
|
||||
"import value from './dep'; export default function(){ return value.answer; }",
|
||||
{ "./dep": "dep" },
|
||||
),
|
||||
{
|
||||
alias: "dep",
|
||||
source: "function(module) { module.exports={answer:8}; }",
|
||||
dependencies: {},
|
||||
format: "commonjs-factory" as const,
|
||||
},
|
||||
],
|
||||
},
|
||||
])("links $name before evaluation", async ({ modules, value }) => {
|
||||
const workerRequest = await request("parser.mjs", "esm", "default", []);
|
||||
workerRequest.left.modules = modules;
|
||||
const result = await runWorker(workerRequest);
|
||||
expect(result.code).toBe(0);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
left: [{ outcome: "return", value }],
|
||||
});
|
||||
});
|
||||
|
||||
it("still denies undeclared static imports", async () => {
|
||||
const workerRequest = await request("parser.mjs", "esm", "default", []);
|
||||
workerRequest.left.modules = [
|
||||
esmModule(
|
||||
"entry",
|
||||
"import './undeclared'; export default function(){ return 1; }",
|
||||
),
|
||||
];
|
||||
const result = await runWorker(workerRequest);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
left: [
|
||||
{
|
||||
outcome: "denied",
|
||||
exception: { message: "Undeclared import: ./undeclared" },
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("deterministic replay Date compatibility", () => {
|
||||
it.each([
|
||||
["new Date(2020, 0, 2).getFullYear()", 2020],
|
||||
|
||||
Reference in New Issue
Block a user