Repository navigation
Expand file tree
/
Copy pathjavascriptFeatureSeed.ts
More file actions
256 lines (245 loc) · 7.48 KB
/
Copy pathjavascriptFeatureSeed.ts
File metadata and controls
256 lines (245 loc) · 7.48 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
import {
featureSeedMatchMode,
type ApplicationFeatureSeed,
} from "./javascriptFeatureTraceSchemas.js";
import type { JsonValue } from "../jsonValue.js";
/** One explicit field that matched a feature seed. */
export interface ApplicationFeatureSeedMatch {
readonly node_id: string;
readonly kind: ApplicationNode["kind"];
readonly basis: "node-id" | "label" | "identity" | "property";
readonly field: string;
}
interface CandidateField {
readonly basis: ApplicationFeatureSeedMatch["basis"];
readonly field: string;
/** Structural field path, retaining literal keys but omitting array indexes. */
readonly selectionPath?: string;
readonly value: string;
}
/** Locate every graph node matching a typed literal seed without regex execution. */
export const findApplicationFeatureSeeds = (
nodes: readonly ApplicationNode[],
seed: ApplicationFeatureSeed,
): ApplicationFeatureSeedMatch[] =>
nodes
.flatMap((node) => seedMatch(node, seed))
.sort((left, right) =>
compareUnicodeCodePoints(
`${left.node_id}\0${left.basis}\0${left.field}`,
`${right.node_id}\0${right.basis}\0${right.field}`,
),
);
const seedMatch = (
node: ApplicationNode,
seed: ApplicationFeatureSeed,
): ApplicationFeatureSeedMatch[] => {
if (seed.kind === "node-id")
return node.node_id === seed.value
? [
{
node_id: node.node_id,
kind: node.kind,
basis: "node-id",
field: "node_id",
},
]
: [];
if (!kindMatches(node, seed.kind)) return [];
const mode = featureSeedMatchMode(seed);
const query = normalized(seed.value, seed.case_sensitive);
const match = candidateFields(node, seed.kind).find((candidate) => {
const value = normalized(candidate.value, seed.case_sensitive);
return mode === "exact" ? value === query : value.includes(query);
});
return match === undefined
? []
: [
{
node_id: node.node_id,
kind: node.kind,
basis: match.basis,
field: match.field,
},
];
};
const kindMatches = (
node: ApplicationNode,
kind: Exclude<ApplicationFeatureSeed["kind"], "node-id">,
): boolean => {
if (kind === "string") return true;
if (kind === "route")
return (
node.kind === "endpoint" &&
node.observations.some(
({ properties }) => properties.endpoint_kind === "route",
)
);
if (kind === "api")
return (
node.kind === "context-bridge-api" ||
(node.kind === "javascript-expression" &&
node.observations.some((o) => o.properties.step_kind === "exposure"))
);
if (kind === "channel") return node.kind === "ipc-channel";
if (kind === "module")
return ["javascript-asset", "javascript-module", "source-module"].includes(
node.kind,
);
return node.kind === "native-export";
};
const candidateFields = (
node: ApplicationNode,
seedKind: Exclude<ApplicationFeatureSeed["kind"], "node-id">,
): CandidateField[] => {
const fields: CandidateField[] = [
...identityFields(node),
...node.observations.flatMap((observation, index) => [
...(observation.label === null
? []
: [
{
basis: "label" as const,
field: `observations[${String(index)}].label`,
value: observation.label,
},
]),
...(observation.source_map_reference !== null &&
observation.source_map_reference.resolution.kind !== "unresolved"
? [
{
basis: "property" as const,
field: `observations[${String(index)}].source_map_reference.resolution.path`,
value: observation.source_map_reference.resolution.path,
},
]
: []),
...propertyFields(
observation.properties,
`observations[${String(index)}].properties`,
),
]),
];
if (seedKind === "string") return fields;
const names = relevantFields[seedKind];
return fields.filter(({ field, selectionPath = field }) =>
names.some((name) => {
const path = memberFieldSuffixes.has(name) ? selectionPath : field;
return path === "identity" || path.endsWith(name);
}),
);
};
const memberFieldSuffixes = new Set([
".members",
".methods",
".exports",
".requested_members",
]);
const relevantFields = {
route: [".label", ".value", ".path"],
api: [".label", ".api_name", ".api_key", ".key", ".methods", ".members"],
channel: [".label", ".channel", ".key"],
module: [
".label",
".module_key",
".path",
".source",
".original_source",
".source_root",
".exports",
],
"native-export": [".label", ".key", ".requested_members", ".members"],
} as const;
const identityFields = (node: ApplicationNode): CandidateField[] => {
const identity = node.identity;
if (identity.strategy === "content-digest")
return [{ basis: "identity", field: "identity", value: identity.sha256 }];
if (identity.strategy === "source-map-original")
return [
{
basis: "identity",
field: "identity.original_source",
value: identity.original_source,
},
...(identity.source_root === null
? []
: [
{
basis: "identity" as const,
field: "identity.source_root",
value: identity.source_root,
},
]),
];
if (identity.strategy === "canonical-path")
return [
{ basis: "identity", field: "identity.path", value: identity.path },
];
if (identity.strategy === "artifact-local-key")
return [{ basis: "identity", field: "identity.key", value: identity.key }];
if (identity.strategy === "runtime-instance")
return [
{
basis: "identity",
field: "identity.runtime_key",
value: identity.runtime_key,
},
];
return [];
};
const propertyFields = (
value: Readonly<Record<string, JsonValue>>,
prefix: string,
): CandidateField[] => {
const output: CandidateField[] = [];
const pending: {
readonly current: JsonValue;
readonly path: string;
readonly selectionPath: string;
}[] = [{ current: value, path: prefix, selectionPath: prefix }];
while (pending.length > 0) {
const next = pending.pop();
if (next === undefined) continue;
const { current, path, selectionPath } = next;
if (typeof current === "string") {
output.push({
basis: "property",
field: path,
selectionPath,
value: current,
});
continue;
}
if (Array.isArray(current)) {
for (let index = current.length - 1; index >= 0; index -= 1) {
const item = current[index];
if (item !== undefined)
pending.push({
current: item,
path: `${path}[${String(index)}]`,
selectionPath,
});
}
continue;
}
if (current !== null && typeof current === "object") {
for (const key of Object.keys(current)
.sort(compareUnicodeCodePoints)
.reverse())
pending.push({
current: current[key] ?? null,
path: `${path}.${key}`,
selectionPath: `${selectionPath}.${key}`,
});
}
}
return output;
};
const normalized = (value: string, caseSensitive: boolean): string => {
const normalizedValue = value.normalize("NFKC");
return caseSensitive
? normalizedValue
: normalizedValue.toLocaleLowerCase("en-US");
};