aws-appsync-subscription-link
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -6,2 +6,10 @@ # Change Log | ||
<a name="2.2.2"></a> | ||
## [2.2.2](https://github.com/awslabs/aws-mobile-appsync-sdk-js/compare/aws-appsync-subscription-link@2.2.1...aws-appsync-subscription-link@2.2.2) (2021-07-09) | ||
**Note:** Version bump only for package aws-appsync-subscription-link | ||
<a name="2.2.1"></a> | ||
@@ -8,0 +16,0 @@ ## [2.2.1](https://github.com/awslabs/aws-mobile-appsync-sdk-js/compare/aws-appsync-subscription-link@2.2.0...aws-appsync-subscription-link@2.2.1) (2020-09-10) |
@@ -33,3 +33,3 @@ /*! | ||
private _handleIncomingSubscriptionMessage; | ||
private _timeoutDisconnect; | ||
private _errorDisconnect; | ||
private _timeoutStartSubscriptionAck; | ||
@@ -36,0 +36,0 @@ static createWebSocket(awsRealTimeUrl: string, protocol: string): WebSocket; |
@@ -118,43 +118,53 @@ "use strict"; | ||
return new apollo_link_1.Observable(function (observer) { | ||
var subscriptionId = uuid_1.v4(); | ||
var options = { | ||
appSyncGraphqlEndpoint: _this.url, | ||
authenticationType: _this.auth.type, | ||
query: graphql_1.print(query), | ||
region: _this.region, | ||
graphql_headers: function () { return (headers); }, | ||
variables: variables, | ||
apiKey: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.API_KEY ? _this.auth.apiKey : "", | ||
credentials: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AWS_IAM ? _this.auth.credentials : null, | ||
jwtToken: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AMAZON_COGNITO_USER_POOLS || | ||
_this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.OPENID_CONNECT | ||
? _this.auth.jwtToken | ||
: null | ||
}; | ||
_this._startSubscriptionWithAWSAppSyncRealTime({ | ||
options: options, | ||
observer: observer, | ||
subscriptionId: subscriptionId | ||
}); | ||
return function () { return __awaiter(_this, void 0, void 0, function () { | ||
var subscriptionState; | ||
return __generator(this, function (_a) { | ||
// Cleanup after unsubscribing or observer.complete was called after _startSubscriptionWithAWSAppSyncRealTime | ||
try { | ||
this._verifySubscriptionAlreadyStarted(subscriptionId); | ||
subscriptionState = this.subscriptionObserverMap.get(subscriptionId).subscriptionState; | ||
if (subscriptionState === types_1.SUBSCRIPTION_STATUS.CONNECTED) { | ||
this._sendUnsubscriptionMessage(subscriptionId); | ||
if (!_this.url) { | ||
observer.error({ | ||
errors: [ | ||
__assign({}, new graphql_1.GraphQLError("Subscribe only available for AWS AppSync endpoint")), | ||
], | ||
}); | ||
observer.complete(); | ||
} | ||
else { | ||
var subscriptionId_1 = uuid_1.v4(); | ||
var options = { | ||
appSyncGraphqlEndpoint: _this.url, | ||
authenticationType: _this.auth.type, | ||
query: graphql_1.print(query), | ||
region: _this.region, | ||
graphql_headers: function () { return (headers); }, | ||
variables: variables, | ||
apiKey: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.API_KEY ? _this.auth.apiKey : "", | ||
credentials: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AWS_IAM ? _this.auth.credentials : null, | ||
jwtToken: _this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.AMAZON_COGNITO_USER_POOLS || | ||
_this.auth.type === aws_appsync_auth_link_1.AUTH_TYPE.OPENID_CONNECT | ||
? _this.auth.jwtToken | ||
: null | ||
}; | ||
_this._startSubscriptionWithAWSAppSyncRealTime({ | ||
options: options, | ||
observer: observer, | ||
subscriptionId: subscriptionId_1 | ||
}); | ||
return function () { return __awaiter(_this, void 0, void 0, function () { | ||
var subscriptionState; | ||
return __generator(this, function (_a) { | ||
// Cleanup after unsubscribing or observer.complete was called after _startSubscriptionWithAWSAppSyncRealTime | ||
try { | ||
this._verifySubscriptionAlreadyStarted(subscriptionId_1); | ||
subscriptionState = this.subscriptionObserverMap.get(subscriptionId_1).subscriptionState; | ||
if (subscriptionState === types_1.SUBSCRIPTION_STATUS.CONNECTED) { | ||
this._sendUnsubscriptionMessage(subscriptionId_1); | ||
} | ||
else { | ||
throw new Error("Subscription has failed, starting to remove subscription."); | ||
} | ||
} | ||
else { | ||
throw new Error("Subscription has failed, starting to remove subscription."); | ||
catch (err) { | ||
this._removeSubscriptionObserver(subscriptionId_1); | ||
return [2 /*return*/]; | ||
} | ||
} | ||
catch (err) { | ||
this._removeSubscriptionObserver(subscriptionId); | ||
return [2 /*return*/]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
}); | ||
}); }; | ||
} | ||
}).filter(function (data) { | ||
@@ -237,3 +247,3 @@ var _a = data.extensions, _b = (_a === void 0 ? {} : _a).controlMsgType, controlMsgType = _b === void 0 ? undefined : _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _b, graphql_headers, credentials, jwtToken, subscriptionState, data, dataString, headerObj, _c, subscriptionMessage, stringToAWSRealTime, err_1, _d, message, _e, subscriptionFailedCallback_1, _f, subscriptionFailedCallback, subscriptionReadyCallback; | ||
var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _b, graphql_headers, credentials, jwtToken, subscriptionState, data, dataString, headerObj, _c, _d, subscriptionMessage, stringToAWSRealTime, err_1, _e, message, subscriptionFailedCallback_1, _f, subscriptionFailedCallback, subscriptionReadyCallback; | ||
var _g; | ||
@@ -255,6 +265,7 @@ var _this = this; | ||
variables: variables, | ||
subscriptionState: subscriptionState | ||
subscriptionState: subscriptionState, | ||
startAckTimeoutId: null, | ||
}); | ||
dataString = JSON.stringify(data); | ||
_c = [__assign({}, graphql_headers())]; | ||
_c = [{}]; | ||
return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({ | ||
@@ -271,3 +282,6 @@ apiKey: apiKey, | ||
case 1: | ||
headerObj = __assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_h.sent())])), (_g = {}, _g[aws_appsync_auth_link_1.USER_AGENT_HEADER] = aws_appsync_auth_link_1.USER_AGENT, _g)]); | ||
_d = [__assign.apply(void 0, _c.concat([(_h.sent())]))]; | ||
return [4 /*yield*/, graphql_headers()]; | ||
case 2: | ||
headerObj = __assign.apply(void 0, [__assign.apply(void 0, _d.concat([(_h.sent())])), (_g = {}, _g[aws_appsync_auth_link_1.USER_AGENT_HEADER] = aws_appsync_auth_link_1.USER_AGENT, _g)]); | ||
subscriptionMessage = { | ||
@@ -284,5 +298,5 @@ id: subscriptionId, | ||
stringToAWSRealTime = JSON.stringify(subscriptionMessage); | ||
_h.label = 2; | ||
case 2: | ||
_h.trys.push([2, 4, , 5]); | ||
_h.label = 3; | ||
case 3: | ||
_h.trys.push([3, 5, , 6]); | ||
return [4 /*yield*/, this._initializeWebSocketConnection({ | ||
@@ -296,8 +310,8 @@ apiKey: apiKey, | ||
})]; | ||
case 3: | ||
case 4: | ||
_h.sent(); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
err_1 = _h.sent(); | ||
_d = err_1.message, message = _d === void 0 ? "" : _d; | ||
_e = err_1.message, message = _e === void 0 ? "" : _e; | ||
observer.error({ | ||
@@ -309,3 +323,3 @@ errors: [ | ||
observer.complete(); | ||
_e = (this.subscriptionObserverMap.get(subscriptionId) || {}).subscriptionFailedCallback, subscriptionFailedCallback_1 = _e === void 0 ? null : _e; | ||
subscriptionFailedCallback_1 = (this.subscriptionObserverMap.get(subscriptionId) || {}).subscriptionFailedCallback; | ||
// Notify concurrent unsubscription | ||
@@ -316,3 +330,3 @@ if (typeof subscriptionFailedCallback_1 === "function") { | ||
return [2 /*return*/]; | ||
case 5: | ||
case 6: | ||
_f = this.subscriptionObserverMap.get(subscriptionId), subscriptionFailedCallback = _f.subscriptionFailedCallback, subscriptionReadyCallback = _f.subscriptionReadyCallback; | ||
@@ -331,3 +345,5 @@ // This must be done before sending the message in order to be listening immediately | ||
}); | ||
this.awsRealTimeSocket.send(stringToAWSRealTime); | ||
if (this.awsRealTimeSocket) { | ||
this.awsRealTimeSocket.send(stringToAWSRealTime); | ||
} | ||
return [2 /*return*/]; | ||
@@ -342,3 +358,3 @@ } | ||
if (this.socketStatus === types_1.SOCKET_STATUS.READY) { | ||
return Promise.resolve(); | ||
return; | ||
} | ||
@@ -587,2 +603,10 @@ return new Promise(function (res, rej) { return __awaiter(_this, void 0, void 0, function () { | ||
_this.awsRealTimeSocket.onmessage = _this._handleIncomingSubscriptionMessage.bind(_this); | ||
_this.awsRealTimeSocket.onerror = function (err) { | ||
logger(err); | ||
_this._errorDisconnect(types_1.CONTROL_MSG.CONNECTION_CLOSED); | ||
}; | ||
_this.awsRealTimeSocket.onclose = function (event) { | ||
logger("WebSocket closed " + event.reason); | ||
_this._errorDisconnect(types_1.CONTROL_MSG.CONNECTION_CLOSED); | ||
}; | ||
res("Cool, connected to AWS AppSyncRealTime"); | ||
@@ -675,3 +699,3 @@ return; | ||
clearTimeout(this.keepAliveTimeoutId); | ||
this.keepAliveTimeoutId = setTimeout(this._timeoutDisconnect.bind(this), this.keepAliveTimeout); | ||
this.keepAliveTimeoutId = setTimeout(this._errorDisconnect.bind(this, types_1.CONTROL_MSG.TIMEOUT_DISCONNECT), this.keepAliveTimeout); | ||
return; | ||
@@ -702,11 +726,13 @@ } | ||
}; | ||
AppSyncRealTimeSubscriptionHandshakeLink.prototype._timeoutDisconnect = function () { | ||
AppSyncRealTimeSubscriptionHandshakeLink.prototype._errorDisconnect = function (msg) { | ||
logger("Disconnect error: " + msg); | ||
this.subscriptionObserverMap.forEach(function (_a) { | ||
var observer = _a.observer; | ||
observer.error({ | ||
errors: [__assign({}, new graphql_1.GraphQLError("Timeout disconnect"))] | ||
}); | ||
observer.complete(); | ||
if (observer && !observer.closed) { | ||
observer.error({ | ||
errors: [__assign({}, new graphql_1.GraphQLError(msg))], | ||
}); | ||
} | ||
}); | ||
this.subscriptionObserverMap = new Map(); | ||
this.subscriptionObserverMap.clear(); | ||
if (this.awsRealTimeSocket) { | ||
@@ -718,3 +744,6 @@ this.awsRealTimeSocket.close(); | ||
AppSyncRealTimeSubscriptionHandshakeLink.prototype._timeoutStartSubscriptionAck = function (subscriptionId) { | ||
var _a = this.subscriptionObserverMap.get(subscriptionId), observer = _a.observer, query = _a.query, variables = _a.variables; | ||
var _a = this.subscriptionObserverMap.get(subscriptionId) || {}, observer = _a.observer, query = _a.query, variables = _a.variables; | ||
if (!observer) { | ||
return; | ||
} | ||
this.subscriptionObserverMap.set(subscriptionId, { | ||
@@ -726,9 +755,11 @@ observer: observer, | ||
}); | ||
observer.error({ | ||
errors: [ | ||
__assign({}, new graphql_1.GraphQLError("Subscription timeout " + JSON.stringify({ query: query, variables: variables }))) | ||
] | ||
}); | ||
// Cleanup will be automatically executed | ||
observer.complete(); | ||
if (observer && !observer.closed) { | ||
observer.error({ | ||
errors: [ | ||
__assign({}, new graphql_1.GraphQLError("Subscription timeout " + JSON.stringify({ query: query, variables: variables }))) | ||
] | ||
}); | ||
// Cleanup will be automatically executed | ||
observer.complete(); | ||
} | ||
logger("timeoutStartSubscription", JSON.stringify({ query: query, variables: variables })); | ||
@@ -735,0 +766,0 @@ }; |
@@ -65,2 +65,7 @@ /// <reference types="zen-observable" /> | ||
} | ||
export declare enum CONTROL_MSG { | ||
CONNECTION_CLOSED = "Connection closed", | ||
TIMEOUT_DISCONNECT = "Timeout disconnect", | ||
SUBSCRIPTION_ACK = "Subscription ack" | ||
} | ||
export declare type UrlInfo = { | ||
@@ -67,0 +72,0 @@ url: string; |
@@ -69,2 +69,8 @@ "use strict"; | ||
})(MESSAGE_TYPES = exports.MESSAGE_TYPES || (exports.MESSAGE_TYPES = {})); | ||
var CONTROL_MSG; | ||
(function (CONTROL_MSG) { | ||
CONTROL_MSG["CONNECTION_CLOSED"] = "Connection closed"; | ||
CONTROL_MSG["TIMEOUT_DISCONNECT"] = "Timeout disconnect"; | ||
CONTROL_MSG["SUBSCRIPTION_ACK"] = "Subscription ack"; | ||
})(CONTROL_MSG = exports.CONTROL_MSG || (exports.CONTROL_MSG = {})); | ||
//#endregion |
{ | ||
"name": "aws-appsync-subscription-link", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"main": "lib/index.js", | ||
@@ -26,3 +26,3 @@ "license": "Apache-2.0", | ||
"apollo-link-retry": "2.2.7", | ||
"aws-appsync-auth-link": "^2.0.3", | ||
"aws-appsync-auth-link": "^2.0.4", | ||
"debug": "2.6.9", | ||
@@ -29,0 +29,0 @@ "url": "^0.11.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
274766
6123
Updatedaws-appsync-auth-link@^2.0.4