@chatroomcp/chatroom 0.1.6 → 0.1.7

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.
@@ -1,4 +1,8 @@
1
1
  import { type CloudLeaseState } from "./types.js";
2
+ interface TunnelTimings {
3
+ readyTimeoutMs: number;
4
+ heartbeatIntervalMs: number;
5
+ }
2
6
  export declare class CloudTunnelClient {
3
7
  private lease;
4
8
  private readonly identity;
@@ -6,11 +10,15 @@ export declare class CloudTunnelClient {
6
10
  private readonly callbacks;
7
11
  private socket;
8
12
  private reconnectTimer;
13
+ private readyTimer;
14
+ private heartbeatTimer;
15
+ private awaitingPong;
9
16
  private stopped;
10
17
  private attempts;
11
18
  private closeWhenIdle;
12
19
  private acceptingServices;
13
20
  private readonly streams;
21
+ private readonly timings;
14
22
  constructor(lease: CloudLeaseState, identity: {
15
23
  devicePrivateKey: string;
16
24
  }, local: {
@@ -20,7 +28,7 @@ export declare class CloudTunnelClient {
20
28
  onConnected(): void;
21
29
  onDisconnected(): void;
22
30
  onError(error: Error): void;
23
- });
31
+ }, timings?: Partial<TunnelTimings>);
24
32
  start(): void;
25
33
  stop(): void;
26
34
  drainAndStop(): void;
@@ -32,5 +40,11 @@ export declare class CloudTunnelClient {
32
40
  private handleBinary;
33
41
  private send;
34
42
  private sendBinary;
43
+ private startReadyTimeout;
44
+ private clearReadyTimeout;
45
+ private startHeartbeat;
46
+ private clearSocketTimers;
47
+ private failSocket;
35
48
  private scheduleReconnect;
36
49
  }
50
+ export {};
@@ -5,6 +5,8 @@ import { cloudServiceForPublicService, } from "./types.js";
5
5
  const PROTOCOL = "chatroom-tunnel-v1";
6
6
  const HIGH_WATER = 8 * 1024 * 1024;
7
7
  const LOW_WATER = 2 * 1024 * 1024;
8
+ const READY_TIMEOUT_MS = 10_000;
9
+ const HEARTBEAT_INTERVAL_MS = 20_000;
8
10
  export class CloudTunnelClient {
9
11
  lease;
10
12
  identity;
@@ -12,17 +14,25 @@ export class CloudTunnelClient {
12
14
  callbacks;
13
15
  socket = null;
14
16
  reconnectTimer = null;
17
+ readyTimer = null;
18
+ heartbeatTimer = null;
19
+ awaitingPong = false;
15
20
  stopped = true;
16
21
  attempts = 0;
17
22
  closeWhenIdle = null;
18
23
  acceptingServices;
19
24
  streams = new Map();
20
- constructor(lease, identity, local, callbacks) {
25
+ timings;
26
+ constructor(lease, identity, local, callbacks, timings = {}) {
21
27
  this.lease = lease;
22
28
  this.identity = identity;
23
29
  this.local = local;
24
30
  this.callbacks = callbacks;
25
31
  this.acceptingServices = new Set(lease.services);
32
+ this.timings = {
33
+ readyTimeoutMs: timings.readyTimeoutMs ?? READY_TIMEOUT_MS,
34
+ heartbeatIntervalMs: timings.heartbeatIntervalMs ?? HEARTBEAT_INTERVAL_MS,
35
+ };
26
36
  }
27
37
  start() {
28
38
  if (!this.stopped)
@@ -39,6 +49,7 @@ export class CloudTunnelClient {
39
49
  if (this.reconnectTimer)
40
50
  clearTimeout(this.reconnectTimer);
41
51
  this.reconnectTimer = null;
52
+ this.clearSocketTimers();
42
53
  this.socket?.close();
43
54
  this.socket = null;
44
55
  for (const stream of this.streams.values())
@@ -51,6 +62,7 @@ export class CloudTunnelClient {
51
62
  if (this.reconnectTimer)
52
63
  clearTimeout(this.reconnectTimer);
53
64
  this.reconnectTimer = null;
65
+ this.clearSocketTimers();
54
66
  if (this.streams.size === 0) {
55
67
  this.closeWhenIdle = null;
56
68
  this.socket?.close();
@@ -86,6 +98,7 @@ export class CloudTunnelClient {
86
98
  const socket = new WebSocket(this.lease.tunnelUrl, PROTOCOL);
87
99
  this.socket = socket;
88
100
  socket.binaryType = "nodebuffer";
101
+ this.startReadyTimeout(socket);
89
102
  socket.on("message", (data, isBinary) => {
90
103
  try {
91
104
  if (isBinary)
@@ -98,9 +111,15 @@ export class CloudTunnelClient {
98
111
  socket.close();
99
112
  }
100
113
  });
101
- socket.on("close", () => {
114
+ socket.on("pong", () => {
102
115
  if (this.socket === socket)
116
+ this.awaitingPong = false;
117
+ });
118
+ socket.on("close", () => {
119
+ if (this.socket === socket) {
120
+ this.clearSocketTimers();
103
121
  this.socket = null;
122
+ }
104
123
  this.closeWhenIdle = null;
105
124
  for (const stream of this.streams.values())
106
125
  stream.request.destroy();
@@ -109,7 +128,7 @@ export class CloudTunnelClient {
109
128
  if (!this.stopped)
110
129
  this.scheduleReconnect();
111
130
  });
112
- socket.on("error", (error) => this.callbacks.onError(error));
131
+ socket.on("error", (error) => this.failSocket(socket, error));
113
132
  }
114
133
  handleControl(message) {
115
134
  if (message.type === "challenge") {
@@ -122,6 +141,9 @@ export class CloudTunnelClient {
122
141
  }
123
142
  if (message.type === "ready") {
124
143
  this.attempts = 0;
144
+ this.clearReadyTimeout();
145
+ if (this.socket)
146
+ this.startHeartbeat(this.socket);
125
147
  this.callbacks.onConnected();
126
148
  return;
127
149
  }
@@ -248,6 +270,50 @@ export class CloudTunnelClient {
248
270
  socket.send(frame, { binary: true });
249
271
  return socket.bufferedAmount < HIGH_WATER;
250
272
  }
273
+ startReadyTimeout(socket) {
274
+ this.clearReadyTimeout();
275
+ this.readyTimer = setTimeout(() => {
276
+ this.readyTimer = null;
277
+ this.failSocket(socket, new Error("Tunnel connection timed out"));
278
+ }, this.timings.readyTimeoutMs);
279
+ this.readyTimer.unref();
280
+ }
281
+ clearReadyTimeout() {
282
+ if (this.readyTimer)
283
+ clearTimeout(this.readyTimer);
284
+ this.readyTimer = null;
285
+ }
286
+ startHeartbeat(socket) {
287
+ if (this.heartbeatTimer)
288
+ clearInterval(this.heartbeatTimer);
289
+ this.awaitingPong = false;
290
+ this.heartbeatTimer = setInterval(() => {
291
+ if (this.socket !== socket || socket.readyState !== WebSocket.OPEN)
292
+ return;
293
+ if (this.awaitingPong) {
294
+ this.failSocket(socket, new Error("Tunnel heartbeat timed out"));
295
+ return;
296
+ }
297
+ this.awaitingPong = true;
298
+ socket.ping();
299
+ }, this.timings.heartbeatIntervalMs);
300
+ this.heartbeatTimer.unref();
301
+ }
302
+ clearSocketTimers() {
303
+ this.clearReadyTimeout();
304
+ if (this.heartbeatTimer)
305
+ clearInterval(this.heartbeatTimer);
306
+ this.heartbeatTimer = null;
307
+ this.awaitingPong = false;
308
+ }
309
+ failSocket(socket, error) {
310
+ if (this.socket !== socket)
311
+ return;
312
+ this.callbacks.onError(error);
313
+ this.clearSocketTimers();
314
+ this.socket = null;
315
+ socket.terminate();
316
+ }
251
317
  scheduleReconnect() {
252
318
  if (this.reconnectTimer || this.stopped)
253
319
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatroomcp/chatroom",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "description": "Local MCP runtime for ChatGPT with workspace, process, Cloud, and WebUI controls.",
6
6
  "bin": {