pusher-js 7.3.0 → 7.4.0

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.
Files changed (44) hide show
  1. package/.github/workflows/release.yml +1 -1
  2. package/.github/workflows/release_pr.yml +1 -1
  3. package/.github/workflows/run-tests.yml +2 -2
  4. package/.gitmodules +1 -1
  5. package/CHANGELOG.md +4 -0
  6. package/README.md +10 -30
  7. package/dist/node/pusher.js +127 -37
  8. package/dist/node/pusher.js.map +1 -1
  9. package/dist/react-native/pusher.js +2 -2
  10. package/dist/react-native/pusher.js.map +1 -1
  11. package/dist/web/pusher-with-encryption.js +127 -37
  12. package/dist/web/pusher-with-encryption.js.map +1 -1
  13. package/dist/web/pusher-with-encryption.min.js +2 -2
  14. package/dist/web/pusher-with-encryption.min.js.map +1 -1
  15. package/dist/web/pusher.js +127 -37
  16. package/dist/web/pusher.js.map +1 -1
  17. package/dist/web/pusher.min.js +2 -2
  18. package/dist/web/pusher.min.js.map +1 -1
  19. package/dist/worker/pusher-with-encryption.worker.js +127 -37
  20. package/dist/worker/pusher-with-encryption.worker.js.map +1 -1
  21. package/dist/worker/pusher-with-encryption.worker.min.js +2 -2
  22. package/dist/worker/pusher-with-encryption.worker.min.js.map +1 -1
  23. package/dist/worker/pusher.worker.js +127 -37
  24. package/dist/worker/pusher.worker.js.map +1 -1
  25. package/dist/worker/pusher.worker.min.js +2 -2
  26. package/dist/worker/pusher.worker.min.js.map +1 -1
  27. package/package.json +5 -4
  28. package/spec/javascripts/helpers/timers/promises.js +9 -0
  29. package/spec/javascripts/unit/core/channels/presence_channel_spec.js +85 -16
  30. package/spec/javascripts/unit/core/user_spec.js +13 -6
  31. package/src/core/channels/presence_channel.ts +21 -12
  32. package/src/core/http/http_socket.ts +3 -1
  33. package/src/core/pusher.ts +1 -1
  34. package/src/core/user.ts +65 -27
  35. package/src/core/utils/flat_promise.ts +10 -0
  36. package/src/runtimes/interface.ts +1 -0
  37. package/src/runtimes/node/runtime.ts +5 -0
  38. package/src/runtimes/react-native/runtime.ts +5 -0
  39. package/src/runtimes/web/runtime.ts +14 -0
  40. package/src/runtimes/worker/runtime.ts +14 -0
  41. package/types/src/core/strategies/transport_strategy.d.ts +3 -0
  42. package/types/src/core/user.d.ts +5 -1
  43. package/types/src/core/utils/flat_promise.d.ts +6 -0
  44. package/types/src/runtimes/interface.d.ts +1 -0
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Pusher JavaScript Library v7.3.0
2
+ * Pusher JavaScript Library v7.4.0
3
3
  * https://pusher.com/
4
4
  *
5
5
  * Copyright 2020, Pusher
@@ -3299,7 +3299,7 @@ function safeJSONStringify(source) {
3299
3299
 
3300
3300
  // CONCATENATED MODULE: ./src/core/defaults.ts
3301
3301
  var Defaults = {
3302
- VERSION: "7.3.0",
3302
+ VERSION: "7.4.0",
3303
3303
  PROTOCOL: 7,
3304
3304
  wsPort: 80,
3305
3305
  wssPort: 443,
@@ -4494,6 +4494,41 @@ var presence_channel_extends = (undefined && undefined.__extends) || (function (
4494
4494
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
4495
4495
  };
4496
4496
  })();
4497
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
4498
+ return new (P || (P = Promise))(function (resolve, reject) {
4499
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
4500
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
4501
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
4502
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
4503
+ });
4504
+ };
4505
+ var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
4506
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4507
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
4508
+ function verb(n) { return function (v) { return step([n, v]); }; }
4509
+ function step(op) {
4510
+ if (f) throw new TypeError("Generator is already executing.");
4511
+ while (_) try {
4512
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
4513
+ if (y = 0, t) op = [op[0] & 2, t.value];
4514
+ switch (op[0]) {
4515
+ case 0: case 1: t = op; break;
4516
+ case 4: _.label++; return { value: op[1], done: false };
4517
+ case 5: _.label++; y = op[1]; op = [0]; continue;
4518
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
4519
+ default:
4520
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
4521
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
4522
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
4523
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
4524
+ if (t[2]) _.ops.pop();
4525
+ _.trys.pop(); continue;
4526
+ }
4527
+ op = body.call(thisArg, _);
4528
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
4529
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
4530
+ }
4531
+ };
4497
4532
 
