Skip to content

Commit be4bc5e

Browse files
authored
fix: Clone Redis error handlers (#201)
1 parent c16da47 commit be4bc5e

3 files changed

Lines changed: 45 additions & 30 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ app.configure(
136136

137137
## How it works
138138

139-
![alt tag](https://raw.githubusercontent.com/PedroMD/feathers-sync/master/feathers-sync%20and%20real-time%20events-60.png)
139+
![alt tag](https://raw.githubusercontent.com/PedroMD/feathers-sync/master/feathers-sync.png)
140140

141141
## Caveat: Listening to service events
142142

@@ -215,6 +215,6 @@ The `data` for the `sync-in` event should be in the same form as the one that is
215215

216216
## License
217217

218-
Copyright (c) 2021 Feathers contributors
218+
Copyright (c) 2025 Feathers contributors
219219

220220
Licensed under the [MIT license](LICENSE).

lib/adapters/redis.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
1-
const redis = require('redis')
2-
const debug = require('debug')('feathers-sync:redis')
3-
const core = require('../core')
1+
const redis = require("redis");
2+
const debug = require("debug")("feathers-sync:redis");
3+
const core = require("../core");
44

5-
module.exports = config => {
6-
return app => {
7-
const { key, serialize, deserialize, redisClient, uri } = config
5+
module.exports = (config) => {
6+
return (app) => {
7+
const { key, serialize, deserialize, redisClient, uri } = config;
88
const options = {
99
url: uri,
10-
...config.redisOptions
11-
}
10+
...config.redisOptions,
11+
};
1212

1313
if (!redisClient) {
14-
debug(`Setting up Redis client for ${options.url}`)
14+
debug(`Setting up Redis client for ${options.url}`);
1515
}
1616

17-
const pub = redisClient || redis.createClient(options)
18-
const sub = pub.duplicate()
17+
const pub = redisClient || redis.createClient(options);
18+
const sub = pub.duplicate();
19+
const errorHandlers = pub.listeners("error");
1920

20-
const msgFromRedisHandler = data => {
21-
debug(`Got ${key} message from Redis`)
22-
app.emit('sync-in', data)
21+
if (errorHandlers.length > 0) {
22+
// If error handlers exists, copy them to sub
23+
errorHandlers.forEach((handler) => {
24+
sub.on("error", handler);
25+
});
26+
} else {
27+
// If not, make sure both pub and sub has an error handler to avoid unhandled rejections
28+
const defaultErrorHandler = (err) => {
29+
console.error("REDIS ERROR", err);
30+
};
31+
pub.on("error", defaultErrorHandler);
32+
sub.on("error", defaultErrorHandler);
2333
}
2434

25-
app.configure(core)
35+
const msgFromRedisHandler = (data) => {
36+
debug(`Got ${key} message from Redis`);
37+
app.emit("sync-in", data);
38+
};
39+
40+
app.configure(core);
2641
app.sync = {
2742
deserialize,
2843
serialize,
2944
pub,
3045
sub,
31-
type: 'redis',
46+
type: "redis",
3247
ready: new Promise((resolve, reject) => {
33-
pub.connect()
34-
sub.connect()
35-
sub.once('ready', resolve)
36-
sub.once('error', reject)
37-
}).then(() => sub.subscribe(key, msgFromRedisHandler, true))
38-
}
48+
pub.connect();
49+
sub.connect();
50+
sub.once("ready", resolve);
51+
sub.once("error", reject);
52+
}).then(() => sub.subscribe(key, msgFromRedisHandler, true)),
53+
};
3954

40-
app.on('sync-out', data => {
41-
debug(`Publishing key ${key} to Redis`)
42-
pub.publish(key, data)
43-
})
44-
}
45-
}
55+
app.on("sync-out", (data) => {
56+
debug(`Publishing key ${key} to Redis`);
57+
pub.publish(key, data);
58+
});
59+
};
60+
};

0 commit comments

Comments
 (0)