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

Function makeNet

packages/platform/node-shared/src/NodeSocket.ts:87–127  ·  view source on GitHub ↗
(
  options: Net.NetConnectOpts & {
    readonly openTimeout?: Duration.Input | undefined
    /**
     * Maximum chunks per pull. Defaults to 4096 unless a positive safe integer or Infinity is supplied.
     * Use Infinity for unbounded batching.
     */
    readonly chunkSize?: number | undefined
  }
)

Source from the content-addressed store, hash-verified

85 * @since 4.0.0
86 */
87export const makeNet = (
88 options: Net.NetConnectOpts & {
89 readonly openTimeout?: Duration.Input | undefined
90 /**
91 * Maximum chunks per pull. Defaults to 4096 unless a positive safe integer or Infinity is supplied.
92 * Use Infinity for unbounded batching.
93 */
94 readonly chunkSize?: number | undefined
95 }
96): Effect.Effect<Socket.Socket> =>
97 fromDuplex(
98 Effect.contextWith((context: Context.Context<Scope.Scope>) => {
99 let conn: Net.Socket | undefined
100 let isOpen = false
101 return Effect.flatMap(
102 Scope.addFinalizer(
103 Context.get(context, Scope.Scope),
104 Effect.sync(() => {
105 if (!conn) return
106 closeSocket(conn, isOpen)
107 })
108 ),
109 () =>
110 Effect.callback<Net.Socket, Socket.SocketError, never>((resume) => {
111 conn = Net.createConnection(options)
112 conn.once("connect", () => {
113 isOpen = true
114 resume(Effect.succeed(conn!))
115 })
116 conn.on("error", (cause: Error) => {
117 resume(Effect.fail(
118 new Socket.SocketError({
119 reason: new Socket.SocketOpenError({ kind: "Unknown", cause })
120 })
121 ))
122 })
123 })
124 )
125 }),
126 options
127 )
128
129/**
130 * Adapts a Node `Duplex` into a `Socket.Socket`.

Callers 1

makeNetChannelFunction · 0.85

Calls 10

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

Tested by

no test coverage detected