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

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 (46) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +10 -1
  3. package/dist/arazzo-runner.browser.js +1112 -231
  4. package/dist/arazzo-runner.browser.min.js +1 -1
  5. package/package.json +16 -15
  6. package/src/ArazzoRunner.cjs +26 -3
  7. package/src/ArazzoRunner.mjs +26 -3
  8. package/src/client/OpenAPIOperationResponse.cjs +9 -0
  9. package/src/client/OpenAPIOperationResponse.mjs +9 -0
  10. package/src/document/APIDocument.cjs +53 -0
  11. package/src/document/APIDocument.mjs +53 -0
  12. package/src/executor/OpenAPIOperationExecutor.cjs +66 -6
  13. package/src/executor/OpenAPIOperationExecutor.mjs +66 -6
  14. package/src/executor/StepExecutor.cjs +16 -2
  15. package/src/executor/StepExecutor.mjs +16 -2
  16. package/src/executor/StepRetryRunner.cjs +5 -3
  17. package/src/executor/StepRetryRunner.mjs +5 -3
  18. package/src/executor/StepTransitionInterpreter.cjs +1 -1
  19. package/src/executor/StepTransitionInterpreter.mjs +1 -1
  20. package/src/executor/WorkflowCallStack.cjs +1 -1
  21. package/src/executor/WorkflowCallStack.mjs +1 -1
  22. package/src/executor/WorkflowExecutor.cjs +269 -53
  23. package/src/executor/WorkflowExecutor.mjs +269 -53
  24. package/src/index.cjs +7 -0
  25. package/src/index.mjs +2 -0
  26. package/src/observability/ArazzoRunnerEvent.cjs +5 -0
  27. package/src/observability/ArazzoRunnerEvent.mjs +1 -0
  28. package/src/observability/DocumentRegistryEvent.cjs +5 -0
  29. package/src/observability/DocumentRegistryEvent.mjs +1 -0
  30. package/src/observability/EventChannel.cjs +96 -0
  31. package/src/observability/EventChannel.mjs +91 -0
  32. package/src/observability/OpenAPIOperationEvent.cjs +5 -0
  33. package/src/observability/OpenAPIOperationEvent.mjs +1 -0
  34. package/src/observability/Redactor.cjs +213 -0
  35. package/src/observability/Redactor.mjs +208 -0
  36. package/src/observability/WorkflowExecutionEvent.cjs +5 -0
  37. package/src/observability/WorkflowExecutionEvent.mjs +1 -0
  38. package/src/registry/DocumentRegistry.cjs +22 -3
  39. package/src/registry/DocumentRegistry.mjs +22 -3
  40. package/src/registry/providers/ArazzoDocumentRegistryProvider.cjs +20 -0
  41. package/src/registry/providers/ArazzoDocumentRegistryProvider.mjs +21 -1
  42. package/src/registry/providers/DocumentRegistryProvider.cjs +14 -0
  43. package/src/registry/providers/DocumentRegistryProvider.mjs +14 -0
  44. package/src/registry/providers/OpenAPIDocumentRegistryProvider.cjs +27 -5
  45. package/src/registry/providers/OpenAPIDocumentRegistryProvider.mjs +27 -5
  46. package/types/runner.d.ts +705 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usearazzo/runner",
3
- "version": "1.0.1-alpha.33",
3
+ "version": "1.0.1-alpha.34",
4
4
  "description": "Runner for Arazzo Workflows.",
