cloudcms-server 3.3.1-beta.9 → 4.0.0-beta.10

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.
Files changed (119) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/broadcast/broadcast.js +6 -3
  4. package/broadcast/providers/redis.js +24 -49
  5. package/clients/nrp.js +117 -0
  6. package/clients/redis.js +64 -0
  7. package/cloudcms-server.iml +1 -0
  8. package/d1/index.js +629 -0
  9. package/d1/index.js.works +203 -0
  10. package/d1/package.json +86 -0
  11. package/d1/package.json.works +14 -0
  12. package/duster/helpers/sample/nyt.js +2 -1
  13. package/framework/controllers.js +4 -4
  14. package/index.js +21 -14
  15. package/insight/insight.js +1 -1
  16. package/launchpad/index.js +203 -11
  17. package/launchpad/launchers/cluster.js +103 -110
  18. package/launchpad/launchers/redis.js +70 -0
  19. package/launchpad/launchers/single.js +36 -22
  20. package/locks/locks.js +63 -9
  21. package/locks/providers/cluster.js +3 -1
  22. package/locks/providers/memory.js +10 -7
  23. package/locks/providers/redis.js +62 -82
  24. package/middleware/admin/admin.js +3 -3
  25. package/middleware/authentication/adapters/session.js +11 -8
  26. package/middleware/authentication/authentication.js +28 -16
  27. package/middleware/authentication/authenticators/default.js +5 -2
  28. package/middleware/authentication/authenticators/session.js +5 -2
  29. package/middleware/authentication/providers/saml.js +2 -2
  30. package/middleware/authorization/authorization.js +11 -8
  31. package/middleware/awareness/awareness.js +63 -38
  32. package/middleware/awareness/plugins/api_event.js +105 -0
  33. package/middleware/awareness/plugins/editorial.js +8 -7
  34. package/middleware/awareness/plugins/resources.js +13 -5
  35. package/middleware/awareness/providers/abstract-async.js +107 -84
  36. package/middleware/awareness/providers/abstract.js +1 -1
  37. package/middleware/awareness/providers/memory.js +0 -14
  38. package/middleware/awareness/providers/redis.js +186 -279
  39. package/middleware/cache/cache.js +4 -2
  40. package/middleware/cache/providers/redis.js +127 -89
  41. package/middleware/cache/providers/shared-memory.js +3 -3
  42. package/middleware/cloudcms/cloudcms.js +22 -16
  43. package/middleware/driver-config/driver-config.js +0 -6
  44. package/middleware/form/form.js +3 -3
  45. package/middleware/modules/modules.js +17 -8
  46. package/middleware/perf/perf.js +3 -2
  47. package/middleware/proxy/proxy.js +8 -21
  48. package/middleware/stores/store.js +0 -1
  49. package/middleware/stores/stores.js +4 -4
  50. package/middleware/virtual-config/virtual-config.js +11 -11
  51. package/middleware/virtual-files/virtual-files.js +0 -3
  52. package/middleware/wcm/wcm.js +4 -4
  53. package/middleware/welcome/welcome.js +0 -3
  54. package/notifications/notifications.js +74 -12
  55. package/notifications/providers/kafka.js +182 -0
  56. package/notifications/providers/stomp.js +4 -0
  57. package/package.json +33 -30
  58. package/server/index.js +508 -416
  59. package/server/standalone.js +9 -0
  60. package/temp/clusterlock/index.js +3 -3
  61. package/temp/clusterlock/package.json +1 -1
  62. package/temp/passport-saml/LICENSE +23 -0
  63. package/temp/passport-saml/README.md +406 -0
  64. package/temp/passport-saml/lib/node-saml/algorithms.d.ts +5 -0
  65. package/temp/passport-saml/lib/node-saml/algorithms.js +41 -0
  66. package/temp/passport-saml/lib/node-saml/algorithms.js.map +1 -0
  67. package/temp/passport-saml/lib/node-saml/index.d.ts +3 -0
  68. package/temp/passport-saml/lib/node-saml/index.js +6 -0
  69. package/temp/passport-saml/lib/node-saml/index.js.map +1 -0
  70. package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.d.ts +45 -0
  71. package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.js +86 -0
  72. package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.js.map +1 -0
  73. package/temp/passport-saml/lib/node-saml/saml-post-signing.d.ts +3 -0
  74. package/temp/passport-saml/lib/node-saml/saml-post-signing.js +15 -0
  75. package/temp/passport-saml/lib/node-saml/saml-post-signing.js.map +1 -0
  76. package/temp/passport-saml/lib/node-saml/saml.d.ts +77 -0
  77. package/temp/passport-saml/lib/node-saml/saml.js +1170 -0
  78. package/temp/passport-saml/lib/node-saml/saml.js.map +1 -0
  79. package/temp/passport-saml/lib/node-saml/types.d.ts +95 -0
  80. package/temp/passport-saml/lib/node-saml/types.js +8 -0
  81. package/temp/passport-saml/lib/node-saml/types.js.map +1 -0
  82. package/temp/passport-saml/lib/node-saml/utility.d.ts +3 -0
  83. package/temp/passport-saml/lib/node-saml/utility.js +19 -0
  84. package/temp/passport-saml/lib/node-saml/utility.js.map +1 -0
  85. package/temp/passport-saml/lib/node-saml/xml.d.ts +21 -0
  86. package/temp/passport-saml/lib/node-saml/xml.js +140 -0
  87. package/temp/passport-saml/lib/node-saml/xml.js.map +1 -0
  88. package/temp/passport-saml/lib/passport-saml/index.d.ts +6 -0
  89. package/temp/passport-saml/lib/passport-saml/index.js +11 -0
  90. package/temp/passport-saml/lib/passport-saml/index.js.map +1 -0
  91. package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.d.ts +13 -0
  92. package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.js +63 -0
  93. package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.js.map +1 -0
  94. package/temp/passport-saml/lib/passport-saml/strategy.d.ts +20 -0
  95. package/temp/passport-saml/lib/passport-saml/strategy.js +167 -0
  96. package/temp/passport-saml/lib/passport-saml/strategy.js.map +1 -0
  97. package/temp/passport-saml/lib/passport-saml/types.d.ts +51 -0
  98. package/temp/passport-saml/lib/passport-saml/types.js +11 -0
  99. package/temp/passport-saml/lib/passport-saml/types.js.map +1 -0
  100. package/temp/passport-saml/package.json +96 -0
  101. package/util/auth.js +16 -10
  102. package/util/cloudcms.js +108 -120
  103. package/util/proxy-factory.js +103 -237
  104. package/util/redis.js +113 -0
  105. package/util/renditions.js +12 -6
  106. package/util/request.js +12 -6
  107. package/util/util.js +16 -2
  108. package/.last_command +0 -7
  109. package/launchpad/launchers/sticky-cluster.js +0 -43
  110. package/temp/memored/.jshintrc +0 -4
  111. package/temp/memored/README.md +0 -240
  112. package/temp/memored/demo/demo1.js +0 -37
  113. package/temp/memored/demo/demo2.js +0 -32
  114. package/temp/memored/gulpfile.js +0 -8
  115. package/temp/memored/index.js +0 -343
  116. package/temp/memored/package.json +0 -54
  117. package/temp/memored/spec/memored.spec.js +0 -265
  118. package/web/cms/ice.js +0 -109
  119. package/web/cms/preview.js +0 -106
