@aws-amplify/api-graphql 4.7.3-unstable.25dbd61.0 → 4.7.3-unstable.86d787b.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.
@@ -23,7 +23,7 @@ import {
23
23
  CONNECTION_INIT_TIMEOUT,
24
24
  CONNECTION_STATE_CHANGE,
25
25
  DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT,
26
- DEFAULT_KEEP_ALIVE_HEARTBEAT_TIMEOUT,
26
+ DEFAULT_KEEP_ALIVE_TIMEOUT,
27
27
  MAX_DELAY_MS,
28
28
  MESSAGE_TYPES,
29
29
  NON_RETRYABLE_CODES,
@@ -83,8 +83,9 @@ export abstract class AWSWebSocketProvider {
83
83
 
84
84
  protected awsRealTimeSocket?: WebSocket;
85
85
  private socketStatus: SOCKET_STATUS = SOCKET_STATUS.CLOSED;
86
- private keepAliveTimestamp: number = Date.now();
87
- private keepAliveHeartbeatIntervalId?: ReturnType<typeof setInterval>;
86
+ private keepAliveTimeoutId?: ReturnType<typeof setTimeout>;
87
+ private keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
88
+ private keepAliveAlertTimeoutId?: ReturnType<typeof setTimeout>;
88
89
  private promiseArray: { res(): void; rej(reason?: any): void }[] = [];
89
90
  private connectionState: ConnectionState | undefined;
90
91
  private readonly connectionStateMonitor = new ConnectionStateMonitor();
@@ -118,7 +119,6 @@ export abstract class AWSWebSocketProvider {
118
119
  return new Promise<void>((resolve, reject) => {
119
120
  if (this.awsRealTimeSocket) {
120
121
  this.awsRealTimeSocket.onclose = (_: CloseEvent) => {
121
- this._closeSocket();
122
122
  this.subscriptionObserverMap = new Map();
123
123
  this.awsRealTimeSocket = undefined;
124
124
  resolve();
@@ -171,7 +171,7 @@ export abstract class AWSWebSocketProvider {
171
171
  this.logger.debug(
172
172
  `${CONTROL_MSG.REALTIME_SUBSCRIPTION_INIT_ERROR}: ${err}`,
173
173
  );
174
- this._closeSocket();
174
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
175
175
  })
176
176
  .finally(() => {
177
177
  subscriptionStartInProgress = false;
@@ -435,7 +435,7 @@ export abstract class AWSWebSocketProvider {
435
435
  this.logger.debug({ err });
436
436
  const message = String(err.message ?? '');
437
437
  // Resolving to give the state observer time to propogate the update
438
- this._closeSocket();
438
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
439
439
 
440
440
  // Capture the error only when the network didn't cause disruption
441
441
  if (
@@ -544,7 +544,12 @@ export abstract class AWSWebSocketProvider {
544
544
  setTimeout(this._closeSocketIfRequired.bind(this), 1000);
545
545
  } else {
546
546
  this.logger.debug('closing WebSocket...');
547
-
547
+ if (this.keepAliveTimeoutId) {
548
+ clearTimeout(this.keepAliveTimeoutId);
549
+ }
550
+ if (this.keepAliveAlertTimeoutId) {
551
+ clearTimeout(this.keepAliveAlertTimeoutId);
552
+ }
548
553
  const tempSocket = this.awsRealTimeSocket;
549
554
  // Cleaning callbacks to avoid race condition, socket still exists
550
555
  tempSocket.onclose = null;
@@ -552,7 +557,7 @@ export abstract class AWSWebSocketProvider {
552
557
  tempSocket.close(1000);
553
558
  this.awsRealTimeSocket = undefined;
554
559
  this.socketStatus = SOCKET_STATUS.CLOSED;
555
- this._closeSocket();
560
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
556
561
  }
557
562
  }
558
563
 
@@ -572,40 +577,13 @@ export abstract class AWSWebSocketProvider {
572
577
  errorType: string;
573
578
  };
574
579
 
575
- private maintainKeepAlive() {
576
- this.keepAliveTimestamp = Date.now();
577
- }
578
-
579
- private keepAliveHeartbeat(connectionTimeoutMs: number) {
580
- const currentTime = Date.now();
581
-
582
- // Check for missed KA message
583
- if (
584
- currentTime - this.keepAliveTimestamp >
585
- DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT
586
- ) {
587
- this.connectionStateMonitor.record(CONNECTION_CHANGE.KEEP_ALIVE_MISSED);
588
- } else {
589
- this.connectionStateMonitor.record(CONNECTION_CHANGE.KEEP_ALIVE);
590
- }
591
-
592
- // Recognize we are disconnected if we haven't seen messages in the keep alive timeout period
593
- if (currentTime - this.keepAliveTimestamp > connectionTimeoutMs) {
594
- this._errorDisconnect(CONTROL_MSG.TIMEOUT_DISCONNECT);
595
- }
596
- }
597
-
598
580
  private _handleIncomingSubscriptionMessage(message: MessageEvent) {
599
581
  if (typeof message.data !== 'string') {
600
582
  return;
601
583
  }
602
584
 
603
585
  const [isData, data] = this._handleSubscriptionData(message);
604
- if (isData) {
605
- this.maintainKeepAlive();
606
-
607
- return;
608
- }
586
+ if (isData) return;
609
587
 
610
588
  const { type, id, payload } = data;
611
589
 
@@ -654,7 +632,16 @@ export abstract class AWSWebSocketProvider {
654
632
  }
655
633
 
656
634
  if (type === MESSAGE_TYPES.GQL_CONNECTION_KEEP_ALIVE) {
657
- this.maintainKeepAlive();
635
+ if (this.keepAliveTimeoutId) clearTimeout(this.keepAliveTimeoutId);
636
+ if (this.keepAliveAlertTimeoutId)
637
+ clearTimeout(this.keepAliveAlertTimeoutId);
638
+ this.keepAliveTimeoutId = setTimeout(() => {
639
+ this._errorDisconnect(CONTROL_MSG.TIMEOUT_DISCONNECT);
640
+ }, this.keepAliveTimeout);
641
+ this.keepAliveAlertTimeoutId = setTimeout(() => {
642
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.KEEP_ALIVE_MISSED);
643
+ }, DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT);
644
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.KEEP_ALIVE);
658
645
 
659
646
  return;
660
647
  }
@@ -699,21 +686,13 @@ export abstract class AWSWebSocketProvider {
699
686
  this.logger.debug(`Disconnect error: ${msg}`);
700
687
 
701
688
  if (this.awsRealTimeSocket) {
702
- this._closeSocket();
689
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
703
690
  this.awsRealTimeSocket.close();
704
691
  }
705
692
 
706
693
  this.socketStatus = SOCKET_STATUS.CLOSED;
707
694
  }
708
695
 
709
- private _closeSocket() {
710
- if (this.keepAliveHeartbeatIntervalId) {
711
- clearInterval(this.keepAliveHeartbeatIntervalId);
712
- this.keepAliveHeartbeatIntervalId = undefined;
713
- }
714
- this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
715
- }
716
-
717
696
  private _timeoutStartSubscriptionAck(subscriptionId: string) {
718
697
  const subscriptionObserver =
719
698
  this.subscriptionObserverMap.get(subscriptionId);
@@ -729,7 +708,7 @@ export abstract class AWSWebSocketProvider {
729
708
  subscriptionState: SUBSCRIPTION_STATUS.FAILED,
730
709
  });
731
710
 
732
- this._closeSocket();
711
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
733
712
  this.logger.debug(
734
713
  'timeoutStartSubscription',
735
714
  JSON.stringify({ query, variables }),
@@ -841,7 +820,6 @@ export abstract class AWSWebSocketProvider {
841
820
  this.logger.debug(`WebSocket connection error`);
842
821
  };
843
822
  newSocket.onclose = () => {
844
- this._closeSocket();
845
823
  reject(new Error('Connection handshake error'));
846
824
  };
847
825
  newSocket.onopen = () => {
@@ -871,7 +849,6 @@ export abstract class AWSWebSocketProvider {
871
849
 
872
850
  this.awsRealTimeSocket.onclose = event => {
873
851
  this.logger.debug(`WebSocket closed ${event.reason}`);
874
- this._closeSocket();
875
852
  reject(new Error(JSON.stringify(event)));
876
853
  };
877
854
 
@@ -935,11 +912,7 @@ export abstract class AWSWebSocketProvider {
935
912
  return;
936
913
  }
937
914
 
938
- // Set up a keep alive heartbeat for this connection
939
- this.keepAliveHeartbeatIntervalId = setInterval(() => {
940
- this.keepAliveHeartbeat(connectionTimeoutMs);
941
- }, DEFAULT_KEEP_ALIVE_HEARTBEAT_TIMEOUT);
942
-
915
+ this.keepAliveTimeout = connectionTimeoutMs;
943
916
  this.awsRealTimeSocket.onmessage =
944
917
  this._handleIncomingSubscriptionMessage.bind(this);
945
918
 
@@ -950,7 +923,6 @@ export abstract class AWSWebSocketProvider {
950
923
 
951
924
  this.awsRealTimeSocket.onclose = event => {
952
925
  this.logger.debug(`WebSocket closed ${event.reason}`);
953
- this._closeSocket();
954
926
  this._errorDisconnect(CONTROL_MSG.CONNECTION_CLOSED);
955
927
  };
956
928
  }
@@ -128,11 +128,6 @@ export const START_ACK_TIMEOUT = 15000;
128
128
  */
129
129
  export const DEFAULT_KEEP_ALIVE_TIMEOUT = 5 * 60 * 1000;
130
130
 
131
- /**
132
- * Default Time in milleseconds between monitoring checks of keep alive status
133
- */
134
- export const DEFAULT_KEEP_ALIVE_HEARTBEAT_TIMEOUT = 5 * 1000;
135
-
136
131
  /**
137
132
  * Default Time in milleseconds to alert for missed GQL_CONNECTION_KEEP_ALIVE message
138
133
  */