@blocklet/rate-limit 1.16.45-beta-20250630-024910-24ef8c24 → 1.16.45-beta-20250701-122807-92b03aba

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 +11 -2
  2. package/package.json +4 -3
package/lib/index.js CHANGED
@@ -12,6 +12,7 @@ exports.createRateLimiter = createRateLimiter;
12
12
  const db_cache_1 = require("@abtnode/db-cache");
13
13
  const express_rate_limit_1 = require("express-rate-limit");
14
14
  const node_path_1 = __importDefault(require("node:path"));
15
+ const md5_1 = __importDefault(require("@abtnode/util/lib/md5"));
15
16
  /**
16
17
  * Store for rate limit
17
18
  */
@@ -19,7 +20,7 @@ class RateLimitStore {
19
20
  constructor(options) {
20
21
  const serverUrls = (0, db_cache_1.getAbtNodeRedisAndSQLiteUrl)();
21
22
  this.dbCache = new db_cache_1.DBCache(() => ({
22
- prefix: `rate-limit-${options.prefix || 'default'}`,
23
+ prefix: 'rate',
23
24
  ttl: options.windowMs || 1000 * 60,
24
25
  redisUrl: options.redisUrl || serverUrls.redisUrl,
25
26
  sqlitePath: options.sqlitePath || serverUrls.sqlitePath || node_path_1.default.join(process.env.BLOCKLET_DATA_DIR, 'rate-limit.db'),
@@ -124,7 +125,15 @@ function createRateLimiter(options) {
124
125
  windowMs: 1000 * 60,
125
126
  limit: 10,
126
127
  legacyHeaders: false,
127
- keyGenerator: getIp,
128
128
  ...options,
129
+ keyGenerator: (req, res) => {
130
+ const key = options.keyGenerator ? options.keyGenerator(req, res) : getIp(req);
131
+ if (!key)
132
+ return null;
133
+ // @ts-ignore
134
+ const did = req.getBlockletDid ? req.getBlockletDid() : 'blocklet';
135
+ const prefix = options.prefix || 'prefix';
136
+ return (0, md5_1.default)(`${did}:${prefix}:${key}`);
137
+ },
129
138
  });
130
139
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.45-beta-20250630-024910-24ef8c24",
6
+ "version": "1.16.45-beta-20250701-122807-92b03aba",
7
7
  "description": "rate limit middleware",
8
8
  "main": "lib/index.js",
9
9
  "typings": "lib/index.d.ts",
@@ -25,7 +25,8 @@
25
25
  "author": "blocklet<blocklet@arcblock.io>",
26
26
  "license": "Apache-2.0",
27
27
  "dependencies": {
28
- "@abtnode/db-cache": "1.16.45-beta-20250630-024910-24ef8c24",
28
+ "@abtnode/db-cache": "1.16.45-beta-20250701-122807-92b03aba",
29
+ "@abtnode/util": "1.16.45-beta-20250701-122807-92b03aba",
29
30
  "express-rate-limit": "^7.5.1"
30
31
  },
31
32
  "devDependencies": {
@@ -45,5 +46,5 @@
45
46
  "ts-node": "^10.9.1",
46
47
  "typescript": "^5.6.3"
47
48
  },
48
- "gitHead": "a619af43bcf85d6d0c9fcba96c4fa1f04d4b80c4"
49
+ "gitHead": "f4fc6b6a7d37bd24a2e2493fbad46045cbb7ea81"
49
50
  }