Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

strategy:
matrix:
node-version: [20.x, 22.x]
node-version: [20.x, 22.x, 24.x]

steps:
- name: Check out repository code
Expand Down
2 changes: 1 addition & 1 deletion __tests__/locky.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Locky", () => {
if (locky) {
await locky.close();
}
await testRedis.quit();
await testRedis.close();
});

describe("#lock", () => {
Expand Down
31 changes: 21 additions & 10 deletions lib/locky.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const resourceKey = require("./resource-key");
* @typedef RedisPromises
* @property {ReturnType<import('util').promisify<RedisClient["get"]>>} get
* @property {ReturnType<import('util').promisify<RedisClient["set"]>>} set
* @property {ReturnType<import('util').promisify<RedisClient["sAdd"]>>} sAdd
* @property {ReturnType<import('util').promisify<RedisClient["pexpire"]>>} pexpire
* @property {ReturnType<import('util').promisify<RedisClient["quit"]>>} quit
* @property {ReturnType<import('util').promisify<RedisClient["smembers"]>>} smembers
Expand Down Expand Up @@ -159,7 +160,7 @@ class Locky extends EventEmitter {

const [, setResult] = await trx.exec();

const success = setResult !== null && setResult !== 0;
const success = setResult !== null;

if (success) {
await this.asyncEmit("lock", resource, locker);
Expand Down Expand Up @@ -199,7 +200,9 @@ class Locky extends EventEmitter {
const key = resourceKey.format(this.prefix, resource);

// Set the TTL of the key.
return this.redis.pExpire(key, this.ttl);
const expire = await this.redis.pExpire(key, this.ttl);

return expire === 1;
});
}

Expand Down Expand Up @@ -234,7 +237,7 @@ class Locky extends EventEmitter {
trx.del(key);
const [, res] = await trx.exec();

const success = res !== 0;
const success = /** @type {number} */ (/** @type {any} */ (res)) !== 0;

if (success) {
await this.asyncEmit("unlock", resource);
Expand Down Expand Up @@ -302,7 +305,10 @@ class Locky extends EventEmitter {
if (this.expirationWorker?.timeout) {
clearTimeout(this.expirationWorker.timeout);
}
await Promise.all([this.redis.quit(), this.expirationWorker?.redis.quit()]);
await Promise.all([
this.redis.close(),
this.expirationWorker?.redis.close(),
]);
}

/**
Expand Down Expand Up @@ -340,9 +346,12 @@ class Locky extends EventEmitter {
this.asyncEmit("error", error);
}

this.expirationWorker.timeout = setTimeout(() => {
run();
}, /** @type {number} */ (this.ttl) / 10);
this.expirationWorker.timeout = setTimeout(
() => {
run();
},
/** @type {number} */ (this.ttl) / 10,
);
};

run();
Expand All @@ -362,7 +371,9 @@ class Locky extends EventEmitter {

const ttlBatch = redisClient.multi();
keys.forEach((key) => ttlBatch.pTTL(key));
const ttls = await ttlBatch.exec();
const ttls = /** @type {number[]} */ (
/** @type {any[]} */ (await ttlBatch.exec())
);

const keysToExpire = keys.filter((_, index) => ttls[index] === -2);

Expand All @@ -380,8 +391,8 @@ class Locky extends EventEmitter {

await Promise.all(
keysToExpire.map((key) =>
this.asyncEmit("expire", resourceKey.parse(this.prefix, key))
)
this.asyncEmit("expire", resourceKey.parse(this.prefix, key)),
),
);
}
}
Expand Down
167 changes: 73 additions & 94 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"standard-version": "^9.5.0",
"typescript": "^4.7.4"
},
"dependencies": {
"redis": "^4.7.0"
},
"engines": {
"node": ">= 12.9.0"
},
"dependencies": {
"redis": "^5.10.0"
}
}