remote-codex 0.11.22 → 0.11.24

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.
@@ -27428,6 +27428,7 @@ function constantTimeEqual(left, right) {
27428
27428
 
27429
27429
  // src/relay-tunnel-client.ts
27430
27430
  var RELAY_HEARTBEAT_INTERVAL_MS = 3e4;
27431
+ var RELAY_CONNECT_TIMEOUT_MS = 15e3;
27431
27432
  var RELAY_RECONNECT_INITIAL_DELAY_MS = 1e3;
27432
27433
  var RELAY_RECONNECT_MAX_DELAY_MS = 3e4;
27433
27434
  var RelayTunnelClient = class {
@@ -27443,6 +27444,7 @@ var RelayTunnelClient = class {
27443
27444
  handleClientMessage;
27444
27445
  socket = null;
27445
27446
  heartbeatHandle = null;
27447
+ connectTimeoutHandle = null;
27446
27448
  reconnectHandle = null;
27447
27449
  reconnectDelayMs = RELAY_RECONNECT_INITIAL_DELAY_MS;
27448
27450
  stopped = false;
@@ -27464,42 +27466,50 @@ var RelayTunnelClient = class {
27464
27466
  const url = new URL("/supervisor/tunnel", this.config.serverUrl ?? void 0);
27465
27467
  url.searchParams.set("token", this.config.agentToken ?? "");
27466
27468
  url.searchParams.set("deviceToken", this.config.agentToken ?? "");
27467
- this.socket = new WebSocket(url);
27468
- this.socket.addEventListener("open", () => {
27469
+ const socket = new WebSocket(url);
27470
+ this.socket = socket;
27471
+ this.connectTimeoutHandle = setTimeout(() => {
27472
+ if (this.socket === socket && socket.readyState !== WebSocket.OPEN) {
27473
+ this.closeAndReconnect(socket);
27474
+ }
27475
+ }, RELAY_CONNECT_TIMEOUT_MS);
27476
+ socket.addEventListener("open", () => {
27477
+ this.clearConnectTimeout();
27469
27478
  this.reconnectDelayMs = RELAY_RECONNECT_INITIAL_DELAY_MS;
27470
27479
  this.sendHeartbeat();
27480
+ this.clearHeartbeat();
27471
27481
  this.heartbeatHandle = setInterval(() => {
27472
27482
  this.sendHeartbeat();
27473
27483
  }, RELAY_HEARTBEAT_INTERVAL_MS);
27474
27484
  });
27475
- this.socket.addEventListener("close", () => {
27476
- this.clearHeartbeat();
27477
- this.cleanupRelayClients();
27478
- this.socket = null;
27479
- this.scheduleReconnect();
27485
+ socket.addEventListener("close", () => {
27486
+ this.closeAndReconnect(socket);
27487
+ });
27488
+ socket.addEventListener("error", () => {
27489
+ this.closeAndReconnect(socket);
27480
27490
  });
27481
- this.socket.addEventListener("message", (event) => {
27491
+ socket.addEventListener("message", (event) => {
27482
27492
  void this.handleMessage(String(event.data));
27483
27493
  });
27484
27494
  }
27485
27495
  stop() {
27486
27496
  this.stopped = true;
27487
27497
  this.clearHeartbeat();
27498
+ this.clearConnectTimeout();
27488
27499
  this.clearReconnect();
27489
27500
  this.cleanupRelayClients();
27490
27501
  this.socket?.close();
27491
27502
  this.socket = null;
27492
27503
  }
27493
27504
  sendHeartbeat() {
27494
- if (this.socket?.readyState !== WebSocket.OPEN) {
27505
+ const socket = this.socket;
27506
+ if (socket?.readyState !== WebSocket.OPEN) {
27495
27507
  return;
27496
27508
  }
27497
- this.socket.send(
27498
- JSON.stringify({
27499
- type: "relay.heartbeat",
27500
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
27501
- })
27502
- );
27509
+ this.sendEnvelope(socket, {
27510
+ type: "relay.heartbeat",
27511
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
27512
+ });
27503
27513
  }
27504
27514
  async handleMessage(rawMessage) {
27505
27515
  let parsed;
@@ -27530,27 +27540,52 @@ var RelayTunnelClient = class {
27530
27540
  return;
27531
27541
  }
27532
27542
  const response = await this.handleRequest(parsed.payload);
27533
- this.socket?.send(
27534
- JSON.stringify({
27543
+ const socket = this.socket;
27544
+ if (socket?.readyState !== WebSocket.OPEN) {
27545
+ return;
27546
+ }
27547
+ this.sendEnvelope(
27548
+ socket,
27549
+ {
27535
27550
  type: "relay.response",
27536
27551
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
27537
27552
  requestId: parsed.requestId,
27538
27553
  payload: response
27539
- })
27554
+ }
27540
27555
  );
27541
27556
  }
27542
27557
  sendClientMessage(clientId, message) {
27543
- if (this.socket?.readyState !== WebSocket.OPEN) {
27558
+ const socket = this.socket;
27559
+ if (socket?.readyState !== WebSocket.OPEN) {
27544
27560
  return;
27545
27561
  }
27546
- this.socket.send(
27547
- JSON.stringify({
27548
- type: "relay.server.message",
27549
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
27550
- clientId,
27551
- payload: message
27552
- })
27553
- );
27562
+ this.sendEnvelope(socket, {
27563
+ type: "relay.server.message",
27564
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
27565
+ clientId,
27566
+ payload: message
27567
+ });
27568
+ }
27569
+ sendEnvelope(socket, message) {
27570
+ try {
27571
+ socket.send(JSON.stringify(message));
27572
+ } catch {
27573
+ this.closeAndReconnect(socket);
27574
+ }
27575
+ }
27576
+ closeAndReconnect(socket) {
27577
+ if (this.socket !== socket) {
27578
+ return;
27579
+ }
27580
+ this.clearHeartbeat();
27581
+ this.clearConnectTimeout();
27582
+ this.cleanupRelayClients();
27583
+ this.socket = null;
27584
+ try {
27585
+ socket.close();
27586
+ } catch {
27587
+ }
27588
+ this.scheduleReconnect();
27554
27589
  }
27555
27590
  clearHeartbeat() {
27556
27591
  if (this.heartbeatHandle) {
@@ -27558,6 +27593,12 @@ var RelayTunnelClient = class {
27558
27593
  this.heartbeatHandle = null;
27559
27594
  }
27560
27595
  }
27596
+ clearConnectTimeout() {
27597
+ if (this.connectTimeoutHandle) {
27598
+ clearTimeout(this.connectTimeoutHandle);
27599
+ this.connectTimeoutHandle = null;
27600
+ }
27601
+ }
27561
27602
  scheduleReconnect() {
27562
27603
  if (this.stopped || this.reconnectHandle) {
27563
27604
  return;