@@ -8,9 +8,8 @@ import { multiaddrToNetConfig } from './utils.js'
8
8
import { AbortError } from '@libp2p/interfaces/errors'
9
9
import { CODE_CIRCUIT , CODE_P2P , CODE_UNIX } from './constants.js'
10
10
import { CreateListenerOptions , DialOptions , symbol , Transport } from '@libp2p/interface-transport'
11
- import type { Multiaddr } from '@multiformats/multiaddr'
11
+ import type { AbortOptions , Multiaddr } from '@multiformats/multiaddr'
12
12
import type { Socket , IpcSocketConnectOpts , TcpSocketConnectOpts } from 'net'
13
- import type { AbortOptions } from '@libp2p/interfaces'
14
13
import type { Connection } from '@libp2p/interface-connection'
15
14
16
15
const log = logger ( 'libp2p:tcp' )
@@ -32,6 +31,24 @@ export interface TCPOptions {
32
31
socketCloseTimeout ?: number
33
32
}
34
33
34
+ /**
35
+ * Expose a subset of net.connect options
36
+ */
37
+ export interface TCPSocketOptions extends AbortOptions {
38
+ noDelay ?: boolean
39
+ keepAlive ?: boolean
40
+ keepAliveInitialDelay ?: number
41
+ allowHalfOpen ?: boolean
42
+ }
43
+
44
+ export interface TCPDialOptions extends DialOptions , TCPSocketOptions {
45
+
46
+ }
47
+
48
+ export interface TCPCreateListenerOptions extends CreateListenerOptions , TCPSocketOptions {
49
+
50
+ }
51
+
35
52
export class TCP implements Transport {
36
53
private readonly opts : TCPOptions
37
54
@@ -47,9 +64,10 @@ export class TCP implements Transport {
47
64
return '@libp2p/tcp'
48
65
}
49
66
50
- async dial ( ma : Multiaddr , options : DialOptions ) : Promise < Connection > {
67
+ async dial ( ma : Multiaddr , options : TCPDialOptions ) : Promise < Connection > {
68
+ options . keepAlive = options . keepAlive ?? true
69
+
51
70
const socket = await this . _connect ( ma , options )
52
- socket . setKeepAlive ( true )
53
71
54
72
// Avoid uncaught errors caused by unstable connections
55
73
socket . on ( 'error' , err => {
@@ -68,7 +86,7 @@ export class TCP implements Transport {
68
86
return conn
69
87
}
70
88
71
- async _connect ( ma : Multiaddr , options : AbortOptions = { } ) {
89
+ async _connect ( ma : Multiaddr , options : TCPDialOptions ) {
72
90
if ( options . signal ?. aborted === true ) {
73
91
throw new AbortError ( )
74
92
}
@@ -137,7 +155,7 @@ export class TCP implements Transport {
137
155
* anytime a new incoming Connection has been successfully upgraded via
138
156
* `upgrader.upgradeInbound`.
139
157
*/
140
- createListener ( options : CreateListenerOptions ) {
158
+ createListener ( options : TCPCreateListenerOptions ) {
141
159
return createListener ( {
142
160
...options ,
143
161
socketInactivityTimeout : this . opts . inboundSocketInactivityTimeout ,
0 commit comments