@libp2p/ping 2.0.36 → 2.0.37-6059227cb

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 +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,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,IAAI;IACnB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CACtF;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,IAAI,CAAA;AAE9B,MAAM,WAAW,QAAQ;IACvB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAEhC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAA;AAEtC,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,wBAAgB,IAAI,CAAE,IAAI,GAAE,QAAa,GAAG,CAAC,UAAU,EAAE,cAAc,KAAK,IAAI,CAE/E;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,IAAI;IACnB,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,QAAQ;IACvB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAEhC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;CACrC;AAED,wBAAgB,IAAI,CAAE,IAAI,GAAE,QAAa,GAAG,CAAC,UAAU,EAAE,cAAc,KAAK,IAAI,CAE/E;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
@@ -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;AAqC7C,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"}
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"}
@@ -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, IncomingStreamData } from '@libp2p/interface';
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;
@@ -10,7 +10,6 @@ export declare class Ping implements Startable, PingInterface {
10
10
  private readonly maxInboundStreams;
11
11
  private readonly maxOutboundStreams;
12
12
  private readonly runOnLimitedConnection;
13
- private readonly log;
14
13
  constructor(components: PingComponents, init?: PingInit);
15
14
  readonly [Symbol.toStringTag] = "@libp2p/ping";
16
15
  readonly [serviceCapabilities]: string[];
@@ -20,7 +19,7 @@ export declare class Ping implements Startable, PingInterface {
20
19
  /**
21
20
  * A handler to register with Libp2p to process ping messages
22
21
  */
23
- handleMessage(data: IncomingStreamData): void;
22
+ handlePing(stream: Stream, connection: Connection): Promise<void>;
24
23
  /**
25
24
  * Ping a given peer and wait for its response, getting the operation latency.
26
25
  */
@@ -1 +1 @@
1
- {"version":3,"file":"ping.d.ts","sourceRoot":"","sources":["../../src/ping.ts"],"names":[],"mappings":"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-1,7 +1,8 @@
1
1
  import { randomBytes } from '@libp2p/crypto';
2
- import { ProtocolError, TimeoutError, serviceCapabilities } from '@libp2p/interface';
3
- import { byteStream } from 'it-byte-stream';
4
- import { setMaxListeners } from 'main-event';
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 {
@@ -12,24 +13,22 @@ export class Ping {
12
13
  maxInboundStreams;
13
14
  maxOutboundStreams;
14
15
  runOnLimitedConnection;
15
- log;
16
16
  constructor(components, init = {}) {
17
17
  this.components = components;
18
- this.log = components.logger.forComponent('libp2p:ping');
19
18
  this.started = false;
20
19
  this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`;
21
20
  this.timeout = init.timeout ?? TIMEOUT;
22
21
  this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS;
23
22
  this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS;
24
23
  this.runOnLimitedConnection = init.runOnLimitedConnection ?? true;
25
- this.handleMessage = this.handleMessage.bind(this);
24
+ this.handlePing = this.handlePing.bind(this);
26
25
  }
27
26
  [Symbol.toStringTag] = '@libp2p/ping';
28
27
  [serviceCapabilities] = [
29
28
  '@libp2p/ping'
30
29
  ];
31
30
  async start() {
32
- await this.components.registrar.handle(this.protocol, this.handleMessage, {
31
+ await this.components.registrar.handle(this.protocol, this.handlePing, {
33
32
  maxInboundStreams: this.maxInboundStreams,
34
33
  maxOutboundStreams: this.maxOutboundStreams,
35
34
  runOnLimitedConnection: this.runOnLimitedConnection
@@ -46,97 +45,84 @@ export class Ping {
46
45
  /**
47
46
  * A handler to register with Libp2p to process ping messages
48
47
  */
49
- handleMessage(data) {
50
- this.log('incoming ping from %p', data.connection.remotePeer);
51
- const { stream } = data;
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
+ });
52
56
  const start = Date.now();
53
- const bytes = byteStream(stream);
54
- let pinged = false;
55
- Promise.resolve().then(async () => {
56
- while (true) {
57
- const signal = AbortSignal.timeout(this.timeout);
58
- setMaxListeners(Infinity, signal);
59
- signal.addEventListener('abort', () => {
60
- stream?.abort(new TimeoutError('ping timeout'));
61
- });
62
- const buf = await bytes.read({
63
- bytes: PING_LENGTH,
64
- signal
65
- });
66
- 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
+ ],
67
68
  signal
68
69
  });
69
- pinged = true;
70
- }
71
- })
72
- .catch(err => {
73
- // ignore the error if we've processed at least one ping, the remote
74
- // closed the stream and we handled or are handling the close cleanly
75
- if (pinged && err.name === 'UnexpectedEOFError' && stream.readStatus !== 'ready') {
76
- return;
70
+ log('stream drained');
77
71
  }
78
- this.log.error('incoming ping from %p failed with error - %e', data.connection.remotePeer, err);
79
- stream?.abort(err);
80
- })
81
- .finally(() => {
82
- const ms = Date.now() - start;
83
- this.log('incoming ping from %p complete in %dms', data.connection.remotePeer, ms);
84
- const signal = AbortSignal.timeout(this.timeout);
85
- setMaxListeners(Infinity, signal);
86
- stream.close({
87
- signal
88
- })
89
- .catch(err => {
90
- this.log.error('error closing ping stream from %p - %e', data.connection.remotePeer, err);
91
- stream?.abort(err);
92
- });
72
+ }
73
+ log('ping from %p complete in %dms', connection.remotePeer, Date.now() - start);
74
+ await stream.close({
75
+ signal
93
76
  });
94
77
  }
95
78
  /**
96
79
  * Ping a given peer and wait for its response, getting the operation latency.
97
80
  */
98
81
  async ping(peer, options = {}) {
99
- this.log('pinging %p', peer);
100
- const start = Date.now();
101
82
  const data = randomBytes(PING_LENGTH);
102
- const connection = await this.components.connectionManager.openConnection(peer, options);
103
- let stream;
104
- if (options.signal == null) {
105
- const signal = AbortSignal.timeout(this.timeout);
106
- options = {
107
- ...options,
108
- signal
109
- };
110
- }
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');
111
88
  try {
112
- stream = await connection.newStream(this.protocol, {
113
- ...options,
114
- runOnLimitedConnection: this.runOnLimitedConnection
115
- });
116
- const bytes = byteStream(stream);
117
- const [, result] = await Promise.all([
118
- bytes.write(data, options),
119
- bytes.read({
120
- ...options,
121
- bytes: PING_LENGTH
122
- })
123
- ]);
124
- const ms = Date.now() - start;
125
- if (!uint8ArrayEquals(data, result.subarray())) {
126
- throw new ProtocolError(`Received wrong ping ack after ${ms}ms`);
127
- }
128
- this.log('ping %p complete in %dms', connection.remotePeer, ms);
129
- return ms;
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);
130
118
  }
131
119
  catch (err) {
132
- this.log.error('error while pinging %p', connection.remotePeer, err);
120
+ log.error('error while pinging %o - %e', peer, err);
133
121
  stream?.abort(err);
134
122
  throw err;
135
123
  }
136
124
  finally {
137
- if (stream != null) {
138
- await stream.close(options);
139
- }
125
+ stream?.close();
140
126
  }
141
127
  }
142
128
  }
@@ -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,mBAAmB,CAAA;AACpF,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,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;IAC/B,GAAG,CAAQ;IAE5B,YAAa,UAA0B,EAAE,OAAiB,EAAE;QAC1D,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,aAAa,CAA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B,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": "2.0.36",
3
+ "version": "2.0.37-6059227cb",
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": "^5.1.7",
47
- "@libp2p/interface": "^2.10.5",
48
- "@libp2p/interface-internal": "^2.3.18",
49
- "@multiformats/multiaddr": "^12.4.4",
50
- "it-byte-stream": "^2.0.2",
51
- "main-event": "^1.0.1",
46
+ "@libp2p/crypto": "5.1.8-6059227cb",
47
+ "@libp2p/interface": "2.11.0-6059227cb",
48
+ "@libp2p/interface-internal": "2.3.19-6059227cb",
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/logger": "^5.1.21",
56
- "@libp2p/peer-id": "^5.1.8",
57
- "aegir": "^47.0.14",
58
- "it-pair": "^2.0.6",
59
- "p-defer": "^4.0.1",
56
+ "@libp2p/peer-id": "5.1.9-6059227cb",
57
+ "@libp2p/utils": "6.7.2-6059227cb",
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
@@ -23,7 +23,7 @@
23
23
  */
24
24
 
25
25
  import { Ping as PingClass } from './ping.js'
26
- import type { AbortOptions, ComponentLogger, PeerId } from '@libp2p/interface'
26
+ import type { AbortOptions, PeerId } from '@libp2p/interface'
27
27
  import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
28
28
  import type { Multiaddr } from '@multiformats/multiaddr'
29
29
 
@@ -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,15 +43,9 @@ 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
59
- logger: ComponentLogger
60
49
  }
61
50
 
62
51
  export function ping (init: PingInit = {}): (components: PingComponents) => Ping {
package/src/ping.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { randomBytes } from '@libp2p/crypto'
2
- import { ProtocolError, TimeoutError, serviceCapabilities } from '@libp2p/interface'
3
- import { byteStream } from 'it-byte-stream'
4
- import { setMaxListeners } from 'main-event'
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, Logger, Stream, PeerId, Startable, IncomingStreamData } from '@libp2p/interface'
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 {
@@ -16,11 +17,9 @@ export class Ping implements Startable, PingInterface {
16
17
  private readonly maxInboundStreams: number
17
18
  private readonly maxOutboundStreams: number
18
19
  private readonly runOnLimitedConnection: boolean
19
- private readonly log: Logger
20
20
 
21
21
  constructor (components: PingComponents, init: PingInit = {}) {
22
22
  this.components = components
23
- this.log = components.logger.forComponent('libp2p:ping')
24
23
  this.started = false
25
24
  this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
26
25
  this.timeout = init.timeout ?? TIMEOUT
@@ -28,7 +27,7 @@ export class Ping implements Startable, PingInterface {
28
27
  this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
29
28
  this.runOnLimitedConnection = init.runOnLimitedConnection ?? true
30
29
 
31
- this.handleMessage = this.handleMessage.bind(this)
30
+ this.handlePing = this.handlePing.bind(this)
32
31
  }
33
32
 
34
33
  readonly [Symbol.toStringTag] = '@libp2p/ping'
@@ -38,7 +37,7 @@ export class Ping implements Startable, PingInterface {
38
37
  ]
39
38
 
40
39
  async start (): Promise<void> {
41
- await this.components.registrar.handle(this.protocol, this.handleMessage, {
40
+ await this.components.registrar.handle(this.protocol, this.handlePing, {
42
41
  maxInboundStreams: this.maxInboundStreams,
43
42
  maxOutboundStreams: this.maxOutboundStreams,
44
43
  runOnLimitedConnection: this.runOnLimitedConnection
@@ -58,115 +57,95 @@ export class Ping implements Startable, PingInterface {
58
57
  /**
59
58
  * A handler to register with Libp2p to process ping messages
60
59
  */
61
- handleMessage (data: IncomingStreamData): void {
62
- this.log('incoming ping from %p', data.connection.remotePeer)
63
-
64
- const { stream } = data
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
- const buf = await bytes.read({
78
- bytes: PING_LENGTH,
79
- signal
80
- })
81
- await bytes.write(buf, {
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
- this.log.error('incoming 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
- this.log('incoming ping from %p complete in %dms', data.connection.remotePeer, ms)
86
+ }
101
87
 
102
- const signal = AbortSignal.timeout(this.timeout)
103
- setMaxListeners(Infinity, signal)
88
+ log('ping from %p complete in %dms', connection.remotePeer, Date.now() - start)
104
89
 
105
- stream.close({
106
- signal
107
- })
108
- .catch(err => {
109
- this.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
- this.log('pinging %p', peer)
120
-
121
- const start = Date.now()
122
99
  const data = randomBytes(PING_LENGTH)
123
- const connection = await this.components.connectionManager.openConnection(peer, options)
124
- let stream: Stream | undefined
125
-
126
- if (options.signal == null) {
127
- const signal = AbortSignal.timeout(this.timeout)
128
-
129
- options = {
130
- ...options,
131
- signal
132
- }
133
- }
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')
134
105
 
135
106
  try {
136
- stream = await connection.newStream(this.protocol, {
137
- ...options,
138
- runOnLimitedConnection: this.runOnLimitedConnection
139
- })
140
-
141
- const bytes = byteStream(stream)
142
-
143
- const [, result] = await Promise.all([
144
- bytes.write(data, options),
145
- bytes.read({
146
- ...options,
147
- bytes: PING_LENGTH
148
- })
149
- ])
150
-
151
- const ms = Date.now() - start
152
-
153
- if (!uint8ArrayEquals(data, result.subarray())) {
154
- throw new ProtocolError(`Received wrong ping ack after ${ms}ms`)
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
+ }
155
134
  }
156
135
 
157
- this.log('ping %p complete in %dms', connection.remotePeer, ms)
136
+ stream.addEventListener('message', onPong)
137
+ stream.send(data)
138
+ await stream.close(options)
158
139
 
159
- return ms
140
+ return await raceSignal(finished.promise, options.signal)
160
141
  } catch (err: any) {
161
- this.log.error('error while pinging %p', connection.remotePeer, err)
142
+ log.error('error while pinging %o - %e', peer, err)
162
143
 
163
144
  stream?.abort(err)
164
145
 
165
146
  throw err
166
147
  } finally {
167
- if (stream != null) {
168
- await stream.close(options)
169
- }
148
+ stream?.close()
170
149
  }
171
150
  }
172
151
  }
@@ -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
- }