pusher-js 8.2.0 → 8.3.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 (27) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/node/pusher.js +24 -17
  3. package/dist/node/pusher.js.map +1 -1
  4. package/dist/react-native/pusher.js +2 -2
  5. package/dist/react-native/pusher.js.map +1 -1
  6. package/dist/web/pusher-with-encryption.js +24 -17
  7. package/dist/web/pusher-with-encryption.js.map +1 -1
  8. package/dist/web/pusher-with-encryption.min.js +2 -2
  9. package/dist/web/pusher-with-encryption.min.js.map +1 -1
  10. package/dist/web/pusher.js +24 -17
  11. package/dist/web/pusher.js.map +1 -1
  12. package/dist/web/pusher.min.js +2 -2
  13. package/dist/web/pusher.min.js.map +1 -1
  14. package/dist/worker/pusher-with-encryption.worker.js +24 -17
  15. package/dist/worker/pusher-with-encryption.worker.js.map +1 -1
  16. package/dist/worker/pusher-with-encryption.worker.min.js +2 -2
  17. package/dist/worker/pusher-with-encryption.worker.min.js.map +1 -1
  18. package/dist/worker/pusher.worker.js +24 -17
  19. package/dist/worker/pusher.worker.js.map +1 -1
  20. package/dist/worker/pusher.worker.min.js +2 -2
  21. package/dist/worker/pusher.worker.min.js.map +1 -1
  22. package/package.json +1 -1
  23. package/spec/javascripts/unit/core/strategies/{cached_strategy_spec.js → websocket_prioritized_cached_strategy_spec.js} +56 -14
  24. package/src/core/strategies/{cached_strategy.ts → websocket_prioritized_cached_strategy.ts} +25 -16
  25. package/src/runtimes/isomorphic/default_strategy.ts +3 -3
  26. package/src/runtimes/web/default_strategy.ts +3 -3
  27. package/types/src/core/strategies/{cached_strategy.d.ts → websocket_prioritized_cached_strategy.d.ts} +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pusher-js",
3
- "version": "8.2.0",
3
+ "version": "8.3.0",
4
4
  "description": "Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers",
5
5
  "main": "dist/node/pusher.js",
6
6
  "browser": "dist/web/pusher.js",
@@ -1,9 +1,10 @@
1
1
  var Mocks = require("mocks");
2
2
  var Runtime = require('runtime').default;
3
- var CachedStrategy = require('core/strategies/cached_strategy').default;
3
+ var WebSocketPrioritizedCachedStrategy = require('core/strategies/websocket_prioritized_cached_strategy')
4
+ .default;
4
5
  var Util = require('core/util').default;
5
6
 
