@camunda8/sdk 8.9.0-alpha.16 → 8.9.0-alpha.17

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.
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.describeStreamState = exports.STREAM_HANDLE_BRAND = exports.backoffDelayMs = exports.getOpenError = exports.toError = exports.cancelStream = exports.asStream = void 0;
4
+ exports.attachStream = attachStream;
5
+ exports.createStreamStateMachine = createStreamStateMachine;
6
+ exports.createStreamHandle = createStreamHandle;
7
+ /**
8
+ * Narrow the value that a stream-opening call resolved to (after
9
+ * `getOpenError` found no error) to a stream. Lives here so that stream workers
10
+ * never need to name, and therefore never hold a typed reference to, the raw
11
+ * gRPC stream type.
12
+ */
13
+ const asStream = (opened) => opened;
14
+ exports.asStream = asStream;
15
+ /**
16
+ * Cancel and destroy a raw stream. Also used for a stream that was never
17
+ * attached (for example one that finished opening after its handle was
18
+ * closed): cancelling reports CANCELLED as an `error` event, which with no
19
+ * listener would be an uncaught exception, so a listener for that expected
20
+ * error is added first.
21
+ */
22
+ const cancelStream = (stream) => {
23
+ stream.on('error', () => undefined);
24
+ stream.cancel();
25
+ stream.destroy();
26
+ };
27
+ exports.cancelStream = cancelStream;
28
+ const toError = (e) => e instanceof Error ? e : new Error(String(e));
29
+ exports.toError = toError;
30
+ /**
31
+ * Attach the lifecycle handlers to a raw gRPC stream. The only place in the
32
+ * stream modules that calls `.on(...)` on a stream.
33
+ *
34
+ * `error`, `end` and `close` events can all fire for one dead stream; the first
35
+ * one decides which handler runs, and the `close` handler runs exactly once.
36
+ */
37
+ function attachStream(stream, handlers) {
38
+ let settled = false;
39
+ const settle = (deliverCause) => {
40
+ if (settled) {
41
+ return;
42
+ }
43
+ settled = true;
44
+ try {
45
+ deliverCause();
46
+ }
47
+ finally {
48
+ handlers.close();
49
+ }
50
+ };
51
+ stream.on('data', (item) => {
52
+ if (settled) {
53
+ return;
54
+ }
55
+ try {
56
+ handlers.data(item);
57
+ }
58
+ catch (e) {
59
+ // A throwing data handler is a bug, but it must not be silent and it
60
+ // must not leave a stream running that nobody is processing.
61
+ (0, exports.cancelStream)(stream);
62
+ settle(() => handlers.error((0, exports.toError)(e)));
63
+ }
64
+ });
65
+ stream.on('error', (e) => settle(() => handlers.error((0, exports.toError)(e))));
66
+ stream.on('end', () => settle(() => handlers.end()));
67
+ stream.on('close', () => settle(() => handlers.end()));
68
+ return {
69
+ dispose: () => {
70
+ settled = true;
71
+ (0, exports.cancelStream)(stream);
72
+ },
73
+ };
74
+ }
75
+ /**
76
+ * `streamActivatedJobsStream` resolves to `{ error }` (or `undefined` when the
77
+ * client is closing) instead of a stream when the stream cannot be opened.
78
+ * Returns the open error, or `undefined` when `result` is a usable stream.
79
+ */
80
+ const getOpenError = (result) => {
81
+ if (!result)
82
+ return new Error('Stream could not be opened');
83
+ if (typeof result === 'object' && 'error' in result) {
84
+ return (0, exports.toError)(result.error);
85
+ }
86
+ return undefined;
87
+ };
88
+ exports.getOpenError = getOpenError;
89
+ /** Delay before reconnect `attempt` (1-based): exponential, capped. */
90
+ const backoffDelayMs = (attempt, initialDelayMs, maxDelayMs) => Math.min(initialDelayMs * 2 ** (attempt - 1), maxDelayMs);
91
+ exports.backoffDelayMs = backoffDelayMs;
92
+ exports.STREAM_HANDLE_BRAND = Symbol('StreamHandle');
93
+ function createStreamStateMachine(initial = { status: 'open' }) {
94
+ let state = initial;
95
+ let resolveDone = () => { };
96
+ const done = new Promise((resolve) => {
97
+ resolveDone = resolve;
98
+ });
99
+ return {
100
+ get state() {
101
+ return state;
102
+ },
103
+ done,
104
+ transition: (next) => {
105
+ if (state.status === 'closed') {
106
+ return false;
107
+ }
108
+ state = next;
109
+ if (next.status === 'closed') {
110
+ resolveDone(next.reason);
111
+ }
112
+ return true;
113
+ },
114
+ };
115
+ }
116
+ function createStreamHandle(machine, close) {
117
+ return {
118
+ [exports.STREAM_HANDLE_BRAND]: true,
119
+ close,
120
+ get state() {
121
+ return machine.state;
122
+ },
123
+ done: machine.done,
124
+ };
125
+ }
126
+ const describeFailure = (failure) => failure.kind === 'end'
127
+ ? 'stream ended'
128
+ : `${failure.kind}: ${failure.error.message}`;
129
+ const describeReason = (reason) => reason.kind === 'stream-failed'
130
+ ? `stream failed (${describeFailure(reason.failure)})`
131
+ : reason.kind;
132
+ // An exhaustive lookup rather than a `switch` (see STYLE.md): adding a state
133
+ // to `StreamState` without describing it here fails `tsc`.
134
+ const describers = {
135
+ open: () => 'open',
136
+ reconnecting: (s) => `reconnecting (attempt ${s.attempt}, ${describeFailure(s.cause)})`,
137
+ closed: (s) => `closed (${describeReason(s.reason)})`,
138
+ };
139
+ /** Human-readable description of a state, for logs. */
140
+ const describeStreamState = (state) => describers[state.status](state);
141
+ exports.describeStreamState = describeStreamState;
142
+ //# sourceMappingURL=StreamLifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StreamLifecycle.js","sourceRoot":"","sources":["../../../src/zeebe/lib/StreamLifecycle.ts"],"names":[],"mappings":";;;AAiIA,oCAyCC;AAuDD,4DAwBC;AAED,gDAYC;AAtKD;;;;;GAKG;AACI,MAAM,QAAQ,GAAG,CAAI,MAAe,EAA2B,EAAE,CACvE,MAAiC,CAAA;AADrB,QAAA,QAAQ,YACa;AAElC;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAAC,MAAqC,EAAQ,EAAE;IAC3E,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;IACnC,MAAM,CAAC,MAAM,EAAE,CAAA;IACf,MAAM,CAAC,OAAO,EAAE,CAAA;AACjB,CAAC,CAAA;AAJY,QAAA,YAAY,gBAIxB;AAEM,MAAM,OAAO,GAAG,CAAC,CAAU,EAAS,EAAE,CAC5C,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAA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cAAc,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG;CACrD,CAAA;AAED,uDAAuD;AAChD,MAAM,mBAAmB,GAAG,CAAC,KAAkB,EAAU,EAAE,CAChE,UAAU,CAAC,KAAK,CAAC,MAAM,CAAgC,CAAC,KAAK,CAAC,CAAA;AADnD,QAAA,mBAAmB,uBACgC"}
@@ -1,5 +1,6 @@
1
1
  import { ZeebeGrpcClient } from '../zb/ZeebeGrpcClient';
2
2
  import { StatefulLogInterceptor } from './StatefulLogInterceptor';
3
+ import { StreamHandle } from './StreamLifecycle';
3
4
  import { IZBJobWorker, Job, JobCompletionInterface, ZBGrpc, ZBWorkerTaskHandler } from './interfaces-1.0';
4
5
  import { StreamActivatedJobsRequest } from './interfaces-grpc-1.0';
5
6
  /**
@@ -44,6 +45,7 @@ export declare class ZBStreamWorker implements IZBJobWorker {
44
45
  private logger;
45
46
  private zbClient;
46
47
  private streams;
48
+ private machines;
47
49
  private closed;
48
50
  private pollTimers;
49
51
  private reconnectTimers;
@@ -81,9 +83,7 @@ export declare class ZBStreamWorker implements IZBJobWorker {
81
83
  * polling (the initial backfill poll still runs).
82
84
  */
83
85
  pollInterval?: number;
84
- } & StreamJobsRecoveryOptions): Promise<{
85
- close: () => void;
86
- }>;
86
+ } & StreamJobsRecoveryOptions): Promise<StreamHandle>;
87
87
  close(): Promise<void>;
88
88
  log(msg: JSON): void;
89
89
  debug(msg: JSON): void;