MCPcopy Create free account
hub / github.com/btd/rollup-plugin-visualizer / deriveTree

Function deriveTree

shared/share/reconstruct.ts:14–121  ·  view source on GitHub ↗
(
  data: VisualizerData,
  recipe: TreeRecipe,
  limits: ShareLimits,
)

Source from the content-addressed store, hash-verified

12}
13
14export const deriveTree = (
15 data: VisualizerData,
16 recipe: TreeRecipe,
17 limits: ShareLimits,
18): ModuleTree => {
19 const partRows = Object.entries(data.nodeParts);
20 const encoder = new TextEncoder();
21 const paths = new Map<string, string[]>(),
22 textLengths = new Map<string, number>();
23 let textBytes = 0;
24 const textLength = (value: string): number => {
25 let length = textLengths.get(value);
26 if (length === undefined) {
27 budget(value.length, limits.maxTextBytes, "Derived tree text");
28 length = encoder.encode(value).length;
29 textLengths.set(value, length);
30 }
31 return length;
32 };
33 const accountText = (bytes: number): void => {
34 textBytes += bytes;
35 budget(textBytes, limits.maxTextBytes, "Derived tree text");
36 };
37 accountText(textLength(recipe.rootName));
38 const children: Array<ModuleTree | ModuleTreeLeaf> = [];
39 const groups = new Map<string, ModuleTree[]>();
40 const folders = new WeakMap<ModuleTree, Map<string, ModuleTree>>();
41 let leaf = 0,
42 nodes = 1 + recipe.names.length;
43 budget(nodes, limits.maxRecords, "Derived tree nodes");
44 recipe.names.forEach((name, i) => {
45 accountText(textLength(name));
46 if (recipe.kinds[i] === 1)
47 children.push({ name, uid: reference(partRows, recipe.parts[leaf++])[0] });
48 else if (recipe.kinds[i] === 0) {
49 const tree: ModuleTree = { name, children: [] };
50 children.push(tree);
51 const sameName = groups.get(name) ?? [];
52 sameName.push(tree);
53 groups.set(name, sameName);
54 } else invalid("Unknown derived bundle kind.");
55 });
56 if (leaf !== recipe.parts.length) invalid("Inconsistent direct-leaf count.");
57 const memberships = new Map<string, string[]>();
58 for (const meta of Object.values(data.nodeMetas))
59 for (const [bundle, uid] of Object.entries(meta.moduleParts)) {
60 const list = memberships.get(uid) ?? [];
61 list.push(bundle);
62 memberships.set(uid, list);
63 }
64 for (const [uid, part] of partRows) {
65 const id = data.nodeMetas[part.metaUid].id;
66 for (const bundle of memberships.get(uid) ?? []) {
67 const bundleTrees = groups.get(bundle);
68 if (!bundleTrees) continue;
69 let path = paths.get(id);
70 if (!path) {
71 path = [];

Callers 3

decodeReportFunction · 0.85
treeRecipeFunction · 0.85

Calls 7

accountTextFunction · 0.85
textLengthFunction · 0.85
budgetFunction · 0.85
referenceFunction · 0.85
invalidFunction · 0.85
mergeFunction · 0.85
putBoolFunction · 0.85

Tested by

no test coverage detected