pusher-js 7.0.6 → 7.2.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 (106) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +27 -87
  3. package/dist/node/pusher.js +325 -73
  4. package/dist/node/pusher.js.map +1 -1
  5. package/dist/react-native/pusher.js +2 -2
  6. package/dist/react-native/pusher.js.map +1 -1
  7. package/dist/web/pusher-with-encryption.js +330 -78
  8. package/dist/web/pusher-with-encryption.js.map +1 -1
  9. package/dist/web/pusher-with-encryption.min.js +2 -2
  10. package/dist/web/pusher-with-encryption.min.js.map +1 -1
  11. package/dist/web/pusher.js +330 -78
  12. package/dist/web/pusher.js.map +1 -1
  13. package/dist/web/pusher.min.js +2 -2
  14. package/dist/web/pusher.min.js.map +1 -1
  15. package/dist/worker/pusher-with-encryption.worker.js +314 -69
  16. package/dist/worker/pusher-with-encryption.worker.js.map +1 -1
  17. package/dist/worker/pusher-with-encryption.worker.min.js +2 -2
  18. package/dist/worker/pusher-with-encryption.worker.min.js.map +1 -1
  19. package/dist/worker/pusher.worker.js +314 -69
  20. package/dist/worker/pusher.worker.js.map +1 -1
  21. package/dist/worker/pusher.worker.min.js +2 -2
  22. package/dist/worker/pusher.worker.min.js.map +1 -1
  23. package/index.d.ts +8 -3
  24. package/package.json +2 -2
  25. package/spec/config/karma/config.worker.js +3 -0
  26. package/spec/config/karma/integration.js +4 -2
  27. package/spec/javascripts/helpers/mocks.js +41 -8
  28. package/spec/javascripts/helpers/worker/mock-dom-dependencies.js +1 -0
  29. package/spec/javascripts/integration/core/cluster_config_spec.js +8 -0
  30. package/spec/javascripts/integration/core/timeout_configuration_spec.js +1 -0
  31. package/spec/javascripts/integration/index.worker.js +12 -1
  32. package/spec/javascripts/unit/core/channels/channel_spec.js +25 -0
  33. package/spec/javascripts/unit/core/channels/encrypted_channel_spec.js +64 -66
  34. package/spec/javascripts/unit/core/channels/presence_channel_spec.js +51 -41
  35. package/spec/javascripts/unit/core/channels/private_channel_spec.js +8 -46
  36. package/spec/javascripts/unit/core/config_spec.js +307 -7
  37. package/spec/javascripts/unit/core/connection/connection_manager_spec.js +1 -0
  38. package/spec/javascripts/unit/core/http/http_socket_spec.js +1 -0
  39. package/spec/javascripts/unit/core/logger_spec.js +21 -20
  40. package/spec/javascripts/unit/core/pusher_spec.js +67 -39
  41. package/spec/javascripts/unit/core/pusher_with_encryption_spec.js +2 -0
  42. package/spec/javascripts/unit/core/strategies/cached_strategy_spec.js +1 -0
  43. package/spec/javascripts/unit/core/strategies/delayed_strategy_spec.js +1 -0
  44. package/spec/javascripts/unit/core/strategies/sequential_strategy_spec.js +1 -0
  45. package/spec/javascripts/unit/core/strategies/transport_strategy_spec.js +1 -0
  46. package/spec/javascripts/unit/core/transports/assistant_to_the_transport_manager_spec.js +1 -0
  47. package/spec/javascripts/unit/core/user_spec.js +295 -0
  48. package/spec/javascripts/unit/core/utils/periodic_timer_spec.js +4 -1
  49. package/spec/javascripts/unit/core/utils/timers_spec.js +6 -0
  50. package/spec/javascripts/unit/core/utils/url_store_spec.js +1 -1
  51. package/spec/javascripts/unit/core_with_runtime/auth/channel_authorizer_spec.js +55 -0
  52. package/spec/javascripts/unit/core_with_runtime/auth/deprecated_channel_authorizer_spec.js +48 -0
  53. package/spec/javascripts/unit/core_with_runtime/auth/user_authorizer_spec.js +52 -0
  54. package/spec/javascripts/unit/core_with_runtime/readme.md +5 -0
  55. package/spec/javascripts/unit/index.node.js +3 -0
  56. package/spec/javascripts/unit/index.web.js +3 -0
  57. package/spec/javascripts/unit/index.worker.js +3 -0
  58. package/spec/javascripts/unit/web/pusher_authorizer_spec.js +15 -16
  59. package/spec/javascripts/unit/web/transports/hosts_and_ports_spec.js +1 -0
  60. package/spec/javascripts/unit/worker/channel_authorizer_spec.js +110 -0
  61. package/src/core/auth/auth_transports.ts +8 -1
  62. package/src/core/auth/channel_authorizer.ts +53 -0
  63. package/src/core/auth/deprecated_channel_authorizer.ts +58 -0
  64. package/src/core/auth/options.ts +52 -17
  65. package/src/core/auth/user_authenticator.ts +51 -0
  66. package/src/core/channels/channel.ts +17 -3
  67. package/src/core/channels/channels.ts +4 -0
  68. package/src/core/channels/encrypted_channel.ts +26 -20
  69. package/src/core/channels/presence_channel.ts +5 -2
  70. package/src/core/channels/private_channel.ts +9 -4
  71. package/src/core/config.ts +76 -11
  72. package/src/core/defaults.ts +15 -0
  73. package/src/core/errors.ts +9 -0
  74. package/src/core/options.ts +18 -5
  75. package/src/core/pusher.ts +9 -1
  76. package/src/core/user.ts +143 -0
  77. package/src/core/utils/factory.ts +1 -10
  78. package/src/core/utils/url_store.ts +4 -1
  79. package/src/runtimes/isomorphic/auth/xhr_auth.ts +32 -19
  80. package/src/runtimes/web/auth/jsonp_auth.ts +13 -7
  81. package/src/runtimes/worker/auth/fetch_auth.ts +17 -12
  82. package/types/src/core/auth/auth_transports.d.ts +2 -1
  83. package/types/src/core/auth/channel_authorizer.d.ts +3 -0
  84. package/types/src/core/auth/deprecated_channel_authorizer.d.ts +18 -0
  85. package/types/src/core/auth/options.d.ts +34 -15
  86. package/types/src/core/auth/user_authenticator.d.ts +3 -0
  87. package/types/src/core/channels/channel.d.ts +4 -2
  88. package/types/src/core/channels/encrypted_channel.d.ts +2 -2
  89. package/types/src/core/channels/private_channel.d.ts +2 -2
  90. package/types/src/core/config.d.ts +4 -6
  91. package/types/src/core/defaults.d.ts +3 -0
  92. package/types/src/core/errors.d.ts +3 -0
  93. package/types/src/core/options.d.ts +6 -3
  94. package/types/src/core/pusher.d.ts +3 -0
  95. package/types/src/core/user.d.ts +15 -0
  96. package/types/src/core/utils/factory.d.ts +0 -2
  97. package/types/src/runtimes/isomorphic/auth/xhr_auth.d.ts +1 -1
  98. package/worker/with-encryption/index.js +1 -1
  99. package/spec/javascripts/unit/core/pusher_authorizer_spec.js +0 -160
  100. package/spec/javascripts/unit/worker/pusher_authorizer_spec.js +0 -111
  101. package/src/core/auth/pusher_authorizer.ts +0 -64
  102. package/types/index.d.ts +0 -15
  103. package/types/src/core/auth/pusher_authorizer.d.ts +0 -13
  104. package/types/src/core/index.d.ts +0 -6
  105. package/types/src/runtimes/react-native/tweetnacl-dummy.d.ts +0 -5
  106. package/types/src/runtimes/react-native/tweetnacl-util-dummy.d.ts +0 -7
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Pusher JavaScript Library v7.0.6
2
+ * Pusher JavaScript Library v7.2.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.0.6",
3302
+ VERSION: "7.2.0",
3303
3303
  PROTOCOL: 7,
