redux-cluster-ws 2.0.2 → 2.0.3

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.
@@ -273,4 +273,3 @@ function client(store) {
273
273
  }
274
274
  return store;
275
275
  }
276
- //# sourceMappingURL=client.js.map
package/dist/cjs/index.js CHANGED
@@ -22,4 +22,3 @@ Object.defineProperty(exports, "client", { enumerable: true, get: function () {
22
22
  Object.defineProperty(exports, "createStore", { enumerable: true, get: function () { return client_js_1.createStore; } });
23
23
  __exportStar(require("./types.js"), exports);
24
24
  __exportStar(require("./utils.js"), exports);
25
- //# sourceMappingURL=index.js.map
@@ -292,4 +292,3 @@ function server(store) {
292
292
  new ReduxClusterWSServerWrapper(store);
293
293
  return store;
294
294
  }
295
- //# sourceMappingURL=server.js.map
package/dist/cjs/types.js CHANGED
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
package/dist/cjs/utils.js CHANGED
@@ -72,4 +72,3 @@ function deepClone(obj) {
72
72
  }
73
73
  return obj;
74
74
  }
75
- //# sourceMappingURL=utils.js.map
@@ -18,20 +18,15 @@ const WebSocketClass = (() => {
18
18
  }
19
19
  })();
20
20
  export class ReduxCluster {
21
- stderr = console.error;
22
- role = [];
23
- mode = "action";
24
- connected = false;
25
- resync = 1000;
26
- RCHash;
27
- version;
28
- homepage;
29
- createWSClient;
30
- store;
31
- altReducer;
32
- defaultState;
33
- static reducers = {};
34
21
  constructor(reducer) {
22
+ this.stderr = console.error;
23
+ this.role = [];
24
+ this.mode = "action";
25
+ this.connected = false;
26
+ this.resync = 1000;
27
+ this.dispatch = (action) => {
28
+ return this.store.dispatch(action);
29
+ };
35
30
  this.altReducer = reducer;
36
31
  this.RCHash = hasher(reducer.name);
37
32
  // Get version and homepage from package.json (would need to be injected in real implementation)
@@ -71,9 +66,6 @@ export class ReduxCluster {
71
66
  getState() {
72
67
  return this.store.getState();
73
68
  }
74
- dispatch = (action) => {
75
- return this.store.dispatch(action);
76
- };
77
69
  subscribe(listener) {
78
70
  return this.store.subscribe(listener);
79
71
  }
@@ -84,16 +76,126 @@ export class ReduxCluster {
84
76
  return this.store[Symbol.observable]();
85
77
  }
86
78
  }
79
+ ReduxCluster.reducers = {};
87
80
  export class ReduxClusterWSClientWrapper {
88
- store;
89
- config;
90
- ws;
91
- reconnectTimer;
92
- authenticated = false;
93
- login;
94
- password;
95
- originalDispatch;
96
81
  constructor(store, config) {
82
+ this.authenticated = false;
83
+ this.reconnect = () => {
84
+ try {
85
+ const url = this.buildWebSocketURL();
86
+ this.ws = new WebSocketClass(url);
87
+ if (this.ws) {
88
+ this.ws.onopen = () => {
89
+ this.sendMessage({
90
+ _msg: "REDUX_CLUSTER_SOCKET_AUTH",
91
+ _hash: this.store.RCHash,
92
+ _login: this.login,
93
+ _password: this.password,
94
+ });
95
+ };
96
+ this.ws.onmessage = (event) => {
97
+ try {
98
+ const message = JSON.parse(event.data);
99
+ this.handleMessage(message);
100
+ }
101
+ catch (error) {
102
+ this.store.stderr(`ReduxCluster.createWSClient message parse error: ${error}`);
103
+ }
104
+ };
105
+ this.ws.onclose = () => {
106
+ this.authenticated = false;
107
+ this.store.connected = false;
108
+ this.scheduleReconnect();
109
+ };
110
+ this.ws.onerror = (error) => {
111
+ this.store.stderr(`ReduxCluster.createWSClient connection error: ${error}`);
112
+ this.authenticated = false;
113
+ this.store.connected = false;
114
+ };
115
+ }
116
+ }
117
+ catch (error) {
118
+ this.store.stderr(`ReduxCluster.createWSClient client error: ${error}`);
119
+ this.store.connected = false;
120
+ this.scheduleReconnect();
121
+ }
122
+ };
123
+ this.handleMessage = (message) => {
124
+ if (message._hash !== this.store.RCHash) {
125
+ return;
126
+ }
127
+ switch (message._msg) {
128
+ case "REDUX_CLUSTER_MSGTOWORKER":
129
+ if (message._action) {
130
+ this.originalDispatch(message._action);
131
+ }
132
+ break;
133
+ case "REDUX_CLUSTER_SOCKET_AUTHSTATE":
134
+ if (message._value === true) {
135
+ this.authenticated = true;
136
+ this.store.connected = true;
137
+ this.sendMessage({
138
+ _msg: "REDUX_CLUSTER_START",
139
+ _hash: this.store.RCHash,
140
+ });
141
+ }
142
+ else {
143
+ this.authenticated = false;
144
+ this.store.connected = false;
145
+ if (message._banned) {
146
+ this.store.stderr("Your IP is locked for 3 hours");
147
+ }
148
+ else {
149
+ this.store.stderr("Authorization failed");
150
+ }
151
+ this.ws?.close();
152
+ }
153
+ break;
154
+ }
155
+ };
156
+ this.dispatch = (action) => {
157
+ try {
158
+ if (this.ws && this.ws.readyState === 1 && this.authenticated) {
159
+ // WebSocket.OPEN = 1
160
+ this.sendMessage({
161
+ _msg: "REDUX_CLUSTER_MSGTOMASTER",
162
+ _hash: this.store.RCHash,
163
+ _action: action,
164
+ });
165
+ }
166
+ else {
167
+ this.store.stderr("WebSocket is not connected or not authenticated");
168
+ }
169
+ }
170
+ catch (error) {
171
+ this.store.stderr(`ReduxCluster.createWSClient write error: ${error}`);
172
+ }
173
+ return action;
174
+ };
175
+ this.sendMessage = (message) => {
176
+ if (this.ws && this.ws.readyState === 1) {
177
+ // WebSocket.OPEN = 1
178
+ this.ws.send(JSON.stringify(message));
179
+ }
180
+ };
181
+ this.scheduleReconnect = () => {
182
+ if (this.reconnectTimer) {
183
+ clearTimeout(this.reconnectTimer);
184
+ }
185
+ this.reconnectTimer = setTimeout(() => {
186
+ this.reconnect();
187
+ }, this.config.reconnectInterval);
188
+ };
189
+ this.destroy = () => {
190
+ if (this.reconnectTimer) {
191
+ clearTimeout(this.reconnectTimer);
192
+ }
193
+ if (this.ws) {
194
+ this.ws.close();
195
+ }
196
+ // Restore original dispatch
197
+ this.store.dispatch = this.originalDispatch;
198
+ };
97
199
  this.store = store;
98
200
  this.config = {
99
201
  port: 10002,
@@ -127,46 +229,6 @@ export class ReduxClusterWSClientWrapper {
127
229
  this.store.connected = false;
128
230
  this.reconnect();
129
231
  }
130
- reconnect = () => {
131
- try {
132
- const url = this.buildWebSocketURL();
133
- this.ws = new WebSocketClass(url);
134
- if (this.ws) {
135
- this.ws.onopen = () => {
136
- this.sendMessage({
137
- _msg: "REDUX_CLUSTER_SOCKET_AUTH",
138
- _hash: this.store.RCHash,
139
- _login: this.login,
140
- _password: this.password,
141
- });
142
- };
143
- this.ws.onmessage = (event) => {
144
- try {
145
- const message = JSON.parse(event.data);
146
- this.handleMessage(message);
147
- }
148
- catch (error) {
149
- this.store.stderr(`ReduxCluster.createWSClient message parse error: ${error}`);
150
- }
151
- };
152
- this.ws.onclose = () => {
153
- this.authenticated = false;
154
- this.store.connected = false;
155
- this.scheduleReconnect();
156
- };
157
- this.ws.onerror = (error) => {
158
- this.store.stderr(`ReduxCluster.createWSClient connection error: ${error}`);
159
- this.authenticated = false;
160
- this.store.connected = false;
161
- };
162
- }
163
- }
164
- catch (error) {
165
- this.store.stderr(`ReduxCluster.createWSClient client error: ${error}`);
166
- this.store.connected = false;
167
- this.scheduleReconnect();
168
- }
169
- };
170
232
  buildWebSocketURL() {
171
233
  const protocol = this.config.host.toLowerCase().includes("https://")
172
234
  ? "wss:"
@@ -175,82 +237,6 @@ export class ReduxClusterWSClientWrapper {
175
237
  const path = `/redux-cluster-${this.store.RCHash}`;
176
238
  return `${protocol}//${host}:${this.config.port}${path}`;
177
239
  }
178
- handleMessage = (message) => {
179
- if (message._hash !== this.store.RCHash) {
180
- return;
181
- }
182
- switch (message._msg) {
183
- case "REDUX_CLUSTER_MSGTOWORKER":
184
- if (message._action) {
185
- this.originalDispatch(message._action);
186
- }
187
- break;
188
- case "REDUX_CLUSTER_SOCKET_AUTHSTATE":
189
- if (message._value === true) {
190
- this.authenticated = true;
191
- this.store.connected = true;
192
- this.sendMessage({
193
- _msg: "REDUX_CLUSTER_START",
194
- _hash: this.store.RCHash,
195
- });
196
- }
197
- else {
198
- this.authenticated = false;
199
- this.store.connected = false;
200
- if (message._banned) {
201
- this.store.stderr("Your IP is locked for 3 hours");
202
- }
203
- else {
204
- this.store.stderr("Authorization failed");
205
- }
206
- this.ws?.close();
207
- }
208
- break;
209
- }
210
- };
211
- dispatch = (action) => {
212
- try {
213
- if (this.ws && this.ws.readyState === 1 && this.authenticated) {
214
- // WebSocket.OPEN = 1
215
- this.sendMessage({
216
- _msg: "REDUX_CLUSTER_MSGTOMASTER",
217
- _hash: this.store.RCHash,
218
- _action: action,
219
- });
220
- }
221
- else {
222
- this.store.stderr("WebSocket is not connected or not authenticated");
223
- }
224
- }
225
- catch (error) {
226
- this.store.stderr(`ReduxCluster.createWSClient write error: ${error}`);
227
- }
228
- return action;
229
- };
230
- sendMessage = (message) => {
231
- if (this.ws && this.ws.readyState === 1) {
232
- // WebSocket.OPEN = 1
233
- this.ws.send(JSON.stringify(message));
234
- }
235
- };
236
- scheduleReconnect = () => {
237
- if (this.reconnectTimer) {
238
- clearTimeout(this.reconnectTimer);
239
- }
240
- this.reconnectTimer = setTimeout(() => {
241
- this.reconnect();
242
- }, this.config.reconnectInterval);
243
- };
244
- destroy = () => {
245
- if (this.reconnectTimer) {
246
- clearTimeout(this.reconnectTimer);
247
- }
248
- if (this.ws) {
249
- this.ws.close();
250
- }
251
- // Restore original dispatch
252
- this.store.dispatch = this.originalDispatch;
253
- };
254
240
  }
255
241
  export function createStore(reducer) {
256
242
  const reduxCluster = new ReduxCluster(reducer);
@@ -279,4 +265,3 @@ export function client(store) {
279
265
  }
280
266
  return store;
281
267
  }
282
- //# sourceMappingURL=client.js.map
package/dist/esm/index.js CHANGED
@@ -2,4 +2,3 @@ export { server } from "./server.js";
2
2
  export { client, createStore } from "./client.js";
3
3
  export * from "./types.js";
4
4
  export * from "./utils.js";
5
- //# sourceMappingURL=index.js.map