@dev-anywhere/proxy 0.4.81 → 0.4.82

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.
package/dist/serve.js CHANGED
@@ -607,6 +607,8 @@ var DEFAULT_PROXY_ID_PATH = join(homedir(), ".dev-anywhere", "proxy-id");
607
607
  var MAX_BACKOFF_MS = 3e4;
608
608
  var BASE_BACKOFF_MS = 1e3;
609
609
  var MAX_QUEUE_SIZE = 1e4;
610
+ var DEFAULT_HEARTBEAT_INTERVAL_MS = 15e3;
611
+ var DEFAULT_HEARTBEAT_TIMEOUT_MS = 1e4;
610
612
  var MAX_JSON_MESSAGE_SIZE = 1 * 1024 * 1024;
611
613
  var RelayConnectionState = {
612
614
  DISCONNECTED: "disconnected",
@@ -648,6 +650,8 @@ var RelayConnection = class extends EventEmitter {
648
650
  queue = new MemoryMessageQueue();
649
651
  reconnectAttempt = 0;
650
652
  reconnectTimer = null;
653
+ heartbeatTimer = null;
654
+ heartbeatTimeoutTimer = null;
651
655
  fsm = createFSM({
652
656
  initial: RelayConnectionState.DISCONNECTED,
653
657
  transitions: RELAY_TRANSITIONS,
@@ -659,12 +663,16 @@ var RelayConnection = class extends EventEmitter {
659
663
  });
660
664
  name;
661
665
  token;
666
+ heartbeatIntervalMs;
667
+ heartbeatTimeoutMs;
662
668
  constructor(relayUrl, options) {
663
669
  super();
664
670
  this.relayUrl = relayUrl;
665
671
  this.proxyId = this.loadOrCreateProxyId(options?.proxyIdPath ?? DEFAULT_PROXY_ID_PATH);
666
672
  this.name = options?.name;
667
673
  this.token = options?.token;
674
+ this.heartbeatIntervalMs = options?.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
675
+ this.heartbeatTimeoutMs = options?.heartbeatTimeoutMs ?? DEFAULT_HEARTBEAT_TIMEOUT_MS;
668
676
  }
669
677
  // 从文件读取或生成新的 proxyId,生成后持久化到文件
670
678
  loadOrCreateProxyId(idPath) {
@@ -728,6 +736,7 @@ var RelayConnection = class extends EventEmitter {
728
736
  serviceLogger.info({ status: msg.status }, "Received register response");
729
737
  if (!this.fsm.tryTransitionTo(RelayConnectionState.SYNCED)) return;
730
738
  this.reconnectAttempt = 0;
739
+ this.startHeartbeat();
731
740
  this.flushQueue();
732
741
  this.emit("connected");
733
742
  return;
@@ -735,6 +744,7 @@ var RelayConnection = class extends EventEmitter {
735
744
  this.emit("message", msg);
736
745
  });
737
746
  this.ws.on("close", (code, reason) => {
747
+ this.stopHeartbeat();
738
748
  this.ws = null;
739
749
  const closeMeta = { code, reason: reason.toString() || void 0 };
740
750
  if (this.fsm.current() !== RelayConnectionState.CLOSED) {
@@ -749,6 +759,9 @@ var RelayConnection = class extends EventEmitter {
749
759
  this.ws.on("error", (err) => {
750
760
  serviceLogger.error({ error: String(err) }, "Relay connection error");
751
761
  });
762
+ this.ws.on("pong", () => {
763
+ this.clearHeartbeatTimeout();
764
+ });
752
765
  } catch (err) {
753
766
  serviceLogger.error({ error: String(err) }, "Failed to create relay connection");
754
767
  if (this.fsm.current() !== RelayConnectionState.CLOSED) {
@@ -757,6 +770,59 @@ var RelayConnection = class extends EventEmitter {
757
770
  }
758
771
  }
759
772
  }
773
+ startHeartbeat() {
774
+ this.stopHeartbeat();
775
+ if (this.heartbeatIntervalMs <= 0 || this.heartbeatTimeoutMs <= 0) return;
776
+ this.heartbeatTimer = setInterval(() => {
777
+ this.sendHeartbeat();
778
+ }, this.heartbeatIntervalMs);
779
+ this.heartbeatTimer.unref?.();
780
+ }
781
+ stopHeartbeat() {
782
+ if (this.heartbeatTimer) {
783
+ clearInterval(this.heartbeatTimer);
784
+ this.heartbeatTimer = null;
785
+ }
786
+ this.clearHeartbeatTimeout();
787
+ }
788
+ clearHeartbeatTimeout() {
789
+ if (!this.heartbeatTimeoutTimer) return;
790
+ clearTimeout(this.heartbeatTimeoutTimer);
791
+ this.heartbeatTimeoutTimer = null;
792
+ }
793
+ sendHeartbeat() {
794
+ const ws = this.ws;
795
+ if (!ws || this.fsm.current() !== RelayConnectionState.SYNCED) return;
796
+ if (ws.readyState !== WebSocket.OPEN) {
797
+ this.terminateStaleSocket(ws, "heartbeat socket is not open");
798
+ return;
799
+ }
800
+ if (this.heartbeatTimeoutTimer) {
801
+ this.terminateStaleSocket(ws, "previous heartbeat did not receive pong");
802
+ return;
803
+ }
804
+ this.heartbeatTimeoutTimer = setTimeout(() => {
805
+ if (this.ws === ws && this.fsm.current() !== RelayConnectionState.CLOSED) {
806
+ this.terminateStaleSocket(ws, "relay heartbeat pong timeout");
807
+ }
808
+ }, this.heartbeatTimeoutMs);
809
+ this.heartbeatTimeoutTimer.unref?.();
810
+ try {
811
+ ws.ping((err) => {
812
+ if (err && this.ws === ws && this.fsm.current() !== RelayConnectionState.CLOSED) {
813
+ this.terminateStaleSocket(ws, `relay heartbeat ping failed: ${String(err)}`);
814
+ }
815
+ });
816
+ } catch (err) {
817
+ this.terminateStaleSocket(ws, `relay heartbeat ping threw: ${String(err)}`);
818
+ }
819
+ }
820
+ terminateStaleSocket(ws, reason) {
821
+ this.clearHeartbeatTimeout();
822
+ if (this.ws !== ws || this.fsm.current() === RelayConnectionState.CLOSED) return;
823
+ serviceLogger.warn({ reason }, "Relay connection heartbeat failed; terminating stale socket");
824
+ ws.terminate();
825
+ }
760
826
  // 将队列中缓存的消息依次发送到 relay
761
827
  flushQueue() {
762
828
  for (const raw of this.queue.drain()) {
@@ -816,6 +882,7 @@ var RelayConnection = class extends EventEmitter {
816
882
  clearTimeout(this.reconnectTimer);
817
883
  this.reconnectTimer = null;
818
884
  }
885
+ this.stopHeartbeat();
819
886
  if (this.ws) {
820
887
  if (this.ws.readyState === WebSocket.OPEN) {
821
888
  this.ws.send(serializeControl({ type: "proxy_disconnect", proxyId: this.proxyId }));