Repository navigation
Expand file tree
/
Copy pathjavascriptRuntimeLoadState.ts
More file actions
164 lines (157 loc) · 5.37 KB
/
Copy pathjavascriptRuntimeLoadState.ts
File metadata and controls
164 lines (157 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
import { uniqueSorted } from "../canonicalOrdering.js";
import type { ParsedStaticLayer } from "./javascriptRuntimeReconciliationParsing.js";
import type {
JavaScriptRuntimeReconciliationItem,
JavaScriptStaticLoadState,
} from "./javascriptRuntimeReconciliationSchemas.js";
import type { RuntimeReconciliationEntity } from "./javascriptRuntimeReconciliationRuntime.js";
import {
createStaticRuntimeScope,
mappedRuntimePaths,
staticNodeWithinRuntimeScope,
} from "./javascriptRuntimeStaticCandidates.js";
export interface StaticLoadStateProjection {
readonly states: readonly JavaScriptStaticLoadState[];
readonly omittedStates: number;
}
/** Classify static code presence without claiming bundled module execution. */
export const classifyStaticLoadStates = (
layers: readonly ParsedStaticLayer[],
runtimeEntities: readonly RuntimeReconciliationEntity[],
matches: readonly JavaScriptRuntimeReconciliationItem[],
options: {
readonly reconciliationComplete: boolean;
},
): StaticLoadStateProjection => {
const states: JavaScriptStaticLoadState[] = [];
for (const layer of layers) {
const projection = classifyLayer(layer, runtimeEntities, matches, {
reconciliationComplete: options.reconciliationComplete,
});
states.push(...projection.states);
}
return { states, omittedStates: 0 };
};
const classifyLayer = (
layer: ParsedStaticLayer,
runtimeEntities: readonly RuntimeReconciliationEntity[],
matches: readonly JavaScriptRuntimeReconciliationItem[],
options: {
readonly reconciliationComplete: boolean;
},
): { readonly states: JavaScriptStaticLoadState[] } => {
const nodes = layer.graph.nodes.filter(isLoadStateNode);
const direct = new Map<string, string[]>();
for (const match of matches)
if (
match.status === "matched" &&
match.static_layer_id === layer.layerId &&
match.static_node_id !== null
)
append(direct, match.static_node_id, match.runtime_node_id);
const resident = residentNodes(layer, new Set(direct.keys()));
const runtimeScope = createStaticRuntimeScope(layer, runtimeEntities);
const scopedEntities = runtimeEntities.filter(
(entity) => mappedRuntimePaths(layer, entity).length > 0,
);
const layerComplete =
options.reconciliationComplete &&
layer.graph.coverage.status === "complete" &&
scopedEntities.length > 0 &&
scopedEntities.every(({ capture }) => capture.scriptsCompleteWithinScope);
const states = nodes.map((node) => {
const runtimeNodeIds = uniqueSorted(direct.get(node.node_id) ?? []);
if (runtimeNodeIds.length > 0)
return state(layer, node, {
status: "loaded",
runtimeNodeIds,
reason: "runtime-script-correspondence",
});
if (resident.has(node.node_id))
return state(layer, node, {
status: "resident-in-loaded-asset",
runtimeNodeIds: [],
reason: "containing-asset-was-loaded",
});
const nodeWithinScope = staticNodeWithinRuntimeScope(node, runtimeScope);
if (layerComplete && nodeWithinScope)
return state(layer, node, {
status: "not-observed-in-capture",
runtimeNodeIds: [],
reason: "not-observed-in-bounded-capture",
});
return state(layer, node, {
status: "unknown",
runtimeNodeIds: [],
reason: !nodeWithinScope
? "layer-outside-runtime-scope"
: "static-or-runtime-coverage-incomplete",
});
});
return { states };
};
const residentNodes = (
layer: ParsedStaticLayer,
directlyLoaded: ReadonlySet<string>,
): ReadonlySet<string> => {
const nodeKinds = new Map(
layer.graph.nodes.map(({ node_id: id, kind }) => [id, kind]),
);
const children = new Map<string, string[]>();
for (const edge of layer.graph.edges)
if (edge.relation === "contains")
append(children, edge.source_node_id, edge.target_node_id);
const pending = [...directlyLoaded].filter(
(nodeId) => nodeKinds.get(nodeId) === "javascript-asset",
);
const resident = new Set<string>();
for (let index = 0; index < pending.length; index += 1) {
const current = pending[index];
if (current === undefined) break;
for (const child of children.get(current) ?? []) {
if (resident.has(child)) continue;
resident.add(child);
pending.push(child);
}
}
return resident;
};
const append = (
values: Map<string, string[]>,
key: string,
value: string,
): void => {
const current = values.get(key);
if (current === undefined) values.set(key, [value]);
else current.push(value);
};
const isLoadStateNode = (
node: ApplicationNode,
): node is ApplicationNode & {
readonly kind: "javascript-asset" | "javascript-chunk" | "javascript-module";
} =>
node.kind === "javascript-asset" ||
node.kind === "javascript-chunk" ||
node.kind === "javascript-module";
const state = (
layer: ParsedStaticLayer,
node: ApplicationNode & {
readonly kind:
| "javascript-asset"
| "javascript-chunk"
| "javascript-module";
},
input: {
readonly status: JavaScriptStaticLoadState["status"];
readonly runtimeNodeIds: readonly string[];
readonly reason: JavaScriptStaticLoadState["reason"];
},
): JavaScriptStaticLoadState => ({
static_layer_id: layer.layerId,
static_node_id: node.node_id,
kind: node.kind,
status: input.status,
runtime_node_ids: [...input.runtimeNodeIds],
reason: input.reason,
});