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
@@ -2995,7 +2995,7 @@ var ScriptReceivers = new ScriptReceiverFactory('_pusher_script_', 'Pusher.Scrip
2995
2995
 
2996
2996
  // CONCATENATED MODULE: ./src/core/defaults.ts
2997
2997
  var Defaults = {
2998
- VERSION: "7.3.0",
2998
+ VERSION: "7.4.1",
2999
2999
  PROTOCOL: 7,
3000
3000
  wsPort: 80,
3001
3001
  wssPort: 443,
@@ -3287,7 +3287,7 @@ var ajax = function (context, query, authOptions, authRequestType, callback) {
3287
3287
  suffix = url_store.buildLogSuffix('authenticationEndpoint');
3288
3288
  break;
3289
3289
  case AuthRequestType.ChannelAuthorization:
3290
- suffix = "Clients must be authenticated to join private or presence channels. " + url_store.buildLogSuffix('authorizationEndpoint');
3290
+ suffix = "Clients must be authorized to join private or presence channels. " + url_store.buildLogSuffix('authorizationEndpoint');
3291
3291
  break;
3292
3292
  }
3293
3293
  callback(new HTTPAuthError(xhr.status, "Unable to retrieve auth string from " + authRequestType.toString() + " endpoint - " +
@@ -4875,6 +4875,42 @@ var presence_channel_extends = (undefined && undefined.__extends) || (function (
4875
4875
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
4876
4876
  };
4877
4877
  })();
4878
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
4879
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4880
+ return new (P || (P = Promise))(function (resolve, reject) {
4881
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
4882
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
4883
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
4884
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
4885
+ });
4886
+ };
4887
+ var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
4888
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4889
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
4890
+ function verb(n) { return function (v) { return step([n, v]); }; }
4891
+ function step(op) {
4892
+ if (f) throw new TypeError("Generator is already executing.");
4893
+ while (_) try {
4894
+ 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;
4895
+ if (y = 0, t) op = [op[0] & 2, t.value];
4896
+ switch (op[0]) {
4897
+ case 0: case 1: t = op; break;
4898
+ case 4: _.label++; return { value: op[1], done: false };
4899
+ case 5: _.label++; y = op[1]; op = [0]; continue;
4900
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
4901
+ default:
4902
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
4903
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
4904
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
4905
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
4906
+ if (t[2]) _.ops.pop();
4907
+ _.trys.pop(); continue;
4908
+ }
4909
+ op = body.call(thisArg, _);
4910
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
4911
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
4912
+ }
4913
+ };
4878
4914
 
4879
4915
 
4880
4916
 
@@ -4888,21 +4924,38 @@ var presence_channel_PresenceChannel = (function (_super) {
4888
4924
  }
4889
4925
  PresenceChannel.prototype.authorize = function (socketId, callback) {
4890
4926
  var _this = this;
4891
- _super.prototype.authorize.call(this, socketId, function (error, authData) {
4892
- if (!error) {
4893
- authData = authData;
4894
- if (authData.channel_data === undefined) {
4895
- var suffix = url_store.buildLogSuffix('authenticationEndpoint');
4896
- logger.error("Invalid auth response for channel '" + _this.name + "'," +
4897
- ("expected 'channel_data' field. " + suffix));
4898
- callback('Invalid auth response');
4899
- return;
4927
+ _super.prototype.authorize.call(this, socketId, function (error, authData) { return __awaiter(_this, void 0, void 0, function () {
4928
+ var channelData, suffix;
4929
+ return __generator(this, function (_a) {
4930
+ switch (_a.label) {
4931
+ case 0:
4932
+ if (!!error) return [3, 3];
4933
+ authData = authData;
4934
+ if (!(authData.channel_data != null)) return [3, 1];
4935
+ channelData = JSON.parse(authData.channel_data);
4936
+ this.members.setMyID(channelData.user_id);
4937
+ return [3, 3];
4938
+ case 1: return [4, this.pusher.user.signinDonePromise];
4939
+ case 2:
4940
+ _a.sent();
4941
+ if (this.pusher.user.user_data != null) {
4942
+ this.members.setMyID(this.pusher.user.user_data.id);
4943
+ }
4944
+ else {
4945
+ suffix = url_store.buildLogSuffix('authorizationEndpoint');
4946
+ logger.error("Invalid auth response for channel '" + this.name + "', " +
4947
+ ("expected 'channel_data' field. " + suffix + ", ") +
4948
+ "or the user should be signed in.");
4949
+ callback('Invalid auth response');
4950
+ return [2];
4951
+ }
4952
+ _a.label = 3;
4953
+ case 3:
4954
+ callback(error, authData);
4955
+ return [2];
4900
4956
  }
4901
- var channelData = JSON.parse(authData.channel_data);
4902
- _this.members.setMyID(channelData.user_id);
4903
- }
4904
- callback(error, authData);
4905
- });
4957
+ });
4958
+ }); });
4906
4959
  };
4907
4960
  PresenceChannel.prototype.handleEvent = function (event) {
4908
4961
  var eventName = event.event;
@@ -6225,7 +6278,7 @@ function replaceHost(url, hostname) {
6225
6278
  return urlParts[1] + hostname + urlParts[3];
6226
6279
  }
6227
6280
  function randomNumber(max) {
6228
- return Math.floor(Math.random() * max);
6281
+ return runtime.randomInt(max);
6229
6282
  }
6230
6283
  function randomString(length) {
6231
6284
  var result = [];
@@ -6471,6 +6524,14 @@ var Runtime = {
6471
6524
  else if (window.detachEvent !== undefined) {
6472
6525
  window.detachEvent('onunload', listener);
6473
6526
  }
6527
+ },
6528
+ randomInt: function (max) {
6529
+ var random = function () {
6530
+ var crypto = window.crypto || window['msCrypto'];
6531
+ var random = crypto.getRandomValues(new Uint32Array(1))[0];
6532
+ return random / Math.pow(2, 32);
6533
+ };
6534
+ return Math.floor(random() * max);
6474
6535
  }
6475
6536
  };
6476
6537
  /* harmony default export */ var runtime = (Runtime);
@@ -6850,7 +6911,7 @@ function getEnableStatsConfig(opts) {
6850
6911
  return false;
6851
6912
  }
6852
6913
  function buildUserAuthenticator(opts) {
6853
- var userAuthentication = __assign({}, defaults.userAuthentication, opts.userAuthentication);
6914
+ var userAuthentication = __assign(__assign({}, defaults.userAuthentication), opts.userAuthentication);
6854
6915
  if ('customHandler' in userAuthentication &&
6855
6916
  userAuthentication['customHandler'] != null) {
6856
6917
  return userAuthentication['customHandler'];
@@ -6860,7 +6921,7 @@ function buildUserAuthenticator(opts) {
6860
6921
  function buildChannelAuth(opts, pusher) {
6861
6922
  var channelAuthorization;
6862
6923
  if ('channelAuthorization' in opts) {
6863
- channelAuthorization = __assign({}, defaults.channelAuthorization, opts.channelAuthorization);
6924
+ channelAuthorization = __assign(__assign({}, defaults.channelAuthorization), opts.channelAuthorization);
6864
6925
  }
6865
6926
  else {
6866
6927
  channelAuthorization = {
@@ -6887,6 +6948,17 @@ function buildChannelAuthorizer(opts, pusher) {
6887
6948
  return channel_authorizer(channelAuthorization);
6888
6949
  }
6889
6950
 
6951
+ // CONCATENATED MODULE: ./src/core/utils/flat_promise.ts
6952
+ function flatPromise() {
6953
+ var resolve, reject;
6954
+ var promise = new Promise(function (res, rej) {
6955
+ resolve = res;
6956
+ reject = rej;
6957
+ });
6958
+ return { promise: promise, resolve: resolve, reject: reject };
6959
+ }
6960
+ /* harmony default export */ var flat_promise = (flatPromise);
6961
+
6890
6962
  // CONCATENATED MODULE: ./src/core/user.ts
6891
6963
  var user_extends = (undefined && undefined.__extends) || (function () {
6892
6964
  var extendStatics = function (d, b) {
@@ -6904,6 +6976,7 @@ var user_extends = (undefined && undefined.__extends) || (function () {
6904
6976
 
6905
6977
 
6906
6978
 
6979
+
6907
6980
  var user_UserFacade = (function (_super) {
6908
6981
  user_extends(UserFacade, _super);
6909
6982
  function UserFacade(pusher) {
@@ -6913,15 +6986,29 @@ var user_UserFacade = (function (_super) {
6913
6986
  _this.signin_requested = false;
6914
6987
  _this.user_data = null;
6915
6988
  _this.serverToUserChannel = null;
6989
+ _this.signinDonePromise = null;
6990
+ _this._signinDoneResolve = null;
6991
+ _this._onAuthorize = function (err, authData) {
6992
+ if (err) {
6993
+ logger.warn("Error during signin: " + err);
6994
+ _this._cleanup();
6995
+ return;
6996
+ }
6997
+ _this.pusher.send_event('pusher:signin', {
6998
+ auth: authData.auth,
6999
+ user_data: authData.user_data
7000
+ });
7001
+ };
6916
7002
  _this.pusher = pusher;
6917
- _this.pusher.connection.bind('connected', function () {
6918
- _this._signin();
6919
- });
6920
- _this.pusher.connection.bind('connecting', function () {
6921
- _this._disconnect();
6922
- });
6923
- _this.pusher.connection.bind('disconnected', function () {
6924
- _this._disconnect();
7003
+ _this.pusher.connection.bind('state_change', function (_a) {
7004
+ var previous = _a.previous, current = _a.current;
7005
+ if (previous !== 'connected' && current === 'connected') {
7006
+ _this._signin();
7007
+ }
7008
+ if (previous === 'connected' && current !== 'connected') {
7009
+ _this._cleanup();
7010
+ _this._newSigninPromiseIfNeeded();
7011
+ }
6925
7012
  });
6926
7013
  _this.pusher.connection.bind('message', function (event) {
6927
7014
  var eventName = event.event;
@@ -6943,26 +7030,16 @@ var user_UserFacade = (function (_super) {
6943
7030
  this._signin();
6944
7031
  };
6945
7032
  UserFacade.prototype._signin = function () {
6946
- var _this = this;
6947
7033
  if (!this.signin_requested) {
6948
7034
  return;
6949
7035
  }
7036
+ this._newSigninPromiseIfNeeded();
6950
7037
  if (this.pusher.connection.state !== 'connected') {
6951
7038
  return;
6952
7039
  }
6953
- var onAuthorize = function (err, authData) {
6954
- if (err) {
6955
- logger.warn("Error during signin: " + err);
6956
- return;
6957
- }
6958
- _this.pusher.send_event('pusher:signin', {
6959
- auth: authData.auth,
6960
- user_data: authData.user_data
6961
- });
6962
- };
6963
7040
  this.pusher.config.userAuthenticator({
6964
7041
  socketId: this.pusher.connection.socket_id
6965
- }, onAuthorize);
7042
+ }, this._onAuthorize);
6966
7043
  };
6967
7044
  UserFacade.prototype._onSigninSuccess = function (data) {
6968
7045
  try {
@@ -6970,12 +7047,15 @@ var user_UserFacade = (function (_super) {
6970
7047
  }
6971
7048
  catch (e) {
6972
7049
  logger.error("Failed parsing user data after signin: " + data.user_data);
7050
+ this._cleanup();
6973
7051
  return;
6974
7052
  }
6975
7053
  if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
6976
7054
  logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
7055
+ this._cleanup();
6977
7056
  return;
6978
7057
  }
7058
+ this._signinDoneResolve();
6979
7059
  this._subscribeChannels();
6980
7060
  };
6981
7061
  UserFacade.prototype._subscribeChannels = function () {
@@ -6999,13 +7079,32 @@ var user_UserFacade = (function (_super) {
6999
7079
  });
7000
7080
  ensure_subscribed(this.serverToUserChannel);
7001
7081
  };
7002
- UserFacade.prototype._disconnect = function () {
7082
+ UserFacade.prototype._cleanup = function () {
7003
7083
  this.user_data = null;
7004
7084
  if (this.serverToUserChannel) {
7005
7085
  this.serverToUserChannel.unbind_all();
7006
7086
  this.serverToUserChannel.disconnect();
7007
7087
  this.serverToUserChannel = null;
7008
7088
  }
7089
+ if (this.signin_requested) {
7090
+ this._signinDoneResolve();
7091
+ }
7092
+ };
7093
+ UserFacade.prototype._newSigninPromiseIfNeeded = function () {
7094
+ if (!this.signin_requested) {
7095
+ return;
7096
+ }
7097
+ if (this.signinDonePromise && !this.signinDonePromise.done) {
7098
+ return;
7099
+ }
7100
+ var _a = flat_promise(), promise = _a.promise, resolve = _a.resolve, _ = _a.reject;
7101
+ promise.done = false;
7102
+ var setDone = function () {
7103
+ promise.done = true;
7104
+ };
7105
+ promise.then(setDone)["catch"](setDone);
7106
+ this.signinDonePromise = promise;
7107
+ this._signinDoneResolve = resolve;
7009
7108
  };
7010
7109
  return UserFacade;
7011
7110
  }(dispatcher));
@@ -7041,7 +7140,7 @@ var pusher_Pusher = (function () {
7041
7140
  this.config = getConfig(options, this);
7042
7141
  this.channels = factory.createChannels();
7043
7142
  this.global_emitter = new dispatcher();
7044
- this.sessionID = Math.floor(Math.random() * 1000000000);
7143
+ this.sessionID = runtime.randomInt(1000000000);
7045
7144
  this.timeline = new timeline_timeline(this.key, this.sessionID, {
7046
7145
  cluster: this.config.cluster,
7047
7146
  features: Pusher.getClientFeatures(),