@beehexa/hexasync-template-index 2608.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/checkpoints.d.ts +47 -0
- package/dist/checkpoints.d.ts.map +1 -0
- package/dist/checkpoints.js +59 -0
- package/dist/checkpoints.js.map +1 -0
- package/dist/cursorContext.d.ts +204 -0
- package/dist/cursorContext.d.ts.map +1 -0
- package/dist/cursorContext.js +462 -0
- package/dist/cursorContext.js.map +1 -0
- package/dist/detectors.d.ts +103 -0
- package/dist/detectors.d.ts.map +1 -0
- package/dist/detectors.js +104 -0
- package/dist/detectors.js.map +1 -0
- package/dist/discovery.d.ts +193 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +394 -0
- package/dist/discovery.js.map +1 -0
- package/dist/effectiveGraph.d.ts +240 -0
- package/dist/effectiveGraph.d.ts.map +1 -0
- package/dist/effectiveGraph.js +523 -0
- package/dist/effectiveGraph.js.map +1 -0
- package/dist/failures.d.ts +50 -0
- package/dist/failures.d.ts.map +1 -0
- package/dist/failures.js +125 -0
- package/dist/failures.js.map +1 -0
- package/dist/graph.d.ts +47 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +230 -0
- package/dist/graph.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/queries.d.ts +102 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +463 -0
- package/dist/queries.js.map +1 -0
- package/dist/ranges.d.ts +37 -0
- package/dist/ranges.d.ts.map +1 -0
- package/dist/ranges.js +217 -0
- package/dist/ranges.js.map +1 -0
- package/dist/resolveField.d.ts +125 -0
- package/dist/resolveField.d.ts.map +1 -0
- package/dist/resolveField.js +234 -0
- package/dist/resolveField.js.map +1 -0
- package/dist/tier1Service.d.ts +96 -0
- package/dist/tier1Service.d.ts.map +1 -0
- package/dist/tier1Service.js +194 -0
- package/dist/tier1Service.js.map +1 -0
- package/dist/tier2Service.d.ts +195 -0
- package/dist/tier2Service.d.ts.map +1 -0
- package/dist/tier2Service.js +642 -0
- package/dist/tier2Service.js.map +1 -0
- package/dist/yamlScalar.d.ts +17 -0
- package/dist/yamlScalar.d.ts.map +1 -0
- package/dist/yamlScalar.js +101 -0
- package/dist/yamlScalar.js.map +1 -0
- package/package.json +31 -0
package/dist/ranges.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a component physically sits in its file (Story 3.4 AC 2).
|
|
3
|
+
*
|
|
4
|
+
* Computed ON DEMAND rather than carried on every component. A range is only ever wanted for the
|
|
5
|
+
* handful of components a developer looks at — a hover, a go-to-definition, a rename preview —
|
|
6
|
+
* while building one for all 273 would mean a second full parse of every worklist file at build
|
|
7
|
+
* time, which is the exact cost Story 3.1 spent the epic removing.
|
|
8
|
+
*
|
|
9
|
+
* The text comes from the graph's own prefetched `sources`, so this reads nothing.
|
|
10
|
+
*
|
|
11
|
+
* Looks the collection up DIRECTLY rather than walking every node and inspecting its ancestors.
|
|
12
|
+
* The first version did the latter and silently returned nothing for every map-keyed collection,
|
|
13
|
+
* because it reached for `.range` on a `Pair` — which is not a Node and has none. That is the
|
|
14
|
+
* whole of `objectAssociations`, so go-to-definition had nowhere to go for precisely the
|
|
15
|
+
* components this epic had just taught the graph to see.
|
|
16
|
+
*/
|
|
17
|
+
import { parseDocument, isScalar, isSeq, isMap } from 'yaml';
|
|
18
|
+
import { normalizeComponentKey } from '@beehexa/hexasync-template-compose';
|
|
19
|
+
const positionAt = (text, offset) => {
|
|
20
|
+
let line = 0;
|
|
21
|
+
let lineStart = 0;
|
|
22
|
+
for (let i = 0; i < offset && i < text.length; i++) {
|
|
23
|
+
if (text[i] === '\n') {
|
|
24
|
+
line++;
|
|
25
|
+
lineStart = i + 1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return { line, column: offset - lineStart };
|
|
29
|
+
};
|
|
30
|
+
const rangeOf = (node) => {
|
|
31
|
+
const range = node?.range;
|
|
32
|
+
return range ? [range[0], range[1]] : undefined;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The range of one component within a file's text.
|
|
36
|
+
*
|
|
37
|
+
* Matched on the component's own identity — its `id`/`key` inside the named collection — rather
|
|
38
|
+
* than on a positional index. A file's components are not necessarily in the same order as the
|
|
39
|
+
* project's merged view of them, and an index taken from one and applied to the other is the
|
|
40
|
+
* defect this epic already had to fix once in the cursor path.
|
|
41
|
+
*
|
|
42
|
+
* When a file declares the same identity twice, the LAST one wins, because that is what the
|
|
43
|
+
* merge does — navigating to the entry that was overridden would point at text with no effect.
|
|
44
|
+
*/
|
|
45
|
+
export function findComponentRange(text, collection, id) {
|
|
46
|
+
let doc;
|
|
47
|
+
try {
|
|
48
|
+
doc = parseDocument(text, { keepSourceTokens: true });
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
// `parseDocument` COLLECTS errors rather than throwing, so the catch above is not the guard it
|
|
54
|
+
// looks like. A file mid-keystroke parses into something partial, and a range read off it can
|
|
55
|
+
// point anywhere; saying "no range" beats pointing a hover at the wrong line.
|
|
56
|
+
if (!doc || doc.errors.length > 0 || !isMap(doc.contents))
|
|
57
|
+
return undefined;
|
|
58
|
+
const node = doc.contents.get(collection, true);
|
|
59
|
+
if (!node)
|
|
60
|
+
return undefined;
|
|
61
|
+
let found;
|
|
62
|
+
// Array form: `collection: [ {id: X}, … ]`.
|
|
63
|
+
if (isSeq(node)) {
|
|
64
|
+
for (const item of node.items) {
|
|
65
|
+
if (!isMap(item))
|
|
66
|
+
continue;
|
|
67
|
+
const identity = normalizeComponentKey(item.get('id') ?? item.get('key'));
|
|
68
|
+
// Normalised the same way the graph keys components, so a quoted id is findable by its bare
|
|
69
|
+
// spelling and a `!`-removal directive is not mistaken for a declaration.
|
|
70
|
+
if (!identity || identity.removes || identity.id !== id)
|
|
71
|
+
continue;
|
|
72
|
+
const itemRange = rangeOf(item);
|
|
73
|
+
// Widened LEFT to include the `- ` list marker, which the item's own range starts after. A
|
|
74
|
+
// "select this component" action built on the unwidened range dropped the marker and produced
|
|
75
|
+
// a fragment that is not a list item.
|
|
76
|
+
found = itemRange
|
|
77
|
+
? [markerStart(text, itemRange[0]), itemRange[1]]
|
|
78
|
+
: found; // keep scanning: last wins
|
|
79
|
+
}
|
|
80
|
+
return found ? toRange(text, found) : undefined;
|
|
81
|
+
}
|
|
82
|
+
// Map form: `collection: { X: … }` — the component IS the pair's value, keyed by its id.
|
|
83
|
+
if (isMap(node)) {
|
|
84
|
+
for (const pair of node.items) {
|
|
85
|
+
if (!isScalar(pair.key) || String(pair.key.value) !== id)
|
|
86
|
+
continue;
|
|
87
|
+
// A `Pair` is not a Node and has no range of its own. The component spans from where its
|
|
88
|
+
// KEY starts to where its VALUE ends, which is what a reader would select.
|
|
89
|
+
const keyRange = rangeOf(pair.key);
|
|
90
|
+
const valueRange = rangeOf(pair.value);
|
|
91
|
+
if (!keyRange)
|
|
92
|
+
continue;
|
|
93
|
+
found = [keyRange[0], valueRange ? valueRange[1] : keyRange[1]];
|
|
94
|
+
}
|
|
95
|
+
return found ? toRange(text, found) : undefined;
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
/** The key `findComponentRanges` maps by — the same identity pair `getComponent` is asked for. */
|
|
100
|
+
export const componentRangeKey = (collection, id) => `${collection}\u0000${id}`;
|
|
101
|
+
/**
|
|
102
|
+
* EVERY component's range in a file, from ONE parse (Phase 3 Story 3.2 AC 3).
|
|
103
|
+
*
|
|
104
|
+
* `findComponentRange` above parses the whole document to answer for one component, which is right for
|
|
105
|
+
* the one-off lookups it was written for — a hover, a go-to-definition. It is quadratic for a caller
|
|
106
|
+
* that wants them all: `getComponent` cached per component, so an outline of a 40-component file paid
|
|
107
|
+
* 40 full parses of that file.
|
|
108
|
+
*
|
|
109
|
+
* Measured, which is why this exists. Story 3.2's outline came in at **p95 313 ms, worst project
|
|
110
|
+
* 1,161 ms** against a 200 ms budget, and the workspace query at 561 ms. The epic's own contingency note
|
|
111
|
+
* says a missed budget is answered with a different strategy and never a relaxed number, because
|
|
112
|
+
* counter-metric C1 makes responsiveness non-negotiable. This is that strategy: parse once, look up N
|
|
113
|
+
* times.
|
|
114
|
+
*
|
|
115
|
+
* Deliberately shares `collectRanges` with the single-component function rather than reimplementing the
|
|
116
|
+
* item walk. The list-marker widening and the `trimEnd` correction below were each found by a failing
|
|
117
|
+
* test, and a second copy of that logic would not have inherited either.
|
|
118
|
+
*/
|
|
119
|
+
export function findComponentRanges(text) {
|
|
120
|
+
const found = new Map();
|
|
121
|
+
let doc;
|
|
122
|
+
try {
|
|
123
|
+
doc = parseDocument(text, { keepSourceTokens: true });
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return found;
|
|
127
|
+
}
|
|
128
|
+
// Same guard as the single lookup, and for the same reason: a partial parse yields ranges that can
|
|
129
|
+
// point anywhere, and an outline of confidently wrong ranges is worse than no outline.
|
|
130
|
+
if (!doc || doc.errors.length > 0 || !isMap(doc.contents))
|
|
131
|
+
return found;
|
|
132
|
+
for (const pair of doc.contents.items) {
|
|
133
|
+
if (!isScalar(pair.key))
|
|
134
|
+
continue;
|
|
135
|
+
const collection = String(pair.key.value);
|
|
136
|
+
const node = pair.value;
|
|
137
|
+
if (!node)
|
|
138
|
+
continue;
|
|
139
|
+
for (const [id, offsets] of collectRanges(text, node)) {
|
|
140
|
+
found.set(componentRangeKey(collection, id), toRange(text, offsets));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return found;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Each component of one collection node, by id, as raw offsets.
|
|
147
|
+
*
|
|
148
|
+
* The shared half of both range functions. Two shapes occur — a sequence of `{id}` maps, and a map
|
|
149
|
+
* keyed by id — and the LAST declaration of an identity wins, because that is what the merge does:
|
|
150
|
+
* navigating to an entry that was overridden would point at text with no effect.
|
|
151
|
+
*/
|
|
152
|
+
function collectRanges(text, node) {
|
|
153
|
+
const found = new Map();
|
|
154
|
+
if (isSeq(node)) {
|
|
155
|
+
for (const item of node.items) {
|
|
156
|
+
if (!isMap(item))
|
|
157
|
+
continue;
|
|
158
|
+
const identity = normalizeComponentKey(item.get('id') ?? item.get('key'));
|
|
159
|
+
if (!identity || identity.removes)
|
|
160
|
+
continue;
|
|
161
|
+
const itemRange = rangeOf(item);
|
|
162
|
+
// Widened LEFT to include the `- ` list marker, which the item's own range starts after.
|
|
163
|
+
if (itemRange)
|
|
164
|
+
found.set(identity.id, [markerStart(text, itemRange[0]), itemRange[1]]);
|
|
165
|
+
}
|
|
166
|
+
return found;
|
|
167
|
+
}
|
|
168
|
+
if (isMap(node)) {
|
|
169
|
+
for (const pair of node.items) {
|
|
170
|
+
if (!isScalar(pair.key))
|
|
171
|
+
continue;
|
|
172
|
+
// A `Pair` is not a Node and has no range of its own. The component spans from where its KEY
|
|
173
|
+
// starts to where its VALUE ends, which is what a reader would select.
|
|
174
|
+
const keyRange = rangeOf(pair.key);
|
|
175
|
+
if (!keyRange)
|
|
176
|
+
continue;
|
|
177
|
+
const valueRange = rangeOf(pair.value);
|
|
178
|
+
found.set(String(pair.key.value), [
|
|
179
|
+
keyRange[0],
|
|
180
|
+
valueRange ? valueRange[1] : keyRange[1],
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return found;
|
|
185
|
+
}
|
|
186
|
+
const toRange = (text, [start, end]) => ({
|
|
187
|
+
start: positionAt(text, start),
|
|
188
|
+
end: positionAt(text, trimEnd(text, start, end)),
|
|
189
|
+
});
|
|
190
|
+
/**
|
|
191
|
+
* Pull an end offset back off trailing whitespace.
|
|
192
|
+
*
|
|
193
|
+
* `node.range[1]` for a block-collection member sits AFTER the newline that ends it, so on
|
|
194
|
+
* `objects:\n - id: ONE\n name: one\n - id: TWO\n` the range for ONE ended at line 3 column 0
|
|
195
|
+
* — which is TWO's own line. Any consumer asking "which component contains this position" got the
|
|
196
|
+
* wrong answer at every boundary, and two adjacent components' ranges touched.
|
|
197
|
+
*/
|
|
198
|
+
function trimEnd(text, start, end) {
|
|
199
|
+
let at = Math.min(end, text.length);
|
|
200
|
+
while (at > start && /\s/.test(text[at - 1]))
|
|
201
|
+
at--;
|
|
202
|
+
return at;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The offset of the `-` that opens this list item, or the item's own start.
|
|
206
|
+
*
|
|
207
|
+
* A sequence item's range begins at its first key, so the `- ` marker sits outside it. Walks back
|
|
208
|
+
* over spaces only — if anything else is there, this is not a block sequence item and the original
|
|
209
|
+
* offset is right.
|
|
210
|
+
*/
|
|
211
|
+
function markerStart(text, itemStart) {
|
|
212
|
+
let at = itemStart - 1;
|
|
213
|
+
while (at >= 0 && (text[at] === ' ' || text[at] === '\t'))
|
|
214
|
+
at--;
|
|
215
|
+
return at >= 0 && text[at] === '-' ? at : itemStart;
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=ranges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ranges.js","sourceRoot":"","sources":["../src/ranges.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAa,MAAM,MAAM,CAAC;AACxE,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAM3E,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,MAAc,EAAE,EAAE;IAClD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACnD,IAAI,I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AAI,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;QAAE,EAAE,EAAE,CAAC;IAChE,OAAO,EAAE,IAAI,CAAC,IAAI,IAAI,CAAC,EAAE,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point-wise value resolution — one field, on demand (FR-45, Story 3.3).
|
|
3
|
+
*
|
|
4
|
+
* Explaining a single field must not cost a whole-project substitution pass. A hover over one
|
|
5
|
+
* `url:` should not resolve 273 components, and a project that takes 650 ms to compose cannot
|
|
6
|
+
* afford to do it per cursor movement.
|
|
7
|
+
*
|
|
8
|
+
* It REUSES the composer's `resolveVariablePool` / `substituteVariables` rather than
|
|
9
|
+
* reimplementing them. Two answers to "what does this variable resolve to" is one answer too
|
|
10
|
+
* many: the editor explaining a field and the composer producing output have to agree, and the
|
|
11
|
+
* only way to guarantee that is one implementation. In particular the longest-key-first rule —
|
|
12
|
+
* without which `**Id**` corrupts `**ProductId**` — is the composer's, not a copy of it.
|
|
13
|
+
*/
|
|
14
|
+
import { type TemplateOccurrence } from '@beehexa/hexasync-template-model';
|
|
15
|
+
import { resolveVariablePool } from '@beehexa/hexasync-template-compose';
|
|
16
|
+
export interface ResolvedField {
|
|
17
|
+
/** The value with every resolvable compose-time token substituted. */
|
|
18
|
+
readonly value: string;
|
|
19
|
+
/** True when nothing needed substituting. */
|
|
20
|
+
readonly literal: boolean;
|
|
21
|
+
/** Compose-time tokens that contributed, with what each resolved to. */
|
|
22
|
+
readonly contributed: readonly {
|
|
23
|
+
key: string;
|
|
24
|
+
value: string;
|
|
25
|
+
}[];
|
|
26
|
+
/**
|
|
27
|
+
* Compose-time tokens with no definition in the pool.
|
|
28
|
+
*
|
|
29
|
+
* Reported, never silently left in the string: returning the raw text as though it were the
|
|
30
|
+
* value is how a `**Missing**` reaches production looking like a deliberate literal.
|
|
31
|
+
*/
|
|
32
|
+
readonly unresolved: readonly string[];
|
|
33
|
+
/**
|
|
34
|
+
* Runtime occurrences, untouched.
|
|
35
|
+
*
|
|
36
|
+
* Carried so a caller can SHOW them without ever trying to resolve them — the composer never
|
|
37
|
+
* owned `{{ … }}`, and reporting it as unresolved would be reporting on the wrong layer.
|
|
38
|
+
*/
|
|
39
|
+
readonly runtime: readonly TemplateOccurrence[];
|
|
40
|
+
}
|
|
41
|
+
/** A pool prepared once and reused across fields. */
|
|
42
|
+
export type PreparedPool = ReturnType<typeof resolveVariablePool>;
|
|
43
|
+
export declare function preparePool(variables: Record<string, unknown>): PreparedPool;
|
|
44
|
+
/**
|
|
45
|
+
* Resolve one field's value against a project's pool.
|
|
46
|
+
*
|
|
47
|
+
* `pool` is prepared by the caller so a burst of fields shares one preparation — the transitive
|
|
48
|
+
* pass over the pool is O(keys²) and doing it per field would dominate everything else.
|
|
49
|
+
*/
|
|
50
|
+
export declare function resolveFieldValue(raw: unknown, pool: PreparedPool, variables: Record<string, unknown>): ResolvedField;
|
|
51
|
+
/**
|
|
52
|
+
* A resolver bound to one project's pool, with a cache.
|
|
53
|
+
*
|
|
54
|
+
* Keyed by PROJECT and pool GENERATION, not by identity: the same field asked twice while
|
|
55
|
+
* nothing changed must not repeat the work, and the cache must vanish the moment
|
|
56
|
+
* `variables.yaml` moves — which is what Story 3.6's invalidation drops.
|
|
57
|
+
*/
|
|
58
|
+
export declare class FieldResolver {
|
|
59
|
+
#private;
|
|
60
|
+
private readonly variables;
|
|
61
|
+
/** Bumped by the owner whenever the pool changes; part of every cache key. */
|
|
62
|
+
readonly generation: number;
|
|
63
|
+
/**
|
|
64
|
+
* The project this resolver belongs to, also part of the key (Story 3.3 AC 3).
|
|
65
|
+
*
|
|
66
|
+
* Two projects genuinely can share a generation number — nothing in the type system stops a
|
|
67
|
+
* caller numbering its own — and `**BaseUrl**` resolves to a different host in each. That the
|
|
68
|
+
* current owner happens to hand out a service-global counter makes the collision unreachable
|
|
69
|
+
* today by accident, not by design, and an accident is not a cache key.
|
|
70
|
+
*/
|
|
71
|
+
readonly projectId: string;
|
|
72
|
+
/**
|
|
73
|
+
* Upper bound on the SHARED store, as a backstop against unbounded growth.
|
|
74
|
+
*
|
|
75
|
+
* Generous, because evicting a live project's entries costs a recompute rather than
|
|
76
|
+
* correctness, and the entries are small. The precise rule is `#evictSuperseded`'s
|
|
77
|
+
* generation check; this only catches what that cannot see.
|
|
78
|
+
*/
|
|
79
|
+
readonly maxEntries: number;
|
|
80
|
+
constructor(variables: Record<string, unknown>,
|
|
81
|
+
/** Bumped by the owner whenever the pool changes; part of every cache key. */
|
|
82
|
+
generation: number,
|
|
83
|
+
/**
|
|
84
|
+
* The project this resolver belongs to, also part of the key (Story 3.3 AC 3).
|
|
85
|
+
*
|
|
86
|
+
* Two projects genuinely can share a generation number — nothing in the type system stops a
|
|
87
|
+
* caller numbering its own — and `**BaseUrl**` resolves to a different host in each. That the
|
|
88
|
+
* current owner happens to hand out a service-global counter makes the collision unreachable
|
|
89
|
+
* today by accident, not by design, and an accident is not a cache key.
|
|
90
|
+
*/
|
|
91
|
+
projectId?: string,
|
|
92
|
+
/**
|
|
93
|
+
* A cache store, optionally SHARED with other resolvers.
|
|
94
|
+
*
|
|
95
|
+
* The default is private to this resolver, and with a private store the composite key below
|
|
96
|
+
* is decorative — the instance already implies its project and generation. That was the state
|
|
97
|
+
* this class shipped in, and a test asserting the key "worked" passed with the key reduced to
|
|
98
|
+
* the raw value, which is no assertion at all.
|
|
99
|
+
*
|
|
100
|
+
* A shared store is what makes the key load-bearing, and is what Story 3.3 AC 3 describes
|
|
101
|
+
* when it asks for a cache "keyed by project and pool generation": one store, many projects,
|
|
102
|
+
* entries that must not be served across them.
|
|
103
|
+
*/
|
|
104
|
+
store?: Map<string, ResolvedField>,
|
|
105
|
+
/**
|
|
106
|
+
* Upper bound on the SHARED store, as a backstop against unbounded growth.
|
|
107
|
+
*
|
|
108
|
+
* Generous, because evicting a live project's entries costs a recompute rather than
|
|
109
|
+
* correctness, and the entries are small. The precise rule is `#evictSuperseded`'s
|
|
110
|
+
* generation check; this only catches what that cannot see.
|
|
111
|
+
*/
|
|
112
|
+
maxEntries?: number);
|
|
113
|
+
resolve(raw: unknown): ResolvedField;
|
|
114
|
+
/**
|
|
115
|
+
* Forget everything this resolver cached.
|
|
116
|
+
*
|
|
117
|
+
* The invalidation hook Story 3.6 AC 2's second clause describes and nothing provided. Scoped to
|
|
118
|
+
* this project, so a shared store goes on serving the others.
|
|
119
|
+
*/
|
|
120
|
+
invalidate(): void;
|
|
121
|
+
/** How many distinct values are cached — exposed so caching is testable. Counts the whole
|
|
122
|
+
* store, which may be shared with other projects' resolvers. */
|
|
123
|
+
get cacheSize(): number;
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=resolveField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveField.d.ts","sourceRoot":"","sources":["../src/resolveField.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,mBAAmB,EAEpB,MAAM,oCAAoC,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,sEAAsE;IACtE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,6CAA6C;IAC7C,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,wEAAwE;IACxE,QAAQ,CAAC,WAAW,EAAE,SAAS;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAChE;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS,kBAAkB,EAAE,CAAC;CACjD;AAED,qDAAqD;AACrD,MAAM,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAElE,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,YAAY,CAE5E;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjC,aAAa,CA+Ff;AAED;;;;;;GAMG;AACH,qBAAa,aAAa;;IAKtB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,8EAA8E;IAC9E,QAAQ,CAAC,UAAU,EAAE,MAAM;IAC3B;;;;;;;OAOG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM;IAc1B;;;;;;OAMG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM;gBAhCV,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACnD,8EAA8E;IACrE,UAAU,EAAE,MAAM;IAC3B;;;;;;;OAOG;IACM,SAAS,GAAE,MAAW;IAC/B;;;;;;;;;;;OAWG;IACH,KAAK,GAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAa;IAC7C;;;;;;OAMG;IACM,UAAU,GAAE,MAAc;IAMrC,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,aAAa;IA0CpC;;;;;OAKG;IACH,UAAU,IAAI,IAAI;IAOlB;qEACiE;IACjE,IAAI,SAAS,IAAI,MAAM,CAEtB;CACF"}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point-wise value resolution — one field, on demand (FR-45, Story 3.3).
|
|
3
|
+
*
|
|
4
|
+
* Explaining a single field must not cost a whole-project substitution pass. A hover over one
|
|
5
|
+
* `url:` should not resolve 273 components, and a project that takes 650 ms to compose cannot
|
|
6
|
+
* afford to do it per cursor movement.
|
|
7
|
+
*
|
|
8
|
+
* It REUSES the composer's `resolveVariablePool` / `substituteVariables` rather than
|
|
9
|
+
* reimplementing them. Two answers to "what does this variable resolve to" is one answer too
|
|
10
|
+
* many: the editor explaining a field and the composer producing output have to agree, and the
|
|
11
|
+
* only way to guarantee that is one implementation. In particular the longest-key-first rule —
|
|
12
|
+
* without which `**Id**` corrupts `**ProductId**` — is the composer's, not a copy of it.
|
|
13
|
+
*/
|
|
14
|
+
import { classifyTemplating, } from '@beehexa/hexasync-template-model';
|
|
15
|
+
import { resolveVariablePool, substituteVariables, } from '@beehexa/hexasync-template-compose';
|
|
16
|
+
export function preparePool(variables) {
|
|
17
|
+
return resolveVariablePool(variables);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Resolve one field's value against a project's pool.
|
|
21
|
+
*
|
|
22
|
+
* `pool` is prepared by the caller so a burst of fields shares one preparation — the transitive
|
|
23
|
+
* pass over the pool is O(keys²) and doing it per field would dominate everything else.
|
|
24
|
+
*/
|
|
25
|
+
export function resolveFieldValue(raw, pool, variables) {
|
|
26
|
+
if (typeof raw !== 'string') {
|
|
27
|
+
return {
|
|
28
|
+
value: String(raw ?? ''),
|
|
29
|
+
literal: true,
|
|
30
|
+
contributed: [],
|
|
31
|
+
unresolved: [],
|
|
32
|
+
runtime: [],
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const occurrences = classifyTemplating(raw);
|
|
36
|
+
const composeTime = occurrences.filter((o) => o.layer === 'compose-time');
|
|
37
|
+
const runtime = occurrences.filter((o) => o.layer === 'runtime');
|
|
38
|
+
if (composeTime.length === 0) {
|
|
39
|
+
// `classifyTemplating` only recognises IDENTIFIER-shaped `**Key**`, while `substituteVariables`
|
|
40
|
+
// replaces any pool key verbatim. A pool key with a dot, a hyphen or nothing at all between the
|
|
41
|
+
// markers therefore substitutes in the composer while the explainer called the field literal and
|
|
42
|
+
// handed back the unsubstituted text — two answers to one question, which this file's own
|
|
43
|
+
// premise says is one too many. Falling through to substitution keeps them the same answer.
|
|
44
|
+
const touched = pool.keys.some((key) => key !== '' && raw.includes(key));
|
|
45
|
+
if (!touched) {
|
|
46
|
+
return {
|
|
47
|
+
value: raw,
|
|
48
|
+
literal: runtime.length === 0,
|
|
49
|
+
contributed: [],
|
|
50
|
+
unresolved: [],
|
|
51
|
+
runtime,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
value: substituteVariables(raw, pool),
|
|
56
|
+
literal: false,
|
|
57
|
+
contributed: pool.keys
|
|
58
|
+
.filter((key) => key !== '' && raw.includes(key))
|
|
59
|
+
.map((key) => ({ key, value: pool.resolved[key] ?? '' })),
|
|
60
|
+
unresolved: [],
|
|
61
|
+
runtime,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const contributed = [];
|
|
65
|
+
const unresolved = [];
|
|
66
|
+
const seen = new Set();
|
|
67
|
+
for (const occurrence of composeTime) {
|
|
68
|
+
if (seen.has(occurrence.raw))
|
|
69
|
+
continue;
|
|
70
|
+
seen.add(occurrence.raw);
|
|
71
|
+
// `raw` is the full `**Key**` form, which is how the pool is keyed.
|
|
72
|
+
if (Object.prototype.hasOwnProperty.call(variables, occurrence.raw)) {
|
|
73
|
+
// A key defined with NO value — `'**Key**':` with nothing after it — is not a definition that
|
|
74
|
+
// resolves to empty. Read the DECLARED value, not `pool.resolved`: the pool already coerces a
|
|
75
|
+
// null through `?.toString() ?? ''`, so by then the distinction is gone. Left as `''` the
|
|
76
|
+
// token was erased from the text and success reported, so a truncated variables line silently
|
|
77
|
+
// deleted characters from every field using it.
|
|
78
|
+
const declared = variables[occurrence.raw];
|
|
79
|
+
if (declared === undefined || declared === null) {
|
|
80
|
+
unresolved.push(occurrence.raw);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
contributed.push({
|
|
84
|
+
key: occurrence.raw,
|
|
85
|
+
value: pool.resolved[occurrence.raw] ?? '',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
unresolved.push(occurrence.raw);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// The composer's own substitution, over one string instead of a whole document.
|
|
93
|
+
const value = substituteVariables(raw, pool);
|
|
94
|
+
// Anything STILL a compose-time token after substitution is unresolved, whatever the pool said.
|
|
95
|
+
//
|
|
96
|
+
// The loop above only inspects tokens written in this field, so a token reached indirectly was
|
|
97
|
+
// invisible: with `{'**Host**': 'api.**Missing**.com'}`, the field `https://**Host**/x` returned
|
|
98
|
+
// `api.**Missing**.com` with `unresolved: []` — the file's own docstring calls that "how a
|
|
99
|
+
// `**Missing**` reaches production looking like a deliberate literal". The same check catches a
|
|
100
|
+
// cycle (`**A**`→`**B**`→`**A**` resolved to itself and reported as resolved) and a pool whose
|
|
101
|
+
// single non-fixpoint pass left a residual token behind, because all three end the same way: a
|
|
102
|
+
// token in the OUTPUT. Checking the result rather than the input is what makes it one rule.
|
|
103
|
+
for (const residual of classifyTemplating(value)) {
|
|
104
|
+
if (residual.layer !== 'compose-time')
|
|
105
|
+
continue;
|
|
106
|
+
if (seen.has(residual.raw) && unresolved.includes(residual.raw))
|
|
107
|
+
continue;
|
|
108
|
+
if (!unresolved.includes(residual.raw))
|
|
109
|
+
unresolved.push(residual.raw);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
value,
|
|
113
|
+
literal: false,
|
|
114
|
+
contributed,
|
|
115
|
+
unresolved,
|
|
116
|
+
runtime,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A resolver bound to one project's pool, with a cache.
|
|
121
|
+
*
|
|
122
|
+
* Keyed by PROJECT and pool GENERATION, not by identity: the same field asked twice while
|
|
123
|
+
* nothing changed must not repeat the work, and the cache must vanish the moment
|
|
124
|
+
* `variables.yaml` moves — which is what Story 3.6's invalidation drops.
|
|
125
|
+
*/
|
|
126
|
+
export class FieldResolver {
|
|
127
|
+
variables;
|
|
128
|
+
generation;
|
|
129
|
+
projectId;
|
|
130
|
+
maxEntries;
|
|
131
|
+
#pool;
|
|
132
|
+
#cache;
|
|
133
|
+
constructor(variables,
|
|
134
|
+
/** Bumped by the owner whenever the pool changes; part of every cache key. */
|
|
135
|
+
generation,
|
|
136
|
+
/**
|
|
137
|
+
* The project this resolver belongs to, also part of the key (Story 3.3 AC 3).
|
|
138
|
+
*
|
|
139
|
+
* Two projects genuinely can share a generation number — nothing in the type system stops a
|
|
140
|
+
* caller numbering its own — and `**BaseUrl**` resolves to a different host in each. That the
|
|
141
|
+
* current owner happens to hand out a service-global counter makes the collision unreachable
|
|
142
|
+
* today by accident, not by design, and an accident is not a cache key.
|
|
143
|
+
*/
|
|
144
|
+
projectId = '',
|
|
145
|
+
/**
|
|
146
|
+
* A cache store, optionally SHARED with other resolvers.
|
|
147
|
+
*
|
|
148
|
+
* The default is private to this resolver, and with a private store the composite key below
|
|
149
|
+
* is decorative — the instance already implies its project and generation. That was the state
|
|
150
|
+
* this class shipped in, and a test asserting the key "worked" passed with the key reduced to
|
|
151
|
+
* the raw value, which is no assertion at all.
|
|
152
|
+
*
|
|
153
|
+
* A shared store is what makes the key load-bearing, and is what Story 3.3 AC 3 describes
|
|
154
|
+
* when it asks for a cache "keyed by project and pool generation": one store, many projects,
|
|
155
|
+
* entries that must not be served across them.
|
|
156
|
+
*/
|
|
157
|
+
store = new Map(),
|
|
158
|
+
/**
|
|
159
|
+
* Upper bound on the SHARED store, as a backstop against unbounded growth.
|
|
160
|
+
*
|
|
161
|
+
* Generous, because evicting a live project's entries costs a recompute rather than
|
|
162
|
+
* correctness, and the entries are small. The precise rule is `#evictSuperseded`'s
|
|
163
|
+
* generation check; this only catches what that cannot see.
|
|
164
|
+
*/
|
|
165
|
+
maxEntries = 5_000) {
|
|
166
|
+
this.variables = variables;
|
|
167
|
+
this.generation = generation;
|
|
168
|
+
this.projectId = projectId;
|
|
169
|
+
this.maxEntries = maxEntries;
|
|
170
|
+
this.#pool = preparePool(variables);
|
|
171
|
+
this.#cache = store;
|
|
172
|
+
}
|
|
173
|
+
resolve(raw) {
|
|
174
|
+
if (typeof raw !== 'string') {
|
|
175
|
+
return resolveFieldValue(raw, this.#pool, this.variables);
|
|
176
|
+
}
|
|
177
|
+
const key = `${this.projectId}\u0000${this.generation}\u0000${raw}`;
|
|
178
|
+
const hit = this.#cache.get(key);
|
|
179
|
+
if (hit)
|
|
180
|
+
return hit;
|
|
181
|
+
const result = resolveFieldValue(raw, this.#pool, this.variables);
|
|
182
|
+
this.#cache.set(key, result);
|
|
183
|
+
this.#evictSuperseded();
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Drop this project's entries from earlier generations, and bound the store.
|
|
188
|
+
*
|
|
189
|
+
* The generation moves on every `variables.yaml` keystroke, so a shared store — the mode this
|
|
190
|
+
* class documents as the real one — accumulated an entry per generation per field for the life of
|
|
191
|
+
* the process. Superseded entries became UNREACHABLE, which is not the same as removed, and the
|
|
192
|
+
* docstring's claim that the cache "must vanish the moment `variables.yaml` moves" had nothing
|
|
193
|
+
* implementing it: no clear, no delete, no bound, and `cacheSize` only observed.
|
|
194
|
+
*
|
|
195
|
+
* Two rules, in order. Entries for THIS project at an older generation are dead by definition, so
|
|
196
|
+
* they go first and precisely. The size cap is the backstop for everything else — other projects'
|
|
197
|
+
* entries, which this resolver cannot reason about — evicting in insertion order, which for a
|
|
198
|
+
* `Map` is oldest-first.
|
|
199
|
+
*/
|
|
200
|
+
#evictSuperseded() {
|
|
201
|
+
const mine = `${this.projectId}\u0000`;
|
|
202
|
+
const current = `${mine}${this.generation}\u0000`;
|
|
203
|
+
for (const key of this.#cache.keys()) {
|
|
204
|
+
if (key.startsWith(mine) && !key.startsWith(current)) {
|
|
205
|
+
this.#cache.delete(key);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
while (this.#cache.size > this.maxEntries) {
|
|
209
|
+
const oldest = this.#cache.keys().next().value;
|
|
210
|
+
if (oldest === undefined)
|
|
211
|
+
break;
|
|
212
|
+
this.#cache.delete(oldest);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Forget everything this resolver cached.
|
|
217
|
+
*
|
|
218
|
+
* The invalidation hook Story 3.6 AC 2's second clause describes and nothing provided. Scoped to
|
|
219
|
+
* this project, so a shared store goes on serving the others.
|
|
220
|
+
*/
|
|
221
|
+
invalidate() {
|
|
222
|
+
const mine = `${this.projectId}\u0000`;
|
|
223
|
+
for (const key of this.#cache.keys()) {
|
|
224
|
+
if (key.startsWith(mine))
|
|
225
|
+
this.#cache.delete(key);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** How many distinct values are cached — exposed so caching is testable. Counts the whole
|
|
229
|
+
* store, which may be shared with other projects' resolvers. */
|
|
230
|
+
get cacheSize() {
|
|
231
|
+
return this.#cache.size;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=resolveField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveField.js","sourceRoot":"","sources":["../src/resolveField.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EACL,kBAAkB,GAEnB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,oCAAoC,CAAC;AA4B5C,MAAM,UAAU,WAAW,CAAC,SAAkC;IAC5D,OAAO,mBAAmB,CAAC,SAAS,CAAC,CAAC;AACxC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAAY,EACZ,IAAkB,EAClB,SAAkC;IAElC,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO;YACL,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC;YACxB,OAAO,EAAE,IAAI;YACb,WAAW,EAAE,EAAE;YACf,UAAU,EAAE,EAAE;YACd,OAAO,EAAE,EAAE;SACZ,CAAC;IACJ,CAAC;IAED,MAAM,WAAW,GAAG,kB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