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
@@ -897,7 +897,7 @@ function safeJSONStringify(source) {
897
897
 
898
898
  // CONCATENATED MODULE: ./src/core/defaults.ts
899
899
  var Defaults = {
900
- VERSION: "7.3.0",
900
+ VERSION: "7.4.1",
901
901
  PROTOCOL: 7,
902
902
  wsPort: 80,
903
903
  wssPort: 443,
@@ -2092,6 +2092,42 @@ var presence_channel_extends = (undefined && undefined.__extends) || (function (
2092
2092
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2093
2093
  };
2094
2094
  })();
2095
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
2096
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
2097
+ return new (P || (P = Promise))(function (resolve, reject) {
2098
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
2099
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
2100
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
2101
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
2102
+ });
2103
+ };
2104
+ var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
2105
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
2106
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
2107
+ function verb(n) { return function (v) { return step([n, v]); }; }
2108
+ function step(op) {
2109
+ if (f) throw new TypeError("Generator is already executing.");
2110
+ while (_) try {
2111
+ 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;
2112
+ if (y = 0, t) op = [op[0] & 2, t.value];
2113
+ switch (op[0]) {
2114
+ case 0: case 1: t = op; break;
2115
+ case 4: _.label++; return { value: op[1], done: false };
2116
+ case 5: _.label++; y = op[1]; op = [0]; continue;
2117
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
2118
+ default:
2119
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
2120
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
2121
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
2122
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
2123
+ if (t[2]) _.ops.pop();
2124
+ _.trys.pop(); continue;
2125
+ }
2126
+ op = body.call(thisArg, _);
2127
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
2128
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
2129
+ }
2130
+ };
2095
2131
 
2096
2132
 
2097
2133
 
