@libp2p/tcp 10.0.6-24fa1d5af → 10.0.6-d9c7e0f7e
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/dist/index.min.js +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/index.d.ts +1 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/listener.d.ts.map +1 -1
- package/dist/src/listener.js +6 -7
- package/dist/src/listener.js.map +1 -1
- package/dist/src/socket-to-conn.d.ts +0 -1
- package/dist/src/socket-to-conn.d.ts.map +1 -1
- package/dist/src/socket-to-conn.js +74 -74
- package/dist/src/socket-to-conn.js.map +1 -1
- package/dist/src/tcp.d.ts.map +1 -1
- package/dist/src/tcp.js +19 -34
- package/dist/src/tcp.js.map +1 -1
- package/package.json +6 -7
- package/src/constants.ts +1 -1
- package/src/index.ts +1 -2
- package/src/listener.ts +8 -9
- package/src/socket-to-conn.ts +77 -81
- package/src/tcp.ts +21 -35
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { InvalidParametersError, TimeoutError } from '@libp2p/interface';
|
|
1
|
+
import { AbortError, InvalidParametersError, TimeoutError } from '@libp2p/interface';
|
|
2
2
|
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr';
|
|
3
|
-
import pDefer from 'p-defer';
|
|
4
|
-
import { raceEvent } from 'race-event';
|
|
5
3
|
import { duplex } from 'stream-to-it';
|
|
6
4
|
import { CLOSE_TIMEOUT, SOCKET_TIMEOUT } from './constants.js';
|
|
7
5
|
import { multiaddrToNetConfig } from './utils.js';
|
|
@@ -10,15 +8,12 @@ import { multiaddrToNetConfig } from './utils.js';
|
|
|
10
8
|
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
11
9
|
*/
|
|
12
10
|
export const toMultiaddrConnection = (socket, options) => {
|
|
13
|
-
let closePromise;
|
|
11
|
+
let closePromise = null;
|
|
14
12
|
const log = options.logger.forComponent('libp2p:tcp:socket');
|
|
15
|
-
const direction = options.direction;
|
|
16
13
|
const metrics = options.metrics;
|
|
17
14
|
const metricPrefix = options.metricPrefix ?? '';
|
|
18
15
|
const inactivityTimeout = options.socketInactivityTimeout ?? SOCKET_TIMEOUT;
|
|
19
16
|
const closeTimeout = options.socketCloseTimeout ?? CLOSE_TIMEOUT;
|
|
20
|
-
let timedout = false;
|
|
21
|
-
let errored = false;
|
|
22
17
|
// Check if we are connected on a unix path
|
|
23
18
|
if (options.listeningAddr?.getPath() != null) {
|
|
24
19
|
options.remoteAddr = options.listeningAddr;
|
|
@@ -26,16 +21,6 @@ export const toMultiaddrConnection = (socket, options) => {
|
|
|
26
21
|
if (options.remoteAddr?.getPath() != null) {
|
|
27
22
|
options.localAddr = options.remoteAddr;
|
|
28
23
|
}
|
|
29
|
-
// handle socket errors
|
|
30
|
-
socket.on('error', err => {
|
|
31
|
-
errored = true;
|
|
32
|
-
if (!timedout) {
|
|
33
|
-
log.error('%s socket error - %e', direction, err);
|
|
34
|
-
metrics?.increment({ [`${metricPrefix}error`]: true });
|
|
35
|
-
}
|
|
36
|
-
socket.destroy();
|
|
37
|
-
maConn.timeline.close = Date.now();
|
|
38
|
-
});
|
|
39
24
|
let remoteAddr;
|
|
40
25
|
if (options.remoteAddr != null) {
|
|
41
26
|
remoteAddr = options.remoteAddr;
|
|
@@ -53,32 +38,32 @@ export const toMultiaddrConnection = (socket, options) => {
|
|
|
53
38
|
const { sink, source } = duplex(socket);
|
|
54
39
|
// by default there is no timeout
|
|
55
40
|
// https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketsettimeouttimeout-callback
|
|
56
|
-
socket.setTimeout(inactivityTimeout)
|
|
57
|
-
|
|
58
|
-
timedout = true;
|
|
59
|
-
log('%s %s socket read timeout', direction, lOptsStr);
|
|
41
|
+
socket.setTimeout(inactivityTimeout, () => {
|
|
42
|
+
log('%s socket read timeout', lOptsStr);
|
|
60
43
|
metrics?.increment({ [`${metricPrefix}timeout`]: true });
|
|
44
|
+
// only destroy with an error if the remote has not sent the FIN message
|
|
45
|
+
let err;
|
|
46
|
+
if (socket.readable) {
|
|
47
|
+
err = new TimeoutError('Socket read timeout');
|
|
48
|
+
}
|
|
61
49
|
// if the socket times out due to inactivity we must manually close the connection
|
|
62
50
|
// https://nodejs.org/dist/latest-v16.x/docs/api/net.html#event-timeout
|
|
63
|
-
socket.destroy(
|
|
64
|
-
maConn.timeline.close = Date.now();
|
|
51
|
+
socket.destroy(err);
|
|
65
52
|
});
|
|
66
53
|
socket.once('close', () => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
log('%s %s socket close', direction, lOptsStr);
|
|
70
|
-
metrics?.increment({ [`${metricPrefix}close`]: true });
|
|
71
|
-
}
|
|
54
|
+
log('%s socket close', lOptsStr);
|
|
55
|
+
metrics?.increment({ [`${metricPrefix}close`]: true });
|
|
72
56
|
// In instances where `close` was not explicitly called,
|
|
73
57
|
// such as an iterable stream ending, ensure we have set the close
|
|
74
58
|
// timeline
|
|
75
|
-
|
|
76
|
-
|
|
59
|
+
if (maConn.timeline.close == null) {
|
|
60
|
+
maConn.timeline.close = Date.now();
|
|
61
|
+
}
|
|
77
62
|
});
|
|
78
63
|
socket.once('end', () => {
|
|
79
64
|
// the remote sent a FIN packet which means no more data will be sent
|
|
80
65
|
// https://nodejs.org/dist/latest-v16.x/docs/api/net.html#event-end
|
|
81
|
-
log('%s
|
|
66
|
+
log('%s socket end', lOptsStr);
|
|
82
67
|
metrics?.increment({ [`${metricPrefix}end`]: true });
|
|
83
68
|
});
|
|
84
69
|
const maConn = {
|
|
@@ -101,7 +86,7 @@ export const toMultiaddrConnection = (socket, options) => {
|
|
|
101
86
|
// If the source errored the socket will already have been destroyed by
|
|
102
87
|
// duplex(). If the socket errored it will already be
|
|
103
88
|
// destroyed. There's nothing to do here except log the error & return.
|
|
104
|
-
log.error('%s
|
|
89
|
+
log.error('%s error in sink', lOptsStr, err);
|
|
105
90
|
}
|
|
106
91
|
}
|
|
107
92
|
// we have finished writing, send the FIN message
|
|
@@ -113,71 +98,86 @@ export const toMultiaddrConnection = (socket, options) => {
|
|
|
113
98
|
timeline: { open: Date.now() },
|
|
114
99
|
async close(options = {}) {
|
|
115
100
|
if (socket.closed) {
|
|
116
|
-
log('
|
|
101
|
+
log('The %s socket is already closed', lOptsStr);
|
|
117
102
|
return;
|
|
118
103
|
}
|
|
119
104
|
if (socket.destroyed) {
|
|
120
|
-
log('
|
|
105
|
+
log('The %s socket is already destroyed', lOptsStr);
|
|
121
106
|
return;
|
|
122
107
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
108
|
+
const abortSignalListener = () => {
|
|
109
|
+
socket.destroy(new AbortError('Destroying socket after timeout'));
|
|
110
|
+
};
|
|
126
111
|
try {
|
|
127
|
-
closePromise
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const eventTarget = socketToEventTarget(socket);
|
|
132
|
-
// don't wait forever to close
|
|
133
|
-
const signal = options.signal ?? AbortSignal.timeout(closeTimeout);
|
|
134
|
-
// wait for any unsent data to be sent
|
|
135
|
-
if (socket.writableLength > 0) {
|
|
136
|
-
log('%s %s draining socket', direction, lOptsStr);
|
|
137
|
-
await raceEvent(eventTarget, 'drain', signal, {
|
|
138
|
-
errorEvent: 'error'
|
|
139
|
-
});
|
|
140
|
-
log('%s %s socket drained', direction, lOptsStr);
|
|
112
|
+
if (closePromise != null) {
|
|
113
|
+
log('The %s socket is already closing', lOptsStr);
|
|
114
|
+
await closePromise;
|
|
115
|
+
return;
|
|
141
116
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
117
|
+
if (options.signal == null) {
|
|
118
|
+
const signal = AbortSignal.timeout(closeTimeout);
|
|
119
|
+
options = {
|
|
120
|
+
...options,
|
|
121
|
+
signal
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
options.signal?.addEventListener('abort', abortSignalListener);
|
|
125
|
+
log('%s closing socket', lOptsStr);
|
|
126
|
+
closePromise = new Promise((resolve, reject) => {
|
|
127
|
+
socket.once('close', () => {
|
|
128
|
+
// socket completely closed
|
|
129
|
+
log('%s socket closed', lOptsStr);
|
|
130
|
+
resolve();
|
|
131
|
+
});
|
|
132
|
+
socket.once('error', (err) => {
|
|
133
|
+
log('%s socket error', lOptsStr, err);
|
|
134
|
+
if (!socket.destroyed) {
|
|
135
|
+
reject(err);
|
|
136
|
+
}
|
|
137
|
+
// if socket is destroyed, 'closed' event will be emitted later to resolve the promise
|
|
138
|
+
});
|
|
139
|
+
// shorten inactivity timeout
|
|
140
|
+
socket.setTimeout(closeTimeout);
|
|
141
|
+
// close writable end of the socket
|
|
142
|
+
socket.end();
|
|
143
|
+
if (socket.writableLength > 0) {
|
|
144
|
+
// there are outgoing bytes waiting to be sent
|
|
145
|
+
socket.once('drain', () => {
|
|
146
|
+
log('%s socket drained', lOptsStr);
|
|
147
|
+
// all bytes have been sent we can destroy the socket (maybe) before the timeout
|
|
148
|
+
socket.destroy();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
// nothing to send, destroy immediately, no need for the timeout
|
|
153
|
+
socket.destroy();
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
await closePromise;
|
|
149
157
|
}
|
|
150
158
|
catch (err) {
|
|
151
159
|
this.abort(err);
|
|
152
160
|
}
|
|
153
161
|
finally {
|
|
154
|
-
|
|
162
|
+
options.signal?.removeEventListener('abort', abortSignalListener);
|
|
155
163
|
}
|
|
156
164
|
},
|
|
157
165
|
abort: (err) => {
|
|
158
|
-
log('%s
|
|
166
|
+
log('%s socket abort due to error', lOptsStr, err);
|
|
159
167
|
// the abortSignalListener may already destroyed the socket with an error
|
|
160
|
-
socket.
|
|
168
|
+
if (!socket.destroyed) {
|
|
169
|
+
socket.destroy(err);
|
|
170
|
+
}
|
|
161
171
|
// closing a socket is always asynchronous (must wait for "close" event)
|
|
162
172
|
// but the tests expect this to be a synchronous operation so we have to
|
|
163
173
|
// set the close time here. the tests should be refactored to reflect
|
|
164
174
|
// reality.
|
|
165
|
-
maConn.timeline.close
|
|
175
|
+
if (maConn.timeline.close == null) {
|
|
176
|
+
maConn.timeline.close = Date.now();
|
|
177
|
+
}
|
|
166
178
|
},
|
|
167
179
|
log
|
|
168
180
|
};
|
|
169
181
|
return maConn;
|
|
170
182
|
};
|
|
171
|
-
function socketToEventTarget(obj) {
|
|
172
|
-
const eventTarget = {
|
|
173
|
-
addEventListener: (type, cb) => {
|
|
174
|
-
obj.addListener(type, cb);
|
|
175
|
-
},
|
|
176
|
-
removeEventListener: (type, cb) => {
|
|
177
|
-
obj.removeListener(type, cb);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
// @ts-expect-error partial implementation
|
|
181
|
-
return eventTarget;
|
|
182
|
-
}
|
|
183
183
|
//# sourceMappingURL=socket-to-conn.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket-to-conn.js","sourceRoot":"","sources":["../../src/socket-to-conn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"socket-to-conn.js","sourceRoot":"","sources":["../../src/socket-to-conn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACpF,OAAO,EAAE,iBAAiB,IAAI,WAAW,EAAE,MAAM,oCAAoC,CAAA;AACrF,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAgBjD;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAA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AI,CAAE,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC;IA8BlE,QAAQ,CAAE,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;IAsExE;;;;OAIG;IACH,cAAc,CAAE,OAAO,EAAE,wBAAwB,GAAG,QAAQ;IAc5D;;OAEG;IACH,YAAY,CAAE,UAAU,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;IAgBnD;;OAEG;IACH,UAAU,CAAE,UAAU,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;CAGlD"}
|
package/dist/src/tcp.js
CHANGED
|
@@ -45,11 +45,7 @@ export class TCP {
|
|
|
45
45
|
this.components = components;
|
|
46
46
|
if (components.metrics != null) {
|
|
47
47
|
this.metrics = {
|
|
48
|
-
|
|
49
|
-
label: 'event',
|
|
50
|
-
help: 'Total count of TCP dialer events by type'
|
|
51
|
-
}),
|
|
52
|
-
errors: components.metrics.registerCounterGroup('libp2p_tcp_dialer_errors_total', {
|
|
48
|
+
dialerEvents: components.metrics.registerCounterGroup('libp2p_tcp_dialer_events_total', {
|
|
53
49
|
label: 'event',
|
|
54
50
|
help: 'Total count of TCP dialer events by type'
|
|
55
51
|
})
|
|
@@ -66,28 +62,22 @@ export class TCP {
|
|
|
66
62
|
options.noDelay = options.noDelay ?? true;
|
|
67
63
|
// options.signal destroys the socket before 'connect' event
|
|
68
64
|
const socket = await this._connect(ma, options);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
catch (err) {
|
|
81
|
-
this.metrics?.errors.increment({ outbound_to_connection: true });
|
|
82
|
-
socket.destroy(err);
|
|
83
|
-
throw err;
|
|
84
|
-
}
|
|
65
|
+
// Avoid uncaught errors caused by unstable connections
|
|
66
|
+
socket.on('error', err => {
|
|
67
|
+
this.log('socket error', err);
|
|
68
|
+
});
|
|
69
|
+
const maConn = toMultiaddrConnection(socket, {
|
|
70
|
+
remoteAddr: ma,
|
|
71
|
+
socketInactivityTimeout: this.opts.outboundSocketInactivityTimeout,
|
|
72
|
+
socketCloseTimeout: this.opts.socketCloseTimeout,
|
|
73
|
+
metrics: this.metrics?.dialerEvents,
|
|
74
|
+
logger: this.components.logger
|
|
75
|
+
});
|
|
85
76
|
try {
|
|
86
77
|
this.log('new outbound connection %s', maConn.remoteAddr);
|
|
87
78
|
return await options.upgrader.upgradeOutbound(maConn, options);
|
|
88
79
|
}
|
|
89
80
|
catch (err) {
|
|
90
|
-
this.metrics?.errors.increment({ outbound_upgrade: true });
|
|
91
81
|
this.log.error('error upgrading outbound connection', err);
|
|
92
82
|
maConn.abort(err);
|
|
93
83
|
throw err;
|
|
@@ -96,7 +86,6 @@ export class TCP {
|
|
|
96
86
|
async _connect(ma, options) {
|
|
97
87
|
options.signal?.throwIfAborted();
|
|
98
88
|
options.onProgress?.(new CustomProgressEvent('tcp:open-connection'));
|
|
99
|
-
let rawSocket;
|
|
100
89
|
return new Promise((resolve, reject) => {
|
|
101
90
|
const start = Date.now();
|
|
102
91
|
const cOpts = multiaddrToNetConfig(ma, {
|
|
@@ -104,29 +93,29 @@ export class TCP {
|
|
|
104
93
|
...options
|
|
105
94
|
});
|
|
106
95
|
this.log('dialing %a', ma);
|
|
107
|
-
rawSocket = net.connect(cOpts);
|
|
96
|
+
const rawSocket = net.connect(cOpts);
|
|
108
97
|
const onError = (err) => {
|
|
109
|
-
this.log.error('dial to %a errored - %e', ma, err);
|
|
110
98
|
const cOptsStr = cOpts.path ?? `${cOpts.host ?? ''}:${cOpts.port}`;
|
|
111
99
|
err.message = `connection error ${cOptsStr}: ${err.message}`;
|
|
112
|
-
this.metrics?.
|
|
100
|
+
this.metrics?.dialerEvents.increment({ error: true });
|
|
113
101
|
done(err);
|
|
114
102
|
};
|
|
115
103
|
const onTimeout = () => {
|
|
116
104
|
this.log('connection timeout %a', ma);
|
|
117
|
-
this.metrics?.
|
|
118
|
-
const err = new TimeoutError(`
|
|
105
|
+
this.metrics?.dialerEvents.increment({ timeout: true });
|
|
106
|
+
const err = new TimeoutError(`connection timeout after ${Date.now() - start}ms`);
|
|
119
107
|
// Note: this will result in onError() being called
|
|
120
108
|
rawSocket.emit('error', err);
|
|
121
109
|
};
|
|
122
110
|
const onConnect = () => {
|
|
123
111
|
this.log('connection opened %a', ma);
|
|
124
|
-
this.metrics?.
|
|
112
|
+
this.metrics?.dialerEvents.increment({ connect: true });
|
|
125
113
|
done();
|
|
126
114
|
};
|
|
127
115
|
const onAbort = () => {
|
|
128
116
|
this.log('connection aborted %a', ma);
|
|
129
|
-
this.metrics?.
|
|
117
|
+
this.metrics?.dialerEvents.increment({ abort: true });
|
|
118
|
+
rawSocket.destroy();
|
|
130
119
|
done(new AbortError());
|
|
131
120
|
};
|
|
132
121
|
const done = (err) => {
|
|
@@ -148,10 +137,6 @@ export class TCP {
|
|
|
148
137
|
if (options.signal != null) {
|
|
149
138
|
options.signal.addEventListener('abort', onAbort);
|
|
150
139
|
}
|
|
151
|
-
})
|
|
152
|
-
.catch(err => {
|
|
153
|
-
rawSocket?.destroy();
|
|
154
|
-
throw err;
|
|
155
140
|
});
|
|
156
141
|
}
|
|
157
142
|
/**
|
package/dist/src/tcp.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tcp.js","sourceRoot":"","sources":["../../src/tcp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAClG,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBA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,2BAA2B,EAAE,IAAI,CAAC,IAAI,CAAC,2BAA2B;YAClE,uBAAuB,EAAE,IAAI,CAAC,IAAI,CAAC,8BAA8B;YACjE,kBAAkB,EAAE,IAAI,CAAC,IAAI,CAAC,kBAAkB;YAChD,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,OAAO;YAChC,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC,MAAM;SAC/B,CAAC,CAAA;IACJ,CAAC;IAED;;OAEG;IACH,YAAY,CAAE,UAAuB;QACnC,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;QAElE,OAAO,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;YAC5B,IAAI,EAAE,CAAC,UAAU,EAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC3C,OAAO,KAAK,CAAA;YACd,CAAC;YAED,IAAI,EAAE,CAAC,UAAU,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;gBACxC,OAAO,IAAI,CAAA;YACb,CAAC;YAED,OAAO,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC,CAAA;QACxD,CAAC,CAAC,CAAA;IACJ,CAAC;IAED;;OAEG;IACH,UAAU,CAAE,UAAuB;QACjC,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;IACtC,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/tcp",
|
|
3
|
-
"version": "10.0.6-
|
|
3
|
+
"version": "10.0.6-d9c7e0f7e",
|
|
4
4
|
"description": "A TCP transport for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-tcp#readme",
|
|
@@ -60,22 +60,21 @@
|
|
|
60
60
|
"test:electron-main": "aegir test -t electron-main"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@libp2p/interface": "2.1.2-
|
|
64
|
-
"@libp2p/utils": "6.0.6-
|
|
63
|
+
"@libp2p/interface": "2.1.2-d9c7e0f7e",
|
|
64
|
+
"@libp2p/utils": "6.0.6-d9c7e0f7e",
|
|
65
65
|
"@multiformats/mafmt": "^12.1.6",
|
|
66
66
|
"@multiformats/multiaddr": "^12.2.3",
|
|
67
67
|
"@types/sinon": "^17.0.3",
|
|
68
|
-
"p-defer": "^4.0.1",
|
|
69
68
|
"progress-events": "^1.0.0",
|
|
70
|
-
"race-event": "^1.3.0",
|
|
71
69
|
"stream-to-it": "^1.0.1"
|
|
72
70
|
},
|
|
73
71
|
"devDependencies": {
|
|
74
|
-
"@libp2p/interface-compliance-tests": "6.1.4-
|
|
75
|
-
"@libp2p/logger": "5.1.0-
|
|
72
|
+
"@libp2p/interface-compliance-tests": "6.1.4-d9c7e0f7e",
|
|
73
|
+
"@libp2p/logger": "5.1.0-d9c7e0f7e",
|
|
76
74
|
"aegir": "^44.0.1",
|
|
77
75
|
"it-all": "^3.0.6",
|
|
78
76
|
"it-pipe": "^3.0.1",
|
|
77
|
+
"p-defer": "^4.0.1",
|
|
79
78
|
"sinon": "^18.0.0",
|
|
80
79
|
"uint8arrays": "^5.1.0",
|
|
81
80
|
"wherearewe": "^2.0.1"
|
package/src/constants.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -123,8 +123,7 @@ export interface TCPComponents {
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
export interface TCPMetrics {
|
|
126
|
-
|
|
127
|
-
errors: CounterGroup<'outbound_to_connection' | 'outbound_upgrade'>
|
|
126
|
+
dialerEvents: CounterGroup<'error' | 'timeout' | 'connect' | 'abort'>
|
|
128
127
|
}
|
|
129
128
|
|
|
130
129
|
export function tcp (init: TCPOptions = {}): (components: TCPComponents) => Transport {
|
package/src/listener.ts
CHANGED
|
@@ -163,19 +163,20 @@ export class TCPListener extends TypedEventEmitter<ListenerEvents> implements Li
|
|
|
163
163
|
this.safeDispatchEvent('close')
|
|
164
164
|
}
|
|
165
165
|
})
|
|
166
|
-
.on('drop', () => {
|
|
167
|
-
this.metrics?.events.increment({ [`${this.addr} drop`]: true })
|
|
168
|
-
})
|
|
169
166
|
}
|
|
170
167
|
|
|
171
168
|
private onSocket (socket: net.Socket): void {
|
|
172
|
-
this.metrics?.events.increment({ [`${this.addr} connection`]: true })
|
|
173
|
-
|
|
174
169
|
if (this.status.code !== TCPListenerStatusCode.ACTIVE) {
|
|
175
170
|
socket.destroy()
|
|
176
171
|
throw new NotStartedError('Server is not listening yet')
|
|
177
172
|
}
|
|
178
173
|
|
|
174
|
+
// Avoid uncaught errors caused by unstable connections
|
|
175
|
+
socket.on('error', err => {
|
|
176
|
+
this.log('socket error', err)
|
|
177
|
+
this.metrics?.events.increment({ [`${this.addr} error`]: true })
|
|
178
|
+
})
|
|
179
|
+
|
|
179
180
|
let maConn: MultiaddrConnection
|
|
180
181
|
try {
|
|
181
182
|
maConn = toMultiaddrConnection(socket, {
|
|
@@ -184,13 +185,11 @@ export class TCPListener extends TypedEventEmitter<ListenerEvents> implements Li
|
|
|
184
185
|
socketCloseTimeout: this.context.socketCloseTimeout,
|
|
185
186
|
metrics: this.metrics?.events,
|
|
186
187
|
metricPrefix: `${this.addr} `,
|
|
187
|
-
logger: this.context.logger
|
|
188
|
-
direction: 'inbound'
|
|
188
|
+
logger: this.context.logger
|
|
189
189
|
})
|
|
190
|
-
} catch (err
|
|
190
|
+
} catch (err) {
|
|
191
191
|
this.log.error('inbound connection failed', err)
|
|
192
192
|
this.metrics?.errors.increment({ [`${this.addr} inbound_to_connection`]: true })
|
|
193
|
-
socket.destroy()
|
|
194
193
|
return
|
|
195
194
|
}
|
|
196
195
|
|