@aws-amplify/pubsub 4.2.9-unstable.9 → 4.2.10-cloud-logging.8

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 (52) hide show
  1. package/CHANGELOG.md +27 -44
  2. package/dist/aws-amplify-pubsub.js +258 -209
  3. package/dist/aws-amplify-pubsub.js.map +1 -1
  4. package/dist/aws-amplify-pubsub.min.js +2 -2
  5. package/dist/aws-amplify-pubsub.min.js.map +1 -1
  6. package/lib/Providers/AWSAppSyncProvider.d.ts +3 -0
  7. package/lib/Providers/AWSAppSyncProvider.js +3 -0
  8. package/lib/Providers/AWSAppSyncProvider.js.map +1 -1
  9. package/lib/Providers/AWSAppSyncRealTimeProvider.d.ts +19 -4
  10. package/lib/Providers/AWSAppSyncRealTimeProvider.js +192 -159
  11. package/lib/Providers/AWSAppSyncRealTimeProvider.js.map +1 -1
  12. package/lib/Providers/AWSIotProvider.d.ts +6 -1
  13. package/lib/Providers/AWSIotProvider.js +3 -2
  14. package/lib/Providers/AWSIotProvider.js.map +1 -1
  15. package/lib/Providers/MqttOverWSProvider.d.ts +14 -11
  16. package/lib/Providers/MqttOverWSProvider.js +17 -10
  17. package/lib/Providers/MqttOverWSProvider.js.map +1 -1
  18. package/lib/Providers/PubSubProvider.d.ts +7 -7
  19. package/lib/Providers/PubSubProvider.js.map +1 -1
  20. package/lib/PubSub.d.ts +6 -6
  21. package/lib/PubSub.js +2 -2
  22. package/lib/PubSub.js.map +1 -1
  23. package/lib/types/Provider.d.ts +3 -3
  24. package/lib/types/PubSub.d.ts +5 -1
  25. package/lib-esm/Providers/AWSAppSyncProvider.d.ts +3 -0
  26. package/lib-esm/Providers/AWSAppSyncProvider.js +3 -0
  27. package/lib-esm/Providers/AWSAppSyncProvider.js.map +1 -1
  28. package/lib-esm/Providers/AWSAppSyncRealTimeProvider.d.ts +19 -4
  29. package/lib-esm/Providers/AWSAppSyncRealTimeProvider.js +192 -159
  30. package/lib-esm/Providers/AWSAppSyncRealTimeProvider.js.map +1 -1
  31. package/lib-esm/Providers/AWSIotProvider.d.ts +6 -1
  32. package/lib-esm/Providers/AWSIotProvider.js +3 -2
  33. package/lib-esm/Providers/AWSIotProvider.js.map +1 -1
  34. package/lib-esm/Providers/MqttOverWSProvider.d.ts +14 -11
  35. package/lib-esm/Providers/MqttOverWSProvider.js +17 -10
  36. package/lib-esm/Providers/MqttOverWSProvider.js.map +1 -1
  37. package/lib-esm/Providers/PubSubProvider.d.ts +7 -7
  38. package/lib-esm/Providers/PubSubProvider.js.map +1 -1
  39. package/lib-esm/PubSub.d.ts +6 -6
  40. package/lib-esm/PubSub.js +2 -2
  41. package/lib-esm/PubSub.js.map +1 -1
  42. package/lib-esm/types/Provider.d.ts +3 -3
  43. package/lib-esm/types/PubSub.d.ts +5 -1
  44. package/package.json +5 -5
  45. package/src/Providers/AWSAppSyncProvider.ts +27 -26
  46. package/src/Providers/AWSAppSyncRealTimeProvider.ts +276 -214
  47. package/src/Providers/AWSIotProvider.ts +10 -1
  48. package/src/Providers/MqttOverWSProvider.ts +52 -33
  49. package/src/Providers/PubSubProvider.ts +8 -8
  50. package/src/PubSub.ts +10 -10
  51. package/src/types/Provider.ts +3 -7
  52. package/src/types/PubSub.ts +6 -1
@@ -97,8 +97,7 @@ import Auth from '@aws-amplify/auth';
97
97
  import { AbstractPubSubProvider } from './PubSubProvider';
98
98
  import { CONTROL_MSG } from '../index';
