Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle NOPERM error for monitor #1555

Merged
merged 1 commit into from
Mar 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
20 changes: 18 additions & 2 deletions test/functional/monitor.ts
Original file line number Diff line number Diff line change
@@ -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/);
});
});