The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect, it } from "@effect/vitest"; |
| 2 | import * as NodeWS from "@effect/platform-node/NodeWS"; |
| 3 | import * as NodeNet from "node:net"; |
| 4 | import * as Context from "effect/Context"; |
| 5 | import * as Deferred from "effect/Deferred"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Fiber from "effect/Fiber"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import { HttpRouter, HttpServer } from "effect/http"; |
| 10 | import * as Socket from "effect/socket/Socket"; |
| 11 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
| 12 | import { |
| 13 | AuthOrchestrationOperateScope, |
| 14 | AuthSessionId, |
| 15 | LOCAL_DEVICE_HOST_ID, |
| 16 | } from "@t3tools/contracts"; |
| 17 | import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; |
| 18 | import * as SessionStore from "../auth/SessionStore.ts"; |
| 19 | import * as DeviceService from "./DeviceService.ts"; |
| 20 | import * as DeviceHubProxy from "./DeviceHubProxy.ts"; |
| 21 | |
| 22 | it.effect("preserves binary input and admission frames through the real socket proxy", () => |
| 23 | Effect.gen(function* () { |
| 24 | const hub = yield* Effect.acquireRelease( |
| 25 | Effect.sync(() => new NodeWS.WebSocketServer({ port: 0 })), |
| 26 | (server) => |
| 27 | Effect.promise( |
| 28 | () => |
| 29 | new Promise<void>((resolve) => { |
| 30 | for (const client of server.clients) client.terminate(); |
| 31 | server.close(() => resolve()); |
| 32 | }), |
| 33 | ), |
| 34 | ); |
| 35 | if (!hub.address()) |
| 36 | yield* Effect.promise(() => new Promise<void>((resolve) => hub.once("listening", resolve))); |
| 37 | const address = hub.address(); |
| 38 | if (typeof address !== "object" || !address) throw new Error("Missing hub port"); |
| 39 | const upstreamClosed = Promise.withResolvers<void>(); |
| 40 | hub.on("connection", (socket) => { |
| 41 | socket.once("close", upstreamClosed.resolve); |
| 42 | socket.send(Uint8Array.of(0x83)); |
| 43 | socket.on("message", (data, binary) => socket.send(data, { binary })); |
| 44 | }); |
| 45 | const invalidated = yield* Deferred.make<void>(); |
| 46 | const services = yield* Layer.build( |
| 47 | HttpRouter.serve(DeviceHubProxy.layer, { disableListenLog: true }).pipe( |
| 48 | Layer.provideMerge(NodeHttpServer.layerTest), |
| 49 | Layer.provide( |
| 50 | Layer.mock(SessionStore.SessionStore, { |
| 51 | cookieName: "test", |
| 52 | legacyCookieName: undefined, |
| 53 | awaitInvalidation: () => Deferred.await(invalidated), |
| 54 | }), |
| 55 | ), |
| 56 | Layer.provide( |
| 57 | Layer.succeed(EnvironmentAuth.EnvironmentAuth, { |
| 58 | authenticateWebSocketUpgrade: () => |
| 59 | Effect.succeed({ |
| 60 | sessionId: AuthSessionId.make("test"), |
| 61 | subject: "test", |
| 62 | method: "bearer-access-token", |
| 63 | scopes: [AuthOrchestrationOperateScope], |
| 64 | }), |
| 65 | } as unknown as EnvironmentAuth.EnvironmentAuth["Service"]), |
| 66 | ), |
| 67 | Layer.provide( |
| 68 | Layer.succeed(DeviceService.DeviceService, { |
| 69 | currentReadiness: () => |
| 70 | Effect.succeed({ |
| 71 | hostId: LOCAL_DEVICE_HOST_ID, |
| 72 | hub: { origin: `http://127.0.0.1:${address.port}` }, |
| 73 | }), |
| 74 | } as DeviceService.DeviceService["Service"]), |
| 75 | ), |
| 76 | ), |
| 77 | ); |
| 78 | const origin = HttpServer.formatAddress( |
| 79 | Context.get(services, HttpServer.HttpServer).address, |
| 80 | ).replace(/^http/, "ws"); |
| 81 | const client = yield* Effect.acquireRelease( |
| 82 | Effect.sync( |
| 83 | () => |
| 84 | new WebSocket( |
| 85 | `${origin}/api/device-hub/vendor/serve-sim/helper/ws?device=test&wsTicket=test`, |
| 86 | ), |
| 87 | ), |
| 88 | (socket) => Effect.sync(() => socket.close()), |
| 89 | ); |
| 90 | client.binaryType = "arraybuffer"; |
| 91 | const frames: number[][] = []; |
| 92 | const received = Promise.withResolvers<void>(); |
| 93 | client.addEventListener("error", () => received.reject(new Error("Proxy failed"))); |
| 94 | client.addEventListener("message", ({ data }) => { |
| 95 | frames.push(Array.from(new Uint8Array(data as ArrayBuffer))); |
| 96 | if (frames.length === 1) { |
| 97 | client.send(Uint8Array.of(0x03, 1)); |
| 98 | client.send(Uint8Array.of(0x03, 2)); |
| 99 | } else if (frames.length === 3) received.resolve(); |
| 100 | }); |
| 101 | yield* Effect.promise(() => received.promise); |
| 102 | expect(frames).toEqual([[0x83], [0x03, 1], [0x03, 2]]); |
| 103 | |
| 104 | const closed = new Promise<void>((resolve) => |
| 105 | client.addEventListener("close", () => resolve(), { once: true }), |
| 106 | ); |
| 107 | yield* Deferred.succeed(invalidated, undefined); |
| 108 | yield* Effect.promise(() => Promise.all([closed, upstreamClosed.promise])); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | it.live("closes an upstream socket that is still connecting without an unhandled error", () => |
| 113 | Effect.gen(function* () { |
| 114 | const dialed = Promise.withResolvers<NodeNet.Socket>(); |
| 115 | // Accepts the connection and never answers the WebSocket handshake. |
| 116 | const hub = yield* Effect.acquireRelease( |
| 117 | Effect.promise( |
| 118 | () => |
| 119 | new Promise<NodeNet.Server>((resolve) => { |
| 120 | const server = NodeNet.createServer((socket) => dialed.resolve(socket)); |
| 121 | server.listen(0, "127.0.0.1", () => resolve(server)); |
| 122 | }), |
| 123 | ), |
| 124 | (server) => |
| 125 | Effect.promise(() => new Promise<void>((resolve) => server.close(() => resolve()))), |
| 126 | ); |
| 127 | const address = hub.address(); |
| 128 | if (typeof address !== "object" || !address) throw new Error("Missing hub port"); |
| 129 | // Handshake headers, as Simlock's routes send, select the `ws` package over the global WebSocket. |
| 130 | const upstream = yield* Socket.makeWebSocket(`ws://127.0.0.1:${address.port}`, { |
| 131 | headers: { authorization: "Bearer test" }, |
| 132 | }).pipe(Effect.provide(DeviceHubProxy.upstreamWebSocketConstructor)); |
| 133 | const reading = yield* Effect.forkChild( |
| 134 | Effect.scoped(Effect.flatMap(upstream.reader, ({ pull }) => pull)), |
| 135 | ); |
| 136 | const connection = yield* Effect.promise(() => dialed.promise); |
| 137 | const abandoned = new Promise<void>((resolve) => connection.once("close", () => resolve())); |
| 138 | connection.resume(); |
| 139 | yield* Fiber.interrupt(reading); |
| 140 | yield* Effect.promise(() => abandoned); |
| 141 | }), |
| 142 | ); |