diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 1ba47bd3..249be9df 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -154,7 +154,6 @@ jobs: - name: Test run: bun test examples/vercel-functions-app-router/ci.test.ts - working-directory: examples/vercel-functions-app-router vercel-functions-pages-router-deployed: concurrency: vercel-functions-pages-router-deployed diff --git a/pkg/commands/subscribe.ts b/pkg/commands/subscribe.ts index 2886ec4a..4bb42368 100644 --- a/pkg/commands/subscribe.ts +++ b/pkg/commands/subscribe.ts @@ -2,6 +2,7 @@ import type { CommandOptions } from "./command"; import { Command } from "./command"; import type { Requester } from "../http"; import { PSubscribeCommand } from "./psubscribe"; +import type { RedisOptions } from "../types"; type SubscriptionInfo = { command: SubscribeCommand; @@ -40,12 +41,19 @@ export class Subscriber extends EventTarget { private subscriptions: Map; private client: Requester; private listeners: Map>>; + private opts?: Pick; - constructor(client: Requester, channels: string[], isPattern: boolean = false) { + constructor( + client: Requester, + channels: string[], + isPattern: boolean = false, + opts?: Pick + ) { super(); this.client = client; this.subscriptions = new Map(); this.listeners = new Map(); + this.opts = opts; for (const channel of channels) { if (isPattern) { @@ -119,7 +127,9 @@ export class Subscriber extends EventTarget { const messageStr = messageData.slice(thirdCommaIndex + 1); try { - const message = JSON.parse(messageStr); + const message = + this.opts?.automaticDeserialization === false ? messageStr : JSON.parse(messageStr); + this.dispatchToListeners("pmessage", { pattern, channel, message }); this.dispatchToListeners(`pmessage:${pattern}`, { pattern, channel, message }); } catch (error) { @@ -141,8 +151,9 @@ export class Subscriber extends EventTarget { const count = Number.parseInt(messageStr); this.dispatchToListeners(type, count); } else { - // For regular messages, emit the full object - const message = JSON.parse(messageStr); + const message = + this.opts?.automaticDeserialization === false ? messageStr : JSON.parse(messageStr); + this.dispatchToListeners(type, { channel, message }); this.dispatchToListeners(`${type}:${channel}`, { channel, message }); } diff --git a/pkg/http.test.ts b/pkg/http.test.ts index 93c5d5f4..d0d2e0d0 100644 --- a/pkg/http.test.ts +++ b/pkg/http.test.ts @@ -60,5 +60,12 @@ describe("http", () => { } finally { server.stop(true); } + + try { + await redis.get("foo"); + throw new Error("Expected to throw"); + } catch (error) { + expect((error as Error).name).toBe("TimeoutError"); + } }); }); diff --git a/pkg/http.ts b/pkg/http.ts index 8f6e47b0..e7815fd1 100644 --- a/pkg/http.ts +++ b/pkg/http.ts @@ -68,7 +68,7 @@ export type RetryConfig = */ retries?: number; /** - * A backoff function receives the current retry cound and returns a number in milliseconds to wait before retrying. + * A backoff function receives the current retry count and returns a number in milliseconds to wait before retrying. * * @default * ```ts diff --git a/pkg/redis.ts b/pkg/redis.ts index 2bba7eae..d639ae4e 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -1044,7 +1044,7 @@ export class Redis { */ psubscribe = (patterns: string | string[]): Subscriber => { const patternArray = Array.isArray(patterns) ? patterns : [patterns]; - return new Subscriber(this.client, patternArray, true); + return new Subscriber(this.client, patternArray, true, this.opts); }; /** @@ -1251,7 +1251,7 @@ export class Redis { */ subscribe = (channels: string | string[]): Subscriber => { const channelArray = Array.isArray(channels) ? channels : [channels]; - return new Subscriber(this.client, channelArray); + return new Subscriber(this.client, channelArray, false, this.opts); }; /** * @see https://redis.io/commands/sunion