@osimatic/helpers-js 1.0.50 → 1.0.53

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/event_bus.js CHANGED
@@ -10,7 +10,9 @@ class EventBus {
10
10
  return;
11
11
  }
12
12
 
13
- handlers.forEach((handler) => handler.call(this, data));
13
+ handlers.forEach(function(handler) {
14
+ handler.call(this, data)
15
+ });
14
16
  }
15
17
 
16
18
  subscribe(name, handler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.50",
3
+ "version": "1.0.53",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/web_rtc.js CHANGED
@@ -4,14 +4,14 @@ class WebRTC {
4
4
  this.stunUrl = stunUrl;
5
5
  }
6
6
 
7
- static setTurnAccount(turnAccount) {
8
- this.turnAccount = turnAccount;
7
+ static setTurnSecret(turnSecret) {
8
+ this.turnSecret = turnSecret;
9
9
  }
10
10
 
11
11
  static offer(stream, iceCandidateCallback) {
12
12
  return new Promise((resolve, reject) => {
13
13
  try {
14
- let { username, password } = this.turnAccount;
14
+ let { username, password } = this.getTurnCredentials();
15
15
  let peerConn = new RTCPeerConnection(
16
16
  {
17
17
  iceServers: [
@@ -40,7 +40,7 @@ class WebRTC {
40
40
  static answer (remoteDescription, onTrackCallback, iceCandidateCallback) {
41
41
  return new Promise((resolve, reject) => {
42
42
  try {
43
- let { username, password } = this.turnAccount;
43
+ let { username, password } = this.getTurnCredentials();
44
44
  let peerConn = new RTCPeerConnection(
45
45
  {
46
46
  iceServers: [
@@ -82,6 +82,23 @@ class WebRTC {
82
82
 
83
83
  return peerConn;
84
84
  }
85
+
86
+ /*
87
+ static-auth credentials
88
+ https://eturnal.net/documentation/
89
+ https://datatracker.ietf.org/doc/html/draft-uberti-behave-turn-rest-00
90
+ */
91
+ static getTurnCredentials() {
92
+ let crypto = require('crypto');
93
+ let username = String(parseInt(Date.now() / 1000) + 24 * 3600); //ttl: 24h
94
+ let hmac = crypto.createHmac('sha1', this.turnSecret);
95
+
96
+ hmac.setEncoding('base64');
97
+ hmac.write(username);
98
+ hmac.end();
99
+
100
+ return { username: username, password: hmac.read() };
101
+ }
85
102
  }
86
103
 
87
104
  module.exports = { WebRTC };
package/web_socket.js CHANGED
@@ -8,53 +8,91 @@ class WebSocket {
8
8
  const { EventBus } = require('./event_bus');
9
9
 
10
10
  this.eventBus = new EventBus();
11
+ this.socketConnectionErrors = ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed'];
11
12
  this.socketEvents = socketEvents;
12
13
  this.busEvents = busEvents;
13
14
  this.customBusEvents = customBusEvents;
14
15
  }
15
16
 
16
- static connect(url, options, connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
17
- const io = require('socket.io-client');
18
-
19
- this.logged = false;
17
+ static connect(url, options, connectInitPayload, onUnavailableCallback, onConnectionAckCallback) {
18
+ const { io } = require('socket.io-client');
19
+
20
+ this.logged = false;
20
21
  this.instance = io(url, options);
21
22
 
22
- this.initEventListeners();
23
- this.initConnectionListeners(onUnavailableCallback);
23
+ this.registerErrorListeners(onUnavailableCallback);
24
+ this.registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback);
25
+ }
26
+
27
+ static registerErrorListeners(onUnavailableCallback) {
28
+ var self = this;
29
+
30
+ self.socketConnectionErrors.forEach(function(event) {
31
+ self.instance.on(event, function() {
32
+ self.clear(onUnavailableCallback, event);
33
+ });
34
+ });
35
+
36
+ self.instance.on('error', function(error) {
37
+ self.clear(onUnavailableCallback, error);
38
+ });
39
+ }
24
40
 
25
- this.instance.on('connect', () => {
26
- if (this.logged) {
27
- this.clear(onUnavailableCallback);
41
+ static registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
42
+ var self = this;
43
+
44
+ self.instance.on('connect', function() {
45
+ if (self.logged) { //reconnexion avant timeout : on supprime les listeners de l'event bus
46
+ self.clear();
28
47
  }
29
48
 
30
- this.logged = true;
31
- this.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
49
+ self.logged = true;
50
+
51
+ self.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
52
+ });
53
+
54
+ self.instance.on('disconnect', function() {
55
+ self.clear(onUnavailableCallback);
56
+ });
57
+
58
+ self.socketEvents.forEach(function (event) {
59
+ self.instance.on(event, function(payload) {
60
+ self.eventBus.publish(event, payload);
61
+ });
62
+ });
63
+
64
+ self.busEvents.forEach(function(event) {
65
+ self.eventBus.subscribe(event, function(payload) {
66
+ self.instance.emit(event, payload);
67
+ });
68
+ });
69
+
70
+ self.customBusEvents.forEach(function(object) {
71
+ self.eventBus.subscribe(object.custom_name, function(payload) {
72
+ self.instance.emit(object.event_name, payload)
73
+ });
32
74
  });
33
75
  }
34
76
 
35
- static initConnectionListeners(onUnavailableCallback) {
36
- ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed', 'disconnect'].forEach(event => this.instance.on(event, () => this.clear(event, onUnavailableCallback)));
37
- this.instance.on('error', (error) => this.clear(error, onUnavailableCallback));
77
+ static unregisterEventListener() {
78
+ var self = this;
79
+
80
+ self.busEvents.forEach(function (event) {
81
+ self.eventBus.unsubscribe(event, {});
82
+ });
83
+
84
+ self.customBusEvents.forEach(function(object) {
85
+ self.eventBus.unsubscribe(object.custom_name, {});
86
+ });
38
87
  }
39
88
 
40
- static clear(error, onUnavailableCallback) {
89
+ static clear(onUnavailableCallback, error) {
41
90
  this.unregisterEventListener();
42
-
43
- if (onUnavailableCallback != 'undefined') {
91
+
92
+ if (typeof onUnavailableCallback !== 'undefined') {
44
93
  onUnavailableCallback(error);
45
94
  }
46
95
  }
47
-
48
- static initEventListeners() {
49
- this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
50
- this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
51
- this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
52
- }
53
-
54
- static unregisterEventListener() {
55
- this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
56
- this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
57
- }
58
96
  }
59
97
 
60
98
  module.exports = { WebSocket };