libp2p 0.36.2 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -21
- package/README.md +13 -10
- package/dist/src/address-manager/index.d.ts +34 -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 +216 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +26 -53
- 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 +18 -10
- 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 +19 -38
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +59 -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 +62 -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/pb/index.d.ts +52 -0
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +98 -0
- package/dist/src/circuit/pb/index.js.map +1 -0
- 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 +192 -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 +91 -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/dialer/auto-dialer.d.ts +16 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/connection-manager/dialer/dial-request.d.ts +27 -0
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/dial-request.js +83 -0
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/connection-manager/dialer/index.d.ts +104 -0
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/index.js +267 -0
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +102 -136
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +333 -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 +27 -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/dht/dummy-dht.d.ts +20 -0
- package/dist/src/dht/dummy-dht.d.ts.map +1 -0
- package/dist/src/dht/dummy-dht.js +46 -0
- package/dist/src/dht/dummy-dht.js.map +1 -0
- package/dist/src/errors.d.ts +70 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +75 -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 +129 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/pb/proto.d.ts +27 -0
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +50 -0
- package/dist/src/fetch/pb/proto.js.map +1 -0
- 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 +42 -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/dist/src/identify/pb/message.d.ts +16 -0
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +24 -0
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +150 -496
- 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 +81 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/pb/proto.d.ts +29 -0
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +53 -0
- package/dist/src/insecure/pb/proto.js.map +1 -0
- 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 +60 -81
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +492 -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 +80 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +397 -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 +41 -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 +40 -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 +57 -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 +12 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +67 -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/pubsub/dummy-pubsub.d.ts +17 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.js +37 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
- package/dist/src/registrar.d.ts +26 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +144 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +36 -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 +144 -201
- package/src/address-manager/index.ts +130 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +286 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +86 -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.ts +82 -0
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.proto +1 -1
- package/src/circuit/pb/index.ts +117 -0
- package/src/circuit/transport.ts +219 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +99 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/dialer/auto-dialer.ts +59 -0
- package/src/{dialer/dial-request.js → connection-manager/dialer/dial-request.ts} +60 -51
- package/src/connection-manager/dialer/index.ts +382 -0
- package/src/connection-manager/index.ts +508 -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 +144 -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/dht/dummy-dht.ts +60 -0
- package/src/errors.ts +73 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/index.ts +167 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/fetch/pb/proto.ts +65 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/identify/pb/message.ts +37 -0
- package/src/index.ts +220 -0
- package/src/insecure/index.ts +101 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/insecure/pb/proto.ts +68 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +587 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +538 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +238 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +186 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +84 -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 +97 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/pubsub/dummy-pubsub.ts +51 -0
- package/src/registrar.ts +189 -0
- package/src/transport-manager.ts +281 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/circuit/protocol/index.d.ts +0 -173
- package/dist/src/dialer/dial-request.d.ts +0 -55
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/index.d.ts +0 -182
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/fetch/proto.d.ts +0 -134
- package/dist/src/identify/message.d.ts +0 -95
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- package/dist/src/insecure/proto.d.ts +0 -128
- 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/listener.js +0 -75
- package/src/circuit/multicodec.js +0 -5
- package/src/circuit/protocol/index.d.ts +0 -173
- package/src/circuit/protocol/index.js +0 -530
- 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/fetch/index.js +0 -159
- package/src/fetch/proto.d.ts +0 -134
- package/src/fetch/proto.js +0 -333
- package/src/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/identify/message.d.ts +0 -95
- package/src/identify/message.js +0 -328
- package/src/index.js +0 -813
- package/src/insecure/plaintext.js +0 -95
- package/src/insecure/proto.d.ts +0 -128
- package/src/insecure/proto.js +0 -371
- 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,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
|
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { EventEmitter } = require('events')
|
|
9
|
-
const VisibilityChangeEmitter = require('./visibility-change-emitter')
|
|
10
|
-
const debug = require('debug')('latency-monitor:LatencyMonitor')
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {Object} SummaryObject
|
|
14
|
-
* @property {number} events How many events were called
|
|
15
|
-
* @property {number} minMS What was the min time for a cb to be called
|
|
16
|
-
* @property {number} maxMS What was the max time for a cb to be called
|
|
17
|
-
* @property {number} avgMs What was the average time for a cb to be called
|
|
18
|
-
* @property {number} lengthMs How long this interval was in ms
|
|
19
|
-
*
|
|
20
|
-
* @typedef {Object} LatencyMonitorOptions
|
|
21
|
-
* @property {number} [latencyCheckIntervalMs=500] - How often to add a latency check event (ms)
|
|
22
|
-
* @property {number} [dataEmitIntervalMs=5000] - How often to summarize latency check events. null or 0 disables event firing
|
|
23
|
-
* @property {Function} [asyncTestFn] - What cb-style async function to use
|
|
24
|
-
* @property {number} [latencyRandomPercentage=5] - What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* A class to monitor latency of any async function which works in a browser or node. This works by periodically calling
|
|
29
|
-
* the asyncTestFn and timing how long it takes the callback to be called. It can also periodically emit stats about this.
|
|
30
|
-
* This can be disabled and stats can be pulled via setting dataEmitIntervalMs = 0.
|
|
31
|
-
*
|
|
32
|
-
* @extends {EventEmitter}
|
|
33
|
-
*
|
|
34
|
-
* The default implementation is an event loop latency monitor. This works by firing periodic events into the event loop
|
|
35
|
-
* and timing how long it takes to get back.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* const monitor = new LatencyMonitor();
|
|
39
|
-
* monitor.on('data', (summary) => console.log('Event Loop Latency: %O', summary));
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* const monitor = new LatencyMonitor({latencyCheckIntervalMs: 1000, dataEmitIntervalMs: 60000, asyncTestFn:ping});
|
|
43
|
-
* monitor.on('data', (summary) => console.log('Ping Pong Latency: %O', summary));
|
|
44
|
-
*/
|
|
45
|
-
class LatencyMonitor extends EventEmitter {
|
|
46
|
-
/**
|
|
47
|
-
* @class
|
|
48
|
-
* @param {LatencyMonitorOptions} [options]
|
|
49
|
-
*/
|
|
50
|
-
constructor ({ latencyCheckIntervalMs, dataEmitIntervalMs, asyncTestFn, latencyRandomPercentage } = {}) {
|
|
51
|
-
super()
|
|
52
|
-
const that = this
|
|
53
|
-
|
|
54
|
-
// 0 isn't valid here, so its ok to use ||
|
|
55
|
-
that.latencyCheckIntervalMs = latencyCheckIntervalMs || 500 // 0.5s
|
|
56
|
-
that.latencyRandomPercentage = latencyRandomPercentage || 10
|
|
57
|
-
that._latecyCheckMultiply = 2 * (that.latencyRandomPercentage / 100.0) * that.latencyCheckIntervalMs
|
|
58
|
-
that._latecyCheckSubtract = that._latecyCheckMultiply / 2
|
|
59
|
-
|
|
60
|
-
that.dataEmitIntervalMs = (dataEmitIntervalMs === null || dataEmitIntervalMs === 0)
|
|
61
|
-
? undefined
|
|
62
|
-
: dataEmitIntervalMs || 5 * 1000 // 5s
|
|
63
|
-
debug('latencyCheckIntervalMs: %s dataEmitIntervalMs: %s',
|
|
64
|
-
that.latencyCheckIntervalMs, that.dataEmitIntervalMs)
|
|
65
|
-
if (that.dataEmitIntervalMs) {
|
|
66
|
-
debug('Expecting ~%s events per summary', that.latencyCheckIntervalMs / that.dataEmitIntervalMs)
|
|
67
|
-
} else {
|
|
68
|
-
debug('Not emitting summaries')
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
that.asyncTestFn = asyncTestFn // If there is no asyncFn, we measure latency
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
start () {
|
|
75
|
-
// If process: use high resolution timer
|
|
76
|
-
if (globalThis.process && globalThis.process.hrtime) { // eslint-disable-line no-undef
|
|
77
|
-
debug('Using process.hrtime for timing')
|
|
78
|
-
this.now = globalThis.process.hrtime // eslint-disable-line no-undef
|
|
79
|
-
this.getDeltaMS = (startTime) => {
|
|
80
|
-
const hrtime = this.now(startTime)
|
|
81
|
-
return (hrtime[0] * 1000) + (hrtime[1] / 1000000)
|
|
82
|
-
}
|
|
83
|
-
// Let's try for a timer that only monotonically increases
|
|
84
|
-
} else if (typeof window !== 'undefined' && window.performance && window.performance.now) {
|
|
85
|
-
debug('Using performance.now for timing')
|
|
86
|
-
this.now = window.performance.now.bind(window.performance)
|
|
87
|
-
this.getDeltaMS = (startTime) => Math.round(this.now() - startTime)
|
|
88
|
-
} else {
|
|
89
|
-
debug('Using Date.now for timing')
|
|
90
|
-
this.now = Date.now
|
|
91
|
-
this.getDeltaMS = (startTime) => this.now() - startTime
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
this._latencyData = this._initLatencyData()
|
|
95
|
-
|
|
96
|
-
// We check for isBrowser because of browsers set max rates of timeouts when a page is hidden,
|
|
97
|
-
// so we fall back to another library
|
|
98
|
-
// See: http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs
|
|
99
|
-
if (isBrowser()) {
|
|
100
|
-
this._visibilityChangeEmitter = new VisibilityChangeEmitter()
|
|
101
|
-
|
|
102
|
-
this._visibilityChangeEmitter.on('visibilityChange', (pageInFocus) => {
|
|
103
|
-
if (pageInFocus) {
|
|
104
|
-
this._startTimers()
|
|
105
|
-
} else {
|
|
106
|
-
this._emitSummary()
|
|
107
|
-
this._stopTimers()
|
|
108
|
-
}
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!this._visibilityChangeEmitter || this._visibilityChangeEmitter.isVisible()) {
|
|
113
|
-
this._startTimers()
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
stop () {
|
|
118
|
-
this._stopTimers()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Start internal timers
|
|
123
|
-
*
|
|
124
|
-
* @private
|
|
125
|
-
*/
|
|
126
|
-
_startTimers () {
|
|
127
|
-
// Timer already started, ignore this
|
|
128
|
-
if (this._checkLatencyID) {
|
|
129
|
-
return
|
|
130
|
-
}
|
|
131
|
-
this._checkLatency()
|
|
132
|
-
if (this.dataEmitIntervalMs) {
|
|
133
|
-
this._emitIntervalID = setInterval(() => this._emitSummary(), this.dataEmitIntervalMs)
|
|
134
|
-
if (typeof this._emitIntervalID.unref === 'function') {
|
|
135
|
-
this._emitIntervalID.unref() // Doesn't block exit
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Stop internal timers
|
|
142
|
-
*
|
|
143
|
-
* @private
|
|
144
|
-
*/
|
|
145
|
-
_stopTimers () {
|
|
146
|
-
if (this._checkLatencyID) {
|
|
147
|
-
clearTimeout(this._checkLatencyID)
|
|
148
|
-
this._checkLatencyID = undefined
|
|
149
|
-
}
|
|
150
|
-
if (this._emitIntervalID) {
|
|
151
|
-
clearInterval(this._emitIntervalID)
|
|
152
|
-
this._emitIntervalID = undefined
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Emit summary only if there were events. It might not have any events if it was forced via a page hidden/show
|
|
158
|
-
*
|
|
159
|
-
* @private
|
|
160
|
-
*/
|
|
161
|
-
_emitSummary () {
|
|
162
|
-
const summary = this.getSummary()
|
|
163
|
-
if (summary.events > 0) {
|
|
164
|
-
this.emit('data', summary)
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Calling this function will end the collection period. If a timing event was already fired and somewhere in the queue,
|
|
170
|
-
* it will not count for this time period
|
|
171
|
-
*
|
|
172
|
-
* @returns {SummaryObject}
|
|
173
|
-
*/
|
|
174
|
-
getSummary () {
|
|
175
|
-
// We might want to adjust for the number of expected events
|
|
176
|
-
// Example: first 1 event it comes back, then such a long blocker that the next emit check comes
|
|
177
|
-
// Then this fires - looks like no latency!!
|
|
178
|
-
const latency = {
|
|
179
|
-
events: this._latencyData.events,
|
|
180
|
-
minMs: this._latencyData.minMs,
|
|
181
|
-
maxMs: this._latencyData.maxMs,
|
|
182
|
-
avgMs: this._latencyData.events
|
|
183
|
-
? this._latencyData.totalMs / this._latencyData.events
|
|
184
|
-
: Number.POSITIVE_INFINITY,
|
|
185
|
-
lengthMs: this.getDeltaMS(this._latencyData.startTime)
|
|
186
|
-
}
|
|
187
|
-
this._latencyData = this._initLatencyData() // Clear
|
|
188
|
-
|
|
189
|
-
debug('Summary: %O', latency)
|
|
190
|
-
return latency
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Randomly calls an async fn every roughly latencyCheckIntervalMs (plus some randomness). If no async fn is found,
|
|
195
|
-
* it will simply report on event loop latency.
|
|
196
|
-
*
|
|
197
|
-
* @private
|
|
198
|
-
*/
|
|
199
|
-
_checkLatency () {
|
|
200
|
-
const that = this
|
|
201
|
-
// Randomness is needed to avoid alignment by accident to regular things in the event loop
|
|
202
|
-
const randomness = (Math.random() * that._latecyCheckMultiply) - that._latecyCheckSubtract
|
|
203
|
-
|
|
204
|
-
// We use this to ensure that in case some overlap somehow, we don't take the wrong startTime/offset
|
|
205
|
-
const localData = {
|
|
206
|
-
deltaOffset: Math.ceil(that.latencyCheckIntervalMs + randomness),
|
|
207
|
-
startTime: that.now()
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const cb = () => {
|
|
211
|
-
// We are already stopped, ignore this datapoint
|
|
212
|
-
if (!this._checkLatencyID) {
|
|
213
|
-
return
|
|
214
|
-
}
|
|
215
|
-
const deltaMS = that.getDeltaMS(localData.startTime) - localData.deltaOffset
|
|
216
|
-
that._checkLatency() // Start again ASAP
|
|
217
|
-
|
|
218
|
-
// Add the data point. If this gets complex, refactor it
|
|
219
|
-
that._latencyData.events++
|
|
220
|
-
that._latencyData.minMs = Math.min(that._latencyData.minMs, deltaMS)
|
|
221
|
-
that._latencyData.maxMs = Math.max(that._latencyData.maxMs, deltaMS)
|
|
222
|
-
that._latencyData.totalMs += deltaMS
|
|
223
|
-
debug('MS: %s Data: %O', deltaMS, that._latencyData)
|
|
224
|
-
}
|
|
225
|
-
debug('localData: %O', localData)
|
|
226
|
-
|
|
227
|
-
this._checkLatencyID = setTimeout(() => {
|
|
228
|
-
// This gets rid of including event loop
|
|
229
|
-
if (that.asyncTestFn) {
|
|
230
|
-
// Clear timing related things
|
|
231
|
-
localData.deltaOffset = 0
|
|
232
|
-
localData.startTime = that.now()
|
|
233
|
-
that.asyncTestFn(cb)
|
|
234
|
-
} else {
|
|
235
|
-
// setTimeout is not more accurate than 1ms, so this will ensure positive numbers. Add 1 to emitted data to remove.
|
|
236
|
-
// This is not the best, but for now it'll be just fine. This isn't meant to be sub ms accurate.
|
|
237
|
-
localData.deltaOffset -= 1
|
|
238
|
-
// If there is no function to test, we mean check latency which is a special case that is really cb => cb()
|
|
239
|
-
// We avoid that for the few extra function all overheads. Also, we want to keep the timers different
|
|
240
|
-
cb()
|
|
241
|
-
}
|
|
242
|
-
}, localData.deltaOffset)
|
|
243
|
-
|
|
244
|
-
if (typeof this._checkLatencyID.unref === 'function') {
|
|
245
|
-
this._checkLatencyID.unref() // Doesn't block exit
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
_initLatencyData () {
|
|
250
|
-
return {
|
|
251
|
-
startTime: this.now(),
|
|
252
|
-
minMs: Number.POSITIVE_INFINITY,
|
|
253
|
-
maxMs: Number.NEGATIVE_INFINITY,
|
|
254
|
-
events: 0,
|
|
255
|
-
totalMs: 0
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function isBrowser () {
|
|
261
|
-
return typeof window !== 'undefined'
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
module.exports = LatencyMonitor
|
package/src/constants.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
|
|
5
|
-
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
|
|
6
|
-
MAX_PER_PEER_DIALS: 4, // Allowed parallel dials per DialRequest
|
|
7
|
-
MAX_ADDRS_TO_DIAL: 25, // Maximum number of allowed addresses to attempt to dial
|
|
8
|
-
METRICS: {
|
|
9
|
-
computeThrottleMaxQueueSize: 1000,
|
|
10
|
-
computeThrottleTimeout: 2000,
|
|
11
|
-
movingAverageIntervals: [
|
|
12
|
-
60 * 1000, // 1 minute
|
|
13
|
-
5 * 60 * 1000, // 5 minutes
|
|
14
|
-
15 * 60 * 1000 // 15 minutes
|
|
15
|
-
],
|
|
16
|
-
maxOldPeersRetention: 50
|
|
17
|
-
}
|
|
18
|
-
}
|