@taphubhq/sdk-core 0.16.0 → 0.16.1
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 +35 -18
- package/dist/index.js +35 -18
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -954,6 +954,19 @@ function createMqttTransport(endpoint, opts = {}) {
|
|
|
954
954
|
}
|
|
955
955
|
});
|
|
956
956
|
client.on("connect", () => {
|
|
957
|
+
const c = client;
|
|
958
|
+
for (const sub of subscriptions.values()) {
|
|
959
|
+
for (const t of sub.topics) c.subscribe(t, { qos: 1 });
|
|
960
|
+
}
|
|
961
|
+
for (const entry of candleSubscriptions.values()) {
|
|
962
|
+
c.subscribe(entry.topic, { qos: 0 });
|
|
963
|
+
}
|
|
964
|
+
for (const entry of statsSubscriptions.values()) {
|
|
965
|
+
c.subscribe(entry.topic, { qos: 0 });
|
|
966
|
+
}
|
|
967
|
+
for (const entry of walletSubscriptions.values()) {
|
|
968
|
+
c.subscribe(entry.topic, { qos: 1 });
|
|
969
|
+
}
|
|
957
970
|
fireLifecycle({ kind: "connect", rttMs: Date.now() - connectStartedAt });
|
|
958
971
|
});
|
|
959
972
|
client.on("reconnect", () => {
|
|
@@ -1668,10 +1681,18 @@ function classifyBackendHealth(samples) {
|
|
|
1668
1681
|
}
|
|
1669
1682
|
return "ok";
|
|
1670
1683
|
}
|
|
1671
|
-
var
|
|
1672
|
-
var
|
|
1673
|
-
var
|
|
1674
|
-
var
|
|
1684
|
+
var RTT_GOOD_TO_FAIR = 180;
|
|
1685
|
+
var RTT_FAIR_TO_GOOD = 120;
|
|
1686
|
+
var RTT_FAIR_TO_POOR = 450;
|
|
1687
|
+
var RTT_POOR_TO_FAIR = 350;
|
|
1688
|
+
var JITTER_POOR = 150;
|
|
1689
|
+
var JITTER_LEAVE_POOR = 120;
|
|
1690
|
+
var LOSS_POOR = 0.05;
|
|
1691
|
+
var LOSS_LEAVE_POOR = 0.03;
|
|
1692
|
+
var isPoor = (m) => m.rtt > RTT_FAIR_TO_POOR || m.jitter > JITTER_POOR || m.lossRate > LOSS_POOR;
|
|
1693
|
+
var canLeavePoor = (m) => m.rtt < RTT_POOR_TO_FAIR && m.jitter < JITTER_LEAVE_POOR && m.lossRate < LOSS_LEAVE_POOR;
|
|
1694
|
+
var isFairUpper = (rtt) => rtt > RTT_GOOD_TO_FAIR;
|
|
1695
|
+
var canReachGood = (rtt) => rtt < RTT_FAIR_TO_GOOD;
|
|
1675
1696
|
function classifyNetworkLevel(metrics, current) {
|
|
1676
1697
|
if (current === "offline") {
|
|
1677
1698
|
return isPoor(metrics) ? "poor" : "fair";
|
|
@@ -1798,13 +1819,11 @@ var NetworkQualityMonitor = class {
|
|
|
1798
1819
|
if (bucket.length > WINDOW_MAX_SAMPLES_PER_SOURCE) {
|
|
1799
1820
|
bucket.shift();
|
|
1800
1821
|
}
|
|
1801
|
-
if (sample.source !== "connection" && sample.source !== "browser") {
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
this.smoother.add(sample.rtt, this.deriveMetrics());
|
|
1807
|
-
}
|
|
1822
|
+
if (sample.reason === "ok" && sample.source !== "connection" && sample.source !== "browser") {
|
|
1823
|
+
this.lastSuccessfulHttpAt = sample.ts;
|
|
1824
|
+
}
|
|
1825
|
+
if (sample.source === "mqtt" && sample.reason === "ok") {
|
|
1826
|
+
this.smoother.add(sample.rtt, this.deriveMetrics());
|
|
1808
1827
|
}
|
|
1809
1828
|
this.recompute(prevMqttConnectedOverride);
|
|
1810
1829
|
}
|
|
@@ -1975,17 +1994,15 @@ var NetworkQualityMonitor = class {
|
|
|
1975
1994
|
(s) => s.reason === "network" || s.reason === "timeout" || s.reason === "offline"
|
|
1976
1995
|
).length;
|
|
1977
1996
|
const lossRate = total > 0 ? lossCount / total : 0;
|
|
1978
|
-
const
|
|
1979
|
-
(s) => s.reason === "ok" || s.reason === "backend5xx" || s.reason === "backend4xx" || s.reason === "gqlError"
|
|
1980
|
-
).map((s) => s.rtt).filter((r) => Number.isFinite(r));
|
|
1997
|
+
const mqttRtts = samples.filter((s) => s.source === "mqtt" && s.reason === "ok").map((s) => s.rtt).filter((r) => Number.isFinite(r));
|
|
1981
1998
|
let jitter = 0;
|
|
1982
|
-
if (
|
|
1983
|
-
const mean =
|
|
1984
|
-
const variance =
|
|
1999
|
+
if (mqttRtts.length > 1) {
|
|
2000
|
+
const mean = mqttRtts.reduce((a, b) => a + b, 0) / mqttRtts.length;
|
|
2001
|
+
const variance = mqttRtts.reduce((a, b) => a + (b - mean) ** 2, 0) / mqttRtts.length;
|
|
1985
2002
|
jitter = Math.sqrt(variance);
|
|
1986
2003
|
}
|
|
1987
2004
|
const connectionFallback = samples.filter((s) => s.source === "connection" && Number.isFinite(s.rtt)).slice(-1)[0]?.rtt;
|
|
1988
|
-
const emaForLevel = this.smoother.ema > 0 ? this.smoother.ema :
|
|
2005
|
+
const emaForLevel = this.smoother.ema > 0 ? this.smoother.ema : mqttRtts.length > 0 ? mqttRtts[mqttRtts.length - 1] ?? 0 : connectionFallback ?? 0;
|
|
1989
2006
|
return { jitter, lossRate, emaForLevel };
|
|
1990
2007
|
}
|
|
1991
2008
|
isHardOffline() {
|
package/dist/index.js
CHANGED
|
@@ -889,6 +889,19 @@ function createMqttTransport(endpoint, opts = {}) {
|
|
|
889
889
|
}
|
|
890
890
|
});
|
|
891
891
|
client.on("connect", () => {
|
|
892
|
+
const c = client;
|
|
893
|
+
for (const sub of subscriptions.values()) {
|
|
894
|
+
for (const t of sub.topics) c.subscribe(t, { qos: 1 });
|
|
895
|
+
}
|
|
896
|
+
for (const entry of candleSubscriptions.values()) {
|
|
897
|
+
c.subscribe(entry.topic, { qos: 0 });
|
|
898
|
+
}
|
|
899
|
+
for (const entry of statsSubscriptions.values()) {
|
|
900
|
+
c.subscribe(entry.topic, { qos: 0 });
|
|
901
|
+
}
|
|
902
|
+
for (const entry of walletSubscriptions.values()) {
|
|
903
|
+
c.subscribe(entry.topic, { qos: 1 });
|
|
904
|
+
}
|
|
892
905
|
fireLifecycle({ kind: "connect", rttMs: Date.now() - connectStartedAt });
|
|
893
906
|
});
|
|
894
907
|
client.on("reconnect", () => {
|
|
@@ -1603,10 +1616,18 @@ function classifyBackendHealth(samples) {
|
|
|
1603
1616
|
}
|
|
1604
1617
|
return "ok";
|
|
1605
1618
|
}
|
|
1606
|
-
var
|
|
1607
|
-
var
|
|
1608
|
-
var
|
|
1609
|
-
var
|
|
1619
|
+
var RTT_GOOD_TO_FAIR = 180;
|
|
1620
|
+
var RTT_FAIR_TO_GOOD = 120;
|
|
1621
|
+
var RTT_FAIR_TO_POOR = 450;
|
|
1622
|
+
var RTT_POOR_TO_FAIR = 350;
|
|
1623
|
+
var JITTER_POOR = 150;
|
|
1624
|
+
var JITTER_LEAVE_POOR = 120;
|
|
1625
|
+
var LOSS_POOR = 0.05;
|
|
1626
|
+
var LOSS_LEAVE_POOR = 0.03;
|
|
1627
|
+
var isPoor = (m) => m.rtt > RTT_FAIR_TO_POOR || m.jitter > JITTER_POOR || m.lossRate > LOSS_POOR;
|
|
1628
|
+
var canLeavePoor = (m) => m.rtt < RTT_POOR_TO_FAIR && m.jitter < JITTER_LEAVE_POOR && m.lossRate < LOSS_LEAVE_POOR;
|
|
1629
|
+
var isFairUpper = (rtt) => rtt > RTT_GOOD_TO_FAIR;
|
|
1630
|
+
var canReachGood = (rtt) => rtt < RTT_FAIR_TO_GOOD;
|
|
1610
1631
|
function classifyNetworkLevel(metrics, current) {
|
|
1611
1632
|
if (current === "offline") {
|
|
1612
1633
|
return isPoor(metrics) ? "poor" : "fair";
|
|
@@ -1733,13 +1754,11 @@ var NetworkQualityMonitor = class {
|
|
|
1733
1754
|
if (bucket.length > WINDOW_MAX_SAMPLES_PER_SOURCE) {
|
|
1734
1755
|
bucket.shift();
|
|
1735
1756
|
}
|
|
1736
|
-
if (sample.source !== "connection" && sample.source !== "browser") {
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
this.smoother.add(sample.rtt, this.deriveMetrics());
|
|
1742
|
-
}
|
|
1757
|
+
if (sample.reason === "ok" && sample.source !== "connection" && sample.source !== "browser") {
|
|
1758
|
+
this.lastSuccessfulHttpAt = sample.ts;
|
|
1759
|
+
}
|
|
1760
|
+
if (sample.source === "mqtt" && sample.reason === "ok") {
|
|
1761
|
+
this.smoother.add(sample.rtt, this.deriveMetrics());
|
|
1743
1762
|
}
|
|
1744
1763
|
this.recompute(prevMqttConnectedOverride);
|
|
1745
1764
|
}
|
|
@@ -1910,17 +1929,15 @@ var NetworkQualityMonitor = class {
|
|
|
1910
1929
|
(s) => s.reason === "network" || s.reason === "timeout" || s.reason === "offline"
|
|
1911
1930
|
).length;
|
|
1912
1931
|
const lossRate = total > 0 ? lossCount / total : 0;
|
|
1913
|
-
const
|
|
1914
|
-
(s) => s.reason === "ok" || s.reason === "backend5xx" || s.reason === "backend4xx" || s.reason === "gqlError"
|
|
1915
|
-
).map((s) => s.rtt).filter((r) => Number.isFinite(r));
|
|
1932
|
+
const mqttRtts = samples.filter((s) => s.source === "mqtt" && s.reason === "ok").map((s) => s.rtt).filter((r) => Number.isFinite(r));
|
|
1916
1933
|
let jitter = 0;
|
|
1917
|
-
if (
|
|
1918
|
-
const mean =
|
|
1919
|
-
const variance =
|
|
1934
|
+
if (mqttRtts.length > 1) {
|
|
1935
|
+
const mean = mqttRtts.reduce((a, b) => a + b, 0) / mqttRtts.length;
|
|
1936
|
+
const variance = mqttRtts.reduce((a, b) => a + (b - mean) ** 2, 0) / mqttRtts.length;
|
|
1920
1937
|
jitter = Math.sqrt(variance);
|
|
1921
1938
|
}
|
|
1922
1939
|
const connectionFallback = samples.filter((s) => s.source === "connection" && Number.isFinite(s.rtt)).slice(-1)[0]?.rtt;
|
|
1923
|
-
const emaForLevel = this.smoother.ema > 0 ? this.smoother.ema :
|
|
1940
|
+
const emaForLevel = this.smoother.ema > 0 ? this.smoother.ema : mqttRtts.length > 0 ? mqttRtts[mqttRtts.length - 1] ?? 0 : connectionFallback ?? 0;
|
|
1924
1941
|
return { jitter, lossRate, emaForLevel };
|
|
1925
1942
|
}
|
|
1926
1943
|
isHardOffline() {
|