The best way to control Claude Code, Codex and more from any computer or phone

main
ts 76 lines 2.64 KB
Raw
1 /**
2 * Node TCP socket transport for Effect Cluster runner-to-runner RPC.
3 *
4 * This module provides the shared Node layers used by socket-based cluster
5 * transports. `layerClientProtocol` opens TCP sockets to peer runner addresses
6 * and wraps them in the current RPC serialization protocol. `layerSocketServer`
7 * exposes the socket server that receives incoming runner RPC traffic.
8 *
9 * @stability unstable
10 * @since 4.0.0
11 */
12 import * as Runners from "effect/cluster/Runners"
13 import * as ShardingConfig from "effect/cluster/ShardingConfig"
14 import * as Effect from "effect/Effect"
15 import * as Layer from "effect/Layer"
16 import * as Option from "effect/Option"
17 import * as RpcClient from "effect/rpc/RpcClient"
18 import * as RpcSerialization from "effect/rpc/RpcSerialization"
19 import { Socket } from "effect/socket/Socket"
20 import type * as SocketServer from "effect/socket/SocketServer"
21 import * as NodeSocket from "./NodeSocket.ts"
22 import * as NodeSocketServer from "./NodeSocketServer.ts"
23
24 /**
25 * Provides the cluster `RpcClientProtocol` by opening TCP sockets to runner
26 * addresses and using the current RPC serialization service.
27 *
28 * @stability unstable
29 * @category layers
30 * @since 4.0.0
31 */
32 export const layerClientProtocol: Layer.Layer<
33 Runners.RpcClientProtocol,
34 never,
35 RpcSerialization.RpcSerialization
36 > = Layer.effect(Runners.RpcClientProtocol)(
37 Effect.gen(function*() {
38 const serialization = yield* RpcSerialization.RpcSerialization
39 return {
40 codecFor: serialization.codecFor,
41 make: Effect.fnUntraced(function*(address) {
42 const socket = yield* NodeSocket.makeNet({
43 openTimeout: 1000,
44 timeout: 5500,
45 host: address.host,
46 port: address.port
47 })
48 return yield* RpcClient.makeProtocolSocket().pipe(
49 Effect.provideService(Socket, socket),
50 Effect.provideService(RpcSerialization.RpcSerialization, serialization)
51 )
52 }, Effect.orDie)
53 }
54 })
55 )
56
57 /**
58 * Provides the socket server used by cluster runners, listening on
59 * `ShardingConfig.runnerListenAddress` or `runnerAddress`.
60 *
61 * @stability unstable
62 * @category layers
63 * @since 4.0.0
64 */
65 export const layerSocketServer: Layer.Layer<
66 SocketServer.SocketServer,
67 SocketServer.SocketServerError,
68 ShardingConfig.ShardingConfig
69 > = Effect.gen(function*() {
70 const config = yield* ShardingConfig.ShardingConfig
71 const listenAddress = Option.orElse(config.runnerListenAddress, () => config.runnerAddress)
72 if (Option.isNone(listenAddress)) {
73 return yield* Effect.die("layerSocketServer: ShardingConfig.runnerListenAddress is None")
74 }
75 return NodeSocketServer.layer(listenAddress.value)
76 }).pipe(Layer.unwrap)