@libp2p/ping 2.0.37 → 3.0.0-049bfa0fa
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/index.min.js.map +4 -4
- package/dist/src/index.d.ts +0 -8
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/ping.d.ts +2 -2
- package/dist/src/ping.d.ts.map +1 -1
- package/dist/src/ping.js +66 -79
- package/dist/src/ping.js.map +1 -1
- package/package.json +13 -12
- package/src/index.ts +0 -10
- package/src/ping.ts +71 -91
- package/dist/typedoc-urls.json +0 -15
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,WAAW,CAAA;AA0B7C,MAAM,UAAU,IAAI,CAAE,OAAiB,EAAE;IACvC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AACxD,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/src/ping.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { serviceCapabilities } from '@libp2p/interface';
|
|
2
2
|
import type { PingComponents, PingInit, Ping as PingInterface } from './index.js';
|
|
3
|
-
import type { AbortOptions, PeerId, Startable,
|
|
3
|
+
import type { AbortOptions, Stream, PeerId, Startable, Connection } from '@libp2p/interface';
|
|
4
4
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
5
5
|
export declare class Ping implements Startable, PingInterface {
|
|
6
6
|
readonly protocol: string;
|
|
@@ -19,7 +19,7 @@ export declare class Ping implements Startable, PingInterface {
|
|
|
19
19
|
/**
|
|
20
20
|
* A handler to register with Libp2p to process ping messages
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
handlePing(stream: Stream, connection: Connection): Promise<void>;
|
|
23
23
|
/**
|
|
24
24
|
* Ping a given peer and wait for its response, getting the operation latency.
|
|
25
25
|
*/
|
package/dist/src/ping.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,mBAAmB,EAAiC,MAAM,mBAAmB,CAAA;AAMrG,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,YAAY,CAAA;AACjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAsB,MAAM,mBAAmB,CAAA;AAChH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,qBAAa,IAAK,YAAW,SAAS,EAAE,aAAa;IACnD,SAAgB,QAAQ,EAAE,MAAM,CAAA;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAC3C,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;IAChC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAQ;IAC1C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAQ;IAC3C,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAS;gBAEnC,UAAU,EAAE,cAAc,EAAE,IAAI,GAAE,QAAa;IAY5D,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,kBAAiB;IAE9C,QAAQ,CAAC,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAEvC;IAEK,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IASvB,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAK5B,SAAS,IAAK,OAAO;IAIrB;;OAEG;IACG,UAAU,CAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAmCxE;;OAEG;IACG,IAAI,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;CAqDjG"}
|
package/dist/src/ping.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto';
|
|
2
|
-
import { ProtocolError,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { ProtocolError, serviceCapabilities, setMaxListeners, TimeoutError } from '@libp2p/interface';
|
|
3
|
+
import { pEvent } from 'p-event';
|
|
4
|
+
import { raceSignal } from 'race-signal';
|
|
5
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
5
6
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
|
|
6
7
|
import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS } from './constants.js';
|
|
7
8
|
export class Ping {
|
|
@@ -20,14 +21,14 @@ export class Ping {
|
|
|
20
21
|
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS;
|
|
21
22
|
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS;
|
|
22
23
|
this.runOnLimitedConnection = init.runOnLimitedConnection ?? true;
|
|
23
|
-
this.
|
|
24
|
+
this.handlePing = this.handlePing.bind(this);
|
|
24
25
|
}
|
|
25
26
|
[Symbol.toStringTag] = '@libp2p/ping';
|
|
26
27
|
[serviceCapabilities] = [
|
|
27
28
|
'@libp2p/ping'
|
|
28
29
|
];
|
|
29
30
|
async start() {
|
|
30
|
-
await this.components.registrar.handle(this.protocol, this.
|
|
31
|
+
await this.components.registrar.handle(this.protocol, this.handlePing, {
|
|
31
32
|
maxInboundStreams: this.maxInboundStreams,
|
|
32
33
|
maxOutboundStreams: this.maxOutboundStreams,
|
|
33
34
|
runOnLimitedConnection: this.runOnLimitedConnection
|
|
@@ -44,98 +45,84 @@ export class Ping {
|
|
|
44
45
|
/**
|
|
45
46
|
* A handler to register with Libp2p to process ping messages
|
|
46
47
|
*/
|
|
47
|
-
|
|
48
|
-
const log =
|
|
49
|
-
log.trace('ping from %p',
|
|
50
|
-
const
|
|
48
|
+
async handlePing(stream, connection) {
|
|
49
|
+
const log = stream.log.newScope('ping');
|
|
50
|
+
log.trace('ping from %p', connection.remotePeer);
|
|
51
|
+
const signal = AbortSignal.timeout(this.timeout);
|
|
52
|
+
setMaxListeners(Infinity, signal);
|
|
53
|
+
signal.addEventListener('abort', () => {
|
|
54
|
+
stream.abort(new TimeoutError('Ping timed out'));
|
|
55
|
+
});
|
|
51
56
|
const start = Date.now();
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
signal
|
|
64
|
-
});
|
|
65
|
-
await bytes.write(buf, {
|
|
57
|
+
for await (const buf of stream) {
|
|
58
|
+
if (stream.status !== 'open') {
|
|
59
|
+
log('stream status changed to %s', stream.status);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (!stream.send(buf)) {
|
|
63
|
+
log('waiting for stream to drain');
|
|
64
|
+
await pEvent(stream, 'drain', {
|
|
65
|
+
rejectionEvents: [
|
|
66
|
+
'close'
|
|
67
|
+
],
|
|
66
68
|
signal
|
|
67
69
|
});
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
.catch(err => {
|
|
72
|
-
// ignore the error if we've processed at least one ping, the remote
|
|
73
|
-
// closed the stream and we handled or are handling the close cleanly
|
|
74
|
-
if (pinged && err.name === 'UnexpectedEOFError' && stream.readStatus !== 'ready') {
|
|
75
|
-
return;
|
|
70
|
+
log('stream drained');
|
|
76
71
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const ms = Date.now() - start;
|
|
82
|
-
log('ping from %p complete in %dms', data.connection.remotePeer, ms);
|
|
83
|
-
const signal = AbortSignal.timeout(this.timeout);
|
|
84
|
-
setMaxListeners(Infinity, signal);
|
|
85
|
-
stream.close({
|
|
86
|
-
signal
|
|
87
|
-
})
|
|
88
|
-
.catch(err => {
|
|
89
|
-
log.error('error closing ping stream from %p - %e', data.connection.remotePeer, err);
|
|
90
|
-
stream?.abort(err);
|
|
91
|
-
});
|
|
72
|
+
}
|
|
73
|
+
log('ping from %p complete in %dms', connection.remotePeer, Date.now() - start);
|
|
74
|
+
await stream.close({
|
|
75
|
+
signal
|
|
92
76
|
});
|
|
93
77
|
}
|
|
94
78
|
/**
|
|
95
79
|
* Ping a given peer and wait for its response, getting the operation latency.
|
|
96
80
|
*/
|
|
97
81
|
async ping(peer, options = {}) {
|
|
98
|
-
const start = Date.now();
|
|
99
82
|
const data = randomBytes(PING_LENGTH);
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
options = {
|
|
106
|
-
...options,
|
|
107
|
-
signal
|
|
108
|
-
};
|
|
109
|
-
}
|
|
83
|
+
const stream = await this.components.connectionManager.openStream(peer, this.protocol, {
|
|
84
|
+
runOnLimitedConnection: this.runOnLimitedConnection,
|
|
85
|
+
...options
|
|
86
|
+
});
|
|
87
|
+
const log = stream.log.newScope('ping');
|
|
110
88
|
try {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
89
|
+
const start = Date.now();
|
|
90
|
+
const finished = Promise.withResolvers();
|
|
91
|
+
const received = new Uint8ArrayList();
|
|
92
|
+
const onPong = (evt) => {
|
|
93
|
+
received.append(evt.data);
|
|
94
|
+
if (received.byteLength === PING_LENGTH) {
|
|
95
|
+
stream.removeEventListener('message', onPong);
|
|
96
|
+
const rtt = Date.now() - start;
|
|
97
|
+
Promise.all([
|
|
98
|
+
stream.closeRead(options)
|
|
99
|
+
])
|
|
100
|
+
.then(() => {
|
|
101
|
+
if (!uint8ArrayEquals(data, received.subarray())) {
|
|
102
|
+
throw new ProtocolError(`Received wrong ping ack after ${rtt}ms`);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
finished.resolve(rtt);
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
.catch(err => {
|
|
109
|
+
stream.abort(err);
|
|
110
|
+
finished.reject(err);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
stream.addEventListener('message', onPong);
|
|
115
|
+
stream.send(data);
|
|
116
|
+
await stream.close(options);
|
|
117
|
+
return await raceSignal(finished.promise, options.signal);
|
|
129
118
|
}
|
|
130
119
|
catch (err) {
|
|
131
|
-
log.error('error while pinging %
|
|
120
|
+
log.error('error while pinging %o - %e', peer, err);
|
|
132
121
|
stream?.abort(err);
|
|
133
122
|
throw err;
|
|
134
123
|
}
|
|
135
124
|
finally {
|
|
136
|
-
|
|
137
|
-
await stream.close(options);
|
|
138
|
-
}
|
|
125
|
+
stream?.close();
|
|
139
126
|
}
|
|
140
127
|
}
|
|
141
128
|
}
|
package/dist/src/ping.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ping.js","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"ping.js","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrG,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,WAAW,EAAE,gBAAgB,EAAE,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAKlJ,MAAM,OAAO,IAAI;IACC,QAAQ,CAAQ;IACf,UAAU,CAAgB;IACnC,OAAO,CAAS;IACP,OAAO,CAAQ;IACf,iBAAiB,CAAQ;IACzB,kBAAkB,CAAQ;IAC1B,sBAAsB,CAAS;IAEhD,YAAa,UAA0B,EAAE,OAAiB,EAAE;QAC1D,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,IAAI,IAAI,CAAC,cAAc,IAAI,eAAe,IAAI,aAAa,IAAI,gBAAgB,EAAE,CAAA;QAC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C,CAAA;YAED,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;YAC1C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACjB,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YAE3B,OAAO,MAAM,UAAU,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;QAC3D,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,GAAG,CAAC,KAAK,CAAC,6BAA6B,EAAE,IAAI,EAAE,GAAG,CAAC,CAAA;YAEnD,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;YAElB,MAAM,GAAG,CAAA;QACX,CAAC;gBAAS,CAAC;YACT,MAAM,EAAE,KAAK,EAAE,CAAA;QACjB,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/ping",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-049bfa0fa",
|
|
4
4
|
"description": "Implementation of Ping Protocol",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/protocol-ping#readme",
|
|
@@ -43,20 +43,21 @@
|
|
|
43
43
|
"doc-check": "aegir doc-check"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@libp2p/crypto": "
|
|
47
|
-
"@libp2p/interface": "
|
|
48
|
-
"@libp2p/interface-internal": "
|
|
49
|
-
"@multiformats/multiaddr": "^
|
|
50
|
-
"
|
|
51
|
-
"
|
|
46
|
+
"@libp2p/crypto": "5.1.9-049bfa0fa",
|
|
47
|
+
"@libp2p/interface": "3.0.0-049bfa0fa",
|
|
48
|
+
"@libp2p/interface-internal": "3.0.0-049bfa0fa",
|
|
49
|
+
"@multiformats/multiaddr": "^13.0.1",
|
|
50
|
+
"p-event": "^6.0.1",
|
|
51
|
+
"race-signal": "^2.0.0",
|
|
52
|
+
"uint8arraylist": "^2.4.8",
|
|
52
53
|
"uint8arrays": "^5.1.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@libp2p/
|
|
56
|
-
"@libp2p/
|
|
57
|
-
"aegir": "^47.0.
|
|
58
|
-
"
|
|
59
|
-
"
|
|
56
|
+
"@libp2p/peer-id": "6.0.0-049bfa0fa",
|
|
57
|
+
"@libp2p/utils": "7.0.0-049bfa0fa",
|
|
58
|
+
"aegir": "^47.0.22",
|
|
59
|
+
"delay": "^6.0.0",
|
|
60
|
+
"sinon": "^21.0.0",
|
|
60
61
|
"sinon-ts": "^2.0.0"
|
|
61
62
|
},
|
|
62
63
|
"sideEffects": false
|
package/src/index.ts
CHANGED
|
@@ -31,11 +31,6 @@ export interface Ping {
|
|
|
31
31
|
ping(peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<number>
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use the `Ping` export instead
|
|
36
|
-
*/
|
|
37
|
-
export type PingService = Ping
|
|
38
|
-
|
|
39
34
|
export interface PingInit {
|
|
40
35
|
protocolPrefix?: string
|
|
41
36
|
maxInboundStreams?: number
|
|
@@ -48,11 +43,6 @@ export interface PingInit {
|
|
|
48
43
|
timeout?: number
|
|
49
44
|
}
|
|
50
45
|
|
|
51
|
-
/**
|
|
52
|
-
* @deprecated Use the `PingInit` export instead
|
|
53
|
-
*/
|
|
54
|
-
export type PingServiceInit = PingInit
|
|
55
|
-
|
|
56
46
|
export interface PingComponents {
|
|
57
47
|
registrar: Registrar
|
|
58
48
|
connectionManager: ConnectionManager
|
package/src/ping.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto'
|
|
2
|
-
import { ProtocolError,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { ProtocolError, serviceCapabilities, setMaxListeners, TimeoutError } from '@libp2p/interface'
|
|
3
|
+
import { pEvent } from 'p-event'
|
|
4
|
+
import { raceSignal } from 'race-signal'
|
|
5
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
6
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
6
7
|
import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS } from './constants.js'
|
|
7
8
|
import type { PingComponents, PingInit, Ping as PingInterface } from './index.js'
|
|
8
|
-
import type { AbortOptions, Stream, PeerId, Startable,
|
|
9
|
+
import type { AbortOptions, Stream, PeerId, Startable, Connection, StreamMessageEvent } from '@libp2p/interface'
|
|
9
10
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
10
11
|
|
|
11
12
|
export class Ping implements Startable, PingInterface {
|
|
@@ -26,7 +27,7 @@ export class Ping implements Startable, PingInterface {
|
|
|
26
27
|
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
|
|
27
28
|
this.runOnLimitedConnection = init.runOnLimitedConnection ?? true
|
|
28
29
|
|
|
29
|
-
this.
|
|
30
|
+
this.handlePing = this.handlePing.bind(this)
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
readonly [Symbol.toStringTag] = '@libp2p/ping'
|
|
@@ -36,7 +37,7 @@ export class Ping implements Startable, PingInterface {
|
|
|
36
37
|
]
|
|
37
38
|
|
|
38
39
|
async start (): Promise<void> {
|
|
39
|
-
await this.components.registrar.handle(this.protocol, this.
|
|
40
|
+
await this.components.registrar.handle(this.protocol, this.handlePing, {
|
|
40
41
|
maxInboundStreams: this.maxInboundStreams,
|
|
41
42
|
maxOutboundStreams: this.maxOutboundStreams,
|
|
42
43
|
runOnLimitedConnection: this.runOnLimitedConnection
|
|
@@ -56,116 +57,95 @@ export class Ping implements Startable, PingInterface {
|
|
|
56
57
|
/**
|
|
57
58
|
* A handler to register with Libp2p to process ping messages
|
|
58
59
|
*/
|
|
59
|
-
|
|
60
|
-
const log =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
async handlePing (stream: Stream, connection: Connection): Promise<void> {
|
|
61
|
+
const log = stream.log.newScope('ping')
|
|
62
|
+
log.trace('ping from %p', connection.remotePeer)
|
|
63
|
+
const signal = AbortSignal.timeout(this.timeout)
|
|
64
|
+
setMaxListeners(Infinity, signal)
|
|
65
|
+
signal.addEventListener('abort', () => {
|
|
66
|
+
stream.abort(new TimeoutError('Ping timed out'))
|
|
67
|
+
})
|
|
65
68
|
const start = Date.now()
|
|
66
|
-
const bytes = byteStream(stream)
|
|
67
|
-
let pinged = false
|
|
68
|
-
|
|
69
|
-
Promise.resolve().then(async () => {
|
|
70
|
-
while (true) {
|
|
71
|
-
const signal = AbortSignal.timeout(this.timeout)
|
|
72
|
-
setMaxListeners(Infinity, signal)
|
|
73
|
-
signal.addEventListener('abort', () => {
|
|
74
|
-
stream?.abort(new TimeoutError('ping timeout'))
|
|
75
|
-
})
|
|
76
69
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
70
|
+
for await (const buf of stream) {
|
|
71
|
+
if (stream.status !== 'open') {
|
|
72
|
+
log('stream status changed to %s', stream.status)
|
|
73
|
+
break
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!stream.send(buf)) {
|
|
77
|
+
log('waiting for stream to drain')
|
|
78
|
+
await pEvent(stream, 'drain', {
|
|
79
|
+
rejectionEvents: [
|
|
80
|
+
'close'
|
|
81
|
+
],
|
|
82
82
|
signal
|
|
83
83
|
})
|
|
84
|
-
|
|
85
|
-
pinged = true
|
|
84
|
+
log('stream drained')
|
|
86
85
|
}
|
|
87
|
-
}
|
|
88
|
-
.catch(err => {
|
|
89
|
-
// ignore the error if we've processed at least one ping, the remote
|
|
90
|
-
// closed the stream and we handled or are handling the close cleanly
|
|
91
|
-
if (pinged && err.name === 'UnexpectedEOFError' && stream.readStatus !== 'ready') {
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
log.error('ping from %p failed with error - %e', data.connection.remotePeer, err)
|
|
96
|
-
stream?.abort(err)
|
|
97
|
-
})
|
|
98
|
-
.finally(() => {
|
|
99
|
-
const ms = Date.now() - start
|
|
100
|
-
log('ping from %p complete in %dms', data.connection.remotePeer, ms)
|
|
86
|
+
}
|
|
101
87
|
|
|
102
|
-
|
|
103
|
-
setMaxListeners(Infinity, signal)
|
|
88
|
+
log('ping from %p complete in %dms', connection.remotePeer, Date.now() - start)
|
|
104
89
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
.catch(err => {
|
|
109
|
-
log.error('error closing ping stream from %p - %e', data.connection.remotePeer, err)
|
|
110
|
-
stream?.abort(err)
|
|
111
|
-
})
|
|
112
|
-
})
|
|
90
|
+
await stream.close({
|
|
91
|
+
signal
|
|
92
|
+
})
|
|
113
93
|
}
|
|
114
94
|
|
|
115
95
|
/**
|
|
116
96
|
* Ping a given peer and wait for its response, getting the operation latency.
|
|
117
97
|
*/
|
|
118
98
|
async ping (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<number> {
|
|
119
|
-
const start = Date.now()
|
|
120
99
|
const data = randomBytes(PING_LENGTH)
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const signal = AbortSignal.timeout(this.timeout)
|
|
127
|
-
|
|
128
|
-
options = {
|
|
129
|
-
...options,
|
|
130
|
-
signal
|
|
131
|
-
}
|
|
132
|
-
}
|
|
100
|
+
const stream = await this.components.connectionManager.openStream(peer, this.protocol, {
|
|
101
|
+
runOnLimitedConnection: this.runOnLimitedConnection,
|
|
102
|
+
...options
|
|
103
|
+
})
|
|
104
|
+
const log = stream.log.newScope('ping')
|
|
133
105
|
|
|
134
106
|
try {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
107
|
+
const start = Date.now()
|
|
108
|
+
const finished = Promise.withResolvers<number>()
|
|
109
|
+
const received = new Uint8ArrayList()
|
|
110
|
+
|
|
111
|
+
const onPong = (evt: StreamMessageEvent): void => {
|
|
112
|
+
received.append(evt.data)
|
|
113
|
+
|
|
114
|
+
if (received.byteLength === PING_LENGTH) {
|
|
115
|
+
stream.removeEventListener('message', onPong)
|
|
116
|
+
|
|
117
|
+
const rtt = Date.now() - start
|
|
118
|
+
|
|
119
|
+
Promise.all([
|
|
120
|
+
stream.closeRead(options)
|
|
121
|
+
])
|
|
122
|
+
.then(() => {
|
|
123
|
+
if (!uint8ArrayEquals(data, received.subarray())) {
|
|
124
|
+
throw new ProtocolError(`Received wrong ping ack after ${rtt}ms`)
|
|
125
|
+
} else {
|
|
126
|
+
finished.resolve(rtt)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
.catch(err => {
|
|
130
|
+
stream.abort(err)
|
|
131
|
+
finished.reject(err)
|
|
132
|
+
})
|
|
133
|
+
}
|
|
154
134
|
}
|
|
155
135
|
|
|
156
|
-
|
|
136
|
+
stream.addEventListener('message', onPong)
|
|
137
|
+
stream.send(data)
|
|
138
|
+
await stream.close(options)
|
|
157
139
|
|
|
158
|
-
return
|
|
140
|
+
return await raceSignal(finished.promise, options.signal)
|
|
159
141
|
} catch (err: any) {
|
|
160
|
-
log.error('error while pinging %
|
|
142
|
+
log.error('error while pinging %o - %e', peer, err)
|
|
161
143
|
|
|
162
144
|
stream?.abort(err)
|
|
163
145
|
|
|
164
146
|
throw err
|
|
165
147
|
} finally {
|
|
166
|
-
|
|
167
|
-
await stream.close(options)
|
|
168
|
-
}
|
|
148
|
+
stream?.close()
|
|
169
149
|
}
|
|
170
150
|
}
|
|
171
151
|
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"Ping": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.Ping.html",
|
|
3
|
-
".:Ping": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.Ping.html",
|
|
4
|
-
"PingComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingComponents.html",
|
|
5
|
-
".:PingComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingComponents.html",
|
|
6
|
-
"PingInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingInit.html",
|
|
7
|
-
".:PingInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingInit.html",
|
|
8
|
-
"PingService": "https://libp2p.github.io/js-libp2p/types/_libp2p_ping.PingService.html",
|
|
9
|
-
".:PingService": "https://libp2p.github.io/js-libp2p/types/_libp2p_ping.PingService.html",
|
|
10
|
-
"PingServiceInit": "https://libp2p.github.io/js-libp2p/types/_libp2p_ping.PingServiceInit.html",
|
|
11
|
-
".:PingServiceInit": "https://libp2p.github.io/js-libp2p/types/_libp2p_ping.PingServiceInit.html",
|
|
12
|
-
"PING_PROTOCOL": "https://libp2p.github.io/js-libp2p/variables/_libp2p_ping.PING_PROTOCOL.html",
|
|
13
|
-
"ping": "https://libp2p.github.io/js-libp2p/functions/_libp2p_ping.ping.html",
|
|
14
|
-
".:ping": "https://libp2p.github.io/js-libp2p/functions/_libp2p_ping.ping.html"
|
|
15
|
-
}
|