pusher-js 7.3.0 → 7.4.1

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 (46) hide show
  1. package/.github/PULL_REQUEST_TEMPLATE.md +4 -0
  2. package/.github/workflows/release.yml +1 -1
  3. package/.github/workflows/release_pr.yml +2 -3
  4. package/.github/workflows/run-tests.yml +2 -2
  5. package/.gitmodules +1 -1
  6. package/CHANGELOG.md +11 -1
  7. package/README.md +11 -31
  8. package/dist/node/pusher.js +159 -61
  9. package/dist/node/pusher.js.map +1 -1
  10. package/dist/react-native/pusher.js +13 -2
  11. package/dist/react-native/pusher.js.map +1 -1
  12. package/dist/web/pusher-with-encryption.js +141 -42
  13. package/dist/web/pusher-with-encryption.js.map +1 -1
  14. package/dist/web/pusher-with-encryption.min.js +2 -2
  15. package/dist/web/pusher-with-encryption.min.js.map +1 -1
  16. package/dist/web/pusher.js +141 -42
  17. package/dist/web/pusher.js.map +1 -1
  18. package/dist/web/pusher.min.js +2 -2
  19. package/dist/web/pusher.min.js.map +1 -1
  20. package/dist/worker/pusher-with-encryption.worker.js +140 -41
  21. package/dist/worker/pusher-with-encryption.worker.js.map +1 -1
  22. package/dist/worker/pusher-with-encryption.worker.min.js +2 -2
  23. package/dist/worker/pusher-with-encryption.worker.min.js.map +1 -1
  24. package/dist/worker/pusher.worker.js +140 -41
  25. package/dist/worker/pusher.worker.js.map +1 -1
  26. package/dist/worker/pusher.worker.min.js +2 -2
  27. package/dist/worker/pusher.worker.min.js.map +1 -1
  28. package/package.json +5 -4
  29. package/spec/javascripts/helpers/timers/promises.js +9 -0
  30. package/spec/javascripts/unit/core/channels/presence_channel_spec.js +85 -16
  31. package/spec/javascripts/unit/core/user_spec.js +13 -6
  32. package/src/core/channels/presence_channel.ts +21 -12
  33. package/src/core/http/http_socket.ts +3 -1
  34. package/src/core/pusher.ts +1 -1
  35. package/src/core/user.ts +65 -27
  36. package/src/core/utils/flat_promise.ts +10 -0
  37. package/src/runtimes/interface.ts +1 -0
  38. package/src/runtimes/isomorphic/auth/xhr_auth.ts +1 -1
  39. package/src/runtimes/node/runtime.ts +5 -0
  40. package/src/runtimes/react-native/runtime.ts +5 -0
  41. package/src/runtimes/web/runtime.ts +14 -0
  42. package/src/runtimes/worker/runtime.ts +14 -0
  43. package/types/src/core/strategies/transport_strategy.d.ts +3 -0
  44. package/types/src/core/user.d.ts +5 -1
  45. package/types/src/core/utils/flat_promise.d.ts +6 -0
  46. 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.1
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.1",
3303
3303
  PROTOCOL: 7,
3304
3304
  wsPort: 80,
3305
3305
  wssPort: 443,
@@ -4494,6 +4494,42 @@ 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
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4499
+ return new (P || (P = Promise))(function (resolve, reject) {
4500
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
4501
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
4502
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
4503
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
4504
+ });
4505
+ };
4506
+ var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
4507
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4508
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
4509
+ function verb(n) { return function (v) { return step([n, v]); }; }
4510
+ function step(op) {
4511
+ if (f) throw new TypeError("Generator is already executing.");
4512
+ while (_) try {
4513
+ 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;
4514
+ if (y = 0, t) op = [op[0] & 2, t.value];
4515
+ switch (op[0]) {
4516
+ case 0: case 1: t = op; break;
4517
+ case 4: _.label++; return { value: op[1], done: false };
4518
+ case 5: _.label++; y = op[1]; op = [0]; continue;
4519
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
4520
+ default:
4521
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
4522
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
4523
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
4524
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
4525
+ if (t[2]) _.ops.pop();
4526
+ _.trys.pop(); continue;
4527
+ }
4528
+ op = body.call(thisArg, _);
4529
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
4530
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
4531
+ }
4532
+ };
4497
4533
 
4498
4534
 
4499
4535
 
