Skip to content

Commit a2f3f5a

Browse files
refactor: remove redis direct dependency
This import meant the redis package was required even if it was not used to create a Redis client. It is replaced by a dynamic import, which is supported since Node.js v13.2.0. Related: - #24 - #2
1 parent 2013436 commit a2f3f5a

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/util.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { randomBytes } from "crypto";
2-
import { commandOptions } from "redis";
32

43
export function hasBinary(obj: any, toJSON?: boolean): boolean {
54
if (!obj || typeof obj !== "object") {
@@ -74,21 +73,23 @@ export function XREAD(
7473
readCount: number
7574
) {
7675
if (isRedisV4Client(redisClient)) {
77-
return redisClient.xRead(
78-
commandOptions({
79-
isolated: true,
80-
}),
81-
[
76+
return import("redis").then((redisPackage) => {
77+
return redisClient.xRead(
78+
redisPackage.commandOptions({
79+
isolated: true,
80+
}),
81+
[
82+
{
83+
key: streamName,
84+
id: offset,
85+
},
86+
],
8287
{
83-
key: streamName,
84-
id: offset,
85-
},
86-
],
87-
{
88-
COUNT: readCount,
89-
BLOCK: 5000,
90-
}
91-
);
88+
COUNT: readCount,
89+
BLOCK: 5000,
90+
}
91+
);
92+
});
9293
} else {
9394
return redisClient
9495
.xread("BLOCK", 100, "COUNT", readCount, "STREAMS", streamName, offset)

test/util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ export function setup({
145145
redisClients.push(redisClient);
146146
ports.push(port);
147147
if (servers.length === nodeCount) {
148+
// ensure all nodes know each other
149+
servers[0].emit("ping");
150+
servers[1].emit("ping");
151+
servers[2].emit("ping");
152+
148153
await sleep(200);
149154

150155
resolve({

0 commit comments

Comments
 (0)