@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 86 lines 2.8 KB
Raw
1 /**
2 * Node.js socket constructors and layers for Effect sockets.
3 *
4 * This module re-exports the shared Node socket support for TCP connections,
5 * Unix domain socket connections, and Node `Duplex` streams. It also provides
6 * WebSocket client layers backed by Node's global `WebSocket`.
7 *
8 * @stability unstable
9 * @since 4.0.0
10 */
11 import type * as Duration from "effect/Duration"
12 import type * as Effect from "effect/Effect"
13 import { flow } from "effect/Function"
14 import * as Layer from "effect/Layer"
15 import * as Socket from "effect/socket/Socket"
16
17 /**
18 * @since 4.0.0
19 */
20 export * from "@effect/platform-node-shared/NodeSocket"
21
22 /**
23 * Provides a `Socket.WebSocketConstructor` backed by Node's global
24 * `WebSocket`.
25 *
26 * **Details**
27 *
28 * Handshake headers and subprotocols in client options objects are passed to
29 * the constructor as Node's `WebSocketInit`.
30 *
31 * **Gotchas**
32 *
33 * Use `NodeWS.layerWebSocketConstructor` if any of these matter:
34 *
35 * - Reads cannot be paused, so incoming frames are buffered without limit, or
36 * fail the socket with a `SocketReadError` once a configured
37 * `highWaterMark` is exceeded.
38 * - A custom `Host` header is ignored.
39 * - Outgoing messages are not compressed.
40 *
41 * @stability unstable
42 * @category layers
43 * @since 4.0.0
44 */
45 export const layerWebSocketConstructor: Layer.Layer<
46 Socket.WebSocketConstructor
47 > = Layer.succeed(Socket.WebSocketConstructor)((url, options) => {
48 if (options === undefined || typeof options === "string" || Array.isArray(options)) {
49 return new globalThis.WebSocket(url, options)
50 }
51 const init: WebSocketInit = {}
52 if (options.protocols !== undefined) init.protocols = options.protocols
53 if (options.headers !== undefined) init.headers = options.headers
54 return new (globalThis.WebSocket as unknown as NodeWebSocketConstructor)(url, init)
55 })
56
57 // Node's `WebSocketInit`, missing from the DOM `WebSocket` constructor type.
58 interface WebSocketInit {
59 protocols?: string | Array<string>
60 headers?: Readonly<Record<string, string>>
61 }
62
63 type NodeWebSocketConstructor = new(url: string, init: WebSocketInit) => globalThis.WebSocket
64
65 /**
66 * Creates a `Socket.Socket` layer for a WebSocket URL using the Node WebSocket
67 * constructor layer, honoring protocol, handshake-header, open-timeout, and
68 * high-water-mark options.
69 *
70 * @stability unstable
71 * @category layers
72 * @since 4.0.0
73 */
74 export const layerWebSocket: (
75 url: string | Effect.Effect<string>,
76 options?: {
77 readonly openTimeout?: Duration.Input | undefined
78 readonly protocols?: string | Array<string> | undefined
79 readonly headers?: Readonly<Record<string, string>> | undefined
80 readonly highWaterMark?: number | undefined
81 } | undefined
82 ) => Layer.Layer<Socket.Socket, never, never> = flow(
83 Socket.makeWebSocket,
84 Layer.effect(Socket.Socket),
85 Layer.provide(layerWebSocketConstructor)
86 )