slapflow 1.2.0 → 1.2.2
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/dist/index.cjs +136 -100
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +136 -100
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -76,6 +76,9 @@ var isBusEvent = (event) => {
|
|
|
76
76
|
|
|
77
77
|
// src/helpers/pubSub/matchesTopic.ts
|
|
78
78
|
var matchesTopic = (topic, pattern) => {
|
|
79
|
+
if (pattern === "*") {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
79
82
|
if (!pattern.includes("*")) {
|
|
80
83
|
return topic === pattern;
|
|
81
84
|
}
|
|
@@ -2675,115 +2678,148 @@ var createWS = (options) => {
|
|
|
2675
2678
|
return { start, stop, reconnect, status: () => currentStatus };
|
|
2676
2679
|
};
|
|
2677
2680
|
|
|
2681
|
+
// src/helpers/parsePayload.ts
|
|
2682
|
+
var parsePayload = (data) => {
|
|
2683
|
+
if (typeof data === "string") {
|
|
2684
|
+
try {
|
|
2685
|
+
return JSON.parse(data);
|
|
2686
|
+
} catch {
|
|
2687
|
+
return data;
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
return data;
|
|
2691
|
+
};
|
|
2692
|
+
|
|
2678
2693
|
// src/createWebSocket.ts
|
|
2679
|
-
var
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
}
|
|
2705
|
-
|
|
2706
|
-
if (
|
|
2694
|
+
var WebSocketConnection = class {
|
|
2695
|
+
constructor(options) {
|
|
2696
|
+
this.options = options;
|
|
2697
|
+
}
|
|
2698
|
+
options;
|
|
2699
|
+
_socket;
|
|
2700
|
+
_retryTimer;
|
|
2701
|
+
_retryAttempt = 0;
|
|
2702
|
+
_queue = [];
|
|
2703
|
+
_started = false;
|
|
2704
|
+
_status = "idle";
|
|
2705
|
+
start() {
|
|
2706
|
+
if (!this._started) {
|
|
2707
|
+
this._started = true;
|
|
2708
|
+
this.connect();
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
stop() {
|
|
2712
|
+
this._started = false;
|
|
2713
|
+
this.clearTimer();
|
|
2714
|
+
this.clearQueue();
|
|
2715
|
+
const current = this._socket;
|
|
2716
|
+
this._socket = void 0;
|
|
2717
|
+
current?.close();
|
|
2718
|
+
this.setStatus("stopped");
|
|
2719
|
+
}
|
|
2720
|
+
reconnect() {
|
|
2721
|
+
if (this._started) {
|
|
2722
|
+
this.clearTimer();
|
|
2723
|
+
const current = this._socket;
|
|
2724
|
+
this._socket = void 0;
|
|
2725
|
+
current?.close();
|
|
2726
|
+
this.connect();
|
|
2727
|
+
}
|
|
2728
|
+
}
|
|
2729
|
+
status() {
|
|
2730
|
+
return this._status;
|
|
2731
|
+
}
|
|
2732
|
+
send(data) {
|
|
2733
|
+
if (this._socket?.readyState === WebSocket.OPEN) {
|
|
2734
|
+
this._socket.send(data);
|
|
2735
|
+
} else {
|
|
2736
|
+
this._queue.push(data);
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
connect() {
|
|
2740
|
+
if (!this._started || this._socket) {
|
|
2707
2741
|
return;
|
|
2708
2742
|
}
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
}
|
|
2738
|
-
socket = void 0;
|
|
2739
|
-
emitSocketEvent("close", { code: event.code, reason: event.reason });
|
|
2740
|
-
if (started) {
|
|
2741
|
-
scheduleRetry();
|
|
2742
|
-
}
|
|
2743
|
-
});
|
|
2744
|
-
current.addEventListener("error", (event) => {
|
|
2745
|
-
emitSocketEvent("error", { error: event });
|
|
2746
|
-
});
|
|
2747
|
-
} catch (error) {
|
|
2748
|
-
socket = void 0;
|
|
2749
|
-
emitSocketEvent("error", { error });
|
|
2750
|
-
if (started) {
|
|
2751
|
-
scheduleRetry();
|
|
2743
|
+
const { url, protocols } = this.options;
|
|
2744
|
+
const current = new WebSocket(url, protocols ?? []);
|
|
2745
|
+
this._socket = current;
|
|
2746
|
+
this.setStatus("connecting");
|
|
2747
|
+
current.addEventListener("open", () => this.onOpen(current));
|
|
2748
|
+
current.addEventListener("message", (event) => this.onMessage(current, event));
|
|
2749
|
+
current.addEventListener("close", (event) => this.onClose(current, event));
|
|
2750
|
+
current.addEventListener("error", (event) => this.onError(current, event));
|
|
2751
|
+
}
|
|
2752
|
+
onOpen(current) {
|
|
2753
|
+
if (this._socket === current) {
|
|
2754
|
+
this._retryAttempt = 0;
|
|
2755
|
+
this.setStatus("connected");
|
|
2756
|
+
this.flush();
|
|
2757
|
+
this.dispatch("open", { url: this.options.url });
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
onMessage(current, event) {
|
|
2761
|
+
if (this._socket === current) {
|
|
2762
|
+
this.dispatch("message", parsePayload(event.data));
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
onClose(current, event) {
|
|
2766
|
+
if (this._socket === current) {
|
|
2767
|
+
this._socket = void 0;
|
|
2768
|
+
this.dispatch("close", event);
|
|
2769
|
+
if (this._started) {
|
|
2770
|
+
this.retry();
|
|
2752
2771
|
}
|
|
2753
2772
|
}
|
|
2754
|
-
}
|
|
2755
|
-
|
|
2756
|
-
if (
|
|
2757
|
-
|
|
2758
|
-
connect();
|
|
2759
|
-
}
|
|
2760
|
-
};
|
|
2761
|
-
const stop = () => {
|
|
2762
|
-
started = false;
|
|
2763
|
-
if (retryTimer) {
|
|
2764
|
-
clearTimeout(retryTimer);
|
|
2773
|
+
}
|
|
2774
|
+
onError(current, event) {
|
|
2775
|
+
if (this._socket === current) {
|
|
2776
|
+
this.dispatch("error", serializeError(event.error));
|
|
2765
2777
|
}
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2778
|
+
}
|
|
2779
|
+
dispatch(topic, parsed) {
|
|
2780
|
+
const { bus, origin } = this.options;
|
|
2781
|
+
bus.dispatch({
|
|
2782
|
+
id: createId(),
|
|
2783
|
+
topic,
|
|
2784
|
+
occurredAt: Date.now(),
|
|
2785
|
+
...origin ? { origin } : {},
|
|
2786
|
+
parsed,
|
|
2787
|
+
serialized: JSON.stringify(parsed)
|
|
2788
|
+
});
|
|
2789
|
+
}
|
|
2790
|
+
flush() {
|
|
2791
|
+
if (this._socket && this._socket.readyState === WebSocket.OPEN) {
|
|
2792
|
+
for (const message of this._queue) {
|
|
2793
|
+
this._socket.send(message);
|
|
2794
|
+
}
|
|
2795
|
+
this.clearQueue();
|
|
2775
2796
|
}
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2797
|
+
}
|
|
2798
|
+
retry() {
|
|
2799
|
+
this.setStatus("reconnecting");
|
|
2800
|
+
this._retryTimer = setTimeout(
|
|
2801
|
+
() => {
|
|
2802
|
+
this._retryTimer = void 0;
|
|
2803
|
+
this.connect();
|
|
2804
|
+
},
|
|
2805
|
+
getRetryDelay(this._retryAttempt, this.options.reconnect ?? {})
|
|
2806
|
+
);
|
|
2807
|
+
this._retryAttempt = this._retryAttempt + 1;
|
|
2808
|
+
}
|
|
2809
|
+
setStatus(status) {
|
|
2810
|
+
this._status = status || "idle";
|
|
2811
|
+
}
|
|
2812
|
+
clearQueue() {
|
|
2813
|
+
this._queue = [];
|
|
2814
|
+
}
|
|
2815
|
+
clearTimer() {
|
|
2816
|
+
if (this._retryTimer) {
|
|
2817
|
+
clearTimeout(this._retryTimer);
|
|
2779
2818
|
}
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
current?.close();
|
|
2783
|
-
connect();
|
|
2784
|
-
};
|
|
2785
|
-
return { start, stop, reconnect, status: () => currentStatus };
|
|
2819
|
+
this._retryTimer = void 0;
|
|
2820
|
+
}
|
|
2786
2821
|
};
|
|
2822
|
+
var createWebSocket = (options) => new WebSocketConnection(options);
|
|
2787
2823
|
|
|
2788
2824
|
// src/helpers/catchError.ts
|
|
2789
2825
|
var catchError = (callback) => new Promise((resolve, reject) => {
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -38,6 +38,9 @@ var isBusEvent = (event) => {
|
|
|
38
38
|
|
|
39
39
|
// src/helpers/pubSub/matchesTopic.ts
|
|
40
40
|
var matchesTopic = (topic, pattern) => {
|
|
41
|
+
if (pattern === "*") {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
41
44
|
if (!pattern.includes("*")) {
|
|
42
45
|
return topic === pattern;
|
|
43
46
|
}
|
|
@@ -2637,115 +2640,148 @@ var createWS = (options) => {
|
|
|
2637
2640
|
return { start, stop, reconnect, status: () => currentStatus };
|
|
2638
2641
|
};
|
|
2639
2642
|
|
|
2643
|
+
// src/helpers/parsePayload.ts
|
|
2644
|
+
var parsePayload = (data) => {
|
|
2645
|
+
if (typeof data === "string") {
|
|
2646
|
+
try {
|
|
2647
|
+
return JSON.parse(data);
|
|
2648
|
+
} catch {
|
|
2649
|
+
return data;
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
return data;
|
|
2653
|
+
};
|
|
2654
|
+
|
|
2640
2655
|
// src/createWebSocket.ts
|
|
2641
|
-
var
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
}
|
|
2667
|
-
|
|
2668
|
-
if (
|
|
2656
|
+
var WebSocketConnection = class {
|
|
2657
|
+
constructor(options) {
|
|
2658
|
+
this.options = options;
|
|
2659
|
+
}
|
|
2660
|
+
options;
|
|
2661
|
+
_socket;
|
|
2662
|
+
_retryTimer;
|
|
2663
|
+
_retryAttempt = 0;
|
|
2664
|
+
_queue = [];
|
|
2665
|
+
_started = false;
|
|
2666
|
+
_status = "idle";
|
|
2667
|
+
start() {
|
|
2668
|
+
if (!this._started) {
|
|
2669
|
+
this._started = true;
|
|
2670
|
+
this.connect();
|
|
2671
|
+
}
|
|
2672
|
+
}
|
|
2673
|
+
stop() {
|
|
2674
|
+
this._started = false;
|
|
2675
|
+
this.clearTimer();
|
|
2676
|
+
this.clearQueue();
|
|
2677
|
+
const current = this._socket;
|
|
2678
|
+
this._socket = void 0;
|
|
2679
|
+
current?.close();
|
|
2680
|
+
this.setStatus("stopped");
|
|
2681
|
+
}
|
|
2682
|
+
reconnect() {
|
|
2683
|
+
if (this._started) {
|
|
2684
|
+
this.clearTimer();
|
|
2685
|
+
const current = this._socket;
|
|
2686
|
+
this._socket = void 0;
|
|
2687
|
+
current?.close();
|
|
2688
|
+
this.connect();
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
status() {
|
|
2692
|
+
return this._status;
|
|
2693
|
+
}
|
|
2694
|
+
send(data) {
|
|
2695
|
+
if (this._socket?.readyState === WebSocket.OPEN) {
|
|
2696
|
+
this._socket.send(data);
|
|
2697
|
+
} else {
|
|
2698
|
+
this._queue.push(data);
|
|
2699
|
+
}
|
|
2700
|
+
}
|
|
2701
|
+
connect() {
|
|
2702
|
+
if (!this._started || this._socket) {
|
|
2669
2703
|
return;
|
|
2670
2704
|
}
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
}
|
|
2700
|
-
socket = void 0;
|
|
2701
|
-
emitSocketEvent("close", { code: event.code, reason: event.reason });
|
|
2702
|
-
if (started) {
|
|
2703
|
-
scheduleRetry();
|
|
2704
|
-
}
|
|
2705
|
-
});
|
|
2706
|
-
current.addEventListener("error", (event) => {
|
|
2707
|
-
emitSocketEvent("error", { error: event });
|
|
2708
|
-
});
|
|
2709
|
-
} catch (error) {
|
|
2710
|
-
socket = void 0;
|
|
2711
|
-
emitSocketEvent("error", { error });
|
|
2712
|
-
if (started) {
|
|
2713
|
-
scheduleRetry();
|
|
2705
|
+
const { url, protocols } = this.options;
|
|
2706
|
+
const current = new WebSocket(url, protocols ?? []);
|
|
2707
|
+
this._socket = current;
|
|
2708
|
+
this.setStatus("connecting");
|
|
2709
|
+
current.addEventListener("open", () => this.onOpen(current));
|
|
2710
|
+
current.addEventListener("message", (event) => this.onMessage(current, event));
|
|
2711
|
+
current.addEventListener("close", (event) => this.onClose(current, event));
|
|
2712
|
+
current.addEventListener("error", (event) => this.onError(current, event));
|
|
2713
|
+
}
|
|
2714
|
+
onOpen(current) {
|
|
2715
|
+
if (this._socket === current) {
|
|
2716
|
+
this._retryAttempt = 0;
|
|
2717
|
+
this.setStatus("connected");
|
|
2718
|
+
this.flush();
|
|
2719
|
+
this.dispatch("open", { url: this.options.url });
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
onMessage(current, event) {
|
|
2723
|
+
if (this._socket === current) {
|
|
2724
|
+
this.dispatch("message", parsePayload(event.data));
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
onClose(current, event) {
|
|
2728
|
+
if (this._socket === current) {
|
|
2729
|
+
this._socket = void 0;
|
|
2730
|
+
this.dispatch("close", event);
|
|
2731
|
+
if (this._started) {
|
|
2732
|
+
this.retry();
|
|
2714
2733
|
}
|
|
2715
2734
|
}
|
|
2716
|
-
}
|
|
2717
|
-
|
|
2718
|
-
if (
|
|
2719
|
-
|
|
2720
|
-
connect();
|
|
2721
|
-
}
|
|
2722
|
-
};
|
|
2723
|
-
const stop = () => {
|
|
2724
|
-
started = false;
|
|
2725
|
-
if (retryTimer) {
|
|
2726
|
-
clearTimeout(retryTimer);
|
|
2735
|
+
}
|
|
2736
|
+
onError(current, event) {
|
|
2737
|
+
if (this._socket === current) {
|
|
2738
|
+
this.dispatch("error", serializeError(event.error));
|
|
2727
2739
|
}
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2740
|
+
}
|
|
2741
|
+
dispatch(topic, parsed) {
|
|
2742
|
+
const { bus, origin } = this.options;
|
|
2743
|
+
bus.dispatch({
|
|
2744
|
+
id: createId(),
|
|
2745
|
+
topic,
|
|
2746
|
+
occurredAt: Date.now(),
|
|
2747
|
+
...origin ? { origin } : {},
|
|
2748
|
+
parsed,
|
|
2749
|
+
serialized: JSON.stringify(parsed)
|
|
2750
|
+
});
|
|
2751
|
+
}
|
|
2752
|
+
flush() {
|
|
2753
|
+
if (this._socket && this._socket.readyState === WebSocket.OPEN) {
|
|
2754
|
+
for (const message of this._queue) {
|
|
2755
|
+
this._socket.send(message);
|
|
2756
|
+
}
|
|
2757
|
+
this.clearQueue();
|
|
2737
2758
|
}
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2759
|
+
}
|
|
2760
|
+
retry() {
|
|
2761
|
+
this.setStatus("reconnecting");
|
|
2762
|
+
this._retryTimer = setTimeout(
|
|
2763
|
+
() => {
|
|
2764
|
+
this._retryTimer = void 0;
|
|
2765
|
+
this.connect();
|
|
2766
|
+
},
|
|
2767
|
+
getRetryDelay(this._retryAttempt, this.options.reconnect ?? {})
|
|
2768
|
+
);
|
|
2769
|
+
this._retryAttempt = this._retryAttempt + 1;
|
|
2770
|
+
}
|
|
2771
|
+
setStatus(status) {
|
|
2772
|
+
this._status = status || "idle";
|
|
2773
|
+
}
|
|
2774
|
+
clearQueue() {
|
|
2775
|
+
this._queue = [];
|
|
2776
|
+
}
|
|
2777
|
+
clearTimer() {
|
|
2778
|
+
if (this._retryTimer) {
|
|
2779
|
+
clearTimeout(this._retryTimer);
|
|
2741
2780
|
}
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
current?.close();
|
|
2745
|
-
connect();
|
|
2746
|
-
};
|
|
2747
|
-
return { start, stop, reconnect, status: () => currentStatus };
|
|
2781
|
+
this._retryTimer = void 0;
|
|
2782
|
+
}
|
|
2748
2783
|
};
|
|
2784
|
+
var createWebSocket = (options) => new WebSocketConnection(options);
|
|
2749
2785
|
|
|
2750
2786
|
// src/helpers/catchError.ts
|
|
2751
2787
|
var catchError = (callback) => new Promise((resolve, reject) => {
|