5
5
  "keywords": [
6
6
  "arazzo",
@@ -56,29 +56,30 @@
56
56
  "author": "Vladimir Gorej <vladimir.gorej@gmail.com>",
57
57
  "license": "Apache-2.0",
58
58
  "dependencies": {
59
- "@babel/runtime-corejs3": "^8.1.0",
60
- "@speclynx/apidom-core": "^7.0.2",
61
- "@speclynx/apidom-datamodel": "^7.0.2",
62
- "@speclynx/apidom-error": "^7.0.2",
63
- "@speclynx/apidom-json-pointer": "^7.0.2",
64
- "@speclynx/apidom-ns-arazzo-1": "^7.0.2",
65
- "@speclynx/apidom-ns-openapi-2": "^7.0.2",
66
- "@speclynx/apidom-ns-openapi-3-0": "^7.0.2",
67
- "@speclynx/apidom-ns-openapi-3-1": "^7.0.2",
68
- "@speclynx/apidom-reference": "^7.0.2",
69
- "@speclynx/apidom-traverse": "^7.0.2",
59
+ "@babel/runtime-corejs3": "^8.1.2",
60
+ "@speclynx/apidom-core": "^7.1.0",
61
+ "@speclynx/apidom-datamodel": "^7.1.0",
62
+ "@speclynx/apidom-error": "^7.1.0",
63
+ "@speclynx/apidom-json-pointer": "^7.1.0",
64
+ "@speclynx/apidom-ns-arazzo-1": "^7.1.0",
65
+ "@speclynx/apidom-ns-openapi-2": "^7.1.0",
66
+ "@speclynx/apidom-ns-openapi-3-0": "^7.1.0",
67
+ "@speclynx/apidom-ns-openapi-3-1": "^7.1.0",
68
+ "@speclynx/apidom-reference": "^7.1.0",
69
+ "@speclynx/apidom-traverse": "^7.1.0",
70
70
  "@swaggerexpert/arazzo-criterion": "^1.2.0",
71
71
  "@swaggerexpert/arazzo-runtime-expression": "^3.4.0",
72
72
  "@swaggerexpert/cookie": "^2.1.1",
73
73
  "@swaggerexpert/json-pointer": "^3.0.2",
74
74
  "@swaggerexpert/jsonpath": "^4.0.5",
75
75
  "@swaggerexpert/media-types": "^1.0.2",
76
- "@usearazzo/parser": "1.0.1-alpha.33",
77
- "@usearazzo/resolver": "1.0.1-alpha.33",
76
+ "@usearazzo/parser": "1.0.1-alpha.34",
77
+ "@usearazzo/resolver": "1.0.1-alpha.34",
78
78
  "@xmldom/xmldom": "^0.9.12",
79
79
  "fontoxpath": "^3.34.0",
80
80
  "ramda": "^0.32.0",
81
81
  "ramda-adjunct": "^6.1.0",
82
+ "short-unique-id": "^5.3.2",
82
83
  "type-fest": "^5.10.0"
83
84
  },
84
85
  "files": [
@@ -94,5 +95,5 @@
94
95
  "devDependencies": {
95
96
  "swagger-client": "^3.39.0"
96
97
  },
97
- "gitHead": "a41d6c5821d21691caa11160e125f89f860f5511"
98
+ "gitHead": "38ba1b2907b4af68cf4cf615ea8031b93fed60e6"
98
99
  }
@@ -32,8 +32,9 @@ var _InvalidEntryDocumentError = _interopRequireDefault(require("./errors/Invali
32
32
  * Every option is a seam of a building block the runner wires: `registry` is
33
33
  * the {@link DocumentRegistry} the documents load into (a fresh one with the
34
34
  * default providers when omitted), `reference` goes to everything the runner
35
- * builds that retrieves documents, `httpClient` and `requestInterceptor` go to
36
- * the {@link OpenAPIOperationExecutor}, and the rest go to the
35
+ * builds that retrieves documents, `httpClient`, `requestInterceptor` and
36
+ * `captureBodies` go to the {@link OpenAPIOperationExecutor}, and the rest go
37
+ * to the
37
38
  * {@link WorkflowExecutor}.
38
39
  *
39
40
  * Retrieving documents and calling APIs are separate on purpose: `reference`
@@ -74,10 +75,24 @@ class ArazzoRunner {
74
75
  static async load(uri, options = {}) {
75
76
  // `resolve` only, whatever else the given object carries
76
77
  const referenceOptions = (0, _ramda.pick)(['resolve'], options.reference ?? {});
78
+ // source maps need the non-strict parsers, which are the ones that keep
79
+ // positions
80
+ const providerOptions = options.sourceMap ? {
81
+ ...referenceOptions,
82
+ parse: {
83
+ parserOpts: {
84
+ sourceMap: true,
85
+ strict: false
86
+ }
87
+ }
88
+ } : referenceOptions;
77
89
  const {
78
90
  registry = new _DocumentRegistry.default({
79
- providers: [new _ArazzoDocumentRegistryProvider.default(referenceOptions), new _OpenAPIDocumentRegistryProvider.default(referenceOptions)]
91
+ providers: [new _ArazzoDocumentRegistryProvider.default(providerOptions), new _OpenAPIDocumentRegistryProvider.default(providerOptions)],
92
+ observer: options.observer,
93
+ now: options.now
80
94
  }),
95
+ sourceMap: _sourceMap,
81
96
  ...rest
82
97
  } = options;
83
98
  // acquiring a second entry would pin it for good and, through the LRU
@@ -109,6 +124,7 @@ class ArazzoRunner {
109
124
  reference,
110
125
  httpClient,
111
126
  requestInterceptor,
127
+ captureBodies,
112
128
  ...workflowExecutorOptions
113
129
  } = options;
114
130
 
@@ -122,9 +138,16 @@ class ArazzoRunner {
122
138
 
123
139
  // `resolve` only, whatever else the given object carries
124
140
  const referenceOptions = (0, _ramda.pick)(['resolve'], reference ?? {});
141
+ const {
142
+ observer,
143
+ now
144
+ } = workflowExecutorOptions;
125
145
  const operationExecutor = new _OpenAPIOperationExecutor.default({
126
146
  httpClient,
127
147
  requestInterceptor,
148
+ captureBodies,
149
+ observer,
150
+ now,
128
151
  operationNormalizer: new _OpenAPIOperationNormalizer.default({
129
152
  openapi2: new _OpenAPI2OperationNormalizer.default(referenceOptions),
130
153
  openapi30: new _OpenAPI30OperationNormalizer.default(referenceOptions),
@@ -24,8 +24,9 @@ import InvalidEntryDocumentError from "./errors/InvalidEntryDocumentError.mjs";
24
24
  * Every option is a seam of a building block the runner wires: `registry` is
25
25
  * the {@link DocumentRegistry} the documents load into (a fresh one with the
26
26
  * default providers when omitted), `reference` goes to everything the runner
27
- * builds that retrieves documents, `httpClient` and `requestInterceptor` go to
28
- * the {@link OpenAPIOperationExecutor}, and the rest go to the
27
+ * builds that retrieves documents, `httpClient`, `requestInterceptor` and
28
+ * `captureBodies` go to the {@link OpenAPIOperationExecutor}, and the rest go
29
+ * to the
29
30
  * {@link WorkflowExecutor}.
30
31
  *
31
32
  * Retrieving documents and calling APIs are separate on purpose: `reference`
@@ -64,10 +65,24 @@ class ArazzoRunner {
64
65
  static async load(uri, options = {}) {
65
66
  // `resolve` only, whatever else the given object carries
66
67
  const referenceOptions = pick(['resolve'], options.reference ?? {});
68
+ // source maps need the non-strict parsers, which are the ones that keep
69
+ // positions
70
+ const providerOptions = options.sourceMap ? {
71
+ ...referenceOptions,
72
+ parse: {
73
+ parserOpts: {
74
+ sourceMap: true,
75
+ strict: false
76
+ }
77
+ }
78
+ } : referenceOptions;
67
79
  const {
68
80
  registry = new DocumentRegistry({
69
- providers: [new ArazzoDocumentRegistryProvider(referenceOptions), new OpenAPIDocumentRegistryProvider(referenceOptions)]
81
+ providers: [new ArazzoDocumentRegistryProvider(providerOptions), new OpenAPIDocumentRegistryProvider(providerOptions)],
82
+ observer: options.observer,
83
+ now: options.now
70
84
  }),
85
+ sourceMap: _sourceMap,
71
86
  ...rest
72
87
  } = options;
73
88
  // acquiring a second entry would pin it for good and, through the LRU
@@ -99,6 +114,7 @@ class ArazzoRunner {
99
114
  reference,
100
115
  httpClient,
101
116
  requestInterceptor,
117
+ captureBodies,
102
118
  ...workflowExecutorOptions
103
119
  } = options;
104
120
 
@@ -112,9 +128,16 @@ class ArazzoRunner {
112
128
 
113
129
  // `resolve` only, whatever else the given object carries
114
130
  const referenceOptions = pick(['resolve'], reference ?? {});
131
+ const {
132
+ observer,
133
+ now
134
+ } = workflowExecutorOptions;
115
135
  const operationExecutor = new OpenAPIOperationExecutor({
116
136
  httpClient,
117
137
  requestInterceptor,
138
+ captureBodies,
139
+ observer,
140
+ now,
118
141
  operationNormalizer: new OpenAPIOperationNormalizer({
119
142
  openapi2: new OpenAPI2OperationNormalizer(referenceOptions),
120
143
  openapi30: new OpenAPI30OperationNormalizer(referenceOptions),
@@ -47,6 +47,15 @@ class OpenAPIOperationResponse {
47
47
  this.request = raw.request;
48
48
  }
49
49
 
50
+ /**
51
+ * The body's size in bytes, as read: after the transport decoded any
52
+ * `Content-Encoding`, so a compressed body counts at its decoded size.
53
+ */
54
+ get size() {
55
+ // a string content encodes back to exactly the bytes received
56
+ return typeof this.#content === 'string' ? new TextEncoder().encode(this.#content).length : this.#content.length;
57
+ }
58
+
50
59
  /**
51
60
  * The body as text, like `Response.text()`; `''` when there is none.
52
61
  */
@@ -41,6 +41,15 @@ class OpenAPIOperationResponse {
41
41
  this.request = raw.request;
42
42
  }
43
43
 
44
+ /**
45
+ * The body's size in bytes, as read: after the transport decoded any
46
+ * `Content-Encoding`, so a compressed body counts at its decoded size.
47
+ */
48
+ get size() {
49
+ // a string content encodes back to exactly the bytes received
50
+ return typeof this.#content === 'string' ? new TextEncoder().encode(this.#content).length : this.#content.length;
51
+ }
52
+
44
53
  /**
45
54
  * The body as text, like `Response.text()`; `''` when there is none.
46
55
  */
@@ -5,6 +5,23 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _apidomCore = require("@speclynx/apidom-core");
8
+ /**
9
+ * A position in a document, as in the Language Server Protocol: a 0-based
10
+ * line, and a 0-based character offset within it in UTF-16 code units.
11
+ * @public
12
+ */
13
+
14
+ /**
15
+ * A range in a document, as in the Language Server Protocol: from `start` to
16
+ * `end`, the shape the validator's diagnostics use.
17
+ * @public
18
+ */
19
+
20
+ /**
21
+ * Where an element is written.
22
+ * @public
23
+ */
24
+
8
25
  /**
9
26
  * Base document held by the document registry.
10
27
  * @public
@@ -17,6 +34,42 @@ class APIDocument {
17
34
  this.parseResult = parseResult;
18
35
  }
19
36
 
37
+ /**
38
+ * Where an element of this document, or one it references, is written, as
39
+ * the registry provider recorded it when it loaded the document: the
40
+ * document it came from (its `ref-origin`) when it came through a
41
+ * reference, and its JSON Pointer there.
42
+ */
43
+ locationOf(element) {
44
+ const origin = (0, _apidomCore.toValue)(element.meta.get('ref-origin'));
45
+ const jsonPointer = (0, _apidomCore.toValue)(element.meta.get('json-pointer'));
46
+ const {
47
+ startLine,
48
+ startCharacter,
49
+ endLine,
50
+ endCharacter
51
+ } = element;
52
+ return {
53
+ uri: typeof origin === 'string' ? origin : this.uri,
54
+ ...(typeof jsonPointer === 'string' && {
55
+ jsonPointer
56
+ }),
57
+ // a source-mapped parse positions every element fully, any other none
58
+ ...(startLine !== undefined && {
59
+ range: {
60
+ start: {
61
+ line: startLine,
62
+ character: startCharacter
63
+ },
64
+ end: {
65
+ line: endLine,
66
+ character: endCharacter
67
+ }
68
+ }
69
+ })
70
+ };
71
+ }
72
+
20
73
  /**
21
74
  * Returns a JSON-serializable representation of the document's API element.
22
75
  * Compatible with JSON.stringify() — JSON.stringify(doc) produces correct output.
@@ -1,5 +1,22 @@
1
1
  import { toValue, toYAML } from '@speclynx/apidom-core';
2
2
 
3
+ /**
4
+ * A position in a document, as in the Language Server Protocol: a 0-based
5
+ * line, and a 0-based character offset within it in UTF-16 code units.
6
+ * @public
7
+ */
8
+
9
+ /**
10
+ * A range in a document, as in the Language Server Protocol: from `start` to
11
+ * `end`, the shape the validator's diagnostics use.
12
+ * @public
13
+ */
14
+
15
+ /**
16
+ * Where an element is written.
17
+ * @public
18
+ */
19
+
3
20
  /**
4
21
  * Base document held by the document registry.
5
22
  * @public
@@ -12,6 +29,42 @@ class APIDocument {
12
29
  this.parseResult = parseResult;
13
30
  }
14
31
 
32
+ /**
33
+ * Where an element of this document, or one it references, is written, as
34
+ * the registry provider recorded it when it loaded the document: the
35
+ * document it came from (its `ref-origin`) when it came through a
36
+ * reference, and its JSON Pointer there.
37
+ */
38
+ locationOf(element) {
39
+ const origin = toValue(element.meta.get('ref-origin'));
40
+ const jsonPointer = toValue(element.meta.get('json-pointer'));
41
+ const {
42
+ startLine,
43
+ startCharacter,
44
+ endLine,
45
+ endCharacter
46
+ } = element;
47
+ return {
48
+ uri: typeof origin === 'string' ? origin : this.uri,
49
+ ...(typeof jsonPointer === 'string' && {
50
+ jsonPointer
51
+ }),
52
+ // a source-mapped parse positions every element fully, any other none
53
+ ...(startLine !== undefined && {
54
+ range: {
55
+ start: {
56
+ line: startLine,
57
+ character: startCharacter
58
+ },
59
+ end: {
60
+ line: endLine,
61
+ character: endCharacter
62
+ }
63
+ }
64
+ })
65
+ };
66
+ }
67
+
15
68
  /**
16
69
  * Returns a JSON-serializable representation of the document's API element.
17
70
  * Compatible with JSON.stringify() — JSON.stringify(doc) produces correct output.
@@ -21,6 +21,7 @@ var _OpenAPIOperationExtractor = _interopRequireDefault(require("../extractor/Op
21
21
  var _OpenAPIOperationNormalizer = _interopRequireDefault(require("../normalizer/OpenAPIOperationNormalizer.cjs"));
22
22
  var _OpenAPIDocumentAssembler = _interopRequireDefault(require("../assembler/OpenAPIDocumentAssembler.cjs"));
23
23
  var _ClientError = _interopRequireDefault(require("../errors/ClientError.cjs"));
24
+ var _EventChannel = _interopRequireDefault(require("../observability/EventChannel.cjs"));
24
25
  // @ts-expect-error vendored swagger-client bundle has no type declarations
25
26
 
26
27
  /**
@@ -169,6 +170,8 @@ class OpenAPIOperationExecutor {
169
170
  #operationExtractor;
170
171
  #operationNormalizer;
171
172
  #documentAssembler;
173
+ #captureBodies;
174
+ #events;
172
175
  constructor(options = {}) {
173
176
  this.#httpClient = options.httpClient ?? _HTTPClientFetch.default;
174
177
  this.#requestInterceptor = options.requestInterceptor;
@@ -176,6 +179,11 @@ class OpenAPIOperationExecutor {
176
179
  this.#operationExtractor = options.operationExtractor ?? new _OpenAPIOperationExtractor.default();
177
180
  this.#operationNormalizer = options.operationNormalizer ?? new _OpenAPIOperationNormalizer.default();
178
181
  this.#documentAssembler = options.documentAssembler ?? new _OpenAPIDocumentAssembler.default();
182
+ this.#events = _EventChannel.default.open({
183
+ observer: options.observer,
184
+ now: options.now
185
+ });
186
+ this.#captureBodies = options.captureBodies ?? false;
179
187
  }
180
188
 
181
189
  /**
@@ -196,17 +204,19 @@ class OpenAPIOperationExecutor {
196
204
  // the legacy id (`{method}-{path}`) matches regardless of an explicit
197
205
  // `operationId` in the assembled document
198
206
  const operationId = (0, _swaggerClient.idFromPathMethodLegacy)((0, _apidomCore.toValue)(operation.meta.get('path')), (0, _apidomCore.toValue)(operation.meta.get('http-method')));
199
- const normalizedOperation = await this.#operationNormalizer.normalize(operation, locator.document);
200
- const assembled = this.#documentAssembler.assemble(normalizedOperation, locator.document);
201
-
202
207
  // the interceptor and step are consumed here, the target always comes from
203
208
  // the locator, and the transport is the httpClient, so none of these
204
209
  // reach buildRequest
205
210
  const {
206
211
  requestInterceptor,
207
- step
212
+ step,
213
+ runId
208
214
  } = executeOptions;
209
- const rest = (0, _ramda.omit)(['requestInterceptor', 'step', 'operationId', 'operationPath', 'userFetch', 'responseInterceptor'], executeOptions);
215
+ // the operation as extracted from its own document, before normalizing
216
+ // copies it, carries where it is written; located once, and only for an
217
+ // observer.
218
+ let located;
219
+ const location = () => located ??= locator.document.locationOf(operation);
210
220
  const declaredOperationId = (0, _apidomCore.toValue)(operation.operationId);
211
221
  const context = {
212
222
  operation: {
@@ -229,6 +239,27 @@ class OpenAPIOperationExecutor {
229
239
  }
230
240
  })
231
241
  };
242
+ this.#events?.emit({
243
+ type: 'openapi-operation-normalize-start',
244
+ ...context,
245
+ runId,
246
+ location: location()
247
+ });
248
+ const normalizedOperation = await this.#operationNormalizer.normalize(operation, locator.document);
249
+ this.#events?.emit({
250
+ type: 'openapi-operation-normalize-settled',
251
+ ...context,
252
+ runId,
253
+ location: location()
254
+ });
255
+ const assembled = this.#documentAssembler.assemble(normalizedOperation, locator.document);
256
+ this.#events?.emit({
257
+ type: 'openapi-operation-assembled',
258
+ ...context,
259
+ runId,
260
+ location: location()
261
+ });
262
+ const rest = (0, _ramda.omit)(['requestInterceptor', 'step', 'runId', 'operationId', 'operationPath', 'userFetch', 'responseInterceptor'], executeOptions);
232
263
 
233
264
  // each step derives the next options rather than editing in place
234
265
  const withServer = this.#applyServerOptions(rest, normalizedOperation, locator.document);
@@ -272,6 +303,20 @@ class OpenAPIOperationExecutor {
272
303
  },
273
304
  body: request.body
274
305
  };
306
+ this.#events?.emit({
307
+ type: 'openapi-operation-request-start',
308
+ ...context,
309
+ runId,
310
+ location: location(),
311
+ request: {
312
+ method: request.method,
313
+ url: request.url,
314
+ headers: request.headers,
315
+ ...(this.#captureBodies ? {
316
+ body: request.body
317
+ } : {})
318
+ }
319
+ });
275
320
  let rawResponse;
276
321
  try {
277
322
  rawResponse = await this.#httpClient(request);
@@ -281,7 +326,22 @@ class OpenAPIOperationExecutor {
281
326
  cause: error
282
327
  });
283
328
  }
284
- return this.#responseNormalizer.normalize(rawResponse, requestInfo);
329
+ const response = await this.#responseNormalizer.normalize(rawResponse, requestInfo);
330
+ this.#events?.emit({
331
+ type: 'openapi-operation-request-settled',
332
+ ...context,
333
+ runId,
334
+ location: location(),
335
+ response: {
336
+ status: response.status,
337
+ headers: response.headers,
338
+ size: response.size,
339
+ ...(this.#captureBodies ? {
340
+ body: response.body
341
+ } : {})
342
+ }
343
+ });
344
+ return response;
285
345
  }
286
346
 
287
347
  /**
@@ -16,6 +16,7 @@ import OpenAPIOperationExtractor from "../extractor/OpenAPIOperationExtractor.mj
16
16
  import OpenAPIOperationNormalizer from "../normalizer/OpenAPIOperationNormalizer.mjs";
17
17
  import OpenAPIDocumentAssembler from "../assembler/OpenAPIDocumentAssembler.mjs";
18
18
  import ClientError from "../errors/ClientError.mjs";
19
+ import EventChannel from "../observability/EventChannel.mjs";
19
20
  /**
20
21
  * The Arazzo step a request is issued for: its `stepId`, and the `workflowId`
21
22
  * of the workflow it belongs to when the caller names one (step ids are only
@@ -157,6 +158,8 @@ class OpenAPIOperationExecutor {
157
158
  #operationExtractor;
158
159
  #operationNormalizer;
159
160
  #documentAssembler;
161
+ #captureBodies;
162
+ #events;
160
163
  constructor(options = {}) {
161
164
  this.#httpClient = options.httpClient ?? httpClientFetch;
162
165
  this.#requestInterceptor = options.requestInterceptor;
@@ -164,6 +167,11 @@ class OpenAPIOperationExecutor {
164
167
  this.#operationExtractor = options.operationExtractor ?? new OpenAPIOperationExtractor();
165
168
  this.#operationNormalizer = options.operationNormalizer ?? new OpenAPIOperationNormalizer();
166
169
  this.#documentAssembler = options.documentAssembler ?? new OpenAPIDocumentAssembler();
170
+ this.#events = EventChannel.open({
171
+ observer: options.observer,
172
+ now: options.now
173
+ });
174
+ this.#captureBodies = options.captureBodies ?? false;
167
175
  }
168
176
 
169
177
  /**
@@ -184,17 +192,19 @@ class OpenAPIOperationExecutor {
184
192
  // the legacy id (`{method}-{path}`) matches regardless of an explicit
185
193
  // `operationId` in the assembled document
186
194
  const operationId = idFromPathMethodLegacy(toValue(operation.meta.get('path')), toValue(operation.meta.get('http-method')));
187
- const normalizedOperation = await this.#operationNormalizer.normalize(operation, locator.document);
188
- const assembled = this.#documentAssembler.assemble(normalizedOperation, locator.document);
189
-
190
195
  // the interceptor and step are consumed here, the target always comes from
191
196
  // the locator, and the transport is the httpClient, so none of these
192
197
  // reach buildRequest
193
198
  const {
194
199
  requestInterceptor,
195
- step
200
+ step,
201
+ runId
196
202
  } = executeOptions;
197
- const rest = omit(['requestInterceptor', 'step', 'operationId', 'operationPath', 'userFetch', 'responseInterceptor'], executeOptions);
203
+ // the operation as extracted from its own document, before normalizing
204
+ // copies it, carries where it is written; located once, and only for an
205
+ // observer.
206
+ let located;
207
+ const location = () => located ??= locator.document.locationOf(operation);
198
208
  const declaredOperationId = toValue(operation.operationId);
199
209
  const context = {
200
210
  operation: {
@@ -217,6 +227,27 @@ class OpenAPIOperationExecutor {
217
227
  }
218
228
  })
219
229
  };
230
+ this.#events?.emit({
231
+ type: 'openapi-operation-normalize-start',
232
+ ...context,
233
+ runId,
234
+ location: location()
235
+ });
236
+ const normalizedOperation = await this.#operationNormalizer.normalize(operation, locator.document);
237
+ this.#events?.emit({
238
+ type: 'openapi-operation-normalize-settled',
239
+ ...context,
240
+ runId,
241
+ location: location()
242
+ });
243
+ const assembled = this.#documentAssembler.assemble(normalizedOperation, locator.document);
244
+ this.#events?.emit({
245
+ type: 'openapi-operation-assembled',
246
+ ...context,
247
+ runId,
248
+ location: location()
249
+ });
250
+ const rest = omit(['requestInterceptor', 'step', 'runId', 'operationId', 'operationPath', 'userFetch', 'responseInterceptor'], executeOptions);
220
251
 
221
252
  // each step derives the next options rather than editing in place
222
253
  const withServer = this.#applyServerOptions(rest, normalizedOperation, locator.document);
@@ -260,6 +291,20 @@ class OpenAPIOperationExecutor {
260
291
  },
261
292
  body: request.body
262
293
  };
294
+ this.#events?.emit({
295
+ type: 'openapi-operation-request-start',
296
+ ...context,
297
+ runId,
298
+ location: location(),
299
+ request: {
300
+ method: request.method,
301
+ url: request.url,
302
+ headers: request.headers,
303
+ ...(this.#captureBodies ? {
304
+ body: request.body
305
+ } : {})
306
+ }
307
+ });
263
308
  let rawResponse;
264
309
  try {
265
310
  rawResponse = await this.#httpClient(request);
@@ -269,7 +314,22 @@ class OpenAPIOperationExecutor {
269
314
  cause: error
270
315
  });
271
316
  }
272
- return this.#responseNormalizer.normalize(rawResponse, requestInfo);
317
+ const response = await this.#responseNormalizer.normalize(rawResponse, requestInfo);
318
+ this.#events?.emit({
319
+ type: 'openapi-operation-request-settled',
320
+ ...context,
321
+ runId,
322
+ location: location(),
323
+ response: {
324
+ status: response.status,
325
+ headers: response.headers,
326
+ size: response.size,
327
+ ...(this.#captureBodies ? {
328
+ body: response.body
329
+ } : {})
330
+ }
331
+ });
332
+ return response;
273
333
  }
274
334
 
275
335
  /**
@@ -417,18 +417,30 @@ class StepExecutor {
417
417
  */
418
418
  async #evaluateCriterion(criterion, context) {
419
419
  const condition = (0, _apidomCore.toValue)(criterion.condition);
420
+ const location = this.#document.locationOf(criterion);
421
+ const values = {};
422
+ const resolve = async expression => {
423
+ const value = await this.#evaluate(context, expression);
424
+ const wholeBody = expression === '$response.body' || expression === '$request.body';
425
+ values[expression] = wholeBody ? '[body]' : value;
426
+ return value;
427
+ };
420
428
  try {
421
429
  const {
422
430
  met,
423
431
  error
424
- } = await this.#criterionEvaluator.evaluate(criterion, expression => this.#evaluate(context, expression));
432
+ } = await this.#criterionEvaluator.evaluate(criterion, resolve);
425
433
  if (error === undefined) return {
426
434
  condition,
427
- met
435
+ met,
436
+ values,
437
+ location
428
438
  };
429
439
  return {
430
440
  condition,
431
441
  met,
442
+ values,
443
+ location,
432
444
  diagnostic: {
433
445
  code: error.reason,
434
446
  message: error.message
@@ -442,6 +454,8 @@ class StepExecutor {
442
454
  return {
443
455
  condition,
444
456
  met: false,
457
+ values,
458
+ location,
445
459
  diagnostic: {
446
460
  code: error.reason,
447
461
  message: error.message
@@ -410,18 +410,30 @@ class StepExecutor {
410
410
  */
411
411
  async #evaluateCriterion(criterion, context) {
412
412
  const condition = toValue(criterion.condition);
413
+ const location = this.#document.locationOf(criterion);
414
+ const values = {};
415
+ const resolve = async expression => {
416
+ const value = await this.#evaluate(context, expression);
417
+ const wholeBody = expression === '$response.body' || expression === '$request.body';
418
+ values[expression] = wholeBody ? '[body]' : value;
419
+ return value;
420
+ };
413
421
  try {
414
422
  const {
415
423
  met,
416
424
  error
417
- } = await this.#criterionEvaluator.evaluate(criterion, expression => this.#evaluate(context, expression));
425
+ } = await this.#criterionEvaluator.evaluate(criterion, resolve);
418
426
  if (error === undefined) return {
419
427
  condition,
420
- met
428
+ met,
429
+ values,
430
+ location
421
431
  };
422
432
  return {
423
433
  condition,
424
434
  met,
435
+ values,
436
+ location,
425
437
  diagnostic: {
426
438
  code: error.reason,
427
439
  message: error.message
@@ -435,6 +447,8 @@ class StepExecutor {
435
447
  return {
436
448
  condition,
437
449
  met: false,
450
+ values,
451
+ location,
438
452
  diagnostic: {
439
453
  code: error.reason,
440
454
  message: error.message