Port of #2239 (v1.x) to v2 (#2275)

This commit is contained in:
Konstantin Konstantinov 2026-06-10 20:14:36 +03:00 • committed by GitHub
commit 1b53a415ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,6 +38,14 @@ export type StdioServerParameters = {
* If not specified, the current working directory will be inherited.
*/
cwd?: string;
/**
* Maximum size of the read buffer in bytes. If a single message exceeds
* this size the transport will emit an error and close.
*
* Defaults to 10 MB.
*/
maxBufferSize?: number;
};
/**
@ -92,7 +100,7 @@ export function getDefaultEnvironment(): Record<string, string> {
*/
export class StdioClientTransport implements Transport {
private _process?: ChildProcess;
private _readBuffer: ReadBuffer = new ReadBuffer();
private _readBuffer: ReadBuffer;
private _serverParams: StdioServerParameters;
private _stderrStream: PassThrough | null = null;
@ -102,6 +110,7 @@ export class StdioClientTransport implements Transport {
constructor(server: StdioServerParameters) {
this._serverParams = server;
this._readBuffer = new ReadBuffer({ maxBufferSize: server.maxBufferSize });
if (server.stderr === 'pipe' || server.stderr === 'overlapped') {
this._stderrStream = new PassThrough();
}
@ -149,8 +158,13 @@ export class StdioClientTransport implements Transport {
});
this._process.stdout?.on('data', chunk => {
this._readBuffer.append(chunk);
this.processReadBuffer();
try {
this._readBuffer.append(chunk);
this.processReadBuffer();
} catch (error) {
this.onerror?.(error as Error);
this.close().catch(() => {});
}
});
this._process.stdout?.on('error', error => {