@blocklet/rate-limit 1.16.45-beta-20250626-115702-00b49b4c → 1.16.45-beta-20250628-221915-2d6b9a34

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.
Files changed (2) hide show
  1. package/lib/index.js +13 -1
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -22,11 +22,13 @@ class RateLimitStore {
22
22
  prefix: `rate-limit-${options.prefix || 'default'}`,
23
23
  ttl: options.windowMs || 1000 * 60,
24
24
  redisUrl: options.redisUrl || serverUrls.redisUrl,
25
- sqlitePath: options.sqlitePath || serverUrls.sqlitePath || node_path_1.default.join(process.env.BLOCKLET_APP_DATA_DIR, 'rate-limit.db'),
25
+ sqlitePath: options.sqlitePath || serverUrls.sqlitePath || node_path_1.default.join(process.env.BLOCKLET_DATA_DIR, 'rate-limit.db'),
26
26
  }));
27
27
  this.windowMs = options.windowMs;
28
28
  }
29
29
  async get(key) {
30
+ if (!key)
31
+ return null;
30
32
  try {
31
33
  const row = (await this.dbCache.get(key));
32
34
  return row
@@ -42,6 +44,8 @@ class RateLimitStore {
42
44
  }
43
45
  }
44
46
  async set(key, limitInfo) {
47
+ if (!key)
48
+ return;
45
49
  try {
46
50
  await this.dbCache.set(key, {
47
51
  totalHits: limitInfo.totalHits,
@@ -57,6 +61,8 @@ class RateLimitStore {
57
61
  totalHits: 1,
58
62
  resetTime: new Date(Date.now() + this.windowMs),
59
63
  };
64
+ if (!key)
65
+ return defaultLimitInfo;
60
66
  try {
61
67
  let limitInfo = await this.get(key);
62
68
  // reset row if expired
@@ -75,6 +81,8 @@ class RateLimitStore {
75
81
  }
76
82
  }
77
83
  async decrement(key) {
84
+ if (!key)
85
+ return;
78
86
  try {
79
87
  const row = await this.get(key);
80
88
  if (row) {
@@ -98,6 +106,9 @@ class RateLimitStore {
98
106
  }
99
107
  }
100
108
  exports.RateLimitStore = RateLimitStore;
109
+ function getIp(req) {
110
+ return req.headers?.['x-real-ip'] || req.ip || req._remoteAddress || (req.connection && req.connection.remoteAddress);
111
+ }
101
112
  /**
102
113
  * express-rate-limit middleware with custom store
103
114
  */
@@ -113,6 +124,7 @@ function createRateLimiter(options) {
113
124
  windowMs: 1000 * 60,
114
125
  limit: 10,
115
126
  legacyHeaders: false,
127
+ keyGenerator: getIp,
116
128
  ...options,
117
129
  });
118
130
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.45-beta-20250626-115702-00b49b4c",
6
+ "version": "1.16.45-beta-20250628-221915-2d6b9a34",
7
7
  "description": "rate limit middleware",
8
8
  "main": "lib/index.js",
9
9
  "typings": "lib/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "author": "blocklet<blocklet@arcblock.io>",
26
26
  "license": "Apache-2.0",
27
27
  "dependencies": {
28
- "@abtnode/db-cache": "1.16.45-beta-20250626-115702-00b49b4c",
28
+ "@abtnode/db-cache": "1.16.45-beta-20250628-221915-2d6b9a34",
29
29
  "express-rate-limit": "^7.5.1"
30
30
  },
31
31
  "devDependencies": {
@@ -45,5 +45,5 @@
45
45
  "ts-node": "^10.9.1",
46
46
  "typescript": "^5.6.3"
47
47
  },
48
- "gitHead": "8a70f88f034e2926185d4d0b9503ab8430a772a1"
48
+ "gitHead": "af045bf41dea3533252fc097214cfbe8c1f2c756"
49
49
  }