pusher-js 7.3.0 → 7.4.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 (44) hide show
  1. package/.github/workflows/release.yml +1 -1
  2. package/.github/workflows/release_pr.yml +1 -1
  3. package/.github/workflows/run-tests.yml +2 -2
  4. package/.gitmodules +1 -1
  5. package/CHANGELOG.md +4 -0
  6. package/README.md +10 -30
  7. package/dist/node/pusher.js +127 -37
  8. package/dist/node/pusher.js.map +1 -1
  9. package/dist/react-native/pusher.js +2 -2
  10. package/dist/react-native/pusher.js.map +1 -1
  11. package/dist/web/pusher-with-encryption.js +127 -37
  12. package/dist/web/pusher-with-encryption.js.map +1 -1
  13. package/dist/web/pusher-with-encryption.min.js +2 -2
  14. package/dist/web/pusher-with-encryption.min.js.map +1 -1
  15. package/dist/web/pusher.js +127 -37
  16. package/dist/web/pusher.js.map +1 -1
  17. package/dist/web/pusher.min.js +2 -2
  18. package/dist/web/pusher.min.js.map +1 -1
  19. package/dist/worker/pusher-with-encryption.worker.js +127 -37
  20. package/dist/worker/pusher-with-encryption.worker.js.map +1 -1
  21. package/dist/worker/pusher-with-encryption.worker.min.js +2 -2
  22. package/dist/worker/pusher-with-encryption.worker.min.js.map +1 -1
  23. package/dist/worker/pusher.worker.js +127 -37
  24. package/dist/worker/pusher.worker.js.map +1 -1
  25. package/dist/worker/pusher.worker.min.js +2 -2
  26. package/dist/worker/pusher.worker.min.js.map +1 -1
  27. package/package.json +5 -4
  28. package/spec/javascripts/helpers/timers/promises.js +9 -0
  29. package/spec/javascripts/unit/core/channels/presence_channel_spec.js +85 -16
  30. package/spec/javascripts/unit/core/user_spec.js +13 -6
  31. package/src/core/channels/presence_channel.ts +21 -12
  32. package/src/core/http/http_socket.ts +3 -1
  33. package/src/core/pusher.ts +1 -1
  34. package/src/core/user.ts +65 -27
  35. package/src/core/utils/flat_promise.ts +10 -0
  36. package/src/runtimes/interface.ts +1 -0
  37. package/src/runtimes/node/runtime.ts +5 -0
  38. package/src/runtimes/react-native/runtime.ts +5 -0
  39. package/src/runtimes/web/runtime.ts +14 -0
  40. package/src/runtimes/worker/runtime.ts +14 -0
  41. package/types/src/core/strategies/transport_strategy.d.ts +3 -0
  42. package/types/src/core/user.d.ts +5 -1
  43. package/types/src/core/utils/flat_promise.d.ts +6 -0
  44. package/types/src/runtimes/interface.d.ts +1 -0
@@ -71,7 +71,7 @@ jobs:
71
71
  - uses: actions/setup-node@v2
72
72
  if: ${{ steps.is-published.outputs.published == 'false' }}
73
73
  with:
74
- node-version: '14'
74
+ node-version: '16'
75
75
  registry-url: https://registry.npmjs.org/
76
76
  - run: npm install
77
77
  if: ${{ steps.is-published.outputs.published == 'false' }}
@@ -28,7 +28,7 @@ jobs:
28
28
  current_version: ${{ env.CURRENT_VERSION }}
29
29
  - uses: actions/setup-node@v2
30
30
  with:
31
- node-version: '14'
31
+ node-version: '16'
32
32
  - run: npm install
33
33
  - name: Push
34
34
  shell: bash
@@ -16,7 +16,7 @@ jobs:
16
16
  - uses: actions/checkout@v2
17
17
  - uses: actions/setup-node@v2
18
18
  with:
