@aws-amplify/pubsub 4.3.1 → 4.3.2-unstable.5

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