-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathutil.ts
More file actions
185 lines (164 loc) · 5.15 KB
/
util.ts
File metadata and controls
185 lines (164 loc) · 5.15 KB
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
export function parseTimeout(value: unknown, defaultValue: number): number {
return typeof value === "number" && value > 0 && value !== Infinity
? value
: defaultValue;
}
export function hasBinary(obj: any, toJSON?: boolean): boolean {
if (!obj || typeof obj !== "object") {
return false;
}
if (obj instanceof ArrayBuffer || ArrayBuffer.isView(obj)) {
return true;
}
if (Array.isArray(obj)) {
for (let i = 0, l = obj.length; i < l; i++) {
if (hasBinary(obj[i])) {
return true;
}
}
return false;
}
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
return true;
}
}
if (obj.toJSON && typeof obj.toJSON === "function" && !toJSON) {
return hasBinary(obj.toJSON(), true);
}
return false;
}
export function parseNumSubResponse(res) {
return parseInt(res[1], 10);
}
export function sumValues(values) {
return values.reduce((acc, val) => {
return acc + val;
}, 0);
}
const RETURN_BUFFERS = true;
/**
* Whether the client comes from the `redis` package
*
* @param redisClient
*
* @see https://github.com/redis/node-redis
*/
function isRedisV4Client(redisClient: any) {
return typeof redisClient.sSubscribe === "function";
}
const kHandlers = Symbol("handlers");
const kListener = Symbol("listener");
const kPendingUnsubscribes = Symbol("pendingUnsubscribes");
export function SSUBSCRIBE(
redisClient: any,
channel: string,
handler: (rawMessage: Buffer, channel: Buffer) => void
): Promise<void> {
if (isRedisV4Client(redisClient)) {
return redisClient.sSubscribe(channel, handler, RETURN_BUFFERS);
} else {
const doSubscribe = (): Promise<void> => {
if (!redisClient[kHandlers]) {
redisClient[kHandlers] = new Map<string, typeof handler>();
redisClient[kPendingUnsubscribes] = new Map<string, Promise<void>>();
redisClient[kListener] = (rawChannel: Buffer, message: Buffer) => {
redisClient[kHandlers].get(rawChannel.toString())?.(
message,
rawChannel
);
};
redisClient.on("smessageBuffer", redisClient[kListener]);
}
redisClient[kHandlers].set(channel, handler);
return redisClient.ssubscribe(channel);
};
// Wait for any pending unsubscribe on this channel to complete first
const pendingUnsubscribe = redisClient[kPendingUnsubscribes]?.get(channel);
if (pendingUnsubscribe) {
return pendingUnsubscribe.then(doSubscribe);
}
return doSubscribe();
}
}
export function SUNSUBSCRIBE(
redisClient: any,
channel: string | string[]
): Promise<void> {
if (isRedisV4Client(redisClient)) {
return redisClient.sUnsubscribe(channel);
} else {
const channels = Array.isArray(channel) ? channel : [channel];
// Remove handlers immediately to stop processing messages
channels.forEach((c) => redisClient[kHandlers]?.delete(c));
// Perform the unsubscribe and track as pending
const unsubscribePromise = redisClient.sunsubscribe(channel).then(() => {
// Remove from pending tracking
channels.forEach((c) => redisClient[kPendingUnsubscribes]?.delete(c));
// Clean up the global listener when no more handlers exist
if (redisClient[kHandlers]?.size === 0 && redisClient[kListener]) {
redisClient.off("smessageBuffer", redisClient[kListener]);
delete redisClient[kHandlers];
delete redisClient[kListener];
delete redisClient[kPendingUnsubscribes];
}
});
// Track pending unsubscribe for each channel
channels.forEach((c) =>
redisClient[kPendingUnsubscribes]?.set(c, unsubscribePromise)
);
return unsubscribePromise;
}
}
/**
* @see https://redis.io/commands/spublish/
*/
export function SPUBLISH(
redisClient: any,
channel: string,
payload: string | Uint8Array
) {
if (isRedisV4Client(redisClient)) {
return redisClient.sPublish(channel, payload);
} else {
return redisClient.spublish(channel, payload);
}
}
export function PUBSUB(redisClient: any, arg: string, channel: string) {
if (redisClient.constructor.name === "Cluster" || redisClient.isCluster) {
// ioredis cluster
return Promise.all(
redisClient.nodes().map((node) => {
return node
.send_command("PUBSUB", [arg, channel])
.then(parseNumSubResponse);
})
).then(sumValues);
} else if (isRedisV4Client(redisClient)) {
const isCluster = Array.isArray(redisClient.masters);
if (isCluster) {
// redis@4 cluster
const nodes = redisClient.masters;
return Promise.all(
nodes.map((node) => {
return node.client
.sendCommand(["PUBSUB", arg, channel])
.then(parseNumSubResponse);
})
).then(sumValues);
} else {
// redis@4 standalone
return redisClient
.sendCommand(["PUBSUB", arg, channel])
.then(parseNumSubResponse);
}
} else {
// ioredis / redis@3 standalone
return new Promise((resolve, reject) => {
redisClient.send_command("PUBSUB", [arg, channel], (err, numSub) => {
if (err) return reject(err);
resolve(parseNumSubResponse(numSub));
});
});
}
}