@aws-amplify/pubsub 4.4.9 → 4.4.10-next.20

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 (102) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/dist/aws-amplify-pubsub.js +1260 -2214
  3. package/dist/aws-amplify-pubsub.js.map +1 -1
  4. package/dist/aws-amplify-pubsub.min.js +3 -3
  5. package/dist/aws-amplify-pubsub.min.js.map +1 -1
  6. package/lib/Providers/AWSAppSyncProvider.js +20 -102
  7. package/lib/Providers/AWSAppSyncProvider.js.map +1 -1
  8. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +10 -2
  9. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +166 -208
  10. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  11. package/lib/Providers/AWSIotProvider.js +4 -52
  12. package/lib/Providers/AWSIotProvider.js.map +1 -1
  13. package/lib/Providers/MqttOverWSProvider.d.ts +5 -1
  14. package/lib/Providers/MqttOverWSProvider.js +172 -187
  15. package/lib/Providers/MqttOverWSProvider.js.map +1 -1
  16. package/lib/Providers/PubSubProvider.d.ts +1 -1
  17. package/lib/Providers/PubSubProvider.js +3 -13
  18. package/lib/Providers/PubSubProvider.js.map +1 -1
  19. package/lib/Providers/{AWSAppSyncRealTimeProvider/constants.d.ts → constants.d.ts} +9 -0
  20. package/lib/Providers/{AWSAppSyncRealTimeProvider/constants.js → constants.js} +9 -0
  21. package/lib/Providers/constants.js.map +1 -0
  22. package/lib/Providers/index.js +6 -8
  23. package/lib/Providers/index.js.map +1 -1
  24. package/lib/PubSub.js +6 -44
  25. package/lib/PubSub.js.map +1 -1
  26. package/lib/index.d.ts +3 -10
  27. package/lib/index.js +5 -14
  28. package/lib/index.js.map +1 -1
  29. package/lib/types/Provider.d.ts +10 -1
  30. package/lib/types/PubSub.d.ts +22 -8
  31. package/lib/types/PubSub.js +56 -0
  32. package/lib/types/PubSub.js.map +1 -1
  33. package/lib/types/index.d.ts +0 -17
  34. package/lib/types/index.js +2 -36
  35. package/lib/types/index.js.map +1 -1
  36. package/lib/utils/ConnectionStateMonitor.d.ts +2 -1
  37. package/lib/utils/ConnectionStateMonitor.js +25 -30
  38. package/lib/utils/ConnectionStateMonitor.js.map +1 -1
  39. package/lib/utils/ReachabilityMonitor/index.d.ts +3 -0
  40. package/lib/utils/ReachabilityMonitor/index.js +5 -0
  41. package/lib/utils/ReachabilityMonitor/index.js.map +1 -0
  42. package/lib/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  43. package/lib/utils/ReachabilityMonitor/index.native.js +9 -0
  44. package/lib/utils/ReachabilityMonitor/index.native.js.map +1 -0
  45. package/lib/utils/ReconnectionMonitor.d.ts +29 -0
  46. package/lib/utils/ReconnectionMonitor.js +73 -0
  47. package/lib/utils/ReconnectionMonitor.js.map +1 -0
  48. package/lib-esm/Providers/AWSAppSyncProvider.js +1 -80
  49. package/lib-esm/Providers/AWSAppSyncProvider.js.map +1 -1
  50. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +10 -2
  51. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +130 -162
  52. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  53. package/lib-esm/Providers/AWSIotProvider.js +1 -49
  54. package/lib-esm/Providers/AWSIotProvider.js.map +1 -1
  55. package/lib-esm/Providers/MqttOverWSProvider.d.ts +5 -1
  56. package/lib-esm/Providers/MqttOverWSProvider.js +154 -159
  57. package/lib-esm/Providers/MqttOverWSProvider.js.map +1 -1
  58. package/lib-esm/Providers/PubSubProvider.d.ts +1 -1
  59. package/lib-esm/Providers/PubSubProvider.js +1 -11
  60. package/lib-esm/Providers/PubSubProvider.js.map +1 -1
  61. package/lib-esm/Providers/{AWSAppSyncRealTimeProvider/constants.d.ts → constants.d.ts} +9 -0
  62. package/lib-esm/Providers/{AWSAppSyncRealTimeProvider/constants.js → constants.js} +9 -0
  63. package/lib-esm/Providers/constants.js.map +1 -0
  64. package/lib-esm/PubSub.js +1 -36
  65. package/lib-esm/PubSub.js.map +1 -1
  66. package/lib-esm/index.d.ts +3 -10
  67. package/lib-esm/index.js +3 -11
  68. package/lib-esm/index.js.map +1 -1
  69. package/lib-esm/types/Provider.d.ts +10 -1
  70. package/lib-esm/types/PubSub.d.ts +22 -8
  71. package/lib-esm/types/PubSub.js +56 -0
  72. package/lib-esm/types/PubSub.js.map +1 -1
  73. package/lib-esm/types/index.d.ts +0 -17
  74. package/lib-esm/types/index.js +1 -36
  75. package/lib-esm/types/index.js.map +1 -1
  76. package/lib-esm/utils/ConnectionStateMonitor.d.ts +2 -1
  77. package/lib-esm/utils/ConnectionStateMonitor.js +14 -16
  78. package/lib-esm/utils/ConnectionStateMonitor.js.map +1 -1
  79. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +3 -0
  80. package/lib-esm/utils/ReachabilityMonitor/index.js +3 -0
  81. package/lib-esm/utils/ReachabilityMonitor/index.js.map +1 -0
  82. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  83. package/lib-esm/utils/ReachabilityMonitor/index.native.js +6 -0
  84. package/lib-esm/utils/ReachabilityMonitor/index.native.js.map +1 -0
  85. package/lib-esm/utils/ReconnectionMonitor.d.ts +29 -0
  86. package/lib-esm/utils/ReconnectionMonitor.js +71 -0
  87. package/lib-esm/utils/ReconnectionMonitor.js.map +1 -0
  88. package/package.json +6 -5
  89. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +154 -90
  90. package/src/Providers/MqttOverWSProvider.ts +120 -53
  91. package/src/Providers/PubSubProvider.ts +1 -1
  92. package/src/Providers/{AWSAppSyncRealTimeProvider/constants.ts → constants.ts} +12 -0
  93. package/src/index.ts +3 -10
  94. package/src/types/Provider.ts +13 -1
  95. package/src/types/PubSub.ts +47 -8
  96. package/src/types/index.ts +0 -43
  97. package/src/utils/ConnectionStateMonitor.ts +20 -5
  98. package/src/utils/ReachabilityMonitor/index.native.ts +5 -0
  99. package/src/utils/ReachabilityMonitor/index.ts +3 -0
  100. package/src/utils/ReconnectionMonitor.ts +74 -0
  101. package/lib/Providers/AWSAppSyncRealTimeProvider/constants.js.map +0 -1
  102. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/constants.js.map +0 -1
