@skmtc/core 0.58.2 → 0.58.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.
Files changed (63) hide show
  1. package/esm/context/BuiltOnRead.d.ts +37 -0
  2. package/esm/context/BuiltOnRead.js +199 -0
  3. package/esm/context/CoreContext.js +11 -5
  4. package/esm/context/GenerateContext.d.ts +3 -3
  5. package/esm/context/GenerateContext.js +13 -6
  6. package/esm/context/ParseContext.js +116 -11
  7. package/esm/context/Subject.d.ts +29 -2
  8. package/esm/context/Subject.js +67 -6
  9. package/esm/context/generateTypes.d.ts +2 -2
  10. package/esm/dsl/model/ModelDriver.js +2 -2
  11. package/esm/dsl/operation/gql/GqlOperationDriver.js +2 -2
  12. package/esm/dsl/operation/oas/OasOperationDriver.js +2 -2
  13. package/esm/dsl/webhook/WebhookDriver.js +2 -2
  14. package/esm/helpers/getPath.d.ts +6 -0
  15. package/esm/helpers/getPath.js +6 -0
  16. package/esm/helpers/isDeepEqual.d.ts +17 -0
  17. package/esm/helpers/isDeepEqual.js +93 -0
  18. package/esm/helpers/parseModuleName.d.ts +8 -8
  19. package/esm/helpers/parseModuleName.js +8 -8
  20. package/esm/helpers/toVariantList.d.ts +1 -1
  21. package/esm/oas/document/Document.d.ts +5 -1
  22. package/esm/oas/document/Document.js +10 -1
  23. package/esm/oas/document/documentSource.d.ts +37 -0
  24. package/esm/oas/document/documentSource.js +27 -0
  25. package/esm/oas/schema/siblings.js +6 -4
  26. package/esm/parse/v3-0/operation/toOperationsV3.d.ts +14 -0
  27. package/esm/parse/v3-0/operation/toOperationsV3.js +68 -67
  28. package/esm/parse/v3-1/operation/toOperationsV3.d.ts +14 -0
  29. package/esm/parse/v3-1/operation/toOperationsV3.js +68 -67
  30. package/esm/parse/v3-2/operation/toOperationsV3.d.ts +14 -0
  31. package/esm/parse/v3-2/operation/toOperationsV3.js +69 -68
  32. package/package.json +1 -4
  33. package/script/context/BuiltOnRead.d.ts +37 -0
  34. package/script/context/BuiltOnRead.js +203 -0
  35. package/script/context/CoreContext.js +11 -5
  36. package/script/context/GenerateContext.d.ts +3 -3
  37. package/script/context/GenerateContext.js +13 -6
  38. package/script/context/ParseContext.js +116 -11
  39. package/script/context/Subject.d.ts +29 -2
  40. package/script/context/Subject.js +67 -6
  41. package/script/context/generateTypes.d.ts +2 -2
  42. package/script/dsl/model/ModelDriver.js +2 -5
  43. package/script/dsl/operation/gql/GqlOperationDriver.js +2 -5
  44. package/script/dsl/operation/oas/OasOperationDriver.js +2 -2
  45. package/script/dsl/webhook/WebhookDriver.js +2 -5
  46. package/script/helpers/getPath.d.ts +6 -0
  47. package/script/helpers/getPath.js +10 -0
  48. package/script/helpers/isDeepEqual.d.ts +17 -0
  49. package/script/helpers/isDeepEqual.js +97 -0
  50. package/script/helpers/parseModuleName.d.ts +8 -8
  51. package/script/helpers/parseModuleName.js +8 -8
  52. package/script/helpers/toVariantList.d.ts +1 -1
  53. package/script/oas/document/Document.d.ts +5 -1
  54. package/script/oas/document/Document.js +10 -1
  55. package/script/oas/document/documentSource.d.ts +37 -0
  56. package/script/oas/document/documentSource.js +36 -0
  57. package/script/oas/schema/siblings.js +6 -7
  58. package/script/parse/v3-0/operation/toOperationsV3.d.ts +14 -0
  59. package/script/parse/v3-0/operation/toOperationsV3.js +71 -69
  60. package/script/parse/v3-1/operation/toOperationsV3.d.ts +14 -0
  61. package/script/parse/v3-1/operation/toOperationsV3.js +71 -69
  62. package/script/parse/v3-2/operation/toOperationsV3.d.ts +14 -0
  63. package/script/parse/v3-2/operation/toOperationsV3.js +72 -70
