relayx-js 1.0.12 → 1.0.14

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
@@ -156,7 +156,8 @@ This event is fired when the library resends the messages upon reconnection to t
156
156
 
157
157
  ## API Reference
158
158
  1. init()<br>
159
- Initializes library with configuration options
159
+ Initializes library with configuration options.
160
+ * debug (boolean): enables library level logging
160
161
  2. connect()<br>
161
162
  Connects the library to the Relay Network. This is an async function.
162
163
  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.14",
4
4
  "main": "realtime/realtime.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -17,12 +17,11 @@
17
17
  "license": "Apache 2.0",
18
18
  "description": "A powerful library for integrating real-time communication into your software stack, powered by the Relay Network.",
19
19
  "dependencies": {
20
- "@msgpack/msgpack": "^3.1.1",
21
- "@nats-io/jetstream": "^3.0.0-35",
22
- "axios": "^1.8.4",
23
- "jest": "^29.7.0",
24
- "nats": "^2.28.2",
25
- "uuid": "^11.1.0"
20
+ "@msgpack/msgpack": "3.1.1",
21
+ "@nats-io/jetstream": "3.0.0-35",
22
+ "jest": "29.7.0",
23
+ "nats": "2.28.2",
24
+ "uuid": "11.1.0"
26
25
  },
27
26
  "devDependencies": {
28
27
  "@babel/core": "^7.26.0",
@@ -1,4 +1,3 @@
1
- import axios from 'axios';
2
1
  import { connect, JSONCodec, Events, DebugEvents, AckPolicy, ReplayPolicy, credsAuthenticator } from "nats";
3
2
  import { DeliverPolicy, jetstream } from "@nats-io/jetstream";
4
3
  import { encode, decode } from "@msgpack/msgpack";
@@ -108,6 +107,7 @@ export class Realtime {
108
107
  }
109
108
 
110
109
  this.staging = staging;
110
+ this.opts = opts;
111
111
 
112
112
  if (staging !== undefined || staging !== null){
113
113
  this.#baseUrl = staging ? [
@@ -116,22 +116,20 @@ export class Realtime {
116
116
  "nats://0.0.0.0:4223"
117
117
  ] :
118
118
  [
119
- "tls://api.relay-x.io:4221",
120
- "tls://api.relay-x.io:4222",
121
- "tls://api.relay-x.io:4223"
119
+ `tls://api.relay-x.io:4221`,
120
+ `tls://api.relay-x.io:4222`,
121
+ `tls://api.relay-x.io:4223`
122
122
  ];
123
123
  }else{
124
124
  this.#baseUrl = [
125
- "tls://api.relay-x.io:4221",
126
- "tls://api.relay-x.io:4222",
127
- "tls://api.relay-x.io:4223"
125
+ `tls://api.relay-x.io:4221`,
126
+ `tls://api.relay-x.io:4222`,
127
+ `tls://api.relay-x.io:4223`
128
128
  ];
129
129
  }
130
130
 
131
131
  this.#log(this.#baseUrl);
132
132
  this.#log(opts);
133
-
134
- this.opts = opts;
135
133
  }
136
134
 
137
135
  /**
@@ -210,6 +208,12 @@ export class Realtime {
210
208
  this.#log("the connection closed!");
211
209
 
212
210
  this.#offlineMessageBuffer.length = 0;
211
+
212
+ if (DISCONNECTED in this.#event_func){
213
+ if (this.#event_func[DISCONNECTED] !== null || this.#event_func[DISCONNECTED] !== undefined){
214
+ this.#event_func[DISCONNECTED]()
215
+ }
216
+ }
213
217
  });
214
218
 
215
219
  (async () => {
@@ -402,6 +406,10 @@ export class Realtime {
402
406
  throw new Error("Invalid topic, use isTopicValid($topic) to validate topic")
403
407
  }
404
408
 
409
+ if(!this.#isMessageValid(data)){
410
+ throw new Error("$message must be JSON, string or number")
411
+ }
412
+
405
413
  var start = Date.now()
406
414
  var messageId = crypto.randomUUID();
407
415
 
@@ -736,6 +744,35 @@ export class Realtime {
736
744
  }
737
745
  }
738
746
 
747
+ #isMessageValid(message){
748
+ if(message == null || message == undefined){
749
+ throw new Error("$message cannot be null / undefined")
750
+ }
751
+
752
+ if(typeof message == "string"){
753
+ return true;
754
+ }
755
+
756
+ if(typeof message == "number"){
757
+ return true;
758
+ }
759
+
760
+ if(this.#isJSON(message)){
761
+ return true;
762
+ }
763
+
764
+ return false;
765
+ }
766
+
767
+ #isJSON(data){
768
+ try{
769
+ JSON.stringify(data?.toString())
770
+ return true;
771
+ }catch(err){
772
+ return false
773
+ }
774
+ }
775
+
739
776
  #getStreamName(){
740
777
  if(this.namespace != null){
741
778
  return this.namespace + "_stream"