laravel-echo-api-gateway 0.5.2 → 0.5.3

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