@alleyboss/micropay-solana-x402-paywall 3.2.2 → 3.3.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.
@@ -4,12 +4,10 @@ function lamportsToSol(lamports) {
4
4
  }
5
5
 
6
6
  // src/pricing/index.ts
7
- var cachedPrice = null;
8
- var config = {};
9
- var lastProviderIndex = -1;
7
+ var priceCache = null;
8
+ var currentConfig = {};
10
9
  function configurePricing(newConfig) {
11
- config = { ...config, ...newConfig };
12
- cachedPrice = null;
10
+ currentConfig = { ...currentConfig, ...newConfig };
13
11
  }
14
12
  var PROVIDERS = [
15
13
  {
@@ -49,56 +47,75 @@ async function fetchFromProvider(provider, timeout) {
49
47
  if (!price || price <= 0) {
50
48
  throw new Error("Invalid price");
51
49
  }
52
- return price;
50
+ return { price, source: provider.name };
53
51
  } finally {
54
52
  clearTimeout(timeoutId);
55
53
  }
56
54
  }
55
+ async function fetchPriceParallel(timeout) {
56
+ const promises = PROVIDERS.map(
57
+ (provider) => fetchFromProvider(provider, timeout).catch(() => null)
58
+ );
59
+ const results = await Promise.all(promises);
60
+ const validResult = results.find((r) => r !== null);
61
+ if (validResult) {
62
+ return validResult;
63
+ }
64
+ throw new Error("All providers failed");
65
+ }
66
+ async function fetchPriceSequential(timeout) {
67
+ for (const provider of PROVIDERS) {
68
+ try {
69
+ return await fetchFromProvider(provider, timeout);
70
+ } catch {
71
+ continue;
72
+ }
73
+ }
74
+ throw new Error("All providers failed");
75
+ }
57
76
  async function getSolPrice() {
58
- const cacheTTL = config.cacheTTL ?? 6e4;
59
- const timeout = config.timeout ?? 5e3;
60
- if (cachedPrice && Date.now() - cachedPrice.fetchedAt.getTime() < cacheTTL) {
61
- return cachedPrice;
77
+ const cacheTTL = currentConfig.cacheTTL ?? 6e4;
78
+ const timeout = currentConfig.timeout ?? 3e3;
79
+ const useParallel = currentConfig.parallelFetch ?? true;
80
+ const now = Date.now();
81
+ if (priceCache && now - priceCache.timestamp < cacheTTL) {
82
+ return priceCache.data;
62
83
  }
63
- if (config.customProvider) {
84
+ if (currentConfig.customProvider) {
64
85
  try {
65
- const price = await config.customProvider();
86
+ const price = await currentConfig.customProvider();
66
87
  if (price > 0) {
67
- cachedPrice = {
88
+ const data = {
68
89
  solPrice: price,
69
90
  fetchedAt: /* @__PURE__ */ new Date(),
70
91
  source: "custom"
71
92
  };
72
- return cachedPrice;
93
+ priceCache = { data, timestamp: now };
94
+ return data;
73
95
  }
74
96
  } catch {
75
97
  }
76
98
  }
77
- for (let i = 0; i < PROVIDERS.length; i++) {
78
- const idx = (lastProviderIndex + 1 + i) % PROVIDERS.length;
79
- const provider = PROVIDERS[idx];
80
- try {
81
- const price = await fetchFromProvider(provider, timeout);
82
- lastProviderIndex = idx;
83
- cachedPrice = {
84
- solPrice: price,
85
- fetchedAt: /* @__PURE__ */ new Date(),
86
- source: provider.name
99
+ try {
100
+ const result = useParallel ? await fetchPriceParallel(timeout) : await fetchPriceSequential(timeout);
101
+ const data = {
102
+ solPrice: result.price,
103
+ fetchedAt: /* @__PURE__ */ new Date(),
104
+ source: result.source
105
+ };
106
+ priceCache = { data, timestamp: now };
107
+ return data;
108
+ } catch {
109
+ if (priceCache) {
110
+ return {
111
+ ...priceCache.data,
112
+ source: `${priceCache.data.source} (stale)`
87
113
  };
88
- return cachedPrice;
89
- } catch {
90
- continue;
91
114
  }
115
+ throw new Error(
116
+ "Failed to fetch SOL price from all providers. Configure a custom provider or ensure network connectivity."
117
+ );
92
118
  }
93
- if (cachedPrice) {
94
- return {
95
- ...cachedPrice,
96
- source: `${cachedPrice.source} (stale)`
97
- };
98
- }
99
- throw new Error(
100
- "Failed to fetch SOL price from all providers. Configure a custom provider or ensure network connectivity."
101
- );
102
119
  }
103
120
  async function lamportsToUsd(lamports) {
104
121
  const { solPrice } = await getSolPrice();
@@ -126,8 +143,7 @@ function formatPriceSync(lamports, solPrice) {
126
143
  };
127
144
  }
128
145
  function clearPriceCache() {
129
- cachedPrice = null;
130
- lastProviderIndex = -1;
146
+ priceCache = null;
131
147
  }
132
148
  function getProviders() {
133
149
  return PROVIDERS.map((p) => ({ name: p.name, url: p.url }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alleyboss/micropay-solana-x402-paywall",
3
- "version": "3.2.2",
3
+ "version": "3.3.1",
4
4
  "description": "Production-ready Solana micropayments library wrapper for official x402 SDK",
5
5
  "author": "AlleyBoss",
6
6
  "license": "MIT",
@@ -135,4 +135,4 @@
135
135
  "engines": {
136
136
  "node": ">=18"
137
137
  }
138
- }
138
+ }