@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
@@ -109,8 +109,7 @@ var auth_1 = __importDefault(require("@aws-amplify/auth"));
109
109
  var PubSubProvider_1 = require("./PubSubProvider");
110
110
  var index_1 = require("../index");
111
111
  var logger = new core_1.Logger('AWSAppSyncRealTimeProvider');
112
- var AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' &&
113
- typeof Symbol.for === 'function'
112
+ var AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
114
113
  ? Symbol.for('amplify_default')
115
114
  : '@@amplify_default');
116
115
  var dispatchApiEvent = function (event, data, message) {
@@ -231,9 +230,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
231
230
  };
232
231
  AWSAppSyncRealTimeProvider.prototype.subscribe = function (_topics, options) {
233
232
  var _this = this;
234
- var appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint;
233
+ var appSyncGraphqlEndpoint = options === null || options === void 0 ? void 0 : options.appSyncGraphqlEndpoint;
235
234
  return new zen_observable_ts_1.default(function (observer) {
236
- if (!appSyncGraphqlEndpoint) {
235
+ if (!options || !appSyncGraphqlEndpoint) {
237
236
  observer.error({
238
237
  errors: [
239
238
  __assign({}, new graphql_1.GraphQLError("Subscribe only available for AWS AppSync endpoint")),
@@ -302,14 +301,15 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
302
301
  });
303
302
  AWSAppSyncRealTimeProvider.prototype._startSubscriptionWithAWSAppSyncRealTime = function (_a) {
304
303
  var options = _a.options, observer = _a.observer, subscriptionId = _a.subscriptionId;
304
+ var _b, _c;
305
305
  return __awaiter(this, void 0, void 0, function () {
306
- 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;
307
- var _h;
306
+ 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;
307
+ var _j;
308
308
  var _this = this;
309
- return __generator(this, function (_j) {
310
- switch (_j.label) {
309
+ return __generator(this, function (_k) {
310
+ switch (_k.label) {
311
311
  case 0:
312
- 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;
312
+ 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;
313
313
  subscriptionState = SUBSCRIPTION_STATUS.PENDING;
314
314
  data = {
315
315
  query: query,
@@ -318,13 +318,13 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
318
318
  // Having a subscription id map will make it simple to forward messages received
319
319
  this.subscriptionObserverMap.set(subscriptionId, {
320
320
  observer: observer,
321
- query: query,
322
- variables: variables,
321
+ query: query !== null && query !== void 0 ? query : '',
322
+ variables: variables !== null && variables !== void 0 ? variables : {},
323
323
  subscriptionState: subscriptionState,
324
- startAckTimeoutId: null,
324
+ startAckTimeoutId: undefined,
325
325
  });
326
326
  dataString = JSON.stringify(data);
327
- _d = [{}];
327
+ _f = [{}];
328
328
  return [4 /*yield*/, this._awsRealTimeHeaderBasedAuth({
329
329
  apiKey: apiKey,
330
330
  appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
@@ -335,10 +335,10 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
335
335
  additionalHeaders: additionalHeaders,
336
336
  })];
337
337
  case 1:
338
- _e = [__assign.apply(void 0, _d.concat([(_j.sent())]))];
338
+ _g = [__assign.apply(void 0, _f.concat([(_k.sent())]))];
339
339
  return [4 /*yield*/, graphql_headers()];
340
340
  case 2:
341
- headerObj = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _e.concat([(_j.sent())])), additionalHeaders]), (_h = {}, _h[core_1.USER_AGENT_HEADER] = core_1.Constants.userAgent, _h)]);
341
+ headerObj = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _g.concat([(_k.sent())])), additionalHeaders]), (_j = {}, _j[core_1.USER_AGENT_HEADER] = core_1.Constants.userAgent, _j)]);
342
342
  subscriptionMessage = {
343
343
  id: subscriptionId,
344
344
  payload: {
@@ -350,9 +350,9 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
350
350
  type: MESSAGE_TYPES.GQL_START,
351
351
  };
352
352
  stringToAWSRealTime = JSON.stringify(subscriptionMessage);
353
- _j.label = 3;
353
+ _k.label = 3;
354
354
  case 3:
355
- _j.trys.push([3, 5, , 6]);
355
+ _k.trys.push([3, 5, , 6]);
356
356
  return [4 /*yield*/, this._initializeWebSocketConnection({
357
357
  apiKey: apiKey,
358
358
  appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
@@ -361,12 +361,12 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
361
361
  additionalHeaders: additionalHeaders,
362
362
  })];
363
363
  case 4:
364
- _j.sent();
364
+ _k.sent();
365
365
  return [3 /*break*/, 6];
366
366
  case 5:
367
- err_2 = _j.sent();
367
+ err_2 = _k.sent();
368
368
  logger.debug({ err: err_2 });
369
- _f = err_2.message, message = _f === void 0 ? '' : _f;
369
+ message = (_b = err_2['message']) !== null && _b !== void 0 ? _b : '';
370
370
  observer.error({
371
371
  errors: [
372
372
  __assign({}, new graphql_1.GraphQLError(index_1.CONTROL_MSG.CONNECTION_FAILED + ": " + message)),
@@ -380,13 +380,13 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
380
380
  }
381
381
  return [2 /*return*/];
382
382
  case 6:
383
- _g = this.subscriptionObserverMap.get(subscriptionId), subscriptionFailedCallback = _g.subscriptionFailedCallback, subscriptionReadyCallback = _g.subscriptionReadyCallback;
383
+ _h = (_c = this.subscriptionObserverMap.get(subscriptionId)) !== null && _c !== void 0 ? _c : {}, subscriptionFailedCallback = _h.subscriptionFailedCallback, subscriptionReadyCallback = _h.subscriptionReadyCallback;
384
384
  // This must be done before sending the message in order to be listening immediately
385
385
  this.subscriptionObserverMap.set(subscriptionId, {
386
386
  observer: observer,
387
387
  subscriptionState: subscriptionState,
388
- variables: variables,
389
- query: query,
388
+ query: query !== null && query !== void 0 ? query : '',
389
+ variables: variables !== null && variables !== void 0 ? variables : {},
390
390
  subscriptionReadyCallback: subscriptionReadyCallback,
391
391
  subscriptionFailedCallback: subscriptionFailedCallback,
392
392
  startAckTimeoutId: setTimeout(function () {
@@ -404,23 +404,26 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
404
404
  // Waiting that subscription has been connected before trying to unsubscribe
405
405
  AWSAppSyncRealTimeProvider.prototype._waitForSubscriptionToBeConnected = function (subscriptionId) {
406
406
  return __awaiter(this, void 0, void 0, function () {
407
- var subscriptionState;
407
+ var subscriptionObserver, subscriptionState;
408
408
  var _this = this;
409
409
  return __generator(this, function (_a) {
410
- subscriptionState = this.subscriptionObserverMap.get(subscriptionId).subscriptionState;
411
- // This in case unsubscribe is invoked before sending start subscription message
412
- if (subscriptionState === SUBSCRIPTION_STATUS.PENDING) {
413
- return [2 /*return*/, new Promise(function (res, rej) {
414
- var _a = _this.subscriptionObserverMap.get(subscriptionId), observer = _a.observer, subscriptionState = _a.subscriptionState, variables = _a.variables, query = _a.query;
415
- _this.subscriptionObserverMap.set(subscriptionId, {
416
- observer: observer,
417
- subscriptionState: subscriptionState,
418
- variables: variables,
419
- query: query,
420
- subscriptionReadyCallback: res,
421
- subscriptionFailedCallback: rej,
422
- });
423
- })];
410
+ subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
411
+ if (subscriptionObserver) {
412
+ subscriptionState = subscriptionObserver.subscriptionState;
413
+ // This in case unsubscribe is invoked before sending start subscription message
414
+ if (subscriptionState === SUBSCRIPTION_STATUS.PENDING) {
415
+ return [2 /*return*/, new Promise(function (res, rej) {
416
+ var observer = subscriptionObserver.observer, subscriptionState = subscriptionObserver.subscriptionState, variables = subscriptionObserver.variables, query = subscriptionObserver.query;
417
+ _this.subscriptionObserverMap.set(subscriptionId, {
418
+ observer: observer,
419
+ subscriptionState: subscriptionState,
420
+ variables: variables,
421
+ query: query,
422
+ subscriptionReadyCallback: res,
423
+ subscriptionFailedCallback: rej,
424
+ });
425
+ })];
426
+ }
424
427
  }
425
428
  return [2 /*return*/];
426
429
  });
@@ -465,21 +468,32 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
465
468
  }
466
469
  else {
467
470
  logger.debug('closing WebSocket...');
468
- clearTimeout(this.keepAliveTimeoutId);
471
+ if (this.keepAliveTimeoutId)
472
+ clearTimeout(this.keepAliveTimeoutId);
469
473
  var tempSocket = this.awsRealTimeSocket;
470
474
  // Cleaning callbacks to avoid race condition, socket still exists
471
- tempSocket.onclose = undefined;
472
- tempSocket.onerror = undefined;
475
+ tempSocket.onclose = null;
476
+ tempSocket.onerror = null;
473
477
  tempSocket.close(1000);
474
- this.awsRealTimeSocket = null;
478
+ this.awsRealTimeSocket = undefined;
475
479
  this.socketStatus = SOCKET_STATUS.CLOSED;
476
480
  }
477
481
  };
478
482
  AWSAppSyncRealTimeProvider.prototype._handleIncomingSubscriptionMessage = function (message) {
479
- logger.debug("subscription message from AWS AppSync RealTime: " + message.data);
483
+ if (message.data && message.data === '{"type":"ka"}') {
484
+ logger.verbose("subscription message from AWS AppSync RealTime: " + message.data);
485
+ }
486
+ else {
487
+ logger.debug("subscription message from AWS AppSync RealTime: " + message.data);
488
+ }
480
489
  var _a = JSON.parse(message.data), _b = _a.id, id = _b === void 0 ? '' : _b, payload = _a.payload, type = _a.type;
481
490
  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;
482
- logger.debug({ id: id, observer: observer, query: query, variables: variables });
491
+ if (!id && !observer && !query && Object.keys(variables).length === 0) {
492
+ logger.verbose({ id: id, observer: observer, query: query, variables: variables });
493
+ }
494
+ else {
495
+ logger.debug({ id: id, observer: observer, query: query, variables: variables });
496
+ }
483
497
  if (type === MESSAGE_TYPES.GQL_DATA && payload && payload.data) {
484
498
  if (observer) {
485
499
  observer.next(payload);
@@ -494,46 +508,53 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
494
508
  if (typeof subscriptionReadyCallback === 'function') {
495
509
  subscriptionReadyCallback();
496
510
  }
497
- clearTimeout(startAckTimeoutId);
511
+ if (startAckTimeoutId)
512
+ clearTimeout(startAckTimeoutId);
498
513
  dispatchApiEvent(index_1.CONTROL_MSG.SUBSCRIPTION_ACK, { query: query, variables: variables }, 'Connection established for subscription');
499
514
  var subscriptionState = SUBSCRIPTION_STATUS.CONNECTED;
500
- this.subscriptionObserverMap.set(id, {
501
- observer: observer,
502
- query: query,
503
- variables: variables,
504
- startAckTimeoutId: null,
505
- subscriptionState: subscriptionState,
506
- subscriptionReadyCallback: subscriptionReadyCallback,
507
- subscriptionFailedCallback: subscriptionFailedCallback,
508
- });
515
+ if (observer) {
516
+ this.subscriptionObserverMap.set(id, {
517
+ observer: observer,
518
+ query: query,
519
+ variables: variables,
520
+ startAckTimeoutId: undefined,
521
+ subscriptionState: subscriptionState,
522
+ subscriptionReadyCallback: subscriptionReadyCallback,
523
+ subscriptionFailedCallback: subscriptionFailedCallback,
524
+ });
525
+ }
509
526
  // TODO: emit event on hub but it requires to store the id first
510
527
  return;
511
528
  }
512
529
  if (type === MESSAGE_TYPES.GQL_CONNECTION_KEEP_ALIVE) {
513
- clearTimeout(this.keepAliveTimeoutId);
530
+ if (this.keepAliveTimeoutId)
531
+ clearTimeout(this.keepAliveTimeoutId);
514
532
  this.keepAliveTimeoutId = setTimeout(this._errorDisconnect.bind(this, index_1.CONTROL_MSG.TIMEOUT_DISCONNECT), this.keepAliveTimeout);
515
533
  return;
516
534
  }
517
535
  if (type === MESSAGE_TYPES.GQL_ERROR) {
518
536
  var subscriptionState = SUBSCRIPTION_STATUS.FAILED;
519
- this.subscriptionObserverMap.set(id, {
520
- observer: observer,
521
- query: query,
522
- variables: variables,
523
- startAckTimeoutId: startAckTimeoutId,
524
- subscriptionReadyCallback: subscriptionReadyCallback,
525
- subscriptionFailedCallback: subscriptionFailedCallback,
526
- subscriptionState: subscriptionState,
527
- });
528
- observer.error({
529
- errors: [
530
- __assign({}, new graphql_1.GraphQLError(index_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
531
- ],
532
- });
533
- clearTimeout(startAckTimeoutId);
534
- observer.complete();
535
- if (typeof subscriptionFailedCallback === 'function') {
536
- subscriptionFailedCallback();
537
+ if (observer) {
538
+ this.subscriptionObserverMap.set(id, {
539
+ observer: observer,
540
+ query: query,
541
+ variables: variables,
542
+ startAckTimeoutId: startAckTimeoutId,
543
+ subscriptionReadyCallback: subscriptionReadyCallback,
544
+ subscriptionFailedCallback: subscriptionFailedCallback,
545
+ subscriptionState: subscriptionState,
546
+ });
547
+ observer.error({
548
+ errors: [
549
+ __assign({}, new graphql_1.GraphQLError(index_1.CONTROL_MSG.CONNECTION_FAILED + ": " + JSON.stringify(payload))),
550
+ ],
551
+ });
552
+ if (startAckTimeoutId)
553
+ clearTimeout(startAckTimeoutId);
554
+ observer.complete();
555
+ if (typeof subscriptionFailedCallback === 'function') {
556
+ subscriptionFailedCallback();
557
+ }
537
558
  }
538
559
  }
539
560
  };
@@ -554,29 +575,32 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
554
575
  this.socketStatus = SOCKET_STATUS.CLOSED;
555
576
  };
556
577
  AWSAppSyncRealTimeProvider.prototype._timeoutStartSubscriptionAck = function (subscriptionId) {
557
- var _a = this.subscriptionObserverMap.get(subscriptionId) || {}, observer = _a.observer, query = _a.query, variables = _a.variables;
558
- if (!observer) {
559
- return;
560
- }
561
- this.subscriptionObserverMap.set(subscriptionId, {
562
- observer: observer,
563
- query: query,
564
- variables: variables,
565
- subscriptionState: SUBSCRIPTION_STATUS.FAILED,
566
- });
567
- if (observer && !observer.closed) {
568
- observer.error({
569
- errors: [
570
- __assign({}, new graphql_1.GraphQLError("Subscription timeout " + JSON.stringify({
571
- query: query,
572
- variables: variables,
573
- }))),
574
- ],
578
+ var subscriptionObserver = this.subscriptionObserverMap.get(subscriptionId);
579
+ if (subscriptionObserver) {
580
+ var observer = subscriptionObserver.observer, query = subscriptionObserver.query, variables = subscriptionObserver.variables;
581
+ if (!observer) {
582
+ return;
583
+ }
584
+ this.subscriptionObserverMap.set(subscriptionId, {
585
+ observer: observer,
586
+ query: query,
587
+ variables: variables,
588
+ subscriptionState: SUBSCRIPTION_STATUS.FAILED,
575
589
  });
576
- // Cleanup will be automatically executed
577
- observer.complete();
590
+ if (observer && !observer.closed) {
591
+ observer.error({
592
+ errors: [
593
+ __assign({}, new graphql_1.GraphQLError("Subscription timeout " + JSON.stringify({
594
+ query: query,
595
+ variables: variables,
596
+ }))),
597
+ ],
598
+ });
599
+ // Cleanup will be automatically executed
600
+ observer.complete();
601
+ }
602
+ logger.debug('timeoutStartSubscription', JSON.stringify({ query: query, variables: variables }));
578
603
  }
579
- logger.debug('timeoutStartSubscription', JSON.stringify({ query: query, variables: variables }));
580
604
  };
581
605
  AWSAppSyncRealTimeProvider.prototype._initializeWebSocketConnection = function (_a) {
582
606
  var _this = this;
@@ -610,19 +634,22 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
610
634
  headerString = _b.apply(_a, [_c.sent()]);
611
635
  headerQs = buffer_1.Buffer.from(headerString).toString('base64');
612
636
  payloadQs = buffer_1.Buffer.from(payloadString).toString('base64');
613
- discoverableEndpoint = appSyncGraphqlEndpoint;
637
+ discoverableEndpoint = appSyncGraphqlEndpoint !== null && appSyncGraphqlEndpoint !== void 0 ? appSyncGraphqlEndpoint : '';
614
638
  if (this.isCustomDomain(discoverableEndpoint)) {
615
- discoverableEndpoint = discoverableEndpoint.concat(customDomainPath);
639
+ discoverableEndpoint =
640
+ discoverableEndpoint.concat(customDomainPath);
616
641
  }
617
642
  else {
618
- discoverableEndpoint = discoverableEndpoint.replace('appsync-api', 'appsync-realtime-api').replace('gogi-beta', 'grt-beta');
643
+ discoverableEndpoint = discoverableEndpoint
644
+ .replace('appsync-api', 'appsync-realtime-api')
645
+ .replace('gogi-beta', 'grt-beta');
619
646
  }
620
647
  protocol = this.isSSLEnabled ? 'wss://' : 'ws://';
621
648
  discoverableEndpoint = discoverableEndpoint
622
649
  .replace('https://', protocol)
623
650
  .replace('http://', protocol);
624
651
  awsRealTimeUrl = discoverableEndpoint + "?header=" + headerQs + "&payload=" + payloadQs;
625
- return [4 /*yield*/, this._initializeRetryableHandshake({ awsRealTimeUrl: awsRealTimeUrl })];
652
+ return [4 /*yield*/, this._initializeRetryableHandshake(awsRealTimeUrl)];
626
653
  case 3:
627
654
  _c.sent();
628
655
  this.promiseArray.forEach(function (_a) {
@@ -644,7 +671,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
644
671
  this.awsRealTimeSocket.readyState === WebSocket.OPEN) {
645
672
  this.awsRealTimeSocket.close(3001);
646
673
  }
647
- this.awsRealTimeSocket = null;
674
+ this.awsRealTimeSocket = undefined;
648
675
  this.socketStatus = SOCKET_STATUS.CLOSED;
649
676
  return [3 /*break*/, 5];
650
677
  case 5: return [2 /*return*/];
@@ -652,25 +679,23 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
652
679
  });
653
680
  }); });
654
681
  };
655
- AWSAppSyncRealTimeProvider.prototype._initializeRetryableHandshake = function (_a) {
656
- var awsRealTimeUrl = _a.awsRealTimeUrl;
682
+ AWSAppSyncRealTimeProvider.prototype._initializeRetryableHandshake = function (awsRealTimeUrl) {
657
683
  return __awaiter(this, void 0, void 0, function () {
658
- return __generator(this, function (_b) {
659
- switch (_b.label) {
684
+ return __generator(this, function (_a) {
685
+ switch (_a.label) {
660
686
  case 0:
661
687
  logger.debug("Initializaling retryable Handshake");
662
- return [4 /*yield*/, core_1.jitteredExponentialRetry(this._initializeHandshake.bind(this), [{ awsRealTimeUrl: awsRealTimeUrl }], MAX_DELAY_MS)];
688
+ return [4 /*yield*/, core_1.jitteredExponentialRetry(this._initializeHandshake.bind(this), [awsRealTimeUrl], MAX_DELAY_MS)];
663
689
  case 1:
664
- _b.sent();
690
+ _a.sent();
665
691
  return [2 /*return*/];
666
692
  }
667
693
  });
668
694
  });
669
695
  };
670
- AWSAppSyncRealTimeProvider.prototype._initializeHandshake = function (_a) {
671
- var awsRealTimeUrl = _a.awsRealTimeUrl;
696
+ AWSAppSyncRealTimeProvider.prototype._initializeHandshake = function (awsRealTimeUrl) {
672
697
  return __awaiter(this, void 0, void 0, function () {
673
- var err_4, errorType, errorCode;
698
+ var err_4, _a, errorType, errorCode;
674
699
  var _this = this;
675
700
  return __generator(this, function (_b) {
676
701
  switch (_b.label) {
@@ -699,48 +724,53 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
699
724
  // Step 2: wait for ack from AWS AppSyncReaTime after sending init
700
725
  return [4 /*yield*/, (function () {
701
726
  return new Promise(function (res, rej) {
702
- var ackOk = false;
703
- _this.awsRealTimeSocket.onerror = function (error) {
704
- logger.debug("WebSocket error " + JSON.stringify(error));
705
- };
706
- _this.awsRealTimeSocket.onclose = function (event) {
707
- logger.debug("WebSocket closed " + event.reason);
708
- rej(new Error(JSON.stringify(event)));
709
- };
710
- _this.awsRealTimeSocket.onmessage = function (message) {
711
- logger.debug("subscription message from AWS AppSyncRealTime: " + message.data + " ");
712
- var data = JSON.parse(message.data);
713
- var type = data.type, _a = data.payload, _b = (_a === void 0 ? {} : _a).connectionTimeoutMs, connectionTimeoutMs = _b === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _b;
714
- if (type === MESSAGE_TYPES.GQL_CONNECTION_ACK) {
715
- ackOk = true;
716
- _this.keepAliveTimeout = connectionTimeoutMs;
717
- _this.awsRealTimeSocket.onmessage = _this._handleIncomingSubscriptionMessage.bind(_this);
718
- _this.awsRealTimeSocket.onerror = function (err) {
719
- logger.debug(err);
720
- _this._errorDisconnect(index_1.CONTROL_MSG.CONNECTION_CLOSED);
721
- };
722
- _this.awsRealTimeSocket.onclose = function (event) {
723
- logger.debug("WebSocket closed " + event.reason);
724
- _this._errorDisconnect(index_1.CONTROL_MSG.CONNECTION_CLOSED);
725
- };
726
- res('Cool, connected to AWS AppSyncRealTime');
727
- return;
728
- }
729
- if (type === MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
730
- 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;
731
- rej({ errorType: errorType, errorCode: errorCode });
732
- }
733
- };
734
- var gqlInit = {
735
- type: MESSAGE_TYPES.GQL_CONNECTION_INIT,
736
- };
737
- _this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
738
- function checkAckOk() {
727
+ if (_this.awsRealTimeSocket) {
728
+ var ackOk_1 = false;
729
+ _this.awsRealTimeSocket.onerror = function (error) {
730
+ logger.debug("WebSocket error " + JSON.stringify(error));
731
+ };
732
+ _this.awsRealTimeSocket.onclose = function (event) {
733
+ logger.debug("WebSocket closed " + event.reason);
734
+ rej(new Error(JSON.stringify(event)));
735
+ };
736
+ _this.awsRealTimeSocket.onmessage = function (message) {
737
+ logger.debug("subscription message from AWS AppSyncRealTime: " + message.data + " ");
738
+ var data = JSON.parse(message.data);
739
+ var type = data.type, _a = data.payload, _b = (_a === void 0 ? {} : _a).connectionTimeoutMs, connectionTimeoutMs = _b === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _b;
740
+ if (type === MESSAGE_TYPES.GQL_CONNECTION_ACK) {
741
+ ackOk_1 = true;
742
+ if (_this.awsRealTimeSocket) {
743
+ _this.keepAliveTimeout = connectionTimeoutMs;
744
+ _this.awsRealTimeSocket.onmessage =
745
+ _this._handleIncomingSubscriptionMessage.bind(_this);
746
+ _this.awsRealTimeSocket.onerror = function (err) {
747
+ logger.debug(err);
748
+ _this._errorDisconnect(index_1.CONTROL_MSG.CONNECTION_CLOSED);
749
+ };
750
+ _this.awsRealTimeSocket.onclose = function (event) {
751
+ logger.debug("WebSocket closed " + event.reason);
752
+ _this._errorDisconnect(index_1.CONTROL_MSG.CONNECTION_CLOSED);
753
+ };
754
+ }
755
+ res('Cool, connected to AWS AppSyncRealTime');
756
+ return;
757
+ }
758
+ if (type === MESSAGE_TYPES.GQL_CONNECTION_ERROR) {
759
+ 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;
760
+ rej({ errorType: errorType, errorCode: errorCode });
761
+ }
762
+ };
763
+ var gqlInit = {
764
+ type: MESSAGE_TYPES.GQL_CONNECTION_INIT,
765
+ };
766
+ _this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
767
+ setTimeout(checkAckOk.bind(_this, ackOk_1), CONNECTION_INIT_TIMEOUT);
768
+ }
769
+ function checkAckOk(ackOk) {
739
770
  if (!ackOk) {
740
771
  rej(new Error("Connection timeout: ack from AWSRealTime was not received on " + CONNECTION_INIT_TIMEOUT + " ms"));
741
772
  }
742
773
  }
743
- setTimeout(checkAckOk.bind(_this), CONNECTION_INIT_TIMEOUT);
744
774
  });
745
775
  })()];
746
776
  case 3:
@@ -749,7 +779,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
749
779
  return [3 /*break*/, 5];
750
780
  case 4:
751
781
  err_4 = _b.sent();
752
- errorType = err_4.errorType, errorCode = err_4.errorCode;
782
+ _a = err_4, errorType = _a.errorType, errorCode = _a.errorCode;
753
783
  if (NON_RETRYABLE_CODES.includes(errorCode)) {
754
784
  throw new core_1.NonRetryableError(errorType);
755
785
  }
@@ -779,12 +809,12 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
779
809
  AMAZON_COGNITO_USER_POOLS: this._awsRealTimeCUPHeader.bind(this),
780
810
  AWS_LAMBDA: this._customAuthHeader,
781
811
  };
812
+ if (!(!authenticationType || !headerHandler[authenticationType])) return [3 /*break*/, 1];
813
+ logger.debug("Authentication type " + authenticationType + " not supported");
814
+ return [2 /*return*/, ''];
815
+ case 1:
782
816
  handler = headerHandler[authenticationType];
783
- if (typeof handler !== 'function') {
784
- logger.debug("Authentication type " + authenticationType + " not supported");
785
- return [2 /*return*/, ''];
786
- }
787
- host = url.parse(appSyncGraphqlEndpoint).host;
817
+ host = url.parse(appSyncGraphqlEndpoint !== null && appSyncGraphqlEndpoint !== void 0 ? appSyncGraphqlEndpoint : '').host;
788
818
  return [4 /*yield*/, handler({
789
819
  payload: payload,
790
820
  canonicalUri: canonicalUri,
@@ -794,7 +824,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
794
824
  host: host,
795
825
  additionalHeaders: additionalHeaders,
796
826
  })];
797
- case 1:
827
+ case 2:
798
828
  result = _b.sent();
799
829
  return [2 /*return*/, result];
800
830
  }
@@ -881,11 +911,14 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
881
911
  if (!credentialsOK) {
882
912
  throw new Error('No credentials');
883
913
  }
884
- return [4 /*yield*/, core_1.Credentials.get().then(function (credentials) { return ({
885
- secret_key: credentials.secretAccessKey,
886
- access_key: credentials.accessKeyId,
887
- session_token: credentials.sessionToken,
888
- }); })];
914
+ return [4 /*yield*/, core_1.Credentials.get().then(function (credentials) {
915
+ var _a = credentials, secretAccessKey = _a.secretAccessKey, accessKeyId = _a.accessKeyId, sessionToken = _a.sessionToken;
916
+ return {
917
+ secret_key: secretAccessKey,
918
+ access_key: accessKeyId,
919
+ session_token: sessionToken,
920
+ };
921
+ })];
889
922
  case 2:
890
923
  creds = _b.sent();
891
924
  request = {
@@ -902,7 +935,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function (_super) {
902
935
  };
903
936
  AWSAppSyncRealTimeProvider.prototype._customAuthHeader = function (_a) {
904
937
  var host = _a.host, additionalHeaders = _a.additionalHeaders;
905
- if (!additionalHeaders.Authorization) {
938
+ if (!additionalHeaders || !additionalHeaders['Authorization']) {
906
939
  throw new Error('No auth token specified');
907
940
  }
908
941
  return {