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 30, 2022
1 parent 92e7cd9 commit a7fd8c4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
18 changes: 9 additions & 9 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,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 @@ -443,16 +444,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
17 changes: 16 additions & 1 deletion test/functional/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Redis from "../../lib/Redis";
import { expect } from "chai";
import { expect, use } from "chai";
import * as sinon from "sinon";
import { waitForMonitorReady } from "../helpers/util";

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

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

it("rejects when monitor is disabled", async () => {
await new Redis().acl(
"SETUSER",
"nomonitor",
"reset",
"+info",
">123456",
"on"
);
const redis = new Redis({ username: "nomonitor", password: "123456" });
expect(redis.monitor()).to.eventually.be.rejectedWith(/NOPERM/);
});
});

0 comments on commit a7fd8c4

Please sign in to comment.