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, 06:00 UTC
| 1 | import * as BunSocket from "@effect/platform-bun/BunSocket" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import { Deferred, Effect } from "effect" |
| 4 | import { Socket } from "effect/socket" |
| 5 | |
| 6 | describe("BunSocket", () => { |
| 7 | it.live("passes headers and a subprotocol to the opening handshake", () => |
| 8 | Effect.gen(function*() { |
| 9 | const handshake = yield* Deferred.make<{ authorization: string | undefined; protocol: string }>() |
| 10 | const server = yield* Effect.acquireRelease( |
| 11 | Effect.sync(() => |
| 12 | Bun.serve<{ authorization: string | undefined; protocol: string }>({ |
| 13 | hostname: "127.0.0.1", |
| 14 | port: 0, |
| 15 | fetch(request, server) { |
| 16 | const protocol = request.headers.get("sec-websocket-protocol") ?? "" |
| 17 | if ( |
| 18 | server.upgrade(request, { |
| 19 | headers: { "Sec-WebSocket-Protocol": protocol }, |
| 20 | data: { |
| 21 | authorization: request.headers.get("authorization") ?? undefined, |
| 22 | protocol |
| 23 | } |
| 24 | }) |
| 25 | ) return |
| 26 | return new Response("Expected a WebSocket upgrade", { status: 400 }) |
| 27 | }, |
| 28 | websocket: { |
| 29 | open(client) { |
| 30 | Deferred.doneUnsafe(handshake, Effect.succeed(client.data)) |
| 31 | }, |
| 32 | message() {} |
| 33 | } |
| 34 | }) |
| 35 | ), |
| 36 | (server) => Effect.promise(() => server.stop(true)) |
| 37 | ) |
| 38 | const options = { |
| 39 | protocols: "graphql-transport-ws", |
| 40 | headers: { Authorization: "Bearer test" } |
| 41 | } |
| 42 | const socket = yield* Socket.makeWebSocket(`ws://127.0.0.1:${server.port}`, options) |
| 43 | yield* socket.reader |
| 44 | assert.deepStrictEqual(yield* Deferred.await(handshake).pipe(Effect.timeout("1 second")), { |
| 45 | authorization: "Bearer test", |
| 46 | protocol: "graphql-transport-ws" |
| 47 | }) |
| 48 | }).pipe(Effect.provide(BunSocket.layerWebSocketConstructor))) |
| 49 | }) |