unlimit-keys 0.2.2 → 0.2.4

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.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Fetches the least recently used API key from the global pool.
3
- * Automatically updates the key's usage timestamp.
3
+ * Atomically updates the key's usage timestamp.
4
4
  *
5
5
  * @returns The API key to be used.
6
6
  * @throws Will throw an error if no keys are found.
@@ -1 +1 @@
1
- {"version":3,"file":"getLeastUsedKey.d.ts","sourceRoot":"","sources":["../../src/schema/getLeastUsedKey.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,MAAM,CAAC,CAwBvD"}
1
+ {"version":3,"file":"getLeastUsedKey.d.ts","sourceRoot":"","sources":["../../src/schema/getLeastUsedKey.ts"],"names":[],"mappings":"AAYA;;;;;;GAMG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,MAAM,CAAC,CAYvD"}
@@ -12,32 +12,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getLeastUsedKey = getLeastUsedKey;
13
13
  const redis_1 = require("./redis");
14
14
  const KEY_SET = 'API_KEYS';
15
+ const SCRIPT = `
16
+ local key = redis.call('zrange', KEYS[1], 0, 0)[1]
17
+ if key then
18
+ redis.call('zadd', KEYS[1], ARGV[1], key)
19
+ return key
20
+ end
21
+ return nil`;
15
22
  /**
16
23
  * Fetches the least recently used API key from the global pool.
17
- * Automatically updates the key's usage timestamp.
24
+ * Atomically updates the key's usage timestamp.
18
25
  *
19
26
  * @returns The API key to be used.
20
27
  * @throws Will throw an error if no keys are found.
21
28
  */
22
29
  function getLeastUsedKey() {
23
30
  return __awaiter(this, void 0, void 0, function* () {
24
- const currentTime = Date.now() / 1000;
25
- // Atomic Lua script:
26
- // 1. Get the key with the lowest score (least recently used)
27
- // 2. If a key exists, update its score to current time
28
- // 3. Return the key
29
- const script = `
30
- local key = redis.call('zrange', KEYS[1], 0, 0)[1]
31
- if key then
32
- redis.call('zadd', KEYS[1], ARGV[1], key)
33
- return key
34
- else
35
- return nil
36
- end
37
- `;
38
- const key = yield redis_1.redis.eval(script, [KEY_SET], [currentTime]);
31
+ const key = yield redis_1.redis.eval(SCRIPT, [KEY_SET], [Date.now() / 1000]);
39
32
  if (!key) {
40
- throw new Error('No API keys found in Redis. Please check your API_KEYS environment variable and run the sync script.');
33
+ throw new Error('unlimit-keys ERROR: No API keys found in Redis.\n' +
34
+ ' 1. Set your keys in the API_KEYS environment variable (comma or newline separated)\n' +
35
+ ' 2. Run: npx unlimit-keys sync');
41
36
  }
42
37
  return key;
43
38
  });
@@ -1 +1 @@
1
- {"version":3,"file":"getLeastUsedKey.js","sourceRoot":"","sources":["../../src/schema/getLeastUsedKey.ts"],"names":[],"mappings":";;;;;;;;;;;AAWA,0CAwBC;AAnCD,mCAAgC;AAEhC,MAAM,OAAO,GAAG,UAAU,CAAC;AAE3B;;;;;;GAMG;AACH,SAAsB,eAAe;;QACjC,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;QAEtC,qBAAqB;QACrB,6DAA6D;QAC7D,uDAAuD;QACvD,oBAAoB;QACpB,MAAM,MAAM,GAAG;;;;;;;;KAQd,CAAC;QAEF,MAAM,GAAG,GAAG,MAAM,aAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,WAAW,CAAC,CAAkB,CAAC;QAEhF,IAAI,CAAC,GAAG,EAAE,CAAC;YACP,MAAM,IAAI,KAAK,CAAC,sGAAsG,CAAC,CAAC;QAC5H,CAAC;QAED,OAAO,GAAG,CAAC;IACf,CAAC;CAAA"}
1
+ {"version":3,"file":"getLeastUsedKey.js","sourceRoot":"","sources":["../../src/schema/getLeastUsedKey.ts"],"names":[],"mappings":";;;;;;;;;;;AAmBA,0CAYC;AA/BD,mCAAgC;AAEhC,MAAM,OAAO,GAAG,UAAU,CAAC;AAE3B,MAAM,MAAM,GAAG;;;;;;WAMJ,CAAC;AAEZ;;;;;;GAMG;AACH,SAAsB,eAAe;;QACjC,MAAM,GAAG,GAAG,MAAM,aAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAkB,CAAC;QAEtF,IAAI,CAAC,GAAG,EAAE,CAAC;YACP,MAAM,IAAI,KAAK,CACX,mDAAmD;gBACnD,wFAAwF;gBACxF,iCAAiC,CACpC,CAAC;QACN,CAAC;QAED,OAAO,GAAG,CAAC;IACf,CAAC;CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unlimit-keys",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "An API key rotation system using Redis",
5
5
  "author": "Anirban Majumder <anirbanmajumder.404@gmail.com>",
6
6
  "repository": {
@@ -48,21 +48,16 @@
48
48
  "node": ">=18"
49
49
  },
50
50
  "dependencies": {
51
- "@upstash/redis": ">=1.0.0",
52
51
  "commander": "^14.0.0",
53
52
  "dotenv": "^17.2.3"
54
53
  },
55
54
  "devDependencies": {
56
55
  "@types/node": "^24.0.15",
56
+ "@upstash/redis": ">=1.0.0",
57
57
  "typescript": "^5.8.3",
58
58
  "vitest": "^4.0.17"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "@upstash/redis": ">=1.0.0"
62
- },
63
- "peerDependenciesMeta": {
64
- "@upstash/redis": {
65
- "optional": true
66
- }
67
62
  }
68
63
  }