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
@@ -7487,7 +7487,7 @@ function safeJSONStringify(source) {
7487
7487
 
7488
7488
  // CONCATENATED MODULE: ./src/core/defaults.ts
7489
7489
  var Defaults = {
7490
- VERSION: "7.0.6",
7490
+ VERSION: "7.2.0",
7491
7491
  PROTOCOL: 7,
7492
7492
  wsPort: 80,
7493
7493
  wssPort: 443,
@@ -7503,6 +7503,14 @@ var Defaults = {
7503
7503
  pongTimeout: 30000,
7504
7504
  unavailableTimeout: 10000,
7505
7505
  cluster: 'mt1',
7506
+ userAuthentication: {
7507
+ endpoint: '/pusher/user-auth',
7508
+ transport: 'ajax'
7509
+ },
7510
+ channelAuthorization: {
7511
+ endpoint: '/pusher/auth',
7512
+ transport: 'ajax'
7513
+ },
7506
7514
  cdn_http: "http://js.pusher.com",
7507
7515
  cdn_https: "https://js.pusher.com",
7508
7516
  dependency_suffix: ""
@@ -8283,42 +8291,6 @@ var handshake_Handshake = (function () {
8283
8291
  }());
8284
8292
  /* harmony default export */ var connection_handshake = (handshake_Handshake);
8285
8293
 
8286
- // CONCATENATED MODULE: ./src/core/auth/pusher_authorizer.ts
8287
-
8288
- var pusher_authorizer_PusherAuthorizer = (function () {
8289
- function PusherAuthorizer(channel, options) {
8290
- this.channel = channel;
8291
- var authTransport = options.authTransport;
8292
- if (typeof node_runtime.getAuthorizers()[authTransport] === 'undefined') {
8293
- throw "'" + authTransport + "' is not a recognized auth transport";
8294
- }
8295
- this.type = authTransport;
8296
- this.options = options;
8297
- this.authOptions = options.auth || {};
8298
- }
8299
- PusherAuthorizer.prototype.composeQuery = function (socketId) {
8300
- var query = 'socket_id=' +
8301
- encodeURIComponent(socketId) +
8302
- '&channel_name=' +
8303
- encodeURIComponent(this.channel.name);
8304
- for (var i in this.authOptions.params) {
8305
- query +=
8306
- '&' +
8307
- encodeURIComponent(i) +
8308
- '=' +
8309
- encodeURIComponent(this.authOptions.params[i]);
8310
- }
8311
- return query;
8312
- };
8313
- PusherAuthorizer.prototype.authorize = function (socketId, callback) {
8314
- PusherAuthorizer.authorizers =
8315
- PusherAuthorizer.authorizers || node_runtime.getAuthorizers();
8316
- PusherAuthorizer.authorizers[this.type].call(this, node_runtime, socketId, callback);
8317
- };
8318
- return PusherAuthorizer;
8319
- }());
8320
- /* harmony default export */ var pusher_authorizer = (pusher_authorizer_PusherAuthorizer);
8321
-
8322
8294
  // CONCATENATED MODULE: ./src/core/timeline/timeline_sender.ts
8323
8295
 
8324
8296
  var timeline_sender_TimelineSender = (function () {
@@ -8361,6 +8333,17 @@ var BadEventName = (function (_super) {
8361
8333
  return BadEventName;
8362
8334
  }(Error));
8363
8335
 
8336
+ var BadChannelName = (function (_super) {
8337
+ errors_extends(BadChannelName, _super);
8338
+ function BadChannelName(msg) {
8339
+ var _newTarget = this.constructor;
8340
+ var _this = _super.call(this, msg) || this;
8341
+ Object.setPrototypeOf(_this, _newTarget.prototype);
8342
+ return _this;
8343
+ }
8344
+ return BadChannelName;
8345
+ }(Error));
8346
+
8364
8347
  var RequestTimedOut = (function (_super) {
8365
8348
  errors_extends(RequestTimedOut, _super);
8366
8349
  function RequestTimedOut(msg) {
@@ -8445,7 +8428,10 @@ var urlStore = {
8445
8428
  baseUrl: 'https://pusher.com',
8446
8429
  urls: {
8447
8430
  authenticationEndpoint: {
8448
- path: '/docs/authenticating_users'
8431
+ path: '/docs/channels/server_api/authenticating_users'
8432
+ },
8433
+ authorizationEndpoint: {
8434
+ path: '/docs/channels/server_api/authorizing-users/'
8449
8435
  },
8450
8436
  javascriptQuickStart: {
8451
8437
  path: '/docs/javascript_quick_start'
@@ -8531,6 +8517,9 @@ var channel_Channel = (function (_super) {
8531
8517
  if (eventName === 'pusher_internal:subscription_succeeded') {
8532
8518
  this.handleSubscriptionSucceededEvent(event);
8533
8519
  }
8520
+ else if (eventName === 'pusher_internal:subscription_count') {
8521
+ this.handleSubscriptionCountEvent(event);
8522
+ }
8534
8523
  else if (eventName.indexOf('pusher_internal:') !== 0) {
8535
8524
  var metadata = {};
8536
8525
  this.emit(eventName, data, metadata);
@@ -8546,6 +8535,12 @@ var channel_Channel = (function (_super) {
8546
8535
  this.emit('pusher:subscription_succeeded', event.data);
8547
8536
  }
8548
8537
  };
8538
+ Channel.prototype.handleSubscriptionCountEvent = function (event) {
8539
+ if (event.data.subscription_count) {
8540
+ this.subscriptionCount = event.data.subscription_count;
8541
+ }
8542
+ this.emit('pusher:subscription_count', event.data);
8543
+ };
8549
8544
  Channel.prototype.subscribe = function () {
8550
8545
  var _this = this;
8551
8546
  if (this.subscribed) {
@@ -8602,19 +8597,20 @@ var private_channel_extends = (undefined && undefined.__extends) || (function ()
8602
8597
  };
8603
8598
  })();
8604
8599
 
8605
-
8606
- var private_channel_PrivateChannel = (function (_super) {
8600
+ var PrivateChannel = (function (_super) {
8607
8601
  private_channel_extends(PrivateChannel, _super);
8608
8602
  function PrivateChannel() {
8609
8603
  return _super !== null && _super.apply(this, arguments) || this;
8610
8604
  }
8611
8605
  PrivateChannel.prototype.authorize = function (socketId, callback) {
8612
- var authorizer = factory.createAuthorizer(this, this.pusher.config);
8613
- return authorizer.authorize(socketId, callback);
8606
+ return this.pusher.config.channelAuthorizer({
8607
+ channelName: this.name,
8608
+ socketId: socketId
8609
+ }, callback);
8614
8610
  };
8615
8611
  return PrivateChannel;
8616
8612
  }(channels_channel));
8617
- /* harmony default export */ var private_channel = (private_channel_PrivateChannel);
8613
+ /* harmony default export */ var private_channel = (PrivateChannel);
8618
8614
 
8619
8615
  // CONCATENATED MODULE: ./src/core/channels/members.ts
8620
8616
 
@@ -8736,6 +8732,9 @@ var presence_channel_PresenceChannel = (function (_super) {
8736
8732
  case 'pusher_internal:subscription_succeeded':
8737
8733
  this.handleSubscriptionSucceededEvent(event);
8738
8734
  break;
8735
+ case 'pusher_internal:subscription_count':
8736
+ this.handleSubscriptionCountEvent(event);
8737
+ break;
8739
8738
  case 'pusher_internal:member_added':
8740
8739
  var addedMember = this.members.addMember(data);
8741
8740
  this.emit('pusher:member_added', addedMember);
@@ -9214,6 +9213,9 @@ function createChannel(name, pusher) {
9214
9213
  else if (name.indexOf('presence-') === 0) {
9215
9214
  return factory.createPresenceChannel(name, pusher);
9216
9215
  }
9216
+ else if (name.indexOf('#') === 0) {
9217
+ throw new BadChannelName('Cannot create a channel with name "' + name + '".');
9218
+ }
9217
9219
  else {
9218
9220
  return factory.createChannel(name, pusher);
9219
9221
  }
@@ -9229,7 +9231,6 @@ function createChannel(name, pusher) {
9229
9231
 
9230
9232
 
9231
9233
 
9232
-
9233
9234
  var Factory = {
9234
9235
  createChannels: function () {
9235
9236
  return new channels();
@@ -9252,12 +9253,6 @@ var Factory = {
9252
9253
  createTimelineSender: function (timeline, options) {
9253
9254
  return new timeline_sender(timeline, options);
9254
9255
  },
9255
- createAuthorizer: function (channel, options) {
9256
- if (options.authorizer) {
9257
- return options.authorizer(channel, options);
9258
- }
9259
- return new pusher_authorizer(channel, options);
9260
- },
9261
9256
  createHandshake: function (transport, callback) {
9262
9257
  return new connection_handshake(transport, callback);
9263
9258
  },
@@ -10166,17 +10161,24 @@ var NetInfo = (function (_super) {
10166
10161
 
10167
10162
  var net_info_Network = new NetInfo();
10168
10163
 
10164
+ // CONCATENATED MODULE: ./src/core/auth/options.ts
10165
+ var AuthRequestType;
10166
+ (function (AuthRequestType) {
10167
+ AuthRequestType["UserAuthentication"] = "user-authentication";
10168
+ AuthRequestType["ChannelAuthorization"] = "channel-authorization";
10169
+ })(AuthRequestType || (AuthRequestType = {}));
10170
+
10169
10171
  // CONCATENATED MODULE: ./src/runtimes/isomorphic/auth/xhr_auth.ts
10170
10172
 
10171
10173
 
10172
10174
 
10173
- var ajax = function (context, socketId, callback) {
10174
- var self = this, xhr;
10175
- xhr = node_runtime.createXHR();
10176
- xhr.open('POST', self.options.authEndpoint, true);
10175
+
10176
+ var ajax = function (context, query, authOptions, authRequestType, callback) {
10177
+ var xhr = node_runtime.createXHR();
10178
+ xhr.open('POST', authOptions.endpoint, true);
10177
10179
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
10178
- for (var headerName in this.authOptions.headers) {
10179
- xhr.setRequestHeader(headerName, this.authOptions.headers[headerName]);
10180
+ for (var headerName in authOptions.headers) {
10181
+ xhr.setRequestHeader(headerName, authOptions.headers[headerName]);
10180
10182
  }
10181
10183
  xhr.onreadystatechange = function () {
10182
10184
  if (xhr.readyState === 4) {
@@ -10188,22 +10190,28 @@ var ajax = function (context, socketId, callback) {
10188
10190
  parsed = true;
10189
10191
  }
10190
10192
  catch (e) {
10191
- callback(new HTTPAuthError(200, 'JSON returned from auth endpoint was invalid, yet status code was 200. Data was: ' +
10192
- xhr.responseText), { auth: '' });
10193
+ callback(new HTTPAuthError(200, "JSON returned from " + authRequestType.toString() + " endpoint was invalid, yet status code was 200. Data was: " + xhr.responseText), null);
10193
10194
  }
10194
10195
  if (parsed) {
10195
10196
  callback(null, data);
10196
10197
  }
10197
10198
  }
10198
10199
  else {
10199
- var suffix = url_store.buildLogSuffix('authenticationEndpoint');
10200
- callback(new HTTPAuthError(xhr.status, 'Unable to retrieve auth string from auth endpoint - ' +
10201
- ("received status: " + xhr.status + " from " + self.options.authEndpoint + ". ") +
10202
- ("Clients must be authenticated to join private or presence channels. " + suffix)), { auth: '' });
10200
+ var suffix = '';
10201
+ switch (authRequestType) {
10202
+ case AuthRequestType.UserAuthentication:
10203
+ suffix = url_store.buildLogSuffix('authenticationEndpoint');
10204
+ break;
10205
+ case AuthRequestType.ChannelAuthorization:
10206
+ suffix = "Clients must be authenticated to join private or presence channels. " + url_store.buildLogSuffix('authorizationEndpoint');
10207
+ break;
10208
+ }
10209
+ callback(new HTTPAuthError(xhr.status, "Unable to retrieve auth string from " + authRequestType.toString() + " endpoint - " +
10210
+ ("received status: " + xhr.status + " from " + authOptions.endpoint + ". " + suffix)), null);
10203
10211
  }
10204
10212
  }
10205
10213
  };
10206
- xhr.send(this.composeQuery(socketId));
10214
+ xhr.send(query);
10207
10215
  return xhr;
10208
10216
  };
10209
10217
  /* harmony default export */ var xhr_auth = (ajax);
@@ -10502,14 +10510,94 @@ var strategy_builder_UnsupportedStrategy = {
10502
10510
  }
10503
10511
  };
10504
10512
 
10513
+ // CONCATENATED MODULE: ./src/core/auth/user_authenticator.ts
10514
+
10515
+
10516
+ var composeChannelQuery = function (params, authOptions) {
10517
+ var query = 'socket_id=' + encodeURIComponent(params.socketId);
10518
+ for (var i in authOptions.params) {
10519
+ query +=
10520
+ '&' +
10521
+ encodeURIComponent(i) +
10522
+ '=' +
10523
+ encodeURIComponent(authOptions.params[i]);
10524
+ }
10525
+ return query;
10526
+ };
10527
+ var UserAuthenticator = function (authOptions) {
10528
+ if (typeof node_runtime.getAuthorizers()[authOptions.transport] === 'undefined') {
10529
+ throw "'" + authOptions.transport + "' is not a recognized auth transport";
10530
+ }
10531
+ return function (params, callback) {
10532
+ var query = composeChannelQuery(params, authOptions);
10533
+ node_runtime.getAuthorizers()[authOptions.transport](node_runtime, query, authOptions, AuthRequestType.UserAuthentication, callback);
10534
+ };
10535
+ };
10536
+ /* harmony default export */ var user_authenticator = (UserAuthenticator);
10537
+
10538
+ // CONCATENATED MODULE: ./src/core/auth/channel_authorizer.ts
10539
+
10540
+
10541
+ var channel_authorizer_composeChannelQuery = function (params, authOptions) {
10542
+ var query = 'socket_id=' + encodeURIComponent(params.socketId);
10543
+ query += '&channel_name=' + encodeURIComponent(params.channelName);
10544
+ for (var i in authOptions.params) {
10545
+ query +=
10546
+ '&' +
10547
+ encodeURIComponent(i) +
10548
+ '=' +
10549
+ encodeURIComponent(authOptions.params[i]);
10550
+ }
10551
+ return query;
10552
+ };
10553
+ var ChannelAuthorizer = function (authOptions) {
10554
+ if (typeof node_runtime.getAuthorizers()[authOptions.transport] === 'undefined') {
10555
+ throw "'" + authOptions.transport + "' is not a recognized auth transport";
10556
+ }
10557
+ return function (params, callback) {
10558
+ var query = channel_authorizer_composeChannelQuery(params, authOptions);
10559
+ node_runtime.getAuthorizers()[authOptions.transport](node_runtime, query, authOptions, AuthRequestType.ChannelAuthorization, callback);
10560
+ };
10561
+ };
10562
+ /* harmony default export */ var channel_authorizer = (ChannelAuthorizer);
10563
+
10564
+ // CONCATENATED MODULE: ./src/core/auth/deprecated_channel_authorizer.ts
10565
+ var ChannelAuthorizerProxy = function (pusher, authOptions, channelAuthorizerGenerator) {
10566
+ var deprecatedAuthorizerOptions = {
10567
+ authTransport: authOptions.transport,
10568
+ authEndpoint: authOptions.endpoint,
10569
+ auth: {
10570
+ params: authOptions.params,
10571
+ headers: authOptions.headers
10572
+ }
10573
+ };
10574
+ return function (params, callback) {
10575
+ var channel = pusher.channel(params.channelName);
10576
+ var channelAuthorizer = channelAuthorizerGenerator(channel, deprecatedAuthorizerOptions);
10577
+ channelAuthorizer.authorize(params.socketId, callback);
10578
+ };
10579
+ };
10580
+
10505
10581
  // CONCATENATED MODULE: ./src/core/config.ts
10582
+ var __assign = (undefined && undefined.__assign) || function () {
10583
+ __assign = Object.assign || function(t) {
10584
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
10585
+ s = arguments[i];
10586
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
10587
+ t[p] = s[p];
10588
+ }
10589
+ return t;
10590
+ };
10591
+ return __assign.apply(this, arguments);
10592
+ };
10593
+
10506
10594
 
10507
10595
 
10508
- function getConfig(opts) {
10596
+
10597
+
10598
+ function getConfig(opts, pusher) {
10509
10599
  var config = {
10510
10600
  activityTimeout: opts.activityTimeout || defaults.activityTimeout,
10511
- authEndpoint: opts.authEndpoint || defaults.authEndpoint,
10512
- authTransport: opts.authTransport || defaults.authTransport,
10513
10601
  cluster: opts.cluster || defaults.cluster,
10514
10602
  httpPath: opts.httpPath || defaults.httpPath,
10515
10603
  httpPort: opts.httpPort || defaults.httpPort,
@@ -10523,12 +10611,10 @@ function getConfig(opts) {
10523
10611
  enableStats: getEnableStatsConfig(opts),
10524
10612
  httpHost: getHttpHost(opts),
10525
10613
  useTLS: shouldUseTLS(opts),
10526
- wsHost: getWebsocketHost(opts)
10614
+ wsHost: getWebsocketHost(opts),
10615
+ userAuthenticator: buildUserAuthenticator(opts),
10616
+ channelAuthorizer: buildChannelAuthorizer(opts, pusher)
10527
10617
  };
10528
- if ('auth' in opts)
10529
- config.auth = opts.auth;
10530
- if ('authorizer' in opts)
10531
- config.authorizer = opts.authorizer;
10532
10618
  if ('disabledTransports' in opts)
10533
10619
  config.disabledTransports = opts.disabledTransports;
10534
10620
  if ('enabledTransports' in opts)
@@ -10581,6 +10667,167 @@ function getEnableStatsConfig(opts) {
10581
10667
  }
10582
10668
  return false;
10583
10669
  }
10670
+ function buildUserAuthenticator(opts) {
10671
+ var userAuthentication = __assign({}, defaults.userAuthentication, opts.userAuthentication);
10672
+ if ('customHandler' in userAuthentication &&
10673
+ userAuthentication['customHandler'] != null) {
10674
+ return userAuthentication['customHandler'];
10675
+ }
10676
+ return user_authenticator(userAuthentication);
10677
+ }
10678
+ function buildChannelAuth(opts, pusher) {
10679
+ var channelAuthorization;
10680
+ if ('channelAuthorization' in opts) {
10681
+ channelAuthorization = __assign({}, defaults.channelAuthorization, opts.channelAuthorization);
10682
+ }
10683
+ else {
10684
+ channelAuthorization = {
10685
+ transport: opts.authTransport || defaults.authTransport,
10686
+ endpoint: opts.authEndpoint || defaults.authEndpoint
10687
+ };
10688
+ if ('auth' in opts) {
10689
+ if ('params' in opts.auth)
10690
+ channelAuthorization.params = opts.auth.params;
10691
+ if ('headers' in opts.auth)
10692
+ channelAuthorization.headers = opts.auth.headers;
10693
+ }
10694
+ if ('authorizer' in opts)
10695
+ channelAuthorization.customHandler = ChannelAuthorizerProxy(pusher, channelAuthorization, opts.authorizer);
10696
+ }
10697
+ return channelAuthorization;
10698
+ }
10699
+ function buildChannelAuthorizer(opts, pusher) {
10700
+ var channelAuthorization = buildChannelAuth(opts, pusher);
10701
+ if ('customHandler' in channelAuthorization &&
10702
+ channelAuthorization['customHandler'] != null) {
10703
+ return channelAuthorization['customHandler'];
10704
+ }
10705
+ return channel_authorizer(channelAuthorization);
10706
+ }
10707
+
10708
+ // CONCATENATED MODULE: ./src/core/user.ts
10709
+ var user_extends = (undefined && undefined.__extends) || (function () {
10710
+ var extendStatics = function (d, b) {
10711
+ extendStatics = Object.setPrototypeOf ||
10712
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
10713
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
10714
+ return extendStatics(d, b);
10715
+ };
10716
+ return function (d, b) {
10717
+ extendStatics(d, b);
10718
+ function __() { this.constructor = d; }
10719
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10720
+ };
10721
+ })();
10722
+
10723
+
10724
+
10725
+ var user_UserFacade = (function (_super) {
10726
+ user_extends(UserFacade, _super);
10727
+ function UserFacade(pusher) {
10728
+ var _this = _super.call(this, function (eventName, data) {
10729
+ logger.debug('No callbacks on user for ' + eventName);
10730
+ }) || this;
10731
+ _this.signin_requested = false;
10732
+ _this.user_data = null;
10733
+ _this.serverToUserChannel = null;
10734
+ _this.pusher = pusher;
10735
+ _this.pusher.connection.bind('connected', function () {
10736
+ _this._signin();
10737
+ });
10738
+ _this.pusher.connection.bind('connecting', function () {
10739
+ _this._disconnect();
10740
+ });
10741
+ _this.pusher.connection.bind('disconnected', function () {
10742
+ _this._disconnect();
10743
+ });
10744
+ _this.pusher.connection.bind('message', function (event) {
10745
+ var eventName = event.event;
10746
+ if (eventName === 'pusher:signin_success') {
10747
+ _this._onSigninSuccess(event.data);
10748
+ }
10749
+ if (_this.serverToUserChannel &&
10750
+ _this.serverToUserChannel.name === event.channel) {
10751
+ _this.serverToUserChannel.handleEvent(event);
10752
+ }
10753
+ });
10754
+ return _this;
10755
+ }
10756
+ UserFacade.prototype.signin = function () {
10757
+ if (this.signin_requested) {
10758
+ return;
10759
+ }
10760
+ this.signin_requested = true;
10761
+ this._signin();
10762
+ };
10763
+ UserFacade.prototype._signin = function () {
10764
+ var _this = this;
10765
+ if (!this.signin_requested) {
10766
+ return;
10767
+ }
10768
+ if (this.pusher.connection.state !== 'connected') {
10769
+ return;
10770
+ }
10771
+ var onAuthorize = function (err, authData) {
10772
+ if (err) {
10773
+ logger.warn("Error during signin: " + err);
10774
+ return;
10775
+ }
10776
+ _this.pusher.send_event('pusher:signin', {
10777
+ auth: authData.auth,
10778
+ user_data: authData.user_data
10779
+ });
10780
+ };
10781
+ this.pusher.config.userAuthenticator({
10782
+ socketId: this.pusher.connection.socket_id
10783
+ }, onAuthorize);
10784
+ };
10785
+ UserFacade.prototype._onSigninSuccess = function (data) {
10786
+ try {
10787
+ this.user_data = JSON.parse(data.user_data);
10788
+ }
10789
+ catch (e) {
10790
+ logger.error("Failed parsing user data after signin: " + data.user_data);
10791
+ return;
10792
+ }
10793
+ if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
10794
+ logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
10795
+ return;
10796
+ }
10797
+ this._subscribeChannels();
10798
+ };
10799
+ UserFacade.prototype._subscribeChannels = function () {
10800
+ var _this = this;
10801
+ var ensure_subscribed = function (channel) {
10802
+ if (channel.subscriptionPending && channel.subscriptionCancelled) {
10803
+ channel.reinstateSubscription();
10804
+ }
10805
+ else if (!channel.subscriptionPending &&
10806
+ _this.pusher.connection.state === 'connected') {
10807
+ channel.subscribe();
10808
+ }
10809
+ };
10810
+ this.serverToUserChannel = new channels_channel("#server-to-user-" + this.user_data.id, this.pusher);
10811
+ this.serverToUserChannel.bind_global(function (eventName, data) {
10812
+ if (eventName.indexOf('pusher_internal:') === 0 ||
10813
+ eventName.indexOf('pusher:') === 0) {
10814
+ return;
10815
+ }
10816
+ _this.emit(eventName, data);
10817
+ });
10818
+ ensure_subscribed(this.serverToUserChannel);
10819
+ };
10820
+ UserFacade.prototype._disconnect = function () {
10821
+ this.user_data = null;
10822
+ if (this.serverToUserChannel) {
10823
+ this.serverToUserChannel.unbind_all();
10824
+ this.serverToUserChannel.disconnect();
10825
+ this.serverToUserChannel = null;
10826
+ }
10827
+ };
10828
+ return UserFacade;
10829
+ }(dispatcher));
10830
+ /* harmony default export */ var user = (user_UserFacade);
10584
10831
 
10585
10832
  // CONCATENATED MODULE: ./src/core/pusher.ts
10586
10833
 
@@ -10595,6 +10842,7 @@ function getEnableStatsConfig(opts) {
10595
10842
 
10596
10843
 
10597
10844
 
10845
+
10598
10846
  var pusher_Pusher = (function () {
10599
10847
  function Pusher(app_key, options) {
10600
10848
  var _this = this;
@@ -10608,7 +10856,7 @@ var pusher_Pusher = (function () {
10608
10856
  logger.warn('The disableStats option is deprecated in favor of enableStats');
10609
10857
  }
10610
10858
  this.key = app_key;
10611
- this.config = getConfig(options);
10859
+ this.config = getConfig(options, this);
10612
10860
  this.channels = factory.createChannels();
10613
10861
  this.global_emitter = new dispatcher();
10614
10862
  this.sessionID = Math.floor(Math.random() * 1000000000);
@@ -10667,6 +10915,7 @@ var pusher_Pusher = (function () {
10667
10915
  });
10668
10916
  Pusher.instances.push(this);
10669
10917
  this.timeline.info({ instances: Pusher.instances.length });
10918
+ this.user = new user(this);
10670
10919
  if (Pusher.isReady) {
10671
10920
  this.connect();
10672
10921
  }
@@ -10764,6 +11013,9 @@ var pusher_Pusher = (function () {
10764
11013
  Pusher.prototype.shouldUseTLS = function () {
10765
11014
  return this.config.useTLS;
10766
11015
  };
11016
+ Pusher.prototype.signin = function () {
11017
+ this.user.signin();
11018
+ };
10767
11019
  Pusher.instances = [];
10768
11020
  Pusher.isReady = false;
10769
11021
  Pusher.logToConsole = false;