libp2p 0.45.5-c042b5bb → 0.45.6

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.
Files changed (42) hide show
  1. package/dist/index.min.js +22 -22
  2. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  3. package/dist/src/circuit-relay/transport/discovery.js +11 -4
  4. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  5. package/dist/src/circuit-relay/transport/reservation-store.d.ts +5 -0
  6. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  7. package/dist/src/circuit-relay/transport/reservation-store.js +14 -2
  8. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  9. package/dist/src/connection-manager/auto-dial.d.ts +3 -0
  10. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  11. package/dist/src/connection-manager/auto-dial.js +64 -27
  12. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  13. package/dist/src/connection-manager/constants.d.ts +4 -0
  14. package/dist/src/connection-manager/constants.d.ts.map +1 -1
  15. package/dist/src/connection-manager/constants.js +4 -0
  16. package/dist/src/connection-manager/constants.js.map +1 -1
  17. package/dist/src/connection-manager/index.d.ts +5 -0
  18. package/dist/src/connection-manager/index.d.ts.map +1 -1
  19. package/dist/src/connection-manager/index.js +11 -10
  20. package/dist/src/connection-manager/index.js.map +1 -1
  21. package/dist/src/upnp-nat/index.d.ts +1 -1
  22. package/dist/src/upnp-nat/index.d.ts.map +1 -1
  23. package/dist/src/upnp-nat/index.js +2 -2
  24. package/dist/src/upnp-nat/index.js.map +1 -1
  25. package/dist/src/utils/peer-job-queue.d.ts +33 -0
  26. package/dist/src/utils/peer-job-queue.d.ts.map +1 -0
  27. package/dist/src/utils/peer-job-queue.js +82 -0
  28. package/dist/src/utils/peer-job-queue.js.map +1 -0
  29. package/dist/src/version.d.ts +1 -1
  30. package/dist/src/version.d.ts.map +1 -1
  31. package/dist/src/version.js +1 -1
  32. package/dist/src/version.js.map +1 -1
  33. package/dist/typedoc-urls.json +58 -0
  34. package/package.json +3 -3
  35. package/src/circuit-relay/transport/discovery.ts +11 -4
  36. package/src/circuit-relay/transport/reservation-store.ts +22 -3
  37. package/src/connection-manager/auto-dial.ts +75 -31
  38. package/src/connection-manager/constants.ts +5 -0
  39. package/src/connection-manager/index.ts +18 -12
  40. package/src/upnp-nat/index.ts +2 -2
  41. package/src/utils/peer-job-queue.ts +119 -0
  42. package/src/version.ts +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/upnp-nat/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,8BAA8B,CAAA;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,qCAAqC,CAAA;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,WAAW,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AACpC,OAAO,KAAK,GAAG,MAAM,eAAe,CAAA;AAMpC,MAAM,GAAG,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAA;AACrC,MAAM,WAAW,GAAG,IAAI,CAAA;AAExB,SAAS,QAAQ,CAAE,GAAG,GAAG,IAAI,EAAE,GAAG,GAAG,KAAK;IACxC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAA;AAC1D,CAAC;AA+CD,MAAM,OAAO;IACM,UAAU,CAAmB;IAC7B,eAAe,CAAS;IACxB,YAAY,CAAS;IACrB,WAAW,CAAQ;IACnB,GAAG,CAA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AI,CAAC,MAAM,GAAG,OAAO,CAAC;YACpB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC,CAAA;QAEF,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,IAAI,SAAS,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE;YACpC,OAAM;SACP;QAED,IAAI;YACF,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAA;YACzB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;SACxB;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;SACf;IACH,CAAC;CACF;AAED,MAAM,UAAU,cAAc,CAAE,OAAoB,EAAE;IACpD,OAAO,CAAC,UAA6B,EAAE,EAAE;QACvC,OAAO,IAAI,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACtC,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,33 @@