19
- node-version: '14'
19
+ node-version: '16'
20
20
  - run: npm install
21
21
  - run: npm run check-format
22
22
  - run: make web_unit
@@ -30,7 +30,7 @@ jobs:
30
30
  - uses: actions/checkout@v2
31
31
  - uses: actions/setup-node@v2
32
32
  with:
33
- node-version: '14'
33
+ node-version: '16'
34
34
  - run: npm install
35
35
  - run: npm run check-format
36
36
  - run: make node_unit
package/.gitmodules CHANGED
@@ -1,3 +1,3 @@
1
1
  [submodule "sockjs-client"]
2
2
  path = src/runtimes/web/dom/sockjs
3
- url = git://github.com/pusher/sockjs-client.git
3
+ url = https://github.com/pusher/sockjs-client.git
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 7.4.0
4
+
5
+ * [FIXED] Allow presence channel authorization to depend on user authentication
6
+
3
7
  ## 7.3.0
4
8
 
5
9
  * [FIXED] Restore previously exported types
package/README.md CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  ![test badge](https://github.com/pusher/pusher-js/workflows/test/badge.svg)
4
4
 
5
- This Pusher Channels client library supports web browsers, web workers, Node.js
6
- and React Native.
5
+ This Pusher Channels client library supports web browsers, web workers and Node.js
7
6
 
8
7
  If you're looking for the Pusher Channels server library for Node.js, use
9
8
  [pusher-http-node](https://github.com/pusher/pusher-http-node) instead.
@@ -145,29 +144,11 @@ var presenceChannel: PusherTypes.PresenceChannel;
145
144
 
146
145
  ### React Native
147
146
 
148
- **Warning it's now necessary to install
149
- [@react-native-community/netinfo](https://github.com/react-native-community/react-native-netinfo)
150
- in order to use pusher-js with react-native.** pusher-js depends on NetInfo.
151
- NetInfo. NetInfo was included within react-native core until v0.60, when it was
152
- moved to the
153
- [@react-native-community/netinfo](https://github.com/react-native-community/react-native-netinfo)
154
- library. Please follow the [install
155
- instructions](https://github.com/react-native-community/react-native-netinfo#getting-started)
156
- for the
157
- [@react-native-community/netinfo](https://github.com/react-native-community/react-native-netinfo)
158
- library before trying to use pusher-js in your react-native project.
159
-
160
- Use a package manager like Yarn or NPM to install `pusher-js` and then import
161
- it as follows:
162
-
163
- ```javascript
164
- import Pusher from 'pusher-js/react-native';
165
- ```
166
-
167
- Notes:
168
-
169
- * The fallbacks available for this runtime are HTTP streaming and polling.
170
- * This build uses React Native's NetInfo API to detect changes on connectivity state. It will use this to automatically reconnect.
147
+ > **⚠️ Important notice**
148
+ >
149
+ > React Native support has been **deprecated** and soon will be removed from this repository.
150
+ >
151
+ > Please, use our official [React Native SDK](https://github.com/pusher/pusher-websocket-react-native) instead.
171
152
 
172
153
  ### Web Workers
173
154
  (`pusher-js`'s Web Workers implementation is currently not compatible with Internet Explorer)
@@ -254,7 +235,7 @@ Object containing the configuration for user authentication. Valid keys are:
254
235
  * `customHandler` (Function) - When present, this function is called instead of a request being made to the endpoint specified by `userAuthentication.endpoint`.
255
236
 
256
237
 
257
- For more information see [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users/).
238
+ For more information see [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users/).
258
239
 
259
240
 
260
241
  ### `channelAuthorization` (Object)
@@ -274,7 +255,7 @@ Object containing the configuration for user authorization. Valid keys are:
274
255
  * `customHandler` (Function) - When present, this function is called instead of a request being made to the endpoint specified by `channelAuthorization.endpoint`.
275
256
 
276
257
 
277
- For more information see [authorizing users](https://pusher.com/docs/channels/server_api/authorizing-users).
258
+ For more information see [authorizing users](https://pusher.com/docs/channels/server_api/authorizing-users).
278
259
 
279
260
 
280
261
  #### `cluster` (String)
@@ -643,7 +624,7 @@ This command will serve `pusher.js`, `sockjs.js`, `json2.js`, and their respecti
643
624
 
644
625
  ### Core Vs. Platform-Specific Code
645
626
 
646
- New to pusher-js 3.1 is the ability for the library to produce builds for different runtimes: classic web, React Native, NodeJS and
627
+ New to pusher-js 3.1 is the ability for the library to produce builds for different runtimes: classic web, NodeJS and
647
628
  Web Workers.
648
629
 
649
630
  In order for this to happen, we have split the library into two directories: `core/` and `runtimes/`. In `core` we keep anything that is platform-independent. In `runtimes` we keep code that depends on certain runtimes.
@@ -682,7 +663,6 @@ In order to specify the library version, you can either update `package.json` or
682
663
  Other build commands include:
683
664
 
684
665
  ```bash
685
- make react-native # for the React Native build
686
666
  make node # for the NodeJS build
687
667
  make worker # for the worker build
688
668
  ```
@@ -696,7 +676,7 @@ Each test environment contains two types of tests:
696
676
 
697
677
  Unit tests are simple, fast and don't need any external dependencies. Integration tests usually connect to production and js-integration-api servers and can use a local server for loading JS files, so they need an Internet connection to work.
698
678
 
699
- There are 3 different testing environments: one for web, one for NodeJS and one for workers. We may consider adding another one for React Native in the future.
679
+ There are 3 different testing environments: one for web, one for NodeJS and one for workers.
700
680
 
701
681
  The web and worker tests use [Karma](https://github.com/karma-runner/karma) to execute specs in real browsers. The NodeJS tests use [jasmine-node](https://github.com/mhevery/jasmine-node).
702
682
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Pusher JavaScript Library v7.3.0
2
+ * Pusher JavaScript Library v7.4.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.3.0",
7490
+ VERSION: "7.4.0",
7491
7491
  PROTOCOL: 7,
7492
7492
  wsPort: 80,
7493
7493
  wssPort: 443,
@@ -8682,6 +8682,41 @@ var presence_channel_extends = (undefined && undefined.__extends) || (function (
8682
8682
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
8683
8683
  };
8684
8684
  })();
8685
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
8686
+ return new (P || (P = Promise))(function (resolve, reject) {
8687
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8688
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8689
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8690
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8691
+ });
8692
+ };
8693
+ var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
8694
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
8695
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
8696
+ function verb(n) { return function (v) { return step([n, v]); }; }
8697
+ function step(op) {
8698
+ if (f) throw new TypeError("Generator is already executing.");
8699
+ while (_) try {
8700
+ 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;
8701
+ if (y = 0, t) op = [op[0] & 2, t.value];
8702
+ switch (op[0]) {
8703
+ case 0: case 1: t = op; break;
8704
+ case 4: _.label++; return { value: op[1], done: false };
8705
+ case 5: _.label++; y = op[1]; op = [0]; continue;
8706
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
8707
+ default:
8708
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
8709
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
8710
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
8711
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
8712
+ if (t[2]) _.ops.pop();
8713
+ _.trys.pop(); continue;
8714
+ }
8715
+ op = body.call(thisArg, _);
8716
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
8717
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
8718
+ }
8719
+ };
8685
8720
 
8686
8721
 
8687
8722
 
@@ -8695,21 +8730,38 @@ var presence_channel_PresenceChannel = (function (_super) {
8695
8730
  }
8696
8731
  PresenceChannel.prototype.authorize = function (socketId, callback) {
8697
8732
  var _this = this;
8698
- _super.prototype.authorize.call(this, socketId, function (error, authData) {
8699
- if (!error) {
8700
- authData = authData;
8701
- if (authData.channel_data === undefined) {
8702
- var suffix = url_store.buildLogSuffix('authenticationEndpoint');
8703
- logger.error("Invalid auth response for channel '" + _this.name + "'," +
8704
- ("expected 'channel_data' field. " + suffix));
8705
- callback('Invalid auth response');
8706
- return;
8733
+ _super.prototype.authorize.call(this, socketId, function (error, authData) { return __awaiter(_this, void 0, void 0, function () {
8734
+ var channelData, suffix;
8735
+ return __generator(this, function (_a) {
8736
+ switch (_a.label) {
8737
+ case 0:
8738
+ if (!!error) return [3, 3];
8739
+ authData = authData;
8740
+ if (!(authData.channel_data != null)) return [3, 1];
8741
+ channelData = JSON.parse(authData.channel_data);
8742
+ this.members.setMyID(channelData.user_id);
8743
+ return [3, 3];
8744
+ case 1: return [4, this.pusher.user.signinDonePromise];
8745
+ case 2:
8746
+ _a.sent();
8747
+ if (this.pusher.user.user_data != null) {
8748
+ this.members.setMyID(this.pusher.user.user_data.id);
8749
+ }
8750
+ else {
8751
+ suffix = url_store.buildLogSuffix('authorizationEndpoint');
8752
+ logger.error("Invalid auth response for channel '" + this.name + "', " +
8753
+ ("expected 'channel_data' field. " + suffix + ", ") +
8754
+ "or the user should be signed in.");
8755
+ callback('Invalid auth response');
8756
+ return [2];
8757
+ }
8758
+ _a.label = 3;
8759
+ case 3:
8760
+ callback(error, authData);
8761
+ return [2];
8707
8762
  }
8708
- var channelData = JSON.parse(authData.channel_data);
8709
- _this.members.setMyID(channelData.user_id);
8710
- }
8711
- callback(error, authData);
8712
- });
8763
+ });
8764
+ }); });
8713
8765
  };
8714
8766
  PresenceChannel.prototype.handleEvent = function (event) {
8715
8767
  var eventName = event.event;
@@ -10705,6 +10757,17 @@ function buildChannelAuthorizer(opts, pusher) {
10705
10757
  return channel_authorizer(channelAuthorization);
10706
10758
  }
10707
10759
 
10760
+ // CONCATENATED MODULE: ./src/core/utils/flat_promise.ts
10761
+ function flatPromise() {
10762
+ var resolve, reject;
10763
+ var promise = new Promise(function (res, rej) {
10764
+ resolve = res;
10765
+ reject = rej;
10766
+ });
10767
+ return { promise: promise, resolve: resolve, reject: reject };
10768
+ }
10769
+ /* harmony default export */ var flat_promise = (flatPromise);
10770
+
10708
10771
  // CONCATENATED MODULE: ./src/core/user.ts
10709
10772
  var user_extends = (undefined && undefined.__extends) || (function () {
10710
10773
  var extendStatics = function (d, b) {
@@ -10722,6 +10785,7 @@ var user_extends = (undefined && undefined.__extends) || (function () {
10722
10785
 
10723
10786
 
10724
10787
 
10788
+
10725
10789
  var user_UserFacade = (function (_super) {
10726
10790
  user_extends(UserFacade, _super);
10727
10791
  function UserFacade(pusher) {
@@ -10731,15 +10795,29 @@ var user_UserFacade = (function (_super) {
10731
10795
  _this.signin_requested = false;
10732
10796
  _this.user_data = null;
10733
10797
  _this.serverToUserChannel = null;
10798
+ _this.signinDonePromise = null;
10799
+ _this._signinDoneResolve = null;
10800
+ _this._onAuthorize = function (err, authData) {
10801
+ if (err) {
10802
+ logger.warn("Error during signin: " + err);
10803
+ _this._cleanup();
10804
+ return;
10805
+ }
10806
+ _this.pusher.send_event('pusher:signin', {
10807
+ auth: authData.auth,
10808
+ user_data: authData.user_data
10809
+ });
10810
+ };
10734
10811
  _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();
10812
+ _this.pusher.connection.bind('state_change', function (_a) {
10813
+ var previous = _a.previous, current = _a.current;
10814
+ if (previous !== 'connected' && current === 'connected') {
10815
+ _this._signin();
10816
+ }
10817
+ if (previous === 'connected' && current !== 'connected') {
10818
+ _this._cleanup();
10819
+ _this._newSigninPromiseIfNeeded();
10820
+ }
10743
10821
  });
10744
10822
  _this.pusher.connection.bind('message', function (event) {
10745
10823
  var eventName = event.event;
@@ -10761,26 +10839,16 @@ var user_UserFacade = (function (_super) {
10761
10839
  this._signin();
10762
10840
  };
10763
10841
  UserFacade.prototype._signin = function () {
10764
- var _this = this;
10765
10842
  if (!this.signin_requested) {
10766
10843
  return;
10767
10844
  }
10845
+ this._newSigninPromiseIfNeeded();
10768
10846
  if (this.pusher.connection.state !== 'connected') {
10769
10847
  return;
10770
10848
  }
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
10849
  this.pusher.config.userAuthenticator({
10782
10850
  socketId: this.pusher.connection.socket_id
10783
- }, onAuthorize);
10851
+ }, this._onAuthorize);
10784
10852
  };
10785
10853
  UserFacade.prototype._onSigninSuccess = function (data) {
10786
10854
  try {
@@ -10788,12 +10856,15 @@ var user_UserFacade = (function (_super) {
10788
10856
  }
10789
10857
  catch (e) {
10790
10858
  logger.error("Failed parsing user data after signin: " + data.user_data);
10859
+ this._cleanup();
10791
10860
  return;
10792
10861
  }
10793
10862
  if (typeof this.user_data.id !== 'string' || this.user_data.id === '') {
10794
10863
  logger.error("user_data doesn't contain an id. user_data: " + this.user_data);
10864
+ this._cleanup();
10795
10865
  return;
10796
10866
  }
10867
+ this._signinDoneResolve();
10797
10868
  this._subscribeChannels();
10798
10869
  };
10799
10870
  UserFacade.prototype._subscribeChannels = function () {
@@ -10817,13 +10888,32 @@ var user_UserFacade = (function (_super) {
10817
10888
  });
10818
10889
  ensure_subscribed(this.serverToUserChannel);
10819
10890
  };
10820
- UserFacade.prototype._disconnect = function () {
10891
+ UserFacade.prototype._cleanup = function () {
10821
10892
  this.user_data = null;
10822
10893
  if (this.serverToUserChannel) {
10823
10894
  this.serverToUserChannel.unbind_all();
10824
10895
  this.serverToUserChannel.disconnect();
10825
10896
  this.serverToUserChannel = null;
10826
10897
  }
10898
+ if (this.signin_requested) {
10899
+ this._signinDoneResolve();
10900
+ }
10901
+ };
10902
+ UserFacade.prototype._newSigninPromiseIfNeeded = function () {
10903
+ if (!this.signin_requested) {
10904
+ return;
10905
+ }
10906
+ if (this.signinDonePromise && !this.signinDonePromise.done) {
10907
+ return;
10908
+ }
10909
+ var _a = flat_promise(), promise = _a.promise, resolve = _a.resolve, _ = _a.reject;
10910
+ promise.done = false;
10911
+ var setDone = function () {
10912
+ promise.done = true;
10913
+ };
10914
+ promise.then(setDone)["catch"](setDone);
10915
+ this.signinDonePromise = promise;
10916
+ this._signinDoneResolve = resolve;
10827
10917
  };
10828
10918
  return UserFacade;
10829
10919
  }(dispatcher));