Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 6401a87

Browse files
authored
fix: expose extra options from net.connect for dial and listen (#211)
To allow more flexibility in use of sockets, expose options from node's net.connect method.
1 parent 637bd31 commit 6401a87

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/index.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { multiaddrToNetConfig } from './utils.js'
88
import { AbortError } from '@libp2p/interfaces/errors'
99
import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'
1010
import { CreateListenerOptions, DialOptions, symbol, Transport } from '@libp2p/interface-transport'
11-
import type { Multiaddr } from '@multiformats/multiaddr'
11+
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
1212
import type { Socket, IpcSocketConnectOpts, TcpSocketConnectOpts } from 'net'
13-
import type { AbortOptions } from '@libp2p/interfaces'
1413
import type { Connection } from '@libp2p/interface-connection'
1514

1615
const log = logger('libp2p:tcp')
@@ -32,6 +31,24 @@ export interface TCPOptions {
3231
socketCloseTimeout?: number
3332
}
3433

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+
3552
export class TCP implements Transport {
3653
private readonly opts: TCPOptions
3754

@@ -47,9 +64,10 @@ export class TCP implements Transport {
4764
return '@libp2p/tcp'
4865
}
4966

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+
5170
const socket = await this._connect(ma, options)
52-
socket.setKeepAlive(true)
5371

5472
// Avoid uncaught errors caused by unstable connections
5573
socket.on('error', err => {
@@ -68,7 +86,7 @@ export class TCP implements Transport {
6886
return conn
6987
}
7088

71-
async _connect (ma: Multiaddr, options: AbortOptions = {}) {
89+
async _connect (ma: Multiaddr, options: TCPDialOptions) {
7290
if (options.signal?.aborted === true) {
7391
throw new AbortError()
7492
}
@@ -137,7 +155,7 @@ export class TCP implements Transport {
137155
* anytime a new incoming Connection has been successfully upgraded via
138156
* `upgrader.upgradeInbound`.
139157
*/
140-
createListener (options: CreateListenerOptions) {
158+
createListener (options: TCPCreateListenerOptions) {
141159
return createListener({
142160
...options,
143161
socketInactivityTimeout: this.opts.inboundSocketInactivityTimeout,

src/listener.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { MultiaddrConnection, Connection } from '@libp2p/interface-connecti
1111
import type { Upgrader, Listener } from '@libp2p/interface-transport'
1212
import type { Server } from 'net'
1313
import type { Multiaddr } from '@multiformats/multiaddr'
14+
import type { TCPCreateListenerOptions } from './index.js'
1415

1516
const log = logger('libp2p:tcp:listener')
1617

@@ -29,7 +30,7 @@ async function attemptClose (maConn: MultiaddrConnection) {
2930
}
3031
}
3132

32-
interface Context {
33+
interface Context extends TCPCreateListenerOptions {
3334
handler?: (conn: Connection) => void
3435
upgrader: Upgrader
3536
socketInactivityTimeout?: number
@@ -44,12 +45,12 @@ export function createListener (context: Context) {
4445
handler, upgrader, socketInactivityTimeout, socketCloseTimeout
4546
} = context
4647

48+
context.keepAlive = context.keepAlive ?? true
49+
4750
let peerId: string | null
4851
let listeningAddr: Multiaddr
4952

50-
const server: ServerWithMultiaddrConnections = Object.assign(net.createServer(socket => {
51-
socket.setKeepAlive(true)
52-
53+
const server: ServerWithMultiaddrConnections = Object.assign(net.createServer(context, socket => {
5354
// Avoid uncaught errors caused by unstable connections
5455
socket.on('error', err => {
5556
log('socket error', err)

0 commit comments

Comments
 (0)