Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

NodeSocket unstable

Node socket adapters for Effect sockets.

This module opens node:net or node:tls connections, or wraps existing Node Duplex streams, and presents them as Socket.Socket values, socket channels, or layers. It also exposes the NetSocket service tag for the underlying Node socket and re-exports the ws package namespace.

9 exports Since v4.0.0 Source

Constructors

fromDuplex

unstable
Since v4.0.0

Adapts a Node Duplex into a Socket.Socket.

Details

Reader acquisition opens the duplex and keeps it paused: each pull drains Node's internal buffer with stream.read(). On Node 26+ that yields each buffered chunk as a separate batch element with no copy. Earlier Node concatenates the buffer into one chunk. The stream is never resumed, so once Node's buffer reaches its highWaterMark the kernel receive window closes and the peer blocks: backpressure is end-to-end with no buffering above Node's own.

Writes use write() return-value backpressure, awaiting one drain when the internal buffer is full. writeAll corks the stream around the batch. Releasing the writer scope half-closes the stream (end()).

Signature

declare function fromDuplex<RO>(open: Effect<Duplex, SocketError, RO>, options?: {
readonly openTimeout?: Input;
readonly tlsServer?: boolean;
}): Effect<Socket, never, Exclude<RO, Scope>>

makeNet

unstable
Since v4.0.0

Opens a Node TCP connection as an Effect socket.

When to use

Use to create a Socket.Socket whose reader acquisition dials net.createConnection.

Details

Supports openTimeout and closes or destroys the underlying socket when the reader scope is finalized.

Signature

declare function makeNet(options: NetConnectOpts & {
readonly openTimeout?: Input | undefined;
}): Effect<Socket>
Since v4.0.0

Creates a Channel over a TCP socket, reading arrays of Uint8Array chunks and writing arrays of bytes, strings, or socket close events.

Signature

declare function makeNetChannel<IE = never>(options: NetConnectOpts): Channel<readonly [Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>], SocketError | IE, void, readonly [string | Uint8Array<ArrayBufferLike> | CloseEvent, string | Uint8Array<ArrayBufferLike> | CloseEvent], IE>

makeTls

unstable
Since v4.0.0

Opens a Node TLS connection as an Effect socket.

When to use

Use to create a Socket.Socket whose reader acquisition dials tls.connect and completes once the TLS handshake has finished.

Details

Accepts the same options as tls.connect, so trust anchors (ca), client certificates (cert / key), ALPN protocols, and servername are set there. A failed handshake, including an untrusted or expired peer certificate, fails with a SocketOpenError. Supports openTimeout and destroys the underlying socket when the reader scope is finalized.

Signature

declare function makeTls(options: ConnectionOptions & {
readonly openTimeout?: Input;
}): Effect<Socket>
Since v4.0.0

Creates a Channel over a TLS socket, reading arrays of Uint8Array chunks and writing arrays of bytes, strings, or socket close events.

Signature

declare function makeTlsChannel<IE = never>(options: ConnectionOptions): Channel<readonly [Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>], SocketError | IE, void, readonly [string | Uint8Array<ArrayBufferLike> | CloseEvent, string | Uint8Array<ArrayBufferLike> | CloseEvent], IE>

Layers

layerNet

unstable
Since v4.0.0

Provides a Socket.Socket by opening a TCP connection with the supplied Node net connection options.

Signature

declare const layerNet: (options: Net.NetConnectOpts) => Layer.Layer<Socket.Socket, Socket.SocketError>

layerTls

unstable
Since v4.0.0

Provides a Socket.Socket by opening a TLS connection with the supplied Node tls connection options.

Signature

declare const layerTls: (options: Tls.ConnectionOptions) => Layer.Layer<Socket.Socket, Socket.SocketError>

Re-Exports

NodeWS

unstable
Since v4.0.0

Signature

declare const NodeWS: any

Services

NetSocket

unstable
Since v4.0.0

Service tag for the underlying Node net.Socket associated with the current socket connection.

Signature

declare class NetSocket extends Shape<"@effect/platform-node/NodeSocket/NetSocket", Socket, this> {
constructor(_: never);
}