@libp2p/tcp 7.0.3 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.min.js +4 -4
- package/dist/src/constants.d.ts +1 -1
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/listener.d.ts +4 -4
- package/dist/src/listener.d.ts.map +1 -1
- package/dist/src/listener.js +1 -1
- package/dist/src/listener.js.map +1 -1
- package/dist/src/socket-to-conn.d.ts +2 -2
- package/dist/src/socket-to-conn.d.ts.map +1 -1
- package/dist/src/socket-to-conn.js +41 -54
- package/dist/src/socket-to-conn.js.map +1 -1
- package/package.json +14 -109
- package/src/constants.ts +1 -1
- package/src/index.ts +4 -4
- package/src/listener.ts +4 -4
- package/src/socket-to-conn.ts +45 -59
- package/dist/typedoc-urls.json +0 -10
package/src/socket-to-conn.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr'
|
|
4
4
|
// @ts-expect-error no types
|
|
5
5
|
import toIterable from 'stream-to-it'
|
|
6
6
|
import { CLOSE_TIMEOUT, SOCKET_TIMEOUT } from './constants.js'
|
|
7
7
|
import { multiaddrToNetConfig } from './utils.js'
|
|
8
|
-
import type { MultiaddrConnection } from '@libp2p/interface
|
|
9
|
-
import type { CounterGroup } from '@libp2p/interface
|
|
10
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
8
|
+
import type { MultiaddrConnection } from '@libp2p/interface/connection'
|
|
9
|
+
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
10
|
+
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
|
|
11
11
|
import type { Socket } from 'net'
|
|
12
12
|
|
|
13
13
|
const log = logger('libp2p:tcp:socket')
|
|
@@ -120,75 +120,61 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
|
|
|
120
120
|
|
|
121
121
|
timeline: { open: Date.now() },
|
|
122
122
|
|
|
123
|
-
async close () {
|
|
123
|
+
async close (options: AbortOptions = {}) {
|
|
124
124
|
if (socket.destroyed) {
|
|
125
125
|
log('%s socket was already destroyed when trying to close', lOptsStr)
|
|
126
126
|
return
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
await new Promise<void>((resolve, reject) => {
|
|
131
|
-
const start = Date.now()
|
|
129
|
+
options.signal = options.signal ?? AbortSignal.timeout(closeTimeout)
|
|
132
130
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
socket.once('error', (err: Error) => {
|
|
144
|
-
log('%s socket error', lOptsStr, err)
|
|
145
|
-
|
|
146
|
-
// error closing socket
|
|
147
|
-
if (maConn.timeline.close == null) {
|
|
148
|
-
maConn.timeline.close = Date.now()
|
|
149
|
-
}
|
|
131
|
+
try {
|
|
132
|
+
log('%s closing socket', lOptsStr)
|
|
133
|
+
await new Promise<void>((resolve, reject) => {
|
|
134
|
+
socket.once('close', () => {
|
|
135
|
+
// socket completely closed
|
|
136
|
+
log('%s socket closed', lOptsStr)
|
|
137
|
+
resolve()
|
|
138
|
+
})
|
|
139
|
+
socket.once('error', (err: Error) => {
|
|
140
|
+
log('%s socket error', lOptsStr, err)
|
|
150
141
|
|
|
151
|
-
|
|
152
|
-
if (
|
|
153
|
-
|
|
142
|
+
// error closing socket
|
|
143
|
+
if (maConn.timeline.close == null) {
|
|
144
|
+
maConn.timeline.close = Date.now()
|
|
154
145
|
}
|
|
155
|
-
}
|
|
156
146
|
|
|
157
|
-
|
|
158
|
-
|
|
147
|
+
reject(err)
|
|
148
|
+
})
|
|
159
149
|
|
|
160
|
-
|
|
161
|
-
|
|
150
|
+
// shorten inactivity timeout
|
|
151
|
+
socket.setTimeout(closeTimeout)
|
|
162
152
|
|
|
163
|
-
|
|
164
|
-
|
|
153
|
+
// close writable end of the socket
|
|
154
|
+
socket.end()
|
|
165
155
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (socket.destroyed) {
|
|
171
|
-
log('%s is already destroyed', lOptsStr)
|
|
172
|
-
resolve()
|
|
173
|
-
} else {
|
|
174
|
-
log('%s socket close timeout after %dms, destroying it manually', lOptsStr, Date.now() - start)
|
|
156
|
+
if (socket.writableLength > 0) {
|
|
157
|
+
// there are outgoing bytes waiting to be sent
|
|
158
|
+
socket.once('drain', () => {
|
|
159
|
+
log('%s socket drained', lOptsStr)
|
|
175
160
|
|
|
176
|
-
//
|
|
177
|
-
socket.destroy(
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
socket.once('drain', () => {
|
|
182
|
-
log('%s socket drained', lOptsStr)
|
|
183
|
-
|
|
184
|
-
// all bytes have been sent we can destroy the socket (maybe) before the timeout
|
|
161
|
+
// all bytes have been sent we can destroy the socket (maybe) before the timeout
|
|
162
|
+
socket.destroy()
|
|
163
|
+
})
|
|
164
|
+
} else {
|
|
165
|
+
// nothing to send, destroy immediately, no need for the timeout
|
|
185
166
|
socket.destroy()
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
} catch (err: any) {
|
|
170
|
+
this.abort(err)
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
abort: (err: Error) => {
|
|
175
|
+
log('%s socket abort due to error', lOptsStr, err)
|
|
176
|
+
|
|
177
|
+
socket.destroy(err)
|
|
192
178
|
}
|
|
193
179
|
}
|
|
194
180
|
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"CloseServerOnMaxConnectionsOpts": "https://libp2p.github.io/js-libp2p-tcp/interfaces/_internal_.CloseServerOnMaxConnectionsOpts.html",
|
|
3
|
-
"TCPComponents": "https://libp2p.github.io/js-libp2p-tcp/interfaces/TCPComponents.html",
|
|
4
|
-
"TCPCreateListenerOptions": "https://libp2p.github.io/js-libp2p-tcp/interfaces/TCPCreateListenerOptions.html",
|
|
5
|
-
"TCPDialOptions": "https://libp2p.github.io/js-libp2p-tcp/interfaces/TCPDialOptions.html",
|
|
6
|
-
"TCPMetrics": "https://libp2p.github.io/js-libp2p-tcp/interfaces/TCPMetrics.html",
|
|
7
|
-
"TCPOptions": "https://libp2p.github.io/js-libp2p-tcp/interfaces/TCPOptions.html",
|
|
8
|
-
"TCPSocketOptions": "https://libp2p.github.io/js-libp2p-tcp/interfaces/TCPSocketOptions.html",
|
|
9
|
-
"tcp": "https://libp2p.github.io/js-libp2p-tcp/functions/tcp.html"
|
|
10
|
-
}
|