|
| 1 | +import { PassThrough } from 'node:stream' |
| 2 | +import { describe, it, expect, vi } from 'vitest' |
| 3 | +import { JsonRpcConnection, ERROR_PARSE, ERROR_METHOD_NOT_FOUND, ERROR_INVALID_PARAMS, ERROR_INTERNAL } from '../src/jsonrpc.js' |
| 4 | + |
| 5 | +function harness() { |
| 6 | + const input = new PassThrough() |
| 7 | + const output = new PassThrough() |
| 8 | + const lines: unknown[] = [] |
| 9 | + output.on('data', (chunk: Buffer) => { |
| 10 | + for (const line of chunk.toString('utf-8').split('\n')) { |
| 11 | + if (line.trim()) lines.push(JSON.parse(line)) |
| 12 | + } |
| 13 | + }) |
| 14 | + const conn = new JsonRpcConnection(input, output) |
| 15 | + return { input, output, lines, conn } |
| 16 | +} |
| 17 | + |
| 18 | +async function flush() { |
| 19 | + await new Promise((r) => setImmediate(r)) |
| 20 | + await new Promise((r) => setImmediate(r)) |
| 21 | +} |
| 22 | + |
| 23 | +describe('JsonRpcConnection framing', () => { |
| 24 | + it('dispatches a request split across two writes mid-JSON exactly once', async () => { |
| 25 | + const { input, lines, conn } = harness() |
| 26 | + const handler = vi.fn().mockReturnValue('pong') |
| 27 | + conn.onRequest('ping', handler) |
| 28 | + |
| 29 | + input.write('{"jsonrpc":"2.0","id":1,"me') |
| 30 | + input.write('thod":"ping","params":{}}\n') |
| 31 | + await flush() |
| 32 | + |
| 33 | + expect(handler).toHaveBeenCalledTimes(1) |
| 34 | + expect(lines).toEqual([{ jsonrpc: '2.0', id: 1, result: 'pong' }]) |
| 35 | + }) |
| 36 | + |
| 37 | + it('dispatches two complete messages in one write, in order', async () => { |
| 38 | + const { input, lines, conn } = harness() |
| 39 | + const seen: string[] = [] |
| 40 | + conn.onRequest('a', () => { |
| 41 | + seen.push('a') |
| 42 | + return 1 |
| 43 | + }) |
| 44 | + conn.onRequest('b', () => { |
| 45 | + seen.push('b') |
| 46 | + return 2 |
| 47 | + }) |
| 48 | + |
| 49 | + input.write('{"jsonrpc":"2.0","id":1,"method":"a","params":{}}\n{"jsonrpc":"2.0","id":2,"method":"b","params":{}}\n') |
| 50 | + await flush() |
| 51 | + |
| 52 | + expect(seen).toEqual(['a', 'b']) |
| 53 | + expect(lines).toEqual([ |
| 54 | + { jsonrpc: '2.0', id: 1, result: 1 }, |
| 55 | + { jsonrpc: '2.0', id: 2, result: 2 }, |
| 56 | + ]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('tolerates \\r\\n line endings and blank lines between messages', async () => { |
| 60 | + const { input, lines, conn } = harness() |
| 61 | + conn.onRequest('ping', () => 'pong') |
| 62 | + |
| 63 | + input.write('{"jsonrpc":"2.0","id":1,"method":"ping","params":{}}\r\n\r\n{"jsonrpc":"2.0","id":2,"method":"ping","params":{}}\r\n') |
| 64 | + await flush() |
| 65 | + |
| 66 | + expect(lines).toEqual([ |
| 67 | + { jsonrpc: '2.0', id: 1, result: 'pong' }, |
| 68 | + { jsonrpc: '2.0', id: 2, result: 'pong' }, |
| 69 | + ]) |
| 70 | + }) |
| 71 | + |
| 72 | + it('responds with a parse error for an unparseable line, then keeps working', async () => { |
| 73 | + const { input, lines, conn } = harness() |
| 74 | + conn.onRequest('ping', () => 'pong') |
| 75 | + |
| 76 | + input.write('not json at all\n{"jsonrpc":"2.0","id":1,"method":"ping","params":{}}\n') |
| 77 | + await flush() |
| 78 | + |
| 79 | + expect(lines[0]).toEqual({ jsonrpc: '2.0', id: null, error: { code: ERROR_PARSE, message: 'parse error' } }) |
| 80 | + expect(lines[1]).toEqual({ jsonrpc: '2.0', id: 1, result: 'pong' }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('responds with method not found for an unknown method, carrying the id', async () => { |
| 84 | + const { input, lines, conn } = harness() |
| 85 | + void conn |
| 86 | + |
| 87 | + input.write('{"jsonrpc":"2.0","id":7,"method":"nope","params":{}}\n') |
| 88 | + await flush() |
| 89 | + |
| 90 | + expect(lines).toHaveLength(1) |
| 91 | + const [msg] = lines as Array<{ id: number; error: { code: number } }> |
| 92 | + expect(msg.id).toBe(7) |
| 93 | + expect(msg.error.code).toBe(ERROR_METHOD_NOT_FOUND) |
| 94 | + }) |
| 95 | + |
| 96 | + it('maps a handler throw carrying a code to that error code', async () => { |
| 97 | + const { input, lines, conn } = harness() |
| 98 | + conn.onRequest('bad', () => { |
| 99 | + throw Object.assign(new Error('bad'), { code: ERROR_INVALID_PARAMS }) |
| 100 | + }) |
| 101 | + |
| 102 | + input.write('{"jsonrpc":"2.0","id":1,"method":"bad","params":{}}\n') |
| 103 | + await flush() |
| 104 | + |
| 105 | + const [msg] = lines as Array<{ error: { code: number } }> |
| 106 | + expect(msg.error.code).toBe(ERROR_INVALID_PARAMS) |
| 107 | + }) |
| 108 | + |
| 109 | + it('maps a plain Error throw to the internal error code', async () => { |
| 110 | + const { input, lines, conn } = harness() |
| 111 | + conn.onRequest('bad', () => { |
| 112 | + throw new Error('x') |
| 113 | + }) |
| 114 | + |
| 115 | + input.write('{"jsonrpc":"2.0","id":1,"method":"bad","params":{}}\n') |
| 116 | + await flush() |
| 117 | + |
| 118 | + const [msg] = lines as Array<{ error: { code: number } }> |
| 119 | + expect(msg.error.code).toBe(ERROR_INTERNAL) |
| 120 | + }) |
| 121 | + |
| 122 | + it('calls the notification handler for a message with no id, and writes nothing', async () => { |
| 123 | + const { input, output, lines, conn } = harness() |
| 124 | + const handler = vi.fn() |
| 125 | + conn.onNotification('note', handler) |
| 126 | + const written = vi.fn() |
| 127 | + output.on('data', written) |
| 128 | + |
| 129 | + input.write('{"jsonrpc":"2.0","method":"note","params":{"x":1}}\n') |
| 130 | + await flush() |
| 131 | + |
| 132 | + expect(handler).toHaveBeenCalledTimes(1) |
| 133 | + expect(handler).toHaveBeenCalledWith({ x: 1 }) |
| 134 | + expect(lines).toHaveLength(0) |
| 135 | + }) |
| 136 | + |
| 137 | + it('outbound request writes a framed request and resolves on a matching success response', async () => { |
| 138 | + const { input, lines, conn } = harness() |
| 139 | + |
| 140 | + const pending = conn.request('foo', { a: 1 }) |
| 141 | + await flush() |
| 142 | + |
| 143 | + expect(lines).toEqual([{ jsonrpc: '2.0', id: 1, method: 'foo', params: { a: 1 } }]) |
| 144 | + |
| 145 | + input.write('{"jsonrpc":"2.0","id":1,"result":{"ok":true}}\n') |
| 146 | + await expect(pending).resolves.toEqual({ ok: true }) |
| 147 | + }) |
| 148 | + |
| 149 | + it('outbound request rejects on a matching error response', async () => { |
| 150 | + const { input, conn } = harness() |
| 151 | + |
| 152 | + const pending = conn.request('foo') |
| 153 | + await flush() |
| 154 | + |
| 155 | + input.write('{"jsonrpc":"2.0","id":1,"error":{"code":-1,"message":"nope"}}\n') |
| 156 | + await expect(pending).rejects.toThrow('nope') |
| 157 | + }) |
| 158 | + |
| 159 | + it('rejects a pending outbound request with "connection closed" on input end', async () => { |
| 160 | + const { input, conn } = harness() |
| 161 | + |
| 162 | + const pending = conn.request('foo') |
| 163 | + await flush() |
| 164 | + input.end() |
| 165 | + |
| 166 | + await expect(pending).rejects.toThrow('connection closed') |
| 167 | + }) |
| 168 | +}) |
0 commit comments