@@ -0,0 +1,37 @@
1
+ /**
2
+ * In a run with `onArtifact`, the operations and the component schemas that
3
+ * pass 1 parsed are let go once the file plan is built. Each is built again
4
+ * from the input document when a generator reads it, and let go after the
5
+ * last subject that can read it. Internal: `mod.ts` does not list this file.
6
+ */
7
+ import type { OasDocument } from '../oas/document/Document.js';
8
+ import type { Reach } from '../parse/Reach.js';
9
+ import type { PlannedSubject } from './Subject.js';
10
+ type BuiltOnReadArgs = {
11
+ document: OasDocument;
12
+ /** The subjects of the run, in the order their steps run. */
13
+ subjects: PlannedSubject[];
14
+ reach: Reach;
15
+ };
16
+ /**
17
+ * The run's operations and component schemas, built when read. `start` and
18
+ * `finish` go round each step, as `FilePlan.finish` does.
19
+ */
20
+ export declare class BuiltOnRead {
21
+ #private;
22
+ private constructor();
23
+ /**
24
+ * Lets the parsed operations and component schemas of `document` go, or
25
+ * returns `undefined` when its parse context builds nothing again (a run
26
+ * without `onArtifact`). Call it once the file plan is built.
27
+ */
28
+ static start({ document, subjects, reach }: BuiltOnReadArgs): BuiltOnRead | undefined;
29
+ /**
30
+ * `subject`'s step starts. The first read of its operation in the step
31
+ * builds it, and every generator of the step gets that object.
32
+ */
33
+ start(subject: PlannedSubject): void;
34
+ /** `subject`'s step has run: its operation goes, and each schema no subject left reaches. */
35
+ finish(subject: PlannedSubject): void;
36
+ }
37
+ export {};
@@ -0,0 +1,199 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _BuiltOnRead_schemas, _OperationsOnRead_instances, _OperationsOnRead_source, _OperationsOnRead_places, _OperationsOnRead_held, _OperationsOnRead_listed, _OperationsOnRead_list, _SchemasOnRead_instances, _SchemasOnRead_source, _SchemasOnRead_reach, _SchemasOnRead_names, _SchemasOnRead_built, _SchemasOnRead_readInStep, _SchemasOnRead_named, _SchemasOnRead_namedLeft, _SchemasOnRead_reachedLeft, _SchemasOnRead_read, _SchemasOnRead_toReached;
13
+ import { OasOperationSubject } from './Subject.js';
14
+ import { fromModelKey } from '../helpers/refFns.js';
15
+ import { setUnlistedOperations, toDocumentSource } from '../oas/document/documentSource.js';
16
+ /**
17
+ * The run's operations and component schemas, built when read. `start` and
18
+ * `finish` go round each step, as `FilePlan.finish` does.
19
+ */
20
+ export class BuiltOnRead {
21
+ constructor(schemas) {
22
+ _BuiltOnRead_schemas.set(this, void 0);
23
+ __classPrivateFieldSet(this, _BuiltOnRead_schemas, schemas, "f");
24
+ }
25
+ /**
26
+ * Lets the parsed operations and component schemas of `document` go, or
27
+ * returns `undefined` when its parse context builds nothing again (a run
28
+ * without `onArtifact`). Call it once the file plan is built.
29
+ */
30
+ static start({ document, subjects, reach }) {
31
+ const source = toDocumentSource(document);
32
+ if (source === undefined) {
33
+ return undefined;
34
+ }
35
+ const operations = new OperationsOnRead({ document, source });
36
+ subjects.forEach(subject => {
37
+ if (subject instanceof OasOperationSubject) {
38
+ subject.buildOnRead(operations);
39
+ }
40
+ });
41
+ return new BuiltOnRead(new SchemasOnRead({ document, source, subjects, reach }));
42
+ }
43
+ /**
44
+ * `subject`'s step starts. The first read of its operation in the step
45
+ * builds it, and every generator of the step gets that object.
46
+ */
47
+ start(subject) {
48
+ if (subject instanceof OasOperationSubject) {
49
+ subject.startStep();
50
+ }
51
+ }
52
+ /** `subject`'s step has run: its operation goes, and each schema no subject left reaches. */
53
+ finish(subject) {
54
+ if (subject instanceof OasOperationSubject) {
55
+ subject.finishStep();
56
+ }
57
+ __classPrivateFieldGet(this, _BuiltOnRead_schemas, "f").finish(subject);
58
+ }
59
+ }
60
+ _BuiltOnRead_schemas = new WeakMap();
61
+ /** The key of an operation's place. A method has no space in it. */
62
+ const toPlaceKey = ({ path, method }) => `${method} ${path}`;
63
+ /**
64
+ * The operations of a document, built when a step loads one. Listing
65
+ * `document.operations` builds every one and keeps it for the rest of the
66
+ * run: a subject's operation is then the listed object.
67
+ */
68
+ class OperationsOnRead {
69
+ constructor({ document, source }) {
70
+ _OperationsOnRead_instances.add(this);
71
+ _OperationsOnRead_source.set(this, void 0);
72
+ /** The place of each operation pass 1 kept, in document order. */
73
+ _OperationsOnRead_places.set(this, void 0);
74
+ /** The operations loaded and not let go yet. */
75
+ _OperationsOnRead_held.set(this, new Map()
76
+ /** Every operation, once they are listed. */
77
+ );
78
+ /** Every operation, once they are listed. */
79
+ _OperationsOnRead_listed.set(this, void 0);
80
+ __classPrivateFieldSet(this, _OperationsOnRead_source, source, "f");
81
+ const operations = document.operations;
82
+ __classPrivateFieldSet(this, _OperationsOnRead_places, operations.map(({ path, method }) => ({ path, method })), "f");
83
+ // The getter gives the document's own array: emptying it lets the parsed operations go.
84
+ operations.splice(0);
85
+ setUnlistedOperations(document, () => __classPrivateFieldGet(this, _OperationsOnRead_instances, "m", _OperationsOnRead_list).call(this));
86
+ }
87
+ load(place) {
88
+ const key = toPlaceKey(place);
89
+ const found = __classPrivateFieldGet(this, _OperationsOnRead_listed, "f")?.get(key) ?? __classPrivateFieldGet(this, _OperationsOnRead_held, "f").get(key);
90
+ if (found !== undefined) {
91
+ return found;
92
+ }
93
+ const operation = __classPrivateFieldGet(this, _OperationsOnRead_source, "f").toOperation(place);
94
+ __classPrivateFieldGet(this, _OperationsOnRead_held, "f").set(key, operation);
95
+ return operation;
96
+ }
97
+ letGo(place) {
98
+ __classPrivateFieldGet(this, _OperationsOnRead_held, "f").delete(toPlaceKey(place));
99
+ }
100
+ }
101
+ _OperationsOnRead_source = new WeakMap(), _OperationsOnRead_places = new WeakMap(), _OperationsOnRead_held = new WeakMap(), _OperationsOnRead_listed = new WeakMap(), _OperationsOnRead_instances = new WeakSet(), _OperationsOnRead_list = function _OperationsOnRead_list() {
102
+ const operations = __classPrivateFieldGet(this, _OperationsOnRead_places, "f").map(place => this.load(place));
103
+ __classPrivateFieldSet(this, _OperationsOnRead_listed, new Map(operations.map(operation => [toPlaceKey(operation), operation])), "f");
104
+ __classPrivateFieldGet(this, _OperationsOnRead_held, "f").clear();
105
+ return operations;
106
+ };
107
+ /**
108
+ * The component schemas of a document, built when read. `components.schemas`
109
+ * keeps its names in their order, and each entry is built when it is read. A
110
+ * built schema is kept while a subject left to run reaches it, and while the
111
+ * step that read it runs. A read after that builds it again.
112
+ *
113
+ * The count is the plan's two-step count: by a model's key, how many
114
+ * subjects left name it; by a schema's name, how many named models with
115
+ * subjects left reach it.
116
+ */
117
+ class SchemasOnRead {
118
+ constructor({ document, source, subjects, reach }) {
119
+ _SchemasOnRead_instances.add(this);
120
+ _SchemasOnRead_source.set(this, void 0);
121
+ _SchemasOnRead_reach.set(this, void 0);
122
+ /** The names of the schemas, which are their model keys. */
123
+ _SchemasOnRead_names.set(this, void 0);
124
+ _SchemasOnRead_built.set(this, new Map()
125
+ /** The schemas built in this step that no subject left reaches. */
126
+ );
127
+ /** The schemas built in this step that no subject left reaches. */
128
+ _SchemasOnRead_readInStep.set(this, new Set());
129
+ _SchemasOnRead_named.set(this, new Map()
130
+ /** By a model's key: how many subjects that name it are left to run. */
131
+ );
132
+ /** By a model's key: how many subjects that name it are left to run. */
133
+ _SchemasOnRead_namedLeft.set(this, new Map()
134
+ /** By a schema's name: how many named models with subjects left reach it. */
135
+ );
136
+ /** By a schema's name: how many named models with subjects left reach it. */
137
+ _SchemasOnRead_reachedLeft.set(this, new Map());
138
+ __classPrivateFieldSet(this, _SchemasOnRead_source, source, "f");
139
+ __classPrivateFieldSet(this, _SchemasOnRead_reach, reach, "f");
140
+ const record = document.components?.schemas ?? {};
141
+ const names = document.components?.toSchemasRefNames() ?? [];
142
+ __classPrivateFieldSet(this, _SchemasOnRead_names, new Set(names), "f");
143
+ subjects.forEach(subject => {
144
+ const named = subject.toNamedModels(reach);
145
+ named.forEach(key => {
146
+ const namedLeft = __classPrivateFieldGet(this, _SchemasOnRead_namedLeft, "f").get(key) ?? 0;
147
+ if (namedLeft === 0) {
148
+ __classPrivateFieldGet(this, _SchemasOnRead_instances, "m", _SchemasOnRead_toReached).call(this, key).forEach(name => {
149
+ __classPrivateFieldGet(this, _SchemasOnRead_reachedLeft, "f").set(name, (__classPrivateFieldGet(this, _SchemasOnRead_reachedLeft, "f").get(name) ?? 0) + 1);
150
+ });
151
+ }
152
+ __classPrivateFieldGet(this, _SchemasOnRead_namedLeft, "f").set(key, namedLeft + 1);
153
+ });
154
+ __classPrivateFieldGet(this, _SchemasOnRead_named, "f").set(subject, named);
155
+ });
156
+ // Each entry becomes a getter at its own position, so the names keep
157
+ // their order, and the parsed schema it held is let go.
158
+ names.forEach(name => {
159
+ Object.defineProperty(record, name, {
160
+ get: () => __classPrivateFieldGet(this, _SchemasOnRead_instances, "m", _SchemasOnRead_read).call(this, name),
161
+ enumerable: true,
162
+ configurable: true
163
+ });
164
+ });
165
+ }
166
+ finish(subject) {
167
+ const named = __classPrivateFieldGet(this, _SchemasOnRead_named, "f").get(subject) ?? [];
168
+ __classPrivateFieldGet(this, _SchemasOnRead_named, "f").delete(subject);
169
+ named.forEach(key => {
170
+ const namedLeft = (__classPrivateFieldGet(this, _SchemasOnRead_namedLeft, "f").get(key) ?? 0) - 1;
171
+ __classPrivateFieldGet(this, _SchemasOnRead_namedLeft, "f").set(key, namedLeft);
172
+ if (namedLeft === 0) {
173
+ __classPrivateFieldGet(this, _SchemasOnRead_instances, "m", _SchemasOnRead_toReached).call(this, key).forEach(name => {
174
+ const reachedLeft = (__classPrivateFieldGet(this, _SchemasOnRead_reachedLeft, "f").get(name) ?? 0) - 1;
175
+ __classPrivateFieldGet(this, _SchemasOnRead_reachedLeft, "f").set(name, reachedLeft);
176
+ if (reachedLeft === 0) {
177
+ __classPrivateFieldGet(this, _SchemasOnRead_built, "f").delete(name);
178
+ }
179
+ });
180
+ }
181
+ });
182
+ __classPrivateFieldGet(this, _SchemasOnRead_readInStep, "f").forEach(name => __classPrivateFieldGet(this, _SchemasOnRead_built, "f").delete(name));
183
+ __classPrivateFieldGet(this, _SchemasOnRead_readInStep, "f").clear();
184
+ }
185
+ }
186
+ _SchemasOnRead_source = new WeakMap(), _SchemasOnRead_reach = new WeakMap(), _SchemasOnRead_names = new WeakMap(), _SchemasOnRead_built = new WeakMap(), _SchemasOnRead_readInStep = new WeakMap(), _SchemasOnRead_named = new WeakMap(), _SchemasOnRead_namedLeft = new WeakMap(), _SchemasOnRead_reachedLeft = new WeakMap(), _SchemasOnRead_instances = new WeakSet(), _SchemasOnRead_read = function _SchemasOnRead_read(name) {
187
+ const found = __classPrivateFieldGet(this, _SchemasOnRead_built, "f").get(name);
188
+ if (found !== undefined) {
189
+ return found;
190
+ }
191
+ const schema = __classPrivateFieldGet(this, _SchemasOnRead_source, "f").toSchema(name);
192
+ __classPrivateFieldGet(this, _SchemasOnRead_built, "f").set(name, schema);
193
+ if ((__classPrivateFieldGet(this, _SchemasOnRead_reachedLeft, "f").get(name) ?? 0) === 0) {
194
+ __classPrivateFieldGet(this, _SchemasOnRead_readInStep, "f").add(name);
195
+ }
196
+ return schema;
197
+ }, _SchemasOnRead_toReached = function _SchemasOnRead_toReached(key) {
198
+ return __classPrivateFieldGet(this, _SchemasOnRead_reach, "f").ofModel(fromModelKey(key)).filter(model => __classPrivateFieldGet(this, _SchemasOnRead_names, "f").has(model));
199
+ };
@@ -27,6 +27,7 @@ import { bold, gray, red, yellow, blue } from '../deps/jsr.io/@std/fmt/1.0.10/co
27
27
  import { toSerializedError } from '../types/SerializedError.js';
