From 2a37c6e7d1fc76bd5e29ddf5690bebc7c3f1320e Mon Sep 17 00:00:00 2001 From: Ethan Shea Date: Wed, 15 Oct 2025 13:20:06 -0400 Subject: [PATCH] Fix double-connect when using the default clients for ioredis I was getting this error when creating a resumableStreamContext ``` Redis is already connecting/connected ``` ioredis will connect clients automatically when constructed https://github.com/redis/ioredis/blob/main/README.md#basic-usage --- src/__tests__/ioredis.test.ts | 24 ++++++++++++++++++------ src/ioredis-adapters.ts | 10 ++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/__tests__/ioredis.test.ts b/src/__tests__/ioredis.test.ts index fc97bdc..d1c1b29 100644 --- a/src/__tests__/ioredis.test.ts +++ b/src/__tests__/ioredis.test.ts @@ -3,12 +3,24 @@ import { resumableStreamTests } from "./tests"; import Redis from "ioredis"; if (process.env.REDIS_URL) { - resumableStreamTests(() => { - return { - subscriber: new Redis(process.env.REDIS_URL!), - publisher: new Redis(process.env.REDIS_URL!), - }; - }, "ioredis"); + describe("default client", () => { + resumableStreamTests(() => { + // Will obtain credentials from the REDIS_URL environment variable automatically + return { + subscriber: undefined, + publisher: undefined, + }; + }, "ioredis"); + }); + + describe("custom client", () => { + resumableStreamTests(() => { + return { + subscriber: new Redis(process.env.REDIS_URL!), + publisher: new Redis(process.env.REDIS_URL!), + }; + }, "ioredis"); + }); } else { console.error("REDIS_URL is not set, skipping tests"); describe("Redis tests", () => { diff --git a/src/ioredis-adapters.ts b/src/ioredis-adapters.ts index 4edaa86..258e3da 100644 --- a/src/ioredis-adapters.ts +++ b/src/ioredis-adapters.ts @@ -8,7 +8,10 @@ import { Publisher, Subscriber } from "./types"; */ export function createSubscriberAdapter(client: Redis): Subscriber { const adapter: Subscriber = { - connect: () => client.connect(), + connect: () => { + // ioredis Redis instances are connected by default. Nothing to do. + return Promise.resolve(); + }, subscribe: async function (channel: string, callback: (message: string) => void) { client.on("message", (innerChannel, message) => { if (channel === innerChannel) { @@ -29,7 +32,10 @@ export function createSubscriberAdapter(client: Redis): Subscriber { */ export function createPublisherAdapter(client: Redis): Publisher { const adapter: Publisher = { - connect: () => client.connect(), + connect: () => { + // ioredis Redis instances are connected by default. Nothing to do. + return Promise.resolve(); + }, publish: (channel: string, message: string | Buffer) => client.publish(channel, message), set: (key: string, value: string | Buffer, options?: { EX?: number }) => { if (options?.EX) {