99
99
  var logger = new Logger('AWSAppSyncRealTimeProvider');
100
- var AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' &&
101
- typeof Symbol.for === 'function'
100
+ var AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
102
101
  ? Symbol.for('amplify_default')
103
102
  : '@@amplify_default');
104
103
  var dispatchApiEvent = function (event, data, message) {
@@ -219,9 +218,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
219
218
  };
220
219
  AWSAppSyncRealTimeProvider.prototype.subscribe = function (_topics, options) {
221
220
  var _this = this;
222
- var appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint;
221
+ var appSyncGraphqlEndpoint = options === null || options === void 0 ? void 0 : options.appSyncGraphqlEndpoint;
223
222
  return new Observable(function (observer) {
224
- if (!appSyncGraphqlEndpoint) {
223
+ if (!options || !appSyncGraphqlEndpoint) {
225
224
  observer.error({
226
225
  errors: [
227
226
  __assign({}, new GraphQLError("Subscribe only available for AWS AppSync endpoint")),
@@ -290,14 +289,15 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
290
289
  });
291
290
  AWSAppSyncRealTimeProvider.prototype._startSubscriptionWithAWSAppSyncRealTime = function (_a) {
292
291
  var options = _a.options, observer = _a.observer, subscriptionId = _a.subscriptionId;
292
+ var _b, _c;
293
293
  return __awaiter(this, void 0, void 0, function () {
294
- var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _b, graphql_headers, _c, additionalHeaders, subscriptionState, data, dataString, headerObj, _d, _e, subscriptionMessage, stringToAWSRealTime, err_2, _f, message, subscriptionFailedCallback_1, _g, subscriptionFailedCallback, subscriptionReadyCallback;
295
- var _h;
294
+ var appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _d, graphql_headers, _e, additionalHeaders, subscriptionState, data, dataString, headerObj, _f, _g, subscriptionMessage, stringToAWSRealTime, err_2, message, subscriptionFailedCallback_1, _h, subscriptionFailedCallback, subscriptionReadyCallback;
295
+ var _j;
296
296
  var _this = this;
297
- return __generator(this, function (_j) {
298
- switch (_j.label) {
297
+ return __generator(this, function (_k) {
298
+ switch (_k.label) {
299
299
  case 0:
300
- appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint, authenticationType = options.authenticationType, query = options.query, variables = options.variables, apiKey = options.apiKey, region = options.region, _b = options.graphql_headers, graphql_headers = _b === void 0 ? function () { return ({}); } : _b, _c = options.additionalHeaders, additionalHeaders = _c === void 0 ? {} : _c;
300
+ appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint, authenticationType = options.authenticationType, query = options.query, variables = options.variables, apiKey = options.apiKey, region = options.region, _d = options.graphql_headers, graphql_headers = _d === void 0 ? function () { return ({}); } : _d, _e = options.additionalHeaders, additionalHeaders = _e === void 0 ? {} : _e;
301
301
  subscriptionState = SUBSCRIPTION_STATUS.PENDING;
302
302
  data = {
303
303
  query: query,
@@ -306,13 +306,13 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
306
306
  // Having a subscription id map will make it simple to forward messages received
307
307
  this.subscriptionObserverMap.set(subscriptionId, {
308
308
  observer: observer,
309
- query: query,
310
- variables: variables,
309
+ query: query !== null && query !== void 0 ? query : '',
310
+ variables: variables !== null && variables !== void 0 ? variables : {},
311
311
  subscriptionState: subscriptionState,
312
- startAckTimeoutId: null,
312
+ startAckTimeoutId: undefined,
313
313
  });
314
314
  dataString = JSON.stringify(data);
315
- _d = [{}];
315
+ _f = [{}];
316
316
  return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
317
317
  apiKey: apiKey,
318
318
  appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
@@ -323,10 +323,10 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
323
323
  additionalHeaders: additionalHeaders,
324
324
  })];
325
325
  case 1:
326
- _e = [__assign.apply(void 0, _d.concat([(_j.sent())]))];
326
+ _g = [__assign.apply(void 0, _f.concat([(_k.sent())]))];
327
327
  return [4 /*yield*/, graphql_headers()];
328
328
  case 2:
329
- headerObj = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _e.concat([(_j.sent())])), additionalHeaders]), (_h = {}, _h[USER_AGENT_HEADER] = Constants.userAgent, _h)]);
329
+ headerObj = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _g.concat([(_k.sent())])), additionalHeaders]), (_j = {}, _j[USER_AGENT_HEADER] = Constants.userAgent, _j)]);
330
330
  subscriptionMessage = {
331
331
  id: subscriptionId,
332
332
  payload: {
@@ -338,9 +338,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
338
338
  type: MESSAGE_TYPES.GQL_START,
339
339
  };
340
340
  stringToAWSRealTime = JSON.stringify(subscriptionMessage);
341
- _j.label = 3;
341
+ _k.label = 3;
342
342
  case 3:
343
- _j.trys.push([3, 5, , 6]);
343
+ _k.trys.push([3, 5, , 6]);
344
344
  return [4 /*yield*/, this._initializeWebSocketConnection({
345
345
  apiKey: apiKey,
346
346
  appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
@@ -349,12 +349,12 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
349
349
  additionalHeaders: additionalHeaders,
350
350
  })];
351
351
  case 4:
352
- _j.sent();
352
+ _k.sent();
353
353
  return [3 /*break*/, 6];
354
354
  case 5:
355
- err_2 = _j.sent();
355
+ err_2 = _k.sent();
356
356
  logger.debug({ err: err_2 });
357
- _f = err_2.message, message = _f === void 0 ? '' : _f;
357
+ message = (_b = err_2['message']) !== null && _b !== void 0 ? _b : '';
358
358
  observer.error({
359
359
  errors: [
360
360
  __assign({}, new GraphQLError(CONTROL_MSG.CONNECTION_FAILED + ": " + message)),
@@ -368,13 +368,13 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
368
368
  }
369
369
  return [2 /*return*/];
370
370
  case 6:
371
- _g = this.subscriptionObserverMap.get(subscriptionId), subscriptionFailedCallback = _g.subscriptionFailedCallback, subscriptionReadyCallback = _g.subscriptionReadyCallback;
371
+ _h = (_c = this.subscriptionObserverMap.get(subscriptionId)) !== null && _c !== void 0 ? _c : {}, subscriptionFailedCallback = _h.subscriptionFailedCallback, subscriptionReadyCallback = _h.subscriptionReadyCallback;
372
372
  // This must be done before sending the message in order to be listening immediately
373
373
  this.subscriptionObserverMap.set(subscriptionId, {
374
374
  observer: observer,
375
375
  subscriptionState: subscriptionState,
376
- variables: variables,
377
- query: query,
376
+ query: query !== null && query !== void 0 ? query : '',
377
+ variables: variables !== null && variables !== void 0 ? variables : {},
378
378
  subscriptionReadyCallback: subscriptionReadyCallback,
379
379
  subscriptionFailedCallback: subscriptionFailedCallback,
380
380
  startAckTimeoutId: setTimeout(function () {
@@ -392,23 +392,26 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
392
392
  // Waiting that subscription has been connected before trying to unsubscribe
393
393
  AWSAppSyncRealTimeProvider.prototype._waitForSubscriptionToBeConnected = function (subscriptionId) {
394
394
  return __awaiter(this, void 0, void 0, function () {
395
- var subscriptionState;
395
+ var subscriptionObserver, subscriptionState;
396
396
  var _this = this;
397
397
  return __generator(this, function (_a) {
398
- subscriptionState = this.subscriptionObserverMap.get(subscriptionId).subscriptionState;
399
- // This in case unsubscribe is invoked before sending start subscription message
400
- if (subscriptionState === SUBSCRIPTION_STATUS.PENDING) {
401
- return [2 /*return*/, new Promise(function (res, rej) {
402
- var _a = _this.subscriptionObserverMap.get(subscriptionId), observer = _a.observer, subscriptionState = _a.subscriptionState, variables = _a.variables, query = _a.query;
403
- _this.subscriptionObserverMap.set(subscriptionId, {
404
- observer: observer,
405
- subscriptionState: subscriptionState,
406
- variables: variables,
407
- query: query,
408
- subscriptionReadyCallback: res,
409
- subscriptionFailedCallback: rej,
410
- });
411
- })];
398
+ subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
399
+ if (subscriptionObserver) {
400
+ subscriptionState = subscriptionObserver.subscriptionState;
401
+ // This in case unsubscribe is invoked before sending start subscription message
402
+ if (subscriptionState === SUBSCRIPTION_STATUS.PENDING) {
403
+ return [2 /*return*/, new Promise(function (res, rej) {
404
+ var observer = subscriptionObserver.observer, subscriptionState = subscriptionObserver.subscriptionState, variables = subscriptionObserver.variables, query = subscriptionObserver.query;
405
+ _this.subscriptionObserverMap.set(subscriptionId, {
406
+ observer: observer,
407
+ subscriptionState: subscriptionState,
408
+ variables: variables,
409
+ query: query,
410
+ subscriptionReadyCallback: res,
411
+ subscriptionFailedCallback: rej,
412
+ });
413
+ })];
414
+ }
412
415
  }
413
416
  return [2 /*return*/];
414
417
  });
@@ -453,21 +456,32 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
453
456
  }
454
457
  else {
455
458
  logger.debug('closing WebSocket...');
456
- clearTimeout(this.keepAliveTimeoutId);
459
+ if (this.keepAliveTimeoutId)
460
+ clearTimeout(this.keepAliveTimeoutId);
457
461
  var tempSocket = this.awsRealTimeSocket;
458
462
  // Cleaning callbacks to avoid race condition, socket still exists
459
- tempSocket.onclose = undefined;
460
- tempSocket.onerror = undefined;
463
+ tempSocket.onclose = null;
464
+ tempSocket.onerror = null;
461
465
  tempSocket.close(1000);
462
- this.awsRealTimeSocket = null;
466
+ this.awsRealTimeSocket = undefined;
463
467
  this.socketStatus = SOCKET_STATUS.CLOSED;
464
468
  }
465
469
  };
466
470
  AWSAppSyncRealTimeProvider.prototype._handleIncomingSubscriptionMessage = function (message) {
467
- logger.debug("subscription message from AWS AppSync RealTime: " + message.data);
471
+ if (message.data && message.data === '{"type":"ka"}') {
472
+ logger.verbose("subscription message from AWS AppSync RealTime: " + message.data);
473
+ }
474
+ else {
475
+ logger.debug("subscription message from AWS AppSync RealTime: " + message.data);
476
+ }
468
477
  var _a = JSON.parse(message.data), _b = _a.id, id = _b === void 0 ? '' : _b, payload = _a.payload, type = _a.type;
469
478
  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;
470
- logger.debug({ id: id, observer: observer, query: query, variables: variables });
479
+ if (!id && !observer && !query && Object.keys(variables).length === 0) {
480
+ logger.verbose({ id: id, observer: observer, query: query, variables: variables });
481
+ }
482
+ else {
483
+ logger.debug({ id: id, observer: observer, query: query, variables: variables });
484
+ }
471
485
  if (type === MESSAGE_TYPES.GQL_DATA && payload && payload.data) {
472
486
  if (observer) {
473
487
  observer.next(payload);
@@ -482,46 +496,53 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
482
496
  if (typeof subscriptionReadyCallback === 'function') {
483
497
  subscriptionReadyCallback();
484
498
  }
485
- clearTimeout(startAckTimeoutId);
499
+ if (startAckTimeoutId)
500
+ clearTimeout(startAckTimeoutId);
486
501
  dispatchApiEvent(CONTROL_MSG.SUBSCRIPTION_ACK, { query: query, variables: variables }, 'Connection established for subscription');
487
502
  var subscriptionState = SUBSCRIPTION_STATUS.CONNECTED;
488
- this.subscriptionObserverMap.set(id, {
489
- observer: observer,
490
- query: query,
491
- variables: variables,
492
- startAckTimeoutId: null,
493
- subscriptionState: subscriptionState,
494
- subscriptionReadyCallback: subscriptionReadyCallback,
495
- subscriptionFailedCallback: subscriptionFailedCallback,
496
- });
503
+ if (observer) {
504
+ this.subscriptionObserverMap.set(id, {
505
+ observer: observer,
506
+ query: query,
507
+ variables: variables,
508
+ startAckTimeoutId: undefined,
509
+ subscriptionState: subscriptionState,
510
+ subscriptionReadyCallback: subscriptionReadyCallback,
511
+ subscriptionFailedCallback: subscriptionFailedCallback,
512
+ });
513
+ }
497
514
  // TODO: emit event on hub but it requires to store the id first
498
515
  return;
499
516
  }
500
517
  if (type === MESSAGE_TYPES.GQL_CONNECTION_KEEP_ALIVE) {
501
- clearTimeout(this.keepAliveTimeoutId);
518
+ if (this.keepAliveTimeoutId)
519
+ clearTimeout(this.keepAliveTimeoutId);
502
520
  this.keepAliveTimeoutId = setTimeout(this._errorDisconnect.bind(this, CONTROL_MSG.TIMEOUT_DISCONNECT), this.keepAliveTimeout);
503
521
  return;
504
522
  }
505
523
  if (type === MESSAGE_TYPES.GQL_ERROR) {
506
524
  var subscriptionState = SUBSCRIPTION_STATUS.FAILED;
507
- this.subscriptionObserverMap.set(id, {
508
- observer: observer,
509
- query: query,
510
- variables: variables,
511
- startAckTimeoutId: startAckTimeoutId,
512
- subscriptionReadyCallback: subscriptionReadyCallback,
513
- subscriptionFailedCallback: subscriptionFailedCallback,
514
- subscriptionState: subscriptionState,
515
- });
516
- observer.error({
517
- errors: [
518
- __assign({}, new GraphQLError(CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
519
- ],
520
- });
521
- clearTimeout(startAckTimeoutId);
522
- observer.complete();
523
- if (typeof subscriptionFailedCallback === 'function') {
524
- subscriptionFailedCallback();
525
+ if (observer) {
526
+ this.subscriptionObserverMap.set(id, {
527
+ observer: observer,
528
+ query: query,
529
+ variables: variables,
530
+ startAckTimeoutId: startAckTimeoutId,
531
+ subscriptionReadyCallback: subscriptionReadyCallback,
532
+ subscriptionFailedCallback: subscriptionFailedCallback,
533
+ subscriptionState: subscriptionState,
534
+ });
535
+ observer.error({
536
+ errors: [
537
+ __assign({}, new GraphQLError(CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
538
+ ],
539
+ });
540
+ if (startAckTimeoutId)
541
+ clearTimeout(startAckTimeoutId);
542
+ observer.complete();
543
+ if (typeof subscriptionFailedCallback === 'function') {
544
+ subscriptionFailedCallback();
545
+ }
525
546
  }
526
547
  }
527
548
  };
@@ -542,29 +563,32 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
542
563
  this.socketStatus = SOCKET_STATUS.CLOSED;
543
564
  };
544
565
  AWSAppSyncRealTimeProvider.prototype._timeoutStartSubscriptionAck = function (subscriptionId) {
545
- var _a = this.subscriptionObserverMap.get(subscriptionId) || {}, observer = _a.observer, query = _a.query, variables = _a.variables;
546
- if (!observer) {
547
- return;
548
- }
549
- this.subscriptionObserverMap.set(subscriptionId, {
550
- observer: observer,
551
- query: query,
552
- variables: variables,
553
- subscriptionState: SUBSCRIPTION_STATUS.FAILED,
554
- });
555
- if (observer && !observer.closed) {
556
- observer.error({
557
- errors: [
558
- __assign({}, new GraphQLError("Subscription timeout " + JSON.stringify({
559
- query: query,
560
- variables: variables,
561
- }))),
562
- ],
566
+ var subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
567
+ if (subscriptionObserver) {
568
+ var observer = subscriptionObserver.observer, query = subscriptionObserver.query, variables = subscriptionObserver.variables;
569
+ if (!observer) {
570
+ return;
571
+ }
572
+ this.subscriptionObserverMap.set(subscriptionId, {
573
+ observer: observer,
574
+ query: query,
575
+ variables: variables,
576
+ subscriptionState: SUBSCRIPTION_STATUS.FAILED,
563
577
  });
564
- // Cleanup will be automatically executed
565
- observer.complete();
578
+ if (observer && !observer.closed) {
579
+ observer.error({
580
+ errors: [
581
+ __assign({}, new GraphQLError("Subscription timeout " + JSON.stringify({
582
+ query: query,
583
+ variables: variables,
584
+ }))),
585
+ ],
586
+ });
587
+ // Cleanup will be automatically executed
588
+ observer.complete();
589
+ }
590
+ logger.debug('timeoutStartSubscription', JSON.stringify({ query: query, variables: variables }));
566
591
  }
567
- logger.debug('timeoutStartSubscription', JSON.stringify({ query: query, variables: variables }));
568
592
  };
569
593
  AWSAppSyncRealTimeProvider.prototype._initializeWebSocketConnection = function (_a) {
570
594
  var _this = this;
@@ -598,19 +622,22 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
598
622
  headerString = _b.apply(_a, [_c.sent()]);
599
623
  headerQs = Buffer.from(headerString).toString('base64');
600
624
  payloadQs = Buffer.from(payloadString).toString('base64');
601
- discoverableEndpoint = appSyncGraphqlEndpoint;
625
+ discoverableEndpoint = appSyncGraphqlEndpoint !== null && appSyncGraphqlEndpoint !== void 0 ? appSyncGraphqlEndpoint : '';
602
626
  if (this.isCustomDomain(discoverableEndpoint)) {
603
- discoverableEndpoint = discoverableEndpoint.concat(customDomainPath);
627
+ discoverableEndpoint =
628
+ discoverableEndpoint.concat(customDomainPath);
604
629
  }
605
630
  else {
606
- discoverableEndpoint = discoverableEndpoint.replace('appsync-api', 'appsync-realtime-api').replace('gogi-beta', 'grt-beta');
631
+ discoverableEndpoint = discoverableEndpoint
632
+ .replace('appsync-api', 'appsync-realtime-api')
633
+ .replace('gogi-beta', 'grt-beta');
607
634
  }
608
635
  protocol = this.isSSLEnabled ? 'wss://' : 'ws://';
609
636
  discoverableEndpoint = discoverableEndpoint
610
637
  .replace('https://', protocol)
611
638
  .replace('http://', protocol);
612
639
  awsRealTimeUrl = discoverableEndpoint + "?header=" + headerQs + "&payload=" + payloadQs;
613
- return [4 /*yield*/, this._initializeRetryableHandshake({ awsRealTimeUrl: awsRealTimeUrl })];
640
+ return [4 /*yield*/, this._initializeRetryableHandshake(awsRealTimeUrl)];
614
641
  case 3:
615
642
  _c.sent();
616
643
  this.promiseArray.forEach(function (_a) {
@@ -632,7 +659,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
632
659
  this.awsRealTimeSocket.readyState === WebSocket.OPEN) {
633
660
  this.awsRealTimeSocket.close(3001);
634
661
  }
635
- this.awsRealTimeSocket = null;
662
+ this.awsRealTimeSocket = undefined;
636
663
  this.socketStatus = SOCKET_STATUS.CLOSED;
637
664
  return [3 /*break*/, 5];
638
665
  case 5: return [2 /*return*/];
@@ -640,25 +667,23 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
640
667
  });
641
668
  }); });
642
669
  };
643
- AWSAppSyncRealTimeProvider.prototype._initializeRetryableHandshake = function (_a) {
644
- var awsRealTimeUrl = _a.awsRealTimeUrl;
670
+ AWSAppSyncRealTimeProvider.prototype._initializeRetryableHandshake = function (awsRealTimeUrl) {
645
671
  return __awaiter(this, void 0, void 0, function () {
646
- return __generator(this, function (_b) {
647
- switch (_b.label) {
672
+ return __generator(this, function (_a) {
673
+ switch (_a.label) {
648
674
  case 0:
649
675
  logger.debug("Initializaling retryable Handshake");
650
- return [4 /*yield*/, jitteredExponentialRetry(this._initializeHandshake.bind(this), [{ awsRealTimeUrl: awsRealTimeUrl }], MAX_DELAY_MS)];
676
+ return [4 /*yield*/, jitteredExponentialRetry(this._initializeHandshake.bind(this), [awsRealTimeUrl], MAX_DELAY_MS)];
651
677
  case 1:
652
- _b.sent();
678
+ _a.sent();
653
679
  return [2 /*return*/];
654
680
  }
655
681
  });
656
682
  });
657
683
  };
658
- AWSAppSyncRealTimeProvider.prototype._initializeHandshake = function (_a) {
659
- var awsRealTimeUrl = _a.awsRealTimeUrl;
684
+ AWSAppSyncRealTimeProvider.prototype._initializeHandshake = function (awsRealTimeUrl) {
660
685
  return __awaiter(this, void 0, void 0, function () {
661
- var err_4, errorType, errorCode;
686
+ var err_4, _a, errorType, errorCode;
662
687
  var _this = this;
663
688
  return __generator(this, function (_b) {
664
689
  switch (_b.label) {
@@ -687,48 +712,53 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
687
712
  // Step 2: wait for ack from AWS AppSyncReaTime after sending init
688
713
  return [4 /*yield*/, (function () {
689
714
  return new Promise(function (res, rej) {
690
- var ackOk = false;
691
- _this.awsRealTimeSocket.onerror = function (error) {
692
- logger.debug("WebSocket error " + JSON.stringify(error));
693
- };
694
- _this.awsRealTimeSocket.onclose = function (event) {
695
- logger.debug("WebSocket closed " + event.reason);
696
- rej(new Error(JSON.stringify(event)));
697
- };
698
- _this.awsRealTimeSocket.onmessage = function (message) {
699
- logger.debug("subscription message from AWS AppSyncRealTime: " + message.data + " ");
700
- var data = JSON.parse(message.data);
701
- var type = data.type, _a = data.payload, _b = (_a === void 0 ? {} : _a).connectionTimeoutMs, connectionTimeoutMs = _b === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _b;
702
- if (type === MESSAGE_TYPES.GQL_CONNECTION_ACK) {
703
- ackOk = true;
704
- _this.keepAliveTimeout = connectionTimeoutMs;
705
- _this.awsRealTimeSocket.onmessage = _this._handleIncomingSubscriptionMessage.bind(_this);
706
- _this.awsRealTimeSocket.onerror = function (err) {
707
- logger.debug(err);
708
- _this._errorDisconnect(CONTROL_MSG.CONNECTION_CLOSED);
709
- };
710
- _this.awsRealTimeSocket.onclose = function (event) {
711
- logger.debug("WebSocket closed " + event.reason);
712
- _this._errorDisconnect(CONTROL_MSG.CONNECTION_CLOSED);
713
- };
714
- res('Cool, connected to AWS AppSyncRealTime');
715
- return;
716
- }
717
- if (type === MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
718
- 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;
719
- rej({ errorType: errorType, errorCode: errorCode });
720
- }
721
- };
722
- var gqlInit = {
723
- type: MESSAGE_TYPES.GQL_CONNECTION_INIT,
724
- };
725
- _this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
726
- function checkAckOk() {
715
+ if (_this.awsRealTimeSocket) {
716
+ var ackOk_1 = false;
717
+ _this.awsRealTimeSocket.onerror = function (error) {
718
+ logger.debug("WebSocket error " + JSON.stringify(error));
719
+ };
720
+ _this.awsRealTimeSocket.onclose = function (event) {
721
+ logger.debug("WebSocket closed " + event.reason);
722
+ rej(new Error(JSON.stringify(event)));
723
+ };
724
+ _this.awsRealTimeSocket.onmessage = function (message) {
725
+ logger.debug("subscription message from AWS AppSyncRealTime: " + message.data + " ");
726
+ var data = JSON.parse(message.data);
727
+ var type = data.type, _a = data.payload, _b = (_a === void 0 ? {} : _a).connectionTimeoutMs, connectionTimeoutMs = _b === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _b;
728
+ if (type === MESSAGE_TYPES.GQL_CONNECTION_ACK) {
729
+ ackOk_1 = true;
730
+ if (_this.awsRealTimeSocket) {
731
+ _this.keepAliveTimeout = connectionTimeoutMs;
732
+ _this.awsRealTimeSocket.onmessage =
733
+ _this._handleIncomingSubscriptionMessage.bind(_this);
734
+ _this.awsRealTimeSocket.onerror = function (err) {
735
+ logger.debug(err);
736
+ _this._errorDisconnect(CONTROL_MSG.CONNECTION_CLOSED);
737
+ };
738
+ _this.awsRealTimeSocket.onclose = function (event) {
739
+ logger.debug("WebSocket closed " + event.reason);
740
+ _this._errorDisconnect(CONTROL_MSG.CONNECTION_CLOSED);
741
+ };
742
+ }
743
+ res('Cool, connected to AWS AppSyncRealTime');
744
+ return;
745
+ }
746
+ if (type === MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
747
+ 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;
748
+ rej({ errorType: errorType, errorCode: errorCode });
749
+ }
750
+ };
751
+ var gqlInit = {
752
+ type: MESSAGE_TYPES.GQL_CONNECTION_INIT,
753
+ };
754
+ _this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
755
+ setTimeout(checkAckOk.bind(_this, ackOk_1), CONNECTION_INIT_TIMEOUT);
756
+ }
757
+ function checkAckOk(ackOk) {
727
758
  if (!ackOk) {
728
759
  rej(new Error("Connection timeout: ack from AWSRealTime was not received on " + CONNECTION_INIT_TIMEOUT + " ms"));
729
760
  }
730
761
  }
731
- setTimeout(checkAckOk.bind(_this), CONNECTION_INIT_TIMEOUT);
732
762
  });
733
763
  })()];
734
764
  case 3:
@@ -737,7 +767,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
737
767
  return [3 /*break*/, 5];
738
768
  case 4:
739
769
  err_4 = _b.sent();
740
- errorType = err_4.errorType, errorCode = err_4.errorCode;
770
+ _a = err_4, errorType = _a.errorType, errorCode = _a.errorCode;
741
771
  if (NON_RETRYABLE_CODES.includes(errorCode)) {
742
772
  throw new NonRetryableError(errorType);
743
773
  }
@@ -767,12 +797,12 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
767
797
  AMAZON_COGNITO_USER_POOLS: this._awsRealTimeCUPHeader.bind(this),
768
798
  AWS_LAMBDA: this._customAuthHeader,
769
799
  };
800
+ if (!(!authenticationType || !headerHandler[authenticationType])) return [3 /*break*/, 1];
801
+ logger.debug("Authentication type " + authenticationType + " not supported");
802
+ return [2 /*return*/, ''];
803
+ case 1:
770
804
  handler = headerHandler[authenticationType];
771
- if (typeof handler !== 'function') {
772
- logger.debug("Authentication type " + authenticationType + " not supported");
773
- return [2 /*return*/, ''];
774
- }
775
- host = url.parse(appSyncGraphqlEndpoint).host;
805
+ host = url.parse(appSyncGraphqlEndpoint !== null && appSyncGraphqlEndpoint !== void 0 ? appSyncGraphqlEndpoint : '').host;
776
806
  return [4 /*yield*/, handler({
777
807
  payload: payload,
778
808
  canonicalUri: canonicalUri,
@@ -782,7 +812,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
782
812
  host: host,
783
813
  additionalHeaders: additionalHeaders,
784
814
  })];
785
- case 1:
815
+ case 2:
786
816
  result = _b.sent();
787
817
  return [2 /*return*/, result];
788
818
  }
@@ -869,11 +899,14 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
869
899
  if (!credentialsOK) {
870
900
  throw new Error('No credentials');
871
901
  }
872
- return [4 /*yield*/, Credentials.get().then(function (credentials) { return ({
873
- secret_key: credentials.secretAccessKey,
874
- access_key: credentials.accessKeyId,
875
- session_token: credentials.sessionToken,
876
- }); })];
902
+ return [4 /*yield*/, Credentials.get().then(function (credentials) {
903
+ var _a = credentials, secretAccessKey = _a.secretAccessKey, accessKeyId = _a.accessKeyId, sessionToken = _a.sessionToken;
904
+ return {
905
+ secret_key: secretAccessKey,
906
+ access_key: accessKeyId,
907
+ session_token: sessionToken,
908
+ };
909
+ })];
877
910
  case 2:
878
911
  creds = _b.sent();
879
912
  request = {
@@ -890,7 +923,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
890
923
  };
891
924
  AWSAppSyncRealTimeProvider.prototype._customAuthHeader = function (_a) {
892
925
  var host = _a.host, additionalHeaders = _a.additionalHeaders;
893
- if (!additionalHeaders.Authorization) {
926
+ if (!additionalHeaders || !additionalHeaders['Authorization']) {
894
927
  throw new Error('No auth token specified');
895
928
  }
896
929
  return {