|
| 1 | +import { describe, it } from "@effectionx/bdd"; |
| 2 | +import { expect } from "expect"; |
| 3 | +import { createServer } from "node:net"; |
| 4 | +import type { Method, Handle, Inspector } from "../lib/types.ts"; |
| 5 | +import { createProtocol } from "../lib/mod.ts"; |
| 6 | +import { attach } from "../lib/attach.ts"; |
| 7 | +import { createScope, call, suspend, useScope, withResolvers } from "effection"; |
| 8 | +import { scope as arktypeScope } from "arktype"; |
| 9 | +import { useSSEServer } from "../lib/sse-server.ts"; |
| 10 | + |
| 11 | +describe("useSSEServer()", () => { |
| 12 | + describe("generic echo protocol", () => { |
| 13 | + it("keeps an active SSE request alive long enough to flush during shutdown", function* () { |
| 14 | + const [serverScope, destroyServerScope] = createScope(); |
| 15 | + const [clientScope, destroyClientScope] = createScope(); |
| 16 | + |
| 17 | + const schema = arktypeScope({ |
| 18 | + NoneArr: "never[]", |
| 19 | + None: "never", |
| 20 | + Str: "string", |
| 21 | + }).export(); |
| 22 | + |
| 23 | + const protocol = createProtocol({ |
| 24 | + echo: { |
| 25 | + args: schema.NoneArr, |
| 26 | + progress: schema.None, |
| 27 | + returns: schema.Str, |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + const requestStarted = withResolvers<void>(); |
| 32 | + const continueRequest = withResolvers<void>(); |
| 33 | + |
| 34 | + const handle = { |
| 35 | + protocol, |
| 36 | + methods: {} as any, |
| 37 | + invoke() { |
| 38 | + return (function* () { |
| 39 | + return { |
| 40 | + *next() { |
| 41 | + requestStarted.resolve(); |
| 42 | + yield* continueRequest.operation; |
| 43 | + return { done: true, value: "goodbye" }; |
| 44 | + }, |
| 45 | + }; |
| 46 | + })(); |
| 47 | + }, |
| 48 | + } as unknown as Handle<{ echo: Method<never[], never, string> }>; |
| 49 | + |
| 50 | + const addressResolver = withResolvers<string>(); |
| 51 | + serverScope.run(function* () { |
| 52 | + const address = yield* useSSEServer(handle, { port: yield* call(getAvailablePort) }); |
| 53 | + addressResolver.resolve(address); |
| 54 | + yield* suspend(); |
| 55 | + }); |
| 56 | + |
| 57 | + const address = yield* addressResolver.operation; |
| 58 | + |
| 59 | + const requestTask = clientScope.run(function* () { |
| 60 | + const response = yield* call(() => |
| 61 | + fetch(`${address}/echo`, { |
| 62 | + method: "POST", |
| 63 | + headers: { "content-type": "application/json" }, |
| 64 | + body: "[]", |
| 65 | + }), |
| 66 | + ); |
| 67 | + |
| 68 | + const text = yield* call(async () => { |
| 69 | + const decoder = new TextDecoder(); |
| 70 | + const reader = response.body?.getReader(); |
| 71 | + let result = ""; |
| 72 | + |
| 73 | + if (!reader) { |
| 74 | + throw new Error("response body is missing"); |
| 75 | + } |
| 76 | + |
| 77 | + while (true) { |
| 78 | + const { done, value } = await reader.read(); |
| 79 | + if (done) break; |
| 80 | + result += decoder.decode(value, { stream: true }); |
| 81 | + } |
| 82 | + |
| 83 | + return result; |
| 84 | + }); |
| 85 | + |
| 86 | + return { status: response.status, text }; |
| 87 | + }); |
| 88 | + |
| 89 | + yield* requestStarted.operation; |
| 90 | + continueRequest.resolve(); |
| 91 | + yield* destroyServerScope(); |
| 92 | + |
| 93 | + const result = yield* requestTask; |
| 94 | + yield* destroyClientScope(); |
| 95 | + |
| 96 | + expect(result.status).toBe(200); |
| 97 | + expect(result.text).toContain("event: return"); |
| 98 | + expect(result.text).toContain("goodbye"); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("loader path integration", () => { |
| 103 | + it("uses attach() and useSSEServer() together and keeps the SSE request alive on shutdown", function* () { |
| 104 | + const schema = arktypeScope({ |
| 105 | + NoneArr: "never[]", |
| 106 | + None: "never", |
| 107 | + Str: "string", |
| 108 | + }).export(); |
| 109 | + |
| 110 | + const protocol = createProtocol({ |
| 111 | + echo: { |
| 112 | + args: schema.NoneArr, |
| 113 | + progress: schema.None, |
| 114 | + returns: schema.Str, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + const handle: Handle<{ echo: Method<never[], never, string> }> = { |
| 119 | + protocol, |
| 120 | + methods: { |
| 121 | + echo: function* () { |
| 122 | + return { |
| 123 | + *next() { |
| 124 | + return { done: true, value: "goodbye" }; |
| 125 | + }, |
| 126 | + }; |
| 127 | + }, |
| 128 | + } as any, |
| 129 | + invoke({ name, args }) { |
| 130 | + return this.methods[name](...args); |
| 131 | + }, |
| 132 | + }; |
| 133 | + |
| 134 | + const inspector: Inspector<{ echo: Method<never[], never, string> }> = { |
| 135 | + protocol, |
| 136 | + *attach(scope) { |
| 137 | + return handle; |
| 138 | + }, |
| 139 | + }; |
| 140 | + |
| 141 | + const scope = yield* useScope(); |
| 142 | + const addressResolver = withResolvers<string>(); |
| 143 | + const detach = yield* attach(scope, inspector, function* (handle) { |
| 144 | + const address = yield* useSSEServer(handle, { port: yield* call(getAvailablePort) }); |
| 145 | + addressResolver.resolve(address); |
| 146 | + }); |
| 147 | + |
| 148 | + const address = yield* addressResolver.operation; |
| 149 | + yield* detach(); |
| 150 | + |
| 151 | + expect(address).toMatch(/^http:\/\/localhost:\d+$/); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +function getAvailablePort(): Promise<number> { |
| 157 | + return new Promise((resolve, reject) => { |
| 158 | + const server = createServer(); |
| 159 | + |
| 160 | + server.once("error", (error) => { |
| 161 | + reject(error); |
| 162 | + }); |
| 163 | + |
| 164 | + server.listen(0, () => { |
| 165 | + const address = server.address(); |
| 166 | + if (address && typeof address === "object") { |
| 167 | + const port = address.port; |
| 168 | + server.close((error) => { |
| 169 | + if (error) { |
| 170 | + reject(error); |
| 171 | + } else { |
| 172 | + resolve(port); |
| 173 | + } |
| 174 | + }); |
| 175 | + } else { |
| 176 | + reject(new Error("unable to determine ephemeral port")); |
| 177 | + } |
| 178 | + }); |
| 179 | + }); |
| 180 | +} |
0 commit comments