Skip to content

Commit 93b873d

Browse files
committedMar 31, 2022
fix: handle NOPERM error for monitor
Closes #1498
1 parent ac00a00 commit 93b873d

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed
 

‎lib/Redis.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,15 @@ class Redis extends Commander {
364364
* });
365365
* ```
366366
*/
367-
monitor(callback: Callback<Redis>): Promise<Redis> {
367+
monitor(callback?: Callback<Redis>): Promise<Redis> {
368368
const monitorInstance = this.duplicate({
369369
monitor: true,
370370
lazyConnect: false,
371371
});
372372

373373
return asCallback(
374-
new Promise(function (resolve) {
374+
new Promise(function (resolve, reject) {
375+
monitorInstance.once("error", reject);
375376
monitorInstance.once("monitoring", function () {
376377
resolve(monitorInstance);
377378
});
@@ -440,16 +441,15 @@ class Redis extends Commander {
440441

441442
if (!writable) {
442443
if (!this.options.enableOfflineQueue) {
443-
command.reject(new Error(
444-
"Stream isn't writeable and enableOfflineQueue options is false"
445-
));
444+
command.reject(
445+
new Error(
446+
"Stream isn't writeable and enableOfflineQueue options is false"
447+
)
448+
);
446449
return command.promise;
447450
}
448451

449-
if (
450-
command.name === "quit" &&
451-
this.offlineQueue.length === 0
452-
) {
452+
if (command.name === "quit" && this.offlineQueue.length === 0) {
453453
this.disconnect();
454454
command.resolve(Buffer.from("OK"));
455455
return command.promise;

‎lib/redis/event_handler.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ export function readyHandler(self) {
230230
self.retryAttempts = 0;
231231

232232
if (self.options.monitor) {
233-
self.call("monitor");
233+
self.call("monitor").then(
234+
() => self.setStatus("monitoring"),
235+
(error: Error) => self.emit("error", error)
236+
);
234237
const { sendCommand } = self;
235238
self.sendCommand = function (command) {
236239
if (Command.checkFlag("VALID_IN_MONITOR_MODE", command.name)) {
@@ -244,7 +247,6 @@ export function readyHandler(self) {
244247
self.once("close", function () {
245248
delete self.sendCommand;
246249
});
247-
self.setStatus("monitoring");
248250
return;
249251
}
250252
const finalSelect = self.prevCondition

‎package-lock.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@semantic-release/commit-analyzer": "^9.0.2",
5858
"@semantic-release/git": "^10.0.1",
5959
"@types/chai": "^4.3.0",
60+
"@types/chai-as-promised": "^7.1.5",
6061
"@types/debug": "^4.1.5",
6162
"@types/lodash.defaults": "^4.2.6",
6263
"@types/lodash.isarguments": "^3.1.6",

‎test/functional/monitor.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Redis from "../../lib/Redis";
2-
import { expect } from "chai";
2+
import { expect, use } from "chai";
33
import * as sinon from "sinon";
4-
import { waitForMonitorReady } from "../helpers/util";
4+
import { getRedisVersion, waitForMonitorReady } from "../helpers/util";
5+
6+
use(require("chai-as-promised"));
57

68
describe("monitor", () => {
79
it("should receive commands", (done) => {
@@ -74,4 +76,18 @@ describe("monitor", () => {
7476
});
7577
});
7678
});
79+
80+
it("rejects when monitor is disabled", async () => {
81+
const redis = new Redis();
82+
const [major] = await getRedisVersion(redis);
83+
if (major < 6) {
84+
return;
85+
}
86+
87+
await redis.acl("SETUSER", "nomonitor", "reset", "+info", ">123456", "on");
88+
89+
await expect(
90+
new Redis({ username: "nomonitor", password: "123456" }).monitor()
91+
).to.eventually.be.rejectedWith(/NOPERM/);
92+
});
7793
});

0 commit comments

Comments
 (0)
Please sign in to comment.