centrifuge 2.7.1 → 2.7.5

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ 2.7.5
2
+ =====
3
+
4
+ * Fix regression of 2.7.4 - `Invalid end of JSON input error`
5
+
6
+ 2.7.4
7
+ =====
8
+
9
+ * Optimize & simplify json encode/decode, see [#138](https://github.com/centrifugal/centrifuge-js/pull/138)
10
+
11
+ 2.7.3
12
+ =====
13
+
14
+ * `SubscribeSuccessContext` can contain `data` field if custom data returned from a server in a subscribe result.
15
+
16
+ 2.7.2
17
+ =====
18
+
19
+ * Handle server-side SUB push in Protobuf case (previously ignored). Sub push is a message that contains information about server-side subscription made after connection already established.
20
+
1
21
  2.7.1
2
22
  =====
3
23
 
package/README.md CHANGED
@@ -865,7 +865,7 @@ If you don't want to give client access to channel then just do not include it i
865
865
 
866
866
  There are also two public API methods which can help to subscribe to many private channels sending only one POST request to your web application backend: `startSubscribeBatching` and `stopSubscribeBatching`. When you `startSubscribeBatching` javascript client will collect private subscriptions until `stopSubscribeBatching()` called – and then send them all at once.
867
867
 
868
- As we just described when client subscribes on private channel by default AJAX request will be sent to `subscribeEndpoint` automatically if channel starts with `$`. In this case developer only needs to return proper response from server. But there is a way to override default behaviour and take full control on authorizing private channels. To do this it's possible to provide custom `onPrivateSubscribe` function in configuration options. This function will be called with all data required to authorize private channels client subscribes to and should call callback (will be provided by centrifuge-js as second argument) with authorization data when done. See our type declarations in `dist` folder to find out data format.
868
+ As we just described when client subscribes on private channel by default AJAX request will be sent to `subscribeEndpoint` automatically if channel starts with `$`. In this case developer only needs to return proper response from server. But there is a way to override default behaviour and take full control on authorizing private channels. To do this it's possible to provide custom `onPrivateSubscribe` function in configuration options. This function will be called with all data required to authorize private channels client subscribes to and should call callback (will be provided by centrifuge-js as second argument) with authorization data when done. See our type declarations in `dist` folder to find out data format (**for `onPrivateSubscribe` it is slightly different** - like `{"status": 200, "data": {"channels": [...]}}`).
869
869
 
870
870
  ## Server-side subscriptions
871
871
 
@@ -984,12 +984,37 @@ var centrifuge = new Centrifuge('ws://localhost:8000/connection/sockjs', {
984
984
  })
985
985
  ```
986
986
 
987
+ ### Custom WebSocket constructor
988
+
989
+ If you are building a client for a non-browser environment and want to pass custom headers then you can use the following approach to wrap a WebSocket constructor and let custom options to be used on connection initialization:
990
+
991
+ ```javascript
992
+ var Centrifuge = require("centrifuge");
993
+ const WebSocket = require('ws');
994
+
995
+ const myWs = function (options) {
996
+ return class wsClass extends WebSocket {
997
+ constructor(...args) {
998
+ super(...[...args, ...[options]])
999
+ }
1000
+ }
1001
+ }
1002
+ ```
1003
+
1004
+ It should be now possible to use pass your custom WebSocket constructor to `centrifuge-js` and so custom headers will be used when connecting to a server:
1005
+
1006
+ ```javascript
1007
+ var centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket', {
1008
+ websocket: myWs({ headers: { Authorization: '<token or key>' } }),
1009
+ });
1010
+ ```
1011
+
987
1012
  ## Feature matrix
988
1013
 
989
1014
  - [x] connect to server using JSON protocol format
990
1015
  - [x] connect to server using Protobuf protocol format
991
1016
  - [x] connect with token (JWT)
992
- - [ ] connect with custom header (not supported by browser API)
1017
+ - [ ] connect with custom header (not supported by browser API, though [possible for a non-browser target env](https://github.com/centrifugal/centrifuge-js#custom-websocket-constructor))
993
1018
  - [x] automatic reconnect in case of errors, network problems etc
994
1019
  - [x] an exponential backoff for reconnect
995
1020
  - [x] connect and disconnect events
@@ -131,6 +131,7 @@ declare namespace Centrifuge {
131
131
  isResubscribe: boolean;
132
132
  recovered: boolean;
133
133
  streamPosition?: StreamPosition;
134
+ data?: any;
134
135
  }
135
136
 
136
137
  export interface SubscribeErrorContext {
@@ -1706,6 +1706,11 @@ var Centrifuge = /*#__PURE__*/function (_EventEmitter) {
1706
1706
  }
1707
1707
 
1708
1708
  ;
1709
+
1710
+ if (result.data) {
1711
+ ctx.data = result.data;
1712
+ }
1713
+
1709
1714
  return ctx;
1710
1715
  }
1711
1716
  }, {
@@ -2422,15 +2427,9 @@ var JsonEncoder = /*#__PURE__*/function () {
2422
2427
  _createClass(JsonEncoder, [{
2423
2428
  key: "encodeCommands",
2424
2429
  value: function encodeCommands(commands) {
2425
- var encodedCommands = [];
2426
-
2427
- for (var i in commands) {
2428
- if (commands.hasOwnProperty(i)) {
2429
- encodedCommands.push(JSON.stringify(commands[i]));
2430
- }
2431
- }
2432
-
2433
- return encodedCommands.join('\n');
2430
+ return commands.map(function (c) {
2431
+ return JSON.stringify(c);
2432
+ }).join('\n');
2434
2433
  }
2435
2434
  }]);
2436
2435
 
@@ -2447,21 +2446,11 @@ var JsonDecoder = /*#__PURE__*/function () {
2447
2446
  _createClass(JsonDecoder, [{
2448
2447
  key: "decodeReplies",
2449
2448
  value: function decodeReplies(data) {
2450
- var replies = [];
2451
- var encodedReplies = data.split('\n');
2452
-
2453
- for (var i in encodedReplies) {
2454
- if (encodedReplies.hasOwnProperty(i)) {
2455
- if (!encodedReplies[i]) {
2456
- continue;
2457
- }
2458
-
2459
- var reply = JSON.parse(encodedReplies[i]);
2460
- replies.push(reply);
2461
- }
2462
- }
2463
-
2464
- return replies;
2449
+ return data.split('\n').filter(function (r) {
2450
+ return r !== '';
2451
+ }).map(function (r) {
2452
+ return JSON.parse(r);
2453
+ });
2465
2454
  }
2466
2455
  }, {
2467
2456
  key: "decodeCommandResult",