cloudcms-server 0.9.254 → 0.9.257

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,7 @@
1
- var path = require("path");
1
+ var redisClientFactory = require("../../clients/redis");
2
+ var redisHelper = require("../../util/redis");
2
3
 
3
- var NRP = require('node-redis-pubsub');
4
-
5
- var logFactory = require("../../util/logger");
4
+ var NRP = require("../../clients/nrp");
6
5
 
7
6
  /**
8
7
  * Redis broadcast provider.
@@ -12,55 +11,31 @@ var logFactory = require("../../util/logger");
12
11
  exports = module.exports = function(broadcastConfig)
13
12
  {
14
13
  var nrp = null;
15
-
16
- var logger = logFactory("REDIS BROADCAST");
17
- logger.setLevel("error");
18
-
19
- // allow for global redis default
20
- // allow for redis broadcast specific
21
- // otherwise default to "error"
22
- if (typeof(process.env.CLOUDCMS_REDIS_DEBUG_LEVEL) !== "undefined") {
23
- logger.setLevel(("" + process.env.CLOUDCMS_REDIS_DEBUG_LEVEL).toLowerCase(), true);
24
- }
25
- else if (typeof(process.env.CLOUDCMS_BROADCAST_REDIS_DEBUG_LEVEL) !== "undefined") {
26
- logger.setLevel(("" + process.env.CLOUDCMS_BROADCAST_REDIS_DEBUG_LEVEL).toLowerCase(), true);
27
- }
28
-
14
+
15
+ var logger = redisHelper.redisLogger("REDIS_BROADCAST", "CLOUDCMS_BROADCAST_", "error")
16
+
29
17
  var r = {};
30
18
 
31
19
  r.start = function(callback)
32
20
  {
33
- var redisPort = broadcastConfig.port;
34
- if (typeof(redisPort) === "undefined" || !redisPort)
35
- {
36
- redisPort = process.env.CLOUDCMS_BROADCAST_REDIS_PORT;
37
- }
38
- if (typeof(redisPort) === "undefined" || !redisPort)
39
- {
40
- redisPort = process.env.CLOUDCMS_REDIS_PORT;
41
- }
42
-
43
- var redisEndpoint = broadcastConfig.endpoint;
44
- if (typeof(redisEndpoint) === "undefined" || !redisEndpoint)
45
- {
46
- redisEndpoint = process.env.CLOUDCMS_BROADCAST_REDIS_ENDPOINT;
47
- }
48
- if (typeof(redisEndpoint) === "undefined" || !redisEndpoint)
49
- {
50
- redisEndpoint = process.env.CLOUDCMS_REDIS_ENDPOINT;
51
- }
52
-
53
- var nrpConfig = {
54
- "port": redisPort,
55
- "host": redisEndpoint,
56
- "scope": "broadcast_cache"
57
- };
58
-
59
- logger.info("using config = " + nrpConfig);
60
-
61
- nrp = new NRP(nrpConfig);
62
-
63
- callback();
21
+ redisClientFactory.create(broadcastConfig, function(err, client) {
22
+
23
+ if (err) {
24
+ return callback(err);
25
+ }
26
+
27
+ var nrpConfig = {
28
+ "client": client,
29
+ "scope": "broadcast_cache"
30
+ };
31
+
32
+ logger.info("using config = " + nrpConfig);
33
+
34
+ nrp = new NRP(nrpConfig);
35
+ nrp.connect(function(err) {
36
+ callback(err);
37
+ });
38
+ });
64
39
  };
65
40
 
66
41
  r.publish = function(topic, message, callback)
package/clients/nrp.js ADDED
@@ -0,0 +1,117 @@
1
+ // a revision of
2
+ // https://raw.githubusercontent.com/louischatriot/node-redis-pubsub/master/lib/node-redis-pubsub.js
3
+ // that works with Redis 6+
4
+
5
+ "use strict";
6
+ var redis = require('redis');
7
+
8
+ /**
9
+ * Create a new NodeRedisPubsub instance that can subscribe to channels and publish messages
10
+ * @param {Object} options Options for the client creations:
11
+ * client - a connected Redis client
12
+ * scope - Optional, two NodeRedisPubsubs with different scopes will not share messages
13
+ */
14
+ function NodeRedisPubsub(options)
15
+ {
16
+ if (!(this instanceof NodeRedisPubsub)){ return new NodeRedisPubsub(options); }
17
+
18
+ options || (options = {});
19
+
20
+ this.emitter = options.client.duplicate();
21
+ this.emitter.setMaxListeners(0);
22
+ this.receiver = options.client.duplicate();
23
+ this.receiver.setMaxListeners(0);
24
+
25
+ this.prefix = options.scope ? options.scope + ':' : '';
26
+ }
27
+
28
+ NodeRedisPubsub.prototype.connect = function(callback)
29
+ {
30
+ var self = this;
31
+
32
+ (async function() {
33
+ await self.emitter.connect();
34
+ await self.receiver.connect();
35
+ callback();
36
+ })();
37
+ };
38
+
39
+ /**
40
+ * Subscribe to a channel
41
+ * @param {String} channel The channel to subscribe to, can be a pattern e.g. 'user.*'
42
+ * @param {Function} handler Function to call with the received message.
43
+ * @param {Function} cb Optional callback to call once the handler is registered.
44
+ */
45
+ NodeRedisPubsub.prototype.on = NodeRedisPubsub.prototype.subscribe = function(channel, handler, callback)
46
+ {
47
+ if (!callback)
48
+ {
49
+ callback = function(){};
50
+ }
51
+
52
+ var self = this;
53
+
54
+ if (channel === "error")
55
+ {
56
+ self.errorHandler = handler;
57
+ self.emitter.on("error", handler);
58
+ self.receiver.on("error", handler);
59
+ return callback();
60
+ }
61
+
62
+ var listener = function(self, handler)
63
+ {
64
+ return function(message, channel) {
65
+
66
+ var jsonmsg = message;
67
+ try{
68
+ jsonmsg = JSON.parse(message);
69
+ } catch (ex){
70
+ if(typeof self.errorHandler === 'function'){
71
+ return self.errorHandler("Invalid JSON received! Channel: " + self.prefix + channel + " Message: " + message);
72
+ }
73
+ }
74
+ return handler(jsonmsg, channel);
75
+ }
76
+ }(self, handler);
77
+
78
+ (async function() {
79
+ await self.receiver.pSubscribe(self.prefix + channel, listener);
80
+ })();
81
+
82
+ callback();
83
+ };
84
+
85
+ /**
86
+ * Emit an event
87
+ * @param {String} channel Channel on which to emit the message
88
+ * @param {Object} message
89
+ */
90
+ NodeRedisPubsub.prototype.emit = NodeRedisPubsub.prototype.publish = function (channel, message)
91
+ {
92
+ var self = this;
93
+
94
+ (async function() {
95
+ return await self.emitter.publish(self.prefix + channel, JSON.stringify(message));
96
+ })();
97
+ };
98
+
99
+ /**
100
+ * Safely close the redis connections 'soon'
101
+ */
102
+ NodeRedisPubsub.prototype.quit = function()
103
+ {
104
+ this.emitter.quit();
105
+ this.receiver.quit();
106
+ };
107
+
108
+ /**
109
+ * Dangerously close the redis connections immediately
110
+ */
111
+ NodeRedisPubsub.prototype.end = function()
112
+ {
113
+ this.emitter.end(true);
114
+ this.receiver.end(true);
115
+ };
116
+
117
+ module.exports = NodeRedisPubsub;
@@ -0,0 +1,48 @@
1
+ var redis = require("redis");
2
+ var redisHelper = require("../util/redis");
3
+
4
+ var clients = {};
5
+
6
+ /**
7
+ * Redis client factory.
8
+ *
9
+ * @type {*}
10
+ */
11
+ exports = module.exports = {};
12
+
13
+ var create = exports.create = function(config, callback)
14
+ {
15
+ if (typeof(config) === "function") {
16
+ callback = config;
17
+ config = {};
18
+ }
19
+
20
+ if (!config) {
21
+ config = {};
22
+ }
23
+
24
+ var redisOptions = redisHelper.redisOptions(config);
25
+ var url = redisOptions.url;
26
+
27
+ // cached client?
28
+ var client = clients[url];
29
+ if (client) {
30
+ return callback(null, client);
31
+ }
32
+
33
+ // connect
34
+ (async function() {
35
+ await redisHelper.createAndConnect(redisOptions, function(err, client) {
36
+
37
+ if (err) {
38
+ return callback(err);
39
+ }
40
+
41
+ // cache it
42
+ clients[url] = client;
43
+
44
+ // return
45
+ return callback(null, client);
46
+ });
47
+ })();
48
+ };
@@ -1,6 +1,8 @@
1
- var path = require("path");
2
- var redis = require("redis");
3
1
  var logFactory = require("../../util/logger");
