Skip to content

Commit 7b718de

Browse files
committed
Report errors when start() is called multiple times
1 parent 83a43fd commit 7b718de

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/client/sse.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export class SSEClientTransport implements Transport {
2222
}
2323

2424
start(): Promise<void> {
25+
if (this._eventSource) {
26+
throw new Error(
27+
"SSEClientTransport already started! If using Client class, note that connect() calls start() automatically.",
28+
);
29+
}
30+
2531
return new Promise((resolve, reject) => {
2632
this._eventSource = new EventSource(this._url.href);
2733
this._abortController = new AbortController();

src/client/stdio.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export class StdioClientTransport implements Transport {
4545
* Starts the server process and prepares to communicate with it.
4646
*/
4747
async start(): Promise<void> {
48+
if (this._process) {
49+
throw new Error(
50+
"StdioClientTransport already started! If using Client class, note that connect() calls start() automatically.",
51+
);
52+
}
53+
4854
return new Promise((resolve, reject) => {
4955
this._process = spawn(
5056
this._serverParams.command,

src/client/websocket.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export class WebSocketClientTransport implements Transport {
1919
}
2020

2121
start(): Promise<void> {
22+
if (this._socket) {
23+
throw new Error(
24+
"WebSocketClientTransport already started! If using Client class, note that connect() calls start() automatically.",
25+
);
26+
}
27+
2228
return new Promise((resolve, reject) => {
2329
this._socket = new WebSocket(this._url, SUBPROTOCOL);
2430

src/server/sse.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export class SSEServerTransport implements Transport {
3737
*/
3838
async start(): Promise<void> {
3939
if (this._sseResponse) {
40-
throw new Error("Already connected!");
40+
throw new Error(
41+
"SSEServerTransport already started! If using Server class, note that connect() calls start() automatically.",
42+
);
4143
}
4244

4345
this.res.writeHead(200, {

src/server/stdio.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Transport } from "../shared/transport.js";
1111
*/
1212
export class StdioServerTransport implements Transport {
1313
private _readBuffer: ReadBuffer = new ReadBuffer();
14+
private _started = false;
1415

1516
constructor(
1617
private _stdin: Readable = process.stdin,
@@ -34,6 +35,13 @@ export class StdioServerTransport implements Transport {
3435
* Starts listening for messages on stdin.
3536
*/
3637
async start(): Promise<void> {
38+
if (this._started) {
39+
throw new Error(
40+
"StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.",
41+
);
42+
}
43+
44+
this._started = true;
3745
this._stdin.on("data", this._ondata);
3846
this._stdin.on("error", this._onerror);
3947
}

0 commit comments

Comments
 (0)