feat(javascript): recover code-split bundles from multiple inputs (#1686)

* feat(javascript): recover code-split bundles from multiple inputs

recover_javascript_sources accepted a single path, and
validateModuleSet bound every module's provenance to that one snapshot.
A code-split app (runtime + chunks, or an Electron main/preload/renderer
set) had to be recovered one file per call, and modules recovered alone
could not resolve imports across chunks: cross-chunk require(id)
references stayed as numeric ids with property reads.

Add an optional additional_paths input. The listed files are
concatenated, in order after path, into the single engine snapshot, so
one Wakaru run sees every module and cross-chunk imports resolve.
validateModuleSet's single-snapshot binding is unchanged. Module
provenance is attributed back to the input file its first extraction
range starts in, with byte ranges rebased to that file's own offsets;
a module whose ranges cross an input boundary keeps the concatenated
snapshot identity and offsets, recorded as an explicit limitation.
Single-path behavior is byte-for-byte unchanged (one segment,
offset 0).

Fixes #1605

* fix(javascript): preserve source identity in multi-input recovery

---------

Co-authored-by: JingHao-Leon <w15909130000@gmail.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
JingHao-Leon
2026-10-11 18:39:49 +08:00
committed by GitHub
co-authored by JingHao-Leon morluto
parent ce845ba428
commit 12c416005f
23 changed files with 695 additions and 121 deletions
+65
View File
@@ -14,6 +14,71 @@ if (args.includes("--version")) {
} else {
const input = args[0];
const output = args[args.indexOf("-o") + 1];
const inputs = args.slice(
0,
args.findIndex((arg) => arg.startsWith("--")),
);
if (inputs.length > 1) {
const modules = [];
const provenance = {
format: "unknown",
strategy: "structural",
modules: {},
};
for (const [index, selected] of inputs.entries()) {
const bytes = await readFile(selected);
const filename = `module-${index}.js`;
modules.push({ filename, kind: "javascript", status: "decompiled" });
await writeFile(join(output, filename), bytes);
provenance.modules[filename] = {
input:
index === 1 && mode === "secondary-foreign"
? "/unselected.js"
: selected,
ranges: [
[
0,
index === 1 && mode === "secondary-overflow"
? bytes.length + 1
: bytes.length,
],
],
context_ranges: [
[0, index === 1 && mode === "secondary-utf8" ? 2 : bytes.length],
],
extraction: "structural",
};
}
if (mode === "redistribute") {
const primary = process.env.REA_RECOVERY_ORIGINAL_PATH;
const secondary = process.env.REA_RECOVERY_ADDITIONAL_PATH;
const first = await readFile(primary);
const second = await readFile(secondary);
await writeFile(primary, Buffer.concat([first, second.subarray(0, 1)]));
await writeFile(secondary, second.subarray(1));
}
if (mode === "secondary-change")
await writeFile(
process.env.REA_RECOVERY_ADDITIONAL_PATH,
"changed additional input",
);
await writeFile(
join(output, "provenance.json"),
JSON.stringify(provenance),
);
console.log(
JSON.stringify({
detected_formats: [],
safety: "normal",
modules,
warnings: [],
total: modules.length,
failed: 0,
elapsed_ms: 1,
}),
);
process.exit(0);
}
const bytes = await readFile(input);
await writeFile(join(dirname(output), "recovery-started"), "started");
if (mode === "hang") await new Promise(() => setInterval(() => {}, 1000));
+1
View File
@@ -38,6 +38,7 @@ export const recoveryFixture = async (
...options.env,
REA_RECOVERY_FIXTURE_MODE: mode,
REA_RECOVERY_ORIGINAL_PATH: path,
REA_RECOVERY_ADDITIONAL_PATH: join(root, "chunk.js"),
REA_RECOVERY_ENGINE_PATH: command,
},
});