The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { NodeHttpServer, NodeSocket } from "@effect/platform-node" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import { Effect, Layer, Schema, Stream } from "effect" |
| 4 | import { GraphQL, GraphQLClient, GraphQLGroup, GraphQLProtocol } from "effect/graphql" |
| 5 | import { FetchHttpClient, HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http" |
| 6 | import type * as NetAddress from "effect/net/NetAddress" |
| 7 | |
| 8 | const Viewer = GraphQL.query("Viewer", { |
| 9 | document: "query Viewer{viewer{login}}", |
| 10 | result: Schema.Struct({ viewer: Schema.Struct({ login: Schema.String }) }) |
| 11 | }) |
| 12 | |
| 13 | const IssueUpdated = GraphQL.subscription("IssueUpdated", { |
| 14 | document: "subscription IssueUpdated($id:ID!){issueUpdated(id:$id){title}}", |
| 15 | variables: { id: Schema.String }, |
| 16 | result: Schema.Struct({ issueUpdated: Schema.Struct({ title: Schema.String }) }) |
| 17 | }) |
| 18 | |
| 19 | const Group = GraphQLGroup.make(Viewer, IssueUpdated) |
| 20 | |
| 21 | const encoder = new TextEncoder() |
| 22 | const decoder = new TextDecoder() |
| 23 | |
| 24 | const viewer = { data: { viewer: { login: "tim" } } } |
| 25 | const issue = (title: string) => ({ data: { issueUpdated: { title } } }) |
| 26 | |
| 27 | /** |
| 28 | * POST answers queries with JSON and subscriptions with a graphql-sse stream. |
| 29 | */ |
| 30 | const post = Effect.gen(function*() { |
| 31 | const request = yield* HttpServerRequest.HttpServerRequest |
| 32 | if (!(request.headers["accept"] ?? "").includes("text/event-stream")) { |
| 33 | return yield* HttpServerResponse.json(viewer) |
| 34 | } |
| 35 | return HttpServerResponse.stream( |
| 36 | Stream.make( |
| 37 | `event: next\ndata: ${JSON.stringify(issue("over sse"))}\n\n`, |
| 38 | "event: complete\ndata:\n\n" |
| 39 | ).pipe(Stream.map((chunk) => encoder.encode(chunk))), |
| 40 | { contentType: "text/event-stream" } |
| 41 | ) |
| 42 | }) |
| 43 | |
| 44 | /** |
| 45 | * A hand-rolled graphql-transport-ws server that requires an Authorization |
| 46 | * header on the handshake. |
| 47 | */ |
| 48 | const webSocket = Effect.gen(function*() { |
| 49 | const request = yield* HttpServerRequest.HttpServerRequest |
| 50 | if (request.headers["authorization"] !== "Bearer secret") { |
| 51 | return HttpServerResponse.empty({ status: 401 }) |
| 52 | } |
| 53 | const socket = yield* request.upgrade |
| 54 | const { write } = yield* socket.writer |
| 55 | const { pull } = yield* socket.reader |
| 56 | const send = (message: unknown) => write(JSON.stringify(message)) |
| 57 | while (true) { |
| 58 | for (const frame of yield* pull) { |
| 59 | const message = JSON.parse(typeof frame === "string" ? frame : decoder.decode(frame)) |
| 60 | switch (message.type) { |
| 61 | case "connection_init": |
| 62 | yield* send({ type: "connection_ack" }) |
| 63 | break |
| 64 | case "ping": |
| 65 | yield* send({ type: "pong" }) |
| 66 | break |
| 67 | case "subscribe": |
| 68 | yield* send({ id: message.id, type: "next", payload: issue("over ws") }) |
| 69 | yield* send({ id: message.id, type: "complete" }) |
| 70 | break |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }).pipe( |
| 75 | Effect.scoped, |
| 76 | Effect.catchTag("SocketError", () => Effect.succeed(HttpServerResponse.empty())) |
| 77 | ) |
| 78 | |
| 79 | describe("GraphQLProtocol over a real server", () => { |
| 80 | it.effect( |
| 81 | "serves a query over POST, a subscription over graphql-ws and one over graphql-sse", |
| 82 | () => |
| 83 | Effect.gen(function*() { |
| 84 | yield* HttpRouter.add("POST", "/graphql", post).pipe( |
| 85 | Layer.merge(HttpRouter.add("GET", "/graphql", webSocket)), |
| 86 | (layer) => HttpRouter.serve(layer, { disableListenLog: true }), |
| 87 | Layer.build |
| 88 | ) |
| 89 | const server = yield* HttpServer.HttpServer |
| 90 | const port = (server.address as NetAddress.InetAddress).port |
| 91 | const url = `http://127.0.0.1:${port}/graphql` |
| 92 | |
| 93 | const splitProtocol = yield* Layer.build(GraphQLProtocol.layerHttp({ |
| 94 | url, |
| 95 | subscriptions: { |
| 96 | webSocket: { url: `ws://127.0.0.1:${port}/graphql`, headers: { authorization: "Bearer secret" } } |
| 97 | } |
| 98 | })) |
| 99 | const split = yield* GraphQLClient.make(Group).pipe(Effect.provide(splitProtocol)) |
| 100 | assert.deepStrictEqual(yield* split.Viewer(), viewer.data) |
| 101 | assert.deepStrictEqual( |
| 102 | yield* Stream.runCollect(split.IssueUpdated({ id: "I_1" })), |
| 103 | [issue("over ws").data] |
| 104 | ) |
| 105 | |
| 106 | const sseProtocol = yield* Layer.build(GraphQLProtocol.layerHttp({ url })) |
| 107 | const sse = yield* GraphQLClient.make(Group).pipe(Effect.provide(sseProtocol)) |
| 108 | assert.deepStrictEqual( |
| 109 | yield* Stream.runCollect(sse.IssueUpdated({ id: "I_1" })), |
| 110 | [issue("over sse").data] |
| 111 | ) |
| 112 | }).pipe( |
| 113 | Effect.provide([FetchHttpClient.layer, NodeSocket.layerWebSocketConstructor]), |
| 114 | Effect.provide(NodeHttpServer.layerTest), |
| 115 | Effect.timeout("5 seconds") |
| 116 | ), |
| 117 | 10000 |
| 118 | ) |
| 119 | }) |