MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeNet

Function makeNet

packages/platform/node-shared/src/NodeSocket.ts:59–98  ·  view source on GitHub ↗
(
  options: Net.NetConnectOpts & {
    readonly openTimeout?: Duration.Input | undefined
  }
)

Source from the content-addressed store, hash-verified

57 * @since 4.0.0
58 */
59export const makeNet = (
60 options: Net.NetConnectOpts & {
61 readonly openTimeout?: Duration.Input | undefined
62 }
63): Effect.Effect<Socket.Socket> =>
64 fromDuplex(
65 Effect.contextWith((context: Context.Context<Scope.Scope>) => {
66 let conn: Net.Socket | undefined
67 return Effect.flatMap(
68 Scope.addFinalizer(
69 Context.get(context, Scope.Scope),
70 Effect.sync(() => {
71 if (!conn) return
72 if (conn.closed === false) {
73 if ("destroySoon" in conn) {
74 conn.destroySoon()
75 } else {
76 ;(conn as Net.Socket).destroy()
77 }
78 }
79 })
80 ),
81 () =>
82 Effect.callback<Net.Socket, Socket.SocketError, never>((resume) => {
83 conn = Net.createConnection(options)
84 conn.once("connect", () => {
85 resume(Effect.succeed(conn!))
86 })
87 conn.on("error", (cause: Error) => {
88 resume(Effect.fail(
89 new Socket.SocketError({
90 reason: new Socket.SocketOpenError({ kind: "Unknown", cause })
91 })
92 ))
93 })
94 })
95 )
96 }),
97 options
98 )
99
100/**
101 * Adapts a Node `Duplex` into a `Socket.Socket`, wiring data events to socket

Callers 1

makeNetChannelFunction · 0.85

Calls 9

addFinalizerMethod · 0.80
onceMethod · 0.80
fromDuplexFunction · 0.70
getMethod · 0.65
onMethod · 0.65
resumeFunction · 0.50
syncMethod · 0.45
succeedMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected