|
| 1 | +// Tlon Urbit SSE bounded-read real wire proof (loopback http.createServer). |
| 2 | +import http from "node:http"; |
| 3 | +import type { AddressInfo } from "node:net"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { UrbitSSEClient } from "./sse-client.js"; |
| 6 | + |
| 7 | +const MAX = 16 * 1024 * 1024; |
| 8 | +const TOTAL = 18 * 1024 * 1024; |
| 9 | + |
| 10 | +describe("UrbitSSEClient processStream bounded-read real wire proof", () => { |
| 11 | + it("caps an oversized body streamed chunked over real wire", async () => { |
| 12 | + const CHUNK = 1024 * 1024; |
| 13 | + const server = http.createServer((req, res) => { |
| 14 | + res.writeHead(200, { "content-type": "text/event-stream" }); |
| 15 | + let sent = 0; |
| 16 | + const tick = setInterval(() => { |
| 17 | + if (sent < 18) { |
| 18 | + res.write(Buffer.alloc(CHUNK)); |
| 19 | + sent++; |
| 20 | + } else { |
| 21 | + clearInterval(tick); |
| 22 | + res.end(); |
| 23 | + } |
| 24 | + }, 1); |
| 25 | + }); |
| 26 | + await new Promise<void>((resolve, reject) => { |
| 27 | + server.once("error", reject); |
| 28 | + server.listen(0, "127.0.0.1", () => { |
| 29 | + resolve(); |
| 30 | + }); |
| 31 | + }); |
| 32 | + const port = (server.address() as AddressInfo).port; |
| 33 | + |
| 34 | + try { |
| 35 | + const fetchRes = await fetch(`http://127.0.0.1:${port}/`); |
| 36 | + const client = new UrbitSSEClient(`http://127.0.0.1:${port}`, "urbauth-~zod=123", { |
| 37 | + autoReconnect: false, |
| 38 | + }); |
| 39 | + |
| 40 | + let captured: Error | undefined; |
| 41 | + try { |
| 42 | + await client.processStream(fetchRes.body); |
| 43 | + } catch (err) { |
| 44 | + captured = err as Error; |
| 45 | + } |
| 46 | + expect(captured).toBeInstanceOf(Error); |
| 47 | + const match = (captured as Error).message.match( |
| 48 | + /tlon Urbit SSE: body exceeds (\d+) bytes \(got (\d+)\)/, |
| 49 | + ); |
| 50 | + expect(match).not.toBeNull(); |
| 51 | + const cap = Number(match![1]); |
| 52 | + const got = Number(match![2]); |
| 53 | + expect(cap).toBe(MAX); |
| 54 | + // Loopback TCP framing can coalesce the final packet, so the reported |
| 55 | + // size at throw time is somewhere between MAX (cap fired) and TOTAL |
| 56 | + // (server's full body) — both bounds prove (a) cap fired (got > MAX) |
| 57 | + // and (b) we did not buffer beyond the server's full 18 MiB (got < TOTAL). |
| 58 | + expect(got).toBeGreaterThan(MAX); |
| 59 | + expect(got).toBeLessThan(TOTAL); |
| 60 | + console.log( |
| 61 | + `[tlon SSE bounded-read proof] oversized path: cap=${MAX} reported=${got} server_total=${TOTAL}`, |
| 62 | + ); |
| 63 | + } finally { |
| 64 | + await new Promise<void>((resolve) => { |
| 65 | + server.close(() => { |
| 66 | + resolve(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns and dispatches events for normal-size SSE body on real wire", async () => { |
| 73 | + const eventText = 'data: {"json":{"hello":"world"}}\n\n' + 'data: {"json":{"foo":"bar"}}\n\n'; |
| 74 | + const server = http.createServer((req, res) => { |
| 75 | + res.writeHead(200, { "content-type": "text/event-stream" }); |
| 76 | + res.end(eventText); |
| 77 | + }); |
| 78 | + await new Promise<void>((resolve, reject) => { |
| 79 | + server.once("error", reject); |
| 80 | + server.listen(0, "127.0.0.1", () => { |
| 81 | + resolve(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + const port = (server.address() as AddressInfo).port; |
| 85 | + |
| 86 | + try { |
| 87 | + const fetchRes = await fetch(`http://127.0.0.1:${port}/`); |
| 88 | + const received: unknown[] = []; |
| 89 | + const client = new UrbitSSEClient(`http://127.0.0.1:${port}`, "urbauth-~zod=123", { |
| 90 | + autoReconnect: false, |
| 91 | + logger: { log: () => {}, error: () => {} }, |
| 92 | + }); |
| 93 | + // One subscription handler. Each JSON payload without an `id` field |
| 94 | + // hits the broadcast branch (processEvent line 316-321), so every |
| 95 | + // event fires this single handler exactly once. |
| 96 | + void client.subscribe({ |
| 97 | + app: "chat", |
| 98 | + path: "/dm-inbox", |
| 99 | + event: (data) => received.push(data), |
| 100 | + }); |
| 101 | + await client.processStream(fetchRes.body); |
| 102 | + expect(received).toEqual([{ hello: "world" }, { foo: "bar" }]); |
| 103 | + console.log(`[tlon SSE bounded-read proof] normal path: events received=${received.length}`); |
| 104 | + } finally { |
| 105 | + await new Promise<void>((resolve) => { |
| 106 | + server.close(() => { |
| 107 | + resolve(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments