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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 7.2.0
4
+
5
+ * [ADDED] Add support for subscription_count event
6
+
7
+ ## 7.1.1-beta
8
+
9
+ [FIXED] Exported Typescript types in index.d.ts
10
+
11
+ ## 7.1.0-beta
12
+
13
+ [ADDED] Support for authenticating users with the `signin` method
14
+
15
+ [ADDED] Support for binding to events sent to a specific authenticated user
16
+
17
+ [UPDATED] The initialization of the `Pusher` object has been changed. Two new parameters were introduced: `userAuthentication` and `channelAuthorization`.
18
+
19
+ [DEPRECATED] The Pusher object parameters `auth`, `authEndpoint`, and `authTransport` are still supported, but deprecated. They have been replaced with the `channelAuthorization` parameter.
20
+
3
21
  ## 7.0.6
4
22
 
5
23
  * [FIXED] pusher-js/worker can now be bundled and used in a web worker context
package/README.md CHANGED
@@ -225,7 +225,9 @@ There are a number of configuration parameters which can be set for the client,
225
225
  ```js
226
226
  const pusher = new Pusher(APP_KEY, {
227
227
  cluster: APP_CLUSTER,
228
- authEndpoint: 'http://example.com/pusher/auth'
228
+ channelAuthorization: {
229
+ endpoint: 'http://example.com/pusher/auth'
230
+ },
229
231
  });
230
232
  ```
231
233
 