4498
4533
 
4499
4534
 
@@ -4507,21 +4542,38 @@ var presence_channel_PresenceChannel = (function (_super) {
4507
4542
  }
4508
4543
  PresenceChannel.prototype.authorize = function (socketId, callback) {
4509
4544
  var _this = this;
4510
- _super.prototype.authorize.call(this, socketId, function (error, authData) {
4511
- if (!error) {
4512
- authData = authData;
4513
- if (authData.channel_data === undefined) {
4514
- var suffix = url_store.buildLogSuffix('authenticationEndpoint');
4515
- logger.error("Invalid auth response for channel '" + _this.name + "'," +
4516
- ("expected 'channel_data' field. " + suffix));
4517
- callback('Invalid auth response');
4518
- return;
4545
+ _super.prototype.authorize.call(this, socketId, function (error, authData) { return __awaiter(_this, void 0, void 0, function () {
4546
+ var channelData, suffix;
4547
+ return __generator(this, function (_a) {
4548
+ switch (_a.label) {
4549
+ case 0:
4550
+ if (!!error) return [3, 3];
4551
+ authData = authData;
4552
+ if (!(authData.channel_data != null)) return [3, 1];
4553
+ channelData = JSON.parse(authData.channel_data);
4554
+ this.members.setMyID(channelData.user_id);
4555
+ return [3, 3];
4556
+ case 1: return [4, this.pusher.user.signinDonePromise];
4557
+ case 2:
4558
+ _a.sent();
4559
+ if (this.pusher.user.user_data != null) {
4560
+ this.members.setMyID(this.pusher.user.user_data.id);
4561
+ }
4562
+ else {
4563
+ suffix = url_store.buildLogSuffix('authorizationEndpoint');
4564
+ logger.error("Invalid auth response for channel '" + this.name + "', " +
4565
+ ("expected 'channel_data' field. " + suffix + ", ") +
4566
+ "or the user should be signed in.");
4567
+ callback('Invalid auth response');
4568
+ return [2];
4569
+ }
4570
+ _a.label = 3;
4571
+ case 3:
4572
+ callback(error, authData);
4573
+ return [2];
4519
4574
  }
4520
- var channelData = JSON.parse(authData.channel_data);
4521
- _this.members.setMyID(channelData.user_id);
4522
- }
4523
- callback(error, authData);
4524
- });
4575
+ });
4576
+ }); });
4525
4577
  };
4526
4578
  PresenceChannel.prototype.handleEvent = function (event) {
4527
4579
  var eventName = event.event;
@@ -6492,6 +6544,17 @@ function buildChannelAuthorizer(opts, pusher) {
6492
6544
  return channel_authorizer(channelAuthorization);
6493
6545
  }
6494
6546
 
6547
+ // CONCATENATED MODULE: ./src/core/utils/flat_promise.ts
6548
+ function flatPromise() {
6549
+ var resolve, reject;
6550
+ var promise = new Promise(function (res, rej) {
6551
+ resolve = res;
6552
+ reject = rej;
6553
+ });
6554
+ return { promise: promise, resolve: resolve, reject: reject };
6555
+ }
6556
+ /* harmony default export */ var flat_promise = (flatPromise);
6557
+
6495
6558
  // CONCATENATED MODULE: ./src/core/user.ts
6496
6559
  var user_extends = (undefined && undefined.__extends) || (function () {
6497
6560
  var extendStatics = function (d, b) {
@@ -6509,6 +6572,7 @@ var user_extends = (undefined && undefined.__extends) || (function () {
6509
6572
 
6510
6573
 
6511
6574
 
6575
+
6512
6576
  var user_UserFacade = (function (_super) {
6513
6577
  user_extends(UserFacade, _super);
6514
6578
  function UserFacade(pusher) {
@@ -6518,15 +6582,29 @@ var user_UserFacade = (function (_super) {
6518
6582
  _this.signin_requested = false;
6519
6583
  _this.user_data = null;
6520
6584
  _this.serverToUserChannel = null;
6585
+ _this.signinDonePromise = null;
6586
+ _this._signinDoneResolve = null;
6587
+ _this._onAuthorize = function (err, authData) {
6588
+ if (err) {
6589
+ logger.warn("Error during signin: " + err);
6590
+ _this._cleanup();
6591
+ return;
6592
+ }
6593
+ _this.pusher.send_event('pusher:signin', {
6594
+ auth: authData.auth,
6595
+ user_data: authData.user_data
6596
+ });
6597
+ };
6521
6598
  _this.pusher = pusher;
6522
- _this.pusher.connection.bind('connected', function () {
6523
- _this._signin();
6524
- });
6525
- _this.pusher.connection.bind('connecting', function () {
6526
- _this._disconnect();
6527
- });
6528
- _this.pusher.connection.bind('disconnected', function () {
6529
- _this._disconnect();
6599
+ _this.pusher.connection.bind('state_change', function (_a) {
6600
+ var previous = _a.previous, current = _a.current;
6601
+ if (previous !== 'connected' && current === 'connected') {
6602
+ _this._signin();
6603
+ }
6604
+ if (previous === 'connected' && current !== 'connected') {
6605
+ _this._cleanup();
6606
+ _this._newSigninPromiseIfNeeded();
6607
+ }
6530
6608
  });
6531
6609
  _this.pusher.connection.bind('message', function (event) {
6532
6610
  var eventName = event.event;
@@ -6548,26 +6626,16 @@ var user_UserFacade = (function (_super) {
6548
6626
  this._signin();
6549
6627
  };
6550
6628
  UserFacade.prototype._signin = function () {
6551
- var _this = this;
6552
6629
  if (!this.signin_requested) {
6553
6630
  return;
6554
6631
  }
6632
+ this._newSigninPromiseIfNeeded();
6555
6633
  if (this.pusher.connection.state !== 'connected') {
6556
6634
  return;
6557
6635
  }
6558
- var onAuthorize = function (err, authData) {
6559
- if (err) {
6560
- logger.warn("Error during signin: " + err);
6561
- return;
6562
- }
6563
- _this.pusher.send_event('pusher:signin', {
6564
- auth: authData.auth,
6565
- user_data: authData.user_data
6566
- });
6567
- };
6568
6636
  this.pusher.config.userAuthenticator({
6569
6637
  socketId: this.pusher.connection.socket_id
6570
- }, onAuthorize);
6638
+ }, this._onAuthorize);
6571
6639
  };
6572
6640
  UserFacade.prototype._onSigninSuccess = function (data) {
6573
6641
  try {
@@ -6575,12 +6643,15 @@ var user_UserFacade = (function (_super) {
6575
6643
  }
6576
6644
  catch (e) {
6577
6645
  logger.error("Failed parsing user data after signin: " + data.user_data);
6646
+ this._cleanup();
6578
6647
  return;
6579
6648
  }
6580
6649
  if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
6581
6650
  logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
6651
+ this._cleanup();
6582
6652
  return;
6583
6653
  }
6654
+ this._signinDoneResolve();
6584
6655
  this._subscribeChannels();
6585
6656
  };
6586
6657
  UserFacade.prototype._subscribeChannels = function () {
@@ -6604,13 +6675,32 @@ var user_UserFacade = (function (_super) {
6604
6675
  });
6605
6676
  ensure_subscribed(this.serverToUserChannel);
6606
6677
  };
6607
- UserFacade.prototype._disconnect = function () {
6678
+ UserFacade.prototype._cleanup = function () {
6608
6679
  this.user_data = null;
6609
6680
  if (this.serverToUserChannel) {
6610
6681
  this.serverToUserChannel.unbind_all();
6611
6682
  this.serverToUserChannel.disconnect();
6612
6683
  this.serverToUserChannel = null;
6613
6684
  }
6685
+ if (this.signin_requested) {
6686
+ this._signinDoneResolve();
6687
+ }
6688
+ };
6689
+ UserFacade.prototype._newSigninPromiseIfNeeded = function () {
6690
+ if (!this.signin_requested) {
6691
+ return;
6692
+ }
6693
+ if (this.signinDonePromise && !this.signinDonePromise.done) {
6694
+ return;
6695
+ }
6696
+ var _a = flat_promise(), promise = _a.promise, resolve = _a.resolve, _ = _a.reject;
6697
+ promise.done = false;
6698
+ var setDone = function () {
6699
+ promise.done = true;
6700
+ };
6701
+ promise.then(setDone)["catch"](setDone);
6702
+ this.signinDonePromise = promise;
6703
+ this._signinDoneResolve = resolve;
6614
6704
  };
6615
6705
  return UserFacade;
6616
6706
  }(dispatcher));