aws-appsync-subscription-link 3.1.3 → 3.1.5
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/lib/index.js +5 -4
- package/lib/non-terminating-http-link.js +6 -5
- package/lib/non-terminating-link.js +6 -3
- package/lib/realtime-subscription-handshake-link.d.ts +1 -1
- package/lib/realtime-subscription-handshake-link.js +86 -56
- package/lib/subscription-handshake-link.d.ts +1 -1
- package/lib/subscription-handshake-link.js +9 -6
- package/lib/types/index.d.ts +4 -4
- package/lib/types/index.js +1 -0
- package/lib/utils/index.js +2 -1
- package/lib/utils/logger.d.ts +1 -1
- package/lib/utils/logger.js +11 -9
- package/lib/utils/retry.js +9 -5
- package/package.json +7 -3
- package/CHANGELOG.md +0 -187
- package/__tests__/link/realtime-subscription-handshake-link-test.ts +0 -525
- package/jest.config.js +0 -19
- package/lib/vendor/vendor/paho-mqtt.js +0 -2392
package/lib/index.js
CHANGED
|
@@ -11,8 +11,9 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.createSubscriptionHandshakeLink = exports.CONTROL_EVENTS_KEY = void 0;
|
|
14
15
|
var subscription_handshake_link_1 = require("./subscription-handshake-link");
|
|
15
|
-
exports
|
|
16
|
+
Object.defineProperty(exports, "CONTROL_EVENTS_KEY", { enumerable: true, get: function () { return subscription_handshake_link_1.CONTROL_EVENTS_KEY; } });
|
|
16
17
|
var core_1 = require("@apollo/client/core");
|
|
17
18
|
var http_1 = require("@apollo/client/link/http");
|
|
18
19
|
var utilities_1 = require("@apollo/client/utilities");
|
|
@@ -22,7 +23,7 @@ function createSubscriptionHandshakeLink(infoOrUrl, theResultsFetcherLink) {
|
|
|
22
23
|
var resultsFetcherLink, subscriptionLinks;
|
|
23
24
|
if (typeof infoOrUrl === "string") {
|
|
24
25
|
resultsFetcherLink =
|
|
25
|
-
theResultsFetcherLink || http_1.createHttpLink({ uri: infoOrUrl });
|
|
26
|
+
theResultsFetcherLink || (0, http_1.createHttpLink)({ uri: infoOrUrl });
|
|
26
27
|
subscriptionLinks = core_1.ApolloLink.from([
|
|
27
28
|
new non_terminating_link_1.NonTerminatingLink("controlMessages", {
|
|
28
29
|
link: new core_1.ApolloLink(function (operation, _forward) {
|
|
@@ -43,12 +44,12 @@ function createSubscriptionHandshakeLink(infoOrUrl, theResultsFetcherLink) {
|
|
|
43
44
|
}
|
|
44
45
|
else {
|
|
45
46
|
var url = infoOrUrl.url;
|
|
46
|
-
resultsFetcherLink = theResultsFetcherLink || http_1.createHttpLink({ uri: url });
|
|
47
|
+
resultsFetcherLink = theResultsFetcherLink || (0, http_1.createHttpLink)({ uri: url });
|
|
47
48
|
subscriptionLinks = new realtime_subscription_handshake_link_1.AppSyncRealTimeSubscriptionHandshakeLink(infoOrUrl);
|
|
48
49
|
}
|
|
49
50
|
return core_1.ApolloLink.split(function (operation) {
|
|
50
51
|
var query = operation.query;
|
|
51
|
-
var _a = utilities_1.getMainDefinition(query), kind = _a.kind, graphqlOperation = _a.operation;
|
|
52
|
+
var _a = (0, utilities_1.getMainDefinition)(query), kind = _a.kind, graphqlOperation = _a.operation;
|
|
52
53
|
var isSubscription = kind === "OperationDefinition" && graphqlOperation === "subscription";
|
|
53
54
|
return isSubscription;
|
|
54
55
|
}, subscriptionLinks, resultsFetcherLink);
|
|
@@ -3,16 +3,19 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
3
3
|
var extendStatics = function (d, b) {
|
|
4
4
|
extendStatics = Object.setPrototypeOf ||
|
|
5
5
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
15
|
};
|
|
14
16
|
})();
|
|
15
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.NonTerminatingHttpLink = void 0;
|
|
16
19
|
/*!
|
|
17
20
|
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
18
21
|
* SPDX-License-Identifier: Apache-2.0
|
|
@@ -22,10 +25,8 @@ var non_terminating_link_1 = require("./non-terminating-link");
|
|
|
22
25
|
var NonTerminatingHttpLink = /** @class */ (function (_super) {
|
|
23
26
|
__extends(NonTerminatingHttpLink, _super);
|
|
24
27
|
function NonTerminatingHttpLink(contextKey, options) {
|
|
25
|
-
var
|
|
26
|
-
|
|
27
|
-
_this = _super.call(this, contextKey, { link: link }) || this;
|
|
28
|
-
return _this;
|
|
28
|
+
var link = (0, http_1.createHttpLink)(options);
|
|
29
|
+
return _super.call(this, contextKey, { link: link }) || this;
|
|
29
30
|
}
|
|
30
31
|
return NonTerminatingHttpLink;
|
|
31
32
|
}(non_terminating_link_1.NonTerminatingLink));
|
|
@@ -3,10 +3,12 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
3
3
|
var extendStatics = function (d, b) {
|
|
4
4
|
extendStatics = Object.setPrototypeOf ||
|
|
5
5
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
@@ -38,7 +40,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
40
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
39
41
|
function step(op) {
|
|
40
42
|
if (f) throw new TypeError("Generator is already executing.");
|
|
41
|
-
while (_) try {
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
42
44
|
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
45
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
44
46
|
switch (op[0]) {
|
|
@@ -60,6 +62,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
60
62
|
}
|
|
61
63
|
};
|
|
62
64
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
+
exports.NonTerminatingLink = void 0;
|
|
63
66
|
/*!
|
|
64
67
|
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
65
68
|
* SPDX-License-Identifier: Apache-2.0
|
|
@@ -77,7 +80,7 @@ var NonTerminatingLink = /** @class */ (function (_super) {
|
|
|
77
80
|
}
|
|
78
81
|
NonTerminatingLink.prototype.request = function (operation, forward) {
|
|
79
82
|
var _this = this;
|
|
80
|
-
return (context_1.setContext(function (_request, prevContext) { return __awaiter(_this, void 0, void 0, function () {
|
|
83
|
+
return ((0, context_1.setContext)(function (_request, prevContext) { return __awaiter(_this, void 0, void 0, function () {
|
|
81
84
|
var result;
|
|
82
85
|
var _a;
|
|
83
86
|
var _this = this;
|
|
@@ -33,5 +33,5 @@ export declare class AppSyncRealTimeSubscriptionHandshakeLink extends ApolloLink
|
|
|
33
33
|
private _handleIncomingSubscriptionMessage;
|
|
34
34
|
private _errorDisconnect;
|
|
35
35
|
private _timeoutStartSubscriptionAck;
|
|
36
|
-
static createWebSocket(awsRealTimeUrl: string, protocol: string): WebSocket;
|
|
36
|
+
static createWebSocket(awsRealTimeUrl: string, protocol: string | string[]): WebSocket;
|
|
37
37
|
}
|
|
@@ -3,10 +3,12 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
3
3
|
var extendStatics = function (d, b) {
|
|
4
4
|
extendStatics = Object.setPrototypeOf ||
|
|
5
5
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
@@ -38,7 +40,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
40
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
39
41
|
function step(op) {
|
|
40
42
|
if (f) throw new TypeError("Generator is already executing.");
|
|
41
|
-
while (_) try {
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
42
44
|
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
45
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
44
46
|
switch (op[0]) {
|
|
@@ -60,6 +62,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
60
62
|
}
|
|
61
63
|
};
|
|
62
64
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
+
exports.AppSyncRealTimeSubscriptionHandshakeLink = exports.CONTROL_EVENTS_KEY = void 0;
|
|
63
66
|
/*!
|
|
64
67
|
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
65
68
|
* SPDX-License-Identifier: Apache-2.0
|
|
@@ -112,9 +115,13 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
112
115
|
_this.region = theRegion;
|
|
113
116
|
_this.auth = theAuth;
|
|
114
117
|
_this.keepAliveTimeout = keepAliveTimeoutMs;
|
|
118
|
+
// Warn about non-HTTPS URLs which would send credentials over unencrypted connections
|
|
119
|
+
if (_this.url && !_this.url.startsWith("https://")) {
|
|
120
|
+
logger("WARNING: AppSync endpoint URL is not using HTTPS. Credentials may be sent unencrypted: ".concat(_this.url));
|
|
121
|
+
}
|
|
115
122
|
if (_this.keepAliveTimeout < SERVER_KEEP_ALIVE_TIMEOUT) {
|
|
116
123
|
var configName = 'keepAliveTimeoutMs';
|
|
117
|
-
throw new Error(configName
|
|
124
|
+
throw new Error("".concat(configName, " must be greater than or equal to ").concat(SERVER_KEEP_ALIVE_TIMEOUT, " (").concat(_this.keepAliveTimeout, " used)."));
|
|
118
125
|
}
|
|
119
126
|
return _this;
|
|
120
127
|
}
|
|
@@ -126,9 +133,9 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
126
133
|
var _a;
|
|
127
134
|
var _this = this;
|
|
128
135
|
var query = operation.query, variables = operation.variables;
|
|
129
|
-
var _b = operation.getContext(), _c = _b.controlMessages, _d =
|
|
136
|
+
var _b = operation.getContext(), _c = _b.controlMessages, _d = _c === void 0 ? (_a = {},
|
|
130
137
|
_a[exports.CONTROL_EVENTS_KEY] = undefined,
|
|
131
|
-
_a) : _c
|
|
138
|
+
_a) : _c, _e = exports.CONTROL_EVENTS_KEY, controlEvents = _d[_e], headers = _b.headers;
|
|
132
139
|
return new core_1.Observable(function (observer) {
|
|
133
140
|
if (!_this.url) {
|
|
134
141
|
observer.error({
|
|
@@ -139,7 +146,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
139
146
|
observer.complete();
|
|
140
147
|
}
|
|
141
148
|
else {
|
|
142
|
-
var subscriptionId_1 = uuid_1.v4();
|
|
149
|
+
var subscriptionId_1 = (0, uuid_1.v4)();
|
|
143
150
|
var token = _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AMAZON_COGNITO_USER_POOLS ||
|
|
144
151
|
_this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.OPENID_CONNECT
|
|
145
152
|
? _this.auth.jwtToken
|
|
@@ -148,7 +155,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
148
155
|
var options = {
|
|
149
156
|
appSyncGraphqlEndpoint: _this.url,
|
|
150
157
|
authenticationType: _this.auth.type,
|
|
151
|
-
query: graphql_1.print(query),
|
|
158
|
+
query: (0, graphql_1.print)(query),
|
|
152
159
|
region: _this.region,
|
|
153
160
|
graphql_headers: function () { return (headers); },
|
|
154
161
|
variables: variables,
|
|
@@ -184,7 +191,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
184
191
|
}); };
|
|
185
192
|
}
|
|
186
193
|
}).filter(function (data) {
|
|
187
|
-
var _a = data.extensions, _b =
|
|
194
|
+
var _a = data.extensions, _b = _a === void 0 ? {} : _a, _c = _b.controlMsgType, controlMsgType = _c === void 0 ? undefined : _c;
|
|
188
195
|
var isControlMsg = typeof controlMsgType !== "undefined";
|
|
189
196
|
return controlEvents === true || !isControlMsg;
|
|
190
197
|
});
|
|
@@ -328,7 +335,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
328
335
|
_d = err_1.message, message = _d === void 0 ? "" : _d;
|
|
329
336
|
observer.error({
|
|
330
337
|
errors: [
|
|
331
|
-
__assign({}, new graphql_1.GraphQLError("Connection failed: "
|
|
338
|
+
__assign({}, new graphql_1.GraphQLError("Connection failed: ".concat(message)))
|
|
332
339
|
]
|
|
333
340
|
});
|
|
334
341
|
observer.complete();
|
|
@@ -367,18 +374,17 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
367
374
|
return;
|
|
368
375
|
}
|
|
369
376
|
return new Promise(function (res, rej) { return __awaiter(_this, void 0, void 0, function () {
|
|
370
|
-
var payloadString,
|
|
371
|
-
return __generator(this, function (
|
|
372
|
-
switch (
|
|
377
|
+
var payloadString, headerObj, headerString, headerBase64url, discoverableEndpoint, protocols, err_2;
|
|
378
|
+
return __generator(this, function (_a) {
|
|
379
|
+
switch (_a.label) {
|
|
373
380
|
case 0:
|
|
374
381
|
this.promiseArray.push({ res: res, rej: rej });
|
|
375
382
|
if (!(this.socketStatus === types_1.SOCKET_STATUS.CLOSED)) return [3 /*break*/, 5];
|
|
376
|
-
|
|
383
|
+
_a.label = 1;
|
|
377
384
|
case 1:
|
|
378
|
-
|
|
385
|
+
_a.trys.push([1, 4, , 5]);
|
|
379
386
|
this.socketStatus = types_1.SOCKET_STATUS.CONNECTING;
|
|
380
387
|
payloadString = "{}";
|
|
381
|
-
_b = (_a = JSON).stringify;
|
|
382
388
|
return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
|
|
383
389
|
authenticationType: authenticationType,
|
|
384
390
|
payload: payloadString,
|
|
@@ -391,9 +397,13 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
391
397
|
graphql_headers: function () { }
|
|
392
398
|
})];
|
|
393
399
|
case 2:
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
400
|
+
headerObj = _a.sent();
|
|
401
|
+
headerString = JSON.stringify(headerObj);
|
|
402
|
+
headerBase64url = Buffer.from(headerString)
|
|
403
|
+
.toString("base64")
|
|
404
|
+
.replace(/\+/g, "-")
|
|
405
|
+
.replace(/\//g, "_")
|
|
406
|
+
.replace(/=+$/, "");
|
|
397
407
|
discoverableEndpoint = appSyncGraphqlEndpoint;
|
|
398
408
|
if (this.isCustomDomain(discoverableEndpoint)) {
|
|
399
409
|
discoverableEndpoint = discoverableEndpoint.concat(customDomainPath);
|
|
@@ -404,10 +414,10 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
404
414
|
discoverableEndpoint = discoverableEndpoint
|
|
405
415
|
.replace("https://", "wss://")
|
|
406
416
|
.replace('http://', 'ws://');
|
|
407
|
-
|
|
408
|
-
return [4 /*yield*/, this._initializeRetryableHandshake({ awsRealTimeUrl:
|
|
417
|
+
protocols = ["graphql-ws", "header-".concat(headerBase64url)];
|
|
418
|
+
return [4 /*yield*/, this._initializeRetryableHandshake({ awsRealTimeUrl: discoverableEndpoint, protocols: protocols })];
|
|
409
419
|
case 3:
|
|
410
|
-
|
|
420
|
+
_a.sent();
|
|
411
421
|
this.promiseArray.forEach(function (_a) {
|
|
412
422
|
var res = _a.res;
|
|
413
423
|
logger("Notifying connection successful");
|
|
@@ -417,7 +427,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
417
427
|
this.promiseArray = [];
|
|
418
428
|
return [3 /*break*/, 5];
|
|
419
429
|
case 4:
|
|
420
|
-
err_2 =
|
|
430
|
+
err_2 = _a.sent();
|
|
421
431
|
this.promiseArray.forEach(function (_a) {
|
|
422
432
|
var rej = _a.rej;
|
|
423
433
|
return rej(err_2);
|
|
@@ -451,8 +461,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
451
461
|
};
|
|
452
462
|
handler = headerHandler[authenticationType];
|
|
453
463
|
if (typeof handler !== "function") {
|
|
454
|
-
|
|
455
|
-
return [2 /*return*/, {}];
|
|
464
|
+
throw new retry_1.NonRetryableError("Authentication type ".concat(authenticationType, " not supported"));
|
|
456
465
|
}
|
|
457
466
|
host = url.parse(appSyncGraphqlEndpoint).host;
|
|
458
467
|
return [4 /*yield*/, handler({
|
|
@@ -476,24 +485,25 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
476
485
|
AppSyncRealTimeSubscriptionHandshakeLink.prototype._awsRealTimeAuthorizationHeader = function (_a) {
|
|
477
486
|
var host = _a.host, token = _a.token, graphql_headers = _a.graphql_headers;
|
|
478
487
|
return __awaiter(this, void 0, void 0, function () {
|
|
479
|
-
var _b, _c
|
|
488
|
+
var _b, _c;
|
|
489
|
+
var _d;
|
|
480
490
|
return __generator(this, function (_e) {
|
|
481
491
|
switch (_e.label) {
|
|
482
492
|
case 0:
|
|
483
|
-
|
|
493
|
+
_d = {};
|
|
484
494
|
if (!(typeof token === "function")) return [3 /*break*/, 2];
|
|
485
495
|
return [4 /*yield*/, token.call(undefined)];
|
|
486
496
|
case 1:
|
|
487
|
-
|
|
497
|
+
_b = _e.sent();
|
|
488
498
|
return [3 /*break*/, 4];
|
|
489
499
|
case 2: return [4 /*yield*/, token];
|
|
490
500
|
case 3:
|
|
491
|
-
|
|
501
|
+
_b = _e.sent();
|
|
492
502
|
_e.label = 4;
|
|
493
503
|
case 4:
|
|
494
|
-
|
|
504
|
+
_c = [(_d.Authorization = _b, _d.host = host, _d)];
|
|
495
505
|
return [4 /*yield*/, graphql_headers()];
|
|
496
|
-
case 5: return [2 /*return*/, __assign.apply(void 0,
|
|
506
|
+
case 5: return [2 /*return*/, __assign.apply(void 0, _c.concat([(_e.sent())]))];
|
|
497
507
|
}
|
|
498
508
|
});
|
|
499
509
|
});
|
|
@@ -546,7 +556,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
546
556
|
session_token: sessionToken
|
|
547
557
|
};
|
|
548
558
|
request = {
|
|
549
|
-
url: ""
|
|
559
|
+
url: "".concat(appSyncGraphqlEndpoint).concat(canonicalUri),
|
|
550
560
|
body: payload,
|
|
551
561
|
method: "POST",
|
|
552
562
|
headers: __assign({}, APPSYNC_REALTIME_HEADERS)
|
|
@@ -558,14 +568,14 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
558
568
|
});
|
|
559
569
|
};
|
|
560
570
|
AppSyncRealTimeSubscriptionHandshakeLink.prototype._initializeRetryableHandshake = function (_a) {
|
|
561
|
-
var awsRealTimeUrl = _a.awsRealTimeUrl;
|
|
571
|
+
var awsRealTimeUrl = _a.awsRealTimeUrl, protocols = _a.protocols;
|
|
562
572
|
return __awaiter(this, void 0, void 0, function () {
|
|
563
573
|
return __generator(this, function (_b) {
|
|
564
574
|
switch (_b.label) {
|
|
565
575
|
case 0:
|
|
566
576
|
logger("Initializaling retryable Handshake");
|
|
567
|
-
return [4 /*yield*/, retry_1.jitteredExponentialRetry(this._initializeHandshake.bind(this), [
|
|
568
|
-
{ awsRealTimeUrl: awsRealTimeUrl }
|
|
577
|
+
return [4 /*yield*/, (0, retry_1.jitteredExponentialRetry)(this._initializeHandshake.bind(this), [
|
|
578
|
+
{ awsRealTimeUrl: awsRealTimeUrl, protocols: protocols }
|
|
569
579
|
])];
|
|
570
580
|
case 1:
|
|
571
581
|
_b.sent();
|
|
@@ -575,20 +585,21 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
575
585
|
});
|
|
576
586
|
};
|
|
577
587
|
AppSyncRealTimeSubscriptionHandshakeLink.prototype._initializeHandshake = function (_a) {
|
|
578
|
-
var awsRealTimeUrl = _a.awsRealTimeUrl;
|
|
588
|
+
var awsRealTimeUrl = _a.awsRealTimeUrl, protocols = _a.protocols;
|
|
579
589
|
return __awaiter(this, void 0, void 0, function () {
|
|
580
590
|
var err_3, errorType, errorCode;
|
|
581
591
|
var _this = this;
|
|
582
592
|
return __generator(this, function (_b) {
|
|
583
593
|
switch (_b.label) {
|
|
584
594
|
case 0:
|
|
585
|
-
|
|
595
|
+
// Log only the endpoint without auth parameters to avoid credential exposure
|
|
596
|
+
logger("Initializing handshake ".concat(awsRealTimeUrl));
|
|
586
597
|
_b.label = 1;
|
|
587
598
|
case 1:
|
|
588
599
|
_b.trys.push([1, 4, , 5]);
|
|
589
600
|
return [4 /*yield*/, (function () {
|
|
590
601
|
return new Promise(function (res, rej) {
|
|
591
|
-
var newSocket = AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket(awsRealTimeUrl,
|
|
602
|
+
var newSocket = AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket(awsRealTimeUrl, protocols);
|
|
592
603
|
newSocket.onerror = function () {
|
|
593
604
|
logger("WebSocket connection error");
|
|
594
605
|
};
|
|
@@ -608,34 +619,45 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
608
619
|
return new Promise(function (res, rej) {
|
|
609
620
|
var ackOk = false;
|
|
610
621
|
_this.awsRealTimeSocket.onerror = function (error) {
|
|
611
|
-
logger("WebSocket closed "
|
|
622
|
+
logger("WebSocket closed ".concat(JSON.stringify(error)));
|
|
612
623
|
};
|
|
613
624
|
_this.awsRealTimeSocket.onclose = function (event) {
|
|
614
|
-
logger("WebSocket closed "
|
|
625
|
+
logger("WebSocket closed ".concat(event.reason));
|
|
615
626
|
rej(new Error(JSON.stringify(event)));
|
|
616
627
|
};
|
|
617
628
|
_this.awsRealTimeSocket.onmessage = function (message) {
|
|
618
629
|
var _a;
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
630
|
+
var data;
|
|
631
|
+
try {
|
|
632
|
+
data = JSON.parse(message.data);
|
|
633
|
+
}
|
|
634
|
+
catch (e) {
|
|
635
|
+
logger("Failed to parse WebSocket message");
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
var type = data.type, _b = data.payload, _c = _b === void 0 ? {} : _b, _d = _c.connectionTimeoutMs, connectionTimeoutMs = _d === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _d;
|
|
639
|
+
logger("subscription message from AWS AppSyncRealTime: ".concat(type));
|
|
622
640
|
if (type === types_1.MESSAGE_TYPES.GQL_CONNECTION_ACK) {
|
|
623
641
|
ackOk = true;
|
|
624
|
-
|
|
642
|
+
// Clamp server-provided timeout to a reasonable range
|
|
643
|
+
var validTimeout = typeof connectionTimeoutMs === 'number' && connectionTimeoutMs >= SERVER_KEEP_ALIVE_TIMEOUT
|
|
644
|
+
? connectionTimeoutMs
|
|
645
|
+
: DEFAULT_KEEP_ALIVE_TIMEOUT;
|
|
646
|
+
_this.keepAliveTimeout = (_a = _this.keepAliveTimeout) !== null && _a !== void 0 ? _a : validTimeout;
|
|
625
647
|
_this.awsRealTimeSocket.onmessage = _this._handleIncomingSubscriptionMessage.bind(_this);
|
|
626
648
|
_this.awsRealTimeSocket.onerror = function (err) {
|
|
627
649
|
logger(err);
|
|
628
650
|
_this._errorDisconnect(types_1.CONTROL_MSG.CONNECTION_CLOSED);
|
|
629
651
|
};
|
|
630
652
|
_this.awsRealTimeSocket.onclose = function (event) {
|
|
631
|
-
logger("WebSocket closed "
|
|
653
|
+
logger("WebSocket closed ".concat(event.reason));
|
|
632
654
|
_this._errorDisconnect(types_1.CONTROL_MSG.CONNECTION_CLOSED);
|
|
633
655
|
};
|
|
634
656
|
res("Cool, connected to AWS AppSyncRealTime");
|
|
635
657
|
return;
|
|
636
658
|
}
|
|
637
659
|
if (type === types_1.MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
|
|
638
|
-
var
|
|
660
|
+
var _e = data.payload, _f = _e === void 0 ? {} : _e, _g = _f.errors, _h = _g === void 0 ? [] : _g, _j = _h[0], _k = _j === void 0 ? {} : _j, _l = _k.errorType, errorType = _l === void 0 ? "" : _l, _m = _k.errorCode, errorCode = _m === void 0 ? 0 : _m;
|
|
639
661
|
rej({ errorType: errorType, errorCode: errorCode });
|
|
640
662
|
}
|
|
641
663
|
};
|
|
@@ -645,7 +667,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
645
667
|
_this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
|
|
646
668
|
function checkAckOk() {
|
|
647
669
|
if (!ackOk) {
|
|
648
|
-
rej(new Error("Connection timeout: ack from AWSRealTime was not received on "
|
|
670
|
+
rej(new Error("Connection timeout: ack from AWSRealTime was not received on ".concat(CONNECTION_INIT_TIMEOUT, " ms")));
|
|
649
671
|
}
|
|
650
672
|
}
|
|
651
673
|
setTimeout(checkAckOk.bind(_this), CONNECTION_INIT_TIMEOUT);
|
|
@@ -674,21 +696,29 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
674
696
|
});
|
|
675
697
|
};
|
|
676
698
|
AppSyncRealTimeSubscriptionHandshakeLink.prototype._handleIncomingSubscriptionMessage = function (message) {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
699
|
+
var parsed;
|
|
700
|
+
try {
|
|
701
|
+
parsed = JSON.parse(message.data);
|
|
702
|
+
}
|
|
703
|
+
catch (e) {
|
|
704
|
+
logger("Failed to parse incoming subscription message");
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
var _a = parsed.id, id = _a === void 0 ? "" : _a, payload = parsed.payload, type = parsed.type;
|
|
708
|
+
logger("subscription message from AWS AppSync RealTime: ".concat(type, " id: ").concat(id));
|
|
709
|
+
var _b = this.subscriptionObserverMap.get(id) || {}, _c = _b.observer, observer = _c === void 0 ? null : _c, _d = _b.query, query = _d === void 0 ? "" : _d, _e = _b.variables, variables = _e === void 0 ? {} : _e, _f = _b.startAckTimeoutId, startAckTimeoutId = _f === void 0 ? 0 : _f, _g = _b.subscriptionReadyCallback, subscriptionReadyCallback = _g === void 0 ? null : _g, _h = _b.subscriptionFailedCallback, subscriptionFailedCallback = _h === void 0 ? null : _h;
|
|
680
710
|
logger({ id: id, observer: observer, query: query, variables: variables });
|
|
681
711
|
if (type === types_1.MESSAGE_TYPES.GQL_DATA && payload && payload.data) {
|
|
682
712
|
if (observer) {
|
|
683
713
|
observer.next(payload);
|
|
684
714
|
}
|
|
685
715
|
else {
|
|
686
|
-
logger("observer not found for id: "
|
|
716
|
+
logger("observer not found for id: ".concat(id));
|
|
687
717
|
}
|
|
688
718
|
return;
|
|
689
719
|
}
|
|
690
720
|
if (type === types_1.MESSAGE_TYPES.GQL_START_ACK) {
|
|
691
|
-
logger("subscription ready for "
|
|
721
|
+
logger("subscription ready for ".concat(JSON.stringify({ query: query, variables: variables })));
|
|
692
722
|
if (typeof subscriptionReadyCallback === "function") {
|
|
693
723
|
subscriptionReadyCallback();
|
|
694
724
|
}
|
|
@@ -702,7 +732,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
702
732
|
});
|
|
703
733
|
}
|
|
704
734
|
else {
|
|
705
|
-
logger("observer not found for id: "
|
|
735
|
+
logger("observer not found for id: ".concat(id));
|
|
706
736
|
}
|
|
707
737
|
var subscriptionState = types_1.SUBSCRIPTION_STATUS.CONNECTED;
|
|
708
738
|
this.subscriptionObserverMap.set(id, {
|
|
@@ -736,13 +766,13 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
736
766
|
if (observer) {
|
|
737
767
|
observer.error({
|
|
738
768
|
errors: [
|
|
739
|
-
__assign({}, new graphql_1.GraphQLError("Connection failed: "
|
|
769
|
+
__assign({}, new graphql_1.GraphQLError("Connection failed: ".concat(JSON.stringify(payload))))
|
|
740
770
|
]
|
|
741
771
|
});
|
|
742
772
|
observer.complete();
|
|
743
773
|
}
|
|
744
774
|
else {
|
|
745
|
-
logger("observer not found for id: "
|
|
775
|
+
logger("observer not found for id: ".concat(id));
|
|
746
776
|
}
|
|
747
777
|
if (typeof subscriptionFailedCallback === "function") {
|
|
748
778
|
subscriptionFailedCallback();
|
|
@@ -750,7 +780,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
750
780
|
}
|
|
751
781
|
};
|
|
752
782
|
AppSyncRealTimeSubscriptionHandshakeLink.prototype._errorDisconnect = function (msg) {
|
|
753
|
-
logger("Disconnect error: "
|
|
783
|
+
logger("Disconnect error: ".concat(msg));
|
|
754
784
|
this.subscriptionObserverMap.forEach(function (_a) {
|
|
755
785
|
var observer = _a.observer;
|
|
756
786
|
if (observer && !observer.closed) {
|
|
@@ -779,7 +809,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
|
|
|
779
809
|
if (observer && !observer.closed) {
|
|
780
810
|
observer.error({
|
|
781
811
|
errors: [
|
|
782
|
-
__assign({}, new graphql_1.GraphQLError("Subscription timeout "
|
|
812
|
+
__assign({}, new graphql_1.GraphQLError("Subscription timeout ".concat(JSON.stringify({ query: query, variables: variables }))))
|
|
783
813
|
]
|
|
784
814
|
});
|
|
785
815
|
// Cleanup will be automatically executed
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { ApolloLink, Observable, Operation, FetchResult } from "@apollo/client/core";
|
|
6
6
|
import * as ZenObservable from 'zen-observable-ts';
|
|
7
|
-
|
|
7
|
+
type MqttConnectionInfo = {
|
|
8
8
|
client: string;
|
|
9
9
|
url: string;
|
|
10
10
|
topics: string[];
|
|
@@ -3,10 +3,12 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
3
3
|
var extendStatics = function (d, b) {
|
|
4
4
|
extendStatics = Object.setPrototypeOf ||
|
|
5
5
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
@@ -38,7 +40,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
40
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
39
41
|
function step(op) {
|
|
40
42
|
if (f) throw new TypeError("Generator is already executing.");
|
|
41
|
-
while (_) try {
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
42
44
|
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
45
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
44
46
|
switch (op[0]) {
|
|
@@ -71,6 +73,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
71
73
|
return t;
|
|
72
74
|
};
|
|
73
75
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
76
|
+
exports.SubscriptionHandshakeLink = exports.CONTROL_EVENTS_KEY = void 0;
|
|
74
77
|
/*!
|
|
75
78
|
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
76
79
|
* SPDX-License-Identifier: Apache-2.0
|
|
@@ -108,8 +111,8 @@ var SubscriptionHandshakeLink = /** @class */ (function (_super) {
|
|
|
108
111
|
SubscriptionHandshakeLink.prototype.request = function (operation) {
|
|
109
112
|
var _a;
|
|
110
113
|
var _this = this;
|
|
111
|
-
var _b = operation.getContext(), _c = this.subsInfoContextKey, subsInfo = _b[_c], _d = _b.controlMessages, _e =
|
|
112
|
-
var
|
|
114
|
+
var _b = operation.getContext(), _c = this.subsInfoContextKey, subsInfo = _b[_c], _d = _b.controlMessages, _e = _d === void 0 ? (_a = {}, _a[exports.CONTROL_EVENTS_KEY] = undefined, _a) : _d, _f = exports.CONTROL_EVENTS_KEY, controlEvents = _e[_f];
|
|
115
|
+
var _g = subsInfo.extensions, _h = _g === void 0 ? { subscription: { newSubscriptions: {}, mqttConnections: [] } } : _g, _j = _h.subscription, newSubscriptions = _j.newSubscriptions, mqttConnections = _j.mqttConnections, _k = subsInfo.errors, errors = _k === void 0 ? [] : _k;
|
|
113
116
|
if (errors && errors.length) {
|
|
114
117
|
return new core_1.Observable(function (observer) {
|
|
115
118
|
observer.error(new core_1.ApolloError({
|
|
@@ -170,7 +173,7 @@ var SubscriptionHandshakeLink = /** @class */ (function (_super) {
|
|
|
170
173
|
}));
|
|
171
174
|
};
|
|
172
175
|
}).filter(function (data) {
|
|
173
|
-
var _a = data.extensions, _b =
|
|
176
|
+
var _a = data.extensions, _b = _a === void 0 ? {} : _a, _c = _b.controlMsgType, controlMsgType = _c === void 0 ? undefined : _c;
|
|
174
177
|
var isControlMsg = typeof controlMsgType !== 'undefined';
|
|
175
178
|
return controlEvents === true || !isControlMsg;
|
|
176
179
|
});
|
|
@@ -181,7 +184,7 @@ var SubscriptionHandshakeLink = /** @class */ (function (_super) {
|
|
|
181
184
|
var _this = this;
|
|
182
185
|
return __generator(this, function (_a) {
|
|
183
186
|
query = operation.query;
|
|
184
|
-
selectionNames = utilities_1.getMainDefinition(query).selectionSet.selections.map(function (_a) {
|
|
187
|
+
selectionNames = (0, utilities_1.getMainDefinition)(query).selectionSet.selections.map(function (_a) {
|
|
185
188
|
var value = _a.name.value;
|
|
186
189
|
return value;
|
|
187
190
|
});
|
package/lib/types/index.d.ts
CHANGED
|
@@ -67,15 +67,15 @@ export declare enum CONTROL_MSG {
|
|
|
67
67
|
TIMEOUT_DISCONNECT = "Timeout disconnect",
|
|
68
68
|
SUBSCRIPTION_ACK = "Subscription ack"
|
|
69
69
|
}
|
|
70
|
-
export
|
|
70
|
+
export type UrlInfo = {
|
|
71
71
|
url: string;
|
|
72
72
|
auth: AuthOptions;
|
|
73
73
|
region: string;
|
|
74
74
|
};
|
|
75
|
-
export
|
|
75
|
+
export type AppSyncRealTimeSubscriptionConfig = UrlInfo & {
|
|
76
76
|
keepAliveTimeoutMs?: number;
|
|
77
77
|
};
|
|
78
|
-
export
|
|
78
|
+
export type ObserverQuery = {
|
|
79
79
|
observer: ZenObservable.SubscriptionObserver<any>;
|
|
80
80
|
query: string;
|
|
81
81
|
variables: object;
|
|
@@ -84,4 +84,4 @@ export declare type ObserverQuery = {
|
|
|
84
84
|
subscriptionFailedCallback?: Function;
|
|
85
85
|
startAckTimeoutId?: number;
|
|
86
86
|
};
|
|
87
|
-
export
|
|
87
|
+
export type DelayFunction = (attempt: number, args?: any[], error?: Error) => number | false;
|
package/lib/types/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONTROL_MSG = exports.MESSAGE_TYPES = exports.SOCKET_STATUS = exports.SUBSCRIPTION_STATUS = void 0;
|
|
3
4
|
//#region Subscription link enums
|
|
4
5
|
var SUBSCRIPTION_STATUS;
|
|
5
6
|
(function (SUBSCRIPTION_STATUS) {
|
package/lib/utils/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rootLogger = void 0;
|
|
3
4
|
/*!
|
|
4
5
|
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
6
|
* SPDX-License-Identifier: Apache-2.0
|
|
6
7
|
*/
|
|
7
8
|
var logger_1 = require("./logger");
|
|
8
|
-
exports
|
|
9
|
+
Object.defineProperty(exports, "rootLogger", { enumerable: true, get: function () { return logger_1.default; } });
|
package/lib/utils/logger.d.ts
CHANGED