@aws-amplify/pubsub 5.5.6-api-v6-models.b3abc9b.0 → 6.0.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.
- package/iot/packages.json +7 -0
- package/lib/Providers/AWSIot.d.ts +10 -0
- package/lib/Providers/AWSIot.js +34 -0
- package/lib/Providers/{MqttOverWSProvider.d.ts → MqttOverWS.d.ts} +16 -16
- package/lib/Providers/MqttOverWS.js +274 -0
- package/lib/Providers/PubSub.d.ts +11 -0
- package/lib/Providers/PubSub.js +19 -0
- package/lib/Providers/constants.d.ts +1 -1
- package/lib/Providers/constants.js +3 -4
- package/lib/Providers/index.d.ts +3 -4
- package/lib/Providers/index.js +8 -10
- package/lib/clients/iot.d.ts +1 -0
- package/lib/clients/iot.js +7 -0
- package/lib/clients/mqtt.d.ts +1 -0
- package/lib/clients/mqtt.js +7 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +7 -11
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/PubSub.d.ts +21 -3
- package/lib/types/PubSub.js +1 -1
- package/lib/types/index.d.ts +0 -1
- package/lib/types/index.js +5 -3
- package/lib/utils/ConnectionStateMonitor.d.ts +5 -5
- package/lib/utils/ConnectionStateMonitor.js +52 -64
- package/lib/utils/ReachabilityMonitor/index.d.ts +1 -3
- package/lib/utils/ReachabilityMonitor/index.js +4 -3
- package/lib/utils/ReachabilityMonitor/index.native.d.ts +1 -3
- package/lib/utils/ReachabilityMonitor/index.native.js +7 -8
- package/lib/utils/ReconnectionMonitor.d.ts +1 -1
- package/lib/utils/ReconnectionMonitor.js +21 -25
- package/lib-esm/Providers/AWSIot.d.ts +10 -0
- package/lib-esm/Providers/AWSIot.js +30 -0
- package/lib-esm/Providers/{MqttOverWSProvider.d.ts → MqttOverWS.d.ts} +16 -16
- package/lib-esm/Providers/MqttOverWS.js +268 -0
- package/lib-esm/Providers/PubSub.d.ts +11 -0
- package/lib-esm/Providers/PubSub.js +15 -0
- package/lib-esm/Providers/constants.d.ts +1 -1
- package/lib-esm/Providers/constants.js +11 -14
- package/lib-esm/Providers/index.d.ts +3 -4
- package/lib-esm/Providers/index.js +3 -5
- package/lib-esm/clients/iot.d.ts +1 -0
- package/{src/internals/index.ts → lib-esm/clients/iot.js} +1 -1
- package/lib-esm/clients/mqtt.d.ts +1 -0
- package/lib-esm/{internals/index.js → clients/mqtt.js} +1 -2
- package/lib-esm/index.d.ts +2 -2
- package/lib-esm/index.js +2 -3
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types/PubSub.d.ts +21 -3
- package/lib-esm/types/PubSub.js +0 -1
- package/lib-esm/types/index.d.ts +0 -1
- package/lib-esm/types/index.js +2 -1
- package/lib-esm/utils/ConnectionStateMonitor.d.ts +5 -5
- package/lib-esm/utils/ConnectionStateMonitor.js +50 -64
- package/lib-esm/utils/ReachabilityMonitor/index.d.ts +1 -3
- package/lib-esm/utils/ReachabilityMonitor/index.js +2 -3
- package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +1 -3
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +2 -5
- package/lib-esm/utils/ReconnectionMonitor.d.ts +1 -1
- package/lib-esm/utils/ReconnectionMonitor.js +19 -25
- package/mqtt/packages.json +7 -0
- package/package.json +146 -126
- package/src/Providers/{AWSIotProvider.ts → AWSIot.ts} +17 -15
- package/src/Providers/{MqttOverWSProvider.ts → MqttOverWS.ts} +38 -50
- package/src/Providers/PubSub.ts +40 -0
- package/src/Providers/constants.ts +2 -6
- package/src/Providers/index.ts +3 -12
- package/src/clients/iot.ts +4 -0
- package/src/clients/mqtt.ts +7 -0
- package/src/index.ts +3 -12
- package/src/types/PubSub.ts +28 -5
- package/src/types/index.ts +0 -1
- package/src/utils/ConnectionStateMonitor.ts +17 -13
- package/src/utils/ReachabilityMonitor/index.native.ts +2 -1
- package/src/utils/ReachabilityMonitor/index.ts +1 -1
- package/src/utils/ReconnectionMonitor.ts +1 -1
- package/internals/package.json +0 -8
- package/lib/.tsbuildinfo +0 -3
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +0 -77
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +0 -856
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js.map +0 -1
- package/lib/Providers/AWSIotProvider.d.ts +0 -11
- package/lib/Providers/AWSIotProvider.js +0 -53
- package/lib/Providers/AWSIotProvider.js.map +0 -1
- package/lib/Providers/MqttOverWSProvider.js +0 -396
- package/lib/Providers/MqttOverWSProvider.js.map +0 -1
- package/lib/Providers/PubSubProvider.d.ts +0 -15
- package/lib/Providers/PubSubProvider.js +0 -28
- package/lib/Providers/PubSubProvider.js.map +0 -1
- package/lib/Providers/constants.js.map +0 -1
- package/lib/Providers/index.js.map +0 -1
- package/lib/PubSub.d.ts +0 -13
- package/lib/PubSub.js +0 -22
- package/lib/PubSub.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/internals/InternalPubSub.d.ts +0 -50
- package/lib/internals/InternalPubSub.js +0 -136
- package/lib/internals/InternalPubSub.js.map +0 -1
- package/lib/internals/index.d.ts +0 -1
- package/lib/internals/index.js +0 -8
- package/lib/internals/index.js.map +0 -1
- package/lib/types/Provider.d.ts +0 -18
- package/lib/types/Provider.js +0 -3
- package/lib/types/Provider.js.map +0 -1
- package/lib/types/PubSub.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib/utils/ConnectionStateMonitor.js.map +0 -1
- package/lib/utils/ReachabilityMonitor/index.js.map +0 -1
- package/lib/utils/ReachabilityMonitor/index.native.js.map +0 -1
- package/lib/utils/ReconnectionMonitor.js.map +0 -1
- package/lib/vendor/paho-mqtt.d.ts +0 -2
- package/lib/vendor/paho-mqtt.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +0 -77
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +0 -854
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js.map +0 -1
- package/lib-esm/Providers/AWSIotProvider.d.ts +0 -11
- package/lib-esm/Providers/AWSIotProvider.js +0 -51
- package/lib-esm/Providers/AWSIotProvider.js.map +0 -1
- package/lib-esm/Providers/MqttOverWSProvider.js +0 -393
- package/lib-esm/Providers/MqttOverWSProvider.js.map +0 -1
- package/lib-esm/Providers/PubSubProvider.d.ts +0 -15
- package/lib-esm/Providers/PubSubProvider.js +0 -26
- package/lib-esm/Providers/PubSubProvider.js.map +0 -1
- package/lib-esm/Providers/constants.js.map +0 -1
- package/lib-esm/Providers/index.js.map +0 -1
- package/lib-esm/PubSub.d.ts +0 -13
- package/lib-esm/PubSub.js +0 -20
- package/lib-esm/PubSub.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/internals/InternalPubSub.d.ts +0 -50
- package/lib-esm/internals/InternalPubSub.js +0 -134
- package/lib-esm/internals/InternalPubSub.js.map +0 -1
- package/lib-esm/internals/index.d.ts +0 -1
- package/lib-esm/internals/index.js.map +0 -1
- package/lib-esm/types/Provider.d.ts +0 -18
- package/lib-esm/types/Provider.js +0 -1
- package/lib-esm/types/Provider.js.map +0 -1
- package/lib-esm/types/PubSub.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
- package/lib-esm/utils/ConnectionStateMonitor.js.map +0 -1
- package/lib-esm/utils/ReachabilityMonitor/index.js.map +0 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js.map +0 -1
- package/lib-esm/utils/ReconnectionMonitor.js.map +0 -1
- package/lib-esm/vendor/paho-mqtt.d.ts +0 -2
- package/lib-esm/vendor/paho-mqtt.js.map +0 -1
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +0 -1049
- package/src/Providers/PubSubProvider.ts +0 -53
- package/src/PubSub.ts +0 -33
- package/src/internals/InternalPubSub.ts +0 -185
- package/src/types/Provider.ts +0 -37
|
@@ -1,856 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var tslib_1 = require("tslib");
|
|
4
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
-
var zen_observable_ts_1 = tslib_1.__importDefault(require("zen-observable-ts"));
|
|
7
|
-
var graphql_1 = require("graphql");
|
|
8
|
-
var url = tslib_1.__importStar(require("url"));
|
|
9
|
-
var uuid_1 = require("uuid");
|
|
10
|
-
var buffer_1 = require("buffer");
|
|
11
|
-
var core_1 = require("@aws-amplify/core");
|
|
12
|
-
var cache_1 = require("@aws-amplify/cache");
|
|
13
|
-
var auth_1 = require("@aws-amplify/auth");
|
|
14
|
-
var PubSubProvider_1 = require("../PubSubProvider");
|
|
15
|
-
var PubSub_1 = require("../../types/PubSub");
|
|
16
|
-
var constants_1 = require("../constants");
|
|
17
|
-
var ConnectionStateMonitor_1 = require("../../utils/ConnectionStateMonitor");
|
|
18
|
-
var ReconnectionMonitor_1 = require("../../utils/ReconnectionMonitor");
|
|
19
|
-
var logger = new core_1.Logger('AWSAppSyncRealTimeProvider');
|
|
20
|
-
var dispatchApiEvent = function (event, data, message) {
|
|
21
|
-
core_1.Hub.dispatch('api', { event: event, data: data, message: message }, 'PubSub', constants_1.AMPLIFY_SYMBOL);
|
|
22
|
-
};
|
|
23
|
-
var standardDomainPattern = /^https:\/\/\w{26}\.appsync\-api\.\w{2}(?:(?:\-\w{2,})+)\-\d\.amazonaws.com(?:\.cn)?\/graphql$/i;
|
|
24
|
-
var customDomainPath = '/realtime';
|
|
25
|
-
var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
|
|
26
|
-
tslib_1.__extends(AWSAppSyncRealTimeProvider, _super);
|
|
27
|
-
function AWSAppSyncRealTimeProvider(options) {
|
|
28
|
-
if (options === void 0) { options = {}; }
|
|
29
|
-
var _this = _super.call(this, options) || this;
|
|
30
|
-
_this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
|
|
31
|
-
_this.keepAliveTimeout = constants_1.DEFAULT_KEEP_ALIVE_TIMEOUT;
|
|
32
|
-
_this.subscriptionObserverMap = new Map();
|
|
33
|
-
_this.promiseArray = [];
|
|
34
|
-
_this.connectionStateMonitor = new ConnectionStateMonitor_1.ConnectionStateMonitor();
|
|
35
|
-
_this.reconnectionMonitor = new ReconnectionMonitor_1.ReconnectionMonitor();
|
|
36
|
-
// Monitor the connection state and pass changes along to Hub
|
|
37
|
-
_this.connectionStateMonitorSubscription =
|
|
38
|
-
_this.connectionStateMonitor.connectionStateObservable.subscribe(function (connectionState) {
|
|
39
|
-
dispatchApiEvent(constants_1.CONNECTION_STATE_CHANGE, {
|
|
40
|
-
provider: _this,
|
|
41
|
-
connectionState: connectionState,
|
|
42
|
-
}, "Connection state is " + connectionState);
|
|
43
|
-
_this.connectionState = connectionState;
|
|
44
|
-
// Trigger START_RECONNECT when the connection is disrupted
|
|
45
|
-
if (connectionState === PubSub_1.ConnectionState.ConnectionDisrupted) {
|
|
46
|
-
_this.reconnectionMonitor.record(ReconnectionMonitor_1.ReconnectEvent.START_RECONNECT);
|
|
47
|
-
}
|
|
48
|
-
// Trigger HALT_RECONNECT to halt reconnection attempts when the state is anything other than
|
|
49
|
-
// ConnectionDisrupted or Connecting
|
|
50
|
-
if ([
|
|
51
|
-
PubSub_1.ConnectionState.Connected,
|
|
52
|
-
PubSub_1.ConnectionState.ConnectedPendingDisconnect,
|
|
53
|
-
PubSub_1.ConnectionState.ConnectedPendingKeepAlive,
|
|
54
|
-
PubSub_1.ConnectionState.ConnectedPendingNetwork,
|
|
55
|
-
PubSub_1.ConnectionState.ConnectionDisruptedPendingNetwork,
|
|
56
|
-
PubSub_1.ConnectionState.Disconnected,
|
|
57
|
-
].includes(connectionState)) {
|
|
58
|
-
_this.reconnectionMonitor.record(ReconnectionMonitor_1.ReconnectEvent.HALT_RECONNECT);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
return _this;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Mark the socket closed and release all active listeners
|
|
65
|
-
*/
|
|
66
|
-
AWSAppSyncRealTimeProvider.prototype.close = function () {
|
|
67
|
-
// Mark the socket closed both in status and the connection monitor
|
|
68
|
-
this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
|
|
69
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_FAILED);
|
|
70
|
-
// Turn off the subscription monitor Hub publishing
|
|
71
|
-
this.connectionStateMonitorSubscription.unsubscribe();
|
|
72
|
-
// Complete all reconnect observers
|
|
73
|
-
this.reconnectionMonitor.close();
|
|
74
|
-
};
|
|
75
|
-
AWSAppSyncRealTimeProvider.prototype.getNewWebSocket = function (url, protocol) {
|
|
76
|
-
return new WebSocket(url, protocol);
|
|
77
|
-
};
|
|
78
|
-
AWSAppSyncRealTimeProvider.prototype.getProviderName = function () {
|
|
79
|
-
return 'AWSAppSyncRealTimeProvider';
|
|
80
|
-
};
|
|
81
|
-
AWSAppSyncRealTimeProvider.prototype.newClient = function () {
|
|
82
|
-
throw new Error('Not used here');
|
|
83
|
-
};
|
|
84
|
-
AWSAppSyncRealTimeProvider.prototype.publish = function (_topics, _msg, _options) {
|
|
85
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
86
|
-
return tslib_1.__generator(this, function (_a) {
|
|
87
|
-
throw new Error('Operation not supported');
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
// Check if url matches standard domain pattern
|
|
92
|
-
AWSAppSyncRealTimeProvider.prototype.isCustomDomain = function (url) {
|
|
93
|
-
return url.match(standardDomainPattern) === null;
|
|
94
|
-
};
|
|
95
|
-
AWSAppSyncRealTimeProvider.prototype.subscribe = function (_topics, options, customUserAgentDetails) {
|
|
96
|
-
var _this = this;
|
|
97
|
-
var appSyncGraphqlEndpoint = options === null || options === void 0 ? void 0 : options.appSyncGraphqlEndpoint;
|
|
98
|
-
return new zen_observable_ts_1.default(function (observer) {
|
|
99
|
-
if (!options || !appSyncGraphqlEndpoint) {
|
|
100
|
-
observer.error({
|
|
101
|
-
errors: [
|
|
102
|
-
tslib_1.__assign({}, new graphql_1.GraphQLError("Subscribe only available for AWS AppSync endpoint")),
|
|
103
|
-
],
|
|
104
|
-
});
|
|
105
|
-
observer.complete();
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
var subscriptionStartActive_1 = false;
|
|
109
|
-
var subscriptionId_1 = uuid_1.v4();
|
|
110
|
-
var startSubscription_1 = function () {
|
|
111
|
-
if (!subscriptionStartActive_1) {
|
|
112
|
-
subscriptionStartActive_1 = true;
|
|
113
|
-
var startSubscriptionPromise = _this._startSubscriptionWithAWSAppSyncRealTime({
|
|
114
|
-
options: options,
|
|
115
|
-
observer: observer,
|
|
116
|
-
subscriptionId: subscriptionId_1,
|
|
117
|
-
customUserAgentDetails: customUserAgentDetails,
|
|
118
|
-
}).catch(function (err) {
|
|
119
|
-
logger.debug(PubSub_1.CONTROL_MSG.REALTIME_SUBSCRIPTION_INIT_ERROR + ": " + err);
|
|
120
|
-
_this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
121
|
-
});
|
|
122
|
-
startSubscriptionPromise.finally(function () {
|
|
123
|
-
subscriptionStartActive_1 = false;
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
var reconnectSubscription_1;
|
|
128
|
-
// Add an observable to the reconnection list to manage reconnection for this subscription
|
|
129
|
-
reconnectSubscription_1 = new zen_observable_ts_1.default(function (observer) {
|
|
130
|
-
_this.reconnectionMonitor.addObserver(observer);
|
|
131
|
-
}).subscribe(function () {
|
|
132
|
-
startSubscription_1();
|
|
133
|
-
});
|
|
134
|
-
startSubscription_1();
|
|
135
|
-
return function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
136
|
-
var subscriptionState, err_1;
|
|
137
|
-
return tslib_1.__generator(this, function (_a) {
|
|
138
|
-
switch (_a.label) {
|
|
139
|
-
case 0:
|
|
140
|
-
// Cleanup reconnection subscription
|
|
141
|
-
reconnectSubscription_1 === null || reconnectSubscription_1 === void 0 ? void 0 : reconnectSubscription_1.unsubscribe();
|
|
142
|
-
_a.label = 1;
|
|
143
|
-
case 1:
|
|
144
|
-
_a.trys.push([1, 3, 4, 5]);
|
|
145
|
-
// Waiting that subscription has been connected before trying to unsubscribe
|
|
146
|
-
return [4 /*yield*/, this._waitForSubscriptionToBeConnected(subscriptionId_1)];
|
|
147
|
-
case 2:
|
|
148
|
-
// Waiting that subscription has been connected before trying to unsubscribe
|
|
149
|
-
_a.sent();
|
|
150
|
-
subscriptionState = (this.subscriptionObserverMap.get(subscriptionId_1) || {}).subscriptionState;
|
|
151
|
-
if (!subscriptionState) {
|
|
152
|
-
// subscription already unsubscribed
|
|
153
|
-
return [2 /*return*/];
|
|
154
|
-
}
|
|
155
|
-
if (subscriptionState === constants_1.SUBSCRIPTION_STATUS.CONNECTED) {
|
|
156
|
-
this._sendUnsubscriptionMessage(subscriptionId_1);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
throw new Error('Subscription never connected');
|
|
160
|
-
}
|
|
161
|
-
return [3 /*break*/, 5];
|
|
162
|
-
case 3:
|
|
163
|
-
err_1 = _a.sent();
|
|
164
|
-
logger.debug("Error while unsubscribing " + err_1);
|
|
165
|
-
return [3 /*break*/, 5];
|
|
166
|
-
case 4:
|
|
167
|
-
this._removeSubscriptionObserver(subscriptionId_1);
|
|
168
|
-
return [7 /*endfinally*/];
|
|
169
|
-
case 5: return [2 /*return*/];
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}); };
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
};
|
|
176
|
-
Object.defineProperty(AWSAppSyncRealTimeProvider.prototype, "isSSLEnabled", {
|
|
177
|
-
get: function () {
|
|
178
|
-
return !this.options['aws_appsync_dangerously_connect_to_http_endpoint_for_testing'];
|
|
179
|
-
},
|
|
180
|
-
enumerable: true,
|
|
181
|
-
configurable: true
|
|
182
|
-
});
|
|
183
|
-
AWSAppSyncRealTimeProvider.prototype._startSubscriptionWithAWSAppSyncRealTime = function (_a) {
|
|
184
|
-
var options = _a.options, observer = _a.observer, subscriptionId = _a.subscriptionId, customUserAgentDetails = _a.customUserAgentDetails;
|
|
185
|
-
var _b;
|
|
186
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
187
|
-
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;
|
|
188
|
-
var _h;
|
|
189
|
-
var _this = this;
|
|
190
|
-
return tslib_1.__generator(this, function (_j) {
|
|
191
|
-
switch (_j.label) {
|
|
192
|
-
case 0:
|
|
193
|
-
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;
|
|
194
|
-
subscriptionState = constants_1.SUBSCRIPTION_STATUS.PENDING;
|
|
195
|
-
data = {
|
|
196
|
-
query: query,
|
|
197
|
-
variables: variables,
|
|
198
|
-
};
|
|
199
|
-
// Having a subscription id map will make it simple to forward messages received
|
|
200
|
-
this.subscriptionObserverMap.set(subscriptionId, {
|
|
201
|
-
observer: observer,
|
|
202
|
-
query: query !== null && query !== void 0 ? query : '',
|
|
203
|
-
variables: variables !== null && variables !== void 0 ? variables : {},
|
|
204
|
-
subscriptionState: subscriptionState,
|
|
205
|
-
startAckTimeoutId: undefined,
|
|
206
|
-
});
|
|
207
|
-
dataString = JSON.stringify(data);
|
|
208
|
-
_e = [{}];
|
|
209
|
-
return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
|
|
210
|
-
apiKey: apiKey,
|
|
211
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
212
|
-
authenticationType: authenticationType,
|
|
213
|
-
payload: dataString,
|
|
214
|
-
canonicalUri: '',
|
|
215
|
-
region: region,
|
|
216
|
-
additionalHeaders: additionalHeaders,
|
|
217
|
-
})];
|
|
218
|
-
case 1:
|
|
219
|
-
_f = [tslib_1.__assign.apply(void 0, _e.concat([(_j.sent())]))];
|
|
220
|
-
return [4 /*yield*/, graphql_headers()];
|
|
221
|
-
case 2:
|
|
222
|
-
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.getAmplifyUserAgent(customUserAgentDetails), _h)]);
|
|
223
|
-
subscriptionMessage = {
|
|
224
|
-
id: subscriptionId,
|
|
225
|
-
payload: {
|
|
226
|
-
data: dataString,
|
|
227
|
-
extensions: {
|
|
228
|
-
authorization: tslib_1.__assign({}, headerObj),
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
type: constants_1.MESSAGE_TYPES.GQL_START,
|
|
232
|
-
};
|
|
233
|
-
stringToAWSRealTime = JSON.stringify(subscriptionMessage);
|
|
234
|
-
_j.label = 3;
|
|
235
|
-
case 3:
|
|
236
|
-
_j.trys.push([3, 5, , 6]);
|
|
237
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.OPENING_CONNECTION);
|
|
238
|
-
return [4 /*yield*/, this._initializeWebSocketConnection({
|
|
239
|
-
apiKey: apiKey,
|
|
240
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
241
|
-
authenticationType: authenticationType,
|
|
242
|
-
region: region,
|
|
243
|
-
additionalHeaders: additionalHeaders,
|
|
244
|
-
})];
|
|
245
|
-
case 4:
|
|
246
|
-
_j.sent();
|
|
247
|
-
return [3 /*break*/, 6];
|
|
248
|
-
case 5:
|
|
249
|
-
err_2 = _j.sent();
|
|
250
|
-
this._logStartSubscriptionError(subscriptionId, observer, err_2);
|
|
251
|
-
return [2 /*return*/];
|
|
252
|
-
case 6:
|
|
253
|
-
_g = (_b = this.subscriptionObserverMap.get(subscriptionId)) !== null && _b !== void 0 ? _b : {}, subscriptionFailedCallback = _g.subscriptionFailedCallback, subscriptionReadyCallback = _g.subscriptionReadyCallback;
|
|
254
|
-
// This must be done before sending the message in order to be listening immediately
|
|
255
|
-
this.subscriptionObserverMap.set(subscriptionId, {
|
|
256
|
-
observer: observer,
|
|
257
|
-
subscriptionState: subscriptionState,
|
|
258
|
-
query: query !== null && query !== void 0 ? query : '',
|
|
259
|
-
variables: variables !== null && variables !== void 0 ? variables : {},
|
|
260
|
-
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
261
|
-
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
262
|
-
startAckTimeoutId: setTimeout(function () {
|
|
263
|
-
_this._timeoutStartSubscriptionAck.call(_this, subscriptionId);
|
|
264
|
-
}, constants_1.START_ACK_TIMEOUT),
|
|
265
|
-
});
|
|
266
|
-
if (this.awsRealTimeSocket) {
|
|
267
|
-
this.awsRealTimeSocket.send(stringToAWSRealTime);
|
|
268
|
-
}
|
|
269
|
-
return [2 /*return*/];
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
};
|
|
274
|
-
// Log logic for start subscription failures
|
|
275
|
-
AWSAppSyncRealTimeProvider.prototype._logStartSubscriptionError = function (subscriptionId, observer, err) {
|
|
276
|
-
var _a;
|
|
277
|
-
logger.debug({ err: err });
|
|
278
|
-
var message = String((_a = err.message) !== null && _a !== void 0 ? _a : '');
|
|
279
|
-
// Resolving to give the state observer time to propogate the update
|
|
280
|
-
Promise.resolve(this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED));
|
|
281
|
-
// Capture the error only when the network didn't cause disruption
|
|
282
|
-
if (this.connectionState !== PubSub_1.ConnectionState.ConnectionDisruptedPendingNetwork) {
|
|
283
|
-
// When the error is non-retriable, error out the observable
|
|
284
|
-
if (core_1.isNonRetryableError(err)) {
|
|
285
|
-
observer.error({
|
|
286
|
-
errors: [
|
|
287
|
-
tslib_1.__assign({}, new graphql_1.GraphQLError(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + message)),
|
|
288
|
-
],
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
logger.debug(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + message);
|
|
293
|
-
}
|
|
294
|
-
var subscriptionFailedCallback = (this.subscriptionObserverMap.get(subscriptionId) || {}).subscriptionFailedCallback;
|
|
295
|
-
// Notify concurrent unsubscription
|
|
296
|
-
if (typeof subscriptionFailedCallback === 'function') {
|
|
297
|
-
subscriptionFailedCallback();
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
// Waiting that subscription has been connected before trying to unsubscribe
|
|
302
|
-
AWSAppSyncRealTimeProvider.prototype._waitForSubscriptionToBeConnected = function (subscriptionId) {
|
|
303
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
304
|
-
var subscriptionObserver, subscriptionState;
|
|
305
|
-
var _this = this;
|
|
306
|
-
return tslib_1.__generator(this, function (_a) {
|
|
307
|
-
subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
|
|
308
|
-
if (subscriptionObserver) {
|
|
309
|
-
subscriptionState = subscriptionObserver.subscriptionState;
|
|
310
|
-
// This in case unsubscribe is invoked before sending start subscription message
|
|
311
|
-
if (subscriptionState === constants_1.SUBSCRIPTION_STATUS.PENDING) {
|
|
312
|
-
return [2 /*return*/, new Promise(function (res, rej) {
|
|
313
|
-
var observer = subscriptionObserver.observer, subscriptionState = subscriptionObserver.subscriptionState, variables = subscriptionObserver.variables, query = subscriptionObserver.query;
|
|
314
|
-
_this.subscriptionObserverMap.set(subscriptionId, {
|
|
315
|
-
observer: observer,
|
|
316
|
-
subscriptionState: subscriptionState,
|
|
317
|
-
variables: variables,
|
|
318
|
-
query: query,
|
|
319
|
-
subscriptionReadyCallback: res,
|
|
320
|
-
subscriptionFailedCallback: rej,
|
|
321
|
-
});
|
|
322
|
-
})];
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return [2 /*return*/];
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
};
|
|
329
|
-
AWSAppSyncRealTimeProvider.prototype._sendUnsubscriptionMessage = function (subscriptionId) {
|
|
330
|
-
try {
|
|
331
|
-
if (this.awsRealTimeSocket &&
|
|
332
|
-
this.awsRealTimeSocket.readyState === WebSocket.OPEN &&
|
|
333
|
-
this.socketStatus === constants_1.SOCKET_STATUS.READY) {
|
|
334
|
-
// Preparing unsubscribe message to stop receiving messages for that subscription
|
|
335
|
-
var unsubscribeMessage = {
|
|
336
|
-
id: subscriptionId,
|
|
337
|
-
type: constants_1.MESSAGE_TYPES.GQL_STOP,
|
|
338
|
-
};
|
|
339
|
-
var stringToAWSRealTime = JSON.stringify(unsubscribeMessage);
|
|
340
|
-
this.awsRealTimeSocket.send(stringToAWSRealTime);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
catch (err) {
|
|
344
|
-
// If GQL_STOP is not sent because of disconnection issue, then there is nothing the client can do
|
|
345
|
-
logger.debug({ err: err });
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
AWSAppSyncRealTimeProvider.prototype._removeSubscriptionObserver = function (subscriptionId) {
|
|
349
|
-
this.subscriptionObserverMap.delete(subscriptionId);
|
|
350
|
-
// Verifying 1000ms after removing subscription in case there are new subscription unmount/mount
|
|
351
|
-
setTimeout(this._closeSocketIfRequired.bind(this), 1000);
|
|
352
|
-
};
|
|
353
|
-
AWSAppSyncRealTimeProvider.prototype._closeSocketIfRequired = function () {
|
|
354
|
-
if (this.subscriptionObserverMap.size > 0) {
|
|
355
|
-
// Active subscriptions on the WebSocket
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
if (!this.awsRealTimeSocket) {
|
|
359
|
-
this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSING_CONNECTION);
|
|
363
|
-
if (this.awsRealTimeSocket.bufferedAmount > 0) {
|
|
364
|
-
// Still data on the WebSocket
|
|
365
|
-
setTimeout(this._closeSocketIfRequired.bind(this), 1000);
|
|
366
|
-
}
|
|
367
|
-
else {
|
|
368
|
-
logger.debug('closing WebSocket...');
|
|
369
|
-
if (this.keepAliveTimeoutId) {
|
|
370
|
-
clearTimeout(this.keepAliveTimeoutId);
|
|
371
|
-
}
|
|
372
|
-
if (this.keepAliveAlertTimeoutId) {
|
|
373
|
-
clearTimeout(this.keepAliveAlertTimeoutId);
|
|
374
|
-
}
|
|
375
|
-
var tempSocket = this.awsRealTimeSocket;
|
|
376
|
-
// Cleaning callbacks to avoid race condition, socket still exists
|
|
377
|
-
tempSocket.onclose = null;
|
|
378
|
-
tempSocket.onerror = null;
|
|
379
|
-
tempSocket.close(1000);
|
|
380
|
-
this.awsRealTimeSocket = undefined;
|
|
381
|
-
this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
|
|
382
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
AWSAppSyncRealTimeProvider.prototype._handleIncomingSubscriptionMessage = function (message) {
|
|
386
|
-
var _this = this;
|
|
387
|
-
if (typeof message.data !== 'string') {
|
|
388
|
-
return;
|
|
389
|
-
}
|
|
390
|
-
logger.debug("subscription message from AWS AppSync RealTime: " + message.data);
|
|
391
|
-
var _a = JSON.parse(String(message.data)), _b = _a.id, id = _b === void 0 ? '' : _b, payload = _a.payload, type = _a.type;
|
|
392
|
-
var _c = this.subscriptionObserverMap.get(id) || {}, _d = _c.observer, observer = _d === void 0 ? null : _d, _e = _c.query, query = _e === void 0 ? '' : _e, _f = _c.variables, variables = _f === void 0 ? {} : _f, startAckTimeoutId = _c.startAckTimeoutId, subscriptionReadyCallback = _c.subscriptionReadyCallback, subscriptionFailedCallback = _c.subscriptionFailedCallback;
|
|
393
|
-
logger.debug({ id: id, observer: observer, query: query, variables: variables });
|
|
394
|
-
if (type === constants_1.MESSAGE_TYPES.GQL_DATA && payload && payload.data) {
|
|
395
|
-
if (observer) {
|
|
396
|
-
observer.next(payload);
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
logger.debug("observer not found for id: " + id);
|
|
400
|
-
}
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
if (type === constants_1.MESSAGE_TYPES.GQL_START_ACK) {
|
|
404
|
-
logger.debug("subscription ready for " + JSON.stringify({ query: query, variables: variables }));
|
|
405
|
-
if (typeof subscriptionReadyCallback === 'function') {
|
|
406
|
-
subscriptionReadyCallback();
|
|
407
|
-
}
|
|
408
|
-
if (startAckTimeoutId)
|
|
409
|
-
clearTimeout(startAckTimeoutId);
|
|
410
|
-
dispatchApiEvent(PubSub_1.CONTROL_MSG.SUBSCRIPTION_ACK, { query: query, variables: variables }, 'Connection established for subscription');
|
|
411
|
-
var subscriptionState = constants_1.SUBSCRIPTION_STATUS.CONNECTED;
|
|
412
|
-
if (observer) {
|
|
413
|
-
this.subscriptionObserverMap.set(id, {
|
|
414
|
-
observer: observer,
|
|
415
|
-
query: query,
|
|
416
|
-
variables: variables,
|
|
417
|
-
startAckTimeoutId: undefined,
|
|
418
|
-
subscriptionState: subscriptionState,
|
|
419
|
-
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
420
|
-
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_ESTABLISHED);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
if (type === constants_1.MESSAGE_TYPES.GQL_CONNECTION_KEEP_ALIVE) {
|
|
427
|
-
if (this.keepAliveTimeoutId)
|
|
428
|
-
clearTimeout(this.keepAliveTimeoutId);
|
|
429
|
-
if (this.keepAliveAlertTimeoutId)
|
|
430
|
-
clearTimeout(this.keepAliveAlertTimeoutId);
|
|
431
|
-
this.keepAliveTimeoutId = setTimeout(function () { return _this._errorDisconnect(PubSub_1.CONTROL_MSG.TIMEOUT_DISCONNECT); }, this.keepAliveTimeout);
|
|
432
|
-
this.keepAliveAlertTimeoutId = setTimeout(function () {
|
|
433
|
-
_this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.KEEP_ALIVE_MISSED);
|
|
434
|
-
}, constants_1.DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT);
|
|
435
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.KEEP_ALIVE);
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
if (type === constants_1.MESSAGE_TYPES.GQL_ERROR) {
|
|
439
|
-
var subscriptionState = constants_1.SUBSCRIPTION_STATUS.FAILED;
|
|
440
|
-
if (observer) {
|
|
441
|
-
this.subscriptionObserverMap.set(id, {
|
|
442
|
-
observer: observer,
|
|
443
|
-
query: query,
|
|
444
|
-
variables: variables,
|
|
445
|
-
startAckTimeoutId: startAckTimeoutId,
|
|
446
|
-
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
447
|
-
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
448
|
-
subscriptionState: subscriptionState,
|
|
449
|
-
});
|
|
450
|
-
logger.debug(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload));
|
|
451
|
-
observer.error({
|
|
452
|
-
errors: [
|
|
453
|
-
tslib_1.__assign({}, new graphql_1.GraphQLError(PubSub_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
|
|
454
|
-
],
|
|
455
|
-
});
|
|
456
|
-
if (startAckTimeoutId)
|
|
457
|
-
clearTimeout(startAckTimeoutId);
|
|
458
|
-
if (typeof subscriptionFailedCallback === 'function') {
|
|
459
|
-
subscriptionFailedCallback();
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
};
|
|
464
|
-
AWSAppSyncRealTimeProvider.prototype._errorDisconnect = function (msg) {
|
|
465
|
-
logger.debug("Disconnect error: " + msg);
|
|
466
|
-
if (this.awsRealTimeSocket) {
|
|
467
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
468
|
-
this.awsRealTimeSocket.close();
|
|
469
|
-
}
|
|
470
|
-
this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
|
|
471
|
-
};
|
|
472
|
-
AWSAppSyncRealTimeProvider.prototype._timeoutStartSubscriptionAck = function (subscriptionId) {
|
|
473
|
-
var subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
|
|
474
|
-
if (subscriptionObserver) {
|
|
475
|
-
var observer = subscriptionObserver.observer, query = subscriptionObserver.query, variables = subscriptionObserver.variables;
|
|
476
|
-
if (!observer) {
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
this.subscriptionObserverMap.set(subscriptionId, {
|
|
480
|
-
observer: observer,
|
|
481
|
-
query: query,
|
|
482
|
-
variables: variables,
|
|
483
|
-
subscriptionState: constants_1.SUBSCRIPTION_STATUS.FAILED,
|
|
484
|
-
});
|
|
485
|
-
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
486
|
-
logger.debug('timeoutStartSubscription', JSON.stringify({ query: query, variables: variables }));
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
AWSAppSyncRealTimeProvider.prototype._initializeWebSocketConnection = function (_a) {
|
|
490
|
-
var _this = this;
|
|
491
|
-
var appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, authenticationType = _a.authenticationType, apiKey = _a.apiKey, region = _a.region, additionalHeaders = _a.additionalHeaders;
|
|
492
|
-
if (this.socketStatus === constants_1.SOCKET_STATUS.READY) {
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
return new Promise(function (res, rej) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
496
|
-
var payloadString, authHeader, headerString, headerQs, payloadQs, discoverableEndpoint, protocol, awsRealTimeUrl, err_3;
|
|
497
|
-
return tslib_1.__generator(this, function (_a) {
|
|
498
|
-
switch (_a.label) {
|
|
499
|
-
case 0:
|
|
500
|
-
this.promiseArray.push({ res: res, rej: rej });
|
|
501
|
-
if (!(this.socketStatus === constants_1.SOCKET_STATUS.CLOSED)) return [3 /*break*/, 5];
|
|
502
|
-
_a.label = 1;
|
|
503
|
-
case 1:
|
|
504
|
-
_a.trys.push([1, 4, , 5]);
|
|
505
|
-
this.socketStatus = constants_1.SOCKET_STATUS.CONNECTING;
|
|
506
|
-
payloadString = '{}';
|
|
507
|
-
return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
|
|
508
|
-
authenticationType: authenticationType,
|
|
509
|
-
payload: payloadString,
|
|
510
|
-
canonicalUri: '/connect',
|
|
511
|
-
apiKey: apiKey,
|
|
512
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
513
|
-
region: region,
|
|
514
|
-
additionalHeaders: additionalHeaders,
|
|
515
|
-
})];
|
|
516
|
-
case 2:
|
|
517
|
-
authHeader = _a.sent();
|
|
518
|
-
headerString = authHeader ? JSON.stringify(authHeader) : '';
|
|
519
|
-
headerQs = buffer_1.Buffer.from(headerString).toString('base64');
|
|
520
|
-
payloadQs = buffer_1.Buffer.from(payloadString).toString('base64');
|
|
521
|
-
discoverableEndpoint = appSyncGraphqlEndpoint !== null && appSyncGraphqlEndpoint !== void 0 ? appSyncGraphqlEndpoint : '';
|
|
522
|
-
if (this.isCustomDomain(discoverableEndpoint)) {
|
|
523
|
-
discoverableEndpoint =
|
|
524
|
-
discoverableEndpoint.concat(customDomainPath);
|
|
525
|
-
}
|
|
526
|
-
else {
|
|
527
|
-
discoverableEndpoint = discoverableEndpoint
|
|
528
|
-
.replace('appsync-api', 'appsync-realtime-api')
|
|
529
|
-
.replace('gogi-beta', 'grt-beta');
|
|
530
|
-
}
|
|
531
|
-
protocol = this.isSSLEnabled ? 'wss://' : 'ws://';
|
|
532
|
-
discoverableEndpoint = discoverableEndpoint
|
|
533
|
-
.replace('https://', protocol)
|
|
534
|
-
.replace('http://', protocol);
|
|
535
|
-
awsRealTimeUrl = discoverableEndpoint + "?header=" + headerQs + "&payload=" + payloadQs;
|
|
536
|
-
return [4 /*yield*/, this._initializeRetryableHandshake(awsRealTimeUrl)];
|
|
537
|
-
case 3:
|
|
538
|
-
_a.sent();
|
|
539
|
-
this.promiseArray.forEach(function (_a) {
|
|
540
|
-
var res = _a.res;
|
|
541
|
-
logger.debug('Notifying connection successful');
|
|
542
|
-
res();
|
|
543
|
-
});
|
|
544
|
-
this.socketStatus = constants_1.SOCKET_STATUS.READY;
|
|
545
|
-
this.promiseArray = [];
|
|
546
|
-
return [3 /*break*/, 5];
|
|
547
|
-
case 4:
|
|
548
|
-
err_3 = _a.sent();
|
|
549
|
-
logger.debug('Connection exited with', err_3);
|
|
550
|
-
this.promiseArray.forEach(function (_a) {
|
|
551
|
-
var rej = _a.rej;
|
|
552
|
-
return rej(err_3);
|
|
553
|
-
});
|
|
554
|
-
this.promiseArray = [];
|
|
555
|
-
if (this.awsRealTimeSocket &&
|
|
556
|
-
this.awsRealTimeSocket.readyState === WebSocket.OPEN) {
|
|
557
|
-
this.awsRealTimeSocket.close(3001);
|
|
558
|
-
}
|
|
559
|
-
this.awsRealTimeSocket = undefined;
|
|
560
|
-
this.socketStatus = constants_1.SOCKET_STATUS.CLOSED;
|
|
561
|
-
return [3 /*break*/, 5];
|
|
562
|
-
case 5: return [2 /*return*/];
|
|
563
|
-
}
|
|
564
|
-
});
|
|
565
|
-
}); });
|
|
566
|
-
};
|
|
567
|
-
AWSAppSyncRealTimeProvider.prototype._initializeRetryableHandshake = function (awsRealTimeUrl) {
|
|
568
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
569
|
-
return tslib_1.__generator(this, function (_a) {
|
|
570
|
-
switch (_a.label) {
|
|
571
|
-
case 0:
|
|
572
|
-
logger.debug("Initializaling retryable Handshake");
|
|
573
|
-
return [4 /*yield*/, core_1.jitteredExponentialRetry(this._initializeHandshake.bind(this), [awsRealTimeUrl], constants_1.MAX_DELAY_MS)];
|
|
574
|
-
case 1:
|
|
575
|
-
_a.sent();
|
|
576
|
-
return [2 /*return*/];
|
|
577
|
-
}
|
|
578
|
-
});
|
|
579
|
-
});
|
|
580
|
-
};
|
|
581
|
-
AWSAppSyncRealTimeProvider.prototype._initializeHandshake = function (awsRealTimeUrl) {
|
|
582
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
583
|
-
var err_4, _a, errorType, errorCode;
|
|
584
|
-
var _this = this;
|
|
585
|
-
return tslib_1.__generator(this, function (_b) {
|
|
586
|
-
switch (_b.label) {
|
|
587
|
-
case 0:
|
|
588
|
-
logger.debug("Initializing handshake " + awsRealTimeUrl);
|
|
589
|
-
_b.label = 1;
|
|
590
|
-
case 1:
|
|
591
|
-
_b.trys.push([1, 4, , 5]);
|
|
592
|
-
return [4 /*yield*/, (function () {
|
|
593
|
-
return new Promise(function (res, rej) {
|
|
594
|
-
var newSocket = _this.getNewWebSocket(awsRealTimeUrl, 'graphql-ws');
|
|
595
|
-
newSocket.onerror = function () {
|
|
596
|
-
logger.debug("WebSocket connection error");
|
|
597
|
-
};
|
|
598
|
-
newSocket.onclose = function () {
|
|
599
|
-
rej(new Error('Connection handshake error'));
|
|
600
|
-
};
|
|
601
|
-
newSocket.onopen = function () {
|
|
602
|
-
_this.awsRealTimeSocket = newSocket;
|
|
603
|
-
return res();
|
|
604
|
-
};
|
|
605
|
-
});
|
|
606
|
-
})()];
|
|
607
|
-
case 2:
|
|
608
|
-
_b.sent();
|
|
609
|
-
// Step 2: wait for ack from AWS AppSyncReaTime after sending init
|
|
610
|
-
return [4 /*yield*/, (function () {
|
|
611
|
-
return new Promise(function (res, rej) {
|
|
612
|
-
if (_this.awsRealTimeSocket) {
|
|
613
|
-
var ackOk_1 = false;
|
|
614
|
-
_this.awsRealTimeSocket.onerror = function (error) {
|
|
615
|
-
logger.debug("WebSocket error " + JSON.stringify(error));
|
|
616
|
-
};
|
|
617
|
-
_this.awsRealTimeSocket.onclose = function (event) {
|
|
618
|
-
logger.debug("WebSocket closed " + event.reason);
|
|
619
|
-
rej(new Error(JSON.stringify(event)));
|
|
620
|
-
};
|
|
621
|
-
_this.awsRealTimeSocket.onmessage = function (message) {
|
|
622
|
-
if (typeof message.data !== 'string') {
|
|
623
|
-
return;
|
|
624
|
-
}
|
|
625
|
-
logger.debug("subscription message from AWS AppSyncRealTime: " + message.data + " ");
|
|
626
|
-
var data = JSON.parse(message.data);
|
|
627
|
-
var type = data.type, _a = data.payload, _b = (_a === void 0 ? {} : _a).connectionTimeoutMs, connectionTimeoutMs = _b === void 0 ? constants_1.DEFAULT_KEEP_ALIVE_TIMEOUT : _b;
|
|
628
|
-
if (type === constants_1.MESSAGE_TYPES.GQL_CONNECTION_ACK) {
|
|
629
|
-
ackOk_1 = true;
|
|
630
|
-
if (_this.awsRealTimeSocket) {
|
|
631
|
-
_this.keepAliveTimeout = connectionTimeoutMs;
|
|
632
|
-
_this.awsRealTimeSocket.onmessage =
|
|
633
|
-
_this._handleIncomingSubscriptionMessage.bind(_this);
|
|
634
|
-
_this.awsRealTimeSocket.onerror = function (err) {
|
|
635
|
-
logger.debug(err);
|
|
636
|
-
_this._errorDisconnect(PubSub_1.CONTROL_MSG.CONNECTION_CLOSED);
|
|
637
|
-
};
|
|
638
|
-
_this.awsRealTimeSocket.onclose = function (event) {
|
|
639
|
-
logger.debug("WebSocket closed " + event.reason);
|
|
640
|
-
_this._errorDisconnect(PubSub_1.CONTROL_MSG.CONNECTION_CLOSED);
|
|
641
|
-
};
|
|
642
|
-
}
|
|
643
|
-
res('Cool, connected to AWS AppSyncRealTime');
|
|
644
|
-
return;
|
|
645
|
-
}
|
|
646
|
-
if (type === constants_1.MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
|
|
647
|
-
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;
|
|
648
|
-
rej({ errorType: errorType, errorCode: errorCode });
|
|
649
|
-
}
|
|
650
|
-
};
|
|
651
|
-
var gqlInit = {
|
|
652
|
-
type: constants_1.MESSAGE_TYPES.GQL_CONNECTION_INIT,
|
|
653
|
-
};
|
|
654
|
-
_this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
|
|
655
|
-
var checkAckOk_1 = function (ackOk) {
|
|
656
|
-
if (!ackOk) {
|
|
657
|
-
_this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_FAILED);
|
|
658
|
-
rej(new Error("Connection timeout: ack from AWSAppSyncRealTime was not received after " + constants_1.CONNECTION_INIT_TIMEOUT + " ms"));
|
|
659
|
-
}
|
|
660
|
-
};
|
|
661
|
-
setTimeout(function () { return checkAckOk_1(ackOk_1); }, constants_1.CONNECTION_INIT_TIMEOUT);
|
|
662
|
-
}
|
|
663
|
-
});
|
|
664
|
-
})()];
|
|
665
|
-
case 3:
|
|
666
|
-
// Step 2: wait for ack from AWS AppSyncReaTime after sending init
|
|
667
|
-
_b.sent();
|
|
668
|
-
return [3 /*break*/, 5];
|
|
669
|
-
case 4:
|
|
670
|
-
err_4 = _b.sent();
|
|
671
|
-
_a = err_4, errorType = _a.errorType, errorCode = _a.errorCode;
|
|
672
|
-
if (constants_1.NON_RETRYABLE_CODES.includes(errorCode)) {
|
|
673
|
-
throw new core_1.NonRetryableError(errorType);
|
|
674
|
-
}
|
|
675
|
-
else if (errorType) {
|
|
676
|
-
throw new Error(errorType);
|
|
677
|
-
}
|
|
678
|
-
else {
|
|
679
|
-
throw err_4;
|
|
680
|
-
}
|
|
681
|
-
return [3 /*break*/, 5];
|
|
682
|
-
case 5: return [2 /*return*/];
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
});
|
|
686
|
-
};
|
|
687
|
-
AWSAppSyncRealTimeProvider.prototype._awsRealTimeHeaderBasedAuth = function (_a) {
|
|
688
|
-
var authenticationType = _a.authenticationType, payload = _a.payload, canonicalUri = _a.canonicalUri, appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, apiKey = _a.apiKey, region = _a.region, additionalHeaders = _a.additionalHeaders;
|
|
689
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
690
|
-
var headerHandler, handler, host, result;
|
|
691
|
-
return tslib_1.__generator(this, function (_b) {
|
|
692
|
-
switch (_b.label) {
|
|
693
|
-
case 0:
|
|
694
|
-
headerHandler = {
|
|
695
|
-
API_KEY: this._awsRealTimeApiKeyHeader.bind(this),
|
|
696
|
-
AWS_IAM: this._awsRealTimeIAMHeader.bind(this),
|
|
697
|
-
OPENID_CONNECT: this._awsRealTimeOPENIDHeader.bind(this),
|
|
698
|
-
AMAZON_COGNITO_USER_POOLS: this._awsRealTimeCUPHeader.bind(this),
|
|
699
|
-
AWS_LAMBDA: this._customAuthHeader,
|
|
700
|
-
};
|
|
701
|
-
if (!(!authenticationType || !headerHandler[authenticationType])) return [3 /*break*/, 1];
|
|
702
|
-
logger.debug("Authentication type " + authenticationType + " not supported");
|
|
703
|
-
return [2 /*return*/, undefined];
|
|
704
|
-
case 1:
|
|
705
|
-
handler = headerHandler[authenticationType];
|
|
706
|
-
host = url.parse(appSyncGraphqlEndpoint !== null && appSyncGraphqlEndpoint !== void 0 ? appSyncGraphqlEndpoint : '').host;
|
|
707
|
-
logger.debug("Authenticating with " + authenticationType);
|
|
708
|
-
return [4 /*yield*/, handler({
|
|
709
|
-
payload: payload,
|
|
710
|
-
canonicalUri: canonicalUri,
|
|
711
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
712
|
-
apiKey: apiKey,
|
|
713
|
-
region: region,
|
|
714
|
-
host: host,
|
|
715
|
-
additionalHeaders: additionalHeaders,
|
|
716
|
-
})];
|
|
717
|
-
case 2:
|
|
718
|
-
result = _b.sent();
|
|
719
|
-
return [2 /*return*/, result];
|
|
720
|
-
}
|
|
721
|
-
});
|
|
722
|
-
});
|
|
723
|
-
};
|
|
724
|
-
AWSAppSyncRealTimeProvider.prototype._awsRealTimeCUPHeader = function (_a) {
|
|
725
|
-
var host = _a.host;
|
|
726
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
727
|
-
var session;
|
|
728
|
-
return tslib_1.__generator(this, function (_b) {
|
|
729
|
-
switch (_b.label) {
|
|
730
|
-
case 0: return [4 /*yield*/, auth_1.Auth.currentSession()];
|
|
731
|
-
case 1:
|
|
732
|
-
session = _b.sent();
|
|
733
|
-
return [2 /*return*/, {
|
|
734
|
-
Authorization: session.getAccessToken().getJwtToken(),
|
|
735
|
-
host: host,
|
|
736
|
-
}];
|
|
737
|
-
}
|
|
738
|
-
});
|
|
739
|
-
});
|
|
740
|
-
};
|
|
741
|
-
AWSAppSyncRealTimeProvider.prototype._awsRealTimeOPENIDHeader = function (_a) {
|
|
742
|
-
var host = _a.host;
|
|
743
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
744
|
-
var token, federatedInfo, currentUser;
|
|
745
|
-
return tslib_1.__generator(this, function (_b) {
|
|
746
|
-
switch (_b.label) {
|
|
747
|
-
case 0: return [4 /*yield*/, cache_1.Cache.getItem('federatedInfo')];
|
|
748
|
-
case 1:
|
|
749
|
-
federatedInfo = _b.sent();
|
|
750
|
-
if (!federatedInfo) return [3 /*break*/, 2];
|
|
751
|
-
token = federatedInfo.token;
|
|
752
|
-
return [3 /*break*/, 4];
|
|
753
|
-
case 2: return [4 /*yield*/, auth_1.Auth.currentAuthenticatedUser()];
|
|
754
|
-
case 3:
|
|
755
|
-
currentUser = _b.sent();
|
|
756
|
-
if (currentUser) {
|
|
757
|
-
token = currentUser.token;
|
|
758
|
-
}
|
|
759
|
-
_b.label = 4;
|
|
760
|
-
case 4:
|
|
761
|
-
if (!token) {
|
|
762
|
-
throw new Error('No federated jwt');
|
|
763
|
-
}
|
|
764
|
-
return [2 /*return*/, {
|
|
765
|
-
Authorization: token,
|
|
766
|
-
host: host,
|
|
767
|
-
}];
|
|
768
|
-
}
|
|
769
|
-
});
|
|
770
|
-
});
|
|
771
|
-
};
|
|
772
|
-
AWSAppSyncRealTimeProvider.prototype._awsRealTimeApiKeyHeader = function (_a) {
|
|
773
|
-
var apiKey = _a.apiKey, host = _a.host;
|
|
774
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
775
|
-
var dt, dtStr;
|
|
776
|
-
return tslib_1.__generator(this, function (_b) {
|
|
777
|
-
dt = new Date();
|
|
778
|
-
dtStr = dt.toISOString().replace(/[:\-]|\.\d{3}/g, '');
|
|
779
|
-
return [2 /*return*/, {
|
|
780
|
-
host: host,
|
|
781
|
-
'x-amz-date': dtStr,
|
|
782
|
-
'x-api-key': apiKey,
|
|
783
|
-
}];
|
|
784
|
-
});
|
|
785
|
-
});
|
|
786
|
-
};
|
|
787
|
-
AWSAppSyncRealTimeProvider.prototype._awsRealTimeIAMHeader = function (_a) {
|
|
788
|
-
var payload = _a.payload, canonicalUri = _a.canonicalUri, appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, region = _a.region;
|
|
789
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
790
|
-
var endpointInfo, credentialsOK, creds, request, signed_params;
|
|
791
|
-
return tslib_1.__generator(this, function (_b) {
|
|
792
|
-
switch (_b.label) {
|
|
793
|
-
case 0:
|
|
794
|
-
endpointInfo = {
|
|
795
|
-
region: region,
|
|
796
|
-
service: 'appsync',
|
|
797
|
-
};
|
|
798
|
-
return [4 /*yield*/, this._ensureCredentials()];
|
|
799
|
-
case 1:
|
|
800
|
-
credentialsOK = _b.sent();
|
|
801
|
-
if (!credentialsOK) {
|
|
802
|
-
throw new Error('No credentials');
|
|
803
|
-
}
|
|
804
|
-
return [4 /*yield*/, core_1.Credentials.get().then(function (credentials) {
|
|
805
|
-
var _a = credentials, secretAccessKey = _a.secretAccessKey, accessKeyId = _a.accessKeyId, sessionToken = _a.sessionToken;
|
|
806
|
-
return {
|
|
807
|
-
secret_key: secretAccessKey,
|
|
808
|
-
access_key: accessKeyId,
|
|
809
|
-
session_token: sessionToken,
|
|
810
|
-
};
|
|
811
|
-
})];
|
|
812
|
-
case 2:
|
|
813
|
-
creds = _b.sent();
|
|
814
|
-
request = {
|
|
815
|
-
url: "" + appSyncGraphqlEndpoint + canonicalUri,
|
|
816
|
-
data: payload,
|
|
817
|
-
method: 'POST',
|
|
818
|
-
headers: tslib_1.__assign({}, constants_1.AWS_APPSYNC_REALTIME_HEADERS),
|
|
819
|
-
};
|
|
820
|
-
signed_params = core_1.Signer.sign(request, creds, endpointInfo);
|
|
821
|
-
return [2 /*return*/, signed_params.headers];
|
|
822
|
-
}
|
|
823
|
-
});
|
|
824
|
-
});
|
|
825
|
-
};
|
|
826
|
-
AWSAppSyncRealTimeProvider.prototype._customAuthHeader = function (_a) {
|
|
827
|
-
var host = _a.host, additionalHeaders = _a.additionalHeaders;
|
|
828
|
-
if (!additionalHeaders || !additionalHeaders['Authorization']) {
|
|
829
|
-
throw new Error('No auth token specified');
|
|
830
|
-
}
|
|
831
|
-
return {
|
|
832
|
-
Authorization: additionalHeaders.Authorization,
|
|
833
|
-
host: host,
|
|
834
|
-
};
|
|
835
|
-
};
|
|
836
|
-
/**
|
|
837
|
-
* @private
|
|
838
|
-
*/
|
|
839
|
-
AWSAppSyncRealTimeProvider.prototype._ensureCredentials = function () {
|
|
840
|
-
return core_1.Credentials.get()
|
|
841
|
-
.then(function (credentials) {
|
|
842
|
-
if (!credentials)
|
|
843
|
-
return false;
|
|
844
|
-
var cred = core_1.Credentials.shear(credentials);
|
|
845
|
-
logger.debug('set credentials for AWSAppSyncRealTimeProvider', cred);
|
|
846
|
-
return true;
|
|
847
|
-
})
|
|
848
|
-
.catch(function (err) {
|
|
849
|
-
logger.warn('ensure credentials error', err);
|
|
850
|
-
return false;
|
|
851
|
-
});
|
|
852
|
-
};
|
|
853
|
-
return AWSAppSyncRealTimeProvider;
|
|
854
|
-
}(PubSubProvider_1.AbstractPubSubProvider));
|
|
855
|
-
exports.AWSAppSyncRealTimeProvider = AWSAppSyncRealTimeProvider;
|
|
856
|
-
//# sourceMappingURL=index.js.map
|