@libp2p/ping 0.0.0 → 1.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 +1 -1
- package/dist/index.min.js +10 -10
- 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 +3 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/ping.d.ts +3 -5
- package/dist/src/ping.d.ts.map +1 -1
- package/dist/src/ping.js +10 -9
- package/dist/src/ping.js.map +1 -1
- package/dist/typedoc-urls.json +1 -0
- package/package.json +12 -8
- package/src/constants.ts +1 -1
- package/src/index.ts +4 -4
- package/src/ping.ts +11 -14
package/dist/src/constants.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,qBAAqB,CAAA;AAC/C,eAAO,MAAM,WAAW,KAAK,CAAA;AAC7B,eAAO,MAAM,gBAAgB,UAAU,CAAA;AACvC,eAAO,MAAM,aAAa,SAAS,CAAA;AACnC,eAAO,MAAM,eAAe,SAAS,CAAA;AACrC,eAAO,MAAM,OAAO,QAAQ,CAAA;AAQ5B,eAAO,MAAM,mBAAmB,IAAI,CAAA;AACpC,eAAO,MAAM,oBAAoB,IAAI,CAAA;AAErC,eAAO,MAAM,kBAAkB,uBAAuB,CAAA"}
|
package/dist/src/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,kBAAkB,CAAA;AAC/C,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,CAAA;AAC7B,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAAO,CAAA;AACvC,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAA;AACnC,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAA;AACrC,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,CAAA;AAE5B,+FAA+F;AAC/F,+FAA+F;AAC/F,+FAA+F;AAC/F,yFAAyF;AACzF,yFAAyF;AACzF,2FAA2F;AAC3F,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAA;AACpC,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAA;AAErC,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAA"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -21,10 +21,8 @@
|
|
|
21
21
|
* console.info(rtt)
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
import type { AbortOptions, ComponentLogger } from '@libp2p/interface';
|
|
25
|
-
import type {
|
|
26
|
-
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager';
|
|
27
|
-
import type { Registrar } from '@libp2p/interface-internal/registrar';
|
|
24
|
+
import type { AbortOptions, ComponentLogger, PeerId } from '@libp2p/interface';
|
|
25
|
+
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal';
|
|
28
26
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
29
27
|
export interface PingService {
|
|
30
28
|
ping(peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<number>;
|
|
@@ -45,4 +43,5 @@ export interface PingServiceComponents {
|
|
|
45
43
|
logger: ComponentLogger;
|
|
46
44
|
}
|
|
47
45
|
export declare function ping(init?: PingServiceInit): (components: PingServiceComponents) => PingService;
|
|
46
|
+
export { PING_PROTOCOL } from './constants.js';
|
|
48
47
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CACtF;AAED,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,wBAAwB,CAAC,EAAE,OAAO,CAAA;IAElC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,wBAAgB,IAAI,CAAE,IAAI,GAAE,eAAoB,GAAG,CAAC,UAAU,EAAE,qBAAqB,KAAK,WAAW,CAEpG;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/src/index.js
CHANGED
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,WAAW,IAAI,gBAAgB,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,WAAW,IAAI,gBAAgB,EAAE,MAAM,WAAW,CAAA;AA2B3D,MAAM,UAAU,IAAI,CAAE,OAAwB,EAAE;IAC9C,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAC/D,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/src/ping.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import type { PingServiceComponents, PingServiceInit, PingService as PingServiceInterface } from './index.js';
|
|
2
|
-
import type { AbortOptions } from '@libp2p/interface';
|
|
3
|
-
import type {
|
|
4
|
-
import type { Startable } from '@libp2p/interface/startable';
|
|
5
|
-
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar';
|
|
2
|
+
import type { AbortOptions, PeerId, Startable } from '@libp2p/interface';
|
|
3
|
+
import type { IncomingStreamData } from '@libp2p/interface-internal';
|
|
6
4
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
7
5
|
export declare class PingService implements Startable, PingServiceInterface {
|
|
8
|
-
#private;
|
|
9
6
|
readonly protocol: string;
|
|
10
7
|
private readonly components;
|
|
11
8
|
private started;
|
|
@@ -13,6 +10,7 @@ export declare class PingService implements Startable, PingServiceInterface {
|
|
|
13
10
|
private readonly maxInboundStreams;
|
|
14
11
|
private readonly maxOutboundStreams;
|
|
15
12
|
private readonly runOnTransientConnection;
|
|
13
|
+
private readonly log;
|
|
16
14
|
constructor(components: PingServiceComponents, init?: PingServiceInit);
|
|
17
15
|
start(): Promise<void>;
|
|
18
16
|
stop(): Promise<void>;
|
package/dist/src/ping.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,eAAe,EAAE,WAAW,IAAI,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAC7G,OAAO,KAAK,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,eAAe,EAAE,WAAW,IAAI,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAC7G,OAAO,KAAK,EAAE,YAAY,EAAkB,MAAM,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AACxF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,qBAAa,WAAY,YAAW,SAAS,EAAE,oBAAoB;IACjE,SAAgB,QAAQ,EAAE,MAAM,CAAA;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAuB;IAClD,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,wBAAwB,CAAS;IAClD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,UAAU,EAAE,qBAAqB,EAAE,IAAI,GAAE,eAAoB;IAapE,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IASvB,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAK5B,SAAS,IAAK,OAAO;IAIrB;;OAEG;IACH,aAAa,CAAE,IAAI,EAAE,kBAAkB,GAAG,IAAI;IAiB9C;;OAEG;IACG,IAAI,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;CA+DjG"}
|
package/dist/src/ping.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto';
|
|
2
|
-
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface
|
|
2
|
+
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface';
|
|
3
3
|
import first from 'it-first';
|
|
4
4
|
import { pipe } from 'it-pipe';
|
|
5
5
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
|
|
@@ -12,16 +12,17 @@ export class PingService {
|
|
|
12
12
|
maxInboundStreams;
|
|
13
13
|
maxOutboundStreams;
|
|
14
14
|
runOnTransientConnection;
|
|
15
|
-
|
|
15
|
+
log;
|
|
16
16
|
constructor(components, init = {}) {
|
|
17
17
|
this.components = components;
|
|
18
|
-
this
|
|
18
|
+
this.log = components.logger.forComponent('libp2p:ping');
|
|
19
19
|
this.started = false;
|
|
20
20
|
this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`;
|
|
21
21
|
this.timeout = init.timeout ?? TIMEOUT;
|
|
22
22
|
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS;
|
|
23
23
|
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS;
|
|
24
24
|
this.runOnTransientConnection = init.runOnTransientConnection ?? true;
|
|
25
|
+
this.handleMessage = this.handleMessage.bind(this);
|
|
25
26
|
}
|
|
26
27
|
async start() {
|
|
27
28
|
await this.components.registrar.handle(this.protocol, this.handleMessage, {
|
|
@@ -42,23 +43,23 @@ export class PingService {
|
|
|
42
43
|
* A handler to register with Libp2p to process ping messages
|
|
43
44
|
*/
|
|
44
45
|
handleMessage(data) {
|
|
45
|
-
this
|
|
46
|
+
this.log('incoming ping from %p', data.connection.remotePeer);
|
|
46
47
|
const { stream } = data;
|
|
47
48
|
const start = Date.now();
|
|
48
49
|
void pipe(stream, stream)
|
|
49
50
|
.catch(err => {
|
|
50
|
-
this
|
|
51
|
+
this.log.error('incoming ping from %p failed with error', data.connection.remotePeer, err);
|
|
51
52
|
})
|
|
52
53
|
.finally(() => {
|
|
53
54
|
const ms = Date.now() - start;
|
|
54
|
-
this
|
|
55
|
+
this.log('incoming ping from %p complete in %dms', data.connection.remotePeer, ms);
|
|
55
56
|
});
|
|
56
57
|
}
|
|
57
58
|
/**
|
|
58
59
|
* Ping a given peer and wait for its response, getting the operation latency.
|
|
59
60
|
*/
|
|
60
61
|
async ping(peer, options = {}) {
|
|
61
|
-
this
|
|
62
|
+
this.log('pinging %p', peer);
|
|
62
63
|
const start = Date.now();
|
|
63
64
|
const data = randomBytes(PING_LENGTH);
|
|
64
65
|
const connection = await this.components.connectionManager.openConnection(peer, options);
|
|
@@ -89,11 +90,11 @@ export class PingService {
|
|
|
89
90
|
if (!uint8ArrayEquals(data, result.subarray())) {
|
|
90
91
|
throw new CodeError(`Received wrong ping ack after ${ms}ms`, ERR_WRONG_PING_ACK);
|
|
91
92
|
}
|
|
92
|
-
this
|
|
93
|
+
this.log('ping %p complete in %dms', connection.remotePeer, ms);
|
|
93
94
|
return ms;
|
|
94
95
|
}
|
|
95
96
|
catch (err) {
|
|
96
|
-
this
|
|
97
|
+
this.log.error('error while pinging %p', connection.remotePeer, err);
|
|
97
98
|
stream?.abort(err);
|
|
98
99
|
throw err;
|
|
99
100
|
}
|
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,SAAS,EAAE,WAAW,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,SAAS,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,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,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAMtK,MAAM,OAAO,WAAW;IACN,QAAQ,CAAQ;IACf,UAAU,CAAuB;IAC1C,OAAO,CAAS;IACP,OAAO,CAAQ;IACf,iBAAiB,CAAQ;IACzB,kBAAkB,CAAQ;IAC1B,wBAAwB,CAAS;IACjC,GAAG,CAAQ;IAE5B,YAAa,UAAiC,EAAE,OAAwB,EAAE;QACxE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,aAAa,CAAC,CAAA;QACxD,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,IAAI,IAAI,CAAC,cAAc,IAAI,eAAe,IAAI,aAAa,IAAI,gBAAgB,EAAE,CAAA;QACjG,IAAI,CAAC,OAAO,GAAG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C;CACF"}
|
package/dist/typedoc-urls.json
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
".:PingServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingServiceComponents.html",
|
|
6
6
|
"PingServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingServiceInit.html",
|
|
7
7
|
".:PingServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_ping.PingServiceInit.html",
|
|
8
|
+
"PING_PROTOCOL": "https://libp2p.github.io/js-libp2p/variables/_libp2p_ping.PING_PROTOCOL.html",
|
|
8
9
|
"ping": "https://libp2p.github.io/js-libp2p/functions/_libp2p_ping.ping.html",
|
|
9
10
|
".:ping": "https://libp2p.github.io/js-libp2p/functions/_libp2p_ping.ping.html"
|
|
10
11
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/ping",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Implementation of Ping Protocol",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
|
-
"homepage": "https://github.com/libp2p/js-libp2p/tree/
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/protocol-ping#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
13
|
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
14
18
|
"type": "module",
|
|
15
19
|
"types": "./dist/src/index.d.ts",
|
|
16
20
|
"files": [
|
|
@@ -45,17 +49,17 @@
|
|
|
45
49
|
"dep-check": "aegir dep-check"
|
|
46
50
|
},
|
|
47
51
|
"dependencies": {
|
|
48
|
-
"@libp2p/crypto": "^
|
|
49
|
-
"@libp2p/interface": "^0.
|
|
50
|
-
"@libp2p/interface-internal": "^0.1.
|
|
51
|
-
"@libp2p/
|
|
52
|
-
"@
|
|
53
|
-
"@multiformats/multiaddr": "^12.1.5",
|
|
52
|
+
"@libp2p/crypto": "^3.0.0",
|
|
53
|
+
"@libp2p/interface": "^1.0.0",
|
|
54
|
+
"@libp2p/interface-internal": "^0.1.10",
|
|
55
|
+
"@libp2p/peer-id-factory": "^3.0.9",
|
|
56
|
+
"@multiformats/multiaddr": "^12.1.10",
|
|
54
57
|
"it-first": "^3.0.3",
|
|
55
58
|
"it-pipe": "^3.0.1",
|
|
56
59
|
"uint8arrays": "^4.0.6"
|
|
57
60
|
},
|
|
58
61
|
"devDependencies": {
|
|
62
|
+
"@libp2p/logger": "^4.0.0",
|
|
59
63
|
"aegir": "^41.0.2",
|
|
60
64
|
"it-byte-stream": "^1.0.1",
|
|
61
65
|
"it-pair": "^2.0.6",
|
package/src/constants.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -23,10 +23,8 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import { PingService as PingServiceClass } from './ping.js'
|
|
26
|
-
import type { AbortOptions, ComponentLogger } from '@libp2p/interface'
|
|
27
|
-
import type {
|
|
28
|
-
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
29
|
-
import type { Registrar } from '@libp2p/interface-internal/registrar'
|
|
26
|
+
import type { AbortOptions, ComponentLogger, PeerId } from '@libp2p/interface'
|
|
27
|
+
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
30
28
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
31
29
|
|
|
32
30
|
export interface PingService {
|
|
@@ -54,3 +52,5 @@ export interface PingServiceComponents {
|
|
|
54
52
|
export function ping (init: PingServiceInit = {}): (components: PingServiceComponents) => PingService {
|
|
55
53
|
return (components) => new PingServiceClass(components, init)
|
|
56
54
|
}
|
|
55
|
+
|
|
56
|
+
export { PING_PROTOCOL } from './constants.js'
|
package/src/ping.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto'
|
|
2
|
-
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface
|
|
2
|
+
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface'
|
|
3
3
|
import first from 'it-first'
|
|
4
4
|
import { pipe } from 'it-pipe'
|
|
5
5
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
6
6
|
import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, ERR_WRONG_PING_ACK } from './constants.js'
|
|
7
7
|
import type { PingServiceComponents, PingServiceInit, PingService as PingServiceInterface } from './index.js'
|
|
8
|
-
import type { AbortOptions, Logger } from '@libp2p/interface'
|
|
9
|
-
import type {
|
|
10
|
-
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
11
|
-
import type { Startable } from '@libp2p/interface/startable'
|
|
12
|
-
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
|
8
|
+
import type { AbortOptions, Logger, Stream, PeerId, Startable } from '@libp2p/interface'
|
|
9
|
+
import type { IncomingStreamData } from '@libp2p/interface-internal'
|
|
13
10
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
14
11
|
|
|
15
12
|
export class PingService implements Startable, PingServiceInterface {
|
|
@@ -20,11 +17,11 @@ export class PingService implements Startable, PingServiceInterface {
|
|
|
20
17
|
private readonly maxInboundStreams: number
|
|
21
18
|
private readonly maxOutboundStreams: number
|
|
22
19
|
private readonly runOnTransientConnection: boolean
|
|
23
|
-
readonly
|
|
20
|
+
private readonly log: Logger
|
|
24
21
|
|
|
25
22
|
constructor (components: PingServiceComponents, init: PingServiceInit = {}) {
|
|
26
23
|
this.components = components
|
|
27
|
-
this
|
|
24
|
+
this.log = components.logger.forComponent('libp2p:ping')
|
|
28
25
|
this.started = false
|
|
29
26
|
this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
|
|
30
27
|
this.timeout = init.timeout ?? TIMEOUT
|
|
@@ -57,19 +54,19 @@ export class PingService implements Startable, PingServiceInterface {
|
|
|
57
54
|
* A handler to register with Libp2p to process ping messages
|
|
58
55
|
*/
|
|
59
56
|
handleMessage (data: IncomingStreamData): void {
|
|
60
|
-
this
|
|
57
|
+
this.log('incoming ping from %p', data.connection.remotePeer)
|
|
61
58
|
|
|
62
59
|
const { stream } = data
|
|
63
60
|
const start = Date.now()
|
|
64
61
|
|
|
65
62
|
void pipe(stream, stream)
|
|
66
63
|
.catch(err => {
|
|
67
|
-
this
|
|
64
|
+
this.log.error('incoming ping from %p failed with error', data.connection.remotePeer, err)
|
|
68
65
|
})
|
|
69
66
|
.finally(() => {
|
|
70
67
|
const ms = Date.now() - start
|
|
71
68
|
|
|
72
|
-
this
|
|
69
|
+
this.log('incoming ping from %p complete in %dms', data.connection.remotePeer, ms)
|
|
73
70
|
})
|
|
74
71
|
}
|
|
75
72
|
|
|
@@ -77,7 +74,7 @@ export class PingService implements Startable, PingServiceInterface {
|
|
|
77
74
|
* Ping a given peer and wait for its response, getting the operation latency.
|
|
78
75
|
*/
|
|
79
76
|
async ping (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<number> {
|
|
80
|
-
this
|
|
77
|
+
this.log('pinging %p', peer)
|
|
81
78
|
|
|
82
79
|
const start = Date.now()
|
|
83
80
|
const data = randomBytes(PING_LENGTH)
|
|
@@ -123,11 +120,11 @@ export class PingService implements Startable, PingServiceInterface {
|
|
|
123
120
|
throw new CodeError(`Received wrong ping ack after ${ms}ms`, ERR_WRONG_PING_ACK)
|
|
124
121
|
}
|
|
125
122
|
|
|
126
|
-
this
|
|
123
|
+
this.log('ping %p complete in %dms', connection.remotePeer, ms)
|
|
127
124
|
|
|
128
125
|
return ms
|
|
129
126
|
} catch (err: any) {
|
|
130
|
-
this
|
|
127
|
+
this.log.error('error while pinging %p', connection.remotePeer, err)
|
|
131
128
|
|
|
132
129
|
stream?.abort(err)
|
|
133
130
|
|