2
+ //var redisHelper = require("../../util/redis");
3
+
4
+ var redisClientFactory = require("../../clients/redis");
5
+ const redisHelper = require("../../util/redis");
4
6
 
5
7
  /**
6
8
  * Redis lock service.
@@ -16,55 +18,24 @@ exports = module.exports = function(locksConfig)
16
18
  delay: 50
17
19
  });
18
20
 
19
- var nrp = null;
20
21
  var client = null;
21
-
22
- var logger = logFactory("REDIS LOCK");
23
-
24
- // allow for global redis default
25
- // allow for redis broadcast specific
26
- // otherwise default to error
27
- if (typeof(process.env.CLOUDCMS_REDIS_DEBUG_LEVEL) !== "undefined") {
28
- logger.setLevel(("" + process.env.CLOUDCMS_REDIS_DEBUG_LEVEL).toLowerCase(), true);
29
- }
30
- else if (typeof(process.env.CLOUDCMS_LOCKS_REDIS_DEBUG_LEVEL) !== "undefined") {
31
- logger.setLevel(("" + process.env.CLOUDCMS_LOCKS_REDIS_DEBUG_LEVEL).toLowerCase(), true);
32
- }
33
- else {
34
- logger.setLevel("error");
35
- }
36
-
22
+
23
+ var logger = redisHelper.redisLogger("REDIS_LOCKS", "CLOUDCMS_LOCKS_", "error")
24
+
37
25
  var r = {};
38
26
 
39
27
  r.init = function(callback)
40
28
  {
41
- var redisPort = locksConfig.port;
42
- if (typeof(redisPort) === "undefined" || !redisPort)
43
- {
44
- redisPort = process.env.CLOUDCMS_LOCKS_REDIS_PORT;
45
- }
46
- if (typeof(redisPort) === "undefined" || !redisPort)
47
- {
48
- redisPort = process.env.CLOUDCMS_REDIS_PORT;
49
- }
50
-
51
- var redisEndpoint = locksConfig.endpoint;
52
- if (typeof(redisEndpoint) === "undefined" || !redisEndpoint)
53
- {
54
- redisEndpoint = process.env.CLOUDCMS_LOCKS_REDIS_ENDPOINT;
55
- }
56
- if (typeof(redisEndpoint) === "undefined" || !redisEndpoint)
57
- {
58
- redisEndpoint = process.env.CLOUDCMS_REDIS_ENDPOINT;
59
- }
60
-
61
- var redisOptions = {};
62
-
63
- //redis.debug_mode = true;
64
-
65
- client = redis.createClient(redisPort, redisEndpoint, redisOptions);
66
-
67
- callback();
29
+ redisClientFactory.create(locksConfig, function(err, _client) {
30
+
31
+ if (err) {
32
+ return callback(err);
33
+ }
34
+
35
+ client = _client;
36
+
37
+ return callback();
38
+ });
68
39
  };
69
40
 
70
41
  r.lock = function(key, fn)
@@ -18,7 +18,7 @@ exports = module.exports = function()
18
18
  {
19
19
  var z = ref.indexOf("://");
20
20
 
21
- var type = ref.substring(0, z + 3);
21
+ var type = ref.substring(0, z);
22
22
  var identifier = ref.substring(z + 3);
23
23
 
24
24
  var parts = identifier.split("/").reverse();
@@ -132,7 +132,7 @@ exports = module.exports = function()
132
132
  {
133
133
  assertAuthenticated(req, res, function() {
134
134
 
135
- doResetCache(req.virtualHost, req.ref, function(err) {
135
+ doResetCache(req.virtualHost, req.query.ref, function(err) {
136
136
  completionFn(req.virtualHost, res, err);
137
137
  });
138
138