@usearazzo/runner 1.0.1-alpha.32 → 1.0.1-alpha.33

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 (37) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/arazzo-runner.browser.js +678 -173
  3. package/dist/arazzo-runner.browser.min.js +1 -1
  4. package/package.json +6 -6
  5. package/src/action/ActionResolver.cjs +13 -12
  6. package/src/action/ActionResolver.mjs +13 -12
  7. package/src/criterion/CriterionEvaluator.cjs +13 -8
  8. package/src/criterion/CriterionEvaluator.mjs +13 -8
  9. package/src/criterion/SimpleCriterionEvaluator.cjs +5 -3
  10. package/src/criterion/SimpleCriterionEvaluator.mjs +6 -4
  11. package/src/executor/OpenAPIOperationLocatorNormalizer.cjs +1 -1
  12. package/src/executor/OpenAPIOperationLocatorNormalizer.mjs +1 -1
  13. package/src/executor/StepExecutor.cjs +19 -14
  14. package/src/executor/StepExecutor.mjs +19 -14
  15. package/src/executor/WorkflowExecutor.cjs +8 -8
  16. package/src/executor/WorkflowExecutor.mjs +8 -8
  17. package/src/expression/RuntimeExpressionEvaluator.cjs +52 -9
  18. package/src/expression/RuntimeExpressionEvaluator.mjs +52 -9
  19. package/src/registry/DocumentRegistry.cjs +35 -8
  20. package/src/registry/DocumentRegistry.mjs +35 -8
  21. package/src/registry/providers/ArazzoDocumentRegistryProvider.cjs +1 -0
  22. package/src/registry/providers/ArazzoDocumentRegistryProvider.mjs +1 -0
  23. package/src/registry/providers/DocumentRegistryProvider.cjs +19 -1
  24. package/src/registry/providers/DocumentRegistryProvider.mjs +19 -1
  25. package/src/registry/providers/OpenAPIDocumentRegistryProvider.cjs +1 -0
  26. package/src/registry/providers/OpenAPIDocumentRegistryProvider.mjs +1 -0
  27. package/src/resolver/ArazzoValueResolver.cjs +6 -3
  28. package/src/resolver/ArazzoValueResolver.mjs +6 -3
  29. package/src/resolver/OpenAPIOperationParameterResolver.cjs +2 -2
  30. package/src/resolver/OpenAPIOperationParameterResolver.mjs +2 -2
  31. package/src/resolver/OutputResolver.cjs +10 -3
  32. package/src/resolver/OutputResolver.mjs +10 -3
  33. package/src/resolver/RequestBodyResolver.cjs +37 -17
  34. package/src/resolver/RequestBodyResolver.mjs +37 -17
  35. package/src/resolver/WorkflowParameterResolver.cjs +2 -2
  36. package/src/resolver/WorkflowParameterResolver.mjs +2 -2
  37. package/types/runner.d.ts +83 -36
@@ -451,7 +451,7 @@ class WorkflowExecutor {
451
451
  stepId,
452
452
  callStack: nested
453
453
  });