1
+ import PQueue from 'p-queue';
2
+ import type { PeerId } from '@libp2p/interface-peer-id';
3
+ import type { QueueAddOptions, Options, Queue } from 'p-queue';
4
+ interface RunFunction {
5
+ (): Promise<unknown>;
6
+ }
7
+ export interface PeerPriorityQueueOptions extends QueueAddOptions {
8
+ peerId: PeerId;
9
+ }
10
+ /**
11
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
12
+ * that adds support for filtering jobs by peer id
13
+ */
14
+ declare class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
15
+ #private;
16
+ enqueue(run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void;
17
+ dequeue(): RunFunction | undefined;
18
+ filter(options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[];
19
+ get size(): number;
20
+ }
21
+ /**
22
+ * Extends PQueue to add support for querying queued jobs by peer id
23
+ */
24
+ export declare class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
25
+ constructor(options?: Options<PeerPriorityQueue, PeerPriorityQueueOptions>);
26
+ /**
27
+ * Returns true if this queue has a job for the passed peer id that has not yet
28
+ * started to run
29
+ */
30
+ hasJob(peerId: PeerId): boolean;
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=peer-job-queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-job-queue.d.ts","sourceRoot":"","sources":["../../../src/utils/peer-job-queue.ts"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,SAAS,CAAA;AAE5B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAuB9D,UAAU,WAAW;IAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;CAAE;AAE9C,MAAM,WAAW,wBAAyB,SAAQ,eAAe;IAC/D,MAAM,EAAE,MAAM,CAAA;CACf;AAQD;;;GAGG;AACH,cAAM,iBAAkB,YAAW,KAAK,CAAC,WAAW,EAAE,wBAAwB,CAAC;;IAG7E,OAAO,CAAE,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAAG,IAAI;IA0B7E,OAAO,IAAK,WAAW,GAAG,SAAS;IAKnC,MAAM,CAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,GAAG,WAAW,EAAE;IAc5E,IAAI,IAAI,IAAK,MAAM,CAElB;CACF;AAED;;GAEG;AACH,qBAAa,YAAa,SAAQ,MAAM,CAAC,iBAAiB,EAAE,wBAAwB,CAAC;gBACtE,OAAO,GAAE,OAAO,CAAC,iBAAiB,EAAE,wBAAwB,CAAM;IAO/E;;;OAGG;IACH,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO;CAKjC"}
@@ -0,0 +1,82 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import { CodeError } from '@libp2p/interfaces/errors';
3
+ import PQueue from 'p-queue';
4
+ import { codes } from '../errors.js';
5
+ // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
6
+ // Used to compute insertion index to keep queue sorted after insertion
7
+ function lowerBound(array, value, comparator) {
8
+ let first = 0;
9
+ let count = array.length;
10
+ while (count > 0) {
11
+ const step = Math.trunc(count / 2);
12
+ let it = first + step;
13
+ if (comparator(array[it], value) <= 0) {
14
+ first = ++it;
15
+ count -= step + 1;
16
+ }
17
+ else {
18
+ count = step;
19
+ }
20
+ }
21
+ return first;
22
+ }
23
+ /**
24
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
25
+ * that adds support for filtering jobs by peer id
26
+ */
27
+ class PeerPriorityQueue {
28
+ #queue = [];
29
+ enqueue(run, options) {
30
+ const peerId = options?.peerId;
31
+ const priority = options?.priority ?? 0;
32
+ if (peerId == null) {
33
+ throw new CodeError('missing peer id', codes.ERR_INVALID_PARAMETERS);
34
+ }
35
+ const element = {
36
+ priority,
37
+ peerId,
38
+ run
39
+ };
40
+ if (this.size > 0 && this.#queue[this.size - 1].priority >= priority) {
41
+ this.#queue.push(element);
42
+ return;
43
+ }
44
+ const index = lowerBound(this.#queue, element, (a, b) => b.priority - a.priority);
45
+ this.#queue.splice(index, 0, element);
46
+ }
47
+ dequeue() {
48
+ const item = this.#queue.shift();
49
+ return item?.run;
50
+ }
51
+ filter(options) {
52
+ if (options.peerId != null) {
53
+ const peerId = options.peerId;
54
+ return this.#queue.filter((element) => peerId.equals(element.peerId)).map((element) => element.run);
55
+ }
56
+ return this.#queue.filter((element) => element.priority === options.priority).map((element) => element.run);
57
+ }
58
+ get size() {
59
+ return this.#queue.length;
60
+ }
61
+ }
62
+ /**
63
+ * Extends PQueue to add support for querying queued jobs by peer id
64
+ */
65
+ export class PeerJobQueue extends PQueue {
66
+ constructor(options = {}) {
67
+ super({
68
+ ...options,
69
+ queueClass: PeerPriorityQueue
70
+ });
71
+ }
72
+ /**
73
+ * Returns true if this queue has a job for the passed peer id that has not yet
74
+ * started to run
75
+ */
76
+ hasJob(peerId) {
77
+ return this.sizeBy({
78
+ peerId
79
+ }) > 0;
80
+ }
81
+ }
82
+ //# sourceMappingURL=peer-job-queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-job-queue.js","sourceRoot":"","sources":["../../../src/utils/peer-job-queue.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAIpC,mFAAmF;AACnF,uEAAuE;AACvE,SAAS,UAAU,CAAK,KAAmB,EAAE,KAAQ,EAAE,UAAkC;IACvF,IAAI,KAAK,GAAG,CAAC,CAAA;IACb,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,CAAA;IAExB,OAAO,KAAK,GAAG,CAAC,EAAE;QAChB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAA;QAClC,IAAI,EAAE,GAAG,KAAK,GAAG,IAAI,CAAA;QAErB,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAE,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE;YACtC,KAAK,GAAG,EAAE,EAAE,CAAA;YACZ,KAAK,IAAI,IAAI,GAAG,CAAC,CAAA;SAClB;aAAM;YACL,KAAK,GAAG,IAAI,CAAA;SACb;KACF;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAcD;;;GAGG;AACH,MAAM,iBAAiB;IACZ,MAAM,GAAc,EAAE,CAAA;IAE/B,OAAO,CAAE,GAA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,OAAO,CAAC,GAAG,CAAC,CAAA;SAChE;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CACvB,CAAC,OAA2C,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,QAAQ,CACvF,CAAC,GAAG,CAAC,CAAC,OAAuC,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACjE,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAA;IAC3B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,MAAmD;IACnF,YAAa,UAAgE,EAAE;QAC7E,KAAK,CAAC;YACJ,GAAG,OAAO;YACV,UAAU,EAAE,iBAAiB;SAC9B,CAAC,CAAA;IACJ,CAAC;IAED;;;OAGG;IACH,MAAM,CAAE,MAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC;YACjB,MAAM;SACP,CAAC,GAAG,CAAC,CAAA;IACR,CAAC;CACF"}
@@ -1,3 +1,3 @@
1
- export declare const version = "0.45.5-c042b5bb";
1
+ export declare const version = "0.45.6";
2
2
  export declare const name = "libp2p";
3
3
  //# sourceMappingURL=version.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,oBAAoB,CAAA;AACxC,eAAO,MAAM,IAAI,WAAW,CAAA"}
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,WAAW,CAAA;AAC/B,eAAO,MAAM,IAAI,WAAW,CAAA"}
@@ -1,3 +1,3 @@
1
- export const version = '0.45.5-c042b5bb';
1
+ export const version = '0.45.6';
2
2
  export const name = 'libp2p';
3
3
  //# sourceMappingURL=version.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,iBAAiB,CAAA;AACxC,MAAM,CAAC,MAAM,IAAI,GAAG,QAAQ,CAAA"}
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAA;AAC/B,MAAM,CAAC,MAAM,IAAI,GAAG,QAAQ,CAAA"}
@@ -0,0 +1,58 @@
1
+ {
2
+ "AutoNATComponents": "https://libp2p.github.io/js-libp2p/interfaces/autonat.AutoNATComponents.html",
3
+ "AutoNATServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/autonat.AutoNATServiceInit.html",
4
+ "autoNATService": "https://libp2p.github.io/js-libp2p/functions/autonat.autoNATService.html",
5
+ "codec": "https://libp2p.github.io/js-libp2p/functions/circuit_relay._internal_.Limit.codec.html",
6
+ "decode": "https://libp2p.github.io/js-libp2p/functions/circuit_relay._internal_.Limit.decode.html",
7
+ "encode": "https://libp2p.github.io/js-libp2p/functions/circuit_relay._internal_.Limit.encode.html",
8
+ "AdvertServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.AdvertServiceComponents.html",
9
+ "AdvertServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.AdvertServiceInit.html",
10
+ "CircuitRelayServerComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayServerComponents.html",
11
+ "CircuitRelayServerInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayServerInit.html",
12
+ "CircuitRelayTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayTransportComponents.html",
13
+ "CircuitRelayTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayTransportInit.html",
14
+ "Limit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.Limit-1.html",
15
+ "RelayDiscoveryComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.RelayDiscoveryComponents.html",
16
+ "RelayStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.RelayStoreInit.html",
17
+ "ReservationStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.ReservationStoreInit.html",
18
+ "CircuitRelayService": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay.CircuitRelayService.html",
19
+ "CircuitRelayServiceEvents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay.CircuitRelayServiceEvents.html",
20
+ "RelayReservation": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay.RelayReservation.html",
21
+ "circuitRelayServer": "https://libp2p.github.io/js-libp2p/functions/circuit_relay.circuitRelayServer.html",
22
+ "circuitRelayTransport": "https://libp2p.github.io/js-libp2p/functions/circuit_relay.circuitRelayTransport.html",
23
+ "FetchService": "https://libp2p.github.io/js-libp2p/interfaces/fetch.FetchService.html",
24
+ "FetchServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/fetch.FetchServiceComponents.html",
25
+ "FetchServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/fetch.FetchServiceInit.html",
26
+ "HandleMessageOptions": "https://libp2p.github.io/js-libp2p/interfaces/fetch.HandleMessageOptions.html",
27
+ "LookupFunction": "https://libp2p.github.io/js-libp2p/interfaces/fetch.LookupFunction.html",
28
+ "fetchService": "https://libp2p.github.io/js-libp2p/functions/fetch.fetchService-1.html",
29
+ "DefaultIdentifyService": "https://libp2p.github.io/js-libp2p/classes/identify._internal_.DefaultIdentifyService.html",
30
+ "Identify": "https://libp2p.github.io/js-libp2p/interfaces/identify._internal_.Identify-1.html",
31
+ "IdentifyServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/identify.IdentifyServiceComponents.html",
32
+ "IdentifyServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/identify.IdentifyServiceInit.html",
33
+ "Message": "https://libp2p.github.io/js-libp2p/variables/identify.Message.html",
34
+ "multicodecs": "https://libp2p.github.io/js-libp2p/variables/identify.multicodecs.html",
35
+ "identifyService": "https://libp2p.github.io/js-libp2p/functions/identify.identifyService.html",
36
+ "AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.AddressFilter.html",
37
+ "AddressManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.AddressManagerInit.html",
38
+ "Components": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.Components.html",
39
+ "ConnectionManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerInit.html",
40
+ "TransportManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.TransportManagerInit.html",
41
+ "Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/index.Libp2pInit.html",
42
+ "Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/index.Libp2pOptions.html",
43
+ "ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/index.ServiceFactoryMap.html",
44
+ "createLibp2p": "https://libp2p.github.io/js-libp2p/functions/index.createLibp2p.html",
45
+ "plaintext": "https://libp2p.github.io/js-libp2p/functions/insecure.plaintext.html",
46
+ "PingService": "https://libp2p.github.io/js-libp2p/interfaces/ping.PingService.html",
47
+ "PingServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/ping.PingServiceComponents.html",
48
+ "PingServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/ping.PingServiceInit.html",
49
+ "pingService": "https://libp2p.github.io/js-libp2p/functions/ping.pingService-1.html",
50
+ "ProtectorInit": "https://libp2p.github.io/js-libp2p/interfaces/pnet.ProtectorInit.html",
51
+ "generateKey": "https://libp2p.github.io/js-libp2p/functions/pnet.generateKey.html",
52
+ "preSharedKey": "https://libp2p.github.io/js-libp2p/functions/pnet.preSharedKey.html",
53
+ "UPnPNAT": "https://libp2p.github.io/js-libp2p/classes/upnp_nat._internal_.UPnPNAT.html",
54
+ "PMPOptions": "https://libp2p.github.io/js-libp2p/interfaces/upnp_nat.PMPOptions.html",
55
+ "UPnPNATComponents": "https://libp2p.github.io/js-libp2p/interfaces/upnp_nat.UPnPNATComponents.html",
56
+ "UPnPNATInit": "https://libp2p.github.io/js-libp2p/interfaces/upnp_nat.UPnPNATInit.html",
57
+ "uPnPNATService": "https://libp2p.github.io/js-libp2p/functions/upnp_nat.uPnPNATService.html"
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.45.5-c042b5bb",
3
+ "version": "0.45.6",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p#readme",
@@ -135,7 +135,7 @@
135
135
  "@libp2p/interface-peer-id": "^2.0.1",
136
136
  "@libp2p/interface-peer-info": "^1.0.3",
137
137
  "@libp2p/interface-peer-routing": "^1.1.0",
138
- "@libp2p/interface-peer-store": "^2.0.3",
138
+ "@libp2p/interface-peer-store": "^2.0.4",
139
139
  "@libp2p/interface-pubsub": "^4.0.0",
140
140
  "@libp2p/interface-record": "^2.0.6",
141
141
  "@libp2p/interface-registrar": "^2.0.3",
@@ -149,7 +149,7 @@
149
149
  "@libp2p/peer-id": "^2.0.0",
150
150
  "@libp2p/peer-id-factory": "^2.0.0",
151
151
  "@libp2p/peer-record": "^5.0.0",
152
- "@libp2p/peer-store": "^8.1.0",
152
+ "@libp2p/peer-store": "^8.2.0",
153
153
  "@libp2p/topology": "^4.0.1",
154
154
  "@libp2p/tracked-map": "^3.0.0",
155
155
  "@libp2p/utils": "^3.0.10",
@@ -89,10 +89,17 @@ export class RelayDiscovery extends EventEmitter<RelayDiscoveryEvents> implement
89
89
  */
90
90
  async discover (): Promise<void> {
91
91
  log('searching peer store for relays')
92
- const peers = (await this.peerStore.all())
93
- // filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
94
- .filter(({ protocols }) => protocols.includes(RELAY_V2_HOP_CODEC))
95
- .sort(() => Math.random() - 0.5)
92
+ const peers = (await this.peerStore.all({
93
+ filters: [
94
+ // filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
95
+ (peer) => {
96
+ return peer.protocols.includes(RELAY_V2_HOP_CODEC)
97
+ }
98
+ ],
99
+ orders: [
100
+ () => Math.random() < 0.5 ? 1 : -1
101
+ ]
102
+ }))
96
103
 
97
104
  for (const peer of peers) {
98
105
  log('found relay peer %p in content peer store', peer.id)
@@ -3,7 +3,7 @@ import { logger } from '@libp2p/logger'
3
3
  import { PeerMap } from '@libp2p/peer-collections'
4
4
  import { multiaddr } from '@multiformats/multiaddr'
5
5
  import { pbStream } from 'it-pb-stream'
6
- import PQueue from 'p-queue'
6
+ import { PeerJobQueue } from '../../utils/peer-job-queue.js'
7
7
  import { DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js'
8
8
  import { HopMessage, Status } from '../pb/index.js'
9
9
  import { getExpirationMilliseconds } from '../utils.js'
@@ -50,6 +50,11 @@ export interface RelayStoreInit {
50
50
  * How many discovered relays to allow in the reservation store
51
51
  */
52
52
  discoverRelays?: number
53
+
54
+ /**
55
+ * Limit the number of potential relays we will dial (default: 100)
56
+ */
57
+ maxReservationQueueLength?: number
53
58
  }
54
59
 
55
60
  export type RelayType = 'discovered' | 'configured'
@@ -71,9 +76,10 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
71
76
  private readonly transportManager: TransportManager
72
77
  private readonly peerStore: PeerStore
73
78
  private readonly events: EventEmitter<Libp2pEvents>
74
- private readonly reserveQueue: PQueue
79
+ private readonly reserveQueue: PeerJobQueue
75
80
  private readonly reservations: PeerMap<RelayEntry>
76
81
  private readonly maxDiscoveredRelays: number
82
+ private readonly maxReservationQueueLength: number
77
83
  private started: boolean
78
84
 
79
85
  constructor (components: RelayStoreComponents, init?: RelayStoreInit) {
@@ -86,10 +92,11 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
86
92
  this.events = components.events
87
93
  this.reservations = new PeerMap()
88
94
  this.maxDiscoveredRelays = init?.discoverRelays ?? 0
95
+ this.maxReservationQueueLength = init?.maxReservationQueueLength ?? 100
89
96
  this.started = false
90
97
 
91
98
  // ensure we don't listen on multiple relays simultaneously
92
- this.reserveQueue = new PQueue({
99
+ this.reserveQueue = new PeerJobQueue({
93
100
  concurrency: init?.reservationConcurrency ?? DEFAULT_RESERVATION_CONCURRENCY
94
101
  })
95
102
 
@@ -130,6 +137,16 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
130
137
  return
131
138
  }
132
139
 
140
+ if (this.reserveQueue.size > this.maxReservationQueueLength) {
141
+ log('not adding relay as the queue is full')
142
+ return
143
+ }
144
+
145
+ if (this.reserveQueue.hasJob(peerId)) {
146
+ log('relay peer is already in the reservation queue')
147
+ return
148
+ }
149
+
133
150
  log('add relay %p', peerId)
134
151
 
135
152
  await this.reserveQueue.add(async () => {
@@ -206,6 +223,8 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
206
223
  // if listening failed, remove the reservation
207
224
  this.reservations.delete(peerId)
208
225
  }
226
+ }, {
227
+ peerId
209
228
  })
210
229
  }
211
230
 
@@ -1,7 +1,7 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import { PeerMap, PeerSet } from '@libp2p/peer-collections'
3
- import PQueue from 'p-queue'
4
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js'
3
+ import { PeerJobQueue } from '../utils/peer-job-queue.js'
4
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js'
5
5
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
6
6
  import type { Libp2pEvents } from '@libp2p/interface-libp2p'
7
7
  import type { PeerStore } from '@libp2p/interface-peer-store'
@@ -12,6 +12,7 @@ const log = logger('libp2p:connection-manager:auto-dial')
12
12
 
13
13
  interface AutoDialInit {
14
14
  minConnections?: number
15
+ maxQueueLength?: number
15
16
  autoDialConcurrency?: number
16
17
  autoDialPriority?: number
17
18
  autoDialInterval?: number
@@ -25,6 +26,7 @@ interface AutoDialComponents {
25
26
 
26
27
  const defaultOptions = {
27
28
  minConnections: MIN_CONNECTIONS,
29
+ maxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH,
28
30
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
29
31
  autoDialPriority: AUTO_DIAL_PRIORITY,
30
32
  autoDialInterval: AUTO_DIAL_INTERVAL
@@ -33,12 +35,14 @@ const defaultOptions = {
33
35
  export class AutoDial implements Startable {
34
36
  private readonly connectionManager: ConnectionManager
35
37
  private readonly peerStore: PeerStore
36
- private readonly queue: PQueue
38
+ private readonly queue: PeerJobQueue
37
39
  private readonly minConnections: number
38
40
  private readonly autoDialPriority: number
39
41
  private readonly autoDialIntervalMs: number
42
+ private readonly autoDialMaxQueueLength: number
40
43
  private autoDialInterval?: ReturnType<typeof setInterval>
41
44
  private started: boolean
45
+ private running: boolean
42
46
 
43
47
  /**
44
48
  * Proactively tries to connect to known peers stored in the PeerStore.
@@ -51,8 +55,10 @@ export class AutoDial implements Startable {
51
55
  this.minConnections = init.minConnections ?? defaultOptions.minConnections
52
56
  this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority
53
57
  this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
58
+ this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
54
59
  this.started = false
55
- this.queue = new PQueue({
60
+ this.running = false
61
+ this.queue = new PeerJobQueue({
56
62
  concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
57
63
  })
58
64
  this.queue.addListener('error', (err) => {
@@ -73,7 +79,7 @@ export class AutoDial implements Startable {
73
79
  }
74
80
 
75
81
  start (): void {
76
- this.autoDialInterval = setInterval(() => {
82
+ this.autoDialInterval = setTimeout(() => {
77
83
  this.autoDial()
78
84
  .catch(err => {
79
85
  log.error('error while autodialing', err)
@@ -92,8 +98,9 @@ export class AutoDial implements Startable {
92
98
  stop (): void {
93
99
  // clear the queue
94
100
  this.queue.clear()
95
- clearInterval(this.autoDialInterval)
101
+ clearTimeout(this.autoDialInterval)
96
102
  this.started = false
103
+ this.running = false
97
104
  }
98
105
 
99
106
  async autoDial (): Promise<void> {
@@ -103,12 +110,6 @@ export class AutoDial implements Startable {
103
110
 
104
111
  const connections = this.connectionManager.getConnectionsMap()
105
112
  const numConnections = connections.size
106
- const dialQueue = new PeerSet(
107
- // @ts-expect-error boolean filter removes falsy peer IDs
108
- this.connectionManager.getDialQueue()
109
- .map(queue => queue.peerId)
110
- .filter(Boolean)
111
- )
112
113
 
113
114
  // Already has enough connections
114
115
  if (numConnections >= this.minConnections) {
@@ -116,34 +117,64 @@ export class AutoDial implements Startable {
116
117
  return
117
118
  }
118
119
 
119
- log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
120
+ if (this.queue.size > this.autoDialMaxQueueLength) {
121
+ log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
122
+ return
123
+ }
120
124
 
121
- // Sort peers on whether we know protocols or public keys for them
122
- const peers = await this.peerStore.all()
125
+ if (this.running) {
126
+ log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
127
+ return
128
+ }
123
129
 
124
- // Remove some peers
125
- const filteredPeers = peers.filter((peer) => {
126
- // Remove peers without addresses
127
- if (peer.addresses.length === 0) {
128
- return false
129
- }
130
+ this.running = true
130
131
 
131
- // remove peers we are already connected to
132
- if (connections.has(peer.id)) {
133
- return false
134
- }
132
+ log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
135
133
 
136
- // remove peers we are already dialling
137
- if (dialQueue.has(peer.id)) {
138
- return false
139
- }
134
+ const dialQueue = new PeerSet(
135
+ // @ts-expect-error boolean filter removes falsy peer IDs
136
+ this.connectionManager.getDialQueue()
137
+ .map(queue => queue.peerId)
138
+ .filter(Boolean)
139
+ )
140
140
 
141
- return true
141
+ // Sort peers on whether we know protocols or public keys for them
142
+ const peers = await this.peerStore.all({
143
+ filters: [
144
+ // Remove some peers
145
+ (peer) => {
146
+ // Remove peers without addresses
147
+ if (peer.addresses.length === 0) {
148
+ log.trace('not autodialing %p because they have no addresses')
149
+ return false
150
+ }
151
+
152
+ // remove peers we are already connected to
153
+ if (connections.has(peer.id)) {
154
+ log.trace('not autodialing %p because they are already connected')
155
+ return false
156
+ }
157
+
158
+ // remove peers we are already dialling
159
+ if (dialQueue.has(peer.id)) {
160
+ log.trace('not autodialing %p because they are already being dialed')
161
+ return false
162
+ }
163
+
164
+ // remove peers already in the autodial queue
165
+ if (this.queue.hasJob(peer.id)) {
166
+ log.trace('not autodialing %p because they are already being autodialed')
167
+ return false
168
+ }
169
+
170
+ return true
171
+ }
172
+ ]
142
173
  })
143
174
 
144
175
  // shuffle the peers - this is so peers with the same tag values will be
145
176
  // dialled in a different order each time
146
- const shuffledPeers = filteredPeers.sort(() => Math.random() > 0.5 ? 1 : -1)
177
+ const shuffledPeers = peers.sort(() => Math.random() > 0.5 ? 1 : -1)
147
178
 
148
179
  // Sort shuffled peers by tag value
149
180
  const peerValues = new PeerMap<number>()
@@ -192,9 +223,22 @@ export class AutoDial implements Startable {
192
223
  // @ts-expect-error needs adding to the ConnectionManager interface
193
224
  priority: this.autoDialPriority
194
225
  })
226
+ }, {
227
+ peerId: peer.id
195
228
  }).catch(err => {
196
229
  log.error('could not connect to peerStore stored peer', err)
197
230
  })
198
231
  }
232
+
233
+ this.running = false
234
+
235
+ if (this.started) {
236
+ this.autoDialInterval = setTimeout(() => {
237
+ this.autoDial()
238
+ .catch(err => {
239
+ log.error('error while autodialing', err)
240
+ })
241
+ }, this.autoDialIntervalMs)
242
+ }
199
243
  }
200
244
  }
@@ -49,6 +49,11 @@ export const AUTO_DIAL_CONCURRENCY = 25
49
49
  */
50
50
  export const AUTO_DIAL_PRIORITY = 0
51
51
 
52
+ /**
53
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialMaxQueueLength
54
+ */
55
+ export const AUTO_DIAL_MAX_QUEUE_LENGTH = 100
56
+
52
57
  /**
53
58
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
54
59
  */
@@ -10,7 +10,7 @@ import { codes } from '../errors.js'
10
10
  import { getPeerAddress } from '../get-peer.js'
11
11
  import { AutoDial } from './auto-dial.js'
12
12
  import { ConnectionPruner } from './connection-pruner.js'
13
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
13
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
14
14
  import { DialQueue } from './dial-queue.js'
15
15
  import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
16
16
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
@@ -18,7 +18,7 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
18
18
  import type { PendingDial, AddressSorter, Libp2pEvents } from '@libp2p/interface-libp2p'
19
19
  import type { Metrics } from '@libp2p/interface-metrics'
20
20
  import type { PeerId } from '@libp2p/interface-peer-id'
21
- import type { PeerStore } from '@libp2p/interface-peer-store'
21
+ import type { Peer, PeerStore } from '@libp2p/interface-peer-store'
22
22
  import type { TransportManager } from '@libp2p/interface-transport'
23
23
  import type { AbortOptions } from '@libp2p/interfaces'
24
24
  import type { EventEmitter } from '@libp2p/interfaces/events'
@@ -61,6 +61,12 @@ export interface ConnectionManagerInit {
61
61
  */
62
62
  autoDialPriority?: number
63
63
 
64
+ /**
65
+ * Limit the maximum number of peers to dial when trying to keep the number of
66
+ * open connections above `minConnections`. (default: 100)
67
+ */
68
+ autoDialMaxQueueLength?: number
69
+
64
70
  /**
65
71
  * Sort the known addresses of a peer before trying to dial, By default public
66
72
  * addresses will be dialled before private (e.g. loopback or LAN) addresses.
@@ -136,7 +142,8 @@ const defaultOptions = {
136
142
  inboundConnectionThreshold: INBOUND_CONNECTION_THRESHOLD,
137
143
  maxIncomingPendingConnections: MAX_INCOMING_PENDING_CONNECTIONS,
138
144
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
139
- autoDialPriority: AUTO_DIAL_PRIORITY
145
+ autoDialPriority: AUTO_DIAL_PRIORITY,
146
+ autoDialMaxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH
140
147
  }
141
148
 
142
149
  export interface DefaultConnectionManagerComponents {
@@ -217,7 +224,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
217
224
  }, {
218
225
  minConnections,
219
226
  autoDialConcurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
220
- autoDialPriority: init.autoDialPriority ?? defaultOptions.autoDialPriority
227
+ autoDialPriority: init.autoDialPriority ?? defaultOptions.autoDialPriority,
228
+ maxQueueLength: init.autoDialMaxQueueLength ?? defaultOptions.autoDialMaxQueueLength
221
229
  })
222
230
 
223
231
  // controls what happens when we have too many connections
@@ -344,17 +352,15 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
344
352
  // re-connect to any peers with the KEEP_ALIVE tag
345
353
  void Promise.resolve()
346
354
  .then(async () => {
347
- const keepAlivePeers: PeerId[] = []
348
-
349
- for (const peer of await this.peerStore.all()) {
350
- if (peer.tags.has(KEEP_ALIVE)) {
351
- keepAlivePeers.push(peer.id)
352
- }
353
- }
355
+ const keepAlivePeers: Peer[] = await this.peerStore.all({
356
+ filters: [(peer) => {
357
+ return peer.tags.has(KEEP_ALIVE)
358
+ }]
359
+ })
354
360
 
355
361
  await Promise.all(
356
362
  keepAlivePeers.map(async peer => {
357
- await this.openConnection(peer)
363
+ await this.openConnection(peer.id)
358
364
  .catch(err => {
359
365
  log.error(err)
360
366
  })
@@ -142,7 +142,7 @@ class UPnPNAT implements Startable {
142
142
  continue
143
143
  }
144
144
 
145
- const client = await this._getClient()
145
+ const client = this._getClient()
146
146
  const publicIp = this.externalAddress ?? await client.externalIp()
147
147
  const isPrivate = isPrivateIp(publicIp)
148
148
 
@@ -173,7 +173,7 @@ class UPnPNAT implements Startable {
173
173
  }
174
174
  }
175
175
 
176
- async _getClient (): Promise<NatAPI> {
176
+ _getClient (): NatAPI {
177
177
  if (this.client != null) {
178
178
  return this.client
179
179
  }