unlimit-keys 0.2.1 → 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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getLeastUsedKey.d.ts","sourceRoot":"","sources":["../../src/schema/getLeastUsedKey.ts"],"names":[],"mappings":"
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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":";;;;;;;;;;;
|
|
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.
|
|
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": {
|
|
@@ -41,29 +41,23 @@
|
|
|
41
41
|
"dev": "tsc --watch",
|
|
42
42
|
"test": "vitest run",
|
|
43
43
|
"sync": "node dist/scripts/sync-api-keys.js",
|
|
44
|
-
"prepublishOnly": "npm run build && npm run test"
|
|
45
|
-
"publish": "npm publish --access public"
|
|
44
|
+
"prepublishOnly": "npm run build && npm run test"
|
|
46
45
|
},
|
|
47
46
|
"type": "commonjs",
|
|
48
47
|
"engines": {
|
|
49
48
|
"node": ">=18"
|
|
50
49
|
},
|
|
51
50
|
"dependencies": {
|
|
52
|
-
"@upstash/redis": ">=1.0.0",
|
|
53
51
|
"commander": "^14.0.0",
|
|
54
52
|
"dotenv": "^17.2.3"
|
|
55
53
|
},
|
|
56
54
|
"devDependencies": {
|
|
57
55
|
"@types/node": "^24.0.15",
|
|
56
|
+
"@upstash/redis": ">=1.0.0",
|
|
58
57
|
"typescript": "^5.8.3",
|
|
59
58
|
"vitest": "^4.0.17"
|
|
60
59
|
},
|
|
61
60
|
"peerDependencies": {
|
|
62
61
|
"@upstash/redis": ">=1.0.0"
|
|
63
|
-
},
|
|
64
|
-
"peerDependenciesMeta": {
|
|
65
|
-
"@upstash/redis": {
|
|
66
|
-
"optional": true
|
|
67
|
-
}
|
|
68
62
|
}
|
|
69
63
|
}
|