@ouroboros/body 0.1.3 → 1.0.1
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/package.json +15 -12
- package/src/Service.d.ts +126 -0
- package/src/Service.js +148 -0
- package/src/Service.ts +166 -0
- package/src/constants.d.ts +13 -0
- package/src/constants.js +15 -0
- package/src/constants.ts +17 -0
- package/src/errors.d.ts +32 -0
- package/src/errors.js +36 -0
- package/src/errors.ts +40 -0
- package/src/index.d.ts +223 -0
- package/src/index.js +401 -0
- package/src/index.ts +514 -0
- package/src/regex.d.ts +11 -0
- package/src/regex.js +13 -0
- package/src/regex.ts +15 -0
- package/index.d.ts +0 -3
- package/index.js +0 -3
- package/jquery.ajax.js +0 -5469
- package/rest.js +0 -408
- package/twoway.js +0 -389
- package/wshelper.js +0 -92
package/twoway.js
DELETED
|
@@ -1,389 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Two Way
|
|
3
|
-
*
|
|
4
|
-
* Allows clients to connect to the backend via websocket so events can be
|
|
5
|
-
* tracked in real time
|
|
6
|
-
*
|
|
7
|
-
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
8
|
-
* @copyright Ouroboros Coding Inc.
|
|
9
|
-
* @created 2019-03-29
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
// Ouroboros modules
|
|
13
|
-
import events from '@ouroboros/events';
|
|
14
|
-
import { empty } from '@ouroboros/tools';
|
|
15
|
-
|
|
16
|
-
// Communication
|
|
17
|
-
import rest from './rest.js';
|
|
18
|
-
import wshelper from './wshelper.js';
|
|
19
|
-
|
|
20
|
-
// The ping timer
|
|
21
|
-
let __ping = null;
|
|
22
|
-
|
|
23
|
-
// The valid close flag
|
|
24
|
-
let __close = true;
|
|
25
|
-
|
|
26
|
-
// The websocket
|
|
27
|
-
let __socket = null;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The service callbacks
|
|
31
|
-
*
|
|
32
|
-
* will contain a string for the service / key with callbacks for those keys,
|
|
33
|
-
* e.g. {
|
|
34
|
-
* "service1": {
|
|
35
|
-
* "key1": [function1, function2],
|
|
36
|
-
* "key2": [function3]
|
|
37
|
-
* },
|
|
38
|
-
* "service2": {
|
|
39
|
-
* "key3": [function4]
|
|
40
|
-
* }
|
|
41
|
-
* }
|
|
42
|
-
*/
|
|
43
|
-
let __services = {};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Add Track
|
|
47
|
-
*
|
|
48
|
-
* Adds tracking for a specific service key
|
|
49
|
-
*
|
|
50
|
-
* @name _addTrack
|
|
51
|
-
* @access private
|
|
52
|
-
* @param String service The name of the service the key is associated with
|
|
53
|
-
* @param String key The key to track
|
|
54
|
-
* @param Function callback The callback for any messages of the key value
|
|
55
|
-
* @return void
|
|
56
|
-
*/
|
|
57
|
-
function _addTrack(service, key, callback) {
|
|
58
|
-
|
|
59
|
-
// If we don't have the service, add it
|
|
60
|
-
if(!(service in __services)) {
|
|
61
|
-
__services[service] = {}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// If we don't have the key for the given service, add the list with
|
|
65
|
-
// the callback
|
|
66
|
-
if(!(key in __services[service])) {
|
|
67
|
-
__services[service][key] = [callback]
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Else, add the callback, to the given service/key
|
|
71
|
-
else {
|
|
72
|
-
__services[service][key].push(callback);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Handle Close
|
|
78
|
-
*
|
|
79
|
-
* Checks if it's a legitimate closed socket, or if we need to reconnect to
|
|
80
|
-
* everything
|
|
81
|
-
*
|
|
82
|
-
* @name _handleClose
|
|
83
|
-
* @return void
|
|
84
|
-
*/
|
|
85
|
-
function _handleClose() {
|
|
86
|
-
|
|
87
|
-
// If we have a ping interval
|
|
88
|
-
if(__ping) {
|
|
89
|
-
clearInterval(__ping);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// If it's a valid close
|
|
93
|
-
if(__close) {
|
|
94
|
-
__socket = null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Else, wait 5 seconds, and reopen the socket
|
|
98
|
-
else {
|
|
99
|
-
__close = true;
|
|
100
|
-
setTimeout(_openSocket, 5000);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Handle Messages
|
|
106
|
-
*
|
|
107
|
-
* Recieves messages from websockets and directs the data to the appropriate
|
|
108
|
-
* callback
|
|
109
|
-
*
|
|
110
|
-
* @name _handleMessage
|
|
111
|
-
* @access private
|
|
112
|
-
* @param WebSocket sock The socket the message came on
|
|
113
|
-
* @param MessageEvent ev The event message received
|
|
114
|
-
* @return void
|
|
115
|
-
*/
|
|
116
|
-
function _handleMessage(sock, ev) {
|
|
117
|
-
|
|
118
|
-
function stupidBlobs(msg) {
|
|
119
|
-
|
|
120
|
-
// If we got an error
|
|
121
|
-
if(msg.error) {
|
|
122
|
-
events.trigger('error', 'Websocket failed: ' + msg.error.msg + ' (' + msg.error.code + ')');
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// If we have the service
|
|
127
|
-
if(msg.service in __services) {
|
|
128
|
-
|
|
129
|
-
// If we have the key
|
|
130
|
-
if(msg.key in __services[msg.service]) {
|
|
131
|
-
|
|
132
|
-
// Call each callback
|
|
133
|
-
for(let f of __services[msg.service][msg.key]) {
|
|
134
|
-
f(msg.data);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// If we got a string back
|
|
141
|
-
if(typeof ev.data === 'string') {
|
|
142
|
-
|
|
143
|
-
// If we're authorized
|
|
144
|
-
if(ev.data === 'authorized') {
|
|
145
|
-
|
|
146
|
-
// Reset the close flag
|
|
147
|
-
__close = false;
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// If it's pong
|
|
152
|
-
if(ev.data === 'pong') {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Convert it to JSON
|
|
157
|
-
stupidBlobs(JSON.parse(ev.data));
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Else we got a blob
|
|
161
|
-
else {
|
|
162
|
-
|
|
163
|
-
// Screw you javascript
|
|
164
|
-
let r = new FileReader();
|
|
165
|
-
r.addEventListener('loadend', function() {
|
|
166
|
-
|
|
167
|
-
// Parse the data
|
|
168
|
-
let oMsg = JSON.parse(r.result);
|
|
169
|
-
|
|
170
|
-
// Handle it
|
|
171
|
-
stupidBlobs(oMsg);
|
|
172
|
-
});
|
|
173
|
-
r.readAsText(ev.data);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Open Socket
|
|
179
|
-
*
|
|
180
|
-
* Opens a new websocket by first sending a message to webpoll to start the
|
|
181
|
-
* authentication handshake, then making the connection, and finally sending
|
|
182
|
-
* all the track messages stored
|
|
183
|
-
*
|
|
184
|
-
* @name _openSocket
|
|
185
|
-
* @access private
|
|
186
|
-
* @return void
|
|
187
|
-
*/
|
|
188
|
-
function _openSocket() {
|
|
189
|
-
|
|
190
|
-
// Notify the backend of a new ws connection
|
|
191
|
-
rest.read('webpoll', 'websocket', {}).done(res => {
|
|
192
|
-
|
|
193
|
-
// Create the websocket
|
|
194
|
-
__socket = wshelper('wss://' + process.env.REACT_APP_WS_DOMAIN + '/ws', {
|
|
195
|
-
open: function(sock) {
|
|
196
|
-
|
|
197
|
-
// Init the message list
|
|
198
|
-
let lMsgs = [];
|
|
199
|
-
|
|
200
|
-
// Add the connect message
|
|
201
|
-
lMsgs.push({
|
|
202
|
-
_type: 'connect',
|
|
203
|
-
key: res.data
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// Add each track message
|
|
207
|
-
for(let s in __services) {
|
|
208
|
-
for(let k in __services[s]) {
|
|
209
|
-
lMsgs.push({
|
|
210
|
-
_type: 'track',
|
|
211
|
-
service: s,
|
|
212
|
-
key: k
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Send the messages
|
|
218
|
-
sock.send(JSON.stringify(lMsgs))
|
|
219
|
-
},
|
|
220
|
-
message: _handleMessage,
|
|
221
|
-
close: _handleClose
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
// If we got false
|
|
225
|
-
if(__socket === false) {
|
|
226
|
-
events.trigger('error', 'Websockets not supported');
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// If we haven't already setup the ping interval
|
|
231
|
-
if(__ping === null) {
|
|
232
|
-
__ping = setInterval(_ping, 300000);
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Ping
|
|
239
|
-
*
|
|
240
|
-
* Send a ping to keep the socket alive
|
|
241
|
-
*
|
|
242
|
-
* @name _ping
|
|
243
|
-
* @access private
|
|
244
|
-
* @return void
|
|
245
|
-
*/
|
|
246
|
-
function _ping() {
|
|
247
|
-
|
|
248
|
-
// Send a ping message over the socket to keep it alive
|
|
249
|
-
__socket.send(JSON.stringify({
|
|
250
|
-
_type: 'ping'
|
|
251
|
-
}));
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Track
|
|
256
|
-
*
|
|
257
|
-
* Takes a URL and an event type and a) opens a new websocket or uses an
|
|
258
|
-
* existing one, then b) sends a tracking message through the websocket so the
|
|
259
|
-
* backend knowsn to send the key type to us
|
|
260
|
-
*
|
|
261
|
-
* @name track
|
|
262
|
-
* @access public
|
|
263
|
-
* @param String service The name of the service the key is associated with
|
|
264
|
-
* @param String key The key to track
|
|
265
|
-
* @param Function callback The callback for any messages of the key value
|
|
266
|
-
* @return void
|
|
267
|
-
*/
|
|
268
|
-
export function track(service, key, callback) {
|
|
269
|
-
|
|
270
|
-
// Add the tracking callback
|
|
271
|
-
_addTrack(service, key, callback);
|
|
272
|
-
|
|
273
|
-
// If we have no socket
|
|
274
|
-
if(!__socket) {
|
|
275
|
-
|
|
276
|
-
// If it's null
|
|
277
|
-
if(__socket === null) {
|
|
278
|
-
|
|
279
|
-
// Set socket to false so we don't try to re-open
|
|
280
|
-
__socket = false;
|
|
281
|
-
|
|
282
|
-
// Open a new one
|
|
283
|
-
_openSocket();
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// Else if it's open
|
|
288
|
-
else if(__socket.readyState === 1) {
|
|
289
|
-
|
|
290
|
-
// Send the tracking message through the websocket
|
|
291
|
-
__socket.send(JSON.stringify({
|
|
292
|
-
_type: 'track',
|
|
293
|
-
service: service,
|
|
294
|
-
key: key
|
|
295
|
-
}));
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// If we have no socket, or it's opening, then upon opening all services/
|
|
299
|
-
// keys in the tracking list will be sent as messages
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Untrack
|
|
304
|
-
*
|
|
305
|
-
* Removes a callback and notifies the websocket we are not tracking anymore
|
|
306
|
-
*
|
|
307
|
-
* @name _untrack
|
|
308
|
-
* @access public
|
|
309
|
-
* @param String service The name of the service the key is associated with
|
|
310
|
-
* @param String key The key to untrack
|
|
311
|
-
* @param Function callback The callback associated with the track
|
|
312
|
-
* @return bool
|
|
313
|
-
*/
|
|
314
|
-
export function untrack(service, key, callback) {
|
|
315
|
-
|
|
316
|
-
// If we have the service
|
|
317
|
-
if(service in __services) {
|
|
318
|
-
|
|
319
|
-
// If we have the key
|
|
320
|
-
if(key in __services[service]) {
|
|
321
|
-
|
|
322
|
-
// Go through each callback
|
|
323
|
-
for(let i = 0; i < __services[service][key].length; ++i) {
|
|
324
|
-
|
|
325
|
-
// If the callback matches
|
|
326
|
-
if(callback === __services[service][key][i]) {
|
|
327
|
-
|
|
328
|
-
// Remove the callback
|
|
329
|
-
__services[service][key].splice(i, 1);
|
|
330
|
-
|
|
331
|
-
// If we have no more callbacks
|
|
332
|
-
if(__services[service][key].length === 0) {
|
|
333
|
-
|
|
334
|
-
// If we have a socket
|
|
335
|
-
if(__socket && __socket.readyState === 1) {
|
|
336
|
-
|
|
337
|
-
// Notify the websocket we aren't tracking the key
|
|
338
|
-
// anymore
|
|
339
|
-
__socket.send(JSON.stringify({
|
|
340
|
-
_type: 'untrack',
|
|
341
|
-
service: service,
|
|
342
|
-
key: key
|
|
343
|
-
}));
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
// Remove the key
|
|
347
|
-
delete __services[service][key];
|
|
348
|
-
|
|
349
|
-
// If we have no more keys in the service
|
|
350
|
-
if(empty(__services[service])) {
|
|
351
|
-
|
|
352
|
-
// Remove the service
|
|
353
|
-
delete __services[service];
|
|
354
|
-
|
|
355
|
-
// If there's no more services
|
|
356
|
-
if(empty(__services)) {
|
|
357
|
-
|
|
358
|
-
// Turn off the ping interval
|
|
359
|
-
clearInterval(__ping);
|
|
360
|
-
__ping = null;
|
|
361
|
-
|
|
362
|
-
// Close the socket
|
|
363
|
-
__close = true;
|
|
364
|
-
|
|
365
|
-
// If we have a socket
|
|
366
|
-
if(__socket) {
|
|
367
|
-
__socket.close(1000, 'nothing else to track');
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// Callback found and removed
|
|
374
|
-
return true;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// Callback not found
|
|
381
|
-
return false;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// Default export
|
|
385
|
-
const twoway = {
|
|
386
|
-
track: track,
|
|
387
|
-
untrack: untrack
|
|
388
|
-
};
|
|
389
|
-
export default twoway;
|
package/wshelper.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Web Socket Helper
|
|
3
|
-
*
|
|
4
|
-
* Simplifies using a websocket
|
|
5
|
-
*
|
|
6
|
-
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
7
|
-
* @copyright Ouroboros Coding Inc.
|
|
8
|
-
* @created 2017-05-17
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// Export the function
|
|
12
|
-
export default function wshelper(url, conf, debug=false) {
|
|
13
|
-
|
|
14
|
-
// If we can't handle websockets
|
|
15
|
-
if(!('WebSocket' in window)) {
|
|
16
|
-
console.error('websocket: WebSockets not supported');
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// If conf is not defined
|
|
21
|
-
if(typeof conf == 'undefined') {
|
|
22
|
-
conf = {}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Else, if it's not an object, we have a problem
|
|
26
|
-
else if(typeof conf != 'object') {
|
|
27
|
-
console.error('websocket: second argument must be an object');
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Create the WebSocket
|
|
32
|
-
let oSock = new WebSocket(url);
|
|
33
|
-
|
|
34
|
-
// Set the open callback
|
|
35
|
-
oSock.onopen = function() {
|
|
36
|
-
|
|
37
|
-
// If an open callback is set
|
|
38
|
-
if('open' in conf) {
|
|
39
|
-
conf['open'](oSock);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Else, just log the event
|
|
43
|
-
else if(debug) {
|
|
44
|
-
console.log('websocket: opened');
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Set the message callback
|
|
49
|
-
oSock.onmessage = function(ev) {
|
|
50
|
-
|
|
51
|
-
// If a message callback is set
|
|
52
|
-
if('message' in conf) {
|
|
53
|
-
conf['message'](oSock, ev);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Else, just log the event
|
|
57
|
-
else if(debug) {
|
|
58
|
-
console.log('websocket: message received, "' + ev.data + '"');
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Set the error callback
|
|
63
|
-
oSock.onerror = function(ev) {
|
|
64
|
-
|
|
65
|
-
// If an error callback is set
|
|
66
|
-
if('error' in conf) {
|
|
67
|
-
conf['error'](oSock, ev);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Else, just log the event
|
|
71
|
-
else if(debug) {
|
|
72
|
-
console.log('websocket: error, "' + JSON.stringify(ev) + '"');
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Set the close callback
|
|
77
|
-
oSock.onclose = function() {
|
|
78
|
-
|
|
79
|
-
// If a close callback is set
|
|
80
|
-
if('close' in conf) {
|
|
81
|
-
conf['close'](oSock);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Else, just log the event
|
|
85
|
-
else if(debug) {
|
|
86
|
-
console.log('websocket: closed');
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Return the socket
|
|
91
|
-
return oSock;
|
|
92
|
-
}
|