@@ -6,8 +6,10 @@ import * as Channel from "effect/Channel"
6
6
import type * as Chunk from "effect/Chunk"
7
7
import * as Context from "effect/Context"
8
8
import * as Deferred from "effect/Deferred"
9
+ import type * as Duration from "effect/Duration"
9
10
import * as Effect from "effect/Effect"
10
11
import * as FiberSet from "effect/FiberSet"
12
+ import { identity } from "effect/Function"
11
13
import * as Layer from "effect/Layer"
12
14
import * as Scope from "effect/Scope"
13
15
import * as Net from "node:net"
@@ -34,7 +36,9 @@ export const NetSocket: Context.Tag<NetSocket, Net.Socket> = Context.GenericTag(
34
36
* @category constructors
35
37
*/
36
38
export const makeNet = (
37
- options : Net . NetConnectOpts
39
+ options : Net . NetConnectOpts & {
40
+ readonly openTimeout ?: Duration . DurationInput | undefined
41
+ }
38
42
) : Effect . Effect < Socket . Socket , Socket . SocketError > =>
39
43
fromDuplex (
40
44
Effect . scopeWith ( ( scope ) => {
@@ -65,15 +69,19 @@ export const makeNet = (
65
69
} )
66
70
} )
67
71
)
68
- } )
72
+ } ) ,
73
+ options
69
74
)
70
75
71
76
/**
72
77
* @since 1.0.0
73
78
* @category constructors
74
79
*/
75
80
export const fromDuplex = < RO > (
76
- open : Effect . Effect < Duplex , Socket . SocketError , RO >
81
+ open : Effect . Effect < Duplex , Socket . SocketError , RO > ,
82
+ options ?: {
83
+ readonly openTimeout ?: Duration . DurationInput | undefined
84
+ }
77
85
) : Effect . Effect < Socket . Socket , never , Exclude < RO , Scope . Scope > > =>
78
86
Effect . withFiberRuntime < Socket . Socket , never , Exclude < RO , Scope . Scope > > ( ( fiber ) => {
79
87
let currentSocket : Duplex | undefined
@@ -96,7 +104,15 @@ export const fromDuplex = <RO>(
96
104
} )
97
105
)
98
106
99
- const conn = yield * Scope . extend ( open , scope )
107
+ const conn = yield * Scope . extend ( open , scope ) . pipe (
108
+ options ?. openTimeout ?
109
+ Effect . timeoutFail ( {
110
+ duration : options . openTimeout ,
111
+ onTimeout : ( ) =>
112
+ new Socket . SocketGenericError ( { reason : "Open" , cause : new Error ( "Connection timed out" ) } )
113
+ } ) :
114
+ identity
115
+ )
100
116
conn . on ( "end" , onEnd )
101
117
conn . on ( "error" , onError )
102
118
conn . on ( "close" , onClose )
0 commit comments