Skip to content

Commit 3810dc5

Browse files
committed
support redis 5
1 parent 2757317 commit 3810dc5

File tree

4 files changed

+60
-78
lines changed

4 files changed

+60
-78
lines changed

package-lock.json

Lines changed: 53 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
"repository": "https://github.com/AmrSaber/simple-redis-mutex",
2222
"license": "ISC",
2323
"peerDependencies": {
24-
"redis": ">=4.7.0"
24+
"redis": ">=5.0.0"
2525
},
2626
"devDependencies": {
2727
"@types/jest": "^26.0.24",
28-
"@types/redis": "^4.0.10",
2928
"eslint": "^7.25.0",
3029
"eslint-config-airbnb-base": "^14.2.1",
3130
"eslint-config-prettier": "^9.0.0",
3231
"eslint-plugin-import": "^2.22.1",
3332
"jest": "^29.7.0",
3433
"prettier": "^3.0.3",
34+
"redis": "^5.0.1",
3535
"ts-jest": "^29.2.5",
3636
"typescript": "^5.7.2"
3737
},

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RedisClientType, RedisClusterType } from 'redis';
22

3-
type RedisClient = RedisClientType<any, any, any> | RedisClusterType<any, any, any>;
3+
type RedisClient = RedisClientType<any, any, any, any> | RedisClusterType<any, any, any, any>;
44

55
type ReleaseCallbackFn = () => void;
66
type ReleaseCallback = { lockKey: string; callback: ReleaseCallbackFn };
@@ -198,7 +198,7 @@ async function listenForUpdates(redis: RedisClient) {
198198

199199
redis.on('end', async () => {
200200
await subscriber?.unsubscribe(REDIS_RELEASES_CHANNEL);
201-
await subscriber?.quit();
201+
await subscriber?.close();
202202
subscriber = undefined;
203203
});
204204
}
@@ -208,7 +208,6 @@ function getLockKey(lockName: string): string {
208208
}
209209

210210
function isRedisClient(redis: RedisClient): redis is RedisClientType<any, any, any> {
211-
// @ts-expect-error script load does not exist on redis cluster
212211
return typeof redis.scriptLoad == 'function';
213212
}
214213

unit.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createClient, RedisClientType } from 'redis';
22
import { lock, LockOptions, tryLock, TryLockOptions } from './src';
33

44
describe('Lock tests', () => {
5-
let redis: RedisClientType<any, any, any>;
5+
let redis: RedisClientType<any, any, any, any>;
66
const lockName = '_test_lock';
77

88
function sleep(millis?: number): Promise<void> {
@@ -11,7 +11,7 @@ describe('Lock tests', () => {
1111

1212
beforeAll(async () => {
1313
redis = await createClient({ url: process.env.REDIS_URI })
14-
.on('error', (err) => console.log('Redis Client Error', err))
14+
.on('error', (err) => console.error('Redis Client Error', err))
1515
.connect();
1616

1717
await redis.select(7); // So that main db is not updated and later flushed
@@ -52,6 +52,7 @@ describe('Lock tests', () => {
5252
[hasLock, release] = await tryLock(redis, lockName);
5353
expect(hasLock).toEqual(true);
5454

55+
// @ts-ignore
5556
redis.scriptLoad = scriptLoad;
5657
});
5758

0 commit comments

Comments
 (0)