6
- describe("CachedStrategy", function() {
7
+ describe("WebSocketPrioritizedCachedStrategy", function() {
7
8
  beforeEach(function() {
8
9
  jasmine.clock().uninstall();
9
10
  jasmine.clock().install();
@@ -16,13 +17,13 @@ describe("CachedStrategy", function() {
16
17
  describe("after calling isSupported", function() {
17
18
  it("should return true when the substrategy is supported", function() {
18
19
  var substrategy = Mocks.getStrategy(true);
19
- var strategy = new CachedStrategy(substrategy, {}, {});
20
+ var strategy = new WebSocketPrioritizedCachedStrategy(substrategy, {}, {});
20
21
  expect(strategy.isSupported()).toBe(true);
21
22
  });
22
23
 
23
24
  it("should return false when the substrategy is not supported", function() {
24
25
  var substrategy = Mocks.getStrategy(false);
25
- var strategy = new CachedStrategy(substrategy, {}, {});
26
+ var strategy = new WebSocketPrioritizedCachedStrategy(substrategy, {}, {});
26
27
  expect(strategy.isSupported()).toBe(false);
27
28
  });
28
29
  });
@@ -34,7 +35,7 @@ describe("CachedStrategy", function() {
34
35
 
35
36
  it("should try the substrategy immediately", function() {
36
37
  var substrategy = Mocks.getStrategy(false);
37
- var strategy = new CachedStrategy(substrategy, {}, {});
38
+ var strategy = new WebSocketPrioritizedCachedStrategy(substrategy, {}, {});
38
39
  var callback = jasmine.createSpy("callback");
39
40
  strategy.connect(0, callback);
40
41
  expect(substrategy.connect).toHaveBeenCalled();
@@ -63,11 +64,12 @@ describe("CachedStrategy", function() {
63
64
  beforeEach(function() {
64
65
  substrategy = Mocks.getStrategy(true);
65
66
  transports = {
66
- test: Mocks.getStrategy(true)
67
+ test: Mocks.getStrategy(true),
68
+ ws: Mocks.getStrategy(true)
67
69
  };
68
70
  timeline = Mocks.getTimeline();
69
71
 
70
- strategy = new CachedStrategy(substrategy, transports, {
72
+ strategy = new WebSocketPrioritizedCachedStrategy(substrategy, transports, {
71
73
  useTLS: useTLS,
72
74
  timeline: timeline
73
75
  });
@@ -139,7 +141,8 @@ describe("CachedStrategy", function() {
139
141
  expect(JSON.parse(localStorage[usedKey])).toEqual({
140
142
  timestamp: Util.now(),
141
143
  transport: "test",
142
- latency: 1000
144
+ latency: 1000,
145
+ cacheSkipCount: 0
143
146
  });
144
147
  expect(localStorage[unusedKey]).toEqual("mock");
145
148
  });
@@ -174,7 +177,8 @@ describe("CachedStrategy", function() {
174
177
  localStorage[usedKey] = JSON.stringify({
175
178
  timestamp: t0,
176
179
  transport: "test",
177
- latency: 1000
180
+ latency: 1000,
181
+ cacheSkipCount: 4
178
182
  });
179
183
  localStorage[unusedKey] = "mock";
180
184
  });
@@ -188,7 +192,7 @@ describe("CachedStrategy", function() {
188
192
  strategy.connect(0, callback);
189
193
  expect(timeline.info).toHaveBeenCalledWith({
190
194
  cached: true,
191
- transport: "test",
195
+ transport: 'test',
192
196
  latency: 1000
193
197
  });
194
198
  });
@@ -235,8 +239,9 @@ describe("CachedStrategy", function() {
235
239
  it("should cache the connected transport", function() {
236
240
  expect(JSON.parse(localStorage[usedKey])).toEqual({
237
241
  timestamp: Util.now(),
238
- transport: "test",
239
- latency: 2000
242
+ transport: 'test',
243
+ latency: 2000,
244
+ cacheSkipCount: 4
240
245
  });
241
246
  expect(localStorage[unusedKey]).toEqual("mock");
242
247
  });
@@ -327,8 +332,9 @@ describe("CachedStrategy", function() {
327
332
  it("should cache the connected transport", function() {
328
333
  expect(JSON.parse(localStorage[usedKey])).toEqual({
329
334
  timestamp: Util.now(),
330
- transport: "test",
331
- latency: 500
335
+ transport: 'test',
336
+ latency: 500,
337
+ cacheSkipCount: 4
332
338
  });
333
339
  expect(localStorage[unusedKey]).toEqual("mock");
334
340
  });
@@ -350,6 +356,42 @@ describe("CachedStrategy", function() {
350
356
  });
351
357
  });
352
358
  });
359
+
360
+ describe('websocket prioritized', function() {
361
+ beforeEach(function() {
362
+ localStorage[unusedKey] = 'mock';
363
+ localStorage[usedKey] = JSON.stringify({
364
+ timestamp: Util.now(),
365
+ transport: 'test',
366
+ latency: 1000,
367
+ cacheSkipCount: 2
368
+ });
369
+ localStorage[usedKey] = "{}";
370
+ });
371
+
372
+ it('should try websocket strategy again', function() {
373
+ strategy.connect(0, callback);
374
+ expect(substrategy.connect).toHaveBeenCalled();
375
+ });
376
+
377
+ it('should try cached strategy when already attempted default strategy', function() {
378
+ localStorage[usedKey] = JSON.stringify({
379
+ timestamp: Util.now(),
380
+ transport: 'test',
381
+ latency: 1000,
382
+ cacheSkipCount: 4
383
+ });
384
+
385
+ strategy.connect(0, callback);
386
+ expect(transports.test.connect).toHaveBeenCalled();
387
+ expect(substrategy.connect).not.toHaveBeenCalled();
388
+
389
+ expect(JSON.parse(localStorage[usedKey])).toEqual(jasmine.objectContaining({
390
+ transport: 'test',
391
+ cacheSkipCount: 4,
392
+ }));
393
+ });
394
+ });
353
395
  }
354
396
 
355
397
  buildCachedTransportTests(false);
@@ -11,13 +11,14 @@ export interface TransportStrategyDictionary {
11
11
  [key: string]: TransportStrategy;
12
12
  }
13
13
 
14
- /** Caches last successful transport and uses it for following attempts.
14
+ /** Caches the last successful transport and, after the first few attempts,
15
+ * uses the cached transport for subsequent attempts.
15
16
  *
16
17
  * @param {Strategy} strategy
17
18
  * @param {Object} transports
18
19
  * @param {Object} options
19
20
  */
20
- export default class CachedStrategy implements Strategy {
21
+ export default class WebSocketPrioritizedCachedStrategy implements Strategy {
21
22
  strategy: Strategy;
22
23
  transports: TransportStrategyDictionary;
23
24
  ttl: number;
@@ -43,22 +44,27 @@ export default class CachedStrategy implements Strategy {
43
44
  connect(minPriority: number, callback: Function) {
44
45
  var usingTLS = this.usingTLS;
45
46
  var info = fetchTransportCache(usingTLS);
47
+ var cacheSkipCount = info && info.cacheSkipCount ? info.cacheSkipCount : 0;
46
48
 
47
49
  var strategies = [this.strategy];
48
50
  if (info && info.timestamp + this.ttl >= Util.now()) {
49
51
  var transport = this.transports[info.transport];
50
52
  if (transport) {
51
- this.timeline.info({
52
- cached: true,
53
- transport: info.transport,
54
- latency: info.latency
55
- });
56
- strategies.push(
57
- new SequentialStrategy([transport], {
58
- timeout: info.latency * 2 + 1000,
59
- failFast: true
60
- })
61
- );
53
+ if (['ws', 'wss'].includes(info.transport) || cacheSkipCount > 3) {
54
+ this.timeline.info({
55
+ cached: true,
56
+ transport: info.transport,
57
+ latency: info.latency
58
+ });
59
+ strategies.push(
60
+ new SequentialStrategy([transport], {
61
+ timeout: info.latency * 2 + 1000,
62
+ failFast: true
63
+ })
64
+ );
65
+ } else {
66
+ cacheSkipCount++;
67
+ }
62
68
  }
63
69
  }
64
70
 
@@ -78,7 +84,8 @@ export default class CachedStrategy implements Strategy {
78
84
  storeTransportCache(
79
85
  usingTLS,
80
86
  handshake.transport.name,
81
- Util.now() - startTimestamp
87
+ Util.now() - startTimestamp,
88
+ cacheSkipCount
82
89
  );
83
90
  callback(null, handshake);
84
91
  }
@@ -120,7 +127,8 @@ function fetchTransportCache(usingTLS: boolean): any {
120
127
  function storeTransportCache(
121
128
  usingTLS: boolean,
122
129
  transport: TransportStrategy,
123
- latency: number
130
+ latency: number,
131
+ cacheSkipCount: number
124
132
  ) {
125
133
  var storage = Runtime.getLocalStorage();
126
134
  if (storage) {
@@ -128,7 +136,8 @@ function storeTransportCache(
128
136
  storage[getTransportCacheKey(usingTLS)] = Collections.safeJSONStringify({
129
137
  timestamp: Util.now(),
130
138
  transport: transport,
131
- latency: latency
139
+ latency: latency,
140
+ cacheSkipCount: cacheSkipCount
132
141
  });
133
142
  } catch (e) {
134
143
  // catch over quota exceptions raised by localStorage
@@ -3,9 +3,9 @@ import TransportManager from 'core/transports/transport_manager';
3
3
  import Strategy from 'core/strategies/strategy';
4
4
  import SequentialStrategy from 'core/strategies/sequential_strategy';
5
5
  import BestConnectedEverStrategy from 'core/strategies/best_connected_ever_strategy';
6
- import CachedStrategy, {
6
+ import WebSocketPrioritizedCachedStrategy, {
7
7
  TransportStrategyDictionary
8
- } from 'core/strategies/cached_strategy';
8
+ } from 'core/strategies/websocket_prioritized_cached_strategy';
9
9
  import DelayedStrategy from 'core/strategies/delayed_strategy';
10
10
  import IfStrategy from 'core/strategies/if_strategy';
11
11
  import FirstConnectedStrategy from 'core/strategies/first_connected_strategy';
@@ -139,7 +139,7 @@ var getDefaultStrategy = function(
139
139
  ]);
140
140
  }
141
141
 
142
- return new CachedStrategy(
142
+ return new WebSocketPrioritizedCachedStrategy(
143
143
  new FirstConnectedStrategy(
144
144
  new IfStrategy(testSupportsStrategy(ws_transport), wsStrategy, http_loop)
145
145
  ),
@@ -4,9 +4,9 @@ import Strategy from 'core/strategies/strategy';
4
4
  import StrategyOptions from 'core/strategies/strategy_options';
5
5
  import SequentialStrategy from 'core/strategies/sequential_strategy';
6
6
  import BestConnectedEverStrategy from 'core/strategies/best_connected_ever_strategy';
7
- import CachedStrategy, {
7
+ import WebSocketPrioritizedCachedStrategy, {
8
8
  TransportStrategyDictionary
9
- } from 'core/strategies/cached_strategy';
9
+ } from 'core/strategies/websocket_prioritized_cached_strategy';
10
10
  import DelayedStrategy from 'core/strategies/delayed_strategy';
11
11
  import IfStrategy from 'core/strategies/if_strategy';
12
12
  import FirstConnectedStrategy from 'core/strategies/first_connected_strategy';
@@ -181,7 +181,7 @@ var getDefaultStrategy = function(
181
181
  ]);
182
182
  }
183
183
 
184
- return new CachedStrategy(
184
+ return new WebSocketPrioritizedCachedStrategy(
185
185
  new FirstConnectedStrategy(
186
186
  new IfStrategy(
187
187
  testSupportsStrategy(ws_transport),
@@ -5,7 +5,7 @@ import Timeline from '../timeline/timeline';
5
5
  export interface TransportStrategyDictionary {
6
6
  [key: string]: TransportStrategy;
7
7
  }
8
- export default class CachedStrategy implements Strategy {
8
+ export default class WebSocketPrioritizedCachedStrategy implements Strategy {
9
9
  strategy: Strategy;
10
10
  transports: TransportStrategyDictionary;
11
11
  ttl: number;