Skip to content
Draft
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
4 changes: 4 additions & 0 deletions lib/DataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface Condition {
select: number;
auth?: string | [string, string];
subscriber: false | SubscriptionSet;
/**
* Whether the connection has issued a subscribe command during `connect` or `ready`.
*/
hasIssuedSubscribe: boolean;
}

export type FlushQueueOptions = {
Expand Down
12 changes: 12 additions & 0 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
? [options.username, options.password]
: options.password,
subscriber: false,
hasIssuedSubscribe: false,
};

const _this = this;
Expand Down Expand Up @@ -429,6 +430,17 @@
command.reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return command.promise;
}

// Make sure know that a subscribe command is sent to the server
// In order to prevent race condition by sending another non-subscribe command
// before we've received the response of the previous subscribe command
if (
(this.status === "connect" || this.status === "ready") &&
Command.checkFlag("ENTER_SUBSCRIBER_MODE", command.name)
) {
this.condition.hasIssuedSubscribe = true;
}

if (
this.condition?.subscriber &&
!Command.checkFlag("VALID_IN_SUBSCRIBER_MODE", command.name)
Expand Down Expand Up @@ -560,35 +572,35 @@
});
}

scanStream(options?: ScanStreamOptions) {

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member scanStream should be declared before all private instance method definitions

Check warning on line 575 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member scanStream should be declared before all private instance method definitions
return this.createScanStream("scan", { options });
}

scanBufferStream(options?: ScanStreamOptions) {

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 579 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member scanBufferStream should be declared before all private instance method definitions
return this.createScanStream("scanBuffer", { options });
}

sscanStream(key: string, options?: ScanStreamOptions) {

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 583 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member sscanStream should be declared before all private instance method definitions
return this.createScanStream("sscan", { key, options });
}

sscanBufferStream(key: string, options?: ScanStreamOptions) {

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 587 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member sscanBufferStream should be declared before all private instance method definitions
return this.createScanStream("sscanBuffer", { key, options });
}

hscanStream(key: string, options?: ScanStreamOptions) {

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 591 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member hscanStream should be declared before all private instance method definitions
return this.createScanStream("hscan", { key, options });
}

hscanBufferStream(key: string, options?: ScanStreamOptions) {

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 595 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member hscanBufferStream should be declared before all private instance method definitions
return this.createScanStream("hscanBuffer", { key, options });
}

zscanStream(key: string, options?: ScanStreamOptions) {

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member zscanStream should be declared before all private instance method definitions
return this.createScanStream("zscan", { key, options });
}

zscanBufferStream(key: string, options?: ScanStreamOptions) {

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 603 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member zscanBufferStream should be declared before all private instance method definitions
return this.createScanStream("zscanBuffer", { key, options });
}

Expand All @@ -597,7 +609,7 @@
*
* @ignore
*/
silentEmit(eventName: string, arg?: unknown): boolean {

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 612 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member silentEmit should be declared before all private instance method definitions
let error: unknown;
if (eventName === "error") {
error = arg;
Expand Down Expand Up @@ -632,7 +644,7 @@
/**
* @ignore
*/
recoverFromFatalError(

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.2)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.2)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, 8.4-RC1-pre)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.2)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, 8.4-RC1-pre)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, rs-7.4.0-v1)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (22.x, rs-7.4.0-v1)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (24.x, 8.4-RC1-pre)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 647 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (20.x, rs-7.4.0-v1)

Member recoverFromFatalError should be declared before all private instance method definitions
_commandError: Error,
err: Error,
options: FlushQueueOptions
Expand Down
13 changes: 9 additions & 4 deletions lib/redis/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ export function connectHandler(self) {

const clientCommandPromises = [];

if (self.options.connectionName) {
if (self.options.connectionName && !self.condition.hasIssuedSubscribe) {
debug("set the connection name [%s]", self.options.connectionName);
clientCommandPromises.push(
self.client("setname", self.options.connectionName).catch(noop)
);
}

if (!self.options.disableClientInfo) {
if (!self.options.disableClientInfo && !self.condition.hasIssuedSubscribe) {
debug("set the client info");
clientCommandPromises.push(
getPackageMeta()
Expand Down Expand Up @@ -112,11 +112,16 @@ export function connectHandler(self) {
Promise.all(clientCommandPromises)
.catch(noop)
.finally(() => {
if (!self.options.enableReadyCheck) {
// Ready check should not be performed after a subscribe command
// Because it might result in a race condition
// Additionally because we're using RESP2 another client should be used for normal commands
const shouldReadyCheck = self.options.enableReadyCheck && !self.condition.hasIssuedSubscribe;

if (!shouldReadyCheck) {
exports.readyHandler(self)();
}

if (self.options.enableReadyCheck) {
if (shouldReadyCheck) {
self._readyCheck(function (err, info) {
if (connectionEpoch !== self.connectionEpoch) {
return;
Expand Down
18 changes: 18 additions & 0 deletions test/functional/pub_sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,22 @@ describe("pub/sub", function () {
expect(await redis.set("foo", "bar")).to.eql("OK");
redis.disconnect();
});

it("should subscribe on connect without errors", (done) => {
const redis = new Redis();

redis.on("error", () => {
throw new Error("should not error");
});

redis.on("connect", async () => {
await redis.subscribe("foo");
redis.disconnect();

redis.on("end", () => {
// Make sure there's enough time for the error to be emitted
setTimeout(done, 500);
});
});
});
});