laravel-echo-api-gateway 0.5.2 → 0.5.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.
@@ -1,3 +1,5 @@
1
+ import axios from 'axios';
2
+
1
3
  function _classCallCheck(instance, Constructor) {
2
4
  if (!(instance instanceof Constructor)) {
3
5
  throw new TypeError("Cannot call a class as a function");
@@ -199,58 +201,59 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
199
201
  };
200
202
  }
201
203
 
202
- class Connector {
203
- /**
204
- * Create a new class instance.
205
- */
206
- constructor(options) {
207
- /**
208
- * Default connector options.
209
- */
210
- this._defaultOptions = {
211
- auth: {
212
- headers: {},
213
- },
214
- authEndpoint: '/broadcasting/auth',
215
- broadcaster: 'pusher',
216
- csrfToken: null,
217
- host: null,
218
- key: null,
219
- namespace: 'App.Events',
220
- };
221
- this.setOptions(options);
222
- this.connect();
223
- }
224
- /**
225
- * Merge the custom options with the defaults.
226
- */
227
- setOptions(options) {
228
- this.options = Object.assign(this._defaultOptions, options);
229
- if (this.csrfToken()) {
230
- this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken();
231
- }
232
- return options;
233
- }
234
- /**
235
- * Extract the CSRF token from the page.
236
- */
237
- csrfToken() {
238
- let selector;
239
- if (typeof window !== 'undefined' && window['Laravel'] && window['Laravel'].csrfToken) {
240
- return window['Laravel'].csrfToken;
241
- }
242
- else if (this.options.csrfToken) {
243
- return this.options.csrfToken;
244
- }
245
- else if (typeof document !== 'undefined' &&
246
- typeof document.querySelector === 'function' &&
247
- (selector = document.querySelector('meta[name="csrf-token"]'))) {
248
- return selector.getAttribute('content');
249
- }
250
- return null;
251
- }
204
+ class Connector {
205
+ /**
206
+ * Create a new class instance.
207
+ */
208
+ constructor(options) {
209
+ /**
210
+ * Default connector options.
211
+ */
212
+ this._defaultOptions = {
213
+ auth: {
214
+ headers: {},
215
+ },
216
+ authEndpoint: '/broadcasting/auth',
217
+ broadcaster: 'pusher',
218
+ csrfToken: null,
219
+ host: null,
220
+ key: null,
221
+ namespace: 'App.Events',
222
+ };
223
+ this.setOptions(options);
224
+ this.connect();
225
+ }
226
+ /**
227
+ * Merge the custom options with the defaults.
228
+ */
229
+ setOptions(options) {
230
+ this.options = Object.assign(this._defaultOptions, options);
231
+ if (this.csrfToken()) {
232
+ this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken();
233
+ }
234
+ return options;
235
+ }
236
+ /**
237
+ * Extract the CSRF token from the page.
238
+ */
239
+ csrfToken() {
240
+ let selector;
241
+ if (typeof window !== 'undefined' && window['Laravel'] && window['Laravel'].csrfToken) {
242
+ return window['Laravel'].csrfToken;
243
+ }
244
+ else if (this.options.csrfToken) {
245
+ return this.options.csrfToken;
246
+ }
247
+ else if (typeof document !== 'undefined' &&
248
+ typeof document.querySelector === 'function' &&
249
+ (selector = document.querySelector('meta[name="csrf-token"]'))) {
250
+ return selector.getAttribute('content');
251
+ }
252
+ return null;
253
+ }
252
254
  }
253
255
 
256
+ var LOG_PREFIX = '[LE-AG-Websocket]';
254
257
  var Websocket = /*#__PURE__*/function () {
255
258
  function Websocket(options) {
256
259
  _classCallCheck(this, Websocket);
@@ -271,7 +274,12 @@ var Websocket = /*#__PURE__*/function () {
271
274
  value: function connect(host) {
272
275
  var _this = this;
273
276
 
274
- this.options.debug && console.log('Trying to connect...');
277
+ if (!host) {
278
+ this.options.debug && console.error(LOG_PREFIX + "Cannont connect without host !");
279
+ return;
280
+ }
281
+
282
+ this.options.debug && console.log(LOG_PREFIX + "Trying to connect to ".concat(host, "..."));
275
283
  this.websocket = new WebSocket(host);
276
284
 
277
285
  this.websocket.onerror = function () {
@@ -285,7 +293,7 @@ var Websocket = /*#__PURE__*/function () {
285
293
  };
286
294
 
287
295
  this.websocket.onopen = function () {
288
- _this.options.debug && console.log('Connected !');
296
+ _this.options.debug && console.log(LOG_PREFIX + ' Connected !');
289
297
  _this.hasConnected = true;
290
298
 
291
299
  _this.send({
@@ -304,14 +312,14 @@ var Websocket = /*#__PURE__*/function () {
304
312
  _this.websocket.onmessage = function (messageEvent) {
305
313
  var message = _this.parseMessage(messageEvent.data);
306
314
 
307
- _this.options.debug && console.log('onmessage', messageEvent.data);
315
+ _this.options.debug && console.log(LOG_PREFIX + ' onmessage', messageEvent.data);
308
316
 
309
317
  if (!message) {
310
318
  return;
311
319
  }
312
320
 
313
321
  if (message.channel) {
314
- _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
322
+ _this.options.debug && console.log("".concat(LOG_PREFIX, " Received event ").concat(message.event, " on channel ").concat(message.channel));
315
323
 
316
324
  if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
317
325
  _this.listeners[message.channel][message.event](message.data);
@@ -328,7 +336,7 @@ var Websocket = /*#__PURE__*/function () {
328
336
 
329
337
  _this.pingInterval = setInterval(function () {
330
338
  if (_this.websocket.readyState === _this.websocket.OPEN) {
331
- _this.options.debug && console.log('Sending ping');
339
+ _this.options.debug && console.log(LOG_PREFIX + ' Sending ping');
332
340
 
333
341
  _this.send({
334
342
  event: 'ping'
@@ -356,7 +364,7 @@ var Websocket = /*#__PURE__*/function () {
356
364
  this.on('whoami', function (_ref) {
357
365
  var socketId = _ref.socket_id;
358
366
  _this.socketId = socketId;
359
- _this.options.debug && console.log("just set socketId to ".concat(socketId)); // Handle the backlog and don't empty it, we'll need it if we lose connection
367
+ _this.options.debug && console.log("".concat(LOG_PREFIX, " Just set socketId to ").concat(socketId)); // Handle the backlog and don't empty it, we'll need it if we lose connection
360
368
 
361
369
  var channel;
362
370
 
@@ -421,6 +429,7 @@ var Websocket = /*#__PURE__*/function () {
421
429
  if (this.getSocketId()) {
422
430
  this.actuallySubscribe(channel);
423
431
  } else {
432
+ this.options.debug && console.log("".concat(LOG_PREFIX, " subscribe - push channel backlog for channel ").concat(channel.name));
424
433
  this.channelBacklog.push(channel);
425
434
  }
426
435
  }
@@ -430,7 +439,7 @@ var Websocket = /*#__PURE__*/function () {
430
439
  var _this2 = this;
431
440
 
432
441
  if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
433
- this.options.debug && console.log("Sending auth request for channel ".concat(channel.name));
442
+ this.options.debug && console.log("".concat(LOG_PREFIX, " Sending auth request for channel ").concat(channel.name));
434
443
 
435
444
  if (this.options.bearerToken) {
436
445
  this.options.auth.headers['Authorization'] = 'Bearer ' + this.options.bearerToken;
@@ -442,7 +451,7 @@ var Websocket = /*#__PURE__*/function () {
442
451
  }, {
443
452
  headers: this.options.auth.headers || {}
444
453
  }).then(function (response) {
445
- _this2.options.debug && console.log("Subscribing to channels ".concat(channel.name));
454
+ _this2.options.debug && console.log("".concat(LOG_PREFIX, " Subscribing to private channel ").concat(channel.name));
446
455
 
447
456
  _this2.send({
448
457
  event: 'subscribe',
@@ -451,11 +460,11 @@ var Websocket = /*#__PURE__*/function () {
451
460
  }, response.data)
452
461
  });
453
462
  })["catch"](function (error) {
454
- _this2.options.debug && console.log("Auth request for channel ".concat(channel.name, " failed"));
463
+ _this2.options.debug && console.log("".concat(LOG_PREFIX, " Auth request for channel ").concat(channel.name, " failed"));
455
464
  _this2.options.debug && console.error(error);
456
465
  });
457
466
  } else {
458
- this.options.debug && console.log("Subscribing to channels ".concat(channel.name));
467
+ this.options.debug && console.log("".concat(LOG_PREFIX, " Subscribing to channel ").concat(channel.name));
459
468
  this.send({
460
469
  event: 'subscribe',
461
470
  data: {
@@ -467,6 +476,7 @@ var Websocket = /*#__PURE__*/function () {
467
476
  }, {
468
477
  key: "unsubscribe",
469
478
  value: function unsubscribe(channel) {
479
+ this.options.debug && console.log("".concat(LOG_PREFIX, " unsubscribe for channel ").concat(channel.name));
470
480
  this.send({
471
481
  event: 'unsubscribe',
472
482
  data: {
@@ -482,11 +492,14 @@ var Websocket = /*#__PURE__*/function () {
482
492
  key: "on",
483
493
  value: function on(event) {
484
494
  var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
495
+ this.options.debug && console.log("".concat(LOG_PREFIX, " on event ").concat(event, " ..."));
485
496
  this.internalListeners[event] = callback;
486
497
  }
487
498
  }, {
488
499
  key: "bind",
489
500
  value: function bind(channel, event, callback) {
501
+ this.options.debug && console.log("".concat(LOG_PREFIX, " bind event ").concat(event, " for channel ").concat(channel.name, " ..."));
502
+
490
503
  if (!this.listeners[channel.name]) {
491
504
  this.listeners[channel.name] = {};
492
505
  }
@@ -497,6 +510,7 @@ var Websocket = /*#__PURE__*/function () {
497
510
  key: "unbindEvent",
498
511
  value: function unbindEvent(channel, event) {
499
512
  var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
513
+ this.options.debug && console.log("".concat(LOG_PREFIX, " unbind event ").concat(event, " for channel ").concat(channel.name, " ..."));
500
514
 
501
515
  if (this.internalListeners[event] && (callback === null || this.internalListeners[event] === callback)) {
502
516
  delete this.internalListeners[event];
@@ -507,62 +521,63 @@ var Websocket = /*#__PURE__*/function () {
507
521
  return Websocket;
508
522
  }();
509
523
 
510
- /**
511
- * Event name formatter
512
- */
513
- class EventFormatter {
514
- /**
515
- * Create a new class instance.
516
- */
517
- constructor(namespace) {
518
- this.setNamespace(namespace);
519
- }
520
- /**
521
- * Format the given event name.
522
- */
523
- format(event) {
524
- if (event.charAt(0) === '.' || event.charAt(0) === '\\') {
525
- return event.substr(1);
526
- }
527
- else if (this.namespace) {
528
- event = this.namespace + '.' + event;
529
- }
530
- return event.replace(/\./g, '\\');
531
- }
532
- /**
533
- * Set the event namespace.
534
- */
535
- setNamespace(value) {
536
- this.namespace = value;
537
- }
524
+ /**
525
+ * Event name formatter
526
+ */
527
+ class EventFormatter {
528
+ /**
529
+ * Create a new class instance.
530
+ */
531
+ constructor(namespace) {
532
+ this.setNamespace(namespace);
533
+ }
534
+ /**
535
+ * Format the given event name.
536
+ */
537
+ format(event) {
538
+ if (event.charAt(0) === '.' || event.charAt(0) === '\\') {
539
+ return event.substr(1);
540
+ }
541
+ else if (this.namespace) {
542
+ event = this.namespace + '.' + event;
543
+ }
544
+ return event.replace(/\./g, '\\');
545
+ }
546
+ /**
547
+ * Set the event namespace.
548
+ */
549
+ setNamespace(value) {
550
+ this.namespace = value;
551
+ }
538
552
  }
539
553
 
540
- /**
541
- * This class represents a basic channel.
542
- */
543
- class Channel {
544
- /**
545
- * Listen for a whisper event on the channel instance.
546
- */
547
- listenForWhisper(event, callback) {
548
- return this.listen('.client-' + event, callback);
549
- }
550
- /**
551
- * Listen for an event on the channel instance.
552
- */
553
- notification(callback) {
554
- return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback);
555
- }
556
- /**
557
- * Stop listening for a whisper event on the channel instance.
558
- */
559
- stopListeningForWhisper(event, callback) {
560
- return this.stopListening('.client-' + event, callback);
561
- }
554
+ /**
555
+ * This class represents a basic channel.
556
+ */
557
+ class Channel {
558
+ /**
559
+ * Listen for a whisper event on the channel instance.
560
+ */
561
+ listenForWhisper(event, callback) {
562
+ return this.listen('.client-' + event, callback);
563
+ }
564
+ /**
565
+ * Listen for an event on the channel instance.
566
+ */
567
+ notification(callback) {
568
+ return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback);
569
+ }
570
+ /**
571
+ * Stop listening for a whisper event on the channel instance.
572
+ */
573
+ stopListeningForWhisper(event, callback) {
574
+ return this.stopListening('.client-' + event, callback);
575
+ }
562
576
  }
563
577
 
564
- /**
565
- * This class represents a Pusher channel.
578
+ var LOG_PREFIX$1 = '[LE-AG-Channel]';
579
+ /**
580
+ * This class represents a Pusher channel.
566
581
  */
567
582
 
568
583
  var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
@@ -570,8 +585,8 @@ var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
570
585
 
571
586
  var _super = _createSuper(Channel);
572
587
 
573
- /**
574
- * Create a new class instance.
588
+ /**
589
+ * Create a new class instance.
575
590
  */
576
591
  function Channel(socket, name, options) {
577
592
  var _this;
@@ -588,76 +603,83 @@ var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
588
603
 
589
604
  return _this;
590
605
  }
591
- /**
592
- * Subscribe to a Pusher channel.
606
+ /**
607
+ * Subscribe to a Pusher channel.
593
608
  */
594
609
 
595
610
 
596
611
  _createClass(Channel, [{
597
612
  key: "subscribe",
598
613
  value: function subscribe() {
614
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " subscribe for channel ").concat(this.name, " ..."));
599
615
  this.socket.subscribe(this);
600
616
  }
601
- /**
602
- * Unsubscribe from a Pusher channel.
617
+ /**
618
+ * Unsubscribe from a Pusher channel.
603
619
  */
604
620
 
605
621
  }, {
606
622
  key: "unsubscribe",
607
623
  value: function unsubscribe() {
624
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " unsubscribe for channel ").concat(this.name, " ..."));
608
625
  this.socket.unsubscribe(this);
609
626
  }
610
- /**
611
- * Listen for an event on the channel instance.
627
+ /**
628
+ * Listen for an event on the channel instance.
612
629
  */
613
630
 
614
631
  }, {
615
632
  key: "listen",
616
633
  value: function listen(event, callback) {
634
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " listen to ").concat(event, " for channel ").concat(this.name, " ..."));
617
635
  this.on(this.eventFormatter.format(event), callback);
618
636
  return this;
619
637
  }
620
- /**
621
- * Stop listening for an event on the channel instance.
638
+ /**
639
+ * Stop listening for an event on the channel instance.
622
640
  */
623
641
 
624
642
  }, {
625
643
  key: "stopListening",
626
644
  value: function stopListening(event, callback) {
645
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " stop listening to ").concat(event, " for channel ").concat(this.name, " ..."));
627
646
  this.socket.unbindEvent(this, event, callback);
628
647
  return this;
629
648
  }
630
- /**
631
- * Register a callback to be called anytime a subscription succeeds.
649
+ /**
650
+ * Register a callback to be called anytime a subscription succeeds.
632
651
  */
633
652
 
634
653
  }, {
635
654
  key: "subscribed",
636
655
  value: function subscribed(callback) {
656
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " subscribed for channel ").concat(this.name, " ..."));
637
657
  this.on('subscription_succeeded', function () {
638
658
  callback();
639
659
  });
640
660
  return this;
641
661
  }
642
- /**
643
- * Register a callback to be called anytime a subscription error occurs.
662
+ /**
663
+ * Register a callback to be called anytime a subscription error occurs.
644
664
  */
645
665
 
646
666
  }, {
647
667
  key: "error",
648
668
  value: function error(callback) {
669
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " error for channel ").concat(this.name, " ..."));
649
670
  this.on('error', function (status) {
650
671
  callback(status);
651
672
  });
652
673
  return this;
653
674
  }
654
- /**
655
- * Bind a channel to an event.
675
+ /**
676
+ * Bind a channel to an event.
656
677
  */
657
678
 
658
679
  }, {
659
680
  key: "on",
660
681
  value: function on(event, callback) {
682
+ this.options["debug"] && console.log("".concat(LOG_PREFIX$1, " on ").concat(event, " for channel ").concat(this.name, " ..."));
661
683
  this.socket.bind(this, event, callback);
662
684
  return this;
663
685
  }
@@ -679,8 +701,8 @@ var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
679
701
  // TODO: implement
680
702
  return this;
681
703
  }
682
- /**
683
- * Listen for someone joining the channel.
704
+ /**
705
+ * Listen for someone joining the channel.
684
706
  */
685
707
 
686
708
  }, {
@@ -689,8 +711,8 @@ var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
689
711
  // TODO: implement
690
712
  return this;
691
713
  }
692
- /**
693
- * Listen for someone leaving the channel.
714
+ /**
715
+ * Listen for someone leaving the channel.
694
716
  */
695
717
 
696
718
  }, {
@@ -707,32 +729,37 @@ var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
707
729
  var broadcaster = function broadcaster(options) {
708
730
  return new Connector$1(options);
709
731
  };
732
+ var LOG_PREFIX$2 = '[LE-AG-Connector]';
710
733
  var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
711
734
  _inherits(Connector, _BaseConnector);
712
735
 
713
736
  var _super = _createSuper(Connector);
714
737
 
715
- function Connector() {
738
+ /**
739
+ * Create a new class instance.
740
+ */
741
+ function Connector(options) {
716
742
  var _this;
717
743
 
718
744
  _classCallCheck(this, Connector);
719
745
 
720
- _this = _super.apply(this, arguments);
721
- /**
722
- * All of the subscribed channel names.
746
+ _this = _super.call(this, options);
747
+ /**
748
+ * All of the subscribed channel names.
723
749
  */
724
750
 
725
751
  _this.channels = {};
726
752
  return _this;
727
753
  }
728
- /**
729
- * Create a fresh Socket.io connection.
754
+ /**
755
+ * Create a fresh Socket.io connection.
730
756
  */
731
757
 
732
758
 
733
759
  _createClass(Connector, [{
734
760
  key: "connect",
735
761
  value: function connect() {
762
+ this.options.debug && console.log(LOG_PREFIX$2 + 'Connect ...');
736
763
  this.socket = new Websocket(this.options);
737
764
  return; //
738
765
  // this.socket.on('reconnect', () => {
@@ -743,8 +770,8 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
743
770
  //
744
771
  // return this.socket;
745
772
  }
746
- /**
747
- * Get a channel instance by name.
773
+ /**
774
+ * Get a channel instance by name.
748
775
  */
749
776
 
750
777
  }, {
@@ -756,8 +783,8 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
756
783
 
757
784
  return this.channels[name];
758
785
  }
759
- /**
760
- * Get a private channel instance by name.
786
+ /**
787
+ * Get a private channel instance by name.
761
788
  */
762
789
 
763
790
  }, {
@@ -769,8 +796,8 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
769
796
 
770
797
  return this.channels['private-' + name];
771
798
  }
772
- /**
773
- * Get a presence channel instance by name.
799
+ /**
800
+ * Get a presence channel instance by name.
774
801
  */
775
802
 
776
803
  }, {
@@ -782,8 +809,8 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
782
809
 
783
810
  return this.channels['presence-' + name];
784
811
  }
785
- /**
786
- * Leave the given channel, as well as its private and presence variants.
812
+ /**
813
+ * Leave the given channel, as well as its private and presence variants.
787
814
  */
788
815
 
789
816
  }, {
@@ -796,8 +823,8 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
796
823
  _this2.leaveChannel(name);
797
824
  });
798
825
  }
799
- /**
800
- * Leave the given channel.
826
+ /**
827
+ * Leave the given channel.
801
828
  */
802
829
 
803
830
  }, {
@@ -808,8 +835,8 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
808
835
  delete this.channels[name];
809
836
  }
810
837
  }
811
- /**
812
- * Get the socket ID for the connection.
838
+ /**
839
+ * Get the socket ID for the connection.
813
840
  */
814
841
 
815
842
  }, {
@@ -817,13 +844,14 @@ var Connector$1 = /*#__PURE__*/function (_BaseConnector) {
817
844
  value: function socketId() {
818
845
  return this.socket.getSocketId();
819
846
  }
820
- /**
821
- * Disconnect socket connection.
847
+ /**
848
+ * Disconnect socket connection.
822
849
  */
823
850
 
824
851
  }, {
825
852
  key: "disconnect",
826
853
  value: function disconnect() {
854
+ this.options.debug && console.log(LOG_PREFIX$2 + 'Disconnect ...');
827
855
  this.socket.close();
828
856
  }
829
857
  }]);