relayx-js 1.0.3 → 1.0.4

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.
@@ -65,6 +65,8 @@ async function run(){
65
65
  }else if(input == "close"){
66
66
  realtime.close();
67
67
  console.log("Connection closed");
68
+ }else if(input == "init"){
69
+ await realtime.connect()
68
70
  }else if(input == "on"){
69
71
  rl.question("topic: ", async (topic) => {
70
72
  await realtime.on(topic, (data) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayx-js",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "main": "realtime/realtime.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -251,7 +251,7 @@ export class Realtime {
251
251
  this.reconnecting = false;
252
252
  this.connected = true;
253
253
 
254
- this.#subscribeToTopics();
254
+ // this.#subscribeToTopics();
255
255
 
256
256
  if(RECONNECT in this.#event_func){
257
257
  this.#event_func[RECONNECT](this.#RECONNECTED);
@@ -356,11 +356,22 @@ export class Realtime {
356
356
  }
357
357
 
358
358
  if(!(topic in this.#event_func)){
359
- this.#event_func[topic] = func;
359
+ this.#event_func[topic] = func;
360
+ }else{
361
+ return false
360
362
  }
361
363
 
362
364
  if (![CONNECTED, DISCONNECTED, RECONNECT, this.#RECONNECTED,
363
365
  this.#RECONNECTING, this.#RECONN_FAIL, MESSAGE_RESEND].includes(topic)){
366
+ if(!this.isTopicValid(topic)){
367
+ // We have an invalid topic, lets remove it
368
+ if(topic in this.#event_func){
369
+ delete this.#event_func[topic];
370
+ }
371
+
372
+ throw new Error("Invalid topic, use isTopicValid($topic) to validate topic")
373
+ }
374
+
364
375
  if(!this.#topicMap.includes(topic)){
365
376
  this.#topicMap.push(topic);
366
377
  }
@@ -370,6 +381,8 @@ export class Realtime {
370
381
  await this.#startConsumer(topic);
371
382
  }
372
383
  }
384
+
385
+ return true;
373
386
  }
374
387
 
375
388
  /**
@@ -533,7 +546,7 @@ export class Realtime {
533
546
  if (consumer != null && consumer != undefined){
534
547
  del = await consumer.delete();
535
548
  }else{
536
- del = true
549
+ del = false
537
550
  }
538
551
 
539
552
  delete this.#consumerMap[topic];
@@ -586,8 +599,12 @@ export class Realtime {
586
599
  */
587
600
  isTopicValid(topic){
588
601
  if(topic !== null && topic !== undefined && (typeof topic) == "string"){
589
- return ![CONNECTED, DISCONNECTED, RECONNECT, this.#RECONNECTED,
602
+ var arrayCheck = ![CONNECTED, DISCONNECTED, RECONNECT, this.#RECONNECTED,
590
603
  this.#RECONNECTING, this.#RECONN_FAIL, MESSAGE_RESEND].includes(topic);
604
+
605
+ var spaceStarCheck = !topic.includes(" ") && !topic.includes("*");
606
+
607
+ return arrayCheck && spaceStarCheck;
591
608
  }else{
592
609
  return false;
593
610
  }
package/tests/test.js CHANGED
@@ -322,7 +322,8 @@ test("on() test", async () => {
322
322
 
323
323
  //---------------------------------------------------------------
324
324
 
325
- await realtime.on("hello_world", () => {});
325
+ var res = await realtime.on("hello_world", () => {});
326
+ assert.strictEqual(res, true)
326
327
 
327
328
  var eventFunc = realtime.testGetEventMap();
328
329
  var topicMap = realtime.testGetTopicMap();
@@ -333,6 +334,16 @@ test("on() test", async () => {
333
334
  assert.notStrictEqual(eventFunc["hello_world"], null)
334
335
  assert.notStrictEqual(eventFunc["hello_world"], undefined)
335
336
  assert.strictEqual(typeof eventFunc["hello_world"], "function")
337
+
338
+ // Realtime already has a reference of this topic, so the return val will be false
339
+ res = await realtime.on("hello_world", () => {});
340
+ assert.strictEqual(res, false)
341
+
342
+ res = await realtime.on("hello_world", () => {});
343
+ assert.strictEqual(res, false)
344
+
345
+ res = await realtime.on("hello_world", () => {});
346
+ assert.strictEqual(res, false)
336
347
  });
337
348
 
338
349
  test("off() test", async () => {
@@ -370,6 +381,21 @@ test("off() test", async () => {
370
381
  assert.strictEqual(!topicMap.includes("hello"), true)
371
382
  assert.strictEqual(eventFunc["hello"], undefined)
372
383
  assert.strictEqual(consumerMap["hello"], undefined)
384
+
385
+ // Turning off topic multiple times to check for crashes.
386
+ // Since it is off already, output will be false
387
+ var status = await realtime.off("hello");
388
+ assert.strictEqual(status, false)
389
+
390
+ var status = await realtime.off("hello");
391
+ assert.strictEqual(status, false)
392
+
393
+ var status = await realtime.off("hello");
394
+ assert.strictEqual(status, false)
395
+
396
+ var status = await realtime.off("hello");
397
+ assert.strictEqual(status, false)
398
+
373
399
  });
374
400
 
375
401
  test("Get stream name test", () => {