@@ -4507,21 +4543,38 @@ var presence_channel_PresenceChannel = (function (_super) {
4507
4543
  }
4508
4544
  PresenceChannel.prototype.authorize = function (socketId, callback) {
4509
4545
  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;
4546
+ _super.prototype.authorize.call(this, socketId, function (error, authData) { return __awaiter(_this, void 0, void 0, function () {
4547
+ var channelData, suffix;
4548
+ return __generator(this, function (_a) {
4549
+ switch (_a.label) {
4550
+ case 0:
4551
+ if (!!error) return [3, 3];
4552
+ authData = authData;
4553
+ if (!(authData.channel_data != null)) return [3, 1];
4554
+ channelData = JSON.parse(authData.channel_data);
4555
+ this.members.setMyID(channelData.user_id);
4556
+ return [3, 3];
4557
+ case 1: return [4, this.pusher.user.signinDonePromise];
4558
+ case 2:
4559
+ _a.sent();
4560
+ if (this.pusher.user.user_data != null) {
4561
+ this.members.setMyID(this.pusher.user.user_data.id);
4562
+ }
4563
+ else {
4564
+ suffix = url_store.buildLogSuffix('authorizationEndpoint');
4565
+ logger.error("Invalid auth response for channel '" + this.name + "', " +
4566
+ ("expected 'channel_data' field. " + suffix + ", ") +
4567
+ "or the user should be signed in.");
4568
+ callback('Invalid auth response');
4569
+ return [2];
4570
+ }
4571
+ _a.label = 3;
4572
+ case 3:
4573
+ callback(error, authData);
4574
+ return [2];
4519
4575
  }
4520
- var channelData = JSON.parse(authData.channel_data);
4521
- _this.members.setMyID(channelData.user_id);
4522
- }
4523
- callback(error, authData);
4524
- });
4576
+ });
4577
+ }); });
4525
4578
  };