@@ -235,107 +237,45 @@ For most users, there is little need to change these. See [client API guide](htt
235
237
 
236
238
  Forces the connection to use TLS. When set to `false` the library will attempt non-TLS connections first. Defaults to `true`.
237
239
 
238
- #### `authEndpoint` (String)
240
+ ### `userAuthentication` (Object)
239
241
 
240
- Endpoint on your server that will return the authentication signature needed for private and presence channels. Defaults to `'/pusher/auth'`.
242
+ Object containing the configuration for user authentication. Valid keys are:
241
243
 
242
- For more information see [authenticating users](https://pusher.com/docs/authenticating_users).
244
+ * `endpoint` (String) - Endpoint on your server that will return the authentication signature needed for signing the user in. Defaults to `/pusher/user-auth`.
243
245
 
244
- #### `authTransport` (String)
246
+ * `transport` (String) - Defines how the authentication endpoint will be called. There are two options available:
247
+ * `ajax` - the **default** option where an `XMLHttpRequest` object will be used to make a request. The parameters will be passed as `POST` parameters.
248
+ * `jsonp` - The authentication endpoint will be called by a `<script>` tag being dynamically created pointing to the endpoint defined by `userAuthentication.endpoint`. This can be used when the authentication endpoint is on a different domain to the web application. The endpoint will therefore be requested as a `GET` and parameters passed in the query string.
245
249
 
246
- Defines how the authentication endpoint, defined using authEndpoint, will be called. There are two options available: `ajax` and `jsonp`.
250
+ * `params` (Object) - Additional parameters to be sent when the user authentication endpoint is called. When using ajax authentication the parameters are passed as additional POST parameters. When using jsonp authentication the parameters are passed as GET parameters. This can be useful with web application frameworks that guard against CSRF (Cross-site request forgery).
247
251
 
248
- * `ajax` - The **default** option where an `XMLHttpRequest` object will be used to make a request. The parameters will be passed as `POST` parameters.
249
- * `jsonp` - The authentication endpoint will be called by a `<script>` tag being dynamically created pointing to the endpoint defined by `authEndpoint`. This can be used when the authentication endpoint is on a different domain to the web application. The endpoint will therefore be requested as a `GET` and parameters passed in the query string.
252
+ * `headers` (Object) - Only applied when using `ajax` as authentication transport. Provides the ability to pass additional HTTP Headers to the user authentication endpoint. This can be useful with some web application frameworks that guard against CSRF CSRF (Cross-site request forgery).
250
253
 
251
- For more information see the [Channel authentication transport section of our authenticating users docs](http://pusher.com/docs/authenticating_users#authTransport).
254
+ * `customHandler` (Function) - When present, this function is called instead of a request being made to the endpoint specified by `userAuthentication.endpoint`.
252
255
 
253
- #### `auth` (Hash)
254
256
 
255
- Allows passing additional data to authorizers. Supports query string params and headers (AJAX only). For example, following will pass `foo=bar` via the query string and `baz: boo` via headers:
257
+ For more information see [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users/).
256
258
 
257
- ```js
258
- const pusher = new Pusher(APP_KEY, {
259
- cluster: APP_CLUSTER,
260
- auth: {
261
- params: { foo: 'bar' },
262
- headers: { baz: 'boo' }
263
- }
264
- });
265
- ```
266
259
 
267
- Additional parameters to be sent when the channel authentication endpoint is called. When using [ajax authentication](https://pusher.com/docs/authenticating_users#ajax_authentication) the parameters are passed as additional `POST` parameters. When using [jsonp authentication](http://pusher.com/docs/authenticating_users#jsonp_authentication) the parameters are passed as `GET` parameters. This can be useful with web application frameworks that guard against [CSRF (Cross-site request forgery)](http://en.wikipedia.org/wiki/Cross-site_request_forgery).
260
+ ### `channelAuthorization` (Object)
268
261
 
269
- ##### CSRF
262
+ Object containing the configuration for user authorization. Valid keys are:
270
263
 
271
- If you require a CSRF header for incoming requests to the private channel authentication endpoint on your server, you should add a CSRF token to the `auth` hash under `headers`. This is applicable to frameworks which apply CSRF protection by default.
264
+ * `endpoint` (String) - Endpoint on your server that will return the authorization signature needed for private and presence channels. Defaults to `/pusher/user-auth`.
272
265
 
273
- ```js
274
- const pusher = new Pusher(APP_KEY, {
275
- cluster: APP_CLUSTER,
276
- auth: {
277
- params: { foo: 'bar' },
278
- headers: { 'X-CSRF-Token': 'SOME_CSRF_TOKEN' }
279
- }
280
- });
281
- ```
266
+ * `transport` (String) - Defines how the authorization endpoint will be called. There are two options available:
267
+ * `ajax` - the **default** option where an `XMLHttpRequest` object will be used to make a request. The parameters will be passed as `POST` parameters.
268
+ * `jsonp` - The authorization endpoint will be called by a `<script>` tag being dynamically created pointing to the endpoint defined by `channelAuthorization.endpoint`. This can be used when the authorization endpoint is on a different domain to the web application. The endpoint will therefore be requested as a `GET` and parameters passed in the query string.
282
269
 
283
- #### `authorizer` (Function)
270
+ * `params` (Object) - Additional parameters to be sent when the channel authorization endpoint is called. When using ajax authorization the parameters are passed as additional POST parameters. When using jsonp authorization the parameters are passed as GET parameters. This can be useful with web application frameworks that guard against CSRF (Cross-site request forgery).
284
271
 
285
- If you need custom authorization behavior you can provide your own `authorizer` function as follows:
272
+ * `headers` (Object) - Only applied when using `ajax` as authorizing transport. Provides the ability to pass additional HTTP Headers to the user authorization endpoint. This can be useful with some web application frameworks that guard against CSRF CSRF (Cross-site request forgery).
286
273
 
287
- ```js
288
- const pusher = new Pusher(APP_KEY, {
289
- cluster: APP_CLUSTER,
290
- authorizer: function (channel, options) {
291
- return {
292
- authorize: function (socketId, callback) {
293
- // Do some ajax to get the auth string
294
- callback(null, {auth: authString});
295
- }
296
- };
297
- }
298
- })
299
- ```
274
+ * `customHandler` (Function) - When present, this function is called instead of a request being made to the endpoint specified by `channelAuthorization.endpoint`.
300
275
 
301
- Example: An authorizer which uses the [`fetch`
302
- API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to make a JSON
303
- request to an auth endpoint
304
276
 
305
- ```js
306
- let authorizer = (channel, options) => {
307
- return {
308
- authorize: (socketId, callback) => {
309
- fetch(authUrl, {
310
- method: "POST",
311
- headers: new Headers({ "Content-Type": "application/json" }),
312
- body: JSON.stringify({
313
- socket_id: socketId,
314
- channel_name: channel.name
315
- })
316
- })
317
- .then(res => {
318
- if (!res.ok) {
319
- throw new Error(`Received ${res.statusCode} from ${authUrl}`);
320
- }
321
- return res.json();
322
- })
323
- .then(data => {
324
- callback(null, data);
325
- })
326
- .catch(err => {
327
- callback(new Error(`Error calling auth endpoint: ${err}`), {
328
- auth: ""
329
- });
330
- });
331
- }
332
- };
333
- };
334
- const pusher = new Pusher(APP_KEY, {
335
- cluster: APP_CLUSTER,
336
- authorizer: authorizer,
337
- })
338
- ```
277
+ For more information see [authorizing users](https://pusher.com/docs/channels/server_api/authorizing-users).
278
+
339
279
 
340
280
  #### `cluster` (String)
341
281
 
@@ -525,7 +465,7 @@ It is possible to access all subscribed channels through the `allChannels` funct
525
465
  pusher.allChannels().forEach(channel => console.log(channel.name));
526
466
  ```
527
467
 
528
- Private, presence and encrypted channels will make a request to your `authEndpoint` (`/pusher/auth`) by default, where you will have to [authenticate the subscription](https://pusher.com/docs/authenticating_users). You will have to send back the correct auth response and a 200 status code.
468
+ Private, presence and encrypted channels will make a request to your `channelAuthorization.endpoint` (`/pusher/auth`) by default, where you will have to [authorize the subscription](https://pusher.com/docs/authorizing_users). You will have to send back the correct authorization response and a 200 status code.
529
469
 
530
470
  ## Unsubscribing from channels
531
471
 
@@ -626,9 +566,9 @@ channel.trigger('client-my-event', {message: 'Hello, world!'})
626
566
  ```
627
567
 
628
568
 
629
- ## Batching auth requests (aka multi-auth)
569
+ ## Batching authorization requests (aka multi-authorization)
630
570
 
631
- Currently, pusher-js itself does not support authenticating multiple channels in one HTTP request. However, thanks to @dirkbonhomme you can use the [pusher-js-auth](https://github.com/dirkbonhomme/pusher-js-auth) plugin that buffers subscription requests and sends auth requests to your endpoint in batches.
571
+ Currently, pusher-js itself does not support authorizing multiple channels in one HTTP request. However, thanks to @dirkbonhomme you can use the [pusher-js-auth](https://github.com/dirkbonhomme/pusher-js-auth) plugin that buffers subscription requests and sends authorization requests to your endpoint in batches.
632
572
 
633
573
  ## Default events
634
574