|
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"); |
4 | 4 |
|
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; |
8 | 8 | const options = { |
9 | 9 | url: uri, |
10 | | - ...config.redisOptions |
11 | | - } |
| 10 | + ...config.redisOptions, |
| 11 | + }; |
12 | 12 |
|
13 | 13 | if (!redisClient) { |
14 | | - debug(`Setting up Redis client for ${options.url}`) |
| 14 | + debug(`Setting up Redis client for ${options.url}`); |
15 | 15 | } |
16 | 16 |
|
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"); |
19 | 20 |
|
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); |
23 | 33 | } |
24 | 34 |
|
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); |
26 | 41 | app.sync = { |
27 | 42 | deserialize, |
28 | 43 | serialize, |
29 | 44 | pub, |
30 | 45 | sub, |
31 | | - type: 'redis', |
| 46 | + type: "redis", |
32 | 47 | 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 | + }; |
39 | 54 |
|
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