(
options: Net.NetConnectOpts & {
readonly openTimeout?: Duration.Input | undefined
}
)
| 57 | * @since 4.0.0 |
| 58 | */ |
| 59 | export 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 |
no test coverage detected