Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core-internal/src/shared/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class ReadBuffer {
}

const line = this._buffer.toString('utf8', 0, index).replace(/\r$/, '');
this._buffer = this._buffer.subarray(index + 1);
const remainder = this._buffer.subarray(index + 1);
this._buffer = remainder.length === 0 ? undefined : remainder;

try {
return deserializeMessage(line);
Expand Down
15 changes: 15 additions & 0 deletions packages/core-internal/test/shared/stdio.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest';
import { ReadBuffer, STDIO_DEFAULT_MAX_BUFFER_SIZE } from '../../src/shared/stdio';
import type { JSONRPCMessage } from '../../src/types/index';

Expand Down Expand Up @@ -156,3 +157,17 @@ describe('buffer size limit', () => {
expect(readBuffer.readMessage()).not.toBeNull();
});
});

test('should clear backing allocation when final byte is consumed', () => {
const readBuffer = new ReadBuffer();
const largePayload = 'x'.repeat(1024);
readBuffer.append(Buffer.from(JSON.stringify({ jsonrpc: '2.0', method: largePayload }) + '\n'));
expect(readBuffer.readMessage()).toEqual({ jsonrpc: '2.0', method: largePayload });

const appendSpy = vi.spyOn(Buffer, 'concat');
const nextChunk = Buffer.from(JSON.stringify(testMessage) + '\n');
readBuffer.append(nextChunk);
expect(appendSpy).not.toHaveBeenCalled();
expect(readBuffer.readMessage()).toEqual(testMessage);
appendSpy.mockRestore();
});
Loading