@flowcore/data-pump 0.11.0 → 0.12.0

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 +13 -0
  2. package/esm/data-pump/data-pump.d.ts +1 -0
  3. package/esm/data-pump/data-pump.d.ts.map +1 -1
  4. package/esm/data-pump/data-pump.js +11 -0
  5. package/esm/data-pump/no-op-logger.d.ts +3 -0
  6. package/esm/data-pump/no-op-logger.d.ts.map +1 -0
  7. package/esm/data-pump/no-op-logger.js +6 -0
  8. package/esm/data-pump/notifier.js +1 -1
  9. package/esm/mod.d.ts +1 -2
  10. package/esm/mod.d.ts.map +1 -1
  11. package/esm/mod.js +1 -2
  12. package/package.json +1 -1
  13. package/script/data-pump/data-pump.d.ts +1 -0
  14. package/script/data-pump/data-pump.d.ts.map +1 -1
  15. package/script/data-pump/data-pump.js +11 -0
  16. package/script/data-pump/no-op-logger.d.ts +3 -0
  17. package/script/data-pump/no-op-logger.d.ts.map +1 -0
  18. package/script/data-pump/no-op-logger.js +9 -0
  19. package/script/data-pump/notifier.js +2 -2
  20. package/script/mod.d.ts +1 -2
  21. package/script/mod.d.ts.map +1 -1
  22. package/script/mod.js +1 -2
  23. package/esm/lib/data-pump-create.d.ts +0 -46
  24. package/esm/lib/data-pump-create.d.ts.map +0 -1
  25. package/esm/lib/data-pump-create.js +0 -64
  26. package/esm/lib/data-pump.d.ts +0 -80
  27. package/esm/lib/data-pump.d.ts.map +0 -1
  28. package/esm/lib/data-pump.js +0 -437
  29. package/esm/lib/nats-notifier.d.ts +0 -18
  30. package/esm/lib/nats-notifier.d.ts.map +0 -1
  31. package/esm/lib/nats-notifier.js +0 -88
  32. package/esm/lib/ws-notifier.d.ts +0 -37
  33. package/esm/lib/ws-notifier.d.ts.map +0 -1
  34. package/esm/lib/ws-notifier.js +0 -86
  35. package/script/lib/data-pump-create.d.ts +0 -46
  36. package/script/lib/data-pump-create.d.ts.map +0 -1
  37. package/script/lib/data-pump-create.js +0 -68
  38. package/script/lib/data-pump.d.ts +0 -80
  39. package/script/lib/data-pump.d.ts.map +0 -1
  40. package/script/lib/data-pump.js +0 -441
  41. package/script/lib/nats-notifier.d.ts +0 -18
  42. package/script/lib/nats-notifier.d.ts.map +0 -1
  43. package/script/lib/nats-notifier.js +0 -115
  44. package/script/lib/ws-notifier.d.ts +0 -37
  45. package/script/lib/ws-notifier.d.ts.map +0 -1
  46. package/script/lib/ws-notifier.js +0 -90
