relayx-js 1.0.12 → 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.12",
3
+ "version": "1.0.13",
4
4
  "main": "realtime/realtime.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -108,6 +108,14 @@ 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 ? [
@@ -116,22 +124,20 @@ export class Realtime {
116
124
  "nats://0.0.0.0:4223"
117
125
  ] :
118
126
  [
119
- "tls://api.relay-x.io:4221",
120
- "tls://api.relay-x.io:4222",
121
- "tls://api.relay-x.io:4223"
127
+ `${protocol}://api.relay-x.io:4221`,
128
+ `${protocol}://api.relay-x.io:4222`,
129
+ `${protocol}://api.relay-x.io:4223`
122
130
  ];
123
131
  }else{
124
132
  this.#baseUrl = [
125
- "tls://api.relay-x.io:4221",
126
- "tls://api.relay-x.io:4222",
127
- "tls://api.relay-x.io:4223"
133
+ `${protocol}://api.relay-x.io:4221`,
134
+ `${protocol}://api.relay-x.io:4222`,
135
+ `${protocol}://api.relay-x.io:4223`
128
136
  ];
129
137
  }
130
138
 
131
139
  this.#log(this.#baseUrl);
132
140
  this.#log(opts);
133
-
134
- this.opts = opts;
135
141
  }
136
142
 
137
143
  /**
@@ -402,6 +408,10 @@ export class Realtime {
402
408
  throw new Error("Invalid topic, use isTopicValid($topic) to validate topic")
403
409
  }
404
410
 
411
+ if(!this.#isMessageValid(data)){
412
+ throw new Error("$message must be JSON, string or number")
413
+ }
414
+
405
415
  var start = Date.now()
406
416
  var messageId = crypto.randomUUID();
407
417
 
@@ -736,6 +746,35 @@ export class Realtime {
736
746
  }
737
747
  }
738
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
+
739
778
  #getStreamName(){
740
779
  if(this.namespace != null){
741
780
  return this.namespace + "_stream"