ton-provider-system 0.2.0 → 0.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 CHANGED
@@ -753,17 +753,43 @@ var OnFinalityProvider = class extends BaseProvider {
753
753
  if (normalized.includes("onfinality.io")) {
754
754
  try {
755
755
  const url = new URL(normalized);
756
- const baseUrl = normalized.split("?")[0];
756
+ const hasQueryParams = normalized.includes("?");
757
+ const queryString = hasQueryParams ? normalized.split("?")[1] : "";
758
+ const hasApiKeyInQuery = queryString.includes("apikey=");
757
759
  if (!url.pathname || url.pathname === "/") {
758
760
  if (this.provider.apiKey) {
759
- return baseUrl.replace(/\/?$/, "/rpc");
761
+ const baseUrl2 = normalized.split("?")[0];
762
+ if (hasApiKeyInQuery) {
763
+ return normalized.replace(/\/?$/, "/rpc");
764
+ }
765
+ if (hasQueryParams) {
766
+ return `${baseUrl2.replace(/\/?$/, "/rpc")}?${queryString}&apikey=${encodeURIComponent(this.provider.apiKey)}`;
767
+ }
768
+ return `${baseUrl2.replace(/\/?$/, "/rpc")}?apikey=${encodeURIComponent(this.provider.apiKey)}`;
760
769
  }
770
+ const baseUrl = normalized.split("?")[0];
761
771
  return baseUrl.replace(/\/?$/, "/public");
762
772
  }
763
773
  if (url.pathname === "/rpc" || url.pathname === "/public") {
764
- return baseUrl;
774
+ if (url.pathname === "/rpc") {
775
+ if (hasApiKeyInQuery) {
776
+ return normalized;
777
+ }
778
+ if (this.provider.apiKey) {
779
+ const baseUrl = normalized.split("?")[0];
780
+ if (hasQueryParams) {
781
+ return `${baseUrl}?${queryString}&apikey=${encodeURIComponent(this.provider.apiKey)}`;
782
+ }
783
+ return `${baseUrl}?apikey=${encodeURIComponent(this.provider.apiKey)}`;
784
+ }
785
+ return normalized;
786
+ }
787
+ if (url.pathname === "/public") {
788
+ return normalized.split("?")[0];
789
+ }
790
+ return normalized;
765
791
  }
766
- return baseUrl;
792
+ return normalized;
767
793
  } catch {
768
794
  if (normalized.includes("{key}")) {
769
795
  return normalized.split("?")[0].replace(/\/?$/, "/public");
@@ -771,7 +797,7 @@ var OnFinalityProvider = class extends BaseProvider {
771
797
  if (!normalized.includes("/rpc") && !normalized.includes("/public")) {
772
798
  return normalized.split("?")[0] + "/public";
773
799
  }
774
- return normalized.split("?")[0];
800
+ return normalized;
775
801
  }
776
802
  }
777
803
  return normalized;
@@ -780,9 +806,6 @@ var OnFinalityProvider = class extends BaseProvider {
780
806
  const headers = {
781
807
  "Content-Type": "application/json"
782
808
  };
783
- if (this.provider.apiKey) {
784
- headers["apikey"] = this.provider.apiKey;
785
- }
786
809
  return headers;
787
810
  }
788
811
  parseResponse(data) {
@@ -1086,7 +1109,8 @@ var HealthChecker = class {
1086
1109
  } catch (error) {
1087
1110
  if (provider.type === "onfinality" && normalizedEndpoint.includes("/rpc") && provider.apiKey && error.message?.includes("backend error")) {
1088
1111
  this.logger.debug(`OnFinality /rpc failed, retrying with /public endpoint`);
1089
- const publicEndpoint = normalizedEndpoint.replace("/rpc", "/public");
1112
+ const baseUrl = normalizedEndpoint.split("?")[0];
1113
+ const publicEndpoint = baseUrl.replace("/rpc", "/public");
1090
1114
  const publicProvider = { ...provider, apiKey: void 0 };
1091
1115
  const publicProviderImpl = createProvider(publicProvider);
1092
1116
  info = await this.callGetMasterchainInfo(publicEndpoint, publicProvider, publicProviderImpl);
@@ -1235,14 +1259,17 @@ var HealthChecker = class {
1235
1259
  }
1236
1260
  /**
1237
1261
  * Mark a provider as degraded (e.g., on 429 error)
1262
+ *
1263
+ * Providers marked as degraded have failed health checks (e.g., rate limit errors)
1264
+ * and should not be selected. The system will failover to the next available provider.
1238
1265
  */
1239
1266
  markDegraded(providerId, network, error) {
1240
1267
  const key = this.getResultKey(providerId, network);
1241
1268
  const existing = this.results.get(key);
1242
1269
  const result = existing ? {
1243
1270
  ...existing,
1244
- success: true,
1245
- // Degraded providers are still usable, just slower/rate-limited
1271
+ success: false,
1272
+ // Degraded providers with errors should not be selected
1246
1273
  status: "degraded",
1247
1274
  error: error || "Marked as degraded",
1248
1275
  lastTested: /* @__PURE__ */ new Date(),
@@ -1250,8 +1277,8 @@ var HealthChecker = class {
1250
1277
  } : {
1251
1278
  id: providerId,
1252
1279
  network,
1253
- success: true,
1254
- // Degraded providers are still usable
1280
+ success: false,
1281
+ // Degraded providers with errors should not be selected
1255
1282
  status: "degraded",
1256
1283
  latencyMs: null,
1257
1284
  seqno: null,