@medialane/sdk 0.85.11 → 0.85.12

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
@@ -1281,6 +1281,32 @@ function createFailoverFetch(urls, options = {}) {
1281
1281
  return failover;
1282
1282
  }
1283
1283
 
1284
+ // src/server/rate-limit.ts
1285
+ function createRateLimiter(windowMs, max) {
1286
+ const counts = /* @__PURE__ */ new Map();
1287
+ let nextSweepAt = Date.now() + windowMs;
1288
+ return function checkRateLimit(key) {
1289
+ const now = Date.now();
1290
+ if (now >= nextSweepAt) {
1291
+ for (const [k, entry2] of counts) {
1292
+ if (now >= entry2.resetAt) counts.delete(k);
1293
+ }
1294
+ nextSweepAt = now + windowMs;
1295
+ }
1296
+ const entry = counts.get(key);
1297
+ if (!entry || now >= entry.resetAt) {
1298
+ counts.set(key, { count: 1, resetAt: now + windowMs });
1299
+ return true;
1300
+ }
1301
+ if (entry.count >= max) return false;
1302
+ entry.count += 1;
1303
+ return true;
1304
+ };
1305
+ }
1306
+ function requestIp(req) {
1307
+ return req.headers.get("x-forwarded-for")?.split(",")[0].trim() ?? "unknown";
1308
+ }
1309
+
1284
1310
  // src/server/rpc-proxy.ts
1285
1311
  var DEFAULT_STARKNET_RPC_METHODS = [
1286
1312
  "starknet_call",
@@ -1359,8 +1385,7 @@ function createRpcProxyHandler(config) {
1359
1385
  if (!isSameOrigin(req)) {
1360
1386
  return rpcError(-32600, "Cross-origin requests are not allowed", 403);
1361
1387
  }
1362
- const ip = req.headers.get("x-forwarded-for")?.split(",")[0].trim() ?? "unknown";
1363
- if (!config.checkRateLimit(ip)) {
1388
+ if (!config.checkRateLimit(requestIp(req))) {
1364
1389
  return rpcError(-32005, "Too many requests", 429);
1365
1390
  }
1366
1391
  let body;
@@ -1558,6 +1583,7 @@ exports.chainSlug = chainSlug;
1558
1583
  exports.coinHref = coinHref;
1559
1584
  exports.collectionHref = collectionHref;
1560
1585
  exports.createFailoverFetch = createFailoverFetch;
1586
+ exports.createRateLimiter = createRateLimiter;
1561
1587
  exports.createRpcProxyHandler = createRpcProxyHandler;
1562
1588
  exports.encodeU256 = encodeU256;
1563
1589
  exports.formatAmount = formatAmount;
@@ -1576,6 +1602,7 @@ exports.listServices = listServices;
1576
1602
  exports.normalizeAddress = normalizeAddress;
1577
1603
  exports.normalizeHash = normalizeHash;
1578
1604
  exports.parseAmount = parseAmount;
1605
+ exports.requestIp = requestIp;
1579
1606
  exports.resolveConfig = resolveConfig;
1580
1607
  exports.resolveFeeConfig = resolveFeeConfig;
1581
1608
  exports.resolveSafeImageContentType = resolveSafeImageContentType;