@usearazzo/runner 1.0.1-alpha.34 → 1.0.1-alpha.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +6 -1
- package/dist/arazzo-runner.browser.js +2212 -1116
- package/dist/arazzo-runner.browser.min.js +1 -1
- package/package.json +78 -25
- package/src/errors/ExecutionError.cjs +2 -0
- package/src/errors/ExecutionError.mjs +2 -0
- package/src/executor/OpenAPIOperationExecutor.cjs +58 -47
- package/src/executor/OpenAPIOperationExecutor.mjs +58 -47
- package/src/executor/WorkflowExecutor.cjs +302 -160
- package/src/executor/WorkflowExecutor.mjs +302 -160
- package/src/index.cjs +14 -0
- package/src/index.mjs +2 -0
- package/src/observability/EventChannel.cjs +30 -20
- package/src/observability/EventChannel.mjs +30 -23
- package/src/observability/Redactor.cjs +7 -5
- package/src/observability/Redactor.mjs +7 -5
- package/src/observability/combineObservers.cjs +18 -0
- package/src/observability/combineObservers.mjs +12 -0
- package/src/observability/instrument.cjs +159 -0
- package/src/observability/instrument.mjs +144 -0
- package/src/observability/logging/LoggingObserver.cjs +177 -0
- package/src/observability/logging/LoggingObserver.mjs +172 -0
- package/src/observability/logging/index.cjs +13 -0
- package/src/observability/logging/index.mjs +1 -0
- package/src/observability/opentelemetry/ArazzoRunnerInstrumentation.cjs +321 -0
- package/src/observability/opentelemetry/ArazzoRunnerInstrumentation.mjs +313 -0
- package/src/observability/opentelemetry/index.cjs +13 -0
- package/src/observability/opentelemetry/index.mjs +1 -0
- package/src/observability/stream/EventStream.cjs +112 -0
- package/src/observability/stream/EventStream.mjs +107 -0
- package/src/observability/stream/index.cjs +13 -0
- package/src/observability/stream/index.mjs +1 -0
- package/src/registry/DocumentRegistry.cjs +14 -9
- package/src/registry/DocumentRegistry.mjs +14 -9
- package/src/vendor/swagger-client.cjs +1 -1
- package/src/vendor/swagger-client.mjs +1 -1
- package/src/version.cjs +9 -0
- package/src/version.mjs +3 -0
- package/types/observability/logging/LoggingObserver.d.cts +82 -0
- package/types/observability/logging/LoggingObserver.d.ts +82 -0
- package/types/observability/logging/index.d.cts +2 -0
- package/types/observability/logging/index.d.ts +2 -0
- package/types/observability/opentelemetry/ArazzoRunnerInstrumentation.d.cts +84 -0
- package/types/observability/opentelemetry/ArazzoRunnerInstrumentation.d.ts +84 -0
- package/types/observability/opentelemetry/index.d.cts +2 -0
- package/types/observability/opentelemetry/index.d.ts +2 -0
- package/types/observability/stream/EventStream.d.cts +68 -0
- package/types/observability/stream/EventStream.d.ts +68 -0
- package/types/observability/stream/index.d.cts +2 -0
- package/types/observability/stream/index.d.ts +2 -0
- package/types/runner.d.cts +3891 -0
- package/types/runner.d.ts +137 -40
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { context, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
2
|
+
import { toValue } from '@speclynx/apidom-core';
|
|
3
|
+
import { instrument } from '@usearazzo/runner';
|
|
4
|
+
import version from "../../version.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration of {@link ArazzoRunnerInstrumentation}, OpenTelemetry's
|
|
7
|
+
* `InstrumentationConfig`.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const NAME = '@usearazzo/runner';
|
|
11
|
+
const locationAttributes = ({
|
|
12
|
+
uri,
|
|
13
|
+
jsonPointer
|
|
14
|
+
}) => ({
|
|
15
|
+
'arazzo.document.uri': uri,
|
|
16
|
+
...(jsonPointer === undefined ? {} : {
|
|
17
|
+
'arazzo.document.json_pointer': jsonPointer
|
|
18
|
+
})
|
|
19
|
+
});
|
|
20
|
+
const spanOf = operation => operation.kind === 'workflow' ? {
|
|
21
|
+
name: `workflow ${operation.workflowId}`,
|
|
22
|
+
attributes: {
|
|
23
|
+
'arazzo.run.id': operation.runId,
|
|
24
|
+
'arazzo.workflow.id': operation.workflowId,
|
|
25
|
+
'arazzo.workflow.via': operation.via,
|
|
26
|
+
...locationAttributes(operation.location)
|
|
27
|
+
}
|
|
28
|
+
} : {
|
|
29
|
+
name: `step ${operation.stepId}`,
|
|
30
|
+
attributes: {
|
|
31
|
+
'arazzo.run.id': operation.runId,
|
|
32
|
+
'arazzo.workflow.id': operation.workflowId,
|
|
33
|
+
'arazzo.step.id': operation.stepId,
|
|
34
|
+
...locationAttributes(operation.location)
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const fail = (span, error) => {
|
|
38
|
+
if (error === undefined) {
|
|
39
|
+
span.setStatus({
|
|
40
|
+
code: SpanStatusCode.ERROR
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
span.recordException(error instanceof Error ? error : String(error));
|
|
45
|
+
span.setStatus({
|
|
46
|
+
code: SpanStatusCode.ERROR,
|
|
47
|
+
...(error instanceof Error ? {
|
|
48
|
+
message: error.message
|
|
49
|
+
} : {})
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Traces every runner in the process with OpenTelemetry, as an OpenTelemetry
|
|
55
|
+
* instrumentation: list it with the others, and no runner needs wiring.
|
|
56
|
+
*
|
|
57
|
+
* ```js
|
|
58
|
+
* import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
59
|
+
* import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
|
|
60
|
+
* import { ArazzoRunnerInstrumentation } from '@usearazzo/runner/observability/opentelemetry';
|
|
61
|
+
*
|
|
62
|
+
* new NodeSDK({
|
|
63
|
+
* instrumentations: [getNodeAutoInstrumentations(), new ArazzoRunnerInstrumentation()],
|
|
64
|
+
* }).start();
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* In the browser, with the fetch instrumentation for the request spans:
|
|
68
|
+
*
|
|
69
|
+
* ```js
|
|
70
|
+
* import { BatchSpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web';
|
|
71
|
+
* import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
72
|
+
* import { ZoneContextManager } from '@opentelemetry/context-zone';
|
|
73
|
+
* import { registerInstrumentations } from '@opentelemetry/instrumentation';
|
|
74
|
+
* import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
|
|
75
|
+
* import { ArazzoRunnerInstrumentation } from '@usearazzo/runner/observability/opentelemetry';
|
|
76
|
+
*
|
|
77
|
+
* const provider = new WebTracerProvider({
|
|
78
|
+
* spanProcessors: [new BatchSpanProcessor(new OTLPTraceExporter())],
|
|
79
|
+
* });
|
|
80
|
+
* provider.register({ contextManager: new ZoneContextManager() });
|
|
81
|
+
* registerInstrumentations({
|
|
82
|
+
* // propagateTraceHeaderCorsUrls: the cross-origin APIs that get a traceparent
|
|
83
|
+
* instrumentations: [
|
|
84
|
+
* new FetchInstrumentation({ propagateTraceHeaderCorsUrls: [/api\.example\.com/] }),
|
|
85
|
+
* new ArazzoRunnerInstrumentation(),
|
|
86
|
+
* ],
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* Each workflow invocation and each step is an `INTERNAL` span, the step's
|
|
91
|
+
* under its workflow's, a sub-workflow's and a retry reference's under the
|
|
92
|
+
* step that calls them, and a `dependsOn` workflow's or a transfer target's
|
|
93
|
+
* under the workflow that runs them. Attempts, criteria,
|
|
94
|
+
* selected actions, retry waits, retry references and the operations a step
|
|
95
|
+
* calls are events of the step's span, each named by the runner event it
|
|
96
|
+
* comes from (`step:attempt`, `criterion:evaluated`, `action:selected`, …). A step runs in its span's context, so
|
|
97
|
+
* the HTTP instrumentation of the client it sends with (undici for Node's
|
|
98
|
+
* `fetch`, `@opentelemetry/instrumentation-fetch` in the browser) nests its
|
|
99
|
+
* request spans under the step and propagates the trace into the API.
|
|
100
|
+
*
|
|
101
|
+
* In the browser, the workflow and step spans keep their tree with any
|
|
102
|
+
* context manager, but request spans nest under the step only where the
|
|
103
|
+
* context survives an `await`: zone.js loses it across a native one, so
|
|
104
|
+
* transpile `async`/`await` as OpenTelemetry's web instrumentation asks.
|
|
105
|
+
*
|
|
106
|
+
* Spans carry the identifiers and outcomes of the run, not its data: no
|
|
107
|
+
* inputs, outputs, criterion values or requests. A step or workflow that
|
|
108
|
+
* throws records the error, whose message can quote what the run saw.
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
class ArazzoRunnerInstrumentation {
|
|
112
|
+
instrumentationName = NAME;
|
|
113
|
+
instrumentationVersion = version;
|
|
114
|
+
#config;
|
|
115
|
+
#tracer;
|
|
116
|
+
#remove;
|
|
117
|
+
// the operation each span traces: an event's active span is acted on only
|
|
118
|
+
// when it is this instrumentation's own
|
|
119
|
+
#operations = new WeakMap();
|
|
120
|
+
// the transition each step span's step last selected, which tells a failure
|
|
121
|
+
// the workflow handles from one that ends it
|
|
122
|
+
#transitions = new WeakMap();
|
|
123
|
+
// the spans each run has open, outermost first: a run's operations are
|
|
124
|
+
// sequential, so the innermost is the parent of the next
|
|
125
|
+
#runs = new Map();
|
|
126
|
+
constructor(config = {}) {
|
|
127
|
+
this.#config = {
|
|
128
|
+
...config,
|
|
129
|
+
enabled: config.enabled ?? true
|
|
130
|
+
};
|
|
131
|
+
this.#tracer = trace.getTracer(this.instrumentationName, this.instrumentationVersion);
|
|
132
|
+
if (this.#config.enabled) this.enable();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Starts tracing every runner in the process. */
|
|
136
|
+
enable() {
|
|
137
|
+
this.#remove ??= instrument({
|
|
138
|
+
wrap: this.#wrap,
|
|
139
|
+
observer: this.#observe
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Stops tracing; runs in progress stop at their next workflow or step. */
|
|
144
|
+
disable() {
|
|
145
|
+
this.#remove?.();
|
|
146
|
+
this.#remove = undefined;
|
|
147
|
+
}
|
|
148
|
+
setTracerProvider(tracerProvider) {
|
|
149
|
+
this.#tracer = tracerProvider.getTracer(this.instrumentationName, this.instrumentationVersion);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Records no metrics. */
|
|
153
|
+
setMeterProvider() {}
|
|
154
|
+
setConfig(config) {
|
|
155
|
+
this.#config = {
|
|
156
|
+
...config,
|
|
157
|
+
enabled: config.enabled ?? true
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
getConfig() {
|
|
161
|
+
return this.#config;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// the span of the operation, made the active context for everything it
|
|
165
|
+
// does. Its parent is the innermost span open in its run, so the run's tree
|
|
166
|
+
// holds with a context manager that loses context across a native await
|
|
167
|
+
// (zone.js in the browser); the run's root takes the active context
|
|
168
|
+
#wrap = async (operation, run) => {
|
|
169
|
+
const parent = this.#runs.get(operation.runId)?.at(-1);
|
|
170
|
+
const parentContext = parent === undefined ? context.active() : trace.setSpan(context.active(), parent);
|
|
171
|
+
const {
|
|
172
|
+
name,
|
|
173
|
+
attributes
|
|
174
|
+
} = spanOf(operation);
|
|
175
|
+
const span = this.#tracer.startSpan(name, {
|
|
176
|
+
kind: SpanKind.INTERNAL,
|
|
177
|
+
attributes
|
|
178
|
+
}, parentContext);
|
|
179
|
+
this.#operations.set(span, operation);
|
|
180
|
+
// registered once the span exists, so a failure to start one leaves nothing behind
|
|
181
|
+
const open = this.#runs.get(operation.runId) ?? [];
|
|
182
|
+
this.#runs.set(operation.runId, open);
|
|
183
|
+
open.push(span);
|
|
184
|
+
try {
|
|
185
|
+
await context.with(trace.setSpan(parentContext, span), run);
|
|
186
|
+
} finally {
|
|
187
|
+
// by identity: an operation settling out of order must not pop another's span
|
|
188
|
+
const index = open.lastIndexOf(span);
|
|
189
|
+
if (index !== -1) open.splice(index, 1);
|
|
190
|
+
if (open.length === 0) this.#runs.delete(operation.runId);
|
|
191
|
+
span.end();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// the span of the operation of `kind` the event belongs to: the innermost
|
|
196
|
+
// open in its run, read from the run rather than the active context for the
|
|
197
|
+
// same reason as the parents above
|
|
198
|
+
#active(event, kind) {
|
|
199
|
+
const span = event.runId === undefined ? undefined : this.#runs.get(event.runId)?.at(-1);
|
|
200
|
+
return span !== undefined && this.#operations.get(span)?.kind === kind ? span : undefined;
|
|
201
|
+
}
|
|
202
|
+
#observe = event => {
|
|
203
|
+
switch (event.type) {
|
|
204
|
+
case 'workflow:settled':
|
|
205
|
+
{
|
|
206
|
+
const span = this.#active(event, 'workflow');
|
|
207
|
+
if (span === undefined) return;
|
|
208
|
+
span.setAttributes({
|
|
209
|
+
'arazzo.workflow.status': event.status,
|
|
210
|
+
'arazzo.workflow.settled_status': event.settledStatus
|
|
211
|
+
});
|
|
212
|
+
// a transferred workflow did not fail itself: its target reports its own outcome
|
|
213
|
+
if (event.status === 'failed' || event.error !== undefined) fail(span, event.error);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
case 'step:settled':
|
|
217
|
+
{
|
|
218
|
+
const span = this.#active(event, 'step');
|
|
219
|
+
if (span === undefined) return;
|
|
220
|
+
span.setAttributes({
|
|
221
|
+
'arazzo.step.attempts': event.attempts,
|
|
222
|
+
'arazzo.step.successful': event.successful
|
|
223
|
+
});
|
|
224
|
+
// an error only when it threw or its failure ends the workflow (break):
|
|
225
|
+
// a failure an onFailure action handles (goto, end) is the workflow's
|
|
226
|
+
// design, not an error
|
|
227
|
+
const unhandled = !event.successful && this.#transitions.get(span) === 'break';
|
|
228
|
+
if (unhandled || event.error !== undefined) fail(span, event.error);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
case 'step:attempt':
|
|
232
|
+
this.#addEvent(event, event.type, {
|
|
233
|
+
'arazzo.step.attempt': event.attempt
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
case 'criterion:evaluated':
|
|
237
|
+
this.#addEvent(event, event.type, {
|
|
238
|
+
'arazzo.step.attempt': event.attempt,
|
|
239
|
+
'arazzo.criterion.kind': event.kind,
|
|
240
|
+
'arazzo.criterion.condition': event.criterion.condition,
|
|
241
|
+
'arazzo.criterion.met': event.criterion.met
|
|
242
|
+
});
|
|
243
|
+
return;
|
|
244
|
+
case 'action:selected':
|
|
245
|
+
{
|
|
246
|
+
const span = this.#active(event, 'step');
|
|
247
|
+
// the step's own decision, not that of a step a retry references
|
|
248
|
+
const step = span === undefined ? undefined : this.#operations.get(span);
|
|
249
|
+
if (span !== undefined && step?.stepId === event.stepId) {
|
|
250
|
+
this.#transitions.set(span, event.transition.kind);
|
|
251
|
+
}
|
|
252
|
+
this.#addEvent(event, event.type, {
|
|
253
|
+
'arazzo.step.attempt': event.attempt,
|
|
254
|
+
'arazzo.action.transition': event.transition.kind,
|
|
255
|
+
...(event.action === undefined ? {} : {
|
|
256
|
+
'arazzo.action.name': String(toValue(event.action.name))
|
|
257
|
+
})
|
|
258
|
+
});
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
case 'retry:wait':
|
|
262
|
+
this.#addEvent(event, event.type, {
|
|
263
|
+
'arazzo.step.attempt': event.attempt,
|
|
264
|
+
'arazzo.retry.delay_ms': event.delayMs
|
|
265
|
+
});
|
|
266
|
+
return;
|
|
267
|
+
case 'retry:reference:start':
|
|
268
|
+
case 'retry:reference:settled':
|
|
269
|
+
this.#addEvent(event, event.type, {
|
|
270
|
+
'arazzo.step.attempt': event.attempt,
|
|
271
|
+
'arazzo.retry.reference.kind': event.kind,
|
|
272
|
+
'arazzo.retry.reference.id': event.id,
|
|
273
|
+
...(event.type === 'retry:reference:settled' ? {
|
|
274
|
+
'arazzo.retry.reference.successful': event.successful
|
|
275
|
+
} : {})
|
|
276
|
+
});
|
|
277
|
+
return;
|
|
278
|
+
case 'openapi:operation:assembled':
|
|
279
|
+
{
|
|
280
|
+
// an operation executor used on its own runs for no step
|
|
281
|
+
if (event.step === undefined) return;
|
|
282
|
+
const {
|
|
283
|
+
documentURI,
|
|
284
|
+
jsonPointer,
|
|
285
|
+
operationId
|
|
286
|
+
} = event.operation;
|
|
287
|
+
this.#addEvent({
|
|
288
|
+
...event.step,
|
|
289
|
+
runId: event.runId
|
|
290
|
+
}, event.type, {
|
|
291
|
+
'arazzo.operation.document_uri': documentURI,
|
|
292
|
+
'arazzo.operation.json_pointer': jsonPointer,
|
|
293
|
+
...(operationId === undefined ? {} : {
|
|
294
|
+
'arazzo.operation.id': operationId
|
|
295
|
+
})
|
|
296
|
+
});
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
default:
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// an event of the step's span. It names its step, since a retry's stepId
|
|
305
|
+
// reference reports under the referenced step within the retrying step
|
|
306
|
+
#addEvent(event, name, attributes) {
|
|
307
|
+
this.#active(event, 'step')?.addEvent(name, {
|
|
308
|
+
'arazzo.step.id': event.stepId,
|
|
309
|
+
...attributes
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
export default ArazzoRunnerInstrumentation;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "ArazzoRunnerInstrumentation", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _ArazzoRunnerInstrumentation.default;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var _ArazzoRunnerInstrumentation = _interopRequireDefault(require("./ArazzoRunnerInstrumentation.cjs"));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as ArazzoRunnerInstrumentation } from "./ArazzoRunnerInstrumentation.mjs";
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _runner = require("@usearazzo/runner");
|
|
8
|
+
/**
|
|
9
|
+
* Options for {@link eventStream}.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A run's events as a stream: wire `observer` into the runner, read the
|
|
15
|
+
* events with `for await`, or pipe `stream`, and end it with `close` or
|
|
16
|
+
* `error` once the run settles.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Queued after the last event by `error()`, so the events before it are
|
|
22
|
+
* still read.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const isFailure = chunk => 'failure' in chunk;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Turns a run's events into a stream. The caller ends it: a runner can run
|
|
29
|
+
* many times into one stream, and an `execute` that fails before its
|
|
30
|
+
* workflow starts (an unknown workflow, a document that does not load)
|
|
31
|
+
* emits no workflow event to end it on:
|
|
32
|
+
*
|
|
33
|
+
* ```js
|
|
34
|
+
* import { ArazzoRunner } from '@usearazzo/runner';
|
|
35
|
+
* import { eventStream } from '@usearazzo/runner/observability/stream';
|
|
36
|
+
*
|
|
37
|
+
* const events = eventStream();
|
|
38
|
+
* const runner = await ArazzoRunner.load(uri, { observer: events.observer });
|
|
39
|
+
* runner.execute('authenticateAndOrderPet', options).then(events.close, events.error);
|
|
40
|
+
*
|
|
41
|
+
* for await (const event of events) render(event);
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* A consumer that stops reading early (a `break` out of `for await`, or a
|
|
45
|
+
* cancelled stream) stops the queuing: the run goes on unobserved.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
const eventStream = (options = {}) => {
|
|
49
|
+
const redactor = options.redactor ?? new _runner.Redactor();
|
|
50
|
+
let controller;
|
|
51
|
+
let open = true;
|
|
52
|
+
const stream = new ReadableStream({
|
|
53
|
+
start(streamController) {
|
|
54
|
+
controller = streamController;
|
|
55
|
+
},
|
|
56
|
+
cancel() {
|
|
57
|
+
open = false;
|
|
58
|
+
}
|
|
59
|
+
}).pipeThrough(new TransformStream({
|
|
60
|
+
transform(chunk, output) {
|
|
61
|
+
if (isFailure(chunk)) output.error(chunk.failure);else output.enqueue(chunk);
|
|
62
|
+
}
|
|
63
|
+
}));
|
|
64
|
+
return {
|
|
65
|
+
observer: event => {
|
|
66
|
+
if (open) controller.enqueue(redactor.event(event));
|
|
67
|
+
},
|
|
68
|
+
stream,
|
|
69
|
+
close: () => {
|
|
70
|
+
if (!open) return;
|
|
71
|
+
open = false;
|
|
72
|
+
controller.close();
|
|
73
|
+
},
|
|
74
|
+
error: reason => {
|
|
75
|
+
if (!open) return;
|
|
76
|
+
open = false;
|
|
77
|
+
controller.enqueue({
|
|
78
|
+
failure: reason
|
|
79
|
+
});
|
|
80
|
+
controller.close();
|
|
81
|
+
},
|
|
82
|
+
// over a reader, as not every browser makes a ReadableStream async
|
|
83
|
+
// iterable (Safari); a consumer that breaks out early cancels the stream,
|
|
84
|
+
// ending the queuing
|
|
85
|
+
[Symbol.asyncIterator]: () => {
|
|
86
|
+
const reader = stream.getReader();
|
|
87
|
+
return {
|
|
88
|
+
next: async () => {
|
|
89
|
+
const {
|
|
90
|
+
done,
|
|
91
|
+
value
|
|
92
|
+
} = await reader.read();
|
|
93
|
+
return done ? {
|
|
94
|
+
done: true,
|
|
95
|
+
value: undefined
|
|
96
|
+
} : {
|
|
97
|
+
done: false,
|
|
98
|
+
value
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
return: async () => {
|
|
102
|
+
await reader.cancel();
|
|
103
|
+
return {
|
|
104
|
+
done: true,
|
|
105
|
+
value: undefined
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
var _default = exports.default = eventStream;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Redactor } from '@usearazzo/runner';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for {@link eventStream}.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A run's events as a stream: wire `observer` into the runner, read the
|
|
10
|
+
* events with `for await`, or pipe `stream`, and end it with `close` or
|
|
11
|
+
* `error` once the run settles.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Queued after the last event by `error()`, so the events before it are
|
|
17
|
+
* still read.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const isFailure = chunk => 'failure' in chunk;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Turns a run's events into a stream. The caller ends it: a runner can run
|
|
24
|
+
* many times into one stream, and an `execute` that fails before its
|
|
25
|
+
* workflow starts (an unknown workflow, a document that does not load)
|
|
26
|
+
* emits no workflow event to end it on:
|
|
27
|
+
*
|
|
28
|
+
* ```js
|
|
29
|
+
* import { ArazzoRunner } from '@usearazzo/runner';
|
|
30
|
+
* import { eventStream } from '@usearazzo/runner/observability/stream';
|
|
31
|
+
*
|
|
32
|
+
* const events = eventStream();
|
|
33
|
+
* const runner = await ArazzoRunner.load(uri, { observer: events.observer });
|
|
34
|
+
* runner.execute('authenticateAndOrderPet', options).then(events.close, events.error);
|
|
35
|
+
*
|
|
36
|
+
* for await (const event of events) render(event);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* A consumer that stops reading early (a `break` out of `for await`, or a
|
|
40
|
+
* cancelled stream) stops the queuing: the run goes on unobserved.
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
const eventStream = (options = {}) => {
|
|
44
|
+
const redactor = options.redactor ?? new Redactor();
|
|
45
|
+
let controller;
|
|
46
|
+
let open = true;
|
|
47
|
+
const stream = new ReadableStream({
|
|
48
|
+
start(streamController) {
|
|
49
|
+
controller = streamController;
|
|
50
|
+
},
|
|
51
|
+
cancel() {
|
|
52
|
+
open = false;
|
|
53
|
+
}
|
|
54
|
+
}).pipeThrough(new TransformStream({
|
|
55
|
+
transform(chunk, output) {
|
|
56
|
+
if (isFailure(chunk)) output.error(chunk.failure);else output.enqueue(chunk);
|
|
57
|
+
}
|
|
58
|
+
}));
|
|
59
|
+
return {
|
|
60
|
+
observer: event => {
|
|
61
|
+
if (open) controller.enqueue(redactor.event(event));
|
|
62
|
+
},
|
|
63
|
+
stream,
|
|
64
|
+
close: () => {
|
|
65
|
+
if (!open) return;
|
|
66
|
+
open = false;
|
|
67
|
+
controller.close();
|
|
68
|
+
},
|
|
69
|
+
error: reason => {
|
|
70
|
+
if (!open) return;
|
|
71
|
+
open = false;
|
|
72
|
+
controller.enqueue({
|
|
73
|
+
failure: reason
|
|
74
|
+
});
|
|
75
|
+
controller.close();
|
|
76
|
+
},
|
|
77
|
+
// over a reader, as not every browser makes a ReadableStream async
|
|
78
|
+
// iterable (Safari); a consumer that breaks out early cancels the stream,
|
|
79
|
+
// ending the queuing
|
|
80
|
+
[Symbol.asyncIterator]: () => {
|
|
81
|
+
const reader = stream.getReader();
|
|
82
|
+
return {
|
|
83
|
+
next: async () => {
|
|
84
|
+
const {
|
|
85
|
+
done,
|
|
86
|
+
value
|
|
87
|
+
} = await reader.read();
|
|
88
|
+
return done ? {
|
|
89
|
+
done: true,
|
|
90
|
+
value: undefined
|
|
91
|
+
} : {
|
|
92
|
+
done: false,
|
|
93
|
+
value
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
return: async () => {
|
|
97
|
+
await reader.cancel();
|
|
98
|
+
return {
|
|
99
|
+
done: true,
|
|
100
|
+
value: undefined
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
export default eventStream;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "eventStream", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _EventStream.default;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var _EventStream = _interopRequireDefault(require("./EventStream.cjs"));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as eventStream } from "./EventStream.mjs";
|
|
@@ -33,10 +33,11 @@ class DocumentRegistry {
|
|
|
33
33
|
#documents = new Map();
|
|
34
34
|
#loading = new Map();
|
|
35
35
|
#providers;
|
|
36
|
+
// emits only while `hasObservers`, so an unobserved block builds no events
|
|
36
37
|
#events;
|
|
37
38
|
constructor(options = {}) {
|
|
38
39
|
this.#providers = options.providers ?? [new _ArazzoDocumentRegistryProvider.default(), new _OpenAPIDocumentRegistryProvider.default()];
|
|
39
|
-
this.#events = _EventChannel.default
|
|
40
|
+
this.#events = new _EventChannel.default({
|
|
40
41
|
observer: options.observer,
|
|
41
42
|
now: options.now
|
|
42
43
|
});
|
|
@@ -85,10 +86,12 @@ class DocumentRegistry {
|
|
|
85
86
|
* caches it.
|
|
86
87
|
*/
|
|
87
88
|
async #load(canonicalURI) {
|
|
88
|
-
this.#events
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
if (this.#events.hasObservers) {
|
|
90
|
+
this.#events.emit({
|
|
91
|
+
type: 'document:load:start',
|
|
92
|
+
uri: canonicalURI
|
|
93
|
+
});
|
|
94
|
+
}
|
|
92
95
|
for (const provider of this.#providers) {
|
|
93
96
|
let document;
|
|
94
97
|
try {
|
|
@@ -107,10 +110,12 @@ class DocumentRegistry {
|
|
|
107
110
|
if (document !== undefined) {
|
|
108
111
|
// outside the try, so an observer's own error is not a failed load, and
|
|
109
112
|
// before caching, so a run it aborts leaves no half-registered document.
|
|
110
|
-
this.#events
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
if (this.#events.hasObservers) {
|
|
114
|
+
this.#events.emit({
|
|
115
|
+
type: 'document:load:settled',
|
|
116
|
+
uri: canonicalURI
|
|
117
|
+
});
|
|
118
|
+
}
|
|
114
119
|
this.#set(canonicalURI, document);
|
|
115
120
|
return document;
|
|
116
121
|
}
|
|
@@ -24,10 +24,11 @@ class DocumentRegistry {
|
|
|
24
24
|
#documents = new Map();
|
|
25
25
|
#loading = new Map();
|
|
26
26
|
#providers;
|
|
27
|
+
// emits only while `hasObservers`, so an unobserved block builds no events
|
|
27
28
|
#events;
|
|
28
29
|
constructor(options = {}) {
|
|
29
30
|
this.#providers = options.providers ?? [new ArazzoDocumentRegistryProvider(), new OpenAPIDocumentRegistryProvider()];
|
|
30
|
-
this.#events = EventChannel
|
|
31
|
+
this.#events = new EventChannel({
|
|
31
32
|
observer: options.observer,
|
|
32
33
|
now: options.now
|
|
33
34
|
});
|
|
@@ -76,10 +77,12 @@ class DocumentRegistry {
|
|
|
76
77
|
* caches it.
|
|
77
78
|
*/
|
|
78
79
|
async #load(canonicalURI) {
|
|
79
|
-
this.#events
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
if (this.#events.hasObservers) {
|
|
81
|
+
this.#events.emit({
|
|
82
|
+
type: 'document:load:start',
|
|
83
|
+
uri: canonicalURI
|
|
84
|
+
});
|
|
85
|
+
}
|
|
83
86
|
for (const provider of this.#providers) {
|
|
84
87
|
let document;
|
|
85
88
|
try {
|
|
@@ -98,10 +101,12 @@ class DocumentRegistry {
|
|
|
98
101
|
if (document !== undefined) {
|
|
99
102
|
// outside the try, so an observer's own error is not a failed load, and
|
|
100
103
|
// before caching, so a run it aborts leaves no half-registered document.
|
|
101
|
-
this.#events
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
if (this.#events.hasObservers) {
|
|
105
|
+
this.#events.emit({
|
|
106
|
+
type: 'document:load:settled',
|
|
107
|
+
uri: canonicalURI
|
|
108
|
+
});
|
|
109
|
+
}
|
|
105
110
|
this.#set(canonicalURI, document);
|
|
106
111
|
return document;
|
|
107
112
|
}
|