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