@@ -2105,21 +2141,38 @@ var presence_channel_PresenceChannel = (function (_super) {
2105
2141
  }
2106
2142
  PresenceChannel.prototype.authorize = function (socketId, callback) {
2107
2143
  var _this = this;
2108
- _super.prototype.authorize.call(this, socketId, function (error, authData) {
2109
- if (!error) {
2110
- authData = authData;
2111
- if (authData.channel_data === undefined) {
2112
- var suffix = url_store.buildLogSuffix('authenticationEndpoint');
2113
- logger.error("Invalid auth response for channel '" + _this.name + "'," +
2114
- ("expected 'channel_data' field. " + suffix));
2115
- callback('Invalid auth response');
2116
- return;
2144
+ _super.prototype.authorize.call(this, socketId, function (error, authData) { return __awaiter(_this, void 0, void 0, function () {
2145
+ var channelData, suffix;
2146
+ return __generator(this, function (_a) {
2147
+ switch (_a.label) {
2148
+ case 0:
2149
+ if (!!error) return [3, 3];
2150
+ authData = authData;
2151
+ if (!(authData.channel_data != null)) return [3, 1];
2152
+ channelData = JSON.parse(authData.channel_data);
2153
+ this.members.setMyID(channelData.user_id);
2154
+ return [3, 3];
2155
+ case 1: return [4, this.pusher.user.signinDonePromise];
2156
+ case 2:
2157
+ _a.sent();
2158
+ if (this.pusher.user.user_data != null) {
2159
+ this.members.setMyID(this.pusher.user.user_data.id);
2160
+ }
2161
+ else {
2162
+ suffix = url_store.buildLogSuffix('authorizationEndpoint');
2163
+ logger.error("Invalid auth response for channel '" + this.name + "', " +
2164
+ ("expected 'channel_data' field. " + suffix + ", ") +
2165
+ "or the user should be signed in.");
2166
+ callback('Invalid auth response');
2167
+ return [2];
2168
+ }
2169
+ _a.label = 3;
2170
+ case 3:
2171
+ callback(error, authData);
2172
+ return [2];
2117
2173
  }
2118
- var channelData = JSON.parse(authData.channel_data);
2119
- _this.members.setMyID(channelData.user_id);
2120
- }
2121
- callback(error, authData);
2122
- });
2174
+ });
2175
+ }); });
2123
2176
  };
2124
2177
  PresenceChannel.prototype.handleEvent = function (event) {
2125
2178
  var eventName = event.event;
@@ -3383,7 +3436,7 @@ function replaceHost(url, hostname) {
3383
3436
  return urlParts[1] + hostname + urlParts[3];
3384
3437
  }
3385
3438
  function randomNumber(max) {
3386
- return Math.floor(Math.random() * max);
3439
+ return worker_runtime.randomInt(max);
3387
3440
  }
3388
3441
  function randomString(length) {
3389
3442
  var result = [];
@@ -3667,6 +3720,14 @@ var Worker = {
3667
3720
  },
3668
3721
  getNetwork: function () {
3669
3722
  return net_info_Network;
3723
+ },
3724
+ randomInt: function (max) {
3725
+ var random = function () {
3726
+ var crypto = window.crypto || window['msCrypto'];
3727
+ var random = crypto.getRandomValues(new Uint32Array(1))[0];
3728
+ return random / Math.pow(2, 32);
3729
+ };
3730
+ return Math.floor(random() * max);
3670
3731
  }
3671
3732
  };
3672
3733
  /* harmony default export */ var worker_runtime = (Worker);
@@ -4053,7 +4114,7 @@ function getEnableStatsConfig(opts) {
4053
4114
  return false;
4054
4115
  }
4055
4116
  function buildUserAuthenticator(opts) {
4056
- var userAuthentication = __assign({}, defaults.userAuthentication, opts.userAuthentication);
4117
+ var userAuthentication = __assign(__assign({}, defaults.userAuthentication), opts.userAuthentication);
4057
4118
  if ('customHandler' in userAuthentication &&
4058
4119
  userAuthentication['customHandler'] != null) {
4059
4120
  return userAuthentication['customHandler'];
@@ -4063,7 +4124,7 @@ function buildUserAuthenticator(opts) {
4063
4124
  function buildChannelAuth(opts, pusher) {
4064
4125
  var channelAuthorization;
4065
4126
  if ('channelAuthorization' in opts) {
4066
- channelAuthorization = __assign({}, defaults.channelAuthorization, opts.channelAuthorization);
4127
+ channelAuthorization = __assign(__assign({}, defaults.channelAuthorization), opts.channelAuthorization);
4067
4128
  }
4068
4129
  else {
4069
4130
  channelAuthorization = {
@@ -4090,6 +4151,17 @@ function buildChannelAuthorizer(opts, pusher) {
4090
4151
  return channel_authorizer(channelAuthorization);
4091
4152
  }
4092
4153
 
4154
+ // CONCATENATED MODULE: ./src/core/utils/flat_promise.ts
4155
+ function flatPromise() {
4156
+ var resolve, reject;
4157
+ var promise = new Promise(function (res, rej) {
4158
+ resolve = res;
4159
+ reject = rej;
4160
+ });
4161
+ return { promise: promise, resolve: resolve, reject: reject };
4162
+ }
4163
+ /* harmony default export */ var flat_promise = (flatPromise);
4164
+
4093
4165
  // CONCATENATED MODULE: ./src/core/user.ts
4094
4166
  var user_extends = (undefined && undefined.__extends) || (function () {
4095
4167
  var extendStatics = function (d, b) {
@@ -4107,6 +4179,7 @@ var user_extends = (undefined && undefined.__extends) || (function () {
4107
4179
 
4108
4180
 
4109
4181
 
4182
+
4110
4183
  var user_UserFacade = (function (_super) {
4111
4184
  user_extends(UserFacade, _super);
4112
4185
  function UserFacade(pusher) {
@@ -4116,15 +4189,29 @@ var user_UserFacade = (function (_super) {
4116
4189
  _this.signin_requested = false;
4117
4190
  _this.user_data = null;
4118
4191
  _this.serverToUserChannel = null;
4192
+ _this.signinDonePromise = null;
4193
+ _this._signinDoneResolve = null;
4194
+ _this._onAuthorize = function (err, authData) {
4195
+ if (err) {
4196
+ logger.warn("Error during signin: " + err);
4197
+ _this._cleanup();
4198
+ return;
4199
+ }
4200
+ _this.pusher.send_event('pusher:signin', {
4201
+ auth: authData.auth,
4202
+ user_data: authData.user_data
4203
+ });
4204
+ };
4119
4205
  _this.pusher = pusher;
4120
- _this.pusher.connection.bind('connected', function () {
4121
- _this._signin();
4122
- });
4123
- _this.pusher.connection.bind('connecting', function () {
4124
- _this._disconnect();
4125
- });
4126
- _this.pusher.connection.bind('disconnected', function () {
4127
- _this._disconnect();
4206
+ _this.pusher.connection.bind('state_change', function (_a) {
4207
+ var previous = _a.previous, current = _a.current;
4208
+ if (previous !== 'connected' && current === 'connected') {
4209
+ _this._signin();
4210
+ }
4211
+ if (previous === 'connected' && current !== 'connected') {
4212
+ _this._cleanup();
4213
+ _this._newSigninPromiseIfNeeded();
4214
+ }
4128
4215
  });
4129
4216
  _this.pusher.connection.bind('message', function (event) {
4130
4217
  var eventName = event.event;
@@ -4146,26 +4233,16 @@ var user_UserFacade = (function (_super) {
4146
4233
  this._signin();
4147
4234
  };
4148
4235
  UserFacade.prototype._signin = function () {
4149
- var _this = this;
4150
4236
  if (!this.signin_requested) {
4151
4237
  return;
4152
4238
  }
4239
+ this._newSigninPromiseIfNeeded();
4153
4240
  if (this.pusher.connection.state !== 'connected') {
4154
4241
  return;
4155
4242
  }
4156
- var onAuthorize = function (err, authData) {
4157
- if (err) {
4158
- logger.warn("Error during signin: " + err);
4159
- return;
4160
- }
4161
- _this.pusher.send_event('pusher:signin', {
4162
- auth: authData.auth,
4163
- user_data: authData.user_data
4164
- });
4165
- };
4166
4243
  this.pusher.config.userAuthenticator({
4167
4244
  socketId: this.pusher.connection.socket_id
4168
- }, onAuthorize);
4245
+ }, this._onAuthorize);
4169
4246
  };
4170
4247
  UserFacade.prototype._onSigninSuccess = function (data) {
4171
4248
  try {
@@ -4173,12 +4250,15 @@ var user_UserFacade = (function (_super) {
4173
4250
  }
4174
4251
  catch (e) {
4175
4252
  logger.error("Failed parsing user data after signin: " + data.user_data);
4253
+ this._cleanup();
4176
4254
  return;
4177
4255
  }
4178
4256
  if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
4179
4257
  logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
4258
+ this._cleanup();
4180
4259
  return;
4181
4260
  }
4261
+ this._signinDoneResolve();
4182
4262
  this._subscribeChannels();
4183
4263
  };
4184
4264
  UserFacade.prototype._subscribeChannels = function () {
@@ -4202,13 +4282,32 @@ var user_UserFacade = (function (_super) {
4202
4282
  });
4203
4283
  ensure_subscribed(this.serverToUserChannel);
4204
4284
  };
4205
- UserFacade.prototype._disconnect = function () {
4285
+ UserFacade.prototype._cleanup = function () {
4206
4286
  this.user_data = null;
4207
4287
  if (this.serverToUserChannel) {
4208
4288
  this.serverToUserChannel.unbind_all();
4209
4289
  this.serverToUserChannel.disconnect();
4210
4290
  this.serverToUserChannel = null;
4211
4291
  }
4292
+ if (this.signin_requested) {
4293
+ this._signinDoneResolve();
4294
+ }
4295
+ };
4296
+ UserFacade.prototype._newSigninPromiseIfNeeded = function () {
4297
+ if (!this.signin_requested) {
4298
+ return;
4299
+ }
4300
+ if (this.signinDonePromise && !this.signinDonePromise.done) {
4301
+ return;
4302
+ }
4303
+ var _a = flat_promise(), promise = _a.promise, resolve = _a.resolve, _ = _a.reject;
4304
+ promise.done = false;
4305
+ var setDone = function () {
4306
+ promise.done = true;
4307
+ };
4308
+ promise.then(setDone)["catch"](setDone);
4309
+ this.signinDonePromise = promise;
4310
+ this._signinDoneResolve = resolve;
4212
4311
  };
4213
4312
  return UserFacade;
4214
4313
  }(dispatcher));
@@ -4244,7 +4343,7 @@ var pusher_Pusher = (function () {
4244
4343
  this.config = getConfig(options, this);
4245
4344
  this.channels = factory.createChannels();
4246
4345
  this.global_emitter = new dispatcher();
4247
- this.sessionID = Math.floor(Math.random() * 1000000000);
4346
+ this.sessionID = worker_runtime.randomInt(1000000000);
4248
4347
  this.timeline = new timeline_timeline(this.key, this.sessionID, {
4249
4348
  cluster: this.config.cluster,
4250
4349
  features: Pusher.getClientFeatures(),