libp2p 0.36.1-902f10d → 0.36.2-64bfcee
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/LICENSE +3 -21
- package/README.md +13 -10
- package/dist/src/address-manager/index.d.ts +33 -52
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +81 -0
- package/dist/src/address-manager/index.js.map +1 -0
- package/dist/src/circuit/auto-relay.d.ts +24 -85
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +215 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +25 -52
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +138 -0
- package/dist/src/circuit/circuit/hop.js.map +1 -0
- package/dist/src/circuit/circuit/stop.d.ts +16 -8
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js +49 -0
- package/dist/src/circuit/circuit/stop.js.map +1 -0
- package/dist/src/circuit/circuit/stream-handler.d.ts +16 -35
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +60 -0
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -0
- package/dist/src/circuit/circuit/utils.d.ts +3 -6
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js +43 -0
- package/dist/src/circuit/circuit/utils.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +24 -6
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +26 -0
- package/dist/src/circuit/constants.js.map +1 -0
- package/dist/src/circuit/index.d.ts +41 -74
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +69 -0
- package/dist/src/circuit/index.js.map +1 -0
- package/dist/src/circuit/listener.d.ts +8 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +54 -0
- package/dist/src/circuit/listener.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +1 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -0
- package/dist/src/circuit/multicodec.js.map +1 -0
- package/dist/src/circuit/{protocol → pb}/index.d.ts +0 -0
- package/{src/circuit/protocol → dist/src/circuit/pb}/index.js +7 -9
- package/dist/src/circuit/transport.d.ts +18 -78
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +190 -0
- package/dist/src/circuit/transport.js.map +1 -0
- package/dist/src/circuit/utils.d.ts +5 -2
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +11 -0
- package/dist/src/circuit/utils.js.map +1 -0
- package/dist/src/config.d.ts +3 -85
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +93 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +105 -0
- package/dist/src/connection-manager/auto-dialler.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +79 -144
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +295 -0
- package/dist/src/connection-manager/index.js.map +1 -0
- package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +220 -0
- package/dist/src/connection-manager/latency-monitor.js.map +1 -0
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
- package/dist/src/constants.d.ts +22 -10
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +27 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +26 -81
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +99 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/content-routing/utils.d.ts +6 -49
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +42 -0
- package/dist/src/content-routing/utils.js.map +1 -0
- package/dist/src/dht/dht-content-routing.d.ts +10 -24
- package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-content-routing.js +32 -0
- package/dist/src/dht/dht-content-routing.js.map +1 -0
- package/dist/src/dht/dht-peer-routing.d.ts +9 -24
- package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-peer-routing.js +26 -0
- package/dist/src/dht/dht-peer-routing.js.map +1 -0
- package/dist/src/dialer/auto-dialer.d.ts +14 -0
- package/dist/src/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/dialer/auto-dialer.js +24 -0
- package/dist/src/dialer/auto-dialer.js.map +1 -0
- package/dist/src/dialer/dial-request.d.ts +18 -46
- package/dist/src/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/dialer/dial-request.js +83 -0
- package/dist/src/dialer/dial-request.js.map +1 -0
- package/dist/src/dialer/index.d.ts +41 -148
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/dialer/index.js +281 -0
- package/dist/src/dialer/index.js.map +1 -0
- package/dist/src/errors.d.ts +68 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +73 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/fetch/constants.d.ts +1 -1
- package/dist/src/fetch/constants.d.ts.map +1 -1
- package/dist/src/fetch/constants.js +3 -0
- package/dist/src/fetch/constants.js.map +1 -0
- package/dist/src/fetch/index.d.ts +30 -49
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +130 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
- package/{src/fetch → dist/src/fetch/pb}/proto.js +7 -9
- package/dist/src/get-peer.d.ts +4 -11
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +43 -0
- package/dist/src/get-peer.js.map +1 -0
- package/dist/src/identify/consts.d.ts +9 -9
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/consts.js +11 -0
- package/dist/src/identify/consts.js.map +1 -0
- package/dist/src/identify/index.d.ts +40 -96
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +322 -0
- package/dist/src/identify/index.js.map +1 -0
- package/{src/identify → dist/src/identify/pb}/message.d.ts +20 -5
- package/{src/identify → dist/src/identify/pb}/message.js +83 -42
- package/dist/src/index.d.ts +159 -493
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/insecure/index.d.ts +9 -0
- package/dist/src/insecure/index.d.ts.map +1 -0
- package/dist/src/insecure/index.js +78 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
- package/{src/insecure → dist/src/insecure/pb}/proto.js +39 -22
- package/dist/src/keychain/cms.d.ts +6 -14
- package/dist/src/keychain/cms.d.ts.map +1 -1
- package/dist/src/keychain/cms.js +129 -0
- package/dist/src/keychain/cms.js.map +1 -0
- package/dist/src/keychain/index.d.ts +62 -82
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +493 -0
- package/dist/src/keychain/index.js.map +1 -0
- package/dist/src/keychain/util.d.ts +3 -6
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/keychain/util.js +79 -0
- package/dist/src/keychain/util.js.map +1 -0
- package/dist/src/libp2p.d.ts +78 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +372 -0
- package/dist/src/libp2p.js.map +1 -0
- package/dist/src/metrics/index.d.ts +47 -104
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/index.js +231 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/metrics/moving-average.d.ts +14 -0
- package/dist/src/metrics/moving-average.d.ts.map +1 -0
- package/dist/src/metrics/moving-average.js +40 -0
- package/dist/src/metrics/moving-average.js.map +1 -0
- package/dist/src/metrics/stats.d.ts +45 -93
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +183 -0
- package/dist/src/metrics/stats.js.map +1 -0
- package/dist/src/nat-manager.d.ts +42 -99
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +114 -0
- package/dist/src/nat-manager.js.map +1 -0
- package/dist/src/peer-record-updater.d.ts +15 -0
- package/dist/src/peer-record-updater.d.ts.map +1 -0
- package/dist/src/peer-record-updater.js +44 -0
- package/dist/src/peer-record-updater.js.map +1 -0
- package/dist/src/peer-routing.d.ts +39 -84
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +108 -0
- package/dist/src/peer-routing.js.map +1 -0
- package/dist/src/ping/constants.d.ts +4 -4
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +5 -0
- package/dist/src/ping/constants.js.map +1 -0
- package/dist/src/ping/index.d.ts +17 -31
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +56 -0
- package/dist/src/ping/index.js.map +1 -0
- package/dist/src/pnet/crypto.d.ts +15 -5
- package/dist/src/pnet/crypto.d.ts.map +1 -1
- package/dist/src/pnet/crypto.js +60 -0
- package/dist/src/pnet/crypto.js.map +1 -0
- package/dist/src/pnet/errors.d.ts +5 -5
- package/dist/src/pnet/errors.d.ts.map +1 -1
- package/dist/src/pnet/errors.js +6 -0
- package/dist/src/pnet/errors.js.map +1 -0
- package/dist/src/pnet/index.d.ts +11 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +66 -0
- package/dist/src/pnet/index.js.map +1 -0
- package/dist/src/pnet/key-generator.d.ts +3 -7
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +25 -0
- package/dist/src/pnet/key-generator.js.map +1 -0
- package/dist/src/registrar.d.ts +27 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +159 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +34 -82
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +223 -0
- package/dist/src/transport-manager.js.map +1 -0
- package/dist/src/upgrader.d.ts +49 -129
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +402 -0
- package/dist/src/upgrader.js.map +1 -0
- package/dist/src/version.d.ts +3 -0
- package/dist/src/version.d.ts.map +1 -0
- package/dist/src/version.js +3 -0
- package/dist/src/version.js.map +1 -0
- package/package.json +148 -197
- package/src/address-manager/index.ts +129 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +284 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +87 -0
- package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
- package/src/circuit/constants.ts +31 -0
- package/src/circuit/index.ts +120 -0
- package/src/circuit/{listener.js → listener.ts} +21 -27
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.d.ts +0 -0
- package/src/circuit/pb/index.js +528 -0
- package/src/circuit/{protocol → pb}/index.proto +0 -0
- package/src/circuit/transport.ts +216 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +101 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/index.ts +422 -0
- package/src/connection-manager/latency-monitor.ts +319 -0
- package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
- package/src/constants.ts +31 -0
- package/src/content-routing/index.ts +143 -0
- package/src/content-routing/utils.ts +54 -0
- package/src/dht/dht-content-routing.ts +43 -0
- package/src/dht/dht-peer-routing.ts +35 -0
- package/src/dialer/auto-dialer.ts +40 -0
- package/src/dialer/{dial-request.js → dial-request.ts} +60 -51
- package/src/dialer/index.ts +374 -0
- package/src/errors.ts +71 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/{index.js → index.ts} +79 -70
- package/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
- package/src/fetch/pb/proto.js +331 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/{dist/src/identify → src/identify/pb}/message.d.ts +20 -5
- package/src/identify/pb/message.js +369 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/index.ts +236 -0
- package/src/insecure/index.ts +97 -0
- package/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
- package/src/insecure/pb/proto.js +388 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +588 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +509 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +243 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +185 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +83 -0
- package/src/pnet/README.md +2 -2
- package/src/pnet/crypto.ts +67 -0
- package/src/pnet/errors.ts +5 -0
- package/src/pnet/index.ts +95 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/registrar.ts +205 -0
- package/src/transport-manager.ts +279 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- package/dist/src/metrics/old-peers.d.ts +0 -3
- package/dist/src/metrics/old-peers.d.ts.map +0 -1
- package/dist/src/metrics/tracked-map.d.ts +0 -8
- package/dist/src/metrics/tracked-map.d.ts.map +0 -1
- package/dist/src/peer-store/address-book.d.ts +0 -67
- package/dist/src/peer-store/address-book.d.ts.map +0 -1
- package/dist/src/peer-store/index.d.ts +0 -58
- package/dist/src/peer-store/index.d.ts.map +0 -1
- package/dist/src/peer-store/key-book.d.ts +0 -40
- package/dist/src/peer-store/key-book.d.ts.map +0 -1
- package/dist/src/peer-store/metadata-book.d.ts +0 -58
- package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
- package/dist/src/peer-store/pb/peer.d.ts +0 -222
- package/dist/src/peer-store/proto-book.d.ts +0 -43
- package/dist/src/peer-store/proto-book.d.ts.map +0 -1
- package/dist/src/peer-store/store.d.ts +0 -73
- package/dist/src/peer-store/store.d.ts.map +0 -1
- package/dist/src/peer-store/types.d.ts +0 -202
- package/dist/src/peer-store/types.d.ts.map +0 -1
- package/dist/src/ping/util.d.ts +0 -5
- package/dist/src/ping/util.d.ts.map +0 -1
- package/dist/src/pubsub-adapter.d.ts +0 -22
- package/dist/src/pubsub-adapter.d.ts.map +0 -1
- package/dist/src/record/envelope/envelope.d.ts +0 -77
- package/dist/src/record/envelope/index.d.ts +0 -80
- package/dist/src/record/envelope/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/consts.d.ts +0 -4
- package/dist/src/record/peer-record/consts.d.ts.map +0 -1
- package/dist/src/record/peer-record/index.d.ts +0 -60
- package/dist/src/record/peer-record/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/peer-record.d.ts +0 -133
- package/dist/src/record/utils.d.ts +0 -12
- package/dist/src/record/utils.d.ts.map +0 -1
- package/dist/src/types.d.ts +0 -90
- package/dist/src/types.d.ts.map +0 -1
- package/src/address-manager/index.js +0 -96
- package/src/circuit/auto-relay.js +0 -302
- package/src/circuit/circuit/hop.js +0 -205
- package/src/circuit/circuit/stop.js +0 -81
- package/src/circuit/circuit/stream-handler.js +0 -94
- package/src/circuit/constants.js +0 -12
- package/src/circuit/index.js +0 -102
- package/src/circuit/multicodec.js +0 -5
- package/src/circuit/transport.js +0 -229
- package/src/circuit/utils.js +0 -17
- package/src/config.js +0 -114
- package/src/connection-manager/auto-dialler.js +0 -132
- package/src/connection-manager/index.js +0 -374
- package/src/connection-manager/latency-monitor.js +0 -264
- package/src/constants.js +0 -18
- package/src/content-routing/index.js +0 -163
- package/src/content-routing/utils.js +0 -89
- package/src/dht/dht-content-routing.js +0 -44
- package/src/dht/dht-peer-routing.js +0 -51
- package/src/dialer/index.js +0 -376
- package/src/errors.js +0 -66
- package/src/fetch/constants.js +0 -6
- package/src/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/index.js +0 -813
- package/src/insecure/plaintext.js +0 -95
- package/src/keychain/index.js +0 -561
- package/src/metrics/index.js +0 -290
- package/src/metrics/old-peers.js +0 -16
- package/src/metrics/stats.js +0 -270
- package/src/metrics/tracked-map.js +0 -94
- package/src/nat-manager.js +0 -197
- package/src/peer-routing.js +0 -176
- package/src/peer-store/README.md +0 -145
- package/src/peer-store/address-book.js +0 -382
- package/src/peer-store/index.js +0 -121
- package/src/peer-store/key-book.js +0 -141
- package/src/peer-store/metadata-book.js +0 -250
- package/src/peer-store/pb/peer.d.ts +0 -222
- package/src/peer-store/pb/peer.js +0 -643
- package/src/peer-store/pb/peer.proto +0 -31
- package/src/peer-store/proto-book.js +0 -237
- package/src/peer-store/store.js +0 -263
- package/src/peer-store/types.ts +0 -245
- package/src/ping/constants.js +0 -8
- package/src/ping/index.js +0 -84
- package/src/ping/util.js +0 -18
- package/src/pnet/crypto.js +0 -84
- package/src/pnet/errors.js +0 -7
- package/src/pnet/index.js +0 -86
- package/src/pnet/key-generator.js +0 -33
- package/src/pubsub-adapter.js +0 -61
- package/src/record/README.md +0 -130
- package/src/record/envelope/envelope.d.ts +0 -77
- package/src/record/envelope/envelope.js +0 -243
- package/src/record/envelope/envelope.proto +0 -19
- package/src/record/envelope/index.js +0 -183
- package/src/record/peer-record/consts.js +0 -14
- package/src/record/peer-record/index.js +0 -113
- package/src/record/peer-record/peer-record.d.ts +0 -133
- package/src/record/peer-record/peer-record.js +0 -367
- package/src/record/peer-record/peer-record.proto +0 -18
- package/src/record/utils.js +0 -25
- package/src/registrar.js +0 -127
- package/src/transport-manager.js +0 -269
- package/src/types.ts +0 -98
- package/src/upgrader.js +0 -492
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const mergeOptions = require('merge-options')
|
|
5
|
-
// @ts-ignore retimer does not have types
|
|
6
|
-
const retimer = require('retimer')
|
|
7
|
-
const all = require('it-all')
|
|
8
|
-
const { pipe } = require('it-pipe')
|
|
9
|
-
const filter = require('it-filter')
|
|
10
|
-
const sort = require('it-sort')
|
|
11
|
-
|
|
12
|
-
const log = Object.assign(debug('libp2p:connection-manager:auto-dialler'), {
|
|
13
|
-
error: debug('libp2p:connection-manager:auto-dialler:err')
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
const defaultOptions = {
|
|
17
|
-
enabled: true,
|
|
18
|
-
minConnections: 0,
|
|
19
|
-
autoDialInterval: 10000
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @typedef {import('../index')} Libp2p
|
|
24
|
-
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @typedef {Object} AutoDiallerOptions
|
|
29
|
-
* @property {boolean} [enabled = true] - Should preemptively guarantee connections are above the low watermark
|
|
30
|
-
* @property {number} [minConnections = 0] - The minimum number of connections to avoid pruning
|
|
31
|
-
* @property {number} [autoDialInterval = 10000] - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
class AutoDialler {
|
|
35
|
-
/**
|
|
36
|
-
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
37
|
-
* It will keep the number of connections below the upper limit and sort
|
|
38
|
-
* the peers to connect based on wether we know their keys and protocols.
|
|
39
|
-
*
|
|
40
|
-
* @class
|
|
41
|
-
* @param {Libp2p} libp2p
|
|
42
|
-
* @param {AutoDiallerOptions} options
|
|
43
|
-
*/
|
|
44
|
-
constructor (libp2p, options = {}) {
|
|
45
|
-
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
|
|
46
|
-
this._libp2p = libp2p
|
|
47
|
-
this._running = false
|
|
48
|
-
this._autoDialTimeout = null
|
|
49
|
-
this._autoDial = this._autoDial.bind(this)
|
|
50
|
-
|
|
51
|
-
log('options: %j', this._options)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Starts the auto dialer
|
|
56
|
-
*/
|
|
57
|
-
async start () {
|
|
58
|
-
if (!this._options.enabled) {
|
|
59
|
-
log('not enabled')
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this._running = true
|
|
64
|
-
this._autoDial().catch(err => {
|
|
65
|
-
log.error('could start autodial', err)
|
|
66
|
-
})
|
|
67
|
-
log('started')
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Stops the auto dialler
|
|
72
|
-
*/
|
|
73
|
-
async stop () {
|
|
74
|
-
if (!this._options.enabled) {
|
|
75
|
-
log('not enabled')
|
|
76
|
-
return
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
this._running = false
|
|
80
|
-
this._autoDialTimeout && this._autoDialTimeout.clear()
|
|
81
|
-
log('stopped')
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async _autoDial () {
|
|
85
|
-
const minConnections = this._options.minConnections
|
|
86
|
-
|
|
87
|
-
// Already has enough connections
|
|
88
|
-
if (this._libp2p.connections.size >= minConnections) {
|
|
89
|
-
this._autoDialTimeout = retimer(this._autoDial, this._options.autoDialInterval)
|
|
90
|
-
return
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Sort peers on whether we know protocols of public keys for them
|
|
94
|
-
// TODO: assuming the `peerStore.getPeers()` order is stable this will mean
|
|
95
|
-
// we keep trying to connect to the same peers?
|
|
96
|
-
const peers = await pipe(
|
|
97
|
-
this._libp2p.peerStore.getPeers(),
|
|
98
|
-
(source) => filter(source, (peer) => !peer.id.equals(this._libp2p.peerId)),
|
|
99
|
-
(source) => sort(source, (a, b) => {
|
|
100
|
-
if (b.protocols && b.protocols.length && (!a.protocols || !a.protocols.length)) {
|
|
101
|
-
return 1
|
|
102
|
-
} else if (b.id.pubKey && !a.id.pubKey) {
|
|
103
|
-
return 1
|
|
104
|
-
}
|
|
105
|
-
return -1
|
|
106
|
-
}),
|
|
107
|
-
(source) => all(source)
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
for (let i = 0; this._running && i < peers.length && this._libp2p.connections.size < minConnections; i++) {
|
|
111
|
-
const peer = peers[i]
|
|
112
|
-
|
|
113
|
-
if (!this._libp2p.connectionManager.get(peer.id)) {
|
|
114
|
-
log('connecting to a peerStore stored peer %s', peer.id.toB58String())
|
|
115
|
-
try {
|
|
116
|
-
await this._libp2p.dialer.connectToPeer(peer.id)
|
|
117
|
-
} catch (/** @type {any} */ err) {
|
|
118
|
-
log.error('could not connect to peerStore stored peer', err)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Connection Manager was stopped
|
|
124
|
-
if (!this._running) {
|
|
125
|
-
return
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
this._autoDialTimeout = retimer(this._autoDial, this._options.autoDialInterval)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
module.exports = AutoDialler
|
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const log = Object.assign(debug('libp2p:connection-manager'), {
|
|
5
|
-
error: debug('libp2p:connection-manager:err')
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
const errcode = require('err-code')
|
|
9
|
-
const mergeOptions = require('merge-options')
|
|
10
|
-
const LatencyMonitor = require('./latency-monitor')
|
|
11
|
-
// @ts-ignore retimer does not have types
|
|
12
|
-
const retimer = require('retimer')
|
|
13
|
-
|
|
14
|
-
const { EventEmitter } = require('events')
|
|
15
|
-
const trackedMap = require('../metrics/tracked-map')
|
|
16
|
-
const PeerId = require('peer-id')
|
|
17
|
-
|
|
18
|
-
const {
|
|
19
|
-
codes: { ERR_INVALID_PARAMETERS }
|
|
20
|
-
} = require('../errors')
|
|
21
|
-
|
|
22
|
-
const defaultOptions = {
|
|
23
|
-
maxConnections: Infinity,
|
|
24
|
-
minConnections: 0,
|
|
25
|
-
maxData: Infinity,
|
|
26
|
-
maxSentData: Infinity,
|
|
27
|
-
maxReceivedData: Infinity,
|
|
28
|
-
maxEventLoopDelay: Infinity,
|
|
29
|
-
pollInterval: 2000,
|
|
30
|
-
autoDialInterval: 10000,
|
|
31
|
-
movingAverageInterval: 60000,
|
|
32
|
-
defaultPeerValue: 1
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const METRICS_COMPONENT = 'connection-manager'
|
|
36
|
-
const METRICS_PEER_CONNECTIONS = 'peer-connections'
|
|
37
|
-
const METRICS_PEER_VALUES = 'peer-values'
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* @typedef {import('../')} Libp2p
|
|
41
|
-
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @typedef {Object} ConnectionManagerOptions
|
|
46
|
-
* @property {number} [maxConnections = Infinity] - The maximum number of connections allowed.
|
|
47
|
-
* @property {number} [minConnections = 0] - The minimum number of connections to avoid pruning.
|
|
48
|
-
* @property {number} [maxData = Infinity] - The max data (in and out), per average interval to allow.
|
|
49
|
-
* @property {number} [maxSentData = Infinity] - The max outgoing data, per average interval to allow.
|
|
50
|
-
* @property {number} [maxReceivedData = Infinity] - The max incoming data, per average interval to allow.
|
|
51
|
-
* @property {number} [maxEventLoopDelay = Infinity] - The upper limit the event loop can take to run.
|
|
52
|
-
* @property {number} [pollInterval = 2000] - How often, in milliseconds, metrics and latency should be checked.
|
|
53
|
-
* @property {number} [movingAverageInterval = 60000] - How often, in milliseconds, to compute averages.
|
|
54
|
-
* @property {number} [defaultPeerValue = 1] - The value of the peer.
|
|
55
|
-
* @property {boolean} [autoDial = true] - Should preemptively guarantee connections are above the low watermark.
|
|
56
|
-
* @property {number} [autoDialInterval = 10000] - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark.
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
|
|
62
|
-
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
|
|
63
|
-
*/
|
|
64
|
-
class ConnectionManager extends EventEmitter {
|
|
65
|
-
/**
|
|
66
|
-
* Responsible for managing known connections.
|
|
67
|
-
*
|
|
68
|
-
* @class
|
|
69
|
-
* @param {Libp2p} libp2p
|
|
70
|
-
* @param {ConnectionManagerOptions} options
|
|
71
|
-
*/
|
|
72
|
-
constructor (libp2p, options = {}) {
|
|
73
|
-
super()
|
|
74
|
-
|
|
75
|
-
this._libp2p = libp2p
|
|
76
|
-
this._peerId = libp2p.peerId.toB58String()
|
|
77
|
-
|
|
78
|
-
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
|
|
79
|
-
if (this._options.maxConnections < this._options.minConnections) {
|
|
80
|
-
throw errcode(new Error('Connection Manager maxConnections must be greater than minConnections'), ERR_INVALID_PARAMETERS)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
log('options: %j', this._options)
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Map of peer identifiers to their peer value for pruning connections.
|
|
87
|
-
*
|
|
88
|
-
* @type {Map<string, number>}
|
|
89
|
-
*/
|
|
90
|
-
this._peerValues = trackedMap({
|
|
91
|
-
component: METRICS_COMPONENT,
|
|
92
|
-
metric: METRICS_PEER_VALUES,
|
|
93
|
-
metrics: this._libp2p.metrics
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Map of connections per peer
|
|
98
|
-
*
|
|
99
|
-
* @type {Map<string, Connection[]>}
|
|
100
|
-
*/
|
|
101
|
-
this.connections = trackedMap({
|
|
102
|
-
component: METRICS_COMPONENT,
|
|
103
|
-
metric: METRICS_PEER_CONNECTIONS,
|
|
104
|
-
metrics: this._libp2p.metrics
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
this._started = false
|
|
108
|
-
this._timer = null
|
|
109
|
-
this._checkMetrics = this._checkMetrics.bind(this)
|
|
110
|
-
|
|
111
|
-
this._latencyMonitor = new LatencyMonitor({
|
|
112
|
-
latencyCheckIntervalMs: this._options.pollInterval,
|
|
113
|
-
dataEmitIntervalMs: this._options.pollInterval
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
// This emitter gets listened to a lot
|
|
117
|
-
this.setMaxListeners(Infinity)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Get current number of open connections.
|
|
122
|
-
*/
|
|
123
|
-
get size () {
|
|
124
|
-
return Array.from(this.connections.values())
|
|
125
|
-
.reduce((accumulator, value) => accumulator + value.length, 0)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
|
130
|
-
* only event loop and connection limits will be monitored.
|
|
131
|
-
*/
|
|
132
|
-
start () {
|
|
133
|
-
if (this._libp2p.metrics) {
|
|
134
|
-
this._timer = this._timer || retimer(this._checkMetrics, this._options.pollInterval)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// latency monitor
|
|
138
|
-
this._latencyMonitor.start()
|
|
139
|
-
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
|
|
140
|
-
this._latencyMonitor.on('data', this._onLatencyMeasure)
|
|
141
|
-
|
|
142
|
-
this._started = true
|
|
143
|
-
log('started')
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Stops the Connection Manager
|
|
148
|
-
*
|
|
149
|
-
* @async
|
|
150
|
-
*/
|
|
151
|
-
async stop () {
|
|
152
|
-
this._timer && this._timer.clear()
|
|
153
|
-
|
|
154
|
-
this._latencyMonitor.removeListener('data', this._onLatencyMeasure)
|
|
155
|
-
this._latencyMonitor.stop()
|
|
156
|
-
|
|
157
|
-
this._started = false
|
|
158
|
-
await this._close()
|
|
159
|
-
log('stopped')
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Cleans up the connections
|
|
164
|
-
*
|
|
165
|
-
* @async
|
|
166
|
-
*/
|
|
167
|
-
async _close () {
|
|
168
|
-
// Close all connections we're tracking
|
|
169
|
-
const tasks = []
|
|
170
|
-
for (const connectionList of this.connections.values()) {
|
|
171
|
-
for (const connection of connectionList) {
|
|
172
|
-
tasks.push(connection.close())
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
await Promise.all(tasks)
|
|
177
|
-
this.connections.clear()
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Sets the value of the given peer. Peers with lower values
|
|
182
|
-
* will be disconnected first.
|
|
183
|
-
*
|
|
184
|
-
* @param {PeerId} peerId
|
|
185
|
-
* @param {number} value - A number between 0 and 1
|
|
186
|
-
* @returns {void}
|
|
187
|
-
*/
|
|
188
|
-
setPeerValue (peerId, value) {
|
|
189
|
-
if (value < 0 || value > 1) {
|
|
190
|
-
throw new Error('value should be a number between 0 and 1')
|
|
191
|
-
}
|
|
192
|
-
this._peerValues.set(peerId.toB58String(), value)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Checks the libp2p metrics to determine if any values have exceeded
|
|
197
|
-
* the configured maximums.
|
|
198
|
-
*
|
|
199
|
-
* @private
|
|
200
|
-
*/
|
|
201
|
-
async _checkMetrics () {
|
|
202
|
-
if (this._libp2p.metrics) {
|
|
203
|
-
try {
|
|
204
|
-
const movingAverages = this._libp2p.metrics.global.movingAverages
|
|
205
|
-
// @ts-ignore moving averages object types
|
|
206
|
-
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
|
|
207
|
-
await this._checkMaxLimit('maxReceivedData', received)
|
|
208
|
-
// @ts-ignore moving averages object types
|
|
209
|
-
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
|
|
210
|
-
await this._checkMaxLimit('maxSentData', sent)
|
|
211
|
-
const total = received + sent
|
|
212
|
-
await this._checkMaxLimit('maxData', total)
|
|
213
|
-
log('metrics update', total)
|
|
214
|
-
} finally {
|
|
215
|
-
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Tracks the incoming connection and check the connection limit
|
|
222
|
-
*
|
|
223
|
-
* @param {Connection} connection
|
|
224
|
-
*/
|
|
225
|
-
async onConnect (connection) {
|
|
226
|
-
if (!this._started) {
|
|
227
|
-
// This can happen when we are in the process of shutting down the node
|
|
228
|
-
await connection.close()
|
|
229
|
-
return
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const peerId = connection.remotePeer
|
|
233
|
-
const peerIdStr = peerId.toB58String()
|
|
234
|
-
const storedConn = this.connections.get(peerIdStr)
|
|
235
|
-
|
|
236
|
-
this.emit('peer:connect', connection)
|
|
237
|
-
|
|
238
|
-
if (storedConn) {
|
|
239
|
-
storedConn.push(connection)
|
|
240
|
-
} else {
|
|
241
|
-
this.connections.set(peerIdStr, [connection])
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
await this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
|
|
245
|
-
|
|
246
|
-
if (!this._peerValues.has(peerIdStr)) {
|
|
247
|
-
this._peerValues.set(peerIdStr, this._options.defaultPeerValue)
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
await this._checkMaxLimit('maxConnections', this.size)
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Removes the connection from tracking
|
|
255
|
-
*
|
|
256
|
-
* @param {Connection} connection
|
|
257
|
-
* @returns {void}
|
|
258
|
-
*/
|
|
259
|
-
onDisconnect (connection) {
|
|
260
|
-
if (!this._started) {
|
|
261
|
-
// This can happen when we are in the process of shutting down the node
|
|
262
|
-
return
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const peerId = connection.remotePeer.toB58String()
|
|
266
|
-
let storedConn = this.connections.get(peerId)
|
|
267
|
-
|
|
268
|
-
if (storedConn && storedConn.length > 1) {
|
|
269
|
-
storedConn = storedConn.filter((conn) => conn.id !== connection.id)
|
|
270
|
-
this.connections.set(peerId, storedConn)
|
|
271
|
-
} else if (storedConn) {
|
|
272
|
-
this.connections.delete(peerId)
|
|
273
|
-
this._peerValues.delete(connection.remotePeer.toB58String())
|
|
274
|
-
this.emit('peer:disconnect', connection)
|
|
275
|
-
|
|
276
|
-
this._libp2p.metrics && this._libp2p.metrics.onPeerDisconnected(connection.remotePeer)
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Get a connection with a peer.
|
|
282
|
-
*
|
|
283
|
-
* @param {PeerId} peerId
|
|
284
|
-
* @returns {Connection|null}
|
|
285
|
-
*/
|
|
286
|
-
get (peerId) {
|
|
287
|
-
const connections = this.getAll(peerId)
|
|
288
|
-
if (connections.length) {
|
|
289
|
-
return connections[0]
|
|
290
|
-
}
|
|
291
|
-
return null
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Get all open connections with a peer.
|
|
296
|
-
*
|
|
297
|
-
* @param {PeerId} peerId
|
|
298
|
-
* @returns {Connection[]}
|
|
299
|
-
*/
|
|
300
|
-
getAll (peerId) {
|
|
301
|
-
if (!PeerId.isPeerId(peerId)) {
|
|
302
|
-
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const id = peerId.toB58String()
|
|
306
|
-
const connections = this.connections.get(id)
|
|
307
|
-
|
|
308
|
-
// Return all open connections
|
|
309
|
-
if (connections) {
|
|
310
|
-
return connections.filter(connection => connection.stat.status === 'open')
|
|
311
|
-
}
|
|
312
|
-
return []
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* If the event loop is slow, maybe close a connection
|
|
317
|
-
*
|
|
318
|
-
* @private
|
|
319
|
-
* @param {*} summary - The LatencyMonitor summary
|
|
320
|
-
*/
|
|
321
|
-
_onLatencyMeasure (summary) {
|
|
322
|
-
this._checkMaxLimit('maxEventLoopDelay', summary.avgMs)
|
|
323
|
-
.catch(err => {
|
|
324
|
-
log.error(err)
|
|
325
|
-
})
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* If the `value` of `name` has exceeded its limit, maybe close a connection
|
|
330
|
-
*
|
|
331
|
-
* @private
|
|
332
|
-
* @param {string} name - The name of the field to check limits for
|
|
333
|
-
* @param {number} value - The current value of the field
|
|
334
|
-
*/
|
|
335
|
-
async _checkMaxLimit (name, value) {
|
|
336
|
-
const limit = this._options[name]
|
|
337
|
-
log('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
338
|
-
if (value > limit) {
|
|
339
|
-
log('%s: limit exceeded: %s, %d', this._peerId, name, value)
|
|
340
|
-
await this._maybeDisconnectOne()
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* If we have more connections than our maximum, close a connection
|
|
346
|
-
* to the lowest valued peer.
|
|
347
|
-
*
|
|
348
|
-
* @private
|
|
349
|
-
*/
|
|
350
|
-
async _maybeDisconnectOne () {
|
|
351
|
-
if (this._options.minConnections < this.connections.size) {
|
|
352
|
-
const peerValues = Array.from(new Map([...this._peerValues.entries()].sort((a, b) => a[1] - b[1])))
|
|
353
|
-
log('%s: sorted peer values: %j', this._peerId, peerValues)
|
|
354
|
-
const disconnectPeer = peerValues[0]
|
|
355
|
-
if (disconnectPeer) {
|
|
356
|
-
const peerId = disconnectPeer[0]
|
|
357
|
-
log('%s: lowest value peer is %s', this._peerId, peerId)
|
|
358
|
-
log('%s: closing a connection to %j', this._peerId, peerId)
|
|
359
|
-
for (const connections of this.connections.values()) {
|
|
360
|
-
if (connections[0].remotePeer.toB58String() === peerId) {
|
|
361
|
-
connections[0].close().catch(err => {
|
|
362
|
-
log.error(err)
|
|
363
|
-
})
|
|
364
|
-
// TODO: should not need to invoke this manually
|
|
365
|
-
this.onDisconnect(connections[0])
|
|
366
|
-
break
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
module.exports = ConnectionManager
|