Files
SnowLuma/.pnpm-store/v11/files/76/bd84836c694fd091a0e9b57f85936e6a726c17faf8fdb36da30426ebdce5f5188d7f1b94ebbb7924746986b5ffa3b22fefe47969c3e14e822069c7d1f5c3c9
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

40 lines
1.3 KiB
Plaintext

import { JSONRPCMessageSchema } from '../types.js';
export const STDIO_DEFAULT_MAX_BUFFER_SIZE = 10 * 1024 * 1024;
/**
* Buffers a continuous stdio stream into discrete JSON-RPC messages.
*/
export class ReadBuffer {
constructor(options) {
this._maxBufferSize = options?.maxBufferSize ?? STDIO_DEFAULT_MAX_BUFFER_SIZE;
}
append(chunk) {
const newSize = (this._buffer?.length ?? 0) + chunk.length;
if (newSize > this._maxBufferSize) {
this.clear();
throw new Error(`ReadBuffer exceeded maximum size of ${this._maxBufferSize} bytes`);
}
this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
}
readMessage() {
if (!this._buffer) {
return null;
}
const index = this._buffer.indexOf('\n');
if (index === -1) {
return null;
}
const line = this._buffer.toString('utf8', 0, index).replace(/\r$/, '');
this._buffer = this._buffer.subarray(index + 1);
return deserializeMessage(line);
}
clear() {
this._buffer = undefined;
}
}
export function deserializeMessage(line) {
return JSONRPCMessageSchema.parse(JSON.parse(line));
}
export function serializeMessage(message) {
return JSON.stringify(message) + '\n';
}
//# sourceMappingURL=stdio.js.map