aws-appsync-subscription-link 3.1.3 → 4.0.0-next.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/__tests__/link/realtime-subscription-handshake-link-test.ts +559 -489
- package/jest.config.js +5 -1
- package/lib/index.d.mts +23 -0
- package/lib/index.d.ts +20 -3
- package/lib/index.js +4884 -44
- package/lib/index.mjs +4869 -0
- package/package.json +21 -6
- package/tsup.config.ts +19 -0
- package/lib/non-terminating-http-link.d.ts +0 -9
- package/lib/non-terminating-http-link.js +0 -32
- package/lib/non-terminating-link.d.ts +0 -15
- package/lib/non-terminating-link.js +0 -101
- package/lib/realtime-subscription-handshake-link.d.ts +0 -37
- package/lib/realtime-subscription-handshake-link.js +0 -795
- package/lib/subscription-handshake-link.d.ts +0 -25
- package/lib/subscription-handshake-link.js +0 -272
- package/lib/types/index.d.ts +0 -87
- package/lib/types/index.js +0 -75
- package/lib/utils/index.d.ts +0 -5
- package/lib/utils/index.js +0 -8
- package/lib/utils/logger.d.ts +0 -5
- package/lib/utils/logger.js +0 -20
- package/lib/utils/retry.d.ts +0 -19
- package/lib/utils/retry.js +0 -130
- package/lib/vendor/paho-mqtt.js +0 -2392
|
@@ -1,795 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
63
|
-
/*!
|
|
64
|
-
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
65
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
66
|
-
*/
|
|
67
|
-
var core_1 = require("@apollo/client/core");
|
|
68
|
-
var utils_1 = require("./utils");
|
|
69
|
-
var aws_appsync_auth_link_1 = require("aws-appsync-auth-link");
|
|
70
|
-
var graphql_1 = require("graphql");
|
|
71
|
-
var url = require("url");
|
|
72
|
-
var uuid_1 = require("uuid");
|
|
73
|
-
var types_1 = require("./types");
|
|
74
|
-
var retry_1 = require("./utils/retry");
|
|
75
|
-
var logger = utils_1.rootLogger.extend("subscriptions");
|
|
76
|
-
exports.CONTROL_EVENTS_KEY = "@@controlEvents";
|
|
77
|
-
var NON_RETRYABLE_CODES = [400, 401, 403];
|
|
78
|
-
var SERVICE = "appsync";
|
|
79
|
-
var APPSYNC_REALTIME_HEADERS = {
|
|
80
|
-
accept: 'application/json, text/javascript',
|
|
81
|
-
'content-encoding': 'amz-1.0',
|
|
82
|
-
'content-type': 'application/json; charset=UTF-8'
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Time in milliseconds to wait for GQL_CONNECTION_INIT message
|
|
86
|
-
*/
|
|
87
|
-
var CONNECTION_INIT_TIMEOUT = 15000;
|
|
88
|
-
/**
|
|
89
|
-
* Time in milliseconds to wait for GQL_START_ACK message
|
|
90
|
-
*/
|
|
91
|
-
var START_ACK_TIMEOUT = 15000;
|
|
92
|
-
/**
|
|
93
|
-
* Frequency in milliseconds in which the server sends GQL_CONNECTION_KEEP_ALIVE messages
|
|
94
|
-
*/
|
|
95
|
-
var SERVER_KEEP_ALIVE_TIMEOUT = 1 * 60 * 1000;
|
|
96
|
-
/**
|
|
97
|
-
* Default Time in milliseconds to wait for GQL_CONNECTION_KEEP_ALIVE message
|
|
98
|
-
*/
|
|
99
|
-
var DEFAULT_KEEP_ALIVE_TIMEOUT = 5 * 60 * 1000;
|
|
100
|
-
var standardDomainPattern = /^https:\/\/\w{26}\.appsync\-api\.\w{2}(?:(?:\-\w{2,})+)\-\d\.amazonaws.com\/graphql$/i;
|
|
101
|
-
var customDomainPath = '/realtime';
|
|
102
|
-
var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super) {
|
|
103
|
-
__extends(AppSyncRealTimeSubscriptionHandshakeLink, _super);
|
|
104
|
-
function AppSyncRealTimeSubscriptionHandshakeLink(_a) {
|
|
105
|
-
var theUrl = _a.url, theRegion = _a.region, theAuth = _a.auth, keepAliveTimeoutMs = _a.keepAliveTimeoutMs;
|
|
106
|
-
var _this = _super.call(this) || this;
|
|
107
|
-
_this.socketStatus = types_1.SOCKET_STATUS.CLOSED;
|
|
108
|
-
_this.keepAliveTimeout = undefined;
|
|
109
|
-
_this.subscriptionObserverMap = new Map();
|
|
110
|
-
_this.promiseArray = [];
|
|
111
|
-
_this.url = theUrl;
|
|
112
|
-
_this.region = theRegion;
|
|
113
|
-
_this.auth = theAuth;
|
|
114
|
-
_this.keepAliveTimeout = keepAliveTimeoutMs;
|
|
115
|
-
if (_this.keepAliveTimeout < SERVER_KEEP_ALIVE_TIMEOUT) {
|
|
116
|
-
var configName = 'keepAliveTimeoutMs';
|
|
117
|
-
throw new Error(configName + " must be greater than or equal to " + SERVER_KEEP_ALIVE_TIMEOUT + " (" + _this.keepAliveTimeout + " used).");
|
|
118
|
-
}
|
|
119
|
-
return _this;
|
|
120
|
-
}
|
|
121
|
-
// Check if url matches standard domain pattern
|
|
122
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype.isCustomDomain = function (url) {
|
|
123
|
-
return url.match(standardDomainPattern) === null;
|
|
124
|
-
};
|
|
125
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype.request = function (operation) {
|
|
126
|
-
var _a;
|
|
127
|
-
var _this = this;
|
|
128
|
-
var query = operation.query, variables = operation.variables;
|
|
129
|
-
var _b = operation.getContext(), _c = _b.controlMessages, _d = exports.CONTROL_EVENTS_KEY, controlEvents = (_c === void 0 ? (_a = {},
|
|
130
|
-
_a[exports.CONTROL_EVENTS_KEY] = undefined,
|
|
131
|
-
_a) : _c)[_d], headers = _b.headers;
|
|
132
|
-
return new core_1.Observable(function (observer) {
|
|
133
|
-
if (!_this.url) {
|
|
134
|
-
observer.error({
|
|
135
|
-
errors: [
|
|
136
|
-
__assign({}, new graphql_1.GraphQLError("Subscribe only available for AWS AppSync endpoint")),
|
|
137
|
-
],
|
|
138
|
-
});
|
|
139
|
-
observer.complete();
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
var subscriptionId_1 = uuid_1.v4();
|
|
143
|
-
var token = _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AMAZON_COGNITO_USER_POOLS ||
|
|
144
|
-
_this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.OPENID_CONNECT
|
|
145
|
-
? _this.auth.jwtToken
|
|
146
|
-
: null;
|
|
147
|
-
token = _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AWS_LAMBDA ? _this.auth.token : token;
|
|
148
|
-
var options = {
|
|
149
|
-
appSyncGraphqlEndpoint: _this.url,
|
|
150
|
-
authenticationType: _this.auth.type,
|
|
151
|
-
query: graphql_1.print(query),
|
|
152
|
-
region: _this.region,
|
|
153
|
-
graphql_headers: function () { return (headers); },
|
|
154
|
-
variables: variables,
|
|
155
|
-
apiKey: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.API_KEY ? _this.auth.apiKey : "",
|
|
156
|
-
credentials: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AWS_IAM ? _this.auth.credentials : null,
|
|
157
|
-
token: token
|
|
158
|
-
};
|
|
159
|
-
_this._startSubscriptionWithAWSAppSyncRealTime({
|
|
160
|
-
options: options,
|
|
161
|
-
observer: observer,
|
|
162
|
-
subscriptionId: subscriptionId_1
|
|
163
|
-
});
|
|
164
|
-
return function () { return __awaiter(_this, void 0, void 0, function () {
|
|
165
|
-
var subscriptionState;
|
|
166
|
-
return __generator(this, function (_a) {
|
|
167
|
-
// Cleanup after unsubscribing or observer.complete was called after _startSubscriptionWithAWSAppSyncRealTime
|
|
168
|
-
try {
|
|
169
|
-
this._verifySubscriptionAlreadyStarted(subscriptionId_1);
|
|
170
|
-
subscriptionState = this.subscriptionObserverMap.get(subscriptionId_1).subscriptionState;
|
|
171
|
-
if (subscriptionState === types_1.SUBSCRIPTION_STATUS.CONNECTED) {
|
|
172
|
-
this._sendUnsubscriptionMessage(subscriptionId_1);
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
throw new Error("Subscription has failed, starting to remove subscription.");
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
catch (err) {
|
|
179
|
-
this._removeSubscriptionObserver(subscriptionId_1);
|
|
180
|
-
return [2 /*return*/];
|
|
181
|
-
}
|
|
182
|
-
return [2 /*return*/];
|
|
183
|
-
});
|
|
184
|
-
}); };
|
|
185
|
-
}
|
|
186
|
-
}).filter(function (data) {
|
|
187
|
-
var _a = data.extensions, _b = (_a === void 0 ? {} : _a).controlMsgType, controlMsgType = _b === void 0 ? undefined : _b;
|
|
188
|
-
var isControlMsg = typeof controlMsgType !== "undefined";
|
|
189
|
-
return controlEvents === true || !isControlMsg;
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._verifySubscriptionAlreadyStarted = function (subscriptionId) {
|
|
193
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
-
var subscriptionState;
|
|
195
|
-
var _this = this;
|
|
196
|
-
return __generator(this, function (_a) {
|
|
197
|
-
subscriptionState = this.subscriptionObserverMap.get(subscriptionId).subscriptionState;
|
|
198
|
-
// This in case unsubscribe is invoked before sending start subscription message
|
|
199
|
-
if (subscriptionState === types_1.SUBSCRIPTION_STATUS.PENDING) {
|
|
200
|
-
return [2 /*return*/, new Promise(function (res, rej) {
|
|
201
|
-
var _a = _this.subscriptionObserverMap.get(subscriptionId), observer = _a.observer, subscriptionState = _a.subscriptionState, variables = _a.variables, query = _a.query;
|
|
202
|
-
_this.subscriptionObserverMap.set(subscriptionId, {
|
|
203
|
-
observer: observer,
|
|
204
|
-
subscriptionState: subscriptionState,
|
|
205
|
-
variables: variables,
|
|
206
|
-
query: query,
|
|
207
|
-
subscriptionReadyCallback: res,
|
|
208
|
-
subscriptionFailedCallback: rej
|
|
209
|
-
});
|
|
210
|
-
})];
|
|
211
|
-
}
|
|
212
|
-
return [2 /*return*/];
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
};
|
|
216
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._sendUnsubscriptionMessage = function (subscriptionId) {
|
|
217
|
-
try {
|
|
218
|
-
if (this.awsRealTimeSocket &&
|
|
219
|
-
this.awsRealTimeSocket.readyState === WebSocket.OPEN &&
|
|
220
|
-
this.socketStatus === types_1.SOCKET_STATUS.READY) {
|
|
221
|
-
// Preparing unsubscribe message to stop receiving messages for that subscription
|
|
222
|
-
var unsubscribeMessage = {
|
|
223
|
-
id: subscriptionId,
|
|
224
|
-
type: types_1.MESSAGE_TYPES.GQL_STOP
|
|
225
|
-
};
|
|
226
|
-
var stringToAWSRealTime = JSON.stringify(unsubscribeMessage);
|
|
227
|
-
this.awsRealTimeSocket.send(stringToAWSRealTime);
|
|
228
|
-
this._removeSubscriptionObserver(subscriptionId);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
catch (err) {
|
|
232
|
-
// If GQL_STOP is not sent because of disconnection issue, then there is nothing the client can do
|
|
233
|
-
logger({ err: err });
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._removeSubscriptionObserver = function (subscriptionId) {
|
|
237
|
-
this.subscriptionObserverMap.delete(subscriptionId);
|
|
238
|
-
// Verifying for 1000ms after removing subscription in case there are new subscriptions on mount / unmount
|
|
239
|
-
setTimeout(this._closeSocketIfRequired.bind(this), 1000);
|
|
240
|
-
};
|
|
241
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._closeSocketIfRequired = function () {
|
|
242
|
-
if (this.subscriptionObserverMap.size > 0) {
|
|
243
|
-
// There are active subscriptions on the WebSocket
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
if (!this.awsRealTimeSocket) {
|
|
247
|
-
this.socketStatus = types_1.SOCKET_STATUS.CLOSED;
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
if (this.awsRealTimeSocket.bufferedAmount > 0) {
|
|
251
|
-
// There is still data on the WebSocket
|
|
252
|
-
setTimeout(this._closeSocketIfRequired.bind(this), 1000);
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
logger("closing WebSocket...");
|
|
256
|
-
clearTimeout(this.keepAliveTimeoutId);
|
|
257
|
-
var tempSocket = this.awsRealTimeSocket;
|
|
258
|
-
tempSocket.close(1000);
|
|
259
|
-
this.awsRealTimeSocket = null;
|
|
260
|
-
this.socketStatus = types_1.SOCKET_STATUS.CLOSED;
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._startSubscriptionWithAWSAppSyncRealTime = function (_a) {
|
|
264
|
-
var options = _a.options, observer = _a.observer, subscriptionId = _a.subscriptionId;
|
|
265
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
266
|
-
var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _b, graphql_headers, credentials, token, subscriptionState, data, dataString, headerObj, _c, subscriptionMessage, stringToAWSRealTime, err_1, _d, message, subscriptionFailedCallback_1, _e, subscriptionFailedCallback, subscriptionReadyCallback;
|
|
267
|
-
var _f;
|
|
268
|
-
var _this = this;
|
|
269
|
-
return __generator(this, function (_g) {
|
|
270
|
-
switch (_g.label) {
|
|
271
|
-
case 0:
|
|
272
|
-
appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint, authenticationType = options.authenticationType, query = options.query, variables = options.variables, apiKey = options.apiKey, region = options.region, _b = options.graphql_headers, graphql_headers = _b === void 0 ? function () { return ({}); } : _b, credentials = options.credentials, token = options.token;
|
|
273
|
-
subscriptionState = types_1.SUBSCRIPTION_STATUS.PENDING;
|
|
274
|
-
data = {
|
|
275
|
-
query: query,
|
|
276
|
-
variables: variables
|
|
277
|
-
};
|
|
278
|
-
// Having a subscription id map will make it simple to forward messages received
|
|
279
|
-
this.subscriptionObserverMap.set(subscriptionId, {
|
|
280
|
-
observer: observer,
|
|
281
|
-
query: query,
|
|
282
|
-
variables: variables,
|
|
283
|
-
subscriptionState: subscriptionState,
|
|
284
|
-
startAckTimeoutId: null,
|
|
285
|
-
});
|
|
286
|
-
dataString = JSON.stringify(data);
|
|
287
|
-
_c = [{}];
|
|
288
|
-
return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
|
|
289
|
-
apiKey: apiKey,
|
|
290
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
291
|
-
authenticationType: authenticationType,
|
|
292
|
-
payload: dataString,
|
|
293
|
-
canonicalUri: "",
|
|
294
|
-
region: region,
|
|
295
|
-
credentials: credentials,
|
|
296
|
-
token: token,
|
|
297
|
-
graphql_headers: graphql_headers
|
|
298
|
-
})];
|
|
299
|
-
case 1:
|
|
300
|
-
headerObj = __assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_g.sent())])), (_f = {}, _f[aws_appsync_auth_link_1.USER_AGENT_HEADER] = aws_appsync_auth_link_1.USER_AGENT, _f)]);
|
|
301
|
-
subscriptionMessage = {
|
|
302
|
-
id: subscriptionId,
|
|
303
|
-
payload: {
|
|
304
|
-
data: dataString,
|
|
305
|
-
extensions: {
|
|
306
|
-
authorization: __assign({}, headerObj)
|
|
307
|
-
}
|
|
308
|
-
},
|
|
309
|
-
type: types_1.MESSAGE_TYPES.GQL_START
|
|
310
|
-
};
|
|
311
|
-
stringToAWSRealTime = JSON.stringify(subscriptionMessage);
|
|
312
|
-
_g.label = 2;
|
|
313
|
-
case 2:
|
|
314
|
-
_g.trys.push([2, 4, , 5]);
|
|
315
|
-
return [4 /*yield*/, this._initializeWebSocketConnection({
|
|
316
|
-
apiKey: apiKey,
|
|
317
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
318
|
-
authenticationType: authenticationType,
|
|
319
|
-
region: region,
|
|
320
|
-
credentials: credentials,
|
|
321
|
-
token: token
|
|
322
|
-
})];
|
|
323
|
-
case 3:
|
|
324
|
-
_g.sent();
|
|
325
|
-
return [3 /*break*/, 5];
|
|
326
|
-
case 4:
|
|
327
|
-
err_1 = _g.sent();
|
|
328
|
-
_d = err_1.message, message = _d === void 0 ? "" : _d;
|
|
329
|
-
observer.error({
|
|
330
|
-
errors: [
|
|
331
|
-
__assign({}, new graphql_1.GraphQLError("Connection failed: " + message))
|
|
332
|
-
]
|
|
333
|
-
});
|
|
334
|
-
observer.complete();
|
|
335
|
-
subscriptionFailedCallback_1 = (this.subscriptionObserverMap.get(subscriptionId) || {}).subscriptionFailedCallback;
|
|
336
|
-
// Notify concurrent unsubscription
|
|
337
|
-
if (typeof subscriptionFailedCallback_1 === "function") {
|
|
338
|
-
subscriptionFailedCallback_1();
|
|
339
|
-
}
|
|
340
|
-
return [2 /*return*/];
|
|
341
|
-
case 5:
|
|
342
|
-
_e = this.subscriptionObserverMap.get(subscriptionId) || {}, subscriptionFailedCallback = _e.subscriptionFailedCallback, subscriptionReadyCallback = _e.subscriptionReadyCallback;
|
|
343
|
-
// This must be done before sending the message in order to be listening immediately
|
|
344
|
-
this.subscriptionObserverMap.set(subscriptionId, {
|
|
345
|
-
observer: observer,
|
|
346
|
-
subscriptionState: subscriptionState,
|
|
347
|
-
variables: variables,
|
|
348
|
-
query: query,
|
|
349
|
-
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
350
|
-
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
351
|
-
startAckTimeoutId: setTimeout(function () {
|
|
352
|
-
_this._timeoutStartSubscriptionAck.call(_this, subscriptionId);
|
|
353
|
-
}, START_ACK_TIMEOUT)
|
|
354
|
-
});
|
|
355
|
-
if (this.awsRealTimeSocket) {
|
|
356
|
-
this.awsRealTimeSocket.send(stringToAWSRealTime);
|
|
357
|
-
}
|
|
358
|
-
return [2 /*return*/];
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
};
|
|
363
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._initializeWebSocketConnection = function (_a) {
|
|
364
|
-
var _this = this;
|
|
365
|
-
var appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, authenticationType = _a.authenticationType, apiKey = _a.apiKey, region = _a.region, credentials = _a.credentials, token = _a.token;
|
|
366
|
-
if (this.socketStatus === types_1.SOCKET_STATUS.READY) {
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
369
|
-
return new Promise(function (res, rej) { return __awaiter(_this, void 0, void 0, function () {
|
|
370
|
-
var payloadString, headerString, _a, _b, headerQs, payloadQs, discoverableEndpoint, awsRealTimeUrl, err_2;
|
|
371
|
-
return __generator(this, function (_c) {
|
|
372
|
-
switch (_c.label) {
|
|
373
|
-
case 0:
|
|
374
|
-
this.promiseArray.push({ res: res, rej: rej });
|
|
375
|
-
if (!(this.socketStatus === types_1.SOCKET_STATUS.CLOSED)) return [3 /*break*/, 5];
|
|
376
|
-
_c.label = 1;
|
|
377
|
-
case 1:
|
|
378
|
-
_c.trys.push([1, 4, , 5]);
|
|
379
|
-
this.socketStatus = types_1.SOCKET_STATUS.CONNECTING;
|
|
380
|
-
payloadString = "{}";
|
|
381
|
-
_b = (_a = JSON).stringify;
|
|
382
|
-
return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
|
|
383
|
-
authenticationType: authenticationType,
|
|
384
|
-
payload: payloadString,
|
|
385
|
-
canonicalUri: "/connect",
|
|
386
|
-
apiKey: apiKey,
|
|
387
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
388
|
-
region: region,
|
|
389
|
-
credentials: credentials,
|
|
390
|
-
token: token,
|
|
391
|
-
graphql_headers: function () { }
|
|
392
|
-
})];
|
|
393
|
-
case 2:
|
|
394
|
-
headerString = _b.apply(_a, [_c.sent()]);
|
|
395
|
-
headerQs = Buffer.from(headerString).toString("base64");
|
|
396
|
-
payloadQs = Buffer.from(payloadString).toString("base64");
|
|
397
|
-
discoverableEndpoint = appSyncGraphqlEndpoint;
|
|
398
|
-
if (this.isCustomDomain(discoverableEndpoint)) {
|
|
399
|
-
discoverableEndpoint = discoverableEndpoint.concat(customDomainPath);
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
discoverableEndpoint = discoverableEndpoint.replace('appsync-api', 'appsync-realtime-api').replace('gogi-beta', 'grt-beta');
|
|
403
|
-
}
|
|
404
|
-
discoverableEndpoint = discoverableEndpoint
|
|
405
|
-
.replace("https://", "wss://")
|
|
406
|
-
.replace('http://', 'ws://');
|
|
407
|
-
awsRealTimeUrl = discoverableEndpoint + "?header=" + headerQs + "&payload=" + payloadQs;
|
|
408
|
-
return [4 /*yield*/, this._initializeRetryableHandshake({ awsRealTimeUrl: awsRealTimeUrl })];
|
|
409
|
-
case 3:
|
|
410
|
-
_c.sent();
|
|
411
|
-
this.promiseArray.forEach(function (_a) {
|
|
412
|
-
var res = _a.res;
|
|
413
|
-
logger("Notifying connection successful");
|
|
414
|
-
res();
|
|
415
|
-
});
|
|
416
|
-
this.socketStatus = types_1.SOCKET_STATUS.READY;
|
|
417
|
-
this.promiseArray = [];
|
|
418
|
-
return [3 /*break*/, 5];
|
|
419
|
-
case 4:
|
|
420
|
-
err_2 = _c.sent();
|
|
421
|
-
this.promiseArray.forEach(function (_a) {
|
|
422
|
-
var rej = _a.rej;
|
|
423
|
-
return rej(err_2);
|
|
424
|
-
});
|
|
425
|
-
this.promiseArray = [];
|
|
426
|
-
if (this.awsRealTimeSocket &&
|
|
427
|
-
this.awsRealTimeSocket.readyState === WebSocket.OPEN) {
|
|
428
|
-
this.awsRealTimeSocket.close(3001);
|
|
429
|
-
}
|
|
430
|
-
this.awsRealTimeSocket = null;
|
|
431
|
-
this.socketStatus = types_1.SOCKET_STATUS.CLOSED;
|
|
432
|
-
return [3 /*break*/, 5];
|
|
433
|
-
case 5: return [2 /*return*/];
|
|
434
|
-
}
|
|
435
|
-
});
|
|
436
|
-
}); });
|
|
437
|
-
};
|
|
438
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._awsRealTimeHeaderBasedAuth = function (_a) {
|
|
439
|
-
var authenticationType = _a.authenticationType, payload = _a.payload, canonicalUri = _a.canonicalUri, appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, apiKey = _a.apiKey, region = _a.region, credentials = _a.credentials, token = _a.token, graphql_headers = _a.graphql_headers;
|
|
440
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
441
|
-
var headerHandler, handler, host, result;
|
|
442
|
-
return __generator(this, function (_b) {
|
|
443
|
-
switch (_b.label) {
|
|
444
|
-
case 0:
|
|
445
|
-
headerHandler = {
|
|
446
|
-
API_KEY: this._awsRealTimeApiKeyHeader.bind(this),
|
|
447
|
-
AWS_IAM: this._awsRealTimeIAMHeader.bind(this),
|
|
448
|
-
OPENID_CONNECT: this._awsRealTimeAuthorizationHeader.bind(this),
|
|
449
|
-
AMAZON_COGNITO_USER_POOLS: this._awsRealTimeAuthorizationHeader.bind(this),
|
|
450
|
-
AWS_LAMBDA: this._awsRealTimeAuthorizationHeader.bind(this)
|
|
451
|
-
};
|
|
452
|
-
handler = headerHandler[authenticationType];
|
|
453
|
-
if (typeof handler !== "function") {
|
|
454
|
-
logger("Authentication type " + authenticationType + " not supported");
|
|
455
|
-
return [2 /*return*/, {}];
|
|
456
|
-
}
|
|
457
|
-
host = url.parse(appSyncGraphqlEndpoint).host;
|
|
458
|
-
return [4 /*yield*/, handler({
|
|
459
|
-
payload: payload,
|
|
460
|
-
canonicalUri: canonicalUri,
|
|
461
|
-
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
462
|
-
apiKey: apiKey,
|
|
463
|
-
region: region,
|
|
464
|
-
host: host,
|
|
465
|
-
credentials: credentials,
|
|
466
|
-
token: token,
|
|
467
|
-
graphql_headers: graphql_headers
|
|
468
|
-
})];
|
|
469
|
-
case 1:
|
|
470
|
-
result = _b.sent();
|
|
471
|
-
return [2 /*return*/, result];
|
|
472
|
-
}
|
|
473
|
-
});
|
|
474
|
-
});
|
|
475
|
-
};
|
|
476
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._awsRealTimeAuthorizationHeader = function (_a) {
|
|
477
|
-
var host = _a.host, token = _a.token, graphql_headers = _a.graphql_headers;
|
|
478
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
479
|
-
var _b, _c, _d;
|
|
480
|
-
return __generator(this, function (_e) {
|
|
481
|
-
switch (_e.label) {
|
|
482
|
-
case 0:
|
|
483
|
-
_b = {};
|
|
484
|
-
if (!(typeof token === "function")) return [3 /*break*/, 2];
|
|
485
|
-
return [4 /*yield*/, token.call(undefined)];
|
|
486
|
-
case 1:
|
|
487
|
-
_c = _e.sent();
|
|
488
|
-
return [3 /*break*/, 4];
|
|
489
|
-
case 2: return [4 /*yield*/, token];
|
|
490
|
-
case 3:
|
|
491
|
-
_c = _e.sent();
|
|
492
|
-
_e.label = 4;
|
|
493
|
-
case 4:
|
|
494
|
-
_d = [(_b.Authorization = _c, _b.host = host, _b)];
|
|
495
|
-
return [4 /*yield*/, graphql_headers()];
|
|
496
|
-
case 5: return [2 /*return*/, __assign.apply(void 0, _d.concat([(_e.sent())]))];
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
});
|
|
500
|
-
};
|
|
501
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._awsRealTimeApiKeyHeader = function (_a) {
|
|
502
|
-
var apiKey = _a.apiKey, host = _a.host, graphql_headers = _a.graphql_headers;
|
|
503
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
504
|
-
var dt, dtStr, _b;
|
|
505
|
-
return __generator(this, function (_c) {
|
|
506
|
-
switch (_c.label) {
|
|
507
|
-
case 0:
|
|
508
|
-
dt = new Date();
|
|
509
|
-
dtStr = dt.toISOString().replace(/[:\-]|\.\d{3}/g, "");
|
|
510
|
-
_b = [{ host: host, "x-amz-date": dtStr, "x-api-key": apiKey }];
|
|
511
|
-
return [4 /*yield*/, graphql_headers()];
|
|
512
|
-
case 1: return [2 /*return*/, __assign.apply(void 0, _b.concat([(_c.sent())]))];
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
});
|
|
516
|
-
};
|
|
517
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._awsRealTimeIAMHeader = function (_a) {
|
|
518
|
-
var payload = _a.payload, canonicalUri = _a.canonicalUri, appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, region = _a.region, credentials = _a.credentials;
|
|
519
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
520
|
-
var endpointInfo, creds, _b, accessKeyId, secretAccessKey, sessionToken, formattedCredentials, request, signed_params;
|
|
521
|
-
return __generator(this, function (_c) {
|
|
522
|
-
switch (_c.label) {
|
|
523
|
-
case 0:
|
|
524
|
-
endpointInfo = {
|
|
525
|
-
region: region,
|
|
526
|
-
service: SERVICE
|
|
527
|
-
};
|
|
528
|
-
creds = typeof credentials === "function"
|
|
529
|
-
? credentials.call()
|
|
530
|
-
: credentials || {};
|
|
531
|
-
if (!(creds && typeof creds.getPromise === "function")) return [3 /*break*/, 2];
|
|
532
|
-
return [4 /*yield*/, creds.getPromise()];
|
|
533
|
-
case 1:
|
|
534
|
-
_c.sent();
|
|
535
|
-
_c.label = 2;
|
|
536
|
-
case 2:
|
|
537
|
-
if (!creds) {
|
|
538
|
-
throw new Error("No credentials");
|
|
539
|
-
}
|
|
540
|
-
return [4 /*yield*/, creds];
|
|
541
|
-
case 3:
|
|
542
|
-
_b = _c.sent(), accessKeyId = _b.accessKeyId, secretAccessKey = _b.secretAccessKey, sessionToken = _b.sessionToken;
|
|
543
|
-
formattedCredentials = {
|
|
544
|
-
access_key: accessKeyId,
|
|
545
|
-
secret_key: secretAccessKey,
|
|
546
|
-
session_token: sessionToken
|
|
547
|
-
};
|
|
548
|
-
request = {
|
|
549
|
-
url: "" + appSyncGraphqlEndpoint + canonicalUri,
|
|
550
|
-
body: payload,
|
|
551
|
-
method: "POST",
|
|
552
|
-
headers: __assign({}, APPSYNC_REALTIME_HEADERS)
|
|
553
|
-
};
|
|
554
|
-
signed_params = aws_appsync_auth_link_1.Signer.sign(request, formattedCredentials, endpointInfo);
|
|
555
|
-
return [2 /*return*/, signed_params.headers];
|
|
556
|
-
}
|
|
557
|
-
});
|
|
558
|
-
});
|
|
559
|
-
};
|
|
560
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._initializeRetryableHandshake = function (_a) {
|
|
561
|
-
var awsRealTimeUrl = _a.awsRealTimeUrl;
|
|
562
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
563
|
-
return __generator(this, function (_b) {
|
|
564
|
-
switch (_b.label) {
|
|
565
|
-
case 0:
|
|
566
|
-
logger("Initializaling retryable Handshake");
|
|
567
|
-
return [4 /*yield*/, retry_1.jitteredExponentialRetry(this._initializeHandshake.bind(this), [
|
|
568
|
-
{ awsRealTimeUrl: awsRealTimeUrl }
|
|
569
|
-
])];
|
|
570
|
-
case 1:
|
|
571
|
-
_b.sent();
|
|
572
|
-
return [2 /*return*/];
|
|
573
|
-
}
|
|
574
|
-
});
|
|
575
|
-
});
|
|
576
|
-
};
|
|
577
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._initializeHandshake = function (_a) {
|
|
578
|
-
var awsRealTimeUrl = _a.awsRealTimeUrl;
|
|
579
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
580
|
-
var err_3, errorType, errorCode;
|
|
581
|
-
var _this = this;
|
|
582
|
-
return __generator(this, function (_b) {
|
|
583
|
-
switch (_b.label) {
|
|
584
|
-
case 0:
|
|
585
|
-
logger("Initializing handshake " + awsRealTimeUrl);
|
|
586
|
-
_b.label = 1;
|
|
587
|
-
case 1:
|
|
588
|
-
_b.trys.push([1, 4, , 5]);
|
|
589
|
-
return [4 /*yield*/, (function () {
|
|
590
|
-
return new Promise(function (res, rej) {
|
|
591
|
-
var newSocket = AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket(awsRealTimeUrl, "graphql-ws");
|
|
592
|
-
newSocket.onerror = function () {
|
|
593
|
-
logger("WebSocket connection error");
|
|
594
|
-
};
|
|
595
|
-
newSocket.onclose = function () {
|
|
596
|
-
rej(new Error("Connection handshake error"));
|
|
597
|
-
};
|
|
598
|
-
newSocket.onopen = function () {
|
|
599
|
-
_this.awsRealTimeSocket = newSocket;
|
|
600
|
-
return res();
|
|
601
|
-
};
|
|
602
|
-
});
|
|
603
|
-
})()];
|
|
604
|
-
case 2:
|
|
605
|
-
_b.sent();
|
|
606
|
-
// Step 2: wait for ack from AWS AppSyncReaTime after sending init
|
|
607
|
-
return [4 /*yield*/, (function () {
|
|
608
|
-
return new Promise(function (res, rej) {
|
|
609
|
-
var ackOk = false;
|
|
610
|
-
_this.awsRealTimeSocket.onerror = function (error) {
|
|
611
|
-
logger("WebSocket closed " + JSON.stringify(error));
|
|
612
|
-
};
|
|
613
|
-
_this.awsRealTimeSocket.onclose = function (event) {
|
|
614
|
-
logger("WebSocket closed " + event.reason);
|
|
615
|
-
rej(new Error(JSON.stringify(event)));
|
|
616
|
-
};
|
|
617
|
-
_this.awsRealTimeSocket.onmessage = function (message) {
|
|
618
|
-
var _a;
|
|
619
|
-
logger("subscription message from AWS AppSyncRealTime: " + message.data + " ");
|
|
620
|
-
var data = JSON.parse(message.data);
|
|
621
|
-
var type = data.type, _b = data.payload, _c = (_b === void 0 ? {} : _b).connectionTimeoutMs, connectionTimeoutMs = _c === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _c;
|
|
622
|
-
if (type === types_1.MESSAGE_TYPES.GQL_CONNECTION_ACK) {
|
|
623
|
-
ackOk = true;
|
|
624
|
-
_this.keepAliveTimeout = (_a = _this.keepAliveTimeout) !== null && _a !== void 0 ? _a : connectionTimeoutMs;
|
|
625
|
-
_this.awsRealTimeSocket.onmessage = _this._handleIncomingSubscriptionMessage.bind(_this);
|
|
626
|
-
_this.awsRealTimeSocket.onerror = function (err) {
|
|
627
|
-
logger(err);
|
|
628
|
-
_this._errorDisconnect(types_1.CONTROL_MSG.CONNECTION_CLOSED);
|
|
629
|
-
};
|
|
630
|
-
_this.awsRealTimeSocket.onclose = function (event) {
|
|
631
|
-
logger("WebSocket closed " + event.reason);
|
|
632
|
-
_this._errorDisconnect(types_1.CONTROL_MSG.CONNECTION_CLOSED);
|
|
633
|
-
};
|
|
634
|
-
res("Cool, connected to AWS AppSyncRealTime");
|
|
635
|
-
return;
|
|
636
|
-
}
|
|
637
|
-
if (type === types_1.MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
|
|
638
|
-
var _d = data.payload, _e = (_d === void 0 ? {} : _d).errors, _f = (_e === void 0 ? [] : _e)[0], _g = _f === void 0 ? {} : _f, _h = _g.errorType, errorType = _h === void 0 ? "" : _h, _j = _g.errorCode, errorCode = _j === void 0 ? 0 : _j;
|
|
639
|
-
rej({ errorType: errorType, errorCode: errorCode });
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
var gqlInit = {
|
|
643
|
-
type: types_1.MESSAGE_TYPES.GQL_CONNECTION_INIT
|
|
644
|
-
};
|
|
645
|
-
_this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
|
|
646
|
-
function checkAckOk() {
|
|
647
|
-
if (!ackOk) {
|
|
648
|
-
rej(new Error("Connection timeout: ack from AWSRealTime was not received on " + CONNECTION_INIT_TIMEOUT + " ms"));
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
setTimeout(checkAckOk.bind(_this), CONNECTION_INIT_TIMEOUT);
|
|
652
|
-
});
|
|
653
|
-
})()];
|
|
654
|
-
case 3:
|
|
655
|
-
// Step 2: wait for ack from AWS AppSyncReaTime after sending init
|
|
656
|
-
_b.sent();
|
|
657
|
-
return [3 /*break*/, 5];
|
|
658
|
-
case 4:
|
|
659
|
-
err_3 = _b.sent();
|
|
660
|
-
errorType = err_3.errorType, errorCode = err_3.errorCode;
|
|
661
|
-
if (NON_RETRYABLE_CODES.indexOf(errorCode) >= 0) {
|
|
662
|
-
throw new retry_1.NonRetryableError(errorType);
|
|
663
|
-
}
|
|
664
|
-
else if (errorType) {
|
|
665
|
-
throw new Error(errorType);
|
|
666
|
-
}
|
|
667
|
-
else {
|
|
668
|
-
throw err_3;
|
|
669
|
-
}
|
|
670
|
-
return [3 /*break*/, 5];
|
|
671
|
-
case 5: return [2 /*return*/];
|
|
672
|
-
}
|
|
673
|
-
});
|
|
674
|
-
});
|
|
675
|
-
};
|
|
676
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._handleIncomingSubscriptionMessage = function (message) {
|
|
677
|
-
logger("subscription message from AWS AppSync RealTime: " + message.data);
|
|
678
|
-
var _a = JSON.parse(message.data), _b = _a.id, id = _b === void 0 ? "" : _b, payload = _a.payload, type = _a.type;
|
|
679
|
-
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, _g = _c.startAckTimeoutId, startAckTimeoutId = _g === void 0 ? 0 : _g, _h = _c.subscriptionReadyCallback, subscriptionReadyCallback = _h === void 0 ? null : _h, _j = _c.subscriptionFailedCallback, subscriptionFailedCallback = _j === void 0 ? null : _j;
|
|
680
|
-
logger({ id: id, observer: observer, query: query, variables: variables });
|
|
681
|
-
if (type === types_1.MESSAGE_TYPES.GQL_DATA && payload && payload.data) {
|
|
682
|
-
if (observer) {
|
|
683
|
-
observer.next(payload);
|
|
684
|
-
}
|
|
685
|
-
else {
|
|
686
|
-
logger("observer not found for id: " + id);
|
|
687
|
-
}
|
|
688
|
-
return;
|
|
689
|
-
}
|
|
690
|
-
if (type === types_1.MESSAGE_TYPES.GQL_START_ACK) {
|
|
691
|
-
logger("subscription ready for " + JSON.stringify({ query: query, variables: variables }));
|
|
692
|
-
if (typeof subscriptionReadyCallback === "function") {
|
|
693
|
-
subscriptionReadyCallback();
|
|
694
|
-
}
|
|
695
|
-
clearTimeout(startAckTimeoutId);
|
|
696
|
-
if (observer) {
|
|
697
|
-
observer.next({
|
|
698
|
-
data: payload,
|
|
699
|
-
extensions: {
|
|
700
|
-
controlMsgType: "CONNECTED"
|
|
701
|
-
}
|
|
702
|
-
});
|
|
703
|
-
}
|
|
704
|
-
else {
|
|
705
|
-
logger("observer not found for id: " + id);
|
|
706
|
-
}
|
|
707
|
-
var subscriptionState = types_1.SUBSCRIPTION_STATUS.CONNECTED;
|
|
708
|
-
this.subscriptionObserverMap.set(id, {
|
|
709
|
-
observer: observer,
|
|
710
|
-
query: query,
|
|
711
|
-
variables: variables,
|
|
712
|
-
startAckTimeoutId: null,
|
|
713
|
-
subscriptionState: subscriptionState,
|
|
714
|
-
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
715
|
-
subscriptionFailedCallback: subscriptionFailedCallback
|
|
716
|
-
});
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
if (type === types_1.MESSAGE_TYPES.GQL_CONNECTION_KEEP_ALIVE) {
|
|
720
|
-
clearTimeout(this.keepAliveTimeoutId);
|
|
721
|
-
this.keepAliveTimeoutId = setTimeout(this._errorDisconnect.bind(this, types_1.CONTROL_MSG.TIMEOUT_DISCONNECT), this.keepAliveTimeout);
|
|
722
|
-
return;
|
|
723
|
-
}
|
|
724
|
-
if (type === types_1.MESSAGE_TYPES.GQL_ERROR) {
|
|
725
|
-
var subscriptionState = types_1.SUBSCRIPTION_STATUS.FAILED;
|
|
726
|
-
this.subscriptionObserverMap.set(id, {
|
|
727
|
-
observer: observer,
|
|
728
|
-
query: query,
|
|
729
|
-
variables: variables,
|
|
730
|
-
startAckTimeoutId: startAckTimeoutId,
|
|
731
|
-
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
732
|
-
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
733
|
-
subscriptionState: subscriptionState
|
|
734
|
-
});
|
|
735
|
-
clearTimeout(startAckTimeoutId);
|
|
736
|
-
if (observer) {
|
|
737
|
-
observer.error({
|
|
738
|
-
errors: [
|
|
739
|
-
__assign({}, new graphql_1.GraphQLError("Connection failed: " + JSON.stringify(payload)))
|
|
740
|
-
]
|
|
741
|
-
});
|
|
742
|
-
observer.complete();
|
|
743
|
-
}
|
|
744
|
-
else {
|
|
745
|
-
logger("observer not found for id: " + id);
|
|
746
|
-
}
|
|
747
|
-
if (typeof subscriptionFailedCallback === "function") {
|
|
748
|
-
subscriptionFailedCallback();
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
};
|
|
752
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._errorDisconnect = function (msg) {
|
|
753
|
-
logger("Disconnect error: " + msg);
|
|
754
|
-
this.subscriptionObserverMap.forEach(function (_a) {
|
|
755
|
-
var observer = _a.observer;
|
|
756
|
-
if (observer && !observer.closed) {
|
|
757
|
-
observer.error({
|
|
758
|
-
errors: [__assign({}, new graphql_1.GraphQLError(msg))],
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
});
|
|
762
|
-
this.subscriptionObserverMap.clear();
|
|
763
|
-
if (this.awsRealTimeSocket) {
|
|
764
|
-
this.awsRealTimeSocket.close();
|
|
765
|
-
}
|
|
766
|
-
this.socketStatus = types_1.SOCKET_STATUS.CLOSED;
|
|
767
|
-
};
|
|
768
|
-
AppSyncRealTimeSubscriptionHandshakeLink.prototype._timeoutStartSubscriptionAck = function (subscriptionId) {
|
|
769
|
-
var _a = this.subscriptionObserverMap.get(subscriptionId) || {}, observer = _a.observer, query = _a.query, variables = _a.variables;
|
|
770
|
-
if (!observer) {
|
|
771
|
-
return;
|
|
772
|
-
}
|
|
773
|
-
this.subscriptionObserverMap.set(subscriptionId, {
|
|
774
|
-
observer: observer,
|
|
775
|
-
query: query,
|
|
776
|
-
variables: variables,
|
|
777
|
-
subscriptionState: types_1.SUBSCRIPTION_STATUS.FAILED
|
|
778
|
-
});
|
|
779
|
-
if (observer && !observer.closed) {
|
|
780
|
-
observer.error({
|
|
781
|
-
errors: [
|
|
782
|
-
__assign({}, new graphql_1.GraphQLError("Subscription timeout " + JSON.stringify({ query: query, variables: variables })))
|
|
783
|
-
]
|
|
784
|
-
});
|
|
785
|
-
// Cleanup will be automatically executed
|
|
786
|
-
observer.complete();
|
|
787
|
-
}
|
|
788
|
-
logger("timeoutStartSubscription", JSON.stringify({ query: query, variables: variables }));
|
|
789
|
-
};
|
|
790
|
-
AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = function (awsRealTimeUrl, protocol) {
|
|
791
|
-
return new WebSocket(awsRealTimeUrl, protocol);
|
|
792
|
-
};
|
|
793
|
-
return AppSyncRealTimeSubscriptionHandshakeLink;
|
|
794
|
-
}(core_1.ApolloLink));
|
|
795
|
-
exports.AppSyncRealTimeSubscriptionHandshakeLink = AppSyncRealTimeSubscriptionHandshakeLink;
|