-
Notifications
You must be signed in to change notification settings - Fork 443
/
index.ts
131 lines (110 loc) · 3.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @packageDocumentation
*
* A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/) based on the TCP networking stack.
*
* @example
*
* ```TypeScript
* import { createLibp2p } from 'libp2p'
* import { tcp } from '@libp2p/tcp'
* import { multiaddr } from '@multiformats/multiaddr'
*
* const node = await createLibp2p({
* transports: [
* tcp()
* ]
* })
*
* const ma = multiaddr('/ip4/123.123.123.123/tcp/1234')
*
* // dial a TCP connection, timing out after 10 seconds
* const connection = await node.dial(ma, {
* signal: AbortSignal.timeout(10_000)
* })
*
* // use connection...
* ```
*/
import { TCP } from './tcp.js'
import type { CloseServerOnMaxConnectionsOpts } from './listener.js'
import type { ComponentLogger, CounterGroup, Metrics, CreateListenerOptions, DialTransportOptions, Transport, OutboundConnectionUpgradeEvents } from '@libp2p/interface'
import type { AbortOptions } from '@multiformats/multiaddr'
import type { ProgressEvent } from 'progress-events'
export interface TCPOptions {
/**
* An optional number in ms that is used as an inactivity timeout after which the socket will be closed
*/
inboundSocketInactivityTimeout?: number
/**
* An optional number in ms that is used as an inactivity timeout after which the socket will be closed
*/
outboundSocketInactivityTimeout?: number
/**
* When closing a socket, wait this long for it to close gracefully before it is closed more forcibly
*/
socketCloseTimeout?: number
/**
* Set this property to reject connections when the server's connection count gets high.
* https://nodejs.org/api/net.html#servermaxconnections
*/
maxConnections?: number
/**
* Parameter to specify the maximum length of the queue of pending connections
* https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
*/
backlog?: number
/**
* Close server (stop listening for new connections) if connections exceed a limit.
* Open server (start listening for new connections) if connections fall below a limit.
*/
closeServerOnMaxConnections?: CloseServerOnMaxConnectionsOpts
/**
* Options passed to `net.connect` for every opened TCP socket
*/
dialOpts?: TCPSocketOptions
/**
* Options passed to every `net.createServer` for every TCP server
*/
listenOpts?: TCPSocketOptions
}
/**
* Expose a subset of net.connect options
*/
export interface TCPSocketOptions extends AbortOptions {
/**
* @see https://nodejs.org/api/net.html#socketconnectoptions-connectlistener
*/
noDelay?: boolean
/**
* @see https://nodejs.org/api/net.html#socketconnectoptions-connectlistener
*/
keepAlive?: boolean
/**
* @see https://nodejs.org/api/net.html#socketconnectoptions-connectlistener
*/
keepAliveInitialDelay?: number
/**
* @see https://nodejs.org/api/net.html#new-netsocketoptions
*/
allowHalfOpen?: boolean
}
export type TCPDialEvents =
OutboundConnectionUpgradeEvents |
ProgressEvent<'tcp:open-connection'>
export interface TCPDialOptions extends DialTransportOptions<TCPDialEvents>, TCPSocketOptions {
}
export interface TCPCreateListenerOptions extends CreateListenerOptions, TCPSocketOptions {
}
export interface TCPComponents {
metrics?: Metrics
logger: ComponentLogger
}
export interface TCPMetrics {
dialerEvents: CounterGroup<'error' | 'timeout' | 'connect' | 'abort'>
}
export function tcp (init: TCPOptions = {}): (components: TCPComponents) => Transport {
return (components: TCPComponents) => {
return new TCP(components, init)
}
}