@@ -1,91 +1,6 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __assign = (this && this.__assign) || function () {
16
- __assign = Object.assign || function(t) {
17
- for (var s, i = 1, n = arguments.length; i < n; i++) {
18
- s = arguments[i];
19
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
- t[p] = s[p];
21
- }
22
- return t;
23
- };
24
- return __assign.apply(this, arguments);
25
- };
26
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
- return new (P || (P = Promise))(function (resolve, reject) {
29
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
- step((generator = generator.apply(thisArg, _arguments || [])).next());
33
- });
34
- };
35
- var __generator = (this && this.__generator) || function (thisArg, body) {
36
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
37
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
38
- function verb(n) { return function (v) { return step([n, v]); }; }
39
- function step(op) {
40
- if (f) throw new TypeError("Generator is already executing.");
41
- while (_) try {
42
- 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;
43
- if (y = 0, t) op = [op[0] & 2, t.value];
44
- switch (op[0]) {
45
- case 0: case 1: t = op; break;
46
- case 4: _.label++; return { value: op[1], done: false };
47
- case 5: _.label++; y = op[1]; op = [0]; continue;
48
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
49
- default:
50
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
51
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
52
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
53
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
54
- if (t[2]) _.ops.pop();
55
- _.trys.pop(); continue;
56
- }
57
- op = body.call(thisArg, _);
58
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
59
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
60
- }
61
- };
62
- var __read = (this && this.__read) || function (o, n) {
63
- var m = typeof Symbol === "function" && o[Symbol.iterator];
64
- if (!m) return o;
65
- var i = m.call(o), r, ar = [], e;
66
- try {
67
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
68
- }
69
- catch (error) { e = { error: error }; }
70
- finally {
71
- try {
72
- if (r && !r.done && (m = i["return"])) m.call(i);
73
- }
74
- finally { if (e) throw e.error; }
75
- }
76
- return ar;
77
- };
78
- var __importDefault = (this && this.__importDefault) || function (mod) {
79
- return (mod && mod.__esModule) ? mod : { "default": mod };
80
- };
81
- var __importStar = (this && this.__importStar) || function (mod) {
82
- if (mod && mod.__esModule) return mod;
83
- var result = {};
84
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
85
- result["default"] = mod;
86
- return result;
87
- };
88
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ var tslib_1 = require("tslib");
89
4
  /*
90
5
  * Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
91
6
  *
@@ -98,18 +13,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
98
13
  * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
99
14
  * and limitations under the License.
100
15
  */
101
- var zen_observable_ts_1 = __importDefault(require("zen-observable-ts"));
16
+ var zen_observable_ts_1 = tslib_1.__importDefault(require("zen-observable-ts"));
102
17
  var graphql_1 = require("graphql");
103
- var url = __importStar(require("url"));
18
+ var url = tslib_1.__importStar(require("url"));
104
19
  var uuid_1 = require("uuid");
105
20
  var buffer_1 = require("buffer");
106
21
  var core_1 = require("@aws-amplify/core");
107
- var cache_1 = __importDefault(require("@aws-amplify/cache"));
108
- var auth_1 = __importDefault(require("@aws-amplify/auth"));
22
+ var cache_1 = tslib_1.__importDefault(require("@aws-amplify/cache"));
23
+ var auth_1 = tslib_1.__importDefault(require("@aws-amplify/auth"));
109
24
  var PubSubProvider_1 = require("../PubSubProvider");
110
- var index_1 = require("../../index");
111
- var constants_1 = require("./constants");
25
+ var PubSub_1 = require("../../types/PubSub");
26
+ var constants_1 = require("../constants");
112
27
  var ConnectionStateMonitor_1 = require("../../utils/ConnectionStateMonitor");
28
+ var ReconnectionMonitor_1 = require("../../utils/ReconnectionMonitor");
113
29
  var logger = new core_1.Logger('AWSAppSyncRealTimeProvider');
114
30
  var dispatchApiEvent = function (event, data, message) {
115
31
  core_1.Hub.dispatch('api', { event: event, data: data, message: message }, 'PubSub', constants_1.AMPLIFY_SYMBOL);
@@ -117,7 +33,7 @@ var dispatchApiEvent = function (event, data, message) {
117
33
  var standardDomainPattern = /^https:\/\/\w{26}\.appsync\-api\.\w{2}(?:(?:\-\w{2,})+)\-\d\.amazonaws.com\/graphql$/i;
118
34
  var customDomainPath = '/realtime';
119
35
  var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
120
- __extends(AWSAppSyncRealTimeProvider, _super);
36
+ tslib_1.__extends(AWSAppSyncRealTimeProvider, _super);
121
37
  function AWSAppSyncRealTimeProvider(options) {
122
38
  if (options === void 0) { options = {}; }
123
39
  var _this = _super.call(this, options) || this;
@@ -126,15 +42,47 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
126
42
  _this.subscriptionObserverMap = new Map();
127
43
  _this.promiseArray = [];
128
44
  _this.connectionStateMonitor = new ConnectionStateMonitor_1.ConnectionStateMonitor();
45
+ _this.reconnectionMonitor = new ReconnectionMonitor_1.ReconnectionMonitor();
129
46
  // Monitor the connection state and pass changes along to Hub
130
- _this.connectionStateMonitor.connectionStateObservable.subscribe(function (ConnectionState) {
131
- dispatchApiEvent(index_1.CONNECTION_STATE_CHANGE, {
132
- provider: _this,
133
- connectionState: ConnectionState,
134
- }, "Connection state is " + ConnectionState);
135
- });
47
+ _this.connectionStateMonitorSubscription =
48
+ _this.connectionStateMonitor.connectionStateObservable.subscribe(function (connectionState) {
49
+ dispatchApiEvent(constants_1.CONNECTION_STATE_CHANGE, {
50
+ provider: _this,
51
+ connectionState: connectionState,
52
+ }, "Connection state is " + connectionState);
53
+ _this.connectionState = connectionState;
54
+ // Trigger START_RECONNECT when the connection is disrupted
55
+ if (connectionState === PubSub_1.ConnectionState.ConnectionDisrupted) {
56
+ _this.reconnectionMonitor.record(ReconnectionMonitor_1.ReconnectEvent.START_RECONNECT);
57
+ }
58
+ // Trigger HALT_RECONNECT to halt reconnection attempts when the state is anything other than
59
+ // ConnectionDisrupted or Connecting
60
+ if ([
61
+ PubSub_1.ConnectionState.Connected,
62
+ PubSub_1.ConnectionState.ConnectedPendingDisconnect,
63
+ PubSub_1.ConnectionState.ConnectedPendingKeepAlive,
64
+ PubSub_1.ConnectionState.ConnectedPendingNetwork,
65
+ PubSub_1.ConnectionState.ConnectedPendingNetwork,
66
+ PubSub_1.ConnectionState.ConnectionDisruptedPendingNetwork,
67
+ PubSub_1.ConnectionState.Disconnected,
68
+ ].includes(connectionState)) {
69
+ _this.reconnectionMonitor.record(ReconnectionMonitor_1.ReconnectEvent.HALT_RECONNECT);
70
+ }
71
+ });
136
72
  return _this;
137
73
  }
74
+ /**
75
+ * Mark the socket closed and release all active listeners
76
+ */
77
+ AWSAppSyncRealTimeProvider.prototype.close = function () {
78
+ // Mark the socket closed both in status and the connection monitor
79
+ this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
80
+ this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_FAILED);
81
+ // Turn off the subscription monitor Hub publishing
82
+ this.connectionStateMonitorSubscription.unsubscribe();
83
+ // Complete all reconnect observers
84
+ this.reconnectionMonitor.close();
85
+ };
138
86
  AWSAppSyncRealTimeProvider.prototype.getNewWebSocket = function (url, protocol) {
139
87
  return new WebSocket(url, protocol);
140
88
  };
@@ -145,8 +93,8 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
145
93
  throw new Error('Not used here');
146
94
  };
147
95
  AWSAppSyncRealTimeProvider.prototype.publish = function (_topics, _msg, _options) {
148
- return __awaiter(this, void 0, void 0, function () {
149
- return __generator(this, function (_a) {
96
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
97
+ return tslib_1.__generator(this, function (_a) {
150
98
  throw new Error('Operation not supported');
151
99
  });
152
100
  });
@@ -162,35 +110,51 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
162
110
  if (!options || !appSyncGraphqlEndpoint) {
163
111
  observer.error({
164
112
  errors: [
165
- __assign({}, new graphql_1.GraphQLError("Subscribe only available for AWS AppSync endpoint")),
113
+ tslib_1.__assign({}, new graphql_1.GraphQLError("Subscribe only available for AWS AppSync endpoint")),
166
114
  ],
167
115
  });
168
116
  observer.complete();
169
117
  }
170
118
  else {
119
+ var subscriptionStartActive_1 = false;
171
120
  var subscriptionId_1 = uuid_1.v4();
172
- _this._startSubscriptionWithAWSAppSyncRealTime({
173
- options: options,
174
- observer: observer,
175
- subscriptionId: subscriptionId_1,
176
- }).catch(function (err) {
177
- observer.error({
178
- errors: [
179
- __assign({}, new graphql_1.GraphQLError(index_1.CONTROL_MSG.REALTIME_SUBSCRIPTION_INIT_ERROR + ": " + err)),
180
- ],
181
- });
182
- _this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
183
- observer.complete();
121
+ var startSubscription_1 = function () {
122
+ if (!subscriptionStartActive_1) {
123
+ subscriptionStartActive_1 = true;
124
+ var startSubscriptionPromise = _this._startSubscriptionWithAWSAppSyncRealTime({
125
+ options: options,
126
+ observer: observer,
127
+ subscriptionId: subscriptionId_1,
128
+ }).catch(function (err) {
129
+ logger.debug(PubSub_1.CONTROL_MSG.REALTIME_SUBSCRIPTION_INIT_ERROR + ": " + err);
130
+ _this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
131
+ });
132
+ startSubscriptionPromise.finally(function () {
133
+ subscriptionStartActive_1 = false;
134
+ });
135
+ }
136
+ };
137
+ var reconnectSubscription_1;
138
+ // Add an observable to the reconnection list to manage reconnection for this subscription
139
+ reconnectSubscription_1 = new zen_observable_ts_1.default(function (observer) {
140
+ _this.reconnectionMonitor.addObserver(observer);
141
+ }).subscribe(function () {
142
+ startSubscription_1();
184
143
  });
185
- return function () { return __awaiter(_this, void 0, void 0, function () {
144
+ startSubscription_1();
145
+ return function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
186
146
  var subscriptionState, err_1;
187
- return __generator(this, function (_a) {
147
+ return tslib_1.__generator(this, function (_a) {
188
148
  switch (_a.label) {
189
149
  case 0:
190
- _a.trys.push([0, 2, 3, 4]);
150
+ // Cleanup reconnection subscription
151
+ reconnectSubscription_1 === null || reconnectSubscription_1 === void 0 ? void 0 : reconnectSubscription_1.unsubscribe();
152
+ _a.label = 1;
153
+ case 1:
154
+ _a.trys.push([1, 3, 4, 5]);
191
155
  // Waiting that subscription has been connected before trying to unsubscribe
192
156
  return [4 /*yield*/, this._waitForSubscriptionToBeConnected(subscriptionId_1)];
193
- case 1:
157
+ case 2:
194
158
  // Waiting that subscription has been connected before trying to unsubscribe
195
159
  _a.sent();
196
160
  subscriptionState = (this.subscriptionObserverMap.get(subscriptionId_1) || {}).subscriptionState;
@@ -204,15 +168,15 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
204
168
  else {
205
169
  throw new Error('Subscription never connected');
206
170
  }
207
- return [3 /*break*/, 4];
208
- case 2:
171
+ return [3 /*break*/, 5];
172
+ case 3:
209
173
  err_1 = _a.sent();
210
174
  logger.debug("Error while unsubscribing " + err_1);
211
- return [3 /*break*/, 4];
212
- case 3:
175
+ return [3 /*break*/, 5];
176
+ case 4:
213
177
  this._removeSubscriptionObserver(subscriptionId_1);
214
178
  return [7 /*endfinally*/];
215
- case 4: return [2 /*return*/];
179
+ case 5: return [2 /*return*/];
216
180
  }
217
181
  });
218
182
  }); };
@@ -229,15 +193,15 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
229
193
  });
230
194
  AWSAppSyncRealTimeProvider.prototype._startSubscriptionWithAWSAppSyncRealTime = function (_a) {
231
195
  var options = _a.options, observer = _a.observer, subscriptionId = _a.subscriptionId;
232
- var _b, _c;
233
- return __awaiter(this, void 0, void 0, function () {
234
- var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _d, graphql_headers, _e, additionalHeaders, subscriptionState, data, dataString, headerObj, _f, _g, subscriptionMessage, stringToAWSRealTime, err_2, message, subscriptionFailedCallback_1, _h, subscriptionFailedCallback, subscriptionReadyCallback;
235
- var _j;
196
+ var _b;
197
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
198
+ var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _c, graphql_headers, _d, additionalHeaders, subscriptionState, data, dataString, headerObj, _e, _f, subscriptionMessage, stringToAWSRealTime, err_2, _g, subscriptionFailedCallback, subscriptionReadyCallback;
199
+ var _h;
236
200
  var _this = this;
237
- return __generator(this, function (_k) {
238
- switch (_k.label) {
201
+ return tslib_1.__generator(this, function (_j) {
202
+ switch (_j.label) {
239
203
  case 0:
240
- appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint, authenticationType = options.authenticationType, query = options.query, variables = options.variables, apiKey = options.apiKey, region = options.region, _d = options.graphql_headers, graphql_headers = _d === void 0 ? function () { return ({}); } : _d, _e = options.additionalHeaders, additionalHeaders = _e === void 0 ? {} : _e;
204
+ appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint, authenticationType = options.authenticationType, query = options.query, variables = options.variables, apiKey = options.apiKey, region = options.region, _c = options.graphql_headers, graphql_headers = _c === void 0 ? function () { return ({}); } : _c, _d = options.additionalHeaders, additionalHeaders = _d === void 0 ? {} : _d;
241
205
  subscriptionState = constants_1.SUBSCRIPTION_STATUS.PENDING;
242
206
  data = {
243
207
  query: query,
@@ -252,7 +216,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
252
216
  startAckTimeoutId: undefined,
253
217
  });
254
218
  dataString = JSON.stringify(data);
255
- _f = [{}];
219
+ _e = [{}];
256
220
  return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
257
221
  apiKey: apiKey,
258
222
  appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
@@ -263,24 +227,24 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
263
227
  additionalHeaders: additionalHeaders,
264
228
  })];
265
229
  case 1:
266
- _g = [__assign.apply(void 0, _f.concat([(_k.sent())]))];
230
+ _f = [tslib_1.__assign.apply(void 0, _e.concat([(_j.sent())]))];
267
231
  return [4 /*yield*/, graphql_headers()];
268
232
  case 2:
269
- headerObj = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _g.concat([(_k.sent())])), additionalHeaders]), (_j = {}, _j[core_1.USER_AGENT_HEADER] = core_1.Constants.userAgent, _j)]);
233
+ headerObj = tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, _f.concat([(_j.sent())])), additionalHeaders]), (_h = {}, _h[core_1.USER_AGENT_HEADER] = core_1.Constants.userAgent, _h)]);
270
234
  subscriptionMessage = {
271
235
  id: subscriptionId,
272
236
  payload: {
273
237
  data: dataString,
274
238
  extensions: {
275
- authorization: __assign({}, headerObj),
239
+ authorization: tslib_1.__assign({}, headerObj),
276
240
  },
277
241
  },
278
242
  type: constants_1.MESSAGE_TYPES.GQL_START,
279
243
  };
280
244
  stringToAWSRealTime = JSON.stringify(subscriptionMessage);
281
- _k.label = 3;
245
+ _j.label = 3;
282
246
  case 3:
283
- _k.trys.push([3, 5, , 6]);
247
+ _j.trys.push([3, 5, , 6]);
284
248
  this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.OPENING_CONNECTION);
285
249
  return [4 /*yield*/, this._initializeWebSocketConnection({
286
250
  apiKey: apiKey,
@@ -290,27 +254,14 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
290
254
  additionalHeaders: additionalHeaders,
291
255
  })];
292
256
  case 4:
293
- _k.sent();
257
+ _j.sent();
294
258
  return [3 /*break*/, 6];
295
259
  case 5:
296
- err_2 = _k.sent();
297
- logger.debug({ err: err_2 });
298
- message = (_b = err_2['message']) !== null && _b !== void 0 ? _b : '';
299
- this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
300
- observer.error({
301
- errors: [
302
- __assign({}, new graphql_1.GraphQLError(index_1.CONTROL_MSG.CONNECTION_FAILED + ": " + message)),
303
- ],
304
- });
305
- observer.complete();
306
- subscriptionFailedCallback_1 = (this.subscriptionObserverMap.get(subscriptionId) || {}).subscriptionFailedCallback;
307
- // Notify concurrent unsubscription
308
- if (typeof subscriptionFailedCallback_1 === 'function') {
309
- subscriptionFailedCallback_1();
310
- }
260
+ err_2 = _j.sent();
261
+ this._logStartSubscriptionError(subscriptionId, observer, err_2);
311
262
  return [2 /*return*/];
312
263
  case 6:
313
- _h = (_c = this.subscriptionObserverMap.get(subscriptionId)) !== null && _c !== void 0 ? _c : {}, subscriptionFailedCallback = _h.subscriptionFailedCallback, subscriptionReadyCallback = _h.subscriptionReadyCallback;
264
+ _g = (_b = this.subscriptionObserverMap.get(subscriptionId)) !== null && _b !== void 0 ? _b : {}, subscriptionFailedCallback = _g.subscriptionFailedCallback, subscriptionReadyCallback = _g.subscriptionReadyCallback;
314
265
  // This must be done before sending the message in order to be listening immediately
315
266
  this.subscriptionObserverMap.set(subscriptionId, {
316
267
  observer: observer,
@@ -331,12 +282,39 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
331
282
  });
332
283
  });
333
284
  };
285
+ // Log logic for start subscription failures
286
+ AWSAppSyncRealTimeProvider.prototype._logStartSubscriptionError = function (subscriptionId, observer, err) {
287
+ var _a;
288
+ logger.debug({ err: err });
289
+ var message = (_a = err['message']) !== null && _a !== void 0 ? _a : '';
290
+ // Resolving to give the state observer time to propogate the update
291
+ Promise.resolve(this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED));
292
+ // Capture the error only when the network didn't cause disruption
293
+ if (this.connectionState !== PubSub_1.ConnectionState.ConnectionDisruptedPendingNetwork) {
294
+ // When the error is non-retriable, error out the observable
295
+ if (core_1.isNonRetryableError(err)) {
296
+ observer.error({
297
+ errors: [
298
+ tslib_1.__assign({}, new graphql_1.GraphQLError(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + message)),
299
+ ],
300
+ });
301
+ }
302
+ else {
303
+ logger.debug(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + message);
304
+ }
305
+ var subscriptionFailedCallback = (this.subscriptionObserverMap.get(subscriptionId) || {}).subscriptionFailedCallback;
306
+ // Notify concurrent unsubscription
307
+ if (typeof subscriptionFailedCallback === 'function') {
308
+ subscriptionFailedCallback();
309
+ }
310
+ }
311
+ };
334
312
  // Waiting that subscription has been connected before trying to unsubscribe
