relayx-js 1.0.11 → 1.0.13

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/README.md CHANGED
@@ -22,7 +22,9 @@ Install the relay library by running the command below in your terminal<br>
22
22
  api_key: process.env.api_key,
23
23
  secret: process.env.secret,
24
24
  });
25
- realtime.init();
25
+ realtime.init({
26
+ browser_mode: true // Enable when using in webapps
27
+ });
26
28
 
27
29
  // Initialization of topic listeners go here... (look at examples/example_chat.js for full implementation)
28
30
 
@@ -156,7 +158,9 @@ This event is fired when the library resends the messages upon reconnection to t
156
158
 
157
159
  ## API Reference
158
160
  1. init()<br>
159
- Initializes library with configuration options
161
+ Initializes library with configuration options.
162
+ * debug (boolean): enables library level logging
163
+ * browser_mode (boolean): Allows websocket connections to be made from a browser. Default is TCP
160
164
  2. connect()<br>
161
165
  Connects the library to the Relay Network. This is an async function.
162
166
  3. close()<br>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayx-js",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "main": "realtime/realtime.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -108,39 +108,36 @@ export class Realtime {
108
108
  }
109
109
 
110
110
  this.staging = staging;
111
+ this.opts = opts;
112
+
113
+ var browserMode = this.opts["browser_mode"];
114
+ var protocol = "tls"
115
+
116
+ if(browserMode != null && browserMode != undefined && (typeof browserMode == "boolean")){
117
+ protocol = browserMode ? "wss" : "tls"
118
+ }
111
119
 
112
120
  if (staging !== undefined || staging !== null){
113
121
  this.#baseUrl = staging ? [
114
122
  "nats://0.0.0.0:4221",
115
123
  "nats://0.0.0.0:4222",
116
- "nats://0.0.0.0:4223",
117
- "nats://0.0.0.0:4224",
118
- "nats://0.0.0.0:4225",
119
- "nats://0.0.0.0:4226"
124
+ "nats://0.0.0.0:4223"
120
125
  ] :
121
126
  [
122
- "nats://api.relay-x.io:4221",
123
- "nats://api.relay-x.io:4222",
124
- "nats://api.relay-x.io:4223",
125
- "nats://api.relay-x.io:4224",
126
- "nats://api.relay-x.io:4225",
127
- "nats://api.relay-x.io:4226",
127
+ `${protocol}://api.relay-x.io:4221`,
128
+ `${protocol}://api.relay-x.io:4222`,
129
+ `${protocol}://api.relay-x.io:4223`
128
130
  ];
129
131
  }else{
130
132
  this.#baseUrl = [
131
- "nats://api.relay-x.io:4221",
132
- "nats://api.relay-x.io:4222",
133
- "nats://api.relay-x.io:4223",
134
- "nats://api.relay-x.io:4224",
135
- "nats://api.relay-x.io:4225",
136
- "nats://api.relay-x.io:4226",
133
+ `${protocol}://api.relay-x.io:4221`,
134
+ `${protocol}://api.relay-x.io:4222`,
135
+ `${protocol}://api.relay-x.io:4223`
137
136
  ];
138
137
  }
139
138
 
140
139
  this.#log(this.#baseUrl);
141
140
  this.#log(opts);
142
-
143
- this.opts = opts;
144
141
  }
145
142
 
146
143
  /**
@@ -186,8 +183,8 @@ export class Realtime {
186
183
  this.#natsClient = await connect({
187
184
  servers: this.SEVER_URL,
188
185
  noEcho: true,
189
- maxReconnectAttempts: 1200,
190
186
  reconnect: true,
187
+ maxReconnectAttempts: 1200,
191
188
  reconnectTimeWait: 1000,
192
189
  authenticator: credsAuth,
193
190
  token: this.api_key,
@@ -217,6 +214,8 @@ export class Realtime {
217
214
 
218
215
  this.#natsClient.closed().then(() => {
219
216
  this.#log("the connection closed!");
217
+
218
+ this.#offlineMessageBuffer.length = 0;
220
219
  });
221
220
 
222
221
  (async () => {
@@ -291,6 +290,8 @@ export class Realtime {
291
290
  if(this.#natsClient !== null){
292
291
  this.reconnected = false;
293
292
  this.disconnected = true;
293
+
294
+ this.#offlineMessageBuffer.length = 0;
294
295
 
295
296
  this.#natsClient.close();
296
297
  }else{
@@ -407,6 +408,10 @@ export class Realtime {
407
408
  throw new Error("Invalid topic, use isTopicValid($topic) to validate topic")
408
409
  }
409
410
 
411
+ if(!this.#isMessageValid(data)){
412
+ throw new Error("$message must be JSON, string or number")
413
+ }
414
+
410
415
  var start = Date.now()
411
416
  var messageId = crypto.randomUUID();
412
417
 
@@ -741,6 +746,35 @@ export class Realtime {
741
746
  }
742
747
  }
743
748
 
749
+ #isMessageValid(message){
750
+ if(message == null || message == undefined){
751
+ throw new Error("$message cannot be null / undefined")
752
+ }
753
+
754
+ if(typeof message == "string"){
755
+ return true;
756
+ }
757
+
758
+ if(typeof message == "number"){
759
+ return true;
760
+ }
761
+
762
+ if(this.#isJSON(message)){
763
+ return true;
764
+ }
765
+
766
+ return false;
767
+ }
768
+
769
+ #isJSON(data){
770
+ try{
771
+ JSON.stringify(data?.toString())
772
+ return true;
773
+ }catch(err){
774
+ return false
775
+ }
776
+ }
777
+
744
778
  #getStreamName(){
745
779
  if(this.namespace != null){
746
780
  return this.namespace + "_stream"