Skip to content

Commit 1133260

Browse files
committed
feat(core): add hasCoordinatorConnection and onError to transport
Expose coordinator liveness on MeshTransport so consumers can degrade gracefully. Add optional onError to TransportEvents for non-fatal error reporting. Reduce connect timeout from 2s to 1s to avoid blocking startup on orphan coordinator ports.
1 parent 7db1be8 commit 1133260

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/core/tcp-transport.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { nanoid } from "./nanoid.js";
3434
const COORDINATOR_HOST = "127.0.0.1";
3535

3636
/** Timeout for connecting to the coordinator before giving up. */
37-
const CONNECT_TIMEOUT_MS = 2000;
37+
const CONNECT_TIMEOUT_MS = 1000;
3838

3939
// ---------------------------------------------------------------------------
4040
// Async socket write helper (not exported)
@@ -132,6 +132,12 @@ export class TcpTransport implements MeshTransport {
132132
return this._isCoordinator;
133133
}
134134

135+
get hasCoordinatorConnection(): boolean {
136+
return (
137+
this.coordinatorSocket !== undefined && !this.coordinatorSocket.destroyed
138+
);
139+
}
140+
135141
// -----------------------------------------------------------------------
136142
// MeshTransport — Data server
137143
// -----------------------------------------------------------------------

src/core/tls-transport.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { nanoid } from "./nanoid.js";
3535
// ---------------------------------------------------------------------------
3636

3737
const COORDINATOR_HOST = "127.0.0.1";
38-
const CONNECT_TIMEOUT_MS = 2000;
38+
const CONNECT_TIMEOUT_MS = 1000;
3939

4040
/**
4141
* Wrap tls.createServer with a retry for intermittent OpenSSL ASN.1 races
@@ -161,6 +161,12 @@ export class TlsTransport {
161161
return this._isCoordinator;
162162
}
163163

164+
get hasCoordinatorConnection(): boolean {
165+
return (
166+
this.coordinatorSocket !== undefined && !this.coordinatorSocket.destroyed
167+
);
168+
}
169+
164170
// -----------------------------------------------------------------------
165171
// TLS options
166172
// -----------------------------------------------------------------------

src/core/transport.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export interface TransportEvents {
6868
*/
6969
onPeerDisconnected(handle: ConnectionHandle): void;
7070

71+
/**
72+
* A non-fatal error occurred that consumers should know about.
73+
*/
74+
onError?(error: Error): void;
75+
7176
/**
7277
* A peer introduced itself to the coordinator.
7378
* Only fires on the coordinator instance.
@@ -123,6 +128,9 @@ export interface MeshTransport {
123128
/** Whether this instance is the mesh coordinator. */
124129
readonly isCoordinator: boolean;
125130

131+
/** Whether this instance has a live connection to a coordinator. */
132+
readonly hasCoordinatorConnection: boolean;
133+
126134
/**
127135
* Start the data server on an OS-assigned port.
128136
* Resolves when the server is listening.

src/core/ws-transport.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ export class WebSocketTransport implements MeshTransport {
109109
return this._isCoordinator;
110110
}
111111

112+
get hasCoordinatorConnection(): boolean {
113+
return this.coordinatorWs?.readyState === WebSocket.OPEN;
114+
}
115+
112116
// -----------------------------------------------------------------------
113117
// MeshTransport — Data server
114118
// -----------------------------------------------------------------------

0 commit comments

Comments
 (0)