454
- const transferredTo = await this.#runReferencedWorkflow(transition.workflowId, stepId, invocation, scope, 'goto', this.#actionInputs(action?.parameters, outcome, invocation, stepId));
454
+ const transferredTo = await this.#runReferencedWorkflow(transition.workflowId, stepId, invocation, scope, 'goto', await this.#actionInputs(action?.parameters, outcome, invocation, stepId));
455
455
  runOutcome = {
456
456
  kind: 'transferred',
457
457
  transferredTo
@@ -515,7 +515,7 @@ class WorkflowExecutor {
515
515
  * {@link WorkflowExecutor.#validateWorkflowOutputsShape} already checked
516
516
  * the shape up front, independent of resolving it.
517
517
  */
518
- #result(invocation, workflow, steps, outcome, dependencies, startedAt) {
518
+ async #result(invocation, workflow, steps, outcome, dependencies, startedAt) {
519
519
  const shared = {
520
520
  workflowId: invocation.workflowId,
521
521
  steps,
@@ -539,7 +539,7 @@ class WorkflowExecutor {
539
539
  }
540
540
  return {
541
541
  ...shared,
542
- outputs: this.#resolveWorkflowOutputs(workflow, invocation),
542
+ outputs: await this.#resolveWorkflowOutputs(workflow, invocation),
543
543
  status: outcome.status,
544
544
  settledStatus: outcome.status
545
545
  };
@@ -680,7 +680,7 @@ class WorkflowExecutor {
680
680
  const reference = toValue(action.workflowId);
681
681
  // resolved before the budget is charged, so an authoring error in them
682
682
  // costs no budget unit.
683
- const inputs = this.#actionInputs(action.parameters, outcome, invocation, stepId);
683
+ const inputs = await this.#actionInputs(action.parameters, outcome, invocation, stepId);
684
684
  this.#chargeBudget(scope, invocation, stepId);
685
685
  const result = await this.#runReferencedWorkflow(reference, stepId, invocation, scope, 'retry', inputs);
686
686
  // the result's workflowId is the bare id the reference resolved to —
@@ -912,7 +912,7 @@ class WorkflowExecutor {
912
912
  // sees the same (usually absent) value on every attempt, not the
913
913
  // previous attempt's own result.
914
914
  const preContext = this.#inputContext(state, subWorkflowId, priorWorkflowEntry);
915
- const inputs = this.#parameterResolver.resolve(step.parameters, expression => this.#evaluate(preContext, expression, document), stepScope);
915
+ const inputs = await this.#parameterResolver.resolve(step.parameters, expression => this.#evaluate(preContext, expression, document), stepScope);
916
916
  const result = await this.#run(locator, inputs, scope, callStack, 'step');
917
917
  subWorkflows.push(result);
918
918
  state.setWorkflow(subWorkflowId, {
@@ -935,7 +935,7 @@ class WorkflowExecutor {
935
935
  workflowId: subWorkflowId
936
936
  }
937
937
  };
938
- const outputs = this.#outputResolver.resolve(step.outputs, expression => this.#evaluate(context, expression, document), stepScope);
938
+ const outputs = await this.#outputResolver.resolve(step.outputs, expression => this.#evaluate(context, expression, document), stepScope);
939
939
  // an `end`ed sub-workflow returned to its caller with outputs, so it took
940
940
  // the success path like a completed one; only a chain settling `failed` is
941
941
  // a failure — a sub-workflow that transferred is judged by where its
@@ -950,12 +950,12 @@ class WorkflowExecutor {
950
950
  // evaluated by the parent document's step executor — only the sub-run
951
951
  // itself switched documents.
952
952
  const stepExecutor = this.#stepExecutor.forDocument(document);
953
- const successCriteria = stepExecutor.evaluateCriteria(step, context, workflowId);
953
+ const successCriteria = await stepExecutor.evaluateCriteria(step, context, workflowId);
954
954
  const successful = !this.#settledFailed(result) && successCriteria.every(criterion => criterion.met);
955
955
  const {
956
956
  matchedActions,
957
957
  actionCriteria
958
- } = stepExecutor.selectActions(step, successful, context, workflowId);
958
+ } = await stepExecutor.selectActions(step, successful, context, workflowId);
959
959
  return {
960
960
  stepId,
961
961
  successful,
@@ -12,6 +12,8 @@ var _json = _interopRequireDefault(require("@swaggerexpert/json-pointer/evaluate
12
12
  var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
13
13
  var _apidomCore = require("@speclynx/apidom-core");
14
14
  var _RuntimeExpressionError = _interopRequireDefault(require("../errors/RuntimeExpressionError.cjs"));
15
+ var _DocumentLoadError = _interopRequireDefault(require("../errors/DocumentLoadError.cjs"));
16
+ var _UnmatchedProviderError = _interopRequireDefault(require("../errors/UnmatchedProviderError.cjs"));
15
17
  var _ArazzoDocument = _interopRequireDefault(require("../document/ArazzoDocument.cjs"));
16
18
  var _OpenAPIDocument = _interopRequireDefault(require("../document/OpenAPIDocument.cjs"));
17
19
  /**
@@ -55,6 +57,9 @@ const HTTP_EXCHANGE_NODES = new Set(['UrlExpression', 'MethodExpression', 'Statu
55
57
  * (via the registry) the source name points at. A step reference resolves to
56
58
  * the Step Object as authored — a locator, not the step's execution result.
57
59
  *
60
+ * Evaluation is asynchronous because a `$sourceDescriptions` reference may
61
+ * have to load the document it points at.
62
+ *
58
63
  * A reference resolves to `undefined` when it is absent. Runtime values
59
64
  * originate from JSON (or ApiDOM), which cannot represent `undefined` — an
60
65
  * absent value is a missing property and an explicit null is `null` — so
@@ -88,11 +93,19 @@ class RuntimeExpressionEvaluator {
88
93
  * The expression must be the bare form without surrounding braces
89
94
  * (e.g. `$inputs.username`, `$response.body#/pets/0/id`). In strict mode an
90
95
  * unresolvable reference throws; otherwise it returns `undefined`.
96
+ *
97
+ * A `$sourceDescriptions` reference whose document cannot be loaded is not
98
+ * merely unresolved: unless it names a field of the Source Description Object,
99
+ * it rejects with the load error in either mode, since nothing else can
100
+ * produce its value.
101
+ *
102
+ * @throws DocumentLoadError - When a referenced source document cannot be read or loaded
103
+ * @throws UnmatchedProviderError - When no provider can load a referenced source document
91
104
  */
92
- evaluate(expression) {
105
+ async evaluate(expression) {
93
106
  const node = this.#parse(expression);
94
107
  this.#assertAvailable(node, expression);
95
- const value = this.#evaluateNode(node, expression);
108
+ const value = await this.#evaluateNode(node, expression);
96
109
  if (value === undefined && this.#strict) {
97
110
  throw new _RuntimeExpressionError.default(`Runtime expression "${expression}" could not be resolved against the context`, {
98
111
  expression,
@@ -110,8 +123,20 @@ class RuntimeExpressionEvaluator {
110
123
  * mode, unresolvable references render as an empty string. A `{…}` that is not
111
124
  * a valid embedded expression (e.g. a literal JSON payload) is left unchanged.
112
125
  */
113
- interpolate(template) {
114
- return (0, _arazzoRuntimeExpression.interpolate)(template, expression => this.evaluate(expression));
126
+ async interpolate(template) {
127
+ // `interpolate` takes a synchronous resolver, so the embedded expressions are
128
+ // collected by a first pass, evaluated in order, and substituted by a second.
129
+ // Interpolation never skips an expression, so the result is the same.
130
+ const expressions = [];
131
+ (0, _arazzoRuntimeExpression.interpolate)(template, expression => {
132
+ expressions.push(expression);
133
+ return '';
134
+ });
135
+ const values = new Map();
136
+ for (const expression of expressions) {
137
+ if (!values.has(expression)) values.set(expression, await this.evaluate(expression));
138
+ }
139
+ return (0, _arazzoRuntimeExpression.interpolate)(template, expression => values.get(expression));
115
140
  }
116
141
 
117
142
  /**
@@ -125,7 +150,7 @@ class RuntimeExpressionEvaluator {
125
150
  * yields the string representation of its value, not the typed value. A
126
151
  * string containing no expressions is returned unchanged.
127
152
  */
128
- resolve(value) {
153
+ async resolve(value) {
129
154
  // `test` returns true only when the entire string is a valid runtime
130
155
  // expression, so a braced or mixed string falls through to interpolation.
131
156
  return (0, _arazzoRuntimeExpression.test)(value) ? this.evaluate(value) : this.interpolate(value);
@@ -263,17 +288,35 @@ class RuntimeExpressionEvaluator {
263
288
  * `$sourceDescriptions.{name}.url` yields the source's URL. An operationId
264
289
  * that collides with a field name takes precedence (tier 1 wins).
265
290
  *
291
+ * The referenced document is acquired only when the registry can load the
292
+ * source's `type`; a source no provider loads (e.g. AsyncAPI) resolves by
293
+ * tier 2 alone. When acquiring fails, a `{reference}` naming a field present
294
+ * on the Source Description Object still resolves to that field — it does
295
+ * not depend on the document — and any other reference rethrows the load
296
+ * error, since nothing else can produce its value.
297
+ *
266
298
  * Returns `undefined` when nothing matches.
267
299
  */
268
- #sourceDescription({
300
+ async #sourceDescription({
269
301
  sourceName,
270
302
  reference,
271
303
  stepsReference
272
304
  }) {
273
- const uri = this.#document?.resolveSourceDescriptionURI(sourceName);
305
+ const document = this.#document;
306
+ const uri = document?.resolveSourceDescriptionURI(sourceName);
307
+ const type = document?.resolveSourceDescriptionField(sourceName, 'type');
308
+ const field = document?.resolveSourceDescriptionField(sourceName, reference);
274
309
 
275
310
  // tier 1: operationId / workflowId / workflow step in the referenced document.
276
- const apiDocument = uri === undefined ? undefined : this.#registry?.get(uri);
311
+ let apiDocument;
312
+ if (uri !== undefined && this.#registry !== undefined && this.#registry.canLoad(typeof type === 'string' ? type : undefined)) {
313
+ try {
314
+ apiDocument = await this.#registry.acquire(uri);
315
+ } catch (error) {
316
+ const unloadable = error instanceof _DocumentLoadError.default || error instanceof _UnmatchedProviderError.default;
317
+ if (!unloadable || field === undefined) throw error;
318
+ }
319
+ }
277
320
  if (apiDocument !== undefined) {
278
321
  if (stepsReference !== undefined) {
279
322
  return this.#step(apiDocument, stepsReference.workflowId, stepsReference.stepId);
@@ -283,7 +326,7 @@ class RuntimeExpressionEvaluator {
283
326
  }
284
327
 
285
328
  // tier 2: a field of the Source Description Object (url, type, ...).
286
- return this.#document?.resolveSourceDescriptionField(sourceName, reference);
329
+ return field;
287
330
  }
288
331
 
289
332
  /**
@@ -5,6 +5,8 @@ import JSONEvaluationRealm from '@swaggerexpert/json-pointer/evaluate/realms/jso
5
5
  import { ApiDOMEvaluationRealm } from '@speclynx/apidom-json-pointer';
6
6
  import { toValue } from '@speclynx/apidom-core';
7
7
  import RuntimeExpressionError from "../errors/RuntimeExpressionError.mjs";
8
+ import DocumentLoadError from "../errors/DocumentLoadError.mjs";
9
+ import UnmatchedProviderError from "../errors/UnmatchedProviderError.mjs";
8
10
  import ArazzoDocument from "../document/ArazzoDocument.mjs";
9
11
  import OpenAPIDocument from "../document/OpenAPIDocument.mjs";
10
12
  /**
@@ -46,6 +48,9 @@ const HTTP_EXCHANGE_NODES = new Set(['UrlExpression', 'MethodExpression', 'Statu
46
48
  * (via the registry) the source name points at. A step reference resolves to
47
49
  * the Step Object as authored — a locator, not the step's execution result.
48
50
  *
51
+ * Evaluation is asynchronous because a `$sourceDescriptions` reference may
52
+ * have to load the document it points at.
53
+ *
49
54
  * A reference resolves to `undefined` when it is absent. Runtime values
50
55
  * originate from JSON (or ApiDOM), which cannot represent `undefined` — an
51
56
  * absent value is a missing property and an explicit null is `null` — so
@@ -79,11 +84,19 @@ class RuntimeExpressionEvaluator {
79
84
  * The expression must be the bare form without surrounding braces
80
85
  * (e.g. `$inputs.username`, `$response.body#/pets/0/id`). In strict mode an
81
86
  * unresolvable reference throws; otherwise it returns `undefined`.
87
+ *
88
+ * A `$sourceDescriptions` reference whose document cannot be loaded is not
89
+ * merely unresolved: unless it names a field of the Source Description Object,
90
+ * it rejects with the load error in either mode, since nothing else can
91
+ * produce its value.
92
+ *
93
+ * @throws DocumentLoadError - When a referenced source document cannot be read or loaded
94
+ * @throws UnmatchedProviderError - When no provider can load a referenced source document
82
95
  */
83
- evaluate(expression) {
96
+ async evaluate(expression) {
84
97
  const node = this.#parse(expression);
85
98
  this.#assertAvailable(node, expression);
86
- const value = this.#evaluateNode(node, expression);
99
+ const value = await this.#evaluateNode(node, expression);
87
100
  if (value === undefined && this.#strict) {
88
101
  throw new RuntimeExpressionError(`Runtime expression "${expression}" could not be resolved against the context`, {
89
102
  expression,
@@ -101,8 +114,20 @@ class RuntimeExpressionEvaluator {
101
114
  * mode, unresolvable references render as an empty string. A `{…}` that is not
102
115
  * a valid embedded expression (e.g. a literal JSON payload) is left unchanged.
103
116
  */
104
- interpolate(template) {
105
- return interpolate(template, expression => this.evaluate(expression));
117
+ async interpolate(template) {
118
+ // `interpolate` takes a synchronous resolver, so the embedded expressions are
119
+ // collected by a first pass, evaluated in order, and substituted by a second.
120
+ // Interpolation never skips an expression, so the result is the same.
121
+ const expressions = [];
122
+ interpolate(template, expression => {
123
+ expressions.push(expression);
124
+ return '';
125
+ });
126
+ const values = new Map();
127
+ for (const expression of expressions) {
128
+ if (!values.has(expression)) values.set(expression, await this.evaluate(expression));
129
+ }
130
+ return interpolate(template, expression => values.get(expression));
106
131
  }
107
132
 
108
133
  /**
@@ -116,7 +141,7 @@ class RuntimeExpressionEvaluator {
116
141
  * yields the string representation of its value, not the typed value. A
117
142
  * string containing no expressions is returned unchanged.
118
143
  */
119
- resolve(value) {
144
+ async resolve(value) {
120
145
  // `test` returns true only when the entire string is a valid runtime
121
146
  // expression, so a braced or mixed string falls through to interpolation.
122
147
  return test(value) ? this.evaluate(value) : this.interpolate(value);
@@ -254,17 +279,35 @@ class RuntimeExpressionEvaluator {
254
279
  * `$sourceDescriptions.{name}.url` yields the source's URL. An operationId
255
280
  * that collides with a field name takes precedence (tier 1 wins).
256
281
  *
282
+ * The referenced document is acquired only when the registry can load the
283
+ * source's `type`; a source no provider loads (e.g. AsyncAPI) resolves by
284
+ * tier 2 alone. When acquiring fails, a `{reference}` naming a field present
285
+ * on the Source Description Object still resolves to that field — it does
286
+ * not depend on the document — and any other reference rethrows the load
287
+ * error, since nothing else can produce its value.
288
+ *
257
289
  * Returns `undefined` when nothing matches.
258
290
  */
259
- #sourceDescription({
291
+ async #sourceDescription({
260
292
  sourceName,
261
293
  reference,
262
294
  stepsReference
263
295
  }) {
264
- const uri = this.#document?.resolveSourceDescriptionURI(sourceName);
296
+ const document = this.#document;
297
+ const uri = document?.resolveSourceDescriptionURI(sourceName);
298
+ const type = document?.resolveSourceDescriptionField(sourceName, 'type');
299
+ const field = document?.resolveSourceDescriptionField(sourceName, reference);
265
300
 
266
301
  // tier 1: operationId / workflowId / workflow step in the referenced document.
267
- const apiDocument = uri === undefined ? undefined : this.#registry?.get(uri);
302
+ let apiDocument;
303
+ if (uri !== undefined && this.#registry !== undefined && this.#registry.canLoad(typeof type === 'string' ? type : undefined)) {
304
+ try {
305
+ apiDocument = await this.#registry.acquire(uri);
306
+ } catch (error) {
307
+ const unloadable = error instanceof DocumentLoadError || error instanceof UnmatchedProviderError;
308
+ if (!unloadable || field === undefined) throw error;
309
+ }
310
+ }
268
311
  if (apiDocument !== undefined) {
269
312
  if (stepsReference !== undefined) {
270
313
  return this.#step(apiDocument, stepsReference.workflowId, stepsReference.stepId);
@@ -274,7 +317,7 @@ class RuntimeExpressionEvaluator {
274
317
  }
275
318
 
276
319
  // tier 2: a field of the Source Description Object (url, type, ...).
277
- return this.#document?.resolveSourceDescriptionField(sourceName, reference);
320
+ return field;
278
321
  }
279
322
 
280
323
  /**
@@ -30,6 +30,7 @@ var constants = _interopRequireWildcard(require("../constants.cjs"));
30
30
  */
31
31
  class DocumentRegistry {
32
32
  #documents = new Map();
33
+ #loading = new Map();
33
34
  #providers;
34
35
  constructor(options = {}) {
35
36
  this.#providers = options.providers ?? [new _ArazzoDocumentRegistryProvider.default(), new _OpenAPIDocumentRegistryProvider.default()];
@@ -52,6 +53,10 @@ class DocumentRegistry {
52
53
  * otherwise detects the format, loads via the matching provider,
53
54
  * caches, and returns it.
54
55
  *
56
+ * Concurrent calls for a document that is still loading share that one
57
+ * load instead of each fetching and parsing it. A failed load is not
58
+ * remembered: the next call tries again.
59
+ *
55
60
  * @throws DocumentLoadError - When the document cannot be read or loaded
56
61
  * @throws UnmatchedProviderError - When no provider can load the document
57
62
  */
@@ -59,6 +64,21 @@ class DocumentRegistry {
59
64
  const canonicalURI = this.#canonicalize(uri);
60
65
  const cachedDocument = this.#get(canonicalURI);
61
66
  if (cachedDocument) return cachedDocument;
67
+ const loading = this.#loading.get(canonicalURI);
68
+ if (loading) return loading;
69
+ const load = this.#load(canonicalURI).finally(() => {
70
+ // only its own entry: after clear(), a newer load may hold the key
71
+ if (this.#loading.get(canonicalURI) === load) this.#loading.delete(canonicalURI);
72
+ });
73
+ this.#loading.set(canonicalURI, load);
74
+ return load;
75
+ }
76
+
77
+ /**
78
+ * Loads a document through the first provider that can provide it, and
79
+ * caches it.
80
+ */
81
+ async #load(canonicalURI) {
62
82
  for (const provider of this.#providers) {
63
83
  try {
64
84
  if (await provider.canProvide(canonicalURI)) {
@@ -82,16 +102,17 @@ class DocumentRegistry {
82
102
  }
83
103
 
84
104
  /**
85
- * Returns an already-loaded document by URI, or undefined if not cached.
105
+ * Whether a source description of `type` can be loaded at all: some provider
106
+ * loads that type, or does not say which type it loads. A source with no
107
+ * `type` may be of any type, so it can.
86
108
  *
87
- * Synchronous counterpart to `acquire`: it never loads a document, only
88
- * returns one the registry already holds, marking it most recently used like
89
- * any other access. Consumers that must resolve a reference without awaiting
90
- * (e.g. runtime expression evaluation) use this; the executor is responsible
91
- * for having acquired the document beforehand.
109
+ * Answered from the providers alone, without fetching anything — so a
110
+ * caller can skip a load that could only fail, such as an AsyncAPI source
111
+ * while no provider loads AsyncAPI.
92
112
  */
93
- get(uri) {
94
- return this.#get(this.#canonicalize(uri));
113
+ canLoad(type) {
114
+ if (type === undefined) return true;
115
+ return this.#providers.some(provider => provider.sourceDescriptionType === undefined || provider.sourceDescriptionType === type);
95
116
  }
96
117
 
97
118
  /**
@@ -127,8 +148,14 @@ class DocumentRegistry {
127
148
  document.isEntry = true;
128
149
  return document;
129
150
  }
151
+
152
+ /**
153
+ * Drops every cached document. A load still in progress is no longer shared:
154
+ * the next `acquire` of that document starts a fresh load.
155
+ */
130
156
  clear() {
131
157
  this.#documents.clear();
158
+ this.#loading.clear();
132
159
  }
133
160
 
134
161
  /**
@@ -21,6 +21,7 @@ import * as constants from "../constants.mjs";
21
21
  */
22
22
  class DocumentRegistry {
23
23
  #documents = new Map();
24
+ #loading = new Map();
24
25
  #providers;
25
26
  constructor(options = {}) {
26
27
  this.#providers = options.providers ?? [new ArazzoDocumentRegistryProvider(), new OpenAPIDocumentRegistryProvider()];
@@ -43,6 +44,10 @@ class DocumentRegistry {
43
44
  * otherwise detects the format, loads via the matching provider,
44
45
  * caches, and returns it.
45
46
  *
47
+ * Concurrent calls for a document that is still loading share that one
48
+ * load instead of each fetching and parsing it. A failed load is not
49
+ * remembered: the next call tries again.
50
+ *
46
51
  * @throws DocumentLoadError - When the document cannot be read or loaded
47
52
  * @throws UnmatchedProviderError - When no provider can load the document
48
53
  */
@@ -50,6 +55,21 @@ class DocumentRegistry {
50
55
  const canonicalURI = this.#canonicalize(uri);
51
56
  const cachedDocument = this.#get(canonicalURI);
52
57
  if (cachedDocument) return cachedDocument;
58
+ const loading = this.#loading.get(canonicalURI);
59
+ if (loading) return loading;
60
+ const load = this.#load(canonicalURI).finally(() => {
61
+ // only its own entry: after clear(), a newer load may hold the key
62
+ if (this.#loading.get(canonicalURI) === load) this.#loading.delete(canonicalURI);
63
+ });
64
+ this.#loading.set(canonicalURI, load);
65
+ return load;
66
+ }
67
+
68
+ /**
69
+ * Loads a document through the first provider that can provide it, and
70
+ * caches it.
71
+ */
72
+ async #load(canonicalURI) {
53
73
  for (const provider of this.#providers) {
54
74
  try {
55
75
  if (await provider.canProvide(canonicalURI)) {
@@ -73,16 +93,17 @@ class DocumentRegistry {
73
93
  }
74
94
 
75
95
  /**
76
- * Returns an already-loaded document by URI, or undefined if not cached.
96
+ * Whether a source description of `type` can be loaded at all: some provider
97
+ * loads that type, or does not say which type it loads. A source with no
98
+ * `type` may be of any type, so it can.
77
99
  *
78
- * Synchronous counterpart to `acquire`: it never loads a document, only
79
- * returns one the registry already holds, marking it most recently used like
80
- * any other access. Consumers that must resolve a reference without awaiting
81
- * (e.g. runtime expression evaluation) use this; the executor is responsible
82
- * for having acquired the document beforehand.
100
+ * Answered from the providers alone, without fetching anything — so a
101
+ * caller can skip a load that could only fail, such as an AsyncAPI source
102
+ * while no provider loads AsyncAPI.
83
103
  */
84
- get(uri) {
85
- return this.#get(this.#canonicalize(uri));
104
+ canLoad(type) {
105
+ if (type === undefined) return true;
106
+ return this.#providers.some(provider => provider.sourceDescriptionType === undefined || provider.sourceDescriptionType === type);
86
107
  }
87
108
 
88
109
  /**
@@ -118,8 +139,14 @@ class DocumentRegistry {
118
139
  document.isEntry = true;
119
140
  return document;
120
141
  }
142
+
143
+ /**
144
+ * Drops every cached document. A load still in progress is no longer shared:
145
+ * the next `acquire` of that document starts a fresh load.
146
+ */
121
147
  clear() {
122
148
  this.#documents.clear();
149
+ this.#loading.clear();
123
150
  }
124
151
 
125
152
  /**
@@ -56,6 +56,7 @@ const providerOptionsOverride = exports.providerOptionsOverride = {
56
56
  * @public
57
57
  */
58
58
  class ArazzoDocumentRegistryProvider extends _DocumentRegistryProvider.default {
59
+ sourceDescriptionType = 'arazzo';
59
60
  #options;
60
61
  constructor(options = {}) {
61
62
  super();
@@ -48,6 +48,7 @@ export const providerOptionsOverride = {
48
48
  * @public
49
49
  */
50
50
  class ArazzoDocumentRegistryProvider extends DocumentRegistryProvider {
51
+ sourceDescriptionType = 'arazzo';
51
52
  #options;
52
53
  constructor(options = {}) {
53
54
  super();
@@ -27,5 +27,23 @@ exports.circularReplacer = circularReplacer;
27
27
  * Subclasses implement `canProvide()` for format detection and `provide()` for loading.
28
28
  * @public
29
29
  */
30
- class DocumentRegistryProvider {}
30
+ class DocumentRegistryProvider {
31
+ /**
32
+ * The Source Description Object `type` this provider loads (`openapi`,
33
+ * `arazzo`, …), or `undefined` when it does not say — then it may load a
34
+ * source of any type.
35
+ *
36
+ * Lets the registry tell, without fetching anything, whether a source of a
37
+ * given type can be loaded at all (`DocumentRegistry.canLoad`).
38
+ */
39
+ sourceDescriptionType;
40
+
41
+ /**
42
+ * Checks if the content at the URI can be handled by this provider.
43
+ */
44
+
45
+ /**
46
+ * Loads and produces a document from a URI.
47
+ */
48
+ }
31
49
  var _default = exports.default = DocumentRegistryProvider;
@@ -21,5 +21,23 @@ export const circularReplacer = ref => new ObjectElement({
21
21
  * Subclasses implement `canProvide()` for format detection and `provide()` for loading.
22
22
  * @public
23
23
  */
24
- class DocumentRegistryProvider {}
24
+ class DocumentRegistryProvider {
25
+ /**
26
+ * The Source Description Object `type` this provider loads (`openapi`,
27
+ * `arazzo`, …), or `undefined` when it does not say — then it may load a
28
+ * source of any type.
29
+ *
30
+ * Lets the registry tell, without fetching anything, whether a source of a
31
+ * given type can be loaded at all (`DocumentRegistry.canLoad`).
32
+ */
33
+ sourceDescriptionType;
34
+
35
+ /**
36
+ * Checks if the content at the URI can be handled by this provider.
37
+ */
38
+
39
+ /**
40
+ * Loads and produces a document from a URI.
41
+ */
42
+ }
25
43
  export default DocumentRegistryProvider;
@@ -48,6 +48,7 @@ const providerOptionsOverride = exports.providerOptionsOverride = {
48
48
  * @public
49
49
  */
50
50
  class OpenAPIDocumentRegistryProvider extends _DocumentRegistryProvider.default {
51
+ sourceDescriptionType = 'openapi';
51
52
  #options;
52
53
  constructor(options = {}) {
53
54
  super();
@@ -40,6 +40,7 @@ export const providerOptionsOverride = {
40
40
  * @public
41
41
  */
42
42
  class OpenAPIDocumentRegistryProvider extends DocumentRegistryProvider {
43
+ sourceDescriptionType = 'openapi';
43
44
  #options;
44
45
  constructor(options = {}) {
45
46
  super();
@@ -7,11 +7,14 @@ exports.default = void 0;
7
7
  var _apidomCore = require("@speclynx/apidom-core");
8
8
  var _arazzoRuntimeExpression = require("@swaggerexpert/arazzo-runtime-expression");
9
9
  /**
10
- * Resolves a runtime expression to its value.
10
+ * Resolves a runtime expression to its value, or to a Promise of it.
11
11
  *
12
12
  * Bridged to a lenient runtime expression evaluator
13
13
  * (`(expression) => runtimeExpressionEvaluator.evaluate(expression)`), so an
14
- * unresolvable reference yields `undefined` rather than throwing.
14
+ * unresolvable reference yields `undefined` rather than throwing; a
15
+ * `$sourceDescriptions` document that cannot be loaded still rejects with the
16
+ * load error (see `RuntimeExpressionEvaluator.evaluate`). The result
17
+ * is awaited, so a synchronous resolver works too.
15
18
  * @public
16
19
  */
17
20
 
@@ -34,7 +37,7 @@ class ArazzoValueResolver {
34
37
  /**
35
38
  * Resolves one authored value element by the rule above.
36
39
  */
37
- resolveValue(element, resolve) {
40
+ async resolveValue(element, resolve) {
38
41
  const value = (0, _apidomCore.toValue)(element);
39
42
  return typeof value === 'string' && (0, _arazzoRuntimeExpression.test)(value) ? resolve(value) : value;
40
43
  }
@@ -2,11 +2,14 @@ import { toValue } from '@speclynx/apidom-core';
2
2
  import { test as isRuntimeExpression } from '@swaggerexpert/arazzo-runtime-expression';
3
3
 
4
4
  /**
5
- * Resolves a runtime expression to its value.
5
+ * Resolves a runtime expression to its value, or to a Promise of it.
6
6
  *
7
7
  * Bridged to a lenient runtime expression evaluator
8
8
  * (`(expression) => runtimeExpressionEvaluator.evaluate(expression)`), so an
9
- * unresolvable reference yields `undefined` rather than throwing.
9
+ * unresolvable reference yields `undefined` rather than throwing; a
10
+ * `$sourceDescriptions` document that cannot be loaded still rejects with the
11
+ * load error (see `RuntimeExpressionEvaluator.evaluate`). The result
12
+ * is awaited, so a synchronous resolver works too.
10
13
  * @public
11
14
  */
12
15
 
@@ -29,7 +32,7 @@ class ArazzoValueResolver {
29
32
  /**
30
33
  * Resolves one authored value element by the rule above.
31
34
  */
32
- resolveValue(element, resolve) {
35
+ async resolveValue(element, resolve) {
33
36
  const value = toValue(element);
34
37
  return typeof value === 'string' && isRuntimeExpression(value) ? resolve(value) : value;
35
38
  }
@@ -60,7 +60,7 @@ class OpenAPIOperationParameterResolver extends _StepParameterResolver.default {
60
60
  *
61
61
  * `scope` names the step in a thrown {@link ResolverError}.
62
62
  */
63
- resolve(parameters, resolve, scope) {
63
+ async resolve(parameters, resolve, scope) {
64
64
  const delivery = new _ParameterDelivery.default();
65
65
  if (parameters === undefined) return delivery.toRecord();
66
66
  // present but scalar: walking it would fail as a bare
@@ -124,7 +124,7 @@ class OpenAPIOperationParameterResolver extends _StepParameterResolver.default {
124
124
  reason: _ResolverError.default.AmbiguousDelivery
125
125
  });
126
126
  }
127
- delivery.set(key, this.resolveValue(parameter.value, resolve));
127
+ delivery.set(key, await this.resolveValue(parameter.value, resolve));
128
128
  }
129
129
  return delivery.toRecord();
130
130
  }
@@ -53,7 +53,7 @@ class OpenAPIOperationParameterResolver extends StepParameterResolver {
53
53
  *
54
54
  * `scope` names the step in a thrown {@link ResolverError}.
55
55
  */
56
- resolve(parameters, resolve, scope) {
56
+ async resolve(parameters, resolve, scope) {
57
57
  const delivery = new ParameterDelivery();
58
58
  if (parameters === undefined) return delivery.toRecord();
59
59
  // present but scalar: walking it would fail as a bare
@@ -117,7 +117,7 @@ class OpenAPIOperationParameterResolver extends StepParameterResolver {
117
117
  reason: ResolverError.AmbiguousDelivery
118
118
  });
119
119
  }
120
- delivery.set(key, this.resolveValue(parameter.value, resolve));
120
+ delivery.set(key, await this.resolveValue(parameter.value, resolve));
121
121
  }
122
122
  return delivery.toRecord();
123
123
  }