hypha-rpc 0.20.35 → 0.20.36

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.
@@ -4572,6 +4572,7 @@ class WebsocketRPCConnection {
4572
4572
  reconnection_token = null,
4573
4573
  timeout = 60,
4574
4574
  WebSocketClass = null,
4575
+ token_refresh_interval = 2 * 60 * 60,
4575
4576
  ) {
4576
4577
  (0,_utils__WEBPACK_IMPORTED_MODULE_1__.assert)(server_url && client_id, "server_url and client_id are required");
4577
4578
  this._server_url = server_url;
@@ -4589,7 +4590,9 @@ class WebsocketRPCConnection {
4589
4590
  this._legacy_auth = null;
4590
4591
  this.connection_info = null;
4591
4592
  this._enable_reconnect = false;
4593
+ this._token_refresh_interval = token_refresh_interval;
4592
4594
  this.manager_id = null;
4595
+ this._refresh_token_task = null;
4593
4596
  }
4594
4597
 
4595
4598
  on_message(handler) {
@@ -4679,6 +4682,19 @@ class WebsocketRPCConnection {
4679
4682
  if (this.connection_info.reconnection_token) {
4680
4683
  this._reconnection_token = this.connection_info.reconnection_token;
4681
4684
  }
4685
+ if (this.connection_info.reconnection_token_life_time) {
4686
+ // make sure the token refresh interval is less than the token life time
4687
+ if (
4688
+ this.token_refresh_interval >
4689
+ this.connection_info.reconnection_token_life_time / 1.5
4690
+ ) {
4691
+ console.warn(
4692
+ `Token refresh interval is too long (${this.token_refresh_interval}), setting it to 1.5 times of the token life time(${this.connection_info.reconnection_token_life_time}).`,
4693
+ );
4694
+ this.token_refresh_interval =
4695
+ this.connection_info.reconnection_token_life_time / 1.5;
4696
+ }
4697
+ }
4682
4698
  this.manager_id = this.connection_info.manager_id || null;
4683
4699
  console.log(
4684
4700
  `Successfully connected to the server, workspace: ${this.connection_info.workspace}, manager_id: ${this.manager_id}`,
@@ -4734,11 +4750,30 @@ class WebsocketRPCConnection {
4734
4750
  this._timeout,
4735
4751
  "Failed to receive the first message from the server",
4736
4752
  );
4753
+ if (this._token_refresh_interval > 0) {
4754
+ setTimeout(() => {
4755
+ this._send_refresh_token();
4756
+ this._refresh_token_task = setInterval(() => {
4757
+ this._send_refresh_token();
4758
+ }, this._token_refresh_interval * 1000);
4759
+ }, 2000);
4760
+ }
4737
4761
  // Listen to messages from the server
4738
4762
  this._enable_reconnect = true;
4739
4763
  this._closed = false;
4740
4764
  this._websocket.onmessage = (event) => {
4741
- this._handle_message(event.data);
4765
+ if (typeof data === "string") {
4766
+ const parsedData = JSON.parse(data);
4767
+ // Check if the message is a reconnection token
4768
+ if (parsedData.type === "reconnection_token") {
4769
+ this._reconnection_token = parsedData.reconnection_token;
4770
+ console.log("Reconnection token received");
4771
+ } else {
4772
+ console.log("Received message from the server:", parsedData);
4773
+ }
4774
+ } else {
4775
+ this._handle_message(event.data);
4776
+ }
4742
4777
  };
4743
4778
 
4744
4779
  this._websocket.onerror = (event) => {
@@ -4761,6 +4796,14 @@ class WebsocketRPCConnection {
4761
4796
  }
4762
4797
  }
4763
4798
 
4799
+ _send_refresh_token() {
4800
+ if (this._websocket && this._websocket.readyState === WebSocket.OPEN) {
4801
+ const refreshMessage = JSON.stringify({ type: "refresh_token" });
4802
+ this._websocket.send(refreshMessage);
4803
+ console.log("Requested refresh token");
4804
+ }
4805
+ }
4806
+
4764
4807
  _handle_close(event) {
4765
4808
  if (
4766
4809
  !this._closed &&
@@ -4845,6 +4888,9 @@ class WebsocketRPCConnection {
4845
4888
  if (this._websocket && this._websocket.readyState === WebSocket.OPEN) {
4846
4889
  this._websocket.close(1000, reason);
4847
4890
  }
4891
+ if (this._refresh_token_task) {
4892
+ clearInterval(this._refresh_token_task);
4893
+ }
4848
4894
  console.info(`WebSocket connection disconnected (${reason})`);
4849
4895
  }
4850
4896
  }