-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathredis-pubsub.ts
197 lines (166 loc) · 6.14 KB
/
redis-pubsub.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {Cluster, Ok, Redis, RedisOptions} from 'ioredis';
import {PubSubEngine} from 'graphql-subscriptions';
import {PubSubAsyncIterator} from './pubsub-async-iterator';
type RedisClient = Redis | Cluster;
type OnMessage<T> = (message: T) => void;
export interface PubSubRedisOptions {
connection?: RedisOptions;
triggerTransform?: TriggerTransform;
connectionListener?: (err: Error) => void;
publisher?: RedisClient;
subscriber?: RedisClient;
reviver?: Reviver;
serializer?: Serializer;
deserializer?: Deserializer;
}
export class RedisPubSub implements PubSubEngine {
constructor(options: PubSubRedisOptions = {}) {
const {
triggerTransform,
connection,
connectionListener,
subscriber,
publisher,
reviver,
serializer,
deserializer,
} = options;
this.triggerTransform = triggerTransform || (trigger => trigger as string);
if (reviver && deserializer) {
throw new Error("Reviver and deserializer can't be used together");
}
this.reviver = reviver;
this.serializer = serializer;
this.deserializer = deserializer;
if (subscriber && publisher) {
this.redisPublisher = publisher;
this.redisSubscriber = subscriber;
} else {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const IORedis = require('ioredis');
this.redisPublisher = new IORedis(connection);
this.redisSubscriber = new IORedis(connection);
if (connectionListener) {
this.redisPublisher
.on('connect', connectionListener)
.on('error', connectionListener);
this.redisSubscriber
.on('connect', connectionListener)
.on('error', connectionListener);
} else {
this.redisPublisher.on('error', console.error);
this.redisSubscriber.on('error', console.error);
}
} catch (error) {
console.error(
`No publisher or subscriber instances were provided and the package 'ioredis' wasn't found. Couldn't create Redis clients.`,
);
}
}
// handle messages received via psubscribe and subscribe
this.redisSubscriber.on('pmessage', this.onMessage.bind(this));
// partially applied function passes undefined for pattern arg since 'message' event won't provide it:
this.redisSubscriber.on('message', this.onMessage.bind(this, undefined));
this.subscriptionMap = {};
this.subsRefsMap = {};
this.currentSubscriptionId = 0;
}
public async publish<T>(trigger: string, payload: T): Promise<void> {
await this.redisPublisher.publish(trigger, this.serializer ? this.serializer(payload) : JSON.stringify(payload));
}
public subscribe<T = any>(
trigger: string,
onMessage: OnMessage<T>,
options: unknown = {},
): Promise<number> {
const triggerName: string = this.triggerTransform(trigger, options);
const id = this.currentSubscriptionId++;
this.subscriptionMap[id] = [triggerName, onMessage];
const refs = this.subsRefsMap[triggerName];
if (refs && refs.length > 0) {
this.subsRefsMap[triggerName] = [...refs, id];
return Promise.resolve(id);
} else {
return new Promise<number>((resolve, reject) => {
const subscribeFn = options['pattern'] ? this.redisSubscriber.psubscribe : this.redisSubscriber.subscribe;
subscribeFn.call(this.redisSubscriber, triggerName, err => {
if (err) {
reject(err);
} else {
this.subsRefsMap[triggerName] = [
...(this.subsRefsMap[triggerName] || []),
id,
];
resolve(id);
}
});
});
}
}
public unsubscribe(subId: number): void {
const [triggerName = null] = this.subscriptionMap[subId] || [];
const refs = this.subsRefsMap[triggerName];
if (!refs) throw new Error(`There is no subscription of id "${subId}"`);
if (refs.length === 1) {
// unsubscribe from specific channel and pattern match
this.redisSubscriber.unsubscribe(triggerName);
this.redisSubscriber.punsubscribe(triggerName);
delete this.subsRefsMap[triggerName];
} else {
const index = refs.indexOf(subId);
this.subsRefsMap[triggerName] = index === -1
? refs
: [...refs.slice(0, index), ...refs.slice(index + 1)];
}
delete this.subscriptionMap[subId];
}
public asyncIterator<T>(triggers: string | string[], options?: unknown): AsyncIterator<T> {
return new PubSubAsyncIterator<T>(this, triggers, options);
}
public getSubscriber(): RedisClient {
return this.redisSubscriber;
}
public getPublisher(): RedisClient {
return this.redisPublisher;
}
public close(): Promise<Ok[]> {
return Promise.all([
this.redisPublisher.quit(),
this.redisSubscriber.quit(),
]);
}
private readonly serializer?: Serializer;
private readonly deserializer?: Deserializer;
private readonly triggerTransform: TriggerTransform;
private readonly redisSubscriber: RedisClient;
private readonly redisPublisher: RedisClient;
private readonly reviver: Reviver;
private readonly subscriptionMap: { [subId: number]: [string, OnMessage<unknown>] };
private readonly subsRefsMap: { [trigger: string]: Array<number> };
private currentSubscriptionId: number;
private onMessage(pattern: string, channel: string, message: string) {
const subscribers = this.subsRefsMap[pattern || channel];
// Don't work for nothing..
if (!subscribers || !subscribers.length) return;
let parsedMessage;
try {
parsedMessage = this.deserializer ? this.deserializer(message) : JSON.parse(message, this.reviver);
} catch (e) {
parsedMessage = message;
}
for (const subId of subscribers) {
const [, listener] = this.subscriptionMap[subId];
listener(parsedMessage);
}
}
}
export type Path = Array<string | number>;
export type Trigger = string | Path;
export type TriggerTransform = (
trigger: Trigger,
channelOptions?: unknown,
) => string;
export type Reviver = (key: any, value: any) => any;
export type Serializer = (source: any) => string;
export type Deserializer = (source: string) => any;