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
package/src/index.js
DELETED
|
@@ -1,813 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const log = Object.assign(debug('libp2p'), {
|
|
5
|
-
error: debug('libp2p:err')
|
|
6
|
-
})
|
|
7
|
-
const { EventEmitter } = require('events')
|
|
8
|
-
|
|
9
|
-
const errCode = require('err-code')
|
|
10
|
-
const PeerId = require('peer-id')
|
|
11
|
-
const { Multiaddr } = require('multiaddr')
|
|
12
|
-
const { MemoryDatastore } = require('datastore-core/memory')
|
|
13
|
-
const PeerRouting = require('./peer-routing')
|
|
14
|
-
const ContentRouting = require('./content-routing')
|
|
15
|
-
const getPeer = require('./get-peer')
|
|
16
|
-
const { validate: validateConfig } = require('./config')
|
|
17
|
-
const { codes, messages } = require('./errors')
|
|
18
|
-
|
|
19
|
-
const AddressManager = require('./address-manager')
|
|
20
|
-
const ConnectionManager = require('./connection-manager')
|
|
21
|
-
const AutoDialler = require('./connection-manager/auto-dialler')
|
|
22
|
-
const Circuit = require('./circuit/transport')
|
|
23
|
-
const Relay = require('./circuit')
|
|
24
|
-
const Dialer = require('./dialer')
|
|
25
|
-
const Keychain = require('./keychain')
|
|
26
|
-
const Metrics = require('./metrics')
|
|
27
|
-
const TransportManager = require('./transport-manager')
|
|
28
|
-
const Upgrader = require('./upgrader')
|
|
29
|
-
const PeerStore = require('./peer-store')
|
|
30
|
-
const PubsubAdapter = require('./pubsub-adapter')
|
|
31
|
-
const Registrar = require('./registrar')
|
|
32
|
-
const IdentifyService = require('./identify')
|
|
33
|
-
const FetchService = require('./fetch')
|
|
34
|
-
const PingService = require('./ping')
|
|
35
|
-
const NatManager = require('./nat-manager')
|
|
36
|
-
const { updateSelfPeerRecord } = require('./record/utils')
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
40
|
-
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
|
41
|
-
* @typedef {import('libp2p-interfaces/src/transport/types').TransportFactory<any, any>} TransportFactory
|
|
42
|
-
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory} MuxerFactory
|
|
43
|
-
* @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule
|
|
44
|
-
* @typedef {import('libp2p-interfaces/src/peer-discovery/types').PeerDiscoveryFactory} PeerDiscoveryFactory
|
|
45
|
-
* @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule
|
|
46
|
-
* @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
|
|
47
|
-
* @typedef {import('libp2p-interfaces/src/pubsub')} Pubsub
|
|
48
|
-
* @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
|
|
49
|
-
* @typedef {import('interface-datastore').Datastore} Datastore
|
|
50
|
-
* @typedef {import('./pnet')} Protector
|
|
51
|
-
* @typedef {import('./types').ConnectionGater} ConnectionGater
|
|
52
|
-
* @typedef {Object} PersistentPeerStoreOptions
|
|
53
|
-
* @property {number} [threshold]
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @typedef {Object} HandlerProps
|
|
58
|
-
* @property {Connection} connection
|
|
59
|
-
* @property {MuxedStream} stream
|
|
60
|
-
* @property {string} protocol
|
|
61
|
-
*
|
|
62
|
-
* @typedef {Object} DhtOptions
|
|
63
|
-
* @property {boolean} [enabled = false]
|
|
64
|
-
* @property {number} [kBucketSize = 20]
|
|
65
|
-
* @property {boolean} [clientMode]
|
|
66
|
-
* @property {import('libp2p-interfaces/src/types').DhtSelectors} [selectors]
|
|
67
|
-
* @property {import('libp2p-interfaces/src/types').DhtValidators} [validators]
|
|
68
|
-
*
|
|
69
|
-
* @typedef {Object} KeychainOptions
|
|
70
|
-
* @property {Datastore} [datastore]
|
|
71
|
-
*
|
|
72
|
-
* @typedef {Object} PeerStoreOptions
|
|
73
|
-
* @property {boolean} persistence
|
|
74
|
-
*
|
|
75
|
-
* @typedef {Object} PubsubLocalOptions
|
|
76
|
-
* @property {boolean} enabled
|
|
77
|
-
*
|
|
78
|
-
* @typedef {Object} MetricsOptions
|
|
79
|
-
* @property {boolean} enabled
|
|
80
|
-
*
|
|
81
|
-
* @typedef {Object} RelayOptions
|
|
82
|
-
* @property {boolean} [enabled = true]
|
|
83
|
-
* @property {import('./circuit').RelayAdvertiseOptions} [advertise]
|
|
84
|
-
* @property {import('./circuit').HopOptions} [hop]
|
|
85
|
-
* @property {import('./circuit').AutoRelayOptions} [autoRelay]
|
|
86
|
-
*
|
|
87
|
-
* @typedef {Object} Libp2pConfig
|
|
88
|
-
* @property {DhtOptions} [dht] dht module options
|
|
89
|
-
* @property {import('./nat-manager').NatManagerOptions} [nat]
|
|
90
|
-
* @property {Record<string, Object|boolean>} [peerDiscovery]
|
|
91
|
-
* @property {PubsubLocalOptions & PubsubOptions} [pubsub] pubsub module options
|
|
92
|
-
* @property {RelayOptions} [relay]
|
|
93
|
-
* @property {Record<string, Object>} [transport] transport options indexed by transport key
|
|
94
|
-
*
|
|
95
|
-
* @typedef {Object} Libp2pModules
|
|
96
|
-
* @property {TransportFactory[]} transport
|
|
97
|
-
* @property {MuxerFactory[]} streamMuxer
|
|
98
|
-
* @property {Crypto[]} connEncryption
|
|
99
|
-
* @property {PeerDiscoveryFactory[]} [peerDiscovery]
|
|
100
|
-
* @property {PeerRoutingModule[]} [peerRouting]
|
|
101
|
-
* @property {ContentRoutingModule[]} [contentRouting]
|
|
102
|
-
* @property {Object} [dht]
|
|
103
|
-
* @property {{new(...args: any[]): Pubsub}} [pubsub]
|
|
104
|
-
* @property {Protector} [connProtector]
|
|
105
|
-
*
|
|
106
|
-
* @typedef {Object} Libp2pOptions
|
|
107
|
-
* @property {Libp2pModules} modules libp2p modules to use
|
|
108
|
-
* @property {import('./address-manager').AddressManagerOptions} [addresses]
|
|
109
|
-
* @property {import('./connection-manager').ConnectionManagerOptions} [connectionManager]
|
|
110
|
-
* @property {Partial<import('./types').ConnectionGater>} [connectionGater]
|
|
111
|
-
* @property {Datastore} [datastore]
|
|
112
|
-
* @property {import('./dialer').DialerOptions} [dialer]
|
|
113
|
-
* @property {import('./identify/index').HostProperties} [host] libp2p host
|
|
114
|
-
* @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
|
|
115
|
-
* @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
|
|
116
|
-
* @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
|
|
117
|
-
* @property {PeerStoreOptions} [peerStore]
|
|
118
|
-
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
|
|
119
|
-
* @property {Libp2pConfig} [config]
|
|
120
|
-
*
|
|
121
|
-
* @typedef {Object} constructorOptions
|
|
122
|
-
* @property {PeerId} peerId
|
|
123
|
-
*
|
|
124
|
-
* @typedef {Object} CreateOptions
|
|
125
|
-
* @property {PeerId} [peerId]
|
|
126
|
-
*
|
|
127
|
-
* @extends {EventEmitter}
|
|
128
|
-
* @fires Libp2p#error Emitted when an error occurs
|
|
129
|
-
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
|
130
|
-
*/
|
|
131
|
-
class Libp2p extends EventEmitter {
|
|
132
|
-
/**
|
|
133
|
-
* Like `new Libp2p(options)` except it will create a `PeerId`
|
|
134
|
-
* instance if one is not provided in options.
|
|
135
|
-
*
|
|
136
|
-
* @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options
|
|
137
|
-
* @returns {Promise<Libp2p>}
|
|
138
|
-
*/
|
|
139
|
-
static async create (options) {
|
|
140
|
-
if (options.peerId) {
|
|
141
|
-
// @ts-ignore 'Libp2pOptions & CreateOptions' is not assignable to 'Libp2pOptions & constructorOptions'
|
|
142
|
-
return new Libp2p(options)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const peerId = await PeerId.create()
|
|
146
|
-
|
|
147
|
-
options.peerId = peerId
|
|
148
|
-
// @ts-ignore 'Libp2pOptions & CreateOptions' is not assignable to 'Libp2pOptions & constructorOptions'
|
|
149
|
-
return new Libp2p(options)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Libp2p node.
|
|
154
|
-
*
|
|
155
|
-
* @class
|
|
156
|
-
* @param {Libp2pOptions & constructorOptions} _options
|
|
157
|
-
*/
|
|
158
|
-
constructor (_options) {
|
|
159
|
-
super()
|
|
160
|
-
// validateConfig will ensure the config is correct,
|
|
161
|
-
// and add default values where appropriate
|
|
162
|
-
this._options = validateConfig(_options)
|
|
163
|
-
|
|
164
|
-
/** @type {PeerId} */
|
|
165
|
-
this.peerId = this._options.peerId
|
|
166
|
-
this.datastore = this._options.datastore
|
|
167
|
-
|
|
168
|
-
// Create Metrics
|
|
169
|
-
if (this._options.metrics.enabled) {
|
|
170
|
-
const metrics = new Metrics({
|
|
171
|
-
...this._options.metrics
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
this.metrics = metrics
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/** @type {ConnectionGater} */
|
|
178
|
-
this.connectionGater = {
|
|
179
|
-
denyDialPeer: async () => Promise.resolve(false),
|
|
180
|
-
denyDialMultiaddr: async () => Promise.resolve(false),
|
|
181
|
-
denyInboundConnection: async () => Promise.resolve(false),
|
|
182
|
-
denyOutboundConnection: async () => Promise.resolve(false),
|
|
183
|
-
denyInboundEncryptedConnection: async () => Promise.resolve(false),
|
|
184
|
-
denyOutboundEncryptedConnection: async () => Promise.resolve(false),
|
|
185
|
-
denyInboundUpgradedConnection: async () => Promise.resolve(false),
|
|
186
|
-
denyOutboundUpgradedConnection: async () => Promise.resolve(false),
|
|
187
|
-
filterMultiaddrForPeer: async () => Promise.resolve(true),
|
|
188
|
-
...this._options.connectionGater
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/** @type {import('./peer-store/types').PeerStore} */
|
|
192
|
-
this.peerStore = new PeerStore({
|
|
193
|
-
peerId: this.peerId,
|
|
194
|
-
datastore: (this.datastore && this._options.peerStore.persistence) ? this.datastore : new MemoryDatastore(),
|
|
195
|
-
addressFilter: this.connectionGater.filterMultiaddrForPeer
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
// Addresses {listen, announce, noAnnounce}
|
|
199
|
-
this.addresses = this._options.addresses
|
|
200
|
-
this.addressManager = new AddressManager(this.peerId, this._options.addresses)
|
|
201
|
-
|
|
202
|
-
// when addresses change, update our peer record
|
|
203
|
-
this.addressManager.on('change:addresses', () => {
|
|
204
|
-
updateSelfPeerRecord(this).catch(err => {
|
|
205
|
-
log.error('Error updating self peer record', err)
|
|
206
|
-
})
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
this._modules = this._options.modules
|
|
210
|
-
this._config = this._options.config
|
|
211
|
-
this._transport = [] // Transport instances/references
|
|
212
|
-
this._discovery = new Map() // Discovery service instances/references
|
|
213
|
-
|
|
214
|
-
// Create the Connection Manager
|
|
215
|
-
this.connectionManager = new ConnectionManager(this, {
|
|
216
|
-
...this._options.connectionManager
|
|
217
|
-
})
|
|
218
|
-
this._autodialler = new AutoDialler(this, {
|
|
219
|
-
enabled: this._config.peerDiscovery.autoDial,
|
|
220
|
-
minConnections: this._options.connectionManager.minConnections,
|
|
221
|
-
autoDialInterval: this._options.connectionManager.autoDialInterval
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
// Create keychain
|
|
225
|
-
if (this._options.keychain && this._options.keychain.datastore) {
|
|
226
|
-
log('creating keychain')
|
|
227
|
-
|
|
228
|
-
const keychainOpts = Keychain.generateOptions()
|
|
229
|
-
|
|
230
|
-
this.keychain = new Keychain(this._options.keychain.datastore, {
|
|
231
|
-
...keychainOpts,
|
|
232
|
-
...this._options.keychain
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
log('keychain constructed')
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Setup the Upgrader
|
|
239
|
-
this.upgrader = new Upgrader({
|
|
240
|
-
connectionGater: this.connectionGater,
|
|
241
|
-
localPeer: this.peerId,
|
|
242
|
-
metrics: this.metrics,
|
|
243
|
-
onConnection: (connection) => this.connectionManager.onConnect(connection),
|
|
244
|
-
onConnectionEnd: (connection) => this.connectionManager.onDisconnect(connection)
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
// Setup the transport manager
|
|
248
|
-
this.transportManager = new TransportManager({
|
|
249
|
-
libp2p: this,
|
|
250
|
-
upgrader: this.upgrader,
|
|
251
|
-
faultTolerance: this._options.transportManager.faultTolerance
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
// Create the Nat Manager
|
|
255
|
-
this.natManager = new NatManager({
|
|
256
|
-
peerId: this.peerId,
|
|
257
|
-
addressManager: this.addressManager,
|
|
258
|
-
transportManager: this.transportManager,
|
|
259
|
-
// @ts-ignore Nat typedef is not understood as Object
|
|
260
|
-
...this._options.config.nat
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
// Create the Registrar
|
|
264
|
-
this.registrar = new Registrar({
|
|
265
|
-
peerStore: this.peerStore,
|
|
266
|
-
connectionManager: this.connectionManager
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
this.handle = this.handle.bind(this)
|
|
270
|
-
this.registrar.handle = this.handle
|
|
271
|
-
|
|
272
|
-
// Attach crypto channels
|
|
273
|
-
if (!this._modules.connEncryption || !this._modules.connEncryption.length) {
|
|
274
|
-
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
|
|
275
|
-
}
|
|
276
|
-
const cryptos = this._modules.connEncryption
|
|
277
|
-
cryptos.forEach((crypto) => {
|
|
278
|
-
this.upgrader.cryptos.set(crypto.protocol, crypto)
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
this.dialer = new Dialer({
|
|
282
|
-
transportManager: this.transportManager,
|
|
283
|
-
connectionGater: this.connectionGater,
|
|
284
|
-
peerStore: this.peerStore,
|
|
285
|
-
metrics: this.metrics,
|
|
286
|
-
...this._options.dialer
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
this._modules.transport.forEach((Transport) => {
|
|
290
|
-
const key = Transport.prototype[Symbol.toStringTag]
|
|
291
|
-
const transportOptions = this._config.transport[key]
|
|
292
|
-
this.transportManager.add(key, Transport, transportOptions)
|
|
293
|
-
})
|
|
294
|
-
|
|
295
|
-
if (this._config.relay.enabled) {
|
|
296
|
-
// @ts-ignore Circuit prototype
|
|
297
|
-
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
|
|
298
|
-
this.relay = new Relay(this)
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Attach stream multiplexers
|
|
302
|
-
if (this._modules.streamMuxer) {
|
|
303
|
-
const muxers = this._modules.streamMuxer
|
|
304
|
-
muxers.forEach((muxer) => {
|
|
305
|
-
this.upgrader.muxers.set(muxer.multicodec, muxer)
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
// Add the identify service since we can multiplex
|
|
309
|
-
this.identifyService = new IdentifyService({ libp2p: this })
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// Attach private network protector
|
|
313
|
-
if (this._modules.connProtector) {
|
|
314
|
-
this.upgrader.protector = this._modules.connProtector
|
|
315
|
-
} else if (globalThis.process !== undefined && globalThis.process.env && globalThis.process.env.LIBP2P_FORCE_PNET) { // eslint-disable-line no-undef
|
|
316
|
-
throw new Error('Private network is enforced, but no protector was provided')
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// dht provided components (peerRouting, contentRouting, dht)
|
|
320
|
-
if (this._modules.dht) {
|
|
321
|
-
const DHT = this._modules.dht
|
|
322
|
-
// @ts-ignore TODO: types need fixing - DHT is an `object` which has no `create` method
|
|
323
|
-
this._dht = DHT.create({
|
|
324
|
-
libp2p: this,
|
|
325
|
-
...this._config.dht
|
|
326
|
-
})
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// Create pubsub if provided
|
|
330
|
-
if (this._modules.pubsub) {
|
|
331
|
-
const Pubsub = this._modules.pubsub
|
|
332
|
-
// using pubsub adapter with *DEPRECATED* handlers functionality
|
|
333
|
-
/** @type {Pubsub} */
|
|
334
|
-
this.pubsub = PubsubAdapter(Pubsub, this, this._config.pubsub)
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// Attach remaining APIs
|
|
338
|
-
// peer and content routing will automatically get modules from _modules and _dht
|
|
339
|
-
this.peerRouting = new PeerRouting(this)
|
|
340
|
-
this.contentRouting = new ContentRouting(this)
|
|
341
|
-
|
|
342
|
-
this._onDiscoveryPeer = this._onDiscoveryPeer.bind(this)
|
|
343
|
-
|
|
344
|
-
this.fetchService = new FetchService(this)
|
|
345
|
-
this.pingService = new PingService(this)
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Overrides EventEmitter.emit to conditionally emit errors
|
|
350
|
-
* if there is a handler. If not, errors will be logged.
|
|
351
|
-
*
|
|
352
|
-
* @param {string} eventName
|
|
353
|
-
* @param {...any} args
|
|
354
|
-
* @returns {boolean}
|
|
355
|
-
*/
|
|
356
|
-
emit (eventName, ...args) {
|
|
357
|
-
// TODO: do we still need this?
|
|
358
|
-
// @ts-ignore _events does not exist in libp2p
|
|
359
|
-
if (eventName === 'error' && !this._events.error) {
|
|
360
|
-
log.error(args)
|
|
361
|
-
return false
|
|
362
|
-
} else {
|
|
363
|
-
return super.emit(eventName, ...args)
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Starts the libp2p node and all its subsystems
|
|
369
|
-
*
|
|
370
|
-
* @returns {Promise<void>}
|
|
371
|
-
*/
|
|
372
|
-
async start () {
|
|
373
|
-
log('libp2p is starting')
|
|
374
|
-
|
|
375
|
-
if (this.identifyService) {
|
|
376
|
-
await this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (this.fetchService) {
|
|
380
|
-
await this.handle(FetchService.PROTOCOL, this.fetchService.handleMessage)
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
if (this.pingService) {
|
|
384
|
-
await this.handle(PingService.getProtocolStr(this), this.pingService.handleMessage)
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
try {
|
|
388
|
-
await this._onStarting()
|
|
389
|
-
await this._onDidStart()
|
|
390
|
-
log('libp2p has started')
|
|
391
|
-
} catch (/** @type {any} */ err) {
|
|
392
|
-
this.emit('error', err)
|
|
393
|
-
log.error('An error occurred starting libp2p', err)
|
|
394
|
-
await this.stop()
|
|
395
|
-
throw err
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Stop the libp2p node by closing its listeners and open connections
|
|
401
|
-
*
|
|
402
|
-
* @async
|
|
403
|
-
* @returns {Promise<void>}
|
|
404
|
-
*/
|
|
405
|
-
async stop () {
|
|
406
|
-
log('libp2p is stopping')
|
|
407
|
-
|
|
408
|
-
try {
|
|
409
|
-
this._isStarted = false
|
|
410
|
-
|
|
411
|
-
if (this.identifyService) {
|
|
412
|
-
await this.identifyService.stop()
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
this.relay && this.relay.stop()
|
|
416
|
-
this.peerRouting.stop()
|
|
417
|
-
await this._autodialler.stop()
|
|
418
|
-
await (this._dht && this._dht.stop())
|
|
419
|
-
|
|
420
|
-
for (const service of this._discovery.values()) {
|
|
421
|
-
service.removeListener('peer', this._onDiscoveryPeer)
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
await Promise.all(Array.from(this._discovery.values(), s => s.stop()))
|
|
425
|
-
|
|
426
|
-
this._discovery = new Map()
|
|
427
|
-
|
|
428
|
-
await this.connectionManager.stop()
|
|
429
|
-
|
|
430
|
-
await Promise.all([
|
|
431
|
-
this.pubsub && this.pubsub.stop(),
|
|
432
|
-
this.metrics && this.metrics.stop()
|
|
433
|
-
])
|
|
434
|
-
|
|
435
|
-
await this.natManager.stop()
|
|
436
|
-
await this.transportManager.close()
|
|
437
|
-
|
|
438
|
-
await this.unhandle(FetchService.PROTOCOL)
|
|
439
|
-
await this.unhandle(PingService.getProtocolStr(this))
|
|
440
|
-
|
|
441
|
-
this.dialer.destroy()
|
|
442
|
-
} catch (/** @type {any} */ err) {
|
|
443
|
-
if (err) {
|
|
444
|
-
log.error(err)
|
|
445
|
-
this.emit('error', err)
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
log('libp2p has stopped')
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* Load keychain keys from the datastore.
|
|
453
|
-
* Imports the private key as 'self', if needed.
|
|
454
|
-
*
|
|
455
|
-
* @async
|
|
456
|
-
* @returns {Promise<void>}
|
|
457
|
-
*/
|
|
458
|
-
async loadKeychain () {
|
|
459
|
-
if (!this.keychain) {
|
|
460
|
-
return
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
try {
|
|
464
|
-
await this.keychain.findKeyByName('self')
|
|
465
|
-
} catch (/** @type {any} */ err) {
|
|
466
|
-
await this.keychain.importPeer('self', this.peerId)
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
isStarted () {
|
|
471
|
-
return this._isStarted
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* Gets a Map of the current connections. The keys are the stringified
|
|
476
|
-
* `PeerId` of the peer. The value is an array of Connections to that peer.
|
|
477
|
-
*
|
|
478
|
-
* @returns {Map<string, Connection[]>}
|
|
479
|
-
*/
|
|
480
|
-
get connections () {
|
|
481
|
-
return this.connectionManager.connections
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Dials to the provided peer. If successful, the known metadata of the
|
|
486
|
-
* peer will be added to the nodes `peerStore`
|
|
487
|
-
*
|
|
488
|
-
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
|
489
|
-
* @param {object} [options]
|
|
490
|
-
* @param {AbortSignal} [options.signal]
|
|
491
|
-
* @returns {Promise<Connection>}
|
|
492
|
-
*/
|
|
493
|
-
dial (peer, options) {
|
|
494
|
-
return this._dial(peer, options)
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
/**
|
|
498
|
-
* Dials to the provided peer and tries to handshake with the given protocols in order.
|
|
499
|
-
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
|
|
500
|
-
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
|
|
501
|
-
*
|
|
502
|
-
* @async
|
|
503
|
-
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
|
504
|
-
* @param {string[]|string} protocols
|
|
505
|
-
* @param {object} [options]
|
|
506
|
-
* @param {AbortSignal} [options.signal]
|
|
507
|
-
*/
|
|
508
|
-
async dialProtocol (peer, protocols, options) {
|
|
509
|
-
if (!protocols || !protocols.length) {
|
|
510
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const connection = await this._dial(peer, options)
|
|
514
|
-
return connection.newStream(protocols)
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* @async
|
|
519
|
-
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
|
520
|
-
* @param {object} [options]
|
|
521
|
-
* @returns {Promise<Connection>}
|
|
522
|
-
*/
|
|
523
|
-
async _dial (peer, options) {
|
|
524
|
-
const { id, multiaddrs } = getPeer(peer)
|
|
525
|
-
|
|
526
|
-
if (id.equals(this.peerId)) {
|
|
527
|
-
throw errCode(new Error('Cannot dial self'), codes.ERR_DIALED_SELF)
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
let connection = this.connectionManager.get(id)
|
|
531
|
-
|
|
532
|
-
if (!connection) {
|
|
533
|
-
connection = await this.dialer.connectToPeer(peer, options)
|
|
534
|
-
} else if (multiaddrs) {
|
|
535
|
-
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
return connection
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/**
|
|
542
|
-
* Get a deduplicated list of peer advertising multiaddrs by concatenating
|
|
543
|
-
* the listen addresses used by transports with any configured
|
|
544
|
-
* announce addresses as well as observed addresses reported by peers.
|
|
545
|
-
*
|
|
546
|
-
* If Announce addrs are specified, configured listen addresses will be
|
|
547
|
-
* ignored though observed addresses will still be included.
|
|
548
|
-
*
|
|
549
|
-
* @returns {Multiaddr[]}
|
|
550
|
-
*/
|
|
551
|
-
get multiaddrs () {
|
|
552
|
-
let addrs = this.addressManager.getAnnounceAddrs().map(ma => ma.toString())
|
|
553
|
-
|
|
554
|
-
if (!addrs.length) {
|
|
555
|
-
// no configured announce addrs, add configured listen addresses
|
|
556
|
-
addrs = this.transportManager.getAddrs().map(ma => ma.toString())
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
addrs = addrs.concat(this.addressManager.getObservedAddrs().map(ma => ma.toString()))
|
|
560
|
-
|
|
561
|
-
const announceFilter = this._options.addresses.announceFilter
|
|
562
|
-
|
|
563
|
-
// dedupe multiaddrs
|
|
564
|
-
const addrSet = new Set(addrs)
|
|
565
|
-
|
|
566
|
-
// Create advertising list
|
|
567
|
-
return announceFilter(Array.from(addrSet).map(str => new Multiaddr(str)))
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
/**
|
|
571
|
-
* Disconnects all connections to the given `peer`
|
|
572
|
-
*
|
|
573
|
-
* @param {PeerId|Multiaddr|string} peer - the peer to close connections to
|
|
574
|
-
* @returns {Promise<void>}
|
|
575
|
-
*/
|
|
576
|
-
async hangUp (peer) {
|
|
577
|
-
const { id } = getPeer(peer)
|
|
578
|
-
|
|
579
|
-
const connections = this.connectionManager.connections.get(id.toB58String())
|
|
580
|
-
|
|
581
|
-
if (!connections) {
|
|
582
|
-
return
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
await Promise.all(
|
|
586
|
-
connections.map(connection => {
|
|
587
|
-
return connection.close()
|
|
588
|
-
})
|
|
589
|
-
)
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/**
|
|
593
|
-
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
594
|
-
*
|
|
595
|
-
* @param {PeerId|Multiaddr} peer
|
|
596
|
-
* @param {string} key
|
|
597
|
-
* @returns {Promise<Uint8Array | null>}
|
|
598
|
-
*/
|
|
599
|
-
fetch (peer, key) {
|
|
600
|
-
return this.fetchService.fetch(peer, key)
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* Pings the given peer in order to obtain the operation latency.
|
|
605
|
-
*
|
|
606
|
-
* @param {PeerId|Multiaddr|string} peer - The peer to ping
|
|
607
|
-
* @returns {Promise<number>}
|
|
608
|
-
*/
|
|
609
|
-
ping (peer) {
|
|
610
|
-
const { id, multiaddrs } = getPeer(peer)
|
|
611
|
-
|
|
612
|
-
// If received multiaddr, ping it
|
|
613
|
-
if (multiaddrs) {
|
|
614
|
-
return this.pingService.ping(multiaddrs[0])
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
return this.pingService.ping(id)
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
/**
|
|
621
|
-
* Registers the `handler` for each protocol
|
|
622
|
-
*
|
|
623
|
-
* @param {string[]|string} protocols
|
|
624
|
-
* @param {(props: HandlerProps) => void} handler
|
|
625
|
-
*/
|
|
626
|
-
async handle (protocols, handler) {
|
|
627
|
-
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
628
|
-
protocols.forEach(protocol => {
|
|
629
|
-
this.upgrader.protocols.set(protocol, handler)
|
|
630
|
-
})
|
|
631
|
-
|
|
632
|
-
// Add new protocols to self protocols in the Protobook
|
|
633
|
-
await this.peerStore.protoBook.add(this.peerId, protocols)
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* Removes the handler for each protocol. The protocol
|
|
638
|
-
* will no longer be supported on streams.
|
|
639
|
-
*
|
|
640
|
-
* @param {string[]|string} protocols
|
|
641
|
-
*/
|
|
642
|
-
async unhandle (protocols) {
|
|
643
|
-
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
644
|
-
protocols.forEach(protocol => {
|
|
645
|
-
this.upgrader.protocols.delete(protocol)
|
|
646
|
-
})
|
|
647
|
-
|
|
648
|
-
// Remove protocols from self protocols in the Protobook
|
|
649
|
-
await this.peerStore.protoBook.remove(this.peerId, protocols)
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
async _onStarting () {
|
|
653
|
-
// Listen on the provided transports for the provided addresses
|
|
654
|
-
const addrs = this.addressManager.getListenAddrs()
|
|
655
|
-
await this.transportManager.listen(addrs)
|
|
656
|
-
|
|
657
|
-
// Manage your NATs
|
|
658
|
-
this.natManager.start()
|
|
659
|
-
|
|
660
|
-
if (this._config.pubsub.enabled) {
|
|
661
|
-
this.pubsub && await this.pubsub.start()
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
// DHT subsystem
|
|
665
|
-
if (this._config.dht.enabled) {
|
|
666
|
-
this._dht && await this._dht.start()
|
|
667
|
-
|
|
668
|
-
// TODO: this should be modified once random-walk is used as
|
|
669
|
-
// the other discovery modules
|
|
670
|
-
this._dht.on('peer', this._onDiscoveryPeer)
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
// Start metrics if present
|
|
674
|
-
this.metrics && this.metrics.start()
|
|
675
|
-
|
|
676
|
-
if (this.identifyService) {
|
|
677
|
-
await this.identifyService.start()
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Called when libp2p has started and before it returns
|
|
683
|
-
*
|
|
684
|
-
* @private
|
|
685
|
-
*/
|
|
686
|
-
async _onDidStart () {
|
|
687
|
-
this._isStarted = true
|
|
688
|
-
|
|
689
|
-
this.peerStore.on('peer', peerId => {
|
|
690
|
-
this.emit('peer:discovery', peerId)
|
|
691
|
-
this._maybeConnect(peerId).catch(err => {
|
|
692
|
-
log.error(err)
|
|
693
|
-
})
|
|
694
|
-
})
|
|
695
|
-
|
|
696
|
-
// Once we start, emit any peers we may have already discovered
|
|
697
|
-
// TODO: this should be removed, as we already discovered these peers in the past
|
|
698
|
-
for await (const peer of this.peerStore.getPeers()) {
|
|
699
|
-
this.emit('peer:discovery', peer.id)
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
this.connectionManager.start()
|
|
703
|
-
await this._autodialler.start()
|
|
704
|
-
|
|
705
|
-
// Peer discovery
|
|
706
|
-
await this._setupPeerDiscovery()
|
|
707
|
-
|
|
708
|
-
// Relay
|
|
709
|
-
this.relay && this.relay.start()
|
|
710
|
-
|
|
711
|
-
this.peerRouting.start()
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
/**
|
|
715
|
-
* Called whenever peer discovery services emit `peer` events.
|
|
716
|
-
* Known peers may be emitted.
|
|
717
|
-
*
|
|
718
|
-
* @private
|
|
719
|
-
* @param {{ id: PeerId, multiaddrs: Multiaddr[], protocols: string[] }} peer
|
|
720
|
-
*/
|
|
721
|
-
_onDiscoveryPeer (peer) {
|
|
722
|
-
if (peer.id.toB58String() === this.peerId.toB58String()) {
|
|
723
|
-
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
|
724
|
-
return
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
|
|
728
|
-
peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
/**
|
|
732
|
-
* Will dial to the given `peerId` if the current number of
|
|
733
|
-
* connected peers is less than the configured `ConnectionManager`
|
|
734
|
-
* minConnections.
|
|
735
|
-
*
|
|
736
|
-
* @private
|
|
737
|
-
* @param {PeerId} peerId
|
|
738
|
-
*/
|
|
739
|
-
async _maybeConnect (peerId) {
|
|
740
|
-
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
|
741
|
-
if (this._config.peerDiscovery.autoDial === true && !this.connectionManager.get(peerId)) {
|
|
742
|
-
const minConnections = this._options.connectionManager.minConnections || 0
|
|
743
|
-
if (minConnections > this.connectionManager.size) {
|
|
744
|
-
log('connecting to discovered peer %s', peerId.toB58String())
|
|
745
|
-
try {
|
|
746
|
-
await this.dialer.connectToPeer(peerId)
|
|
747
|
-
} catch (/** @type {any} */ err) {
|
|
748
|
-
log.error(`could not connect to discovered peer ${peerId.toB58String()} with ${err}`)
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
/**
|
|
755
|
-
* Initializes and starts peer discovery services
|
|
756
|
-
*
|
|
757
|
-
* @async
|
|
758
|
-
* @private
|
|
759
|
-
*/
|
|
760
|
-
async _setupPeerDiscovery () {
|
|
761
|
-
/**
|
|
762
|
-
* @param {PeerDiscoveryFactory} DiscoveryService
|
|
763
|
-
*/
|
|
764
|
-
const setupService = (DiscoveryService) => {
|
|
765
|
-
let config = {
|
|
766
|
-
enabled: true // on by default
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
if (DiscoveryService.tag &&
|
|
770
|
-
this._config.peerDiscovery &&
|
|
771
|
-
this._config.peerDiscovery[DiscoveryService.tag]) {
|
|
772
|
-
// @ts-ignore PeerDiscovery not understood as an Object for spread
|
|
773
|
-
config = { ...config, ...this._config.peerDiscovery[DiscoveryService.tag] }
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
if (config.enabled &&
|
|
777
|
-
!this._discovery.has(DiscoveryService.tag)) { // not already added
|
|
778
|
-
let discoveryService
|
|
779
|
-
|
|
780
|
-
if (typeof DiscoveryService === 'function') {
|
|
781
|
-
// @ts-ignore DiscoveryService has no constructor type inferred
|
|
782
|
-
discoveryService = new DiscoveryService(Object.assign({}, config, {
|
|
783
|
-
peerId: this.peerId,
|
|
784
|
-
libp2p: this
|
|
785
|
-
}))
|
|
786
|
-
} else {
|
|
787
|
-
discoveryService = DiscoveryService
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
discoveryService.on('peer', this._onDiscoveryPeer)
|
|
791
|
-
this._discovery.set(DiscoveryService.tag, discoveryService)
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
// Discovery modules
|
|
796
|
-
for (const DiscoveryService of this._modules.peerDiscovery || []) {
|
|
797
|
-
setupService(DiscoveryService)
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
// Transport modules with discovery
|
|
801
|
-
for (const Transport of this.transportManager.getTransports()) {
|
|
802
|
-
// @ts-ignore Transport interface does not include discovery
|
|
803
|
-
if (Transport.discovery) {
|
|
804
|
-
// @ts-ignore Transport interface does not include discovery
|
|
805
|
-
setupService(Transport.discovery)
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
await Promise.all(Array.from(this._discovery.values(), d => d.start()))
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
module.exports = Libp2p
|