From c488b42308f74965ccb7893bcc311a3e6ae4693a Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 19 May 2021 10:09:29 +0530 Subject: [PATCH] revert: improvement: pub/sub layer now accepts any data type --- adonis-typings/redis.ts | 8 ++++---- src/AbstractConnection/index.ts | 24 +++++------------------- test/redis-connection.spec.ts | 10 +++++----- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/adonis-typings/redis.ts b/adonis-typings/redis.ts index 335cd3b..c3ea381 100644 --- a/adonis-typings/redis.ts +++ b/adonis-typings/redis.ts @@ -33,8 +33,8 @@ declare module '@ioc:Adonis/Addons/Redis' { /** * Pubsub subscriber */ - export type PubSubChannelHandler = (data: T) => Promise | void - export type PubSubPatternHandler = ( + export type PubSubChannelHandler = (data: T) => Promise | void + export type PubSubPatternHandler = ( channel: string, data: T ) => Promise | void @@ -45,10 +45,10 @@ declare module '@ioc:Adonis/Addons/Redis' { export interface RedisPubSubContract { publish( channel: string, - message: any, + message: string, callback: (error: Error | null, count: number) => void ): void - publish(channel: string, message: any): Promise + publish(channel: string, message: string): Promise subscribe(channel: string, handler: PubSubChannelHandler | string): void psubscribe(pattern: string, handler: PubSubPatternHandler | string): void unsubscribe(channel: string): void diff --git a/src/AbstractConnection/index.ts b/src/AbstractConnection/index.ts index 7cd6379..0bf920f 100644 --- a/src/AbstractConnection/index.ts +++ b/src/AbstractConnection/index.ts @@ -12,7 +12,6 @@ import { EventEmitter } from 'events' import { Redis, Cluster } from 'ioredis' import { Exception } from '@poppinss/utils' -import { MessageBuilder } from '@poppinss/utils/build/helpers' import { ApplicationContract } from '@ioc:Adonis/Core/Application' import { ContainerBindings, IocResolverContract } from '@ioc:Adonis/Core/Application' @@ -22,8 +21,6 @@ import { PubSubPatternHandler, } from '@ioc:Adonis/Addons/Redis' -const PUBSUB_PURPOSE = 'adonis-pubsub' - /** * Helper to sleep */ @@ -181,12 +178,7 @@ export abstract class AbstractConnection extends Even this.ioSubscriberConnection!.on('message', (channel, message) => { const handler = this.subscriptions.get(channel) if (handler) { - /** - * If the message is not originated from the same process and not built using - * the message builder, then should pass it as it is - */ - const verifiedMessage = new MessageBuilder().verify(message, PUBSUB_PURPOSE) - handler(verifiedMessage || message) + handler(message) } }) @@ -196,12 +188,7 @@ export abstract class AbstractConnection extends Even this.ioSubscriberConnection!.on('pmessage', (pattern, channel, message) => { const handler = this.psubscriptions.get(pattern) if (handler) { - /** - * If the message is not originated from the same process and not built using - * the message builder, then should pass it as it is - */ - const verifiedMessage = new MessageBuilder().verify(message, PUBSUB_PURPOSE) - handler(channel, verifiedMessage || message) + handler(channel, message) } }) @@ -414,10 +401,9 @@ export abstract class AbstractConnection extends Even } } - public publish(channel: string, message: any, callback?: any) { - const messageString = new MessageBuilder().build(message, undefined, PUBSUB_PURPOSE) + public publish(channel: string, message: string, callback?: any) { return callback - ? this.ioConnection.publish(channel, messageString, callback) - : this.ioConnection.publish(channel, messageString) + ? this.ioConnection.publish(channel, message, callback) + : this.ioConnection.publish(channel, message) } } diff --git a/test/redis-connection.spec.ts b/test/redis-connection.spec.ts index 0794bf9..a6bc4ec 100644 --- a/test/redis-connection.spec.ts +++ b/test/redis-connection.spec.ts @@ -482,12 +482,12 @@ test.group('Redis factory - PSubscribe', () => { ) as unknown as RedisConnectionContract factory.on('psubscription:ready', () => { - factory.publish('news:prime', { title: 'breaking news at 9' }) + factory.publish('news:prime', JSON.stringify({ title: 'breaking news at 9' })) }) factory.psubscribe('news:*', (channel, message) => { assert.equal(channel, 'news:prime') - assert.deepEqual(message, { title: 'breaking news at 9' }) + assert.deepEqual(message, JSON.stringify({ title: 'breaking news at 9' })) factory.punsubscribe('news:*') factory.publish('news:prime', 'breaking news at 9', (_error, count) => { @@ -509,9 +509,9 @@ test.group('Redis factory - PSubscribe', () => { ) as unknown as RedisConnectionContract class RedisListeners { - public async onNews(channel: string, message: { title: string }) { + public async onNews(channel: string, message: string) { assert.equal(channel, 'news:prime') - assert.deepEqual(message, { title: 'breaking news at 9' }) + assert.equal(message, JSON.stringify({ title: 'breaking news at 9' })) await factory.quit() done() } @@ -524,7 +524,7 @@ test.group('Redis factory - PSubscribe', () => { factory.psubscribe('news:*', 'RedisListeners.onNews') factory.on('psubscription:ready', () => { - factory.publish('news:prime', { title: 'breaking news at 9' }) + factory.publish('news:prime', JSON.stringify({ title: 'breaking news at 9' })) }) })