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
@@ -593,7 +593,7 @@ var ScriptReceivers = new ScriptReceiverFactory('_pusher_script_', 'Pusher.Scrip
593
593
 
594
594
  // CONCATENATED MODULE: ./src/core/defaults.ts
595
595
  var Defaults = {
596
- VERSION: "7.3.0",
596
+ VERSION: "7.4.1",
597
597
  PROTOCOL: 7,
598
598
  wsPort: 80,
599
599
  wssPort: 443,
@@ -885,7 +885,7 @@ var ajax = function (context, query, authOptions, authRequestType, callback) {
885
885
  suffix = url_store.buildLogSuffix('authenticationEndpoint');
886
886
  break;
887
887
  case AuthRequestType.ChannelAuthorization:
888
- suffix = "Clients must be authenticated to join private or presence channels. " + url_store.buildLogSuffix('authorizationEndpoint');
888
+ suffix = "Clients must be authorized to join private or presence channels. " + url_store.buildLogSuffix('authorizationEndpoint');
889
889
  break;
890
890
  }
891
891
  callback(new HTTPAuthError(xhr.status, "Unable to retrieve auth string from " + authRequestType.toString() + " endpoint - " +
@@ -2473,6 +2473,42 @@ var presence_channel_extends = (undefined && undefined.__extends) || (function (
2473
2473
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2474
2474
  };
2475
2475
  })();
2476
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
2477
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
2478
+ return new (P || (P = Promise))(function (resolve, reject) {
2479
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
2480
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
2481
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
2482
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
2483
+ });
2484
+ };
2485
+ var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
2486
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
2487
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
2488
+ function verb(n) { return function (v) { return step([n, v]); }; }
2489
+ function step(op) {
2490
+ if (f) throw new TypeError("Generator is already executing.");
2491
+ while (_) try {
2492
+ 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;
2493
+ if (y = 0, t) op = [op[0] & 2, t.value];
2494
+ switch (op[0]) {
2495
+ case 0: case 1: t = op; break;
2496
+ case 4: _.label++; return { value: op[1], done: false };
2497
+ case 5: _.label++; y = op[1]; op = [0]; continue;
2498
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
2499
+ default:
2500
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
2501
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
2502
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
2503
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
2504
+ if (t[2]) _.ops.pop();
2505
+ _.trys.pop(); continue;
2506
+ }
2507
+ op = body.call(thisArg, _);
2508
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
2509
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
2510
+ }
2511
+ };
2476
2512
 
2477
2513
 
2478
2514
 
@@ -2486,21 +2522,38 @@ var presence_channel_PresenceChannel = (function (_super) {
2486
2522
  }
2487
2523
  PresenceChannel.prototype.authorize = function (socketId, callback) {
2488
2524
  var _this = this;
2489
- _super.prototype.authorize.call(this, socketId, function (error, authData) {
2490
- if (!error) {
2491
- authData = authData;
2492
- if (authData.channel_data === undefined) {
2493
- var suffix = url_store.buildLogSuffix('authenticationEndpoint');
2494
- logger.error("Invalid auth response for channel '" + _this.name + "'," +
2495
- ("expected 'channel_data' field. " + suffix));
2496
- callback('Invalid auth response');
2497
- return;
2525
+ _super.prototype.authorize.call(this, socketId, function (error, authData) { return __awaiter(_this, void 0, void 0, function () {
2526
+ var channelData, suffix;
2527
+ return __generator(this, function (_a) {
2528
+ switch (_a.label) {
2529
+ case 0:
2530
+ if (!!error) return [3, 3];
2531
+ authData = authData;
2532
+ if (!(authData.channel_data != null)) return [3, 1];
2533
+ channelData = JSON.parse(authData.channel_data);
2534
+ this.members.setMyID(channelData.user_id);
2535
+ return [3, 3];
2536
+ case 1: return [4, this.pusher.user.signinDonePromise];
2537
+ case 2:
2538
+ _a.sent();
2539
+ if (this.pusher.user.user_data != null) {
2540
+ this.members.setMyID(this.pusher.user.user_data.id);
2541
+ }
2542
+ else {
2543
+ suffix = url_store.buildLogSuffix('authorizationEndpoint');
2544
+ logger.error("Invalid auth response for channel '" + this.name + "', " +
2545
+ ("expected 'channel_data' field. " + suffix + ", ") +
2546
+ "or the user should be signed in.");
2547
+ callback('Invalid auth response');
2548
+ return [2];
2549
+ }
2550
+ _a.label = 3;
2551
+ case 3:
2552
+ callback(error, authData);
2553
+ return [2];
2498
2554
  }
2499
- var channelData = JSON.parse(authData.channel_data);
2500
- _this.members.setMyID(channelData.user_id);
2501
- }
2502
- callback(error, authData);
2503
- });
2555
+ });
2556
+ }); });
2504
2557
  };
2505
2558
  PresenceChannel.prototype.handleEvent = function (event) {
2506
2559
  var eventName = event.event;
@@ -3823,7 +3876,7 @@ function replaceHost(url, hostname) {
3823
3876
  return urlParts[1] + hostname + urlParts[3];
3824
3877
  }
3825
3878
  function randomNumber(max) {
3826
- return Math.floor(Math.random() * max);
3879
+ return runtime.randomInt(max);
3827
3880
  }
3828
3881
  function randomString(length) {
3829
3882
  var result = [];
@@ -4069,6 +4122,14 @@ var Runtime = {
4069
4122
  else if (window.detachEvent !== undefined) {
4070
4123
  window.detachEvent('onunload', listener);
4071
4124
  }
4125
+ },
4126
+ randomInt: function (max) {
4127
+ var random = function () {
4128
+ var crypto = window.crypto || window['msCrypto'];
4129
+ var random = crypto.getRandomValues(new Uint32Array(1))[0];
4130
+ return random / Math.pow(2, 32);
4131
+ };
4132
+ return Math.floor(random() * max);
4072
4133
  }
4073
4134
  };
4074
4135
  /* harmony default export */ var runtime = (Runtime);
@@ -4448,7 +4509,7 @@ function getEnableStatsConfig(opts) {
4448
4509
  return false;
4449
4510
  }
4450
4511
  function buildUserAuthenticator(opts) {
4451
- var userAuthentication = __assign({}, defaults.userAuthentication, opts.userAuthentication);
4512
+ var userAuthentication = __assign(__assign({}, defaults.userAuthentication), opts.userAuthentication);
4452
4513
  if ('customHandler' in userAuthentication &&
4453
4514
  userAuthentication['customHandler'] != null) {
4454
4515
  return userAuthentication['customHandler'];
@@ -4458,7 +4519,7 @@ function buildUserAuthenticator(opts) {
4458
4519
  function buildChannelAuth(opts, pusher) {
4459
4520
  var channelAuthorization;
4460
4521
  if ('channelAuthorization' in opts) {
4461
- channelAuthorization = __assign({}, defaults.channelAuthorization, opts.channelAuthorization);
4522
+ channelAuthorization = __assign(__assign({}, defaults.channelAuthorization), opts.channelAuthorization);
4462
4523
  }
4463
4524
  else {
4464
4525
  channelAuthorization = {
@@ -4485,6 +4546,17 @@ function buildChannelAuthorizer(opts, pusher) {
4485
4546
  return channel_authorizer(channelAuthorization);
4486
4547
  }
4487
4548
 
4549
+ // CONCATENATED MODULE: ./src/core/utils/flat_promise.ts
4550
+ function flatPromise() {
4551
+ var resolve, reject;
4552
+ var promise = new Promise(function (res, rej) {
4553
+ resolve = res;
4554
+ reject = rej;
4555
+ });
4556
+ return { promise: promise, resolve: resolve, reject: reject };
4557
+ }
4558
+ /* harmony default export */ var flat_promise = (flatPromise);
4559
+
4488
4560
  // CONCATENATED MODULE: ./src/core/user.ts
4489
4561
  var user_extends = (undefined && undefined.__extends) || (function () {
4490
4562
  var extendStatics = function (d, b) {
@@ -4502,6 +4574,7 @@ var user_extends = (undefined && undefined.__extends) || (function () {
4502
4574
 
4503
4575
 
4504
4576
 
4577
+
4505
4578
  var user_UserFacade = (function (_super) {
4506
4579
  user_extends(UserFacade, _super);
4507
4580
  function UserFacade(pusher) {
@@ -4511,15 +4584,29 @@ var user_UserFacade = (function (_super) {
4511
4584
  _this.signin_requested = false;
4512
4585
  _this.user_data = null;
4513
4586
  _this.serverToUserChannel = null;
4587
+ _this.signinDonePromise = null;
4588
+ _this._signinDoneResolve = null;
4589
+ _this._onAuthorize = function (err, authData) {
4590
+ if (err) {
4591
+ logger.warn("Error during signin: " + err);
4592
+ _this._cleanup();
4593
+ return;
4594
+ }
4595
+ _this.pusher.send_event('pusher:signin', {
4596
+ auth: authData.auth,
4597
+ user_data: authData.user_data
4598
+ });
4599
+ };
4514
4600
  _this.pusher = pusher;
4515
- _this.pusher.connection.bind('connected', function () {
4516
- _this._signin();
4517
- });
4518
- _this.pusher.connection.bind('connecting', function () {
4519
- _this._disconnect();
4520
- });
4521
- _this.pusher.connection.bind('disconnected', function () {
4522
- _this._disconnect();
4601
+ _this.pusher.connection.bind('state_change', function (_a) {
4602
+ var previous = _a.previous, current = _a.current;
4603
+ if (previous !== 'connected' && current === 'connected') {
4604
+ _this._signin();
4605
+ }
4606
+ if (previous === 'connected' && current !== 'connected') {
4607
+ _this._cleanup();
4608
+ _this._newSigninPromiseIfNeeded();
4609
+ }
4523
4610
  });
4524
4611
  _this.pusher.connection.bind('message', function (event) {
4525
4612
  var eventName = event.event;
@@ -4541,26 +4628,16 @@ var user_UserFacade = (function (_super) {
4541
4628
  this._signin();
4542
4629
  };
4543
4630
  UserFacade.prototype._signin = function () {
4544
- var _this = this;
4545
4631
  if (!this.signin_requested) {
4546
4632
  return;
4547
4633
  }
4634
+ this._newSigninPromiseIfNeeded();
4548
4635
  if (this.pusher.connection.state !== 'connected') {
4549
4636
  return;
4550
4637
  }
4551
- var onAuthorize = function (err, authData) {
4552
- if (err) {
4553
- logger.warn("Error during signin: " + err);
4554
- return;
4555
- }
4556
- _this.pusher.send_event('pusher:signin', {
4557
- auth: authData.auth,
4558
- user_data: authData.user_data
4559
- });
4560
- };
4561
4638
  this.pusher.config.userAuthenticator({
4562
4639
  socketId: this.pusher.connection.socket_id
4563
- }, onAuthorize);
4640
+ }, this._onAuthorize);
4564
4641
  };
4565
4642
  UserFacade.prototype._onSigninSuccess = function (data) {
4566
4643
  try {
@@ -4568,12 +4645,15 @@ var user_UserFacade = (function (_super) {
4568
4645
  }
4569
4646
  catch (e) {
4570
4647
  logger.error("Failed parsing user data after signin: " + data.user_data);
4648
+ this._cleanup();
4571
4649
  return;
4572
4650
  }
4573
4651
  if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
4574
4652
  logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
4653
+ this._cleanup();
4575
4654
  return;
4576
4655
  }
4656
+ this._signinDoneResolve();
4577
4657
  this._subscribeChannels();
4578
4658
  };
4579
4659
  UserFacade.prototype._subscribeChannels = function () {
@@ -4597,13 +4677,32 @@ var user_UserFacade = (function (_super) {
4597
4677
  });
4598
4678
  ensure_subscribed(this.serverToUserChannel);
4599
4679
  };
4600
- UserFacade.prototype._disconnect = function () {
4680
+ UserFacade.prototype._cleanup = function () {
4601
4681
  this.user_data = null;
4602
4682
  if (this.serverToUserChannel) {
4603
4683
  this.serverToUserChannel.unbind_all();
4604
4684
  this.serverToUserChannel.disconnect();
4605
4685
  this.serverToUserChannel = null;
4606
4686
  }
4687
+ if (this.signin_requested) {
4688
+ this._signinDoneResolve();
4689
+ }
4690
+ };
4691
+ UserFacade.prototype._newSigninPromiseIfNeeded = function () {
4692
+ if (!this.signin_requested) {
4693
+ return;
4694
+ }
4695
+ if (this.signinDonePromise && !this.signinDonePromise.done) {
4696
+ return;
4697
+ }
4698
+ var _a = flat_promise(), promise = _a.promise, resolve = _a.resolve, _ = _a.reject;
4699
+ promise.done = false;
4700
+ var setDone = function () {
4701
+ promise.done = true;
4702
+ };
4703
+ promise.then(setDone)["catch"](setDone);
4704
+ this.signinDonePromise = promise;
4705
+ this._signinDoneResolve = resolve;
4607
4706
  };
4608
4707
  return UserFacade;
4609
4708
  }(dispatcher));
@@ -4639,7 +4738,7 @@ var pusher_Pusher = (function () {
4639
4738
  this.config = getConfig(options, this);
4640
4739
  this.channels = factory.createChannels();
4641
4740
  this.global_emitter = new dispatcher();
4642
- this.sessionID = Math.floor(Math.random() * 1000000000);
4741
+ this.sessionID = runtime.randomInt(1000000000);
4643
4742
  this.timeline = new timeline_timeline(this.key, this.sessionID, {
4644
4743
  cluster: this.config.cluster,
4645
4744
  features: Pusher.getClientFeatures(),