Skip to content

Commit

Permalink
fix: handle NOPERM error for monitor
Browse files Browse the repository at this point in the history
Closes #1498
  • Loading branch information
luin committed Mar 31, 2022
1 parent ac00a00 commit 93b873d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/Redis.ts
Expand Up @@ -364,14 +364,15 @@ class Redis extends Commander {
* });
* ```
*/
monitor(callback: Callback<Redis>): Promise<Redis> {
monitor(callback?: Callback<Redis>): Promise<Redis> {
const monitorInstance = this.duplicate({
monitor: true,
lazyConnect: false,
});

return asCallback(
new Promise(function (resolve) {
new Promise(function (resolve, reject) {
monitorInstance.once("error", reject);
monitorInstance.once("monitoring", function () {
resolve(monitorInstance);
});
Expand Down Expand Up @@ -440,16 +441,15 @@ class Redis extends Commander {

if (!writable) {
if (!this.options.enableOfflineQueue) {
command.reject(new Error(
"Stream isn't writeable and enableOfflineQueue options is false"
));
command.reject(
new Error(
"Stream isn't writeable and enableOfflineQueue options is false"
)
);
return command.promise;
}

if (
command.name === "quit" &&
this.offlineQueue.length === 0
) {
if (command.name === "quit" && this.offlineQueue.length === 0) {
this.disconnect();
command.resolve(Buffer.from("OK"));
return command.promise;
Expand Down
6 changes: 4 additions & 2 deletions lib/redis/event_handler.ts
Expand Up @@ -230,7 +230,10 @@ export function readyHandler(self) {
self.retryAttempts = 0;

if (self.options.monitor) {
self.call("monitor");
self.call("monitor").then(
() => self.setStatus("monitoring"),
(error: Error) => self.emit("error", error)
);
const { sendCommand } = self;
self.sendCommand = function (command) {
if (Command.checkFlag("VALID_IN_MONITOR_MODE", command.name)) {
Expand All @@ -244,7 +247,6 @@ export function readyHandler(self) {
self.once("close", function () {
delete self.sendCommand;
});
self.setStatus("monitoring");
return;
}
const finalSelect = self.prevCondition
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/debug": "^4.1.5",
"@types/lodash.defaults": "^4.2.6",
"@types/lodash.isarguments": "^3.1.6",
Expand Down
20 changes: 18 additions & 2 deletions test/functional/monitor.ts
@@ -1,7 +1,9 @@
import Redis from "../../lib/Redis";
import { expect } from "chai";
import { expect, use } from "chai";
import * as sinon from "sinon";
import { waitForMonitorReady } from "../helpers/util";
import { getRedisVersion, waitForMonitorReady } from "../helpers/util";

use(require("chai-as-promised"));

describe("monitor", () => {
it("should receive commands", (done) => {
Expand Down Expand Up @@ -74,4 +76,18 @@ describe("monitor", () => {
});
});
});

it("rejects when monitor is disabled", async () => {
const redis = new Redis();
const [major] = await getRedisVersion(redis);
if (major < 6) {
return;
}

await redis.acl("SETUSER", "nomonitor", "reset", "+info", ">123456", "on");

await expect(
new Redis({ username: "nomonitor", password: "123456" }).monitor()
).to.eventually.be.rejectedWith(/NOPERM/);
});
});

0 comments on commit 93b873d

Please sign in to comment.