Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

NodeSocket.ts

96 lines3.4 KB

1/**
2 * Node.js socket constructors and layers for Effect sockets.
3 *
4 * This module combines shared Node stream-backed socket support with
5 * Node-specific WebSocket constructor layers. Use it to open TCP clients, Unix
6 * domain socket clients, adapt existing Node `Duplex` streams, or provide
7 * WebSocket clients to protocols built on Effect's `Socket.Socket`.
8 *
9 * **Mental model**
10 *
11 * TCP and Unix sockets come from `node:net` and are exposed as scoped
12 * `Socket.Socket` values. Stream open, read, write, and close events are
13 * translated to `SocketError` values, and finalization closes or destroys the
14 * underlying stream. WebSocket layers provide only the constructor service used
15 * by `Socket.makeWebSocket`; `layerWebSocket` combines that constructor with a
16 * URL to provide a socket layer.
17 *
18 * **Common tasks**
19 *
20 * - Use `makeNet`, `makeNetChannel`, or `layerNet` for TCP connections.
21 * - Set `NetConnectOpts.path` for Unix domain sockets.
22 * - Use `fromDuplex` when another library already owns a Node `Duplex`.
23 * - Use `layerWebSocketConstructor` for the native WebSocket when present, with
24 * fallback to `ws`; use `layerWebSocketConstructorWS` to force `ws`.
25 *
26 * **Gotchas**
27 *
28 * Socket lifetime is scoped, so release the layer or scope to close the
29 * connection. Writes complete when Node accepts or flushes the chunk, not when
30 * a peer processes it. Remote `end` events complete the socket run, while
31 * abnormal closes, open timeouts, and stream errors surface through
32 * `SocketError`; handle them in the Effect that runs the socket.
33 *
34 * @since 4.0.0
35 */
36import { NodeWS as WS } from "@effect/platform-node-shared/NodeSocket"
37import type * as Duration from "effect/Duration"
38import type * as Effect from "effect/Effect"
39import { flow } from "effect/Function"
40import * as Layer from "effect/Layer"
41import * as Socket from "effect/unstable/socket/Socket"
42​
43/**
44 * @since 4.0.0
45 */
46export * from "@effect/platform-node-shared/NodeSocket"
47​
48/**
49 * Provides a `Socket.WebSocketConstructor`, using `globalThis.WebSocket` when
50 * available and falling back to the `ws` package otherwise.
51 *
52 * @category layers
53 * @since 4.0.0
54 */
55export const layerWebSocketConstructor: Layer.Layer<
56 Socket.WebSocketConstructor
57> = Layer.sync(Socket.WebSocketConstructor)(() => {
58 if ("WebSocket" in globalThis) {
59 return (url, protocols) => new globalThis.WebSocket(url, protocols)
60 }
61 return (url, protocols) => new WS.WebSocket(url, protocols) as unknown as globalThis.WebSocket
62})
63​
64/**
65 * Provides a `Socket.WebSocketConstructor` backed explicitly by the `ws`
66 * package.
67 *
68 * @category layers
69 * @since 4.0.0
70 */
71export const layerWebSocketConstructorWS: Layer.Layer<
72 Socket.WebSocketConstructor
73> = Layer.succeed(Socket.WebSocketConstructor)(
74 (url, protocols) => new WS.WebSocket(url, protocols) as unknown as globalThis.WebSocket
76​
77/**
78 * Creates a `Socket.Socket` layer for a WebSocket URL using the Node WebSocket
79 * constructor layer, honoring protocol, open-timeout, and close-code error
80 * options.
81 *
82 * @category layers
83 * @since 4.0.0
84 */
85export const layerWebSocket: (
86 url: string | Effect.Effect<string>,
87 options?: {
88 readonly closeCodeIsError?: ((code: number) => boolean) | undefined
89 readonly openTimeout?: Duration.Input | undefined
90 readonly protocols?: string | Array<string> | undefined
91 } | undefined
92) => Layer.Layer<Socket.Socket, never, never> = flow(
93 Socket.makeWebSocket,
94 Layer.effect(Socket.Socket),
95 Layer.provide(layerWebSocketConstructor)