Skip to content

Commit 75b95af

Browse files
author
Sahar Shemesh
committed
fix: ignore invalid json messages only
1 parent 5b40979 commit 75b95af

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/shared/stdio.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ZodError } from 'zod/v4';
12
import { JSONRPCMessage } from '../types.js';
23
import { ReadBuffer } from './stdio.js';
34

@@ -34,9 +35,17 @@ test('should be reusable after clearing', () => {
3435
expect(readBuffer.readMessage()).toEqual(testMessage);
3536
});
3637

37-
test('should override invalid messages and return null', () => {
38+
test('should override invalid json message and return null', () => {
3839
const readBuffer = new ReadBuffer();
3940

4041
readBuffer.append(Buffer.from('invalid message\n'));
4142
expect(readBuffer.readMessage()).toBeNull();
4243
});
44+
45+
test('should throw validation error on invalid JSON-RPC message', () => {
46+
const readBuffer = new ReadBuffer();
47+
const invalidJsonRpcMessage = '{"jsonrpc":"2.0","method":123}\n';
48+
readBuffer.append(Buffer.from(invalidJsonRpcMessage));
49+
expect(() => readBuffer.readMessage()).toThrowError(ZodError);
50+
});
51+

src/shared/stdio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export function deserializeMessage(line: string): JSONRPCMessage | null {
3535
try {
3636
return JSONRPCMessageSchema.parse(JSON.parse(line));
3737
} catch (error: unknown) {
38-
// When Non JSONRPC message is received (parsing error or schema validation error), return null
39-
if (error instanceof ZodError || error instanceof SyntaxError) {
38+
// When non-JSON messages are received, we ignore them.
39+
if (error instanceof SyntaxError) {
4040
return null;
4141
}
4242
throw error;

0 commit comments

Comments
 (0)