28
28
  import { GenerationFailure } from './toGenerationFailure.js';
29
29
  import { toCountPhrase } from '../helpers/toCountPhrase.js';
30
+ import { markPlannedParse } from '../oas/document/documentSource.js';
30
31
  /**
31
32
  * The main orchestration class for the SKMTC transformation pipeline.
32
33
  *
@@ -308,7 +309,12 @@ export class CoreContext {
308
309
  __classPrivateFieldSet(this, _CoreContext_phaseName, 'parse', "f");
309
310
  __classPrivateFieldSet(this, _CoreContext_parseIssues, [], "f");
310
311
  __classPrivateFieldSet(this, _CoreContext_enrichmentWarnings, [], "f");
311
- const phase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupParsePhase).call(this, document, settings);
312
+ // A capturing render and an inspection read every file at the end
313
+ // of the run, so such a run gives no file before then. A run that
314
+ // gives files early can build its operations and schemas again when
315
+ // a generator reads them, so the parse context knows it first.
316
+ const givesFilesEarly = onArtifact !== undefined && !attribution?.postPass && !inspect;
317
+ const phase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupParsePhase).call(this, document, settings, givesFilesEarly);
312
318
  __classPrivateFieldSet(this, _CoreContext_phase, phase, "f");
313
319
  __classPrivateFieldSet(this, _CoreContext_parseIssues, phase.context.issues, "f");
314
320
  const parsedDocument = stackTrail.trace('parse', st => phase.context.parse(st));
@@ -334,9 +340,6 @@ export class CoreContext {
334
340
  captureChannel
335
341
  });
336
342
  __classPrivateFieldSet(this, _CoreContext_phase, generatePhase, "f");
337
- // A capturing render and an inspection read every file at the end
338
- // of the run, so such a run gives no file before then.
339
- const givesFilesEarly = onArtifact !== undefined && !attribution?.postPass && !inspect;
340
343
  const generated = generatePhase.context.toArtifacts(st, givesFilesEarly
341
344
  ? {
342
345
  reach: phase.context.toReach(),
@@ -639,7 +642,7 @@ _CoreContext_phase = new WeakMap(), _CoreContext_results = new WeakMap(), _CoreC
639
642
  throw new Error(`Unhandled protocol type: ${JSON.stringify(_exhaustive)}`);
640
643
  }
641
644
  }
642
- }, _CoreContext_setupParsePhase = function _CoreContext_setupParsePhase(input, settings) {
645
+ }, _CoreContext_setupParsePhase = function _CoreContext_setupParsePhase(input, settings, planned = false) {
643
646
  if (settings?.parse && settings.system) {
644
647
  this.logger.warn('settings.parse and settings.system are both set: settings.parse is used and settings.system is ignored. ' +
645
648
  'settings.system is the deprecated name of settings.parse; remove it.');
@@ -650,6 +653,9 @@ _CoreContext_phase = new WeakMap(), _CoreContext_results = new WeakMap(), _CoreC
650
653
  silent: this.silent,
651
654
  system: settings?.parse ?? settings?.system
652
655
  });
656
+ if (planned) {
657
+ markPlannedParse(parseContext);
658
+ }
653
659
  return { type: 'parse', context: parseContext };
654
660
  }, _CoreContext_setupGeneratePhase = function _CoreContext_setupGeneratePhase({ document, settings, toGeneratorConfigMap, captureChannel }) {
655
661
  const generateContext = new GenerateContext({
@@ -175,9 +175,9 @@ export declare class GenerateContext implements GenerateContextType {
175
175
  * The recording enrichment accessor — the single choke point every
176
176
  * enrichment lookup routes through (dispatch loops, projection-base
177
177
  * `toEnrichments`, the Drivers' peer-variant guards). Segments are
178
- * literal keys (the array form of `get` keeps dotted paths intact);
179
- * the read is recorded so the post-walk consumption audit can flag
180
- * configured entries no lookup ever touched.
178
+ * literal keys (`getPath` keeps dotted paths intact); the read is
179
+ * recorded so the post-walk consumption audit can flag configured
180
+ * entries no lookup ever touched.
181
181
  */
