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