ton-provider-system 0.1.8 → 0.1.9

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.d.cts CHANGED
@@ -1082,6 +1082,7 @@ declare class ProviderSelector {
1082
1082
  private autoSelect;
1083
1083
  private customEndpoint;
1084
1084
  private bestProviderByNetwork;
1085
+ private activeProviderByNetwork;
1085
1086
  constructor(registry: ProviderRegistry, healthChecker: HealthChecker, config?: Partial<SelectionConfig>, logger?: Logger);
1086
1087
  /**
1087
1088
  * Get the best provider for a network
@@ -1137,8 +1138,9 @@ declare class ProviderSelector {
1137
1138
  isUsingCustomEndpoint(): boolean;
1138
1139
  /**
1139
1140
  * Clear cached best providers (forces recalculation)
1141
+ * @param network - Optional network to clear cache for. If not provided, clears all networks.
1140
1142
  */
1141
- clearCache(): void;
1143
+ clearCache(network?: Network): void;
1142
1144
  /**
1143
1145
  * Update best provider after health check
1144
1146
  */
@@ -1147,6 +1149,11 @@ declare class ProviderSelector {
1147
1149
  * Handle provider failure (switch to next best)
1148
1150
  */
1149
1151
  handleProviderFailure(providerId: string, network: Network): ResolvedProvider | null;
1152
+ /**
1153
+ * Get the currently active provider ID for a network
1154
+ * (the one that was last selected and is being used)
1155
+ */
1156
+ getActiveProviderId(network: Network): string | null;
1150
1157
  /**
1151
1158
  * Get active provider info
1152
1159
  */
@@ -1248,8 +1255,10 @@ declare class ProviderManager {
1248
1255
  * Get endpoint with rate limiting
1249
1256
  *
1250
1257
  * Waits for rate limit token before returning endpoint.
1258
+ *
1259
+ * @param forceRefresh - If true, clears cache and forces re-selection
1251
1260
  */
1252
- getEndpointWithRateLimit(timeoutMs?: number): Promise<string>;
1261
+ getEndpointWithRateLimit(timeoutMs?: number, forceRefresh?: boolean): Promise<string>;
1253
1262
  /**
1254
1263
  * Get current active provider
1255
1264
  */
package/dist/index.d.ts CHANGED
@@ -1082,6 +1082,7 @@ declare class ProviderSelector {
1082
1082
  private autoSelect;
1083
1083
  private customEndpoint;
1084
1084
  private bestProviderByNetwork;
1085
+ private activeProviderByNetwork;
1085
1086
  constructor(registry: ProviderRegistry, healthChecker: HealthChecker, config?: Partial<SelectionConfig>, logger?: Logger);
1086
1087
  /**
1087
1088
  * Get the best provider for a network
@@ -1137,8 +1138,9 @@ declare class ProviderSelector {
1137
1138
  isUsingCustomEndpoint(): boolean;
1138
1139
  /**
1139
1140
  * Clear cached best providers (forces recalculation)
1141
+ * @param network - Optional network to clear cache for. If not provided, clears all networks.
1140
1142
  */
1141
- clearCache(): void;
1143
+ clearCache(network?: Network): void;
1142
1144
  /**
1143
1145
  * Update best provider after health check
1144
1146
  */
@@ -1147,6 +1149,11 @@ declare class ProviderSelector {
1147
1149
  * Handle provider failure (switch to next best)
1148
1150
  */
1149
1151
  handleProviderFailure(providerId: string, network: Network): ResolvedProvider | null;
1152
+ /**
1153
+ * Get the currently active provider ID for a network
1154
+ * (the one that was last selected and is being used)
1155
+ */
1156
+ getActiveProviderId(network: Network): string | null;
1150
1157
  /**
1151
1158
  * Get active provider info
1152
1159
  */
@@ -1248,8 +1255,10 @@ declare class ProviderManager {
1248
1255
  * Get endpoint with rate limiting
1249
1256
  *
1250
1257
  * Waits for rate limit token before returning endpoint.
1258
+ *
1259
+ * @param forceRefresh - If true, clears cache and forces re-selection
1251
1260
  */
1252
- getEndpointWithRateLimit(timeoutMs?: number): Promise<string>;
1261
+ getEndpointWithRateLimit(timeoutMs?: number, forceRefresh?: boolean): Promise<string>;
1253
1262
  /**
1254
1263
  * Get current active provider
1255
1264
  */
package/dist/index.js CHANGED
@@ -949,6 +949,8 @@ var HealthChecker = class {
949
949
  const existing = this.results.get(key);
950
950
  const result = existing ? {
951
951
  ...existing,
952
+ success: false,
953
+ // CRITICAL: Always set success: false for degraded providers
952
954
  status: "degraded",
953
955
  error: error || "Marked as degraded",
954
956
  lastTested: /* @__PURE__ */ new Date()
@@ -1569,6 +1571,8 @@ var ProviderSelector = class {
1569
1571
  this.autoSelect = true;
1570
1572
  this.customEndpoint = null;
1571
1573
  this.bestProviderByNetwork = /* @__PURE__ */ new Map();
1574
+ // Track currently active provider per network (the one actually being used)
1575
+ this.activeProviderByNetwork = /* @__PURE__ */ new Map();
1572
1576
  this.registry = registry;
1573
1577
  this.healthChecker = healthChecker;
1574
1578
  this.config = { ...DEFAULT_CONFIG2, ...config };
@@ -1587,6 +1591,7 @@ var ProviderSelector = class {
1587
1591
  if (!this.autoSelect && this.selectedProviderId) {
1588
1592
  const provider = this.registry.getProvider(this.selectedProviderId);
1589
1593
  if (provider && provider.network === network) {
1594
+ this.activeProviderByNetwork.set(network, provider.id);
1590
1595
  return provider;
1591
1596
  }
1592
1597
  this.logger.warn(
@@ -1597,8 +1602,13 @@ var ProviderSelector = class {
1597
1602
  if (cachedBestId) {
1598
1603
  const cached = this.registry.getProvider(cachedBestId);
1599
1604
  const health = this.healthChecker.getResult(cachedBestId, network);
1600
- if (cached && health && health.success !== false && this.config.minStatus.includes(health.status)) {
1605
+ if (cached && health && health.success !== false && health.success !== void 0 && // Explicitly check for undefined
1606
+ this.config.minStatus.includes(health.status)) {
1607
+ this.activeProviderByNetwork.set(network, cachedBestId);
1601
1608
  return cached;
1609
+ } else {
1610
+ this.bestProviderByNetwork.delete(network);
1611
+ this.activeProviderByNetwork.delete(network);
1602
1612
  }
1603
1613
  }
1604
1614
  return this.findBestProvider(network);
@@ -1620,10 +1630,17 @@ var ProviderSelector = class {
1620
1630
  const defaults = this.registry.getDefaultOrderForNetwork(network);
1621
1631
  for (const defaultProvider of defaults) {
1622
1632
  const health = this.healthChecker.getResult(defaultProvider.id, network);
1623
- if (!health || health.status === "untested" || health.success === true) {
1633
+ if (!health || health.status === "untested") {
1634
+ this.logger.warn(
1635
+ `No healthy providers for ${network}, using untested default: ${defaultProvider.id}`
1636
+ );
1637
+ this.activeProviderByNetwork.set(network, defaultProvider.id);
1638
+ return defaultProvider;
1639
+ } else if (health.success === true) {
1624
1640
  this.logger.warn(
1625
1641
  `No healthy providers for ${network}, using default: ${defaultProvider.id}`
1626
1642
  );
1643
+ this.activeProviderByNetwork.set(network, defaultProvider.id);
1627
1644
  return defaultProvider;
1628
1645
  }
1629
1646
  }
@@ -1633,6 +1650,7 @@ var ProviderSelector = class {
1633
1650
  this.logger.warn(
1634
1651
  `No tested healthy providers for ${network}, using untested: ${provider.id}`
1635
1652
  );
1653
+ this.activeProviderByNetwork.set(network, provider.id);
1636
1654
  return provider;
1637
1655
  }
1638
1656
  }
@@ -1643,10 +1661,12 @@ var ProviderSelector = class {
1643
1661
  const bestHealth = this.healthChecker.getResult(best.id, network);
1644
1662
  if (bestHealth && bestHealth.success === true) {
1645
1663
  this.bestProviderByNetwork.set(network, best.id);
1664
+ this.activeProviderByNetwork.set(network, best.id);
1646
1665
  this.logger.debug(
1647
1666
  `Best provider for ${network}: ${best.id} (score: ${scored[0].score.toFixed(2)})`
1648
1667
  );
1649
1668
  } else {
1669
+ this.activeProviderByNetwork.set(network, best.id);
1650
1670
  this.logger.debug(
1651
1671
  `Best provider for ${network}: ${best.id} (score: ${scored[0].score.toFixed(2)}, untested)`
1652
1672
  );
@@ -1786,9 +1806,16 @@ var ProviderSelector = class {
1786
1806
  }
1787
1807
  /**
1788
1808
  * Clear cached best providers (forces recalculation)
1809
+ * @param network - Optional network to clear cache for. If not provided, clears all networks.
1789
1810
  */
1790
- clearCache() {
1791
- this.bestProviderByNetwork.clear();
1811
+ clearCache(network) {
1812
+ if (network) {
1813
+ this.bestProviderByNetwork.delete(network);
1814
+ this.activeProviderByNetwork.delete(network);
1815
+ } else {
1816
+ this.bestProviderByNetwork.clear();
1817
+ this.activeProviderByNetwork.clear();
1818
+ }
1792
1819
  }
1793
1820
  /**
1794
1821
  * Update best provider after health check
@@ -1803,8 +1830,16 @@ var ProviderSelector = class {
1803
1830
  if (this.bestProviderByNetwork.get(network) === providerId) {
1804
1831
  this.bestProviderByNetwork.delete(network);
1805
1832
  }
1833
+ this.activeProviderByNetwork.delete(network);
1806
1834
  return this.getNextProvider(network, [providerId]);
1807
1835
  }
1836
+ /**
1837
+ * Get the currently active provider ID for a network
1838
+ * (the one that was last selected and is being used)
1839
+ */
1840
+ getActiveProviderId(network) {
1841
+ return this.activeProviderByNetwork.get(network) || null;
1842
+ }
1808
1843
  // ========================================================================
1809
1844
  // Info
1810
1845
  // ========================================================================
@@ -2041,6 +2076,7 @@ var _ProviderManager = class _ProviderManager {
2041
2076
  this.options.logger.warn("No providers available, using fallback");
2042
2077
  return this.getFallbackEndpoint();
2043
2078
  }
2079
+ this.selector.getActiveProviderId(this.network) || this.selector.getBestProvider(this.network);
2044
2080
  if (provider.isDynamic && provider.type === "orbs") {
2045
2081
  try {
2046
2082
  const { getHttpEndpoint } = await import('@orbs-network/ton-access');
@@ -2056,9 +2092,14 @@ var _ProviderManager = class _ProviderManager {
2056
2092
  * Get endpoint with rate limiting
2057
2093
  *
2058
2094
  * Waits for rate limit token before returning endpoint.
2095
+ *
2096
+ * @param forceRefresh - If true, clears cache and forces re-selection
2059
2097
  */
2060
- async getEndpointWithRateLimit(timeoutMs) {
2098
+ async getEndpointWithRateLimit(timeoutMs, forceRefresh = false) {
2061
2099
  this.ensureInitialized();
2100
+ if (forceRefresh) {
2101
+ this.selector.clearCache(this.network);
2102
+ }
2062
2103
  const provider = this.selector.getBestProvider(this.network);
2063
2104
  if (!provider) {
2064
2105
  return this.getFallbackEndpoint();
@@ -2110,7 +2151,14 @@ var _ProviderManager = class _ProviderManager {
2110
2151
  */
2111
2152
  reportError(error) {
2112
2153
  if (!this.initialized || !this.network) return;
2113
- const provider = this.selector.getBestProvider(this.network);
2154
+ const activeProviderId = this.selector.getActiveProviderId(this.network);
2155
+ let provider = null;
2156
+ if (activeProviderId) {
2157
+ provider = this.registry.getProvider(activeProviderId) || null;
2158
+ }
2159
+ if (!provider) {
2160
+ provider = this.selector.getBestProvider(this.network);
2161
+ }
2114
2162
  if (!provider) return;
2115
2163
  const errorMsg = error instanceof Error ? error.message : String(error);
2116
2164
  const errorMsgLower = errorMsg.toLowerCase();
@@ -2129,6 +2177,7 @@ var _ProviderManager = class _ProviderManager {
2129
2177
  this.rateLimiter.reportError(provider.id);
2130
2178
  this.healthChecker.markDegraded(provider.id, this.network, errorMsg);
2131
2179
  }
2180
+ this.selector.clearCache(this.network);
2132
2181
  this.selector.handleProviderFailure(provider.id, this.network);
2133
2182
  this.notifyListeners();
2134
2183
  }