Skip to content
Closed
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
12 changes: 12 additions & 0 deletions packages/core-internal/test/shared/stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ test('should only yield a message after a newline', () => {
expect(readBuffer.readMessage()).toBeNull();
});

test('should clear consumed buffer when newline is the final byte', () => {
const readBuffer = new ReadBuffer();

readBuffer.append(Buffer.from(JSON.stringify(testMessage) + '\n'));
expect(readBuffer.readMessage()).toEqual(testMessage);
expect((readBuffer as unknown as { _buffer?: Buffer })._buffer).toBeUndefined();

const nextChunk = Buffer.from(JSON.stringify(testMessage));
readBuffer.append(nextChunk);
expect((readBuffer as unknown as { _buffer?: Buffer })._buffer).toBe(nextChunk);
});

test('should be reusable after clearing', () => {
const readBuffer = new ReadBuffer();

Expand Down
Loading