4526
4579
  PresenceChannel.prototype.handleEvent = function (event) {
4527
4580
  var eventName = event.event;
@@ -5785,7 +5838,7 @@ function replaceHost(url, hostname) {
5785
5838
  return urlParts[1] + hostname + urlParts[3];
5786
5839
  }
5787
5840
  function randomNumber(max) {
5788
- return Math.floor(Math.random() * max);
5841
+ return worker_runtime.randomInt(max);
5789
5842
  }
5790
5843
  function randomString(length) {
5791
5844
  var result = [];
@@ -6069,6 +6122,14 @@ var Worker = {
6069
6122
  },
6070
6123
  getNetwork: function () {
6071
6124
  return net_info_Network;
6125
+ },
6126
+ randomInt: function (max) {
6127
+ var random = function () {
6128
+ var crypto = window.crypto || window['msCrypto'];
6129
+ var random = crypto.getRandomValues(new Uint32Array(1))[0];
6130
+ return random / Math.pow(2, 32);
6131
+ };
6132
+ return Math.floor(random() * max);
6072
6133
  }
6073
6134
  };
6074
6135
  /* harmony default export */ var worker_runtime = (Worker);
@@ -6455,7 +6516,7 @@ function getEnableStatsConfig(opts) {
6455
6516
  return false;
6456
6517
  }
6457
6518
  function buildUserAuthenticator(opts) {
6458
- var userAuthentication = __assign({}, defaults.userAuthentication, opts.userAuthentication);
6519
+ var userAuthentication = __assign(__assign({}, defaults.userAuthentication), opts.userAuthentication);
6459
6520
  if ('customHandler' in userAuthentication &&
6460
6521
  userAuthentication['customHandler'] != null) {
6461
6522
  return userAuthentication['customHandler'];
@@ -6465,7 +6526,7 @@ function buildUserAuthenticator(opts) {
6465
6526
  function buildChannelAuth(opts, pusher) {
6466
6527
  var channelAuthorization;
6467
6528
  if ('channelAuthorization' in opts) {
6468
- channelAuthorization = __assign({}, defaults.channelAuthorization, opts.channelAuthorization);
6529
+ channelAuthorization = __assign(__assign({}, defaults.channelAuthorization), opts.channelAuthorization);
6469
6530
  }
6470
6531
  else {
6471
6532
  channelAuthorization = {
@@ -6492,6 +6553,17 @@ function buildChannelAuthorizer(opts, pusher) {
6492
6553
  return channel_authorizer(channelAuthorization);
6493
6554
  }
6494
6555
 
6556
+ // CONCATENATED MODULE: ./src/core/utils/flat_promise.ts
6557
+ function flatPromise() {
6558
+ var resolve, reject;
6559
+ var promise = new Promise(function (res, rej) {
6560
+ resolve = res;
6561
+ reject = rej;
6562
+ });
6563
+ return { promise: promise, resolve: resolve, reject: reject };
6564
+ }
6565
+ /* harmony default export */ var flat_promise = (flatPromise);
6566
+
6495
6567
  // CONCATENATED MODULE: ./src/core/user.ts
6496
6568
  var user_extends = (undefined && undefined.__extends) || (function () {
6497
6569
  var extendStatics = function (d, b) {
@@ -6509,6 +6581,7 @@ var user_extends = (undefined && undefined.__extends) || (function () {
6509
6581
 
6510
6582
 
6511
6583
 
6584
+
6512
6585
  var user_UserFacade = (function (_super) {
6513
6586
  user_extends(UserFacade, _super);
6514
6587
  function UserFacade(pusher) {
@@ -6518,15 +6591,29 @@ var user_UserFacade = (function (_super) {
6518
6591
  _this.signin_requested = false;
6519
6592
  _this.user_data = null;
6520
6593
  _this.serverToUserChannel = null;
6594
+ _this.signinDonePromise = null;
6595
+ _this._signinDoneResolve = null;
6596
+ _this._onAuthorize = function (err, authData) {
6597
+ if (err) {
6598
+ logger.warn("Error during signin: " + err);
6599
+ _this._cleanup();
6600
+ return;
6601
+ }
6602
+ _this.pusher.send_event('pusher:signin', {
6603
+ auth: authData.auth,
6604
+ user_data: authData.user_data
6605
+ });
6606
+ };
6521
6607
  _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();
6608
+ _this.pusher.connection.bind('state_change', function (_a) {
6609
+ var previous = _a.previous, current = _a.current;
6610
+ if (previous !== 'connected' && current === 'connected') {
6611
+ _this._signin();
6612
+ }
6613
+ if (previous === 'connected' && current !== 'connected') {
6614
+ _this._cleanup();
6615
+ _this._newSigninPromiseIfNeeded();
6616
+ }
6530
6617
  });
6531
6618
  _this.pusher.connection.bind('message', function (event) {
6532
6619
  var eventName = event.event;
@@ -6548,26 +6635,16 @@ var user_UserFacade = (function (_super) {
6548
6635
  this._signin();
6549
6636
  };
6550
6637
  UserFacade.prototype._signin = function () {
6551
- var _this = this;
6552
6638
  if (!this.signin_requested) {
6553
6639
  return;
6554
6640
  }
6641
+ this._newSigninPromiseIfNeeded();
6555
6642
  if (this.pusher.connection.state !== 'connected') {
6556
6643
  return;
6557
6644
  }
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
6645
  this.pusher.config.userAuthenticator({
6569
6646
  socketId: this.pusher.connection.socket_id
6570
- }, onAuthorize);
6647
+ }, this._onAuthorize);
6571
6648
  };
6572
6649
  UserFacade.prototype._onSigninSuccess = function (data) {
6573
6650
  try {
@@ -6575,12 +6652,15 @@ var user_UserFacade = (function (_super) {
6575
6652
  }
6576
6653
  catch (e) {
6577
6654
  logger.error("Failed parsing user data after signin: " + data.user_data);
6655
+ this._cleanup();
6578
6656
  return;
6579
6657
  }
6580
6658
  if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
6581
6659
  logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
6660
+ this._cleanup();
6582
6661
  return;
6583
6662
  }
6663
+ this._signinDoneResolve();
6584
6664
  this._subscribeChannels();
6585
6665
  };
6586
6666
  UserFacade.prototype._subscribeChannels = function () {
@@ -6604,13 +6684,32 @@ var user_UserFacade = (function (_super) {
6604
6684
  });
6605
6685
  ensure_subscribed(this.serverToUserChannel);
6606
6686
  };
6607
- UserFacade.prototype._disconnect = function () {
6687
+ UserFacade.prototype._cleanup = function () {
6608
6688
  this.user_data = null;
6609
6689
  if (this.serverToUserChannel) {
6610
6690
  this.serverToUserChannel.unbind_all();
6611
6691
  this.serverToUserChannel.disconnect();
6612
6692
  this.serverToUserChannel = null;
6613
6693
  }
6694
+ if (this.signin_requested) {
6695
+ this._signinDoneResolve();
6696
+ }
6697
+ };
6698
+ UserFacade.prototype._newSigninPromiseIfNeeded = function () {
6699
+ if (!this.signin_requested) {
6700
+ return;
6701
+ }
6702
+ if (this.signinDonePromise && !this.signinDonePromise.done) {
6703
+ return;
6704
+ }
6705
+ var _a = flat_promise(), promise = _a.promise, resolve = _a.resolve, _ = _a.reject;
6706
+ promise.done = false;
6707
+ var setDone = function () {
6708
+ promise.done = true;
6709
+ };
6710
+ promise.then(setDone)["catch"](setDone);
6711
+ this.signinDonePromise = promise;
6712
+ this._signinDoneResolve = resolve;
6614
6713
  };
6615
6714
  return UserFacade;
6616
6715
  }(dispatcher));
@@ -6646,7 +6745,7 @@ var pusher_Pusher = (function () {
6646
6745
  this.config = getConfig(options, this);
6647
6746
  this.channels = factory.createChannels();
6648
6747
  this.global_emitter = new dispatcher();
6649
- this.sessionID = Math.floor(Math.random() * 1000000000);
6748
+ this.sessionID = worker_runtime.randomInt(1000000000);
6650
6749
  this.timeline = new timeline_timeline(this.key, this.sessionID, {
6651
6750
  cluster: this.config.cluster,
6652
6751
  features: Pusher.getClientFeatures(),