|
| 1 | +import http2 from 'node:http2'; |
| 2 | +import { createH2Fetch } from '../src/lib/h2-transport/index.ts'; |
| 3 | +import { execSync } from 'node:child_process'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | + |
| 8 | +// Generate self-signed certs for the test server |
| 9 | +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sse-test-')); |
| 10 | +const keyPath = path.join(tmpDir, 'key.pem'); |
| 11 | +const certPath = path.join(tmpDir, 'cert.pem'); |
| 12 | + |
| 13 | +execSync( |
| 14 | + `openssl req -x509 -newkey rsa:2048 -keyout ${keyPath} -out ${certPath} ` + |
| 15 | + `-days 1 -nodes -subj "/CN=localhost" 2>/dev/null`, |
| 16 | +); |
| 17 | + |
| 18 | +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
| 19 | + |
| 20 | +async function startSSEServer(): Promise<{ port: number; close: () => void }> { |
| 21 | + return new Promise((resolve) => { |
| 22 | + const server = http2.createSecureServer({ |
| 23 | + key: fs.readFileSync(keyPath), |
| 24 | + cert: fs.readFileSync(certPath), |
| 25 | + }); |
| 26 | + |
| 27 | + server.on('stream', (stream: http2.ServerHttp2Stream, headers) => { |
| 28 | + if (headers[':path'] === '/v1/test/sse') { |
| 29 | + stream.respond({ |
| 30 | + ':status': 200, |
| 31 | + 'content-type': 'text/event-stream', |
| 32 | + 'cache-control': 'no-cache', |
| 33 | + }); |
| 34 | + |
| 35 | + const events = [ |
| 36 | + 'data: {"id":1,"message":"hello"}\n\n', |
| 37 | + 'data: {"id":2,"message":"world"}\n\n', |
| 38 | + 'data: {"id":3,"message":"streaming done"}\n\n', |
| 39 | + ]; |
| 40 | + |
| 41 | + let i = 0; |
| 42 | + const interval = setInterval(() => { |
| 43 | + if (i < events.length) { |
| 44 | + stream.write(events[i]); |
| 45 | + i++; |
| 46 | + } else { |
| 47 | + clearInterval(interval); |
| 48 | + stream.end(); |
| 49 | + } |
| 50 | + }, 50); |
| 51 | + } else { |
| 52 | + stream.respond({ ':status': 200, 'content-type': 'application/json' }); |
| 53 | + stream.end(JSON.stringify({ ok: true })); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + server.listen(0, () => { |
| 58 | + const port = (server.address() as any).port; |
| 59 | + resolve({ port, close: () => server.close() }); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +async function main() { |
| 65 | + const { port, close } = await startSSEServer(); |
| 66 | + console.log('SSE test server on port', port); |
| 67 | + |
| 68 | + const h2Fetch = createH2Fetch(); |
| 69 | + |
| 70 | + // Test 1: Normal JSON request |
| 71 | + console.log('\n--- Test 1: Normal JSON request ---'); |
| 72 | + const jsonResp = (await h2Fetch(`https://localhost:${port}/v1/test/json`, { |
| 73 | + method: 'GET', |
| 74 | + headers: {}, |
| 75 | + })) as any; |
| 76 | + console.log('Status:', jsonResp.status); |
| 77 | + const jsonBody = await jsonResp.json(); |
| 78 | + console.log('Body:', jsonBody); |
| 79 | + console.log('PASS'); |
| 80 | + |
| 81 | + // Test 2: SSE streaming via getReader |
| 82 | + console.log('\n--- Test 2: SSE streaming (getReader) ---'); |
| 83 | + const sseResp = (await h2Fetch(`https://localhost:${port}/v1/test/sse`, { |
| 84 | + method: 'GET', |
| 85 | + headers: { accept: 'text/event-stream' }, |
| 86 | + })) as any; |
| 87 | + console.log('Status:', sseResp.status); |
| 88 | + console.log('Content-Type:', sseResp.headers.get('content-type')); |
| 89 | + |
| 90 | + const reader = sseResp.body.getReader(); |
| 91 | + const decoder = new TextDecoder(); |
| 92 | + const events: string[] = []; |
| 93 | + |
| 94 | + while (true) { |
| 95 | + const { done, value } = await reader.read(); |
| 96 | + if (done) break; |
| 97 | + const text = decoder.decode(value, { stream: true }); |
| 98 | + events.push(text); |
| 99 | + console.log(' Chunk:', JSON.stringify(text)); |
| 100 | + } |
| 101 | + reader.releaseLock(); |
| 102 | + console.log('Chunks received:', events.length); |
| 103 | + console.log('PASS'); |
| 104 | + |
| 105 | + // Test 3: SSE via async iteration (Node 18+ fast path) |
| 106 | + console.log('\n--- Test 3: SSE streaming (async iteration) ---'); |
| 107 | + const sseResp2 = (await h2Fetch(`https://localhost:${port}/v1/test/sse`, { |
| 108 | + method: 'GET', |
| 109 | + headers: { accept: 'text/event-stream' }, |
| 110 | + })) as any; |
| 111 | + |
| 112 | + const iterEvents: string[] = []; |
| 113 | + for await (const chunk of sseResp2.body) { |
| 114 | + const text = decoder.decode(chunk, { stream: true }); |
| 115 | + iterEvents.push(text); |
| 116 | + console.log(' Chunk:', JSON.stringify(text)); |
| 117 | + } |
| 118 | + console.log('Chunks received:', iterEvents.length); |
| 119 | + console.log('PASS'); |
| 120 | + |
| 121 | + console.log('\n=== All SSE tests PASSED ==='); |
| 122 | + await h2Fetch.close(); |
| 123 | + close(); |
| 124 | + |
| 125 | + // Cleanup |
| 126 | + fs.rmSync(tmpDir, { recursive: true }); |
| 127 | + process.exit(0); |
| 128 | +} |
| 129 | + |
| 130 | +main().catch((err) => { |
| 131 | + console.error('FAIL:', err); |
| 132 | + process.exit(1); |
| 133 | +}); |
0 commit comments