|
| 1 | +import type { DevframeHost, DevframeNodeContext, DevframeRpcClientFunctions, DevframeRpcServerFunctions } from 'devframe/types' |
| 2 | +import { mkdtempSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { createRpcClient } from 'devframe/rpc/client' |
| 6 | +import { createWsRpcChannel } from 'devframe/rpc/transports/ws-client' |
| 7 | +import { getPort } from 'get-port-please' |
| 8 | +import { describe, expect, it, vi } from 'vitest' |
| 9 | +import { WebSocket } from 'ws' |
| 10 | +import { createHostContext } from '../context' |
| 11 | +import { startHttpAndWs } from '../server' |
| 12 | + |
| 13 | +function makeHost(storageDir: string): DevframeHost { |
| 14 | + return { |
| 15 | + mountStatic: () => {}, |
| 16 | + resolveOrigin: () => 'http://localhost', |
| 17 | + getStorageDir: () => storageDir, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function createTestContext(): Promise<DevframeNodeContext> { |
| 22 | + const storageDir = mkdtempSync(join(tmpdir(), 'devframe-server-')) |
| 23 | + return createHostContext({ cwd: storageDir, mode: 'dev', host: makeHost(storageDir) }) |
| 24 | +} |
| 25 | + |
| 26 | +function connectClient(host: string, port: number) { |
| 27 | + return createRpcClient<DevframeRpcServerFunctions, DevframeRpcClientFunctions>( |
| 28 | + {} as DevframeRpcClientFunctions, |
| 29 | + { channel: createWsRpcChannel({ url: `ws://${host}:${port}` }) }, |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +describe('startHttpAndWs rpcOptions passthrough', () => { |
| 34 | + it('forwards a thrown handler error to rpcOptions.onFunctionError without swallowing the response', async () => { |
| 35 | + const context = await createTestContext() |
| 36 | + context.rpc.register({ |
| 37 | + name: 'test:boom', |
| 38 | + type: 'action', |
| 39 | + handler: () => { |
| 40 | + throw new Error('kaboom') |
| 41 | + }, |
| 42 | + }) |
| 43 | + |
| 44 | + const onFunctionError = vi.fn() |
| 45 | + const host = '127.0.0.1' |
| 46 | + const port = await getPort({ port: 0, host }) |
| 47 | + const server = await startHttpAndWs({ |
| 48 | + context, |
| 49 | + host, |
| 50 | + port, |
| 51 | + auth: false, |
| 52 | + rpcOptions: { onFunctionError }, |
| 53 | + }) |
| 54 | + |
| 55 | + try { |
| 56 | + const client = connectClient(host, port) |
| 57 | + await expect(client.$call('test:boom' as any)).rejects.toThrow('kaboom') |
| 58 | + |
| 59 | + expect(onFunctionError).toHaveBeenCalledTimes(1) |
| 60 | + const [error, name] = onFunctionError.mock.calls[0]! |
| 61 | + expect(name).toBe('test:boom') |
| 62 | + expect((error as Error).message).toBe('kaboom') |
| 63 | + client.$close() |
| 64 | + } |
| 65 | + finally { |
| 66 | + await server.close() |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + it('forwards a deserialize failure to rpcOptions.onGeneralError', async () => { |
| 71 | + const context = await createTestContext() |
| 72 | + // Returning `true` tells birpc the error was handled, suppressing its |
| 73 | + // default rethrow — matches how a "log and swallow" host would use this. |
| 74 | + const onGeneralError = vi.fn(() => true) |
| 75 | + const host = '127.0.0.1' |
| 76 | + const port = await getPort({ port: 0, host }) |
| 77 | + const server = await startHttpAndWs({ |
| 78 | + context, |
| 79 | + host, |
| 80 | + port, |
| 81 | + auth: false, |
| 82 | + rpcOptions: { onGeneralError }, |
| 83 | + }) |
| 84 | + |
| 85 | + try { |
| 86 | + const raw = new WebSocket(`ws://${host}:${port}`) |
| 87 | + await new Promise<void>((resolve, reject) => { |
| 88 | + raw.once('open', () => resolve()) |
| 89 | + raw.once('error', reject) |
| 90 | + }) |
| 91 | + raw.send('not valid json and not structured-clone either') |
| 92 | + |
| 93 | + await vi.waitFor(() => { |
| 94 | + expect(onGeneralError).toHaveBeenCalledTimes(1) |
| 95 | + }) |
| 96 | + raw.close() |
| 97 | + } |
| 98 | + finally { |
| 99 | + await server.close() |
| 100 | + } |
| 101 | + }) |
| 102 | +}) |
0 commit comments