182
182
  readEnrichment(segments: readonly string[]): unknown;
183
183
  /** Add a warning to this run's enrichment audit (deduplicated). */
@@ -27,8 +27,9 @@ import { DEFAULT_VARIANT } from '../types/Variant.js';
27
27
  import { toVariantList } from '../helpers/toVariantList.js';
28
28
  import { GqlOperationSubject, ModelSubject, OasOperationSubject, WebhookSubject } from './Subject.js';
29
29
  import { FilePlan } from './FilePlan.js';
30
+ import { BuiltOnRead } from './BuiltOnRead.js';
30
31
  export { toGqlOperationSource, toModelSource, toOasOperationSource, toWebhookSource } from './Subject.js';
31
- import get from 'lodash-es/get.js';
32
+ import { getPath } from '../helpers/getPath.js';
32
33
  import { Inserted } from '../dsl/Inserted.js';
33
34
  import { CodeFileBase } from '../dsl/CodeFileBase.js';
34
35
  import { isDefinitionContainer } from '../dsl/DefinitionContainer.js';
@@ -218,9 +219,9 @@ export class GenerateContext {
218
219
  * The recording enrichment accessor — the single choke point every
219
220
  * enrichment lookup routes through (dispatch loops, projection-base
220
221
  * `toEnrichments`, the Drivers' peer-variant guards). Segments are
221
- * literal keys (the array form of `get` keeps dotted paths intact);
222
- * the read is recorded so the post-walk consumption audit can flag
223
- * configured entries no lookup ever touched.
222
+ * literal keys (`getPath` keeps dotted paths intact); the read is
223
+ * recorded so the post-walk consumption audit can flag configured
224
+ * entries no lookup ever touched.
224
225
  */
225
226
  readEnrichment(segments) {
226
227
  // Recording only matters when there is config to audit — on the common
@@ -229,7 +230,7 @@ export class GenerateContext {
229
230
  if (this.settings?.enrichments !== undefined) {
230
231
  __classPrivateFieldGet(this, _GenerateContext_enrichmentAudit, "f").consume(segments);
231
232
  }
232
- return get(this.settings, ['enrichments', ...segments]);
233
+ return getPath(this.settings, ['enrichments', ...segments]);
233
234
  }
234
235
  /** Add a warning to this run's enrichment audit (deduplicated). */
235
236
  reportEnrichmentWarning(warning) {
@@ -1025,11 +1026,17 @@ _GenerateContext_files = new WeakMap(), _GenerateContext_failedItems = new WeakM
1025
1026
  ...(readyAfter.get(0) ?? []),
1026
1027
  ...modelSteps.flatMap((step, index) => [step, ...(readyAfter.get(index + 1) ?? [])])
1027
1028
  ];
1028
- const plan = this.toFilePlan({ subjects: steps.map(({ subject }) => subject), reach });
1029
+ const subjects = steps.map(({ subject }) => subject);
1030
+ const plan = this.toFilePlan({ subjects, reach });
1031
+ // The plan asked the pass 1 operations. From here each step builds its
1032
+ // operation again, and each schema is built when it is read.
1033
+ const builtOnRead = BuiltOnRead.start({ document: oasDocument, subjects, reach });
1029
1034
  steps.forEach(step => {
1035
+ builtOnRead?.start(step.subject);
1030
1036
  step.run();
1031
1037
  // A run that has failed gives no more files: it will write nothing.
1032
1038
  const complete = plan.finish(step.subject);
1039
+ builtOnRead?.finish(step.subject);
1033
1040
  if (__classPrivateFieldGet(this, _GenerateContext_failedItems, "f").length === 0) {
1034
1041
  complete.forEach(path => __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_release).call(this, path, onFile));
1035
1042
  }
@@ -9,7 +9,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
11
  };
12
- var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_refTargets, _ParseContext_refTarget, _ParseContext_keptRefs, _ParseContext_issueIdentities, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_parseRefTargets, _ParseContext_inRefTarget, _ParseContext_addIssue, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_logDependencyIssue, _ParseContext_toDocumentLocation, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_toRefError, _ParseContext_toLoopOf, _ParseContext_removeHolder;
12
+ var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_refTargets, _ParseContext_refTarget, _ParseContext_keptRefs, _ParseContext_issueIdentities, _ParseContext_rootFrames, _ParseContext_dialect, _ParseContext_rebuilding, _ParseContext_removedPathParameters, _ParseContext_rebuildOperation, _ParseContext_rebuildSchema, _ParseContext_whileRebuilding, _ParseContext_removeComponent, _ParseContext_parseRefTargets, _ParseContext_inRefTarget, _ParseContext_addIssue, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_logDependencyIssue, _ParseContext_toDocumentLocation, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_toRefError, _ParseContext_toLoopOf, _ParseContext_removeHolder;
13
13
  /**
14
14
  * Unified parse context for both OAS and GraphQL inputs.
15
15
  *
@@ -51,6 +51,21 @@ import { toRefKey } from '../parse/toRefKey.js';
51
51
  import { toRefTargetV3 } from '../parse/v3-0/ref/toRefTargetV3.js';
52
52
  import { toRefTargetV3 as toRefTargetV31 } from '../parse/v3-1/ref/toRefTargetV3.js';
53
53
  import { toRefTargetV3 as toRefTargetV32 } from '../parse/v3-2/ref/toRefTargetV3.js';
54
+ import { toPathOperationsV3 } from '../parse/v3-0/operation/toOperationsV3.js';
55
+ import { toPathOperationsV3 as toPathOperationsV31 } from '../parse/v3-1/operation/toOperationsV3.js';
56
+ import { toPathOperationsV3 as toPathOperationsV32 } from '../parse/v3-2/operation/toOperationsV3.js';
57
+ import { toSchemaV3 } from '../parse/v3-0/schema/toSchemasV3.js';
58
+ import { toSchemaV3 as toSchemaV31 } from '../parse/v3-1/schema/toSchemasV3.js';
59
+ import { toSchemaV3 as toSchemaV32 } from '../parse/v3-2/schema/toSchemasV3.js';
60
+ import { isPlannedParse, setDocumentSource } from '../oas/document/documentSource.js';
61
+ /** Takes a removed path-level parameter out of `pathItem`, which its operations share. */
62
+ const removePathParameter = (pathItem, { entryRef, name, in: location }) => {
63
+ if (pathItem?.parameters) {
64
+ pathItem.parameters = pathItem.parameters.filter(parameter => parameter.isRef()
65
+ ? parameter.$ref !== entryRef
66
+ : entryRef !== undefined || parameter.name !== name || parameter.location !== location);
67
+ }
68
+ };
54
69
  /** The issue logged when a ref target's parser throws. */
55
70
  const toInvalidType = (refType) => {
56
71
  switch (refType) {
@@ -177,7 +192,17 @@ export class ParseContext {
177
192
  * 'parse']` through `toArtifacts`). Every trail built during the walk
178
193
  * starts with them; `#toDocumentTrail` strips them.
179
194
  */
180
- _ParseContext_rootFrames.set(this, []);
195
+ _ParseContext_rootFrames.set(this, []
196
+ /** The dialect `parse()` picked for an OpenAPI document. */
197
+ );
198
+ /** The dialect `parse()` picked for an OpenAPI document. */
199
+ _ParseContext_dialect.set(this, void 0);
200
+ /** While a part of the document is built again, nothing is recorded: no issue, no ref and no ref error. */
201
+ _ParseContext_rebuilding.set(this, false
202
+ /** By path: the path-level parameters `#removePathParameterConsumer` took out of the path item. */
203
+ );
204
+ /** By path: the path-level parameters `#removePathParameterConsumer` took out of the path item. */
205
+ _ParseContext_removedPathParameters.set(this, new Map());
181
206
  this.logger = logger;
182
207
  this.silent = silent;
183
208
  this.system = system ?? {};
@@ -295,6 +320,7 @@ export class ParseContext {
295
320
  // parser tree. Everything downstream is version-specific code living
296
321
  // in its own tree (v3-0 / v3-1 / v3-2) and never re-checks the version.
297
322
  const dialect = toOasDialect(oasState.documentObject.openapi);
323
+ __classPrivateFieldSet(this, _ParseContext_dialect, dialect, "f");
298
324
  switch (dialect) {
299
325
  case '3.0':
300
326
  oasState.oasDocument.fields = toDocumentFieldsV3({
@@ -327,6 +353,14 @@ export class ParseContext {
327
353
  }
328
354
  __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_checkRefs).call(this);
329
355
  this.removeErroredItems();
356
+ // A run that gives files early builds its operations and component
357
+ // schemas again when a generator reads them (`BuiltOnRead`).
358
+ if (isPlannedParse(this)) {
359
+ setDocumentSource(oasState.oasDocument, {
360
+ toOperation: place => __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_rebuildOperation).call(this, place),
361
+ toSchema: name => __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_rebuildSchema).call(this, name)
362
+ });
363
+ }
330
364
  return { type: 'oas', value: oasState.oasDocument };
331
365
  }
332
366
  case 'gql': {
@@ -438,6 +472,9 @@ export class ParseContext {
438
472
  * would use type names or qualified field paths.
439
473
  */
440
474
  registerRef(consumer, refKey) {
475
+ if (__classPrivateFieldGet(this, _ParseContext_rebuilding, "f")) {
476
+ return;
477
+ }
441
478
  const refConsumer = { consumer, refTarget: __classPrivateFieldGet(this, _ParseContext_refTarget, "f") };
442
479
  const existing = __classPrivateFieldGet(this, _ParseContext_refConsumers, "f").get(refKey);
443
480
  if (existing) {
@@ -454,6 +491,9 @@ export class ParseContext {
454
491
  * resolve it there.
455
492
  */
456
493
  registerOasRef(consumer, ref) {
494
+ if (__classPrivateFieldGet(this, _ParseContext_rebuilding, "f")) {
495
+ return;
496
+ }
457
497
  this.registerRef(consumer, toRefKey(ref));
458
498
  __classPrivateFieldGet(this, _ParseContext_oasRefs, "f").push({ consumer, ref, refTarget: __classPrivateFieldGet(this, _ParseContext_refTarget, "f") });
459
499
  }
@@ -490,7 +530,7 @@ export class ParseContext {
490
530
  * each `INVALID_DEPENDENCY_REF` it causes names it.
491
531
  */
492
532
  registerRefError(error, refKey, location) {
493
- if (!refKey)
533
+ if (!refKey || __classPrivateFieldGet(this, _ParseContext_rebuilding, "f"))
494
534
  return;
495
535
  const refError = { error, location };
496
536
  const existing = __classPrivateFieldGet(this, _ParseContext_refErrors, "f").get(refKey);
@@ -518,6 +558,10 @@ export class ParseContext {
518
558
  * manifest).
519
559
  */
520
560
  logIssueAt(issue, parent) {
561
+ // A rebuild logs nothing: pass 1 logged each issue once. Every issue is kept through here.
562
+ if (__classPrivateFieldGet(this, _ParseContext_rebuilding, "f")) {
563
+ return;
564
+ }
521
565
  const added = __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_addIssue).call(this, toParseIssue(issue));
522
566
  if (added && !this.silent) {
523
567
  const message = `${issue.type} at ${__classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_toDocumentLocation).call(this, issue)}: ${issue.message}`;
@@ -651,7 +695,69 @@ export class ParseContext {
651
695
  });
652
696
  }
653
697
  }
654
- _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMap(), _ParseContext_oasRefs = new WeakMap(), _ParseContext_refTargets = new WeakMap(), _ParseContext_refTarget = new WeakMap(), _ParseContext_keptRefs = new WeakMap(), _ParseContext_issueIdentities = new WeakMap(), _ParseContext_rootFrames = new WeakMap(), _ParseContext_instances = new WeakSet(), _ParseContext_removeComponent = function _ParseContext_removeComponent(refKey) {
698
+ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMap(), _ParseContext_oasRefs = new WeakMap(), _ParseContext_refTargets = new WeakMap(), _ParseContext_refTarget = new WeakMap(), _ParseContext_keptRefs = new WeakMap(), _ParseContext_issueIdentities = new WeakMap(), _ParseContext_rootFrames = new WeakMap(), _ParseContext_dialect = new WeakMap(), _ParseContext_rebuilding = new WeakMap(), _ParseContext_removedPathParameters = new WeakMap(), _ParseContext_instances = new WeakSet(), _ParseContext_rebuildOperation = function _ParseContext_rebuildOperation({ path, method }) {
699
+ const pathItem = this.documentObject.paths?.[path];
700
+ if (!isPlainObject(pathItem)) {
701
+ throw new Error(`Cannot build the operation '${method.toUpperCase()} ${path}' again: the document has no such path`);
702
+ }
703
+ const args = {
704
+ path,
705
+ pathItem,
706
+ method,
707
+ stackTrail: new StackTrail([...__classPrivateFieldGet(this, _ParseContext_rootFrames, "f"), 'paths', path]),
708
+ context: this
709
+ };
710
+ const [operation] = __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_whileRebuilding).call(this, () => {
711
+ switch (__classPrivateFieldGet(this, _ParseContext_dialect, "f")) {
712
+ case '3.0':
713
+ return toPathOperationsV3(args);
714
+ case '3.1':
715
+ return toPathOperationsV31(args);
716
+ case '3.2':
717
+ return toPathOperationsV32(args);
718
+ default:
719
+ throw new Error('Cannot build an operation again before the document is parsed');
720
+ }
721
+ });
722
+ if (operation === undefined) {
723
+ throw new Error(`Cannot build the operation '${method.toUpperCase()} ${path}' again: it does not parse`);
724
+ }
725
+ __classPrivateFieldGet(this, _ParseContext_removedPathParameters, "f")
726
+ .get(path)
727
+ ?.forEach(removed => removePathParameter(operation.pathItem, removed));
728
+ return operation;
729
+ }, _ParseContext_rebuildSchema = function _ParseContext_rebuildSchema(name) {
730
+ const schema = this.documentObject.components?.schemas?.[name];
731
+ if (schema === undefined) {
732
+ throw new Error(`Cannot build the schema '${name}' again: the document has no such schema`);
733
+ }
734
+ const args = {
735
+ schema,
736
+ stackTrail: new StackTrail([...__classPrivateFieldGet(this, _ParseContext_rootFrames, "f"), 'components', 'schemas', name]),
737
+ context: this
738
+ };
739
+ return __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_whileRebuilding).call(this, () => {
740
+ switch (__classPrivateFieldGet(this, _ParseContext_dialect, "f")) {
741
+ case '3.0':
742
+ return toSchemaV3(args);
743
+ case '3.1':
744
+ return toSchemaV31(args);
745
+ case '3.2':
746
+ return toSchemaV32(args);
747
+ default:
748
+ throw new Error('Cannot build a schema again before the document is parsed');
749
+ }
750
+ });
751
+ }, _ParseContext_whileRebuilding = function _ParseContext_whileRebuilding(fn) {
752
+ const previous = __classPrivateFieldGet(this, _ParseContext_rebuilding, "f");
753
+ __classPrivateFieldSet(this, _ParseContext_rebuilding, true, "f");
754
+ try {
755
+ return fn();
756
+ }
757
+ finally {
758
+ __classPrivateFieldSet(this, _ParseContext_rebuilding, previous, "f");
759
+ }
760
+ }, _ParseContext_removeComponent = function _ParseContext_removeComponent(refKey) {
655
761
  if (this.protocol.type !== 'oas') {
656
762
  return;
657
763
  }
@@ -760,19 +866,18 @@ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMa
760
866
  ? rawEntry
761
867
  : toRawRefTarget(documentObject, toRefParts({ $ref: entryRef })));
762
868
  const broken = [{ location: documentTrail.toString(), message: '', ...nameAndIn }];
869
+ const removed = { entryRef, ...nameAndIn };
870
+ // An operation built again later gets a path item of its own, which loses the entry the same way.
871
+ if (first === 'paths') {
872
+ __classPrivateFieldGet(this, _ParseContext_removedPathParameters, "f").set(second, [...(__classPrivateFieldGet(this, _ParseContext_removedPathParameters, "f").get(second) ?? []), removed]);
873
+ }
763
874
  const operations = first === 'paths'
764
875
  ? oasDocument.operations.filter(({ path }) => path === second)
765
876
  : oasDocument.webhooks.filter(({ name }) => name === second);
766
877
  const removedLocations = operations.flatMap(({ method, pathItem }) => {
767
878
  const rawOperation = isPlainObject(rawPathItem) ? rawPathItem[method] : undefined;
768
879
  if (toApplyingBrokenParameter({ broken, operation: rawOperation }) === undefined) {
769
- if (pathItem?.parameters) {
770
- pathItem.parameters = pathItem.parameters.filter(parameter => parameter.isRef()
771
- ? parameter.$ref !== entryRef
772
- : entryRef !== undefined ||
773
- parameter.name !== nameAndIn.name ||
774
- parameter.location !== nameAndIn.in);
775
- }
880
+ removePathParameter(pathItem, removed);
776
881
  return [];
777
882
  }
778
883
  return oasDocument.removeItem(new StackTrail([first, second, method])) === undefined
@@ -8,8 +8,10 @@ import type { OasWebhook } from '../oas/webhook/Webhook.js';
8
8
  import type { GqlOperation } from '../gql/operation/GqlOperation.js';
9
9
  import type { DeclaredProjection } from '../dsl/DeclaredProjection.js';
10
10
  import type { Reach } from '../parse/Reach.js';
11
+ import type { OperationPlace } from '../oas/document/documentSource.js';
11
12
  import { type RefParts } from '../helpers/refFns.js';
12
13
  import type { RefName } from '../types/RefName.js';
14
+ import type { Method } from '../types/Method.js';
13
15
  import type { IncludeModelRefs, IncludePaths, SkipModelRefs, SkipPaths } from '../types/Settings.js';
14
16
  import type { GqlOperationSource, ModelSource, OasOperationSource, PreviewModule, WebhookSource } from '../types/Preview.js';
15
17
  /** A generator entry, the run's context and one variant: what a subject is asked with. */
@@ -71,14 +73,39 @@ export type Subject<Entry, Filter> = PlannedSubject & {
71
73
  toPreviewModule: (args: AskArgs<Entry>) => PreviewModule | undefined;
72
74
  toSource(args: ToSourceArgs): OasOperationSource | WebhookSource | GqlOperationSource | ModelSource;
73
75
  };
74
- /** An OpenAPI operation as a subject. Its results and enrichments are at `[path][method]`. */
76
+ /** What builds an operation again from its place, and lets it go. */
77
+ export type OperationLoader = {
78
+ load(place: OperationPlace): OasOperation;
79
+ letGo(place: OperationPlace): void;
80
+ };
81
+ /**
82
+ * An OpenAPI operation as a subject. Its results and enrichments are at `[path][method]`.
83
+ *
84
+ * It holds the operation it is constructed with. In a run that builds
85
+ * operations on read, it lets that one go once the file plan is built. In
86
+ * its step, the first read builds its operation through the document, and
87
+ * it lets the operation go when the step has run.
88
+ */
75
89
  export declare class OasOperationSubject implements Subject<OasOperationEntry, IncludePaths | SkipPaths> {
90
+ #private;
76
91
  readonly type = "oasOperation";
77
- operation: OasOperation;
92
+ readonly path: string;
93
+ readonly method: Method;
78
94
  trail: string[];
79
95
  enrichmentPath: string[];
80
96
  label: string;
81
97
  constructor(operation: OasOperation);
98
+ /**
99
+ * Its operation. In a run that builds operations on read, only while its
100
+ * step runs: the first read in the step builds it.
101
+ */
102
+ get operation(): OasOperation;
103
+ /** Lets go of the operation it was constructed with: from now on, `loader` gives it. */
104
+ buildOnRead(loader: OperationLoader): void;
105
+ /** Its step starts. A step that never reads its operation, such as one with no generator, builds none. */
106
+ startStep(): void;
107
+ /** Its step has run: it lets go of the operation the step built. */
108
+ finishStep(): void;
82
109
  isSupported({ entry, context, variant }: AskArgs<OasOperationEntry>): boolean;
83
110
  matches(filter: IncludePaths | SkipPaths | undefined, variant: string): boolean;
84
111
  transform({ entry, context, variant }: AskArgs<OasOperationEntry>): void;