@@ -1,437 +0,0 @@
1
- import { DataCoreFetchCommand, EventsFetchCommand, EventsFetchTimeBucketsByNamesCommand, } from "../deps/jsr.io/@flowcore/sdk/1.26.3/src/mod.js";
2
- import { TimeUuid } from "../deps/jsr.io/@flowcore/time-uuid/0.1.2/src/mod.js";
3
- import { format, startOfHour } from "date-fns";
4
- import { utc } from "@date-fns/utc";
5
- var FlowcoreBufferEventStatus;
6
- (function (FlowcoreBufferEventStatus) {
7
- FlowcoreBufferEventStatus["OPEN"] = "open";
8
- FlowcoreBufferEventStatus["DELIVERED"] = "delivered";
9
- })(FlowcoreBufferEventStatus || (FlowcoreBufferEventStatus = {}));
10
- export class DataPump {
11
- constructor(options) {
12
- Object.defineProperty(this, "options", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: options
17
- });
18
- Object.defineProperty(this, "logger", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: void 0
23
- });
24
- Object.defineProperty(this, "state", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: void 0
29
- });
30
- Object.defineProperty(this, "_timeBuckets", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: []
35
- });
36
- Object.defineProperty(this, "buffer", {
37
- enumerable: true,
38
- configurable: true,
39
- writable: true,
40
- value: []
41
- });
42
- Object.defineProperty(this, "bufferWaiter", {
43
- enumerable: true,
44
- configurable: true,
45
- writable: true,
46
- value: null
47
- });
48
- Object.defineProperty(this, "waiter", {
49
- enumerable: true,
50
- configurable: true,
51
- writable: true,
52
- value: null
53
- });
54
- Object.defineProperty(this, "running", {
55
- enumerable: true,
56
- configurable: true,
57
- writable: true,
58
- value: false
59
- });
60
- Object.defineProperty(this, "notifierAbortController", {
61
- enumerable: true,
62
- configurable: true,
63
- writable: true,
64
- value: void 0
65
- });
66
- Object.defineProperty(this, "dataCoreId", {
67
- enumerable: true,
68
- configurable: true,
69
- writable: true,
70
- value: void 0
71
- });
72
- Object.defineProperty(this, "stopAt", {
73
- enumerable: true,
74
- configurable: true,
75
- writable: true,
76
- value: void 0
77
- });
78
- this.state = {
79
- timeBucket: this.getNowTimeBucket(),
80
- };
81
- this.logger = options.logger ?? undefined;
82
- }
83
- async start() {
84
- if (this.running) {
85
- return;
86
- }
87
- await this.updateTimeBuckets();
88
- if (this.options.stopAt) {
89
- const timeBucket = format(startOfHour(utc(this.options.stopAt)), "yyyyMMddHH0000");
90
- const lastTimeBucket = this.getClosestTimeBucket(timeBucket, true);
91
- const lastEventId = TimeUuid.fromDate(this.options.stopAt).toString();
92
- this.stopAt = {
93
- timeBucket: lastTimeBucket,
94
- eventId: lastEventId,
95
- eventBufferReached: false,
96
- };
97
- this.logger?.debug("stop at", `${this.stopAt.timeBucket} @ ${this.stopAt.eventId}`);
98
- }
99
- const newState = await this.options.stateManager.getState();
100
- if (!newState) {
101
- this.state.timeBucket = this.getClosestTimeBucket(this.getNowTimeBucket());
102
- this.state.eventId = TimeUuid.now().toString();
103
- }
104
- else {
105
- this.state.timeBucket = this.getClosestTimeBucket(newState.timeBucket);
106
- this.state.eventId = newState.eventId;
107
- }
108
- if (this.stopAt?.timeBucket && this.state.timeBucket > this.stopAt.timeBucket) {
109
- throw new Error("Stop at time is lower than current state");
110
- }
111
- this.running = true;
112
- this.logger?.debug("Starting event buffer loop");
113
- this.eventBufferLoop()
114
- .then(() => this.logger?.debug("Event buffer loop stopped"))
115
- .catch((error) => this.logger?.error("Error in event buffer loop", { error }))
116
- .finally(() => this.stop());
117
- if (this.options.processor) {
118
- this.logger?.debug("Starting processor");
119
- this.proceccor()
120
- .then(() => this.logger?.debug("Processor stopped"))
121
- .catch((error) => this.logger?.error("Error in processor", { error }))
122
- .finally(() => this.stop());
123
- }
124
- }
125
- stop() {
126
- if (!this.running) {
127
- return;
128
- }
129
- this.logger?.debug("Stopping data pump");
130
- this.running = false;
131
- this.notifierAbortController?.abort("stopping");
132
- this.bufferWaiter?.(true);
133
- this.waiter?.(true);
134
- }
135
- async proceccor() {
136
- if (!this.options.processor) {
137
- throw new Error("Processor not set");
138
- }
139
- while (this.running) {
140
- try {
141
- const events = await this.pullInner(this.options.processor.concurrency);
142
- if (!this.running) {
143
- break;
144
- }
145
- const success = await this.options.processor.onEvents(events);
146
- if (success) {
147
- await this.acknowledgeInner(events.map((event) => event.eventId));
148
- }
149
- }
150
- catch (error) {
151
- this.logger?.error("Error in processor", { error });
152
- await this.stop();
153
- }
154
- }
155
- }
156
- async eventBufferLoop() {
157
- // Stop buffer loop if we are not running
158
- if (!this.running) {
159
- return;
160
- }
161
- if (this.stopAt?.eventBufferReached) {
162
- await new Promise((resolve) => setTimeout(resolve, 10_000));
163
- return this.eventBufferLoop();
164
- }
165
- // Buffer is full, wait for some space
166
- if (this.buffer.length >= this.options.buffer.size - this.options.buffer.threshold && this.running) {
167
- this.logger?.debug("Buffer is full, waiting for some space");
168
- await this.waitForBufferThreshold();
169
- }
170
- // Stop buffer loop if we are not running
171
- if (!this.running) {
172
- return;
173
- }
174
- // How many events should we fetch
175
- const eventsToFetch = this.options.buffer.size - this.buffer.length;
176
- this.logger?.info(`fetching events from ${this.state.timeBucket} @ ${this.state.eventId ?? "-"}`);
177
- const timeStart = Date.now();
178
- const command = new EventsFetchCommand({
179
- tenant: this.options.dataSource.tenant,
180
- dataCoreId: await this.getDataCoreId(),
181
- flowType: this.options.dataSource.flowType,
182
- eventTypes: this.options.dataSource.eventTypes,
183
- timeBucket: this.state.timeBucket,
184
- afterEventId: this.state.eventId,
185
- pageSize: eventsToFetch,
186
- toEventId: this.stopAt?.eventId,
187
- });
188
- const result = await this.options.flowcoreClient.execute(command);
189
- const time = Date.now() - timeStart;
190
- // Stop buffer loop if we are not running
191
- if (!this.running) {
192
- return;
193
- }
194
- const events = result.events.map((event) => ({
195
- event,
196
- status: FlowcoreBufferEventStatus.OPEN,
197
- deliveryCount: 0,
198
- deliveryTime: 0,
199
- deliveryId: "",
200
- }));
201
- this.buffer.push(...events);
202
- // Check if we have a waiter and if we have events, notify the waiter
203
- events.length && this.waiter?.();
204
- this.logger?.info(`Got ${result.events.length} events from ${this.state.timeBucket} @ ${this.state.eventId ?? "-"} in ${time}ms. Buffer size ${this.buffer.length}`);
205
- // Update buffer state
206
- this.state.eventId = result.events[result.events.length - 1]?.eventId ?? this.state.eventId;
207
- // If we don't have a next cursor move to next time bucket
208
- if (!result.nextCursor) {
209
- // Check if we reached the stop at
210
- if (this.state.timeBucket === this.stopAt?.timeBucket) {
211
- this.stopAt.eventBufferReached = true;
212
- this.logger?.debug("Reached stop at");
213
- this.waiter?.();
214
- return this.eventBufferLoop();
215
- }
216
- // Get next time bucket
217
- const nextTimeBucketIndex = this.timeBuckets.indexOf(this.state.timeBucket) + 1;
218
- // There is no next time bucket
219
- if (!this.timeBuckets[nextTimeBucketIndex]) {
220
- if (result.events.length) {
221
- // If we got events, we can try to get more one more time
222
- this.logger?.debug("Try to get more events");
223
- }
224
- else {
225
- // If we didn't get events, we wait for notifier
226
- await this.waitForNotifier();
227
- }
228
- }
229
- else {
230
- // Set next time bucket
231
- this.state.timeBucket = this.timeBuckets[nextTimeBucketIndex];
232
- }
233
- }
234
- return this.eventBufferLoop();
235
- }
236
- async reOpen(eventIds, deliveryId) {
237
- if (!eventIds.length) {
238
- return;
239
- }
240
- const failedEvents = [];
241
- const reopenedEvents = [];
242
- let lastEvent;
243
- this.buffer = this.buffer.filter((event) => {
244
- if (event.deliveryId === deliveryId && this.options.buffer.maxRedeliveryCount > -1 &&
245
- event.deliveryCount > this.options.buffer.maxRedeliveryCount) {
246
- failedEvents.push(event.event);
247
- lastEvent = event.event;
248
- return false;
249
- }
250
- if (event.deliveryId === deliveryId && eventIds.includes(event.event.eventId)) {
251
- event.status = FlowcoreBufferEventStatus.OPEN;
252
- event.deliveryId = "";
253
- reopenedEvents.push(event.event);
254
- }
255
- return true;
256
- });
257
- if (reopenedEvents.length) {
258
- this.waiter?.();
259
- }
260
- if (this.bufferWaiter && this.buffer.length <= this.options.buffer.size - this.options.buffer.threshold) {
261
- this.bufferWaiter();
262
- }
263
- if (failedEvents.length) {
264
- await this.options.processor?.onFailedEvents?.(failedEvents);
265
- await this.updateState(lastEvent?.eventId);
266
- }
267
- }
268
- updateState(eventId) {
269
- const firstEvent = this.buffer[0];
270
- if (!firstEvent) {
271
- if (eventId) {
272
- const date = TimeUuid.fromString(eventId).getDate();
273
- const timeBucket = format(startOfHour(utc(date)), "yyyyMMddHH0000");
274
- return this.options.stateManager.setState?.({ timeBucket, eventId });
275
- }
276
- return;
277
- }
278
- const timeUuid = TimeUuid.fromString(firstEvent.event.eventId);
279
- const timeBucket = firstEvent.event.timeBucket;
280
- return this.options.stateManager.setState?.({ timeBucket, eventId: timeUuid.getBefore().toString() });
281
- }
282
- get timeBuckets() {
283
- const timeBucketNow = this.getNowTimeBucket();
284
- if (this._timeBuckets[this._timeBuckets.length - 1] !== timeBucketNow) {
285
- return [...this._timeBuckets, timeBucketNow];
286
- }
287
- return this._timeBuckets;
288
- }
289
- getClosestTimeBucket(timeBucket, getBefore = false) {
290
- if (!timeBucket.match(/^\d{14}$/)) {
291
- throw new Error(`Invalid timebucket: ${timeBucket}`);
292
- }
293
- if (getBefore) {
294
- return (this.timeBuckets.findLast((t) => Number.parseFloat(t) <= Number.parseFloat(timeBucket)) ??
295
- this.timeBuckets[this.timeBuckets.length - 1]);
296
- }
297
- return (this.timeBuckets.find((t) => Number.parseFloat(t) >= Number.parseFloat(timeBucket)) ??
298
- this.timeBuckets[this.timeBuckets.length - 1]);
299
- }
300
- async updateTimeBuckets() {
301
- this._timeBuckets = [];
302
- let first = true;
303
- let cursor;
304
- while (cursor !== undefined || first) {
305
- first = false;
306
- const command = new EventsFetchTimeBucketsByNamesCommand({
307
- tenant: this.options.dataSource.tenant,
308
- dataCoreId: await this.getDataCoreId(),
309
- flowType: this.options.dataSource.flowType,
310
- eventTypes: this.options.dataSource.eventTypes,
311
- pageSize: 10_000,
312
- });
313
- // sdkCommandCount.labels("EventsFetchTimeBucketsByNamesCommand").inc(1)
314
- const result = await this.options.flowcoreClient.execute(command);
315
- this._timeBuckets.push(...result.timeBuckets);
316
- cursor = result.nextCursor;
317
- }
318
- }
319
- async pullInner(amount) {
320
- if (!this.running) {
321
- throw new Error("Data pump not running");
322
- }
323
- if (this.stopAt?.eventBufferReached && this.buffer.length === 0) {
324
- await this.options.processor?.onDone?.();
325
- await this.stop();
326
- return [];
327
- }
328
- const deliveryId = crypto.randomUUID();
329
- const events = [];
330
- for (const event of this.buffer) {
331
- if (event.status === FlowcoreBufferEventStatus.OPEN) {
332
- event.status = FlowcoreBufferEventStatus.DELIVERED;
333
- event.deliveryCount++;
334
- event.deliveryId = deliveryId;
335
- events.push(event.event);
336
- if (events.length >= amount) {
337
- break;
338
- }
339
- }
340
- }
341
- if (!events.length) {
342
- await this.waitForEvents();
343
- if (!this.running) {
344
- return [];
345
- }
346
- return this.pullInner(amount);
347
- }
348
- setTimeout(() => {
349
- this.reOpen(events.map((event) => event.eventId), deliveryId);
350
- }, this.options.buffer.achknowledgeTimeoutMs);
351
- return events;
352
- }
353
- async acknowledgeInner(eventIds) {
354
- if (!eventIds.length) {
355
- return;
356
- }
357
- // this.logger.info("Acknowledging events", { events: eventIds.length })
358
- const lastEventInBuffer = this.buffer[this.buffer.length - 1];
359
- this.buffer = this.buffer.filter((event) => {
360
- if (eventIds.includes(event.event.eventId)) {
361
- return false;
362
- }
363
- return true;
364
- });
365
- if (this.bufferWaiter && this.buffer.length <= this.options.buffer.size - this.options.buffer.threshold) {
366
- this.bufferWaiter();
367
- }
368
- if (!this.buffer.length) {
369
- await this.updateState(lastEventInBuffer?.event.eventId);
370
- }
371
- else {
372
- await this.updateState();
373
- }
374
- if (!this.buffer.length && this.stopAt?.eventBufferReached) {
375
- await this.options.processor?.onDone?.();
376
- await this.stop();
377
- }
378
- }
379
- async waitForNotifier() {
380
- this.logger?.info("No more events, waiting for notifier...");
381
- this.notifierAbortController = new AbortController();
382
- await this.options.notifier.wait({ ...this.options.dataSource, dataCoreId: await this.getDataCoreId() }, this.notifierAbortController.signal);
383
- this.notifierAbortController = undefined;
384
- }
385
- async waitForEvents() {
386
- this.logger?.debug("No events, waiting for events");
387
- await new Promise((resolve) => {
388
- this.waiter = (stopping) => {
389
- if (!stopping) {
390
- this.logger?.debug("Notifying waiter that we have events");
391
- }
392
- resolve();
393
- };
394
- });
395
- this.waiter = null;
396
- }
397
- async waitForBufferThreshold() {
398
- if (this.buffer.length < this.options.buffer.size - this.options.buffer.threshold) {
399
- return;
400
- }
401
- await new Promise((resolve) => {
402
- this.bufferWaiter = (stopping) => {
403
- if (!stopping) {
404
- this.logger?.debug("Notifying buffer waiter that we are under threshold");
405
- }
406
- resolve();
407
- };
408
- });
409
- this.bufferWaiter = null;
410
- }
411
- getNowTimeBucket() {
412
- return format(startOfHour(utc(new Date())), "yyyyMMddHH0000");
413
- }
414
- async getDataCoreId() {
415
- if (this.dataCoreId) {
416
- return this.dataCoreId;
417
- }
418
- const dataCore = await this.options.flowcoreClient.execute(new DataCoreFetchCommand({
419
- dataCore: this.options.dataSource.dataCore,
420
- tenant: this.options.dataSource.tenant,
421
- }));
422
- this.dataCoreId = dataCore.id;
423
- return this.dataCoreId;
424
- }
425
- pull(amount) {
426
- if (this.options.processor) {
427
- throw new Error("Pull is not supported when processor is set");
428
- }
429
- return this.pullInner(amount);
430
- }
431
- acknowledge(eventIds) {
432
- if (this.options.processor) {
433
- throw new Error("Acknowledge is not supported when processor is set");
434
- }
435
- return this.acknowledgeInner(eventIds);
436
- }
437
- }
@@ -1,18 +0,0 @@
1
- import type { DataPumpNotifier } from "./data-pump.js";
2
- interface NatsClientOptions {
3
- servers: string[];
4
- timeoutMs?: number;
5
- }
6
- export declare class NatsNotifier {
7
- private readonly options;
8
- private nats;
9
- private timeoutMs;
10
- constructor(options: NatsClientOptions);
11
- private start;
12
- private stop;
13
- private awaitMessage;
14
- private getSubscription;
15
- getNotifier(): DataPumpNotifier;
16
- }
17
- export {};
18
- //# sourceMappingURL=nats-notifier.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"nats-notifier.d.ts","sourceRoot":"","sources":["../../src/lib/nats-notifier.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAEtD,UAAU,iBAAiB;IACzB,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,YAAY;IAGX,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,IAAI,CAAiC;IAC7C,OAAO,CAAC,SAAS,CAAQ;gBACI,OAAO,EAAE,iBAAiB;YAIzC,KAAK;YAOL,IAAI;IAOlB,OAAO,CAAC,YAAY;IAoCpB,OAAO,CAAC,eAAe;IAgBhB,WAAW,IAAI,gBAAgB;CAavC"}
@@ -1,88 +0,0 @@
1
- import * as Nats from "nats";
2
- export class NatsNotifier {
3
- constructor(options) {
4
- Object.defineProperty(this, "options", {
5
- enumerable: true,
6
- configurable: true,
7
- writable: true,
8
- value: options
9
- });
10
- Object.defineProperty(this, "nats", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: void 0
15
- });
16
- Object.defineProperty(this, "timeoutMs", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: void 0
21
- });
22
- this.timeoutMs = options.timeoutMs ?? 20_000;
23
- }
24
- async start() {
25
- if (this.nats) {
26
- return;
27
- }
28
- this.nats = await Nats.connect({ servers: this.options.servers });
29
- }
30
- async stop() {
31
- if (this.nats) {
32
- await this.nats.close();
33
- this.nats = undefined;
34
- }
35
- }
36
- awaitMessage(subjects, timeoutMs) {
37
- const abortController = new AbortController();
38
- const promise = new Promise((resolve) => {
39
- const timer = setTimeout(() => {
40
- for (const subscription of subscriptions) {
41
- subscription.unsubscribe();
42
- }
43
- resolve();
44
- }, timeoutMs);
45
- const subscriptions = [];
46
- for (const subject of subjects) {
47
- subscriptions.push(this.getSubscription(subject, (_message) => {
48
- for (const subscription of subscriptions) {
49
- clearTimeout(timer);
50
- subscription.unsubscribe();
51
- }
52
- resolve();
53
- }));
54
- }
55
- abortController.signal.addEventListener("abort", () => {
56
- for (const subscription of subscriptions) {
57
- subscription.unsubscribe();
58
- clearTimeout(timer);
59
- }
60
- resolve();
61
- });
62
- });
63
- return [promise, abortController];
64
- }
65
- getSubscription(subject, onMessage) {
66
- if (!this.nats) {
67
- throw new Error("NatsClient is not started");
68
- }
69
- const subscription = this.nats.subscribe(subject);
70
- (async () => {
71
- for await (const message of subscription) {
72
- await onMessage(message, subscription);
73
- }
74
- })().catch(console.error);
75
- return subscription;
76
- }
77
- getNotifier() {
78
- return {
79
- wait: async (dataSource, signal) => {
80
- await this.start();
81
- const topics = dataSource.eventTypes.map((eventType) => `stored.event.notify.0.${dataSource.dataCoreId}.${dataSource.flowType}.${eventType}`);
82
- const [promise, abortController] = this.awaitMessage(topics, this.timeoutMs);
83
- signal?.addEventListener("abort", () => abortController.abort());
84
- await promise;
85
- },
86
- };
87
- }
88
- }
@@ -1,37 +0,0 @@
1
- import type { DataPumpNotifier, Logger } from "./data-pump.js";
2
- interface WebSocketNotifierOptionsAuthOidcClient {
3
- oidcClient: {
4
- getToken: () => Promise<{
5
- accessToken: string;
6
- }>;
7
- };
8
- }
9
- interface WebSocketNotifierOptionsAuthApiKey {
10
- apiKey: string;
11
- apiKeyId: string;
12
- }
13
- type WebSocketNotifierOptionsAuth = WebSocketNotifierOptionsAuthOidcClient | WebSocketNotifierOptionsAuthApiKey;
14
- export declare class WebSocketNotifier {
15
- private readonly authOptions;
16
- private subject?;
17
- private notificationClient?;
18
- private dataSource;
19
- private eventResolver?;
20
- private logger?;
21
- private timeoutMs;
22
- constructor(options: {
23
- auth: WebSocketNotifierOptionsAuth;
24
- dataSource: {
25
- tenant: string;
26
- dataCore: string;
27
- flowType: string;
28
- eventTypes: string[];
29
- };
30
- logger?: Logger;
31
- timeoutMs?: number;
32
- });
33
- private onEvent;
34
- getNotifier(): DataPumpNotifier;
35
- }
36
- export {};
37
- //# sourceMappingURL=ws-notifier.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ws-notifier.d.ts","sourceRoot":"","sources":["../../src/lib/ws-notifier.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAI9D,UAAU,sCAAsC;IAC9C,UAAU,EAAE;QACV,QAAQ,EAAE,MAAM,OAAO,CAAC;YAAE,WAAW,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KACjD,CAAA;CACF;AAED,UAAU,kCAAkC;IAC1C,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,KAAK,4BAA4B,GAAG,sCAAsC,GAAG,kCAAkC,CAAA;AAE/G,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA8B;IAC1D,OAAO,CAAC,OAAO,CAAC,CAA4B;IAC5C,OAAO,CAAC,kBAAkB,CAAC,CAAoB;IAC/C,OAAO,CAAC,UAAU,CAKjB;IACD,OAAO,CAAC,aAAa,CAAC,CAAY;IAClC,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,SAAS,CAAQ;gBAEb,OAAO,EAAE;QACnB,IAAI,EAAE,4BAA4B,CAAA;QAClC,UAAU,EAAE;YACV,MAAM,EAAE,MAAM,CAAA;YACd,QAAQ,EAAE,MAAM,CAAA;YAChB,QAAQ,EAAE,MAAM,CAAA;YAChB,UAAU,EAAE,MAAM,EAAE,CAAA;SACrB,CAAA;QACD,MAAM,CAAC,EAAE,MAAM,CAAA;QACf,SAAS,CAAC,EAAE,MAAM,CAAA;KACnB;IAOD,OAAO,CAAC,OAAO;IAMR,WAAW,IAAI,gBAAgB;CAkCvC"}
@@ -1,86 +0,0 @@
1
- import { Subject } from "rxjs";
2
- import { NotificationClient } from "../deps/jsr.io/@flowcore/sdk/1.26.3/src/mod.js";
3
- export class WebSocketNotifier {
4
- constructor(options) {
5
- Object.defineProperty(this, "authOptions", {
6
- enumerable: true,
7
- configurable: true,
8
- writable: true,
9
- value: void 0
10
- });
11
- Object.defineProperty(this, "subject", {
12
- enumerable: true,
13
- configurable: true,
14
- writable: true,
15
- value: void 0
16
- });
17
- Object.defineProperty(this, "notificationClient", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: void 0
22
- });
23
- Object.defineProperty(this, "dataSource", {
24
- enumerable: true,
25
- configurable: true,
26
- writable: true,
27
- value: void 0
28
- });
29
- Object.defineProperty(this, "eventResolver", {
30
- enumerable: true,
31
- configurable: true,
32
- writable: true,
33
- value: void 0
34
- });
35
- Object.defineProperty(this, "logger", {
36
- enumerable: true,
37
- configurable: true,
38
- writable: true,
39
- value: void 0
40
- });
41
- Object.defineProperty(this, "timeoutMs", {
42
- enumerable: true,
43
- configurable: true,
44
- writable: true,
45
- value: void 0
46
- });
47
- this.authOptions = options.auth;
48
- this.dataSource = options.dataSource;
49
- this.logger = options.logger;
50
- this.timeoutMs = options.timeoutMs ?? 20_000;
51
- }
52
- onEvent(event) {
53
- if (this.dataSource.eventTypes.includes(event.data.eventType)) {
54
- this.eventResolver?.();
55
- }
56
- }
57
- getNotifier() {
58
- return {
59
- wait: async (_, signal) => {
60
- this.subject = new Subject();
61
- this.subject.subscribe({
62
- next: this.onEvent.bind(this),
63
- error: (error) => this.logger?.error("Notification stream error:", error),
64
- complete: () => this.logger?.info("Notification stream completed"),
65
- });
66
- this.notificationClient = new NotificationClient(this.subject, this.authOptions, {
67
- tenant: this.dataSource.tenant,
68
- dataCore: this.dataSource.dataCore,
69
- flowType: this.dataSource.flowType,
70
- }, {
71
- logger: this.logger,
72
- reconnectInterval: 1000,
73
- });
74
- await this.notificationClient.connect();
75
- const promise = new Promise((resolve) => {
76
- this.eventResolver = resolve;
77
- });
78
- setTimeout(() => this.eventResolver?.(), this.timeoutMs);
79
- signal?.addEventListener("abort", () => this.eventResolver?.());
80
- await promise;
81
- this.eventResolver = undefined;
82
- this.notificationClient.disconnect();
83
- },
84
- };
85
- }
86
- }