@cocreate/socket-server 1.10.1 → 1.10.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.
- package/.github/FUNDING.yml +3 -3
- package/.github/workflows/automated.yml +55 -55
- package/CHANGELOG.md +665 -649
- package/CONTRIBUTING.md +96 -96
- package/CoCreate.config.js +25 -25
- package/LICENSE +21 -21
- package/README.md +52 -52
- package/demo/server.js +19 -19
- package/docs/index.html +194 -64
- package/package.json +1 -1
- package/release.config.js +21 -21
- package/src/index.js +251 -306
- package/src/AsyncMessage.js +0 -52
package/src/index.js
CHANGED
|
@@ -1,306 +1,251 @@
|
|
|
1
|
-
const WebSocket = require('ws');
|
|
2
|
-
const url = require("url");
|
|
3
|
-
const EventEmitter = require("events").EventEmitter;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
let
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (permission && permission.authorized)
|
|
253
|
-
data = permission.authorized
|
|
254
|
-
|
|
255
|
-
let responseData = JSON.stringify({
|
|
256
|
-
action,
|
|
257
|
-
data
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
socket.send(responseData);
|
|
261
|
-
|
|
262
|
-
if (socket.config && socket.config.organization_id)
|
|
263
|
-
this.emit("setBandwidth", {
|
|
264
|
-
type: 'out',
|
|
265
|
-
data: responseData,
|
|
266
|
-
organization_id: socket.config.organization_id
|
|
267
|
-
})
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// addAsyncMessage(key) {
|
|
277
|
-
// let asyncMessage = this.asyncMessages.get(key)
|
|
278
|
-
// if (!asyncMessage) {
|
|
279
|
-
// this.asyncMessages.set(key, new AsyncMessage(key));
|
|
280
|
-
// }
|
|
281
|
-
// }
|
|
282
|
-
|
|
283
|
-
getKeyFromUrl(pathname) {
|
|
284
|
-
var path = pathname.split("/");
|
|
285
|
-
var params = {
|
|
286
|
-
type: null,
|
|
287
|
-
key: pathname
|
|
288
|
-
}
|
|
289
|
-
if (path.length > 0) {
|
|
290
|
-
params.type = path[1];
|
|
291
|
-
params.organization_id = path[2]
|
|
292
|
-
}
|
|
293
|
-
return params
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
getHost(req) {
|
|
297
|
-
const headers = req['headers']
|
|
298
|
-
let origin = headers['origin'];
|
|
299
|
-
if (origin && origin !== 'null')
|
|
300
|
-
return url.parse(origin).hostname;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
module.exports = SocketServer
|
|
1
|
+
const WebSocket = require('ws');
|
|
2
|
+
const url = require("url");
|
|
3
|
+
const EventEmitter = require("events").EventEmitter;
|
|
4
|
+
const uid = require('@cocreate/uuid')
|
|
5
|
+
|
|
6
|
+
class SocketServer extends EventEmitter {
|
|
7
|
+
constructor(prefix) {
|
|
8
|
+
super();
|
|
9
|
+
this.clients = new Map();
|
|
10
|
+
this.prefix = prefix || "crud";
|
|
11
|
+
this.wss = new WebSocket.Server({ noServer: true });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
handleUpgrade(req, socket, head) {
|
|
15
|
+
const self = this;
|
|
16
|
+
const pathname = url.parse(req.url).pathname;
|
|
17
|
+
const config = this.getKeyFromUrl(pathname)
|
|
18
|
+
if (config.type == this.prefix) {
|
|
19
|
+
self.wss.handleUpgrade(req, socket, head, function (socket) {
|
|
20
|
+
socket.config = config
|
|
21
|
+
self.onWebSocket(req, socket);
|
|
22
|
+
})
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
onWebSocket(req, socket) {
|
|
29
|
+
const self = this;
|
|
30
|
+
|
|
31
|
+
this.addClient(socket);
|
|
32
|
+
|
|
33
|
+
socket.on('message', async (message) => {
|
|
34
|
+
self.onMessage(req, socket, message);
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
socket.on('close', function () {
|
|
38
|
+
self.removeClient(socket)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
socket.on("error", () => {
|
|
42
|
+
self.removeClient(socket)
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.send(socket, 'connect', socket.config.key);
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
addClient(socket) {
|
|
50
|
+
let organization_id = socket.config.organization_id
|
|
51
|
+
let user_id = socket.config.user_id
|
|
52
|
+
let key = socket.config.key
|
|
53
|
+
let room_clients = this.clients.get(key);
|
|
54
|
+
if (room_clients) {
|
|
55
|
+
room_clients.push(socket);
|
|
56
|
+
} else {
|
|
57
|
+
room_clients = [socket];
|
|
58
|
+
}
|
|
59
|
+
this.clients.set(key, room_clients);
|
|
60
|
+
|
|
61
|
+
if (user_id)
|
|
62
|
+
this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
|
|
63
|
+
|
|
64
|
+
//. add metrics
|
|
65
|
+
let total_cnt = 0;
|
|
66
|
+
this.clients.forEach((c) => total_cnt += c.length)
|
|
67
|
+
|
|
68
|
+
this.emit("createMetrics", {
|
|
69
|
+
organization_id,
|
|
70
|
+
client_cnt: room_clients.length,
|
|
71
|
+
total_cnt: total_cnt
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
removeClient(socket) {
|
|
76
|
+
let organization_id = socket.config.organization_id
|
|
77
|
+
let user_id = socket.config.user_id
|
|
78
|
+
let key = socket.config.key
|
|
79
|
+
let room_clients = this.clients.get(key)
|
|
80
|
+
const index = room_clients.indexOf(socket);
|
|
81
|
+
|
|
82
|
+
if (index > -1) {
|
|
83
|
+
room_clients.splice(index, 1);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (room_clients.length == 0) {
|
|
87
|
+
if (user_id)
|
|
88
|
+
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
|
|
89
|
+
|
|
90
|
+
// TODO: remove if no one else connected to organization
|
|
91
|
+
this.emit("deleteMetrics", { organization_id });
|
|
92
|
+
this.emit("deletePermissions", organization_id);
|
|
93
|
+
this.emit('disconnect', organization_id)
|
|
94
|
+
} else {
|
|
95
|
+
let total_cnt = 0;
|
|
96
|
+
this.clients.forEach((c) => total_cnt += c.length)
|
|
97
|
+
|
|
98
|
+
this.emit("changeCountMetrics", {
|
|
99
|
+
organization_id,
|
|
100
|
+
total_cnt,
|
|
101
|
+
client_cnt: room_clients.length
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async onMessage(req, socket, message) {
|
|
108
|
+
try {
|
|
109
|
+
const organization_id = socket.config.organization_id
|
|
110
|
+
let { action, data } = JSON.parse(message)
|
|
111
|
+
|
|
112
|
+
if (action) {
|
|
113
|
+
this.emit("setBandwidth", {
|
|
114
|
+
type: 'in',
|
|
115
|
+
data: message,
|
|
116
|
+
organization_id
|
|
117
|
+
});
|
|
118
|
+
let user_id = null;
|
|
119
|
+
if (this.authenticate)
|
|
120
|
+
user_id = await this.authenticate.getUserId(req);
|
|
121
|
+
|
|
122
|
+
if (this.authorize) {
|
|
123
|
+
data.host = this.getHost(req)
|
|
124
|
+
const permission = await this.authorize.check(action, data, user_id)
|
|
125
|
+
if (permission.dbUrl === false) {
|
|
126
|
+
data.database = process.env.organization_id
|
|
127
|
+
data.organization_id = process.env.organization_id
|
|
128
|
+
|
|
129
|
+
const permission2 = await this.authorize.check(action, data, req, user_id)
|
|
130
|
+
if (!permission2 || permission2.error) {
|
|
131
|
+
return this.send(socket, 'Access Denied', { action, permission2, ...data })
|
|
132
|
+
}
|
|
133
|
+
} else if (!permission || permission.error) {
|
|
134
|
+
if (!user_id)
|
|
135
|
+
this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
|
|
136
|
+
|
|
137
|
+
return this.send(socket, 'Access Denied', { action, permission, ...data })
|
|
138
|
+
}
|
|
139
|
+
// dburl is true and db does not have 'keys' collection
|
|
140
|
+
// action: syncCollection data{collection: 'keys', document[]}
|
|
141
|
+
// actions: add keys as once keys are added admin user can do anything
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
// }
|
|
145
|
+
|
|
146
|
+
if (permission.authorized)
|
|
147
|
+
data = permission.authorized
|
|
148
|
+
|
|
149
|
+
if (user_id) {
|
|
150
|
+
if (!socket.config.user_id) {
|
|
151
|
+
socket.config.user_id = user_id
|
|
152
|
+
this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.emit(action, socket, data);
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
} catch (e) {
|
|
163
|
+
console.log(e);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
broadcast(socket, action, data) {
|
|
168
|
+
if (!data.uid)
|
|
169
|
+
data.uid = uid.generate()
|
|
170
|
+
|
|
171
|
+
let organization_id = socket.config.organization_id;
|
|
172
|
+
let url = `/${this.prefix}/${organization_id}`;
|
|
173
|
+
|
|
174
|
+
let namespace = data.namespace;
|
|
175
|
+
if (namespace) {
|
|
176
|
+
url += `/${namespace}`
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
let rooms = data.room;
|
|
181
|
+
if (rooms) {
|
|
182
|
+
if (!rooms.isArray())
|
|
183
|
+
rooms = [rooms]
|
|
184
|
+
for (let room of rooms) {
|
|
185
|
+
let url = url;
|
|
186
|
+
url += `/${room}`;
|
|
187
|
+
|
|
188
|
+
this.send(socket, action, data, url)
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
this.send(socket, action, data, url)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async send(socket, action, data, url) {
|
|
196
|
+
let clients
|
|
197
|
+
if (!url)
|
|
198
|
+
clients = [socket]
|
|
199
|
+
else
|
|
200
|
+
clients = this.clients.get(url);
|
|
201
|
+
|
|
202
|
+
if (clients) {
|
|
203
|
+
for (let client of clients) {
|
|
204
|
+
if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
|
|
205
|
+
const permission = await this.authorize.check(action, data, socket.config.user_id)
|
|
206
|
+
if (permission && permission.authorized)
|
|
207
|
+
data = permission.authorized
|
|
208
|
+
|
|
209
|
+
let responseData = JSON.stringify({
|
|
210
|
+
action,
|
|
211
|
+
data
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
socket.send(responseData);
|
|
215
|
+
|
|
216
|
+
if (socket.config && socket.config.organization_id)
|
|
217
|
+
this.emit("setBandwidth", {
|
|
218
|
+
type: 'out',
|
|
219
|
+
data: responseData,
|
|
220
|
+
organization_id: socket.config.organization_id
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
getKeyFromUrl(pathname) {
|
|
229
|
+
var path = pathname.split("/");
|
|
230
|
+
var params = {
|
|
231
|
+
type: null,
|
|
232
|
+
key: pathname
|
|
233
|
+
}
|
|
234
|
+
if (path.length > 0) {
|
|
235
|
+
params.type = path[1];
|
|
236
|
+
params.organization_id = path[2]
|
|
237
|
+
}
|
|
238
|
+
return params
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
getHost(req) {
|
|
242
|
+
const headers = req['headers']
|
|
243
|
+
let origin = headers['origin'];
|
|
244
|
+
if (origin && origin !== 'null')
|
|
245
|
+
return url.parse(origin).hostname;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
module.exports = SocketServer
|
package/src/AsyncMessage.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
class AsyncMessage {
|
|
3
|
-
constructor(key) {
|
|
4
|
-
this.key = key;
|
|
5
|
-
this.messages = new Map();
|
|
6
|
-
this.orders = [];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
defineMessage(id) {
|
|
10
|
-
this.orders.push(id);
|
|
11
|
-
this.messages.set(id, null);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
setMessage(id, data) {
|
|
15
|
-
this.messages.set(id, data);
|
|
16
|
-
if (this.orders.length > 0) {
|
|
17
|
-
this.runMessage();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
runMessage() {
|
|
23
|
-
let runIndex = -1;
|
|
24
|
-
this.orders.some((key, index) => {
|
|
25
|
-
let messageData = this.messages.get(key)
|
|
26
|
-
if (messageData != null) {
|
|
27
|
-
messageData.forEach(({socket, message}) => {
|
|
28
|
-
try {
|
|
29
|
-
if (socket) {
|
|
30
|
-
socket.send(message);
|
|
31
|
-
}
|
|
32
|
-
} catch (error) {
|
|
33
|
-
console.log(error);
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
runIndex = index;
|
|
38
|
-
this.messages.delete(key);
|
|
39
|
-
return false;
|
|
40
|
-
} else {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
if (runIndex !== -1) {
|
|
46
|
-
this.orders.splice(0, runIndex + 1);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = AsyncMessage;
|