@libp2p/tcp 7.0.3 → 8.0.0-e66f4891

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.
@@ -1,13 +1,13 @@
1
- import { CodeError } from '@libp2p/interfaces/errors'
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-connection'
9
- import type { CounterGroup } from '@libp2p/interface-metrics'
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
- log('%s closing socket', lOptsStr)
130
- await new Promise<void>((resolve, reject) => {
131
- const start = Date.now()
129
+ options.signal = options.signal ?? AbortSignal.timeout(closeTimeout)
132
130
 
133
- let timeout: NodeJS.Timeout | undefined
134
-
135
- socket.once('close', () => {
136
- log('%s socket closed', lOptsStr)
137
- // socket completely closed
138
- if (timeout !== undefined) {
139
- clearTimeout(timeout)
140
- }
141
- resolve()
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
- if (socket.destroyed) {
152
- if (timeout !== undefined) {
153
- clearTimeout(timeout)
142
+ // error closing socket
143
+ if (maConn.timeline.close == null) {
144
+ maConn.timeline.close = Date.now()
154
145
  }
155
- }
156
146
 
157
- reject(err)
158
- })
147
+ reject(err)
148
+ })
159
149
 
160
- // shorten inactivity timeout
161
- socket.setTimeout(closeTimeout)
150
+ // shorten inactivity timeout
151
+ socket.setTimeout(closeTimeout)
162
152
 
163
- // close writable end of the socket
164
- socket.end()
153
+ // close writable end of the socket
154
+ socket.end()
165
155
 
166
- if (socket.writableLength > 0) {
167
- // Attempt to end the socket. If it takes longer to close than the
168
- // timeout, destroy it manually.
169
- timeout = setTimeout(() => {
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
- // will trigger 'error' and 'close' events that resolves promise
177
- socket.destroy(new CodeError('Socket close timeout', 'ERR_SOCKET_CLOSE_TIMEOUT'))
178
- }
179
- }, closeTimeout).unref()
180
- // there are outgoing bytes waiting to be sent
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
- } else {
188
- // nothing to send, destroy immediately, no need the timeout
189
- socket.destroy()
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
 
@@ -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
- }