335
313
  AWSAppSyncRealTimeProvider.prototype._waitForSubscriptionToBeConnected = function (subscriptionId) {
336
- return __awaiter(this, void 0, void 0, function () {
314
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
337
315
  var subscriptionObserver, subscriptionState;
338
316
  var _this = this;
339
- return __generator(this, function (_a) {
317
+ return tslib_1.__generator(this, function (_a) {
340
318
  subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
341
319
  if (subscriptionObserver) {
342
320
  subscriptionState = subscriptionObserver.subscriptionState;
@@ -437,7 +415,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
437
415
  }
438
416
  if (startAckTimeoutId)
439
417
  clearTimeout(startAckTimeoutId);
440
- dispatchApiEvent(index_1.CONTROL_MSG.SUBSCRIPTION_ACK, { query: query, variables: variables }, 'Connection established for subscription');
418
+ dispatchApiEvent(PubSub_1.CONTROL_MSG.SUBSCRIPTION_ACK, { query: query, variables: variables }, 'Connection established for subscription');
441
419
  var subscriptionState = constants_1.SUBSCRIPTION_STATUS.CONNECTED;
442
420
  if (observer) {
443
421
  this.subscriptionObserverMap.set(id, {
@@ -458,7 +436,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
458
436
  clearTimeout(this.keepAliveTimeoutId);
459
437
  if (this.keepAliveAlertTimeoutId)
460
438
  clearTimeout(this.keepAliveAlertTimeoutId);
461
- this.keepAliveTimeoutId = setTimeout(function () { return _this._errorDisconnect(index_1.CONTROL_MSG.TIMEOUT_DISCONNECT); }, this.keepAliveTimeout);
439
+ this.keepAliveTimeoutId = setTimeout(function () { return _this._errorDisconnect(PubSub_1.CONTROL_MSG.TIMEOUT_DISCONNECT); }, this.keepAliveTimeout);
462
440
  this.keepAliveAlertTimeoutId = setTimeout(function () {
463
441
  _this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.KEEP_ALIVE_MISSED);
464
442
  }, constants_1.DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT);
@@ -477,14 +455,14 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
477
455
  subscriptionFailedCallback: subscriptionFailedCallback,
478
456
  subscriptionState: subscriptionState,
479
457
  });
458
+ logger.debug(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload));
480
459
  observer.error({
481
460
  errors: [
482
- __assign({}, new graphql_1.GraphQLError(index_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
461
+ tslib_1.__assign({}, new graphql_1.GraphQLError(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
483
462
  ],
484
463
  });
485
464
  if (startAckTimeoutId)
486
465
  clearTimeout(startAckTimeoutId);
487
- observer.complete();
488
466
  if (typeof subscriptionFailedCallback === 'function') {
489
467
  subscriptionFailedCallback();
490
468
  }
@@ -493,15 +471,6 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
493
471
  };
494
472
  AWSAppSyncRealTimeProvider.prototype._errorDisconnect = function (msg) {
495
473
  logger.debug("Disconnect error: " + msg);
496
- this.subscriptionObserverMap.forEach(function (_a) {
497
- var observer = _a.observer;
498
- if (observer && !observer.closed) {
499
- observer.error({
500
- errors: [__assign({}, new graphql_1.GraphQLError(msg))],
501
- });
502
- }
503
- });
504
- this.subscriptionObserverMap.clear();
505
474
  if (this.awsRealTimeSocket) {
506
475
  this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
507
476
  this.awsRealTimeSocket.close();
@@ -521,18 +490,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
521
490
  variables: variables,
522
491
  subscriptionState: constants_1.SUBSCRIPTION_STATUS.FAILED,
523
492
  });
524
- if (observer && !observer.closed) {
525
- observer.error({
526
- errors: [
527
- __assign({}, new graphql_1.GraphQLError("Subscription timeout " + JSON.stringify({
528
- query: query,
529
- variables: variables,
530
- }))),
531
- ],
532
- });
533
- // Cleanup will be automatically executed
534
- observer.complete();
535
- }
493
+ this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
536
494
  logger.debug('timeoutStartSubscription', JSON.stringify({ query: query, variables: variables }));
537
495
  }
538
496
  };
@@ -542,9 +500,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
542
500
  if (this.socketStatus === constants_1.SOCKET_STATUS.READY) {
543
501
  return;
544
502
  }
545
- return new Promise(function (res, rej) { return __awaiter(_this, void 0, void 0, function () {
503
+ return new Promise(function (res, rej) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
546
504
  var payloadString, headerString, _a, _b, headerQs, payloadQs, discoverableEndpoint, protocol, awsRealTimeUrl, err_3;
547
- return __generator(this, function (_c) {
505
+ return tslib_1.__generator(this, function (_c) {
548
506
  switch (_c.label) {
549
507
  case 0:
550
508
  this.promiseArray.push({ res: res, rej: rej });
@@ -596,6 +554,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
596
554
  return [3 /*break*/, 5];
597
555
  case 4:
598
556
  err_3 = _c.sent();
557
+ logger.debug('Connection exited with', err_3);
599
558
  this.promiseArray.forEach(function (_a) {
600
559
  var rej = _a.rej;
601
560
  return rej(err_3);
@@ -614,8 +573,8 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
614
573
  }); });
615
574
  };
616
575
  AWSAppSyncRealTimeProvider.prototype._initializeRetryableHandshake = function (awsRealTimeUrl) {
617
- return __awaiter(this, void 0, void 0, function () {
618
- return __generator(this, function (_a) {
576
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
577
+ return tslib_1.__generator(this, function (_a) {
619
578
  switch (_a.label) {
620
579
  case 0:
621
580
  logger.debug("Initializaling retryable Handshake");
@@ -628,10 +587,10 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
628
587
  });
629
588
  };
630
589
  AWSAppSyncRealTimeProvider.prototype._initializeHandshake = function (awsRealTimeUrl) {
631
- return __awaiter(this, void 0, void 0, function () {
590
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
632
591
  var err_4, _a, errorType, errorCode;
633
592
  var _this = this;
634
- return __generator(this, function (_b) {
593
+ return tslib_1.__generator(this, function (_b) {
635
594
  switch (_b.label) {
636
595
  case 0:
637
596
  logger.debug("Initializing handshake " + awsRealTimeUrl);
@@ -645,7 +604,6 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
645
604
  logger.debug("WebSocket connection error");
646
605
  };
647
606
  newSocket.onclose = function () {
648
- _this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_FAILED);
649
607
  rej(new Error('Connection handshake error'));
650
608
  };
651
609
  newSocket.onopen = function () {
@@ -680,18 +638,18 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
680
638
  _this._handleIncomingSubscriptionMessage.bind(_this);
681
639
  _this.awsRealTimeSocket.onerror = function (err) {
682
640
  logger.debug(err);
683
- _this._errorDisconnect(index_1.CONTROL_MSG.CONNECTION_CLOSED);
641
+ _this._errorDisconnect(PubSub_1.CONTROL_MSG.CONNECTION_CLOSED);
684
642
  };
685
643
  _this.awsRealTimeSocket.onclose = function (event) {
686
644
  logger.debug("WebSocket closed " + event.reason);
687
- _this._errorDisconnect(index_1.CONTROL_MSG.CONNECTION_CLOSED);
645
+ _this._errorDisconnect(PubSub_1.CONTROL_MSG.CONNECTION_CLOSED);
688
646
  };
689
647
  }
690
648
  res('Cool, connected to AWS AppSyncRealTime');
691
649
  return;
692
650
  }
693
651
  if (type === constants_1.MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
694
- var _c = data.payload, _d = (_c === void 0 ? {} : _c).errors, _e = __read(_d === void 0 ? [] : _d, 1), _f = _e[0], _g = _f === void 0 ? {} : _f, _h = _g.errorType, errorType = _h === void 0 ? '' : _h, _j = _g.errorCode, errorCode = _j === void 0 ? 0 : _j;
652
+ var _c = data.payload, _d = (_c === void 0 ? {} : _c).errors, _e = tslib_1.__read(_d === void 0 ? [] : _d, 1), _f = _e[0], _g = _f === void 0 ? {} : _f, _h = _g.errorType, errorType = _h === void 0 ? '' : _h, _j = _g.errorCode, errorCode = _j === void 0 ? 0 : _j;
695
653
  rej({ errorType: errorType, errorCode: errorCode });
696
654
  }
697
655
  };
@@ -699,13 +657,13 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
699
657
  type: constants_1.MESSAGE_TYPES.GQL_CONNECTION_INIT,
700
658
  };
701
659
  _this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
702
- setTimeout(checkAckOk.bind(_this, ackOk_1), constants_1.CONNECTION_INIT_TIMEOUT);
703
- }
704
- function checkAckOk(ackOk) {
705
- if (!ackOk) {
706
- this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_FAILED);
707
- rej(new Error("Connection timeout: ack from AWSRealTime was not received on " + constants_1.CONNECTION_INIT_TIMEOUT + " ms"));
708
- }
660
+ var checkAckOk_1 = function (ackOk) {
661
+ if (!ackOk) {
662
+ _this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_FAILED);
663
+ rej(new Error("Connection timeout: ack from AWSAppSyncRealTime was not received after " + constants_1.CONNECTION_INIT_TIMEOUT + " ms"));
664
+ }
665
+ };
666
+ setTimeout(function () { return checkAckOk_1(ackOk_1); }, constants_1.CONNECTION_INIT_TIMEOUT);
709
667
  }
710
668
  });
711
669
  })()];
@@ -733,9 +691,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
733
691
  };
734
692
  AWSAppSyncRealTimeProvider.prototype._awsRealTimeHeaderBasedAuth = function (_a) {
735
693
  var authenticationType = _a.authenticationType, payload = _a.payload, canonicalUri = _a.canonicalUri, appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, apiKey = _a.apiKey, region = _a.region, additionalHeaders = _a.additionalHeaders;
736
- return __awaiter(this, void 0, void 0, function () {
694
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
737
695
  var headerHandler, handler, host, result;
738
- return __generator(this, function (_b) {
696
+ return tslib_1.__generator(this, function (_b) {
739
697
  switch (_b.label) {
740
698
  case 0:
741
699
  headerHandler = {
@@ -770,9 +728,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
770
728
  };
771
729
  AWSAppSyncRealTimeProvider.prototype._awsRealTimeCUPHeader = function (_a) {
772
730
  var host = _a.host;
773
- return __awaiter(this, void 0, void 0, function () {
731
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
774
732
  var session;
775
- return __generator(this, function (_b) {
733
+ return tslib_1.__generator(this, function (_b) {
776
734
  switch (_b.label) {
777
735
  case 0: return [4 /*yield*/, auth_1.default.currentSession()];
778
736
  case 1:
@@ -787,9 +745,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
787
745
  };
788
746
  AWSAppSyncRealTimeProvider.prototype._awsRealTimeOPENIDHeader = function (_a) {
789
747
  var host = _a.host;
790
- return __awaiter(this, void 0, void 0, function () {
748
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
791
749
  var token, federatedInfo, currentUser;
792
- return __generator(this, function (_b) {
750
+ return tslib_1.__generator(this, function (_b) {
793
751
  switch (_b.label) {
794
752
  case 0: return [4 /*yield*/, cache_1.default.getItem('federatedInfo')];
795
753
  case 1:
@@ -818,9 +776,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
818
776
  };
819
777
  AWSAppSyncRealTimeProvider.prototype._awsRealTimeApiKeyHeader = function (_a) {
820
778
  var apiKey = _a.apiKey, host = _a.host;
821
- return __awaiter(this, void 0, void 0, function () {
779
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
822
780
  var dt, dtStr;
823
- return __generator(this, function (_b) {
781
+ return tslib_1.__generator(this, function (_b) {
824
782
  dt = new Date();
825
783
  dtStr = dt.toISOString().replace(/[:\-]|\.\d{3}/g, '');
826
784
  return [2 /*return*/, {
@@ -833,9 +791,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
833
791
  };
834
792
  AWSAppSyncRealTimeProvider.prototype._awsRealTimeIAMHeader = function (_a) {
835
793
  var payload = _a.payload, canonicalUri = _a.canonicalUri, appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, region = _a.region;
836
- return __awaiter(this, void 0, void 0, function () {
794
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
837
795
  var endpointInfo, credentialsOK, creds, request, signed_params;
838
- return __generator(this, function (_b) {
796
+ return tslib_1.__generator(this, function (_b) {
839
797
  switch (_b.label) {
840
798
  case 0:
841
799
  endpointInfo = {
@@ -862,7 +820,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
862
820
  url: "" + appSyncGraphqlEndpoint + canonicalUri,
863
821
  data: payload,
864
822
  method: 'POST',
865
- headers: __assign({}, constants_1.AWS_APPSYNC_REALTIME_HEADERS),
823
+ headers: tslib_1.__assign({}, constants_1.AWS_APPSYNC_REALTIME_HEADERS),
866
824
  };
867
825
  signed_params = core_1.Signer.sign(request, creds, endpointInfo);
868
826
  return [2 /*return*/, signed_params.headers];