package/.last_command DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "command": {
3
- "group": "server",
4
- "name": "start"
5
- },
6
- "arguments": {}
7
- }
@@ -1,43 +0,0 @@
1
- var clusterlock = require("../../temp/clusterlock");
2
-
3
- // use sticky-session library
4
- var sticky = require('sticky-session');
5
-
6
- module.exports = function(options) {
7
-
8
- var factoryCallback = options.factory;
9
- var reportCallback = options.report;
10
- if (!reportCallback)
11
- {
12
- reportCallback = function () {
13
- };
14
- }
15
- var completionCallback = options.complete;
16
- if (!completionCallback)
17
- {
18
- completionCallback = function () {
19
- };
20
- }
21
-
22
- factoryCallback(function (server) {
23
-
24
- if (!sticky.listen(server, server._listenPort))
25
- {
26
- // master code
27
-
28
- // start up cluster locks
29
- clusterlock.setup();
30
-
31
- // wait 5 seconds, then fire callbacks
32
- setTimeout(function () {
33
- reportCallback();
34
- completionCallback();
35
- }, 5000);
36
- }
37
- else
38
- {
39
- // worker code
40
- }
41
-
42
- });
43
- };
@@ -1,4 +0,0 @@
1
- {
2
- "node": true,
3
- "unused": true
4
- }
@@ -1,240 +0,0 @@
1
- # Memored
2
-
3
- Memored implements an in-memory shared cache to use in nodejs applications which uses [cluster](http://nodejs.org/api/cluster.html) module.
4
-
5
- Let's say you want your application to take advantage of multi-core CPUs using nodejs cluster module; you will be able to run several _isolated_ processes which shared nothing but a communication channel with parent process.
6
- If you need a fast volatile cache, common solutions would create an in-memory map for every process you run, so you end up with the same data stored several times in your machine RAM.
7
-
8
- Memored uses communication channel between master process and its workers to use a unique in-memory storage, reducing the amount of memory your application would use.
9
-
10
- ## Getting Started
11
- Install this module with _npm_:
12
- ```javascript
13
- npm install memored
14
- ```
15
-
16
- Store and read values is straightforward:
17
- ```javascript
18
- var cluster = require('cluster'),
19
- memored = require('memored');
20
-
21
- if (cluster.isMaster) {
22
- cluster.fork();
23
- } else {
24
- var han = {
25
- firstname: 'Han',
26
- lastname: 'Solo'
27
- },
28
- luke = {
29
- firstname: 'Luke',
30
- lastname: 'Skywalker'
31
- };
32
-
33
- // Store and read
34
- memored.store('character1', han, function() {
35
- console.log('Value stored!');
36
-
37
- memored.read('character1', function(err, value) {
38
- console.log('Read value:', value);
39
- });
40
- });
41
-
42
- // You can also set a ttl (milliseconds)
43
- memored.store('character2', luke, 1000, function(err, expirationTime) {
44
- console.log('Value stored until:', new Date(expirationTime));
45
-
46
- setTimeout(function() {
47
- memored.read('character2', function(err, value) {
48
- console.log('Value is gone?', value === undefined);
49
-
50
- process.exit();
51
- });
52
- }, 1050);
53
- });
54
- }
55
- ```
56
-
57
- ## Invalidation management
58
- By default, _memored_ will evict cache entries (stored with _ttl_) passively. This is, when you read an expired entry, you will get no value on return and _memored_ will delete the value from its internal cache.
59
-
60
- You can also configure _memored_ to actively evict expired entries every N milliseconds. For this to work, you need to pass the attribute *purgeInterval* to the *setup* function.
61
- This will trigger an internal function which looks for expired entries and deletes them from its internal cache.
62
-
63
- Example:
64
- ```javascript
65
- var cluster = require('cluster'),
66
- async = require('async'),
67
- memored = require('memored');
68
-
69
- if (cluster.isMaster) {
70
-
71
- cluster.fork();
72
- memored.setup({ purgeInterval: 500});
73
-
74
- } else {
75
-
76
- async.series({
77
- storeValue: function(next) {
78
- memored.store('key1', 'My simple string value', 100, next);
79
- },
80
- readCacheSize: function(next) {
81
- memored.size(function(err, size) {
82
- console.log('Current size is 1?', size === 1);
83
- next();
84
- });
85
- },
86
- wait: function(next) {
87
- setTimeout(next, 600);
88
- },
89
- readCacheSizeAgain: function(next) {
90
- memored.size(function(err, size) {
91
- console.log('Current size is 0?', size === 0);
92
- next();
93
- });
94
- }
95
- }, process.exit);
96
- }
97
- ```
98
-
99
- ## API
100
- Documentation for every module function:
101
-
102
-
103
- ### setup(options)
104
-
105
- This function is used to configure _memored_.
106
-
107
- **Arguments**:
108
-
109
- - **purgeInterval** {Number} (optional): Configures and triggers _memored_ expired entries auto deletion. Value expressed in milliseconds. _It's only used when called this method from the master process of your application_.
110
- - **logger** {Object} (optional): In you want _memored_ to log something, you must provide an object implementing *log* and *warn* functions.
111
-
112
- **Example**:
113
- ```javascript
114
- memored.setup({
115
- purgeInterval: 15000,
116
- logger: console
117
- });
118
- ```
119
-
120
- ### store(key, value, [ttl], callback)
121
-
122
- This function stores a value in the cache.
123
- _It is intended to be called from a worker process_.
124
-
125
- **Arguments**:
126
-
127
- - **key** {String} (required): Key used to lookup the entry
128
- - **value** {Mixed} (required): Whatever you want to store
129
- - **ttl** {Number} (optional): Time to live for this value in the cache (milliseconds)
130
- - **callback** {Function} (optional): Function to be call on store completion. Callback arguments:
131
- - _err_ {Error}: Optional error
132
- - _expirationTime_ {Number}: The timestamp of the moment when this entry will expire. If _ttl_ is not used, this value will be undefined.
133
-
134
- **Examples**:
135
- ```javascript
136
- memored.store('key1', {firstname: 'Han', lastname: 'Solo'}, function() {
137
- console.log('Value stored!');
138
- });
139
-
140
- memored.store('key2', ['a', 'b', 'c'], 15000, function(err, expirationTime) {
141
- console.log('This value will expire on:', new Date(expirationTime));
142
- });
143
- ```
144
-
145
- ### read(key, callback)
146
-
147
- This function reads a value from the cache.
148
- _It is intended to be called from a worker process_.
149
-
150
- **Arguments**:
151
-
152
- - **key** {String} (required): Key used to lookup the entry
153
- - **callback** {Function} (required): Function to be called on read completion. Callback arguments:
154
- - _err_ {Error}: Optional error
155
- - _value_ {Mixed}: Contents of the cached entry. If the value is not found or is expired, it will be undefined.
156
-
157
- **Example**:
158
- ```javascript
159
- memored.read('key1', function(err, value) {
160
- console.log('Key1 value:', value);
161
- });
162
-
163
- memored.read('key1', function(err, value, expirationTime) {
164
- console.log('Key1 value:', value);
165
- console.log('Key1 expiration time:', new Date(expirationTime));
166
- });
167
-
168
- memored.read('unknownKey', function(err, value) {
169
- console.log('No data read?', value === undefined);
170
- });
171
- ```
172
-
173
- ### remove(key, callback)
174
-
175
- This function removes an entry from the cache.
176
- _It is intended to be called from a worker process_.
177
-
178
- **Arguments**:
179
-
180
- - **key** {String} (required): Key used to lookup the entry
181
- - **callback** {Function} (optional): Function to be called on read completion.
182
-
183
- **Example**:
184
- ```javascript
185
- memored.remove('key1', function() {
186
- console.log('Key removed from the cache.');
187
- });
188
- ```
189
-
190
- ### clean(callback)
191
-
192
- This function removes all the entries from the cache.
193
- _It is intended to be called from a worker process_.
194
-
195
- **Arguments**:
196
-
197
- - **callback** {Function} (optional): Function to be called on read completion.
198
-
199
- **Example**:
200
- ```javascript
201
- memored.clean(function() {
202
- console.log('All cache entries have been deleted.');
203
- });
204
- ```
205
-
206
- ### size(callback)
207
-
208
- This function returns the number of entries in the cache.
209
-
210
- **Arguments**:
211
-
212
- - **callback** {Function} (required): Function to be called on size calculation is complete. Callback arguments:
213
- - _err_ {Error}: Optional error
214
- - _size_ {Number}: The number of entries in the cache.
215
-
216
- **Example**:
217
-
218
- ```javascript
219
- memored.size(function(err, size) {
220
- console.log('Cache size:', size);
221
- });
222
- ```
223
-
224
- ### version
225
-
226
- This is an attribute wich provides module's version number
227
-
228
-
229
- # Final note
230
- All the callbacks first parameter is an optional error object. Actually, this param will never be an error because there is no expected error in the internal code. There's no function call that can possible throw an expected error that this module would deal with.
231
- The existence of this param is to follow the *convention* about libraries callbacks in nodejs. As everybody expects this first callback parameter to be an optional one, I decided to include it.
232
-
233
- - [Nodeguide](http://nodeguide.com/style.html#callbacks)
234
- - [Nodejitsu](http://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions)
235
- - [GoSquared](https://engineering.gosquared.com/node-js-error-handling-callbacks-vs-promises)
236
-
237
-
238
- ## License
239
- Copyright (c) 2014 PaquitoSoft
240
- Licensed under the MIT license.
@@ -1,37 +0,0 @@
1
- var cluster = require('cluster'),
2
- memored = require('../index');
3
-
4
- if (cluster.isMaster) {
5
- cluster.fork();
6
- } else {
7
- var han = {
8
- firstname: 'Han',
9
- lastname: 'Solo'
10
- },
11
- luke = {
12
- firstname: 'Luke',
13
- lastname: 'Skywalker'
14
- };
15
-
16
- // Store and read
17
- memored.store('character1', han, function() {
18
- console.log('Value stored!');
19
-
20
- memored.read('character1', function(err, value) {
21
- console.log('Read value:', value);
22
- });
23
- });
24
-
25
- // You can also set a ttl (milliseconds)
26
- memored.store('character2', luke, 1000, function(err, expirationTime) {
27
- console.log('Value stored until:', new Date(expirationTime));
28
-
29
- setTimeout(function() {
30
- memored.read('character2', function(err, value) {
31
- console.log('Value is gone?', value === undefined);
32
-
33
- process.exit();
34
- });
35
- }, 1050);
36
- });
37
- }
@@ -1,32 +0,0 @@
1
- var cluster = require('cluster'),
2
- async = require('async'),
3
- memored = require('../index');
4
-
5
- if (cluster.isMaster) {
6
-
7
- cluster.fork();
8
- memored.setup({ purgeInterval: 500});
9
-
10
- } else {
11
-
12
- async.series({
13
- storeValue: function(next) {
14
- memored.store('key1', 'My simple string value', 100, next);
15
- },
16
- readCacheSize: function(next) {
17
- memored.size(function(err, size) {
18
- console.log('Current size is 1?', size === 1);
19
- next();
20
- });
21
- },
22
- wait: function(next) {
23
- setTimeout(next, 600);
24
- },
25
- readCacheSizeAgain: function(next) {
26
- memored.size(function(err, size) {
27
- console.log('Current size is 0?', size === 0);
28
- next();
29
- });
30
- }
31
- }, process.exit);
32
- }
@@ -1,8 +0,0 @@
1
- var gulp = require('gulp'),
2
- mocha = require('gulp-mocha')/*,
3
- plumber = require('gulp-plumber')*/;
4
-
5
- gulp.task('default', function() {
6
- return gulp.src('spec/*.spec.js', {read: true})
7
- .pipe(mocha({ui: 'bdd', reporter: 'spec'}));
8
- });
@@ -1,343 +0,0 @@
1
- 'use strict';
2
-
3
- var cluster = require('cluster'),
4
- packageInfo = require('./package');
5
-
6
- var logger = {
7
- log: function() {},
8
- warn: function() {}
9
- };
10
-
11
- var messagesCounter = 0;
12
-
13
- var activeMessages = {};
14
-
15
- var purgeIntervalObj;
16
-
17
- /*
18
- message
19
- - workerPid
20
- - type
21
- - requestParams
22
- */
23
-
24
- var cache = {};
25
-
26
- function CacheEntry(data) { // ttl -> milliseconds
27
- this.key = data.key;
28
- this.value = data.value;
29
- this.creationTime = Date.now();
30
- if (data.ttl) {
31
- this.ttl = data.ttl;
32
- this.expirationTime = this.creationTime + data.ttl;
33
- }
34
- }
35
- CacheEntry.prototype.isExpired = function() {
36
- return this.expirationTime && Date.now() > this.expirationTime;
37
- };
38
- CacheEntry.prototype.toString = function() {
39
- return "Key: " + this.key + "; Value: " + this.value + "; Ttl: " + this.ttl;
40
- };
41
-
42
- function _findWorkerByPid(workerPid) {
43
- var i = 0,
44
- workerIds = Object.keys(cluster.workers),
45
- len = workerIds.length,
46
- worker;
47
-
48
- for (; i < len; i++) {
49
- if (cluster.workers[workerIds[i]].process.pid == workerPid) {
50
- worker = cluster.workers[workerIds[i]];
51
- break;
52
- }
53
- }
54
-
55
- return worker;
56
- }
57
-
58
- function _getResultParamsValues(paramsObj) {
59
- var result = [null],
60
- prop;
61
- if (paramsObj) {
62
- for (prop in paramsObj) {
63
- result.push(paramsObj[prop]);
64
- }
65
- }
66
- return result;
67
- }
68
-
69
- function _sendMessageToWorker(message) {
70
- var worker = _findWorkerByPid(message.workerPid);
71
- worker.send(message);
72
- }
73
-
74
- function _sendMessageToMaster(message) {
75
- message.channel = 'memored';
76
- message.workerPid = process.pid;
77
- message.id = process.pid + '::' + messagesCounter++;
78
- process.send(message);
79
- if (message.callback) {
80
- activeMessages[message.id] = message;
81
- }
82
- }
83
-
84
- function _readCacheValue(message) {
85
- var cacheEntry = cache[message.requestParams.key];
86
- if (!cacheEntry) return _sendMessageToWorker(message);
87
- if (cacheEntry.isExpired()) {
88
- process.nextTick(function() {
89
- delete cache[message.requestParams.key];
90
- });
91
- cacheEntry = null;
92
- }
93
-
94
- if (cacheEntry) {
95
- message.responseParams = {
96
- value: cacheEntry.value
97
- };
98
- if (cacheEntry.expirationTime) {
99
- message.responseParams.expirationTime = cacheEntry.expirationTime;
100
- }
101
- }
102
-
103
- _sendMessageToWorker(message);
104
- }
105
-
106
- function _storeCacheValue(message) {
107
- cache[message.requestParams.key] = new CacheEntry(message.requestParams);
108
- if (message.requestParams.ttl) {
109
- message.responseParams = {
110
- expirationTime: cache[message.requestParams.key].expirationTime
111
- };
112
- }
113
- _sendMessageToWorker(message);
114
- }
115
-
116
- function _removeCacheValue(message) {
117
- delete cache[message.requestParams.key];
118
- _sendMessageToWorker(message);
119
- }
120
-
121
- function _cleanCache(message) {
122
- cache = {};
123
- _sendMessageToWorker(message);
124
- }
125
-
126
- function _getCacheSize(message) {
127
- message.responseParams = {
128
- size: Object.keys(cache).length
129
- };
130
- _sendMessageToWorker(message);
131
- }
132
-
133
- function _getCacheKeys(message) {
134
- message.responseParams = {
135
- keys: Object.keys(cache)
136
- };
137
- _sendMessageToWorker(message);
138
- }
139
-
140
- function _purgeCache() {
141
- var now = Date.now();
142
- Object.keys(cache).forEach(function(cacheKey) {
143
- if (cache[cacheKey].expirationTime && cache[cacheKey].expirationTime < now) {
144
- delete cache[cacheKey];
145
- }
146
- });
147
- }
148
-
149
- function _masterIncomingMessagesHanlder(message) {
150
- logger.log('Master received message:', message);
151
-
152
- if (!message || message.channel !== 'memored') return false;
153
-
154
- switch (message.type) {
155
- case 'read':
156
- _readCacheValue(message);
157
- break;
158
- case 'store':
159
- _storeCacheValue(message);
160
- break;
161
- case 'remove':
162
- _removeCacheValue(message);
163
- break;
164
- case 'clean':
165
- _cleanCache(message);
166
- break;
167
- case 'size':
168
- _getCacheSize(message);
169
- break;
170
- case 'keys':
171
- _getCacheKeys(message);
172
- break;
173
- default:
174
- logger.warn('Received an invalid message type:', message.type);
175
- }
176
- }
177
-
178
- function _workerIncomingMessagesHandler(message) {
179
- logger.log('Worker received message:', message);
180
-
181
- var pendingMessage;
182
-
183
- if (!message || message.channel !== 'memored') return false;
184
-
185
- pendingMessage = activeMessages[message.id];
186
- if (pendingMessage && pendingMessage.callback) {
187
- pendingMessage.callback.apply(null, _getResultParamsValues(message.responseParams));
188
- delete activeMessages[message.id];
189
- }
190
-
191
- }
192
-
193
- if (cluster.isMaster) {
194
-
195
- Object.keys(cluster.workers).forEach(function(workerId) {
196
- cluster.workers[workerId].on('message', _masterIncomingMessagesHanlder);
197
- });
198
-
199
- // Listen for new workers so we can listen to its messages
200
- cluster.on('fork', function(worker) {
201
- worker.on('message', _masterIncomingMessagesHanlder);
202
- });
203
-
204
- // TODO: Only for testing purposes
205
- // setInterval(function() {
206
- // logger.log('\n------------------------------------------');
207
- // logger.log(cache);
208
- // logger.log('------------------------------------------\n');
209
- // }, 2000).unref();
210
-
211
- } else {
212
-
213
- process.on('message', _workerIncomingMessagesHandler);
214
-
215
- }
216
-
217
- function _setup(options) {
218
- options = options || {};
219
- logger = options.logger || logger;
220
-
221
- if (cluster.isMaster) {
222
-
223
- if (options.mockData) {
224
- options.mockData.forEach(function(mock) {
225
- // key, value, ttl
226
- cache[mock.key] = new CacheEntry(mock);
227
- });
228
- }
229
-
230
- if (options.purgeInterval) {
231
- purgeIntervalObj = setInterval(function() {
232
- _purgeCache();
233
- }, options.purgeInterval).unref();
234
- }
235
- }
236
- }
237
-
238
- function _read(key, callback) {
239
- if (cluster.isWorker) {
240
- _sendMessageToMaster({
241
- type: 'read',
242
- requestParams: {
243
- key: key
244
- },
245
- callback: callback
246
- });
247
- } else {
248
- logger.warn('Memored::read# Cannot call this function from master process');
249
- }
250
- }
251
-
252
- function _store(key, value, ttl, callback) {
253
- if (cluster.isWorker) {
254
- if (callback === undefined) {
255
- callback = ttl;
256
- ttl = undefined;
257
- }
258
-
259
- _sendMessageToMaster({
260
- type: 'store',
261
- requestParams: {
262
- key: key,
263
- value: value,
264
- ttl: ttl
265
- },
266
- callback: callback
267
- });
268
- } else {
269
- logger.warn('Memored::store# Cannot call this function from master process');
270
- }
271
- }
272
-
273
- function _remove(key, callback) {
274
- if (cluster.isWorker) {
275
- _sendMessageToMaster({
276
- type: 'remove',
277
- requestParams: {
278
- key: key
279
- },
280
- callback: callback
281
- });
282
- } else {
283
- logger.warn('Memored::remove# Cannot call this function from master process');
284
- }
285
- }
286
-
287
- function _clean(callback) {
288
- if (cluster.isWorker) {
289
- _sendMessageToMaster({
290
- type: 'clean',
291
- callback: callback
292
- });
293
- } else {
294
- logger.warn('Memored::clean# Cannot call this function from master process');
295
- }
296
- }
297
-
298
- function _size(callback) {
299
- if (cluster.isWorker) {
300
- _sendMessageToMaster({
301
- type: 'size',
302
- callback: callback
303
- });
304
- } else {
305
- setImmediate(callback,{
306
- size: Object.keys(cache).length
307
- });
308
- }
309
- }
310
-
311
- function _reset(callback) {
312
- if (cluster.isMaster) {
313
- clearInterval(purgeIntervalObj);
314
- setImmediate(callback);
315
- } else {
316
- logger.warn('Memored::reset# Cannot call this function from a worker process');
317
- }
318
- }
319
-
320
- function _keys(callback) {
321
- if (cluster.isWorker) {
322
- _sendMessageToMaster({
323
- type: 'keys',
324
- callback: callback
325
- });
326
- } else {
327
- setImmediate(callback,{
328
- keys: Object.keys(cache)
329
- });
330
- }
331
- }
332
-
333
- module.exports = {
334
- version: packageInfo.version,
335
- setup: _setup,
336
- read: _read,
337
- store: _store,
338
- remove: _remove,
339
- clean: _clean,
340
- size: _size,
341
- reset: _reset,
342
- keys: _keys
343
- };