@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 57 lines 1.91 KB
Raw
1 /**
2 * Browser WebSocket layers for Effect sockets.
3 *
4 * `layerWebSocket` creates a `Socket.Socket` connected to a WebSocket URL using
5 * the browser `WebSocket` constructor. `layerWebSocketConstructor` provides
6 * only the browser-backed constructor service for lower-level socket code.
7 *
8 * @stability unstable
9 * @since 4.0.0
10 */
11 import type * as Duration from "effect/Duration"
12 import * as Layer from "effect/Layer"
13 import * as Socket from "effect/socket/Socket"
14
15 /**
16 * Creates a `Socket` layer connected to the given URL using the browser `WebSocket` constructor.
17 *
18 * **When to use**
19 *
20 * Use when you need browser code to satisfy the platform socket service from a
21 * URL without wiring the browser constructor service separately.
22 *
23 * **Details**
24 *
25 * Delegates socket construction to `Socket.makeWebSocket` and provides the
26 * browser-backed `WebSocketConstructor` service.
27 *
28 * **Gotchas**
29 *
30 * Browser WebSocket rules still control URL schemes, mixed-content blocking,
31 * cookies, authentication, origin checks, subprotocols, and extensions. Every
32 * close, whatever the code, fails the socket's reader with a `SocketError`.
33 *
34 * @see {@link layerWebSocketConstructor} for providing only the browser constructor service
35 *
36 * @stability unstable
37 * @category layers
38 * @since 4.0.0
39 */
40 export const layerWebSocket = (url: string, options?: {
41 readonly openTimeout?: Duration.Input | undefined
42 readonly protocols?: string | Array<string> | undefined
43 readonly highWaterMark?: number | undefined
44 }): Layer.Layer<Socket.Socket> =>
45 Layer.effect(Socket.Socket, Socket.makeWebSocket(url, options)).pipe(
46 Layer.provide(layerWebSocketConstructor)
47 )
48
49 /**
50 * Layer that provides a `WebSocketConstructor` service backed by `globalThis.WebSocket`.
51 *
52 * @stability unstable
53 * @category layers
54 * @since 4.0.0
55 */
56 export const layerWebSocketConstructor: Layer.Layer<Socket.WebSocketConstructor> =
57 Socket.layerWebSocketConstructorGlobal