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