3304
3304
  wsPort: 80,
3305
3305
  wssPort: 443,
@@ -3315,6 +3315,14 @@ var Defaults = {
3315
3315
  pongTimeout: 30000,
3316
3316
  unavailableTimeout: 10000,
3317
3317
  cluster: 'mt1',
3318
+ userAuthentication: {
3319
+ endpoint: '/pusher/user-auth',
3320
+ transport: 'ajax'
3321
+ },
3322
+ channelAuthorization: {
3323
+ endpoint: '/pusher/auth',
3324
+ transport: 'ajax'
3325
+ },
3318
3326
  cdn_http: "http://js.pusher.com",
3319
3327
  cdn_https: "https://js.pusher.com",
3320
3328
  dependency_suffix: ""
@@ -4095,42 +4103,6 @@ var handshake_Handshake = (function () {
4095
4103
  }());
4096
4104
  /* harmony default export */ var connection_handshake = (handshake_Handshake);
4097
4105
 
4098
- // CONCATENATED MODULE: ./src/core/auth/pusher_authorizer.ts
4099
-
4100
- var pusher_authorizer_PusherAuthorizer = (function () {
4101
- function PusherAuthorizer(channel, options) {
4102
- this.channel = channel;
4103
- var authTransport = options.authTransport;
4104
- if (typeof worker_runtime.getAuthorizers()[authTransport] === 'undefined') {
4105
- throw "'" + authTransport + "' is not a recognized auth transport";
4106
- }
4107
- this.type = authTransport;
4108
- this.options = options;
4109
- this.authOptions = options.auth || {};
4110
- }
4111
- PusherAuthorizer.prototype.composeQuery = function (socketId) {
4112
- var query = 'socket_id=' +
4113
- encodeURIComponent(socketId) +
4114
- '&channel_name=' +
4115
- encodeURIComponent(this.channel.name);
4116
- for (var i in this.authOptions.params) {
4117
- query +=
4118
- '&' +
4119
- encodeURIComponent(i) +
4120
- '=' +
4121
- encodeURIComponent(this.authOptions.params[i]);
4122
- }
4123
- return query;
4124
- };
4125
- PusherAuthorizer.prototype.authorize = function (socketId, callback) {
4126
- PusherAuthorizer.authorizers =
4127
- PusherAuthorizer.authorizers || worker_runtime.getAuthorizers();
4128
- PusherAuthorizer.authorizers[this.type].call(this, worker_runtime, socketId, callback);
4129
- };
4130
- return PusherAuthorizer;
4131
- }());
4132
- /* harmony default export */ var pusher_authorizer = (pusher_authorizer_PusherAuthorizer);
4133
-
4134
4106
  // CONCATENATED MODULE: ./src/core/timeline/timeline_sender.ts
4135
4107
 
4136
4108
  var timeline_sender_TimelineSender = (function () {
@@ -4173,6 +4145,17 @@ var BadEventName = (function (_super) {
4173
4145
  return BadEventName;
4174
4146
  }(Error));
4175
4147
 
4148
+ var BadChannelName = (function (_super) {
4149
+ errors_extends(BadChannelName, _super);
4150
+ function BadChannelName(msg) {
4151
+ var _newTarget = this.constructor;
4152
+ var _this = _super.call(this, msg) || this;
4153
+ Object.setPrototypeOf(_this, _newTarget.prototype);
4154
+ return _this;
4155
+ }
4156
+ return BadChannelName;
4157
+ }(Error));
4158
+
4176
4159
  var RequestTimedOut = (function (_super) {
4177
4160
  errors_extends(RequestTimedOut, _super);
4178
4161
  function RequestTimedOut(msg) {
@@ -4257,7 +4240,10 @@ var urlStore = {
4257
4240
  baseUrl: 'https://pusher.com',
4258
4241
  urls: {
4259
4242
  authenticationEndpoint: {
4260
- path: '/docs/authenticating_users'
4243
+ path: '/docs/channels/server_api/authenticating_users'
4244
+ },
4245
+ authorizationEndpoint: {
4246
+ path: '/docs/channels/server_api/authorizing-users/'
4261
4247
  },
4262
4248
  javascriptQuickStart: {
4263
4249
  path: '/docs/javascript_quick_start'
@@ -4343,6 +4329,9 @@ var channel_Channel = (function (_super) {
4343
4329
  if (eventName === 'pusher_internal:subscription_succeeded') {
4344
4330
  this.handleSubscriptionSucceededEvent(event);
4345
4331
  }
4332
+ else if (eventName === 'pusher_internal:subscription_count') {
4333
+ this.handleSubscriptionCountEvent(event);
4334
+ }
4346
4335
  else if (eventName.indexOf('pusher_internal:') !== 0) {
4347
4336
  var metadata = {};
4348
4337
  this.emit(eventName, data, metadata);
@@ -4358,6 +4347,12 @@ var channel_Channel = (function (_super) {
4358
4347
  this.emit('pusher:subscription_succeeded', event.data);
4359
4348
  }
4360
4349
  };
4350
+ Channel.prototype.handleSubscriptionCountEvent = function (event) {
4351
+ if (event.data.subscription_count) {
4352
+ this.subscriptionCount = event.data.subscription_count;
4353
+ }
4354
+ this.emit('pusher:subscription_count', event.data);
4355
+ };
4361
4356
  Channel.prototype.subscribe = function () {
4362
4357
  var _this = this;
4363
4358
  if (this.subscribed) {
@@ -4414,19 +4409,20 @@ var private_channel_extends = (undefined && undefined.__extends) || (function ()
4414
4409
  };
4415
4410
  })();
4416
4411
 
4417
-
4418
- var private_channel_PrivateChannel = (function (_super) {
4412
+ var PrivateChannel = (function (_super) {
4419
4413
  private_channel_extends(PrivateChannel, _super);
4420
4414
  function PrivateChannel() {
4421
4415
  return _super !== null && _super.apply(this, arguments) || this;
4422
4416
  }
4423
4417
  PrivateChannel.prototype.authorize = function (socketId, callback) {
4424
- var authorizer = factory.createAuthorizer(this, this.pusher.config);
4425
- return authorizer.authorize(socketId, callback);
4418
+ return this.pusher.config.channelAuthorizer({
4419
+ channelName: this.name,
4420
+ socketId: socketId
4421
+ }, callback);
4426
4422
  };
4427
4423
  return PrivateChannel;
4428
4424
  }(channels_channel));
4429
- /* harmony default export */ var private_channel = (private_channel_PrivateChannel);
4425
+ /* harmony default export */ var private_channel = (PrivateChannel);
4430
4426
 
4431
4427
  // CONCATENATED MODULE: ./src/core/channels/members.ts
4432
4428
 
@@ -4548,6 +4544,9 @@ var presence_channel_PresenceChannel = (function (_super) {
4548
4544
  case 'pusher_internal:subscription_succeeded':
4549
4545
  this.handleSubscriptionSucceededEvent(event);
4550
4546
  break;
4547
+ case 'pusher_internal:subscription_count':
4548
+ this.handleSubscriptionCountEvent(event);
4549
+ break;
4551
4550
  case 'pusher_internal:member_added':
4552
4551
  var addedMember = this.members.addMember(data);
4553
4552
  this.emit('pusher:member_added', addedMember);
@@ -5026,6 +5025,9 @@ function createChannel(name, pusher) {
5026
5025
  else if (name.indexOf('presence-') === 0) {
5027
5026
  return factory.createPresenceChannel(name, pusher);
5028
5027
  }
5028
+ else if (name.indexOf('#') === 0) {
5029
+ throw new BadChannelName('Cannot create a channel with name "' + name + '".');
5030
+ }
5029
5031
  else {
5030
5032
  return factory.createChannel(name, pusher);
5031
5033
  }
@@ -5041,7 +5043,6 @@ function createChannel(name, pusher) {
5041
5043
 
5042
5044
 
5043
5045
 
5044
-
5045
5046
  var Factory = {
5046
5047
  createChannels: function () {
5047
5048
  return new channels();
@@ -5064,12 +5065,6 @@ var Factory = {
5064
5065
  createTimelineSender: function (timeline, options) {
5065
5066
  return new timeline_sender(timeline, options);
5066
5067
  },
5067
- createAuthorizer: function (channel, options) {
5068
- if (options.authorizer) {
5069
- return options.authorizer(channel, options);
5070
- }
5071
- return new pusher_authorizer(channel, options);
5072
- },
5073
5068
  createHandshake: function (transport, callback) {
5074
5069
  return new connection_handshake(transport, callback);
5075
5070
  },
@@ -5974,14 +5969,14 @@ var net_info_Network = new NetInfo();
5974
5969
 
5975
5970
  // CONCATENATED MODULE: ./src/runtimes/worker/auth/fetch_auth.ts
5976
5971
 
5977
- var fetchAuth = function (context, socketId, callback) {
5972
+ var fetchAuth = function (context, query, authOptions, authRequestType, callback) {
5978
5973
  var headers = new Headers();
5979
5974
  headers.set('Content-Type', 'application/x-www-form-urlencoded');
5980
- for (var headerName in this.authOptions.headers) {
5981
- headers.set(headerName, this.authOptions.headers[headerName]);
5975
+ for (var headerName in authOptions.headers) {
5976
+ headers.set(headerName, authOptions.headers[headerName]);
5982
5977
  }
5983
- var body = this.composeQuery(socketId);
5984
- var request = new Request(this.options.authEndpoint, {
5978
+ var body = query;
5979
+ var request = new Request(authOptions.endpoint, {
5985
5980
  headers: headers,
5986
5981
  body: body,
5987
5982
  credentials: 'same-origin',
@@ -5993,7 +5988,7 @@ var fetchAuth = function (context, socketId, callback) {
5993
5988
  if (status === 200) {
5994
5989
  return response.text();
5995
5990
  }
5996
- throw new HTTPAuthError(200, "Could not get auth info from your auth endpoint, status: " + status);
5991
+ throw new HTTPAuthError(200, "Could not get " + authRequestType.toString() + " info from your auth endpoint, status: " + status);
5997
5992
  })
5998
5993
  .then(function (data) {
5999
5994
  var parsedData;
@@ -6001,12 +5996,11 @@ var fetchAuth = function (context, socketId, callback) {
6001
5996
  parsedData = JSON.parse(data);
6002
5997
  }
6003
5998
  catch (e) {
6004
- throw new HTTPAuthError(200, 'JSON returned from auth endpoint was invalid, yet status code was 200. Data was: ' +
6005
- data);
5999
+ throw new HTTPAuthError(200, "JSON returned from " + authRequestType.toString() + " endpoint was invalid, yet status code was 200. Data was: " + data);
6006
6000
  }
6007
6001
  callback(null, parsedData);
6008
6002
  })["catch"](function (err) {
6009
- callback(err, { auth: '' });
6003
+ callback(err, null);
6010
6004
  });
6011
6005
  };
6012
6006
  /* harmony default export */ var fetch_auth = (fetchAuth);
@@ -6296,14 +6290,101 @@ var strategy_builder_UnsupportedStrategy = {
6296
6290
  }
6297
6291
  };
6298
6292
 
6293
+ // CONCATENATED MODULE: ./src/core/auth/options.ts
6294
+ var AuthRequestType;
6295
+ (function (AuthRequestType) {
6296
+ AuthRequestType["UserAuthentication"] = "user-authentication";
6297
+ AuthRequestType["ChannelAuthorization"] = "channel-authorization";
6298
+ })(AuthRequestType || (AuthRequestType = {}));
6299
+
6300
+ // CONCATENATED MODULE: ./src/core/auth/user_authenticator.ts
6301
+
6302
+
6303
+ var composeChannelQuery = function (params, authOptions) {
6304
+ var query = 'socket_id=' + encodeURIComponent(params.socketId);
6305
+ for (var i in authOptions.params) {
6306
+ query +=
6307
+ '&' +
6308
+ encodeURIComponent(i) +
6309
+ '=' +
6310
+ encodeURIComponent(authOptions.params[i]);
6311
+ }
6312
+ return query;
6313
+ };
6314
+ var UserAuthenticator = function (authOptions) {
6315
+ if (typeof worker_runtime.getAuthorizers()[authOptions.transport] === 'undefined') {
6316
+ throw "'" + authOptions.transport + "' is not a recognized auth transport";
6317
+ }
6318
+ return function (params, callback) {
6319
+ var query = composeChannelQuery(params, authOptions);
6320
+ worker_runtime.getAuthorizers()[authOptions.transport](worker_runtime, query, authOptions, AuthRequestType.UserAuthentication, callback);
6321
+ };
6322
+ };
6323
+ /* harmony default export */ var user_authenticator = (UserAuthenticator);
6324
+
6325
+ // CONCATENATED MODULE: ./src/core/auth/channel_authorizer.ts
6326
+
6327
+
6328
+ var channel_authorizer_composeChannelQuery = function (params, authOptions) {
6329
+ var query = 'socket_id=' + encodeURIComponent(params.socketId);
6330
+ query += '&channel_name=' + encodeURIComponent(params.channelName);
6331
+ for (var i in authOptions.params) {
6332
+ query +=
6333
+ '&' +
6334
+ encodeURIComponent(i) +
6335
+ '=' +
6336
+ encodeURIComponent(authOptions.params[i]);
6337
+ }
6338
+ return query;
6339
+ };
6340
+ var ChannelAuthorizer = function (authOptions) {
6341
+ if (typeof worker_runtime.getAuthorizers()[authOptions.transport] === 'undefined') {
6342
+ throw "'" + authOptions.transport + "' is not a recognized auth transport";
6343
+ }
6344
+ return function (params, callback) {
6345
+ var query = channel_authorizer_composeChannelQuery(params, authOptions);
6346
+ worker_runtime.getAuthorizers()[authOptions.transport](worker_runtime, query, authOptions, AuthRequestType.ChannelAuthorization, callback);
6347
+ };
6348
+ };
6349
+ /* harmony default export */ var channel_authorizer = (ChannelAuthorizer);
6350
+
6351
+ // CONCATENATED MODULE: ./src/core/auth/deprecated_channel_authorizer.ts
6352
+ var ChannelAuthorizerProxy = function (pusher, authOptions, channelAuthorizerGenerator) {
6353
+ var deprecatedAuthorizerOptions = {
6354
+ authTransport: authOptions.transport,
6355
+ authEndpoint: authOptions.endpoint,
6356
+ auth: {
6357
+ params: authOptions.params,
6358
+ headers: authOptions.headers
6359
+ }
6360
+ };
6361
+ return function (params, callback) {
6362
+ var channel = pusher.channel(params.channelName);
6363
+ var channelAuthorizer = channelAuthorizerGenerator(channel, deprecatedAuthorizerOptions);
6364
+ channelAuthorizer.authorize(params.socketId, callback);
6365
+ };
6366
+ };
6367
+
6299
6368
  // CONCATENATED MODULE: ./src/core/config.ts
6369
+ var __assign = (undefined && undefined.__assign) || function () {
6370
+ __assign = Object.assign || function(t) {
6371
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
6372
+ s = arguments[i];
6373
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6374
+ t[p] = s[p];
6375
+ }
6376
+ return t;
6377
+ };
6378
+ return __assign.apply(this, arguments);
6379
+ };
6300
6380
 
6301
6381
 
6302
- function getConfig(opts) {
6382
+
6383
+
6384
+
6385
+ function getConfig(opts, pusher) {
6303
6386
  var config = {
6304
6387
  activityTimeout: opts.activityTimeout || defaults.activityTimeout,
6305
- authEndpoint: opts.authEndpoint || defaults.authEndpoint,
6306
- authTransport: opts.authTransport || defaults.authTransport,
6307
6388
  cluster: opts.cluster || defaults.cluster,
6308
6389
  httpPath: opts.httpPath || defaults.httpPath,
6309
6390
  httpPort: opts.httpPort || defaults.httpPort,
@@ -6317,12 +6398,10 @@ function getConfig(opts) {
6317
6398
  enableStats: getEnableStatsConfig(opts),
6318
6399
  httpHost: getHttpHost(opts),
6319
6400
  useTLS: shouldUseTLS(opts),
6320
- wsHost: getWebsocketHost(opts)
6401
+ wsHost: getWebsocketHost(opts),
6402
+ userAuthenticator: buildUserAuthenticator(opts),
6403
+ channelAuthorizer: buildChannelAuthorizer(opts, pusher)
6321
6404
  };
6322
- if ('auth' in opts)
6323
- config.auth = opts.auth;
6324
- if ('authorizer' in opts)
6325
- config.authorizer = opts.authorizer;
6326
6405
  if ('disabledTransports' in opts)
6327
6406
  config.disabledTransports = opts.disabledTransports;
6328
6407
  if ('enabledTransports' in opts)
@@ -6375,6 +6454,167 @@ function getEnableStatsConfig(opts) {
6375
6454
  }
6376
6455
  return false;
6377
6456
  }
6457
+ function buildUserAuthenticator(opts) {
6458
+ var userAuthentication = __assign({}, defaults.userAuthentication, opts.userAuthentication);
6459
+ if ('customHandler' in userAuthentication &&
6460
+ userAuthentication['customHandler'] != null) {
6461
+ return userAuthentication['customHandler'];
6462
+ }
6463
+ return user_authenticator(userAuthentication);
6464
+ }
6465
+ function buildChannelAuth(opts, pusher) {
6466
+ var channelAuthorization;
6467
+ if ('channelAuthorization' in opts) {
6468
+ channelAuthorization = __assign({}, defaults.channelAuthorization, opts.channelAuthorization);
6469
+ }
6470
+ else {
6471
+ channelAuthorization = {
6472
+ transport: opts.authTransport || defaults.authTransport,
6473
+ endpoint: opts.authEndpoint || defaults.authEndpoint
6474
+ };
6475
+ if ('auth' in opts) {
6476
+ if ('params' in opts.auth)
6477
+ channelAuthorization.params = opts.auth.params;
6478
+ if ('headers' in opts.auth)
6479
+ channelAuthorization.headers = opts.auth.headers;
6480
+ }
6481
+ if ('authorizer' in opts)
6482
+ channelAuthorization.customHandler = ChannelAuthorizerProxy(pusher, channelAuthorization, opts.authorizer);
6483
+ }
6484
+ return channelAuthorization;
6485
+ }
6486
+ function buildChannelAuthorizer(opts, pusher) {
6487
+ var channelAuthorization = buildChannelAuth(opts, pusher);
6488
+ if ('customHandler' in channelAuthorization &&
6489
+ channelAuthorization['customHandler'] != null) {
6490
+ return channelAuthorization['customHandler'];
6491
+ }
6492
+ return channel_authorizer(channelAuthorization);
6493
+ }
6494
+
6495
+ // CONCATENATED MODULE: ./src/core/user.ts
6496
+ var user_extends = (undefined && undefined.__extends) || (function () {
6497
+ var extendStatics = function (d, b) {
6498
+ extendStatics = Object.setPrototypeOf ||
6499
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6500
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6501
+ return extendStatics(d, b);
6502
+ };
6503
+ return function (d, b) {
6504
+ extendStatics(d, b);
6505
+ function __() { this.constructor = d; }
6506
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6507
+ };
6508
+ })();
6509
+
6510
+
6511
+
6512
+ var user_UserFacade = (function (_super) {
6513
+ user_extends(UserFacade, _super);
6514
+ function UserFacade(pusher) {
6515
+ var _this = _super.call(this, function (eventName, data) {
6516
+ logger.debug('No callbacks on user for ' + eventName);
6517
+ }) || this;
6518
+ _this.signin_requested = false;
6519
+ _this.user_data = null;
6520
+ _this.serverToUserChannel = null;
6521
+ _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();
6530
+ });
6531
+ _this.pusher.connection.bind('message', function (event) {
6532
+ var eventName = event.event;
6533
+ if (eventName === 'pusher:signin_success') {
6534
+ _this._onSigninSuccess(event.data);
6535
+ }
6536
+ if (_this.serverToUserChannel &&
6537
+ _this.serverToUserChannel.name === event.channel) {
6538
+ _this.serverToUserChannel.handleEvent(event);
6539
+ }
6540
+ });
6541
+ return _this;
6542
+ }
6543
+ UserFacade.prototype.signin = function () {
6544
+ if (this.signin_requested) {
6545
+ return;
6546
+ }
6547
+ this.signin_requested = true;
6548
+ this._signin();
6549
+ };
6550
+ UserFacade.prototype._signin = function () {
6551
+ var _this = this;
6552
+ if (!this.signin_requested) {
6553
+ return;
6554
+ }
6555
+ if (this.pusher.connection.state !== 'connected') {
6556
+ return;
6557
+ }
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
+ this.pusher.config.userAuthenticator({
6569
+ socketId: this.pusher.connection.socket_id
6570
+ }, onAuthorize);
6571
+ };
6572
+ UserFacade.prototype._onSigninSuccess = function (data) {
6573
+ try {
6574
+ this.user_data = JSON.parse(data.user_data);
6575
+ }
6576
+ catch (e) {
6577
+ logger.error("Failed parsing user data after signin: " + data.user_data);
6578
+ return;
6579
+ }
6580
+ if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
6581
+ logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
6582
+ return;
6583
+ }
6584
+ this._subscribeChannels();
6585
+ };
6586
+ UserFacade.prototype._subscribeChannels = function () {
6587
+ var _this = this;
6588
+ var ensure_subscribed = function (channel) {
6589
+ if (channel.subscriptionPending && channel.subscriptionCancelled) {
6590
+ channel.reinstateSubscription();
6591
+ }
6592
+ else if (!channel.subscriptionPending &&
6593
+ _this.pusher.connection.state === 'connected') {
6594
+ channel.subscribe();
6595
+ }
6596
+ };
6597
+ this.serverToUserChannel = new channels_channel("#server-to-user-" + this.user_data.id, this.pusher);
6598
+ this.serverToUserChannel.bind_global(function (eventName, data) {
6599
+ if (eventName.indexOf('pusher_internal:') === 0 ||
6600
+ eventName.indexOf('pusher:') === 0) {
6601
+ return;
6602
+ }
6603
+ _this.emit(eventName, data);
6604
+ });
6605
+ ensure_subscribed(this.serverToUserChannel);
6606
+ };
6607
+ UserFacade.prototype._disconnect = function () {
6608
+ this.user_data = null;
6609
+ if (this.serverToUserChannel) {
6610
+ this.serverToUserChannel.unbind_all();
6611
+ this.serverToUserChannel.disconnect();
6612
+ this.serverToUserChannel = null;
6613
+ }
6614
+ };
6615
+ return UserFacade;
6616
+ }(dispatcher));
6617
+ /* harmony default export */ var user = (user_UserFacade);
6378
6618
 
6379
6619
  // CONCATENATED MODULE: ./src/core/pusher.ts
6380
6620
 
@@ -6389,6 +6629,7 @@ function getEnableStatsConfig(opts) {
6389
6629
 
6390
6630
 
6391
6631
 
6632
+
6392
6633
  var pusher_Pusher = (function () {
6393
6634
  function Pusher(app_key, options) {
6394
6635
  var _this = this;
@@ -6402,7 +6643,7 @@ var pusher_Pusher = (function () {
6402
6643
  logger.warn('The disableStats option is deprecated in favor of enableStats');
6403
6644
  }
6404
6645
  this.key = app_key;
6405
- this.config = getConfig(options);
6646
+ this.config = getConfig(options, this);
6406
6647
  this.channels = factory.createChannels();
6407
6648
  this.global_emitter = new dispatcher();
6408
6649
  this.sessionID = Math.floor(Math.random() * 1000000000);
@@ -6461,6 +6702,7 @@ var pusher_Pusher = (function () {
6461
6702
  });
6462
6703
  Pusher.instances.push(this);
6463
6704
  this.timeline.info({ instances: Pusher.instances.length });
6705
+ this.user = new user(this);
6464
6706
  if (Pusher.isReady) {
6465
6707
  this.connect();
6466
6708
  }
@@ -6558,6 +6800,9 @@ var pusher_Pusher = (function () {
6558
6800
  Pusher.prototype.shouldUseTLS = function () {
6559
6801
  return this.config.useTLS;
6560
6802
  };
6803
+ Pusher.prototype.signin = function () {
6804
+ this.user.signin();
6805
+ };
6561
6806
  Pusher.instances = [];
6562
6807
  Pusher.isReady = false;
6563
6808
  Pusher.logToConsole = false;