| "use strict";
|
| var __importDefault = (this && this.__importDefault) || function (mod) {
|
| return (mod && mod.__esModule) ? mod : { "default": mod };
|
| };
|
| Object.defineProperty(exports, "__esModule", { value: true });
|
| exports.StdioServerTransport = void 0;
|
| const node_process_1 = __importDefault(require("node:process"));
|
| const stdio_js_1 = require("../shared/stdio.js");
|
| |
| |
| |
| |
|
|
| class StdioServerTransport {
|
| constructor(_stdin = node_process_1.default.stdin, _stdout = node_process_1.default.stdout) {
|
| this._stdin = _stdin;
|
| this._stdout = _stdout;
|
| this._readBuffer = new stdio_js_1.ReadBuffer();
|
| this._started = false;
|
|
|
| this._ondata = (chunk) => {
|
| this._readBuffer.append(chunk);
|
| this.processReadBuffer();
|
| };
|
| this._onerror = (error) => {
|
| this.onerror?.(error);
|
| };
|
| }
|
| |
| |
|
|
| async start() {
|
| if (this._started) {
|
| throw new Error('StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.');
|
| }
|
| this._started = true;
|
| this._stdin.on('data', this._ondata);
|
| this._stdin.on('error', this._onerror);
|
| }
|
| processReadBuffer() {
|
| while (true) {
|
| try {
|
| const message = this._readBuffer.readMessage();
|
| if (message === null) {
|
| break;
|
| }
|
| this.onmessage?.(message);
|
| }
|
| catch (error) {
|
| this.onerror?.(error);
|
| }
|
| }
|
| }
|
| async close() {
|
|
|
| this._stdin.off('data', this._ondata);
|
| this._stdin.off('error', this._onerror);
|
|
|
| const remainingDataListeners = this._stdin.listenerCount('data');
|
| if (remainingDataListeners === 0) {
|
|
|
|
|
| this._stdin.pause();
|
| }
|
|
|
| this._readBuffer.clear();
|
| this.onclose?.();
|
| }
|
| send(message) {
|
| return new Promise(resolve => {
|
| const json = (0, stdio_js_1.serializeMessage)(message);
|
| if (this._stdout.write(json)) {
|
| resolve();
|
| }
|
| else {
|
| this._stdout.once('drain', resolve);
|
| }
|
| });
|
| }
|
| }
|
| exports.StdioServerTransport = StdioServerTransport;
|
| |