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,133 +0,0 @@
|
|
|
1
|
-
import * as $protobuf from "protobufjs";
|
|
2
|
-
/** Properties of a PeerRecord. */
|
|
3
|
-
export interface IPeerRecord {
|
|
4
|
-
|
|
5
|
-
/** PeerRecord peerId */
|
|
6
|
-
peerId?: (Uint8Array|null);
|
|
7
|
-
|
|
8
|
-
/** PeerRecord seq */
|
|
9
|
-
seq?: (number|null);
|
|
10
|
-
|
|
11
|
-
/** PeerRecord addresses */
|
|
12
|
-
addresses?: (PeerRecord.IAddressInfo[]|null);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Represents a PeerRecord. */
|
|
16
|
-
export class PeerRecord implements IPeerRecord {
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Constructs a new PeerRecord.
|
|
20
|
-
* @param [p] Properties to set
|
|
21
|
-
*/
|
|
22
|
-
constructor(p?: IPeerRecord);
|
|
23
|
-
|
|
24
|
-
/** PeerRecord peerId. */
|
|
25
|
-
public peerId: Uint8Array;
|
|
26
|
-
|
|
27
|
-
/** PeerRecord seq. */
|
|
28
|
-
public seq: number;
|
|
29
|
-
|
|
30
|
-
/** PeerRecord addresses. */
|
|
31
|
-
public addresses: PeerRecord.IAddressInfo[];
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Encodes the specified PeerRecord message. Does not implicitly {@link PeerRecord.verify|verify} messages.
|
|
35
|
-
* @param m PeerRecord message or plain object to encode
|
|
36
|
-
* @param [w] Writer to encode to
|
|
37
|
-
* @returns Writer
|
|
38
|
-
*/
|
|
39
|
-
public static encode(m: IPeerRecord, w?: $protobuf.Writer): $protobuf.Writer;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Decodes a PeerRecord message from the specified reader or buffer.
|
|
43
|
-
* @param r Reader or buffer to decode from
|
|
44
|
-
* @param [l] Message length if known beforehand
|
|
45
|
-
* @returns PeerRecord
|
|
46
|
-
* @throws {Error} If the payload is not a reader or valid buffer
|
|
47
|
-
* @throws {$protobuf.util.ProtocolError} If required fields are missing
|
|
48
|
-
*/
|
|
49
|
-
public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerRecord;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Creates a PeerRecord message from a plain object. Also converts values to their respective internal types.
|
|
53
|
-
* @param d Plain object
|
|
54
|
-
* @returns PeerRecord
|
|
55
|
-
*/
|
|
56
|
-
public static fromObject(d: { [k: string]: any }): PeerRecord;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Creates a plain object from a PeerRecord message. Also converts values to other types if specified.
|
|
60
|
-
* @param m PeerRecord
|
|
61
|
-
* @param [o] Conversion options
|
|
62
|
-
* @returns Plain object
|
|
63
|
-
*/
|
|
64
|
-
public static toObject(m: PeerRecord, o?: $protobuf.IConversionOptions): { [k: string]: any };
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Converts this PeerRecord to JSON.
|
|
68
|
-
* @returns JSON object
|
|
69
|
-
*/
|
|
70
|
-
public toJSON(): { [k: string]: any };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export namespace PeerRecord {
|
|
74
|
-
|
|
75
|
-
/** Properties of an AddressInfo. */
|
|
76
|
-
interface IAddressInfo {
|
|
77
|
-
|
|
78
|
-
/** AddressInfo multiaddr */
|
|
79
|
-
multiaddr?: (Uint8Array|null);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Represents an AddressInfo. */
|
|
83
|
-
class AddressInfo implements IAddressInfo {
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Constructs a new AddressInfo.
|
|
87
|
-
* @param [p] Properties to set
|
|
88
|
-
*/
|
|
89
|
-
constructor(p?: PeerRecord.IAddressInfo);
|
|
90
|
-
|
|
91
|
-
/** AddressInfo multiaddr. */
|
|
92
|
-
public multiaddr: Uint8Array;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Encodes the specified AddressInfo message. Does not implicitly {@link PeerRecord.AddressInfo.verify|verify} messages.
|
|
96
|
-
* @param m AddressInfo message or plain object to encode
|
|
97
|
-
* @param [w] Writer to encode to
|
|
98
|
-
* @returns Writer
|
|
99
|
-
*/
|
|
100
|
-
public static encode(m: PeerRecord.IAddressInfo, w?: $protobuf.Writer): $protobuf.Writer;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Decodes an AddressInfo message from the specified reader or buffer.
|
|
104
|
-
* @param r Reader or buffer to decode from
|
|
105
|
-
* @param [l] Message length if known beforehand
|
|
106
|
-
* @returns AddressInfo
|
|
107
|
-
* @throws {Error} If the payload is not a reader or valid buffer
|
|
108
|
-
* @throws {$protobuf.util.ProtocolError} If required fields are missing
|
|
109
|
-
*/
|
|
110
|
-
public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerRecord.AddressInfo;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Creates an AddressInfo message from a plain object. Also converts values to their respective internal types.
|
|
114
|
-
* @param d Plain object
|
|
115
|
-
* @returns AddressInfo
|
|
116
|
-
*/
|
|
117
|
-
public static fromObject(d: { [k: string]: any }): PeerRecord.AddressInfo;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Creates a plain object from an AddressInfo message. Also converts values to other types if specified.
|
|
121
|
-
* @param m AddressInfo
|
|
122
|
-
* @param [o] Conversion options
|
|
123
|
-
* @returns Plain object
|
|
124
|
-
*/
|
|
125
|
-
public static toObject(m: PeerRecord.AddressInfo, o?: $protobuf.IConversionOptions): { [k: string]: any };
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Converts this AddressInfo to JSON.
|
|
129
|
-
* @returns JSON object
|
|
130
|
-
*/
|
|
131
|
-
public toJSON(): { [k: string]: any };
|
|
132
|
-
}
|
|
133
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type Libp2p = import('../');
|
|
2
|
-
/**
|
|
3
|
-
* @typedef {import('../')} Libp2p
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Create (or update if existing) self peer record and store it in the AddressBook.
|
|
7
|
-
*
|
|
8
|
-
* @param {Libp2p} libp2p
|
|
9
|
-
* @returns {Promise<void>}
|
|
10
|
-
*/
|
|
11
|
-
export function updateSelfPeerRecord(libp2p: Libp2p): Promise<void>;
|
|
12
|
-
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/record/utils.js"],"names":[],"mappings":"qBAMa,OAAO,KAAK,CAAC;AAD1B;;GAEG;AAEH;;;;;GAKG;AACH,6CAHW,MAAM,GACJ,QAAQ,IAAI,CAAC,CASzB"}
|
package/dist/src/types.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type PeerId from 'peer-id';
|
|
2
|
-
import type { Multiaddr } from 'multiaddr';
|
|
3
|
-
import type { MultiaddrConnection } from 'libp2p-interfaces/src/transport/types';
|
|
4
|
-
export interface ConnectionGater {
|
|
5
|
-
/**
|
|
6
|
-
* denyDialMultiaddr tests whether we're permitted to Dial the
|
|
7
|
-
* specified peer.
|
|
8
|
-
*
|
|
9
|
-
* This is called by the dialer.connectToPeer implementation before
|
|
10
|
-
* dialling a peer.
|
|
11
|
-
*
|
|
12
|
-
* Return true to prevent dialing the passed peer.
|
|
13
|
-
*/
|
|
14
|
-
denyDialPeer: (peerId: PeerId) => Promise<boolean>;
|
|
15
|
-
/**
|
|
16
|
-
* denyDialMultiaddr tests whether we're permitted to dial the specified
|
|
17
|
-
* multiaddr for the given peer.
|
|
18
|
-
*
|
|
19
|
-
* This is called by the dialer.connectToPeer implementation after it has
|
|
20
|
-
* resolved the peer's addrs, and prior to dialling each.
|
|
21
|
-
*
|
|
22
|
-
* Return true to prevent dialing the passed peer on the passed multiaddr.
|
|
23
|
-
*/
|
|
24
|
-
denyDialMultiaddr: (peerId: PeerId, multiaddr: Multiaddr) => Promise<boolean>;
|
|
25
|
-
/**
|
|
26
|
-
* denyInboundConnection tests whether an incipient inbound connection is allowed.
|
|
27
|
-
*
|
|
28
|
-
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
|
|
29
|
-
* Bluetooth), straight after it has accepted a connection from its socket.
|
|
30
|
-
*
|
|
31
|
-
* Return true to deny the incoming passed connection.
|
|
32
|
-
*/
|
|
33
|
-
denyInboundConnection: (maConn: MultiaddrConnection) => Promise<boolean>;
|
|
34
|
-
/**
|
|
35
|
-
* denyOutboundConnection tests whether an incipient outbound connection is allowed.
|
|
36
|
-
*
|
|
37
|
-
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
|
|
38
|
-
* Bluetooth), straight after it has created a connection with its socket.
|
|
39
|
-
*
|
|
40
|
-
* Return true to deny the incoming passed connection.
|
|
41
|
-
*/
|
|
42
|
-
denyOutboundConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
43
|
-
/**
|
|
44
|
-
* denyInboundEncryptedConnection tests whether a given connection, now encrypted,
|
|
45
|
-
* is allowed.
|
|
46
|
-
*
|
|
47
|
-
* This is called by the upgrader, after it has performed the security
|
|
48
|
-
* handshake, and before it negotiates the muxer, or by the directly by the
|
|
49
|
-
* transport, at the exact same checkpoint.
|
|
50
|
-
*
|
|
51
|
-
* Return true to deny the passed secured connection.
|
|
52
|
-
*/
|
|
53
|
-
denyInboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
54
|
-
/**
|
|
55
|
-
* denyOutboundEncryptedConnection tests whether a given connection, now encrypted,
|
|
56
|
-
* is allowed.
|
|
57
|
-
*
|
|
58
|
-
* This is called by the upgrader, after it has performed the security
|
|
59
|
-
* handshake, and before it negotiates the muxer, or by the directly by the
|
|
60
|
-
* transport, at the exact same checkpoint.
|
|
61
|
-
*
|
|
62
|
-
* Return true to deny the passed secured connection.
|
|
63
|
-
*/
|
|
64
|
-
denyOutboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
65
|
-
/**
|
|
66
|
-
* denyInboundUpgradedConnection tests whether a fully capable connection is allowed.
|
|
67
|
-
*
|
|
68
|
-
* This is called after encryption has been negotiated and the connection has been
|
|
69
|
-
* multiplexed, if a multiplexer is configured.
|
|
70
|
-
*
|
|
71
|
-
* Return true to deny the passed upgraded connection.
|
|
72
|
-
*/
|
|
73
|
-
denyInboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
74
|
-
/**
|
|
75
|
-
* denyOutboundUpgradedConnection tests whether a fully capable connection is allowed.
|
|
76
|
-
*
|
|
77
|
-
* This is called after encryption has been negotiated and the connection has been
|
|
78
|
-
* multiplexed, if a multiplexer is configured.
|
|
79
|
-
*
|
|
80
|
-
* Return true to deny the passed upgraded connection.
|
|
81
|
-
*/
|
|
82
|
-
denyOutboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
83
|
-
/**
|
|
84
|
-
* Used by the address book to filter passed addresses.
|
|
85
|
-
*
|
|
86
|
-
* Return true to allow storing the passed multiaddr for the passed peer.
|
|
87
|
-
*/
|
|
88
|
-
filterMultiaddrForPeer: (peer: PeerId, multiaddr: Multiaddr) => Promise<boolean>;
|
|
89
|
-
}
|
|
90
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/src/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,SAAS,CAAA;AACjC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAA;AAEhF,MAAM,WAAW,eAAe;IAC9B;;;;;;;;OAQG;IACH,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAElD;;;;;;;;OAQG;IACH,iBAAiB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAE7E;;;;;;;OAOG;IACH,qBAAqB,EAAE,CAAC,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAExE;;;;;;;OAOG;IACH,sBAAsB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEzF;;;;;;;;;OASG;IACH,8BAA8B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEjG;;;;;;;;;OASG;IACH,+BAA+B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAElG;;;;;;;OAOG;IACH,6BAA6B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEhG;;;;;;;OAOG;IACH,8BAA8B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEjG;;;;OAIG;IACH,sBAAsB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CACjF"}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { EventEmitter } = require('events')
|
|
4
|
-
const { Multiaddr } = require('multiaddr')
|
|
5
|
-
const PeerId = require('peer-id')
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {Object} AddressManagerOptions
|
|
9
|
-
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
|
|
10
|
-
* @property {string[]} [announce = []] - list of multiaddrs string representation to announce.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @fires AddressManager#change:addresses Emitted when a addresses change.
|
|
15
|
-
*/
|
|
16
|
-
class AddressManager extends EventEmitter {
|
|
17
|
-
/**
|
|
18
|
-
* Responsible for managing the peer addresses.
|
|
19
|
-
* Peers can specify their listen and announce addresses.
|
|
20
|
-
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
|
21
|
-
* while the announce addresses will be used for the peer addresses' to other peers in the network.
|
|
22
|
-
*
|
|
23
|
-
* @class
|
|
24
|
-
* @param {PeerId} peerId - The Peer ID of the node
|
|
25
|
-
* @param {object} [options]
|
|
26
|
-
* @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
|
|
27
|
-
* @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
|
|
28
|
-
*/
|
|
29
|
-
constructor (peerId, { listen = [], announce = [] } = {}) {
|
|
30
|
-
super()
|
|
31
|
-
|
|
32
|
-
this.peerId = peerId
|
|
33
|
-
this.listen = new Set(listen.map(ma => ma.toString()))
|
|
34
|
-
this.announce = new Set(announce.map(ma => ma.toString()))
|
|
35
|
-
this.observed = new Set()
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Get peer listen multiaddrs.
|
|
40
|
-
*
|
|
41
|
-
* @returns {Multiaddr[]}
|
|
42
|
-
*/
|
|
43
|
-
getListenAddrs () {
|
|
44
|
-
return Array.from(this.listen).map((a) => new Multiaddr(a))
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get peer announcing multiaddrs.
|
|
49
|
-
*
|
|
50
|
-
* @returns {Multiaddr[]}
|
|
51
|
-
*/
|
|
52
|
-
getAnnounceAddrs () {
|
|
53
|
-
return Array.from(this.announce).map((a) => new Multiaddr(a))
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get observed multiaddrs.
|
|
58
|
-
*
|
|
59
|
-
* @returns {Array<Multiaddr>}
|
|
60
|
-
*/
|
|
61
|
-
getObservedAddrs () {
|
|
62
|
-
return Array.from(this.observed).map((a) => new Multiaddr(a))
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Add peer observed addresses
|
|
67
|
-
*
|
|
68
|
-
* @param {string | Multiaddr} addr
|
|
69
|
-
*/
|
|
70
|
-
addObservedAddr (addr) {
|
|
71
|
-
let ma = new Multiaddr(addr)
|
|
72
|
-
const remotePeer = ma.getPeerId()
|
|
73
|
-
|
|
74
|
-
// strip our peer id if it has been passed
|
|
75
|
-
if (remotePeer) {
|
|
76
|
-
const remotePeerId = PeerId.createFromB58String(remotePeer)
|
|
77
|
-
|
|
78
|
-
// use same encoding for comparison
|
|
79
|
-
if (remotePeerId.equals(this.peerId)) {
|
|
80
|
-
ma = ma.decapsulate(new Multiaddr(`/p2p/${this.peerId}`))
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const addrString = ma.toString()
|
|
85
|
-
|
|
86
|
-
// do not trigger the change:addresses event if we already know about this address
|
|
87
|
-
if (this.observed.has(addrString)) {
|
|
88
|
-
return
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.observed.add(addrString)
|
|
92
|
-
this.emit('change:addresses')
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
module.exports = AddressManager
|
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const log = Object.assign(debug('libp2p:auto-relay'), {
|
|
5
|
-
error: debug('libp2p:auto-relay:err')
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
|
9
|
-
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
|
|
10
|
-
const { Multiaddr } = require('multiaddr')
|
|
11
|
-
const all = require('it-all')
|
|
12
|
-
|
|
13
|
-
const { relay: multicodec } = require('./multicodec')
|
|
14
|
-
const { canHop } = require('./circuit/hop')
|
|
15
|
-
const { namespaceToCid } = require('./utils')
|
|
16
|
-
const {
|
|
17
|
-
CIRCUIT_PROTO_CODE,
|
|
18
|
-
HOP_METADATA_KEY,
|
|
19
|
-
HOP_METADATA_VALUE,
|
|
20
|
-
RELAY_RENDEZVOUS_NS
|
|
21
|
-
} = require('./constants')
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
25
|
-
* @typedef {import('../peer-store/types').Address} Address
|
|
26
|
-
* @typedef {import('peer-id')} PeerId
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @typedef {Object} AutoRelayProperties
|
|
31
|
-
* @property {import('../')} libp2p
|
|
32
|
-
*
|
|
33
|
-
* @typedef {Object} AutoRelayOptions
|
|
34
|
-
* @property {number} [maxListeners = 1] - maximum number of relays to listen.
|
|
35
|
-
* @property {(error: Error, msg?: string) => {}} [onError]
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
class AutoRelay {
|
|
39
|
-
/**
|
|
40
|
-
* Creates an instance of AutoRelay.
|
|
41
|
-
*
|
|
42
|
-
* @class
|
|
43
|
-
* @param {AutoRelayProperties & AutoRelayOptions} props
|
|
44
|
-
*/
|
|
45
|
-
constructor ({ libp2p, maxListeners = 1, onError }) {
|
|
46
|
-
this._libp2p = libp2p
|
|
47
|
-
this._peerId = libp2p.peerId
|
|
48
|
-
this._peerStore = libp2p.peerStore
|
|
49
|
-
this._connectionManager = libp2p.connectionManager
|
|
50
|
-
this._transportManager = libp2p.transportManager
|
|
51
|
-
this._addressSorter = libp2p.dialer.addressSorter
|
|
52
|
-
|
|
53
|
-
this.maxListeners = maxListeners
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @type {Set<string>}
|
|
57
|
-
*/
|
|
58
|
-
this._listenRelays = new Set()
|
|
59
|
-
|
|
60
|
-
this._onProtocolChange = this._onProtocolChange.bind(this)
|
|
61
|
-
this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
|
|
62
|
-
|
|
63
|
-
this._peerStore.on('change:protocols', this._onProtocolChange)
|
|
64
|
-
this._connectionManager.on('peer:disconnect', this._onPeerDisconnected)
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @param {Error} error
|
|
68
|
-
* @param {string} [msg]
|
|
69
|
-
*/
|
|
70
|
-
this._onError = (error, msg) => {
|
|
71
|
-
log.error(msg || error)
|
|
72
|
-
onError && onError(error, msg)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Check if a peer supports the relay protocol.
|
|
78
|
-
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
|
79
|
-
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
|
80
|
-
* inside the threshold.
|
|
81
|
-
*
|
|
82
|
-
* @param {Object} props
|
|
83
|
-
* @param {PeerId} props.peerId
|
|
84
|
-
* @param {string[]} props.protocols
|
|
85
|
-
* @returns {Promise<void>}
|
|
86
|
-
*/
|
|
87
|
-
async _onProtocolChange ({ peerId, protocols }) {
|
|
88
|
-
const id = peerId.toB58String()
|
|
89
|
-
|
|
90
|
-
// Check if it has the protocol
|
|
91
|
-
const hasProtocol = protocols.find(protocol => protocol === multicodec)
|
|
92
|
-
|
|
93
|
-
// If no protocol, check if we were keeping the peer before as a listenRelay
|
|
94
|
-
if (!hasProtocol && this._listenRelays.has(id)) {
|
|
95
|
-
await this._removeListenRelay(id)
|
|
96
|
-
return
|
|
97
|
-
} else if (!hasProtocol || this._listenRelays.has(id)) {
|
|
98
|
-
return
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
|
102
|
-
try {
|
|
103
|
-
const connection = this._connectionManager.get(peerId)
|
|
104
|
-
if (!connection) {
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Do not hop on a relayed connection
|
|
109
|
-
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
|
110
|
-
log(`relayed connection to ${id} will not be used to hop on`)
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const supportsHop = await canHop({ connection })
|
|
115
|
-
|
|
116
|
-
if (supportsHop) {
|
|
117
|
-
await this._peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
|
|
118
|
-
await this._addListenRelay(connection, id)
|
|
119
|
-
}
|
|
120
|
-
} catch (/** @type {any} */ err) {
|
|
121
|
-
this._onError(err)
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Peer disconnects.
|
|
127
|
-
*
|
|
128
|
-
* @param {Connection} connection - connection to the peer
|
|
129
|
-
*/
|
|
130
|
-
_onPeerDisconnected (connection) {
|
|
131
|
-
const peerId = connection.remotePeer
|
|
132
|
-
const id = peerId.toB58String()
|
|
133
|
-
|
|
134
|
-
// Not listening on this relay
|
|
135
|
-
if (!this._listenRelays.has(id)) {
|
|
136
|
-
return
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
this._removeListenRelay(id).catch(err => {
|
|
140
|
-
log.error(err)
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Attempt to listen on the given relay connection.
|
|
146
|
-
*
|
|
147
|
-
* @private
|
|
148
|
-
* @param {Connection} connection - connection to the peer
|
|
149
|
-
* @param {string} id - peer identifier string
|
|
150
|
-
* @returns {Promise<void>}
|
|
151
|
-
*/
|
|
152
|
-
async _addListenRelay (connection, id) {
|
|
153
|
-
try {
|
|
154
|
-
// Check if already listening on enough relays
|
|
155
|
-
if (this._listenRelays.size >= this.maxListeners) {
|
|
156
|
-
return
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Get peer known addresses and sort them per public addresses first
|
|
160
|
-
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
|
|
161
|
-
connection.remotePeer, this._addressSorter
|
|
162
|
-
)
|
|
163
|
-
|
|
164
|
-
// Attempt to listen on relay
|
|
165
|
-
const result = await Promise.all(
|
|
166
|
-
remoteAddrs.map(async addr => {
|
|
167
|
-
try {
|
|
168
|
-
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
|
169
|
-
await this._transportManager.listen([new Multiaddr(`${addr.toString()}/p2p-circuit`)])
|
|
170
|
-
return true
|
|
171
|
-
} catch (/** @type {any} */ err) {
|
|
172
|
-
this._onError(err)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return false
|
|
176
|
-
})
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
if (result.includes(true)) {
|
|
180
|
-
this._listenRelays.add(id)
|
|
181
|
-
}
|
|
182
|
-
} catch (/** @type {any} */ err) {
|
|
183
|
-
this._onError(err)
|
|
184
|
-
this._listenRelays.delete(id)
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Remove listen relay.
|
|
190
|
-
*
|
|
191
|
-
* @private
|
|
192
|
-
* @param {string} id - peer identifier string.
|
|
193
|
-
*/
|
|
194
|
-
async _removeListenRelay (id) {
|
|
195
|
-
if (this._listenRelays.delete(id)) {
|
|
196
|
-
// TODO: this should be responsibility of the connMgr
|
|
197
|
-
await this._listenOnAvailableHopRelays([id])
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Try to listen on available hop relay connections.
|
|
203
|
-
* The following order will happen while we do not have enough relays.
|
|
204
|
-
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
|
205
|
-
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
|
206
|
-
* 3. Search the network.
|
|
207
|
-
*
|
|
208
|
-
* @param {string[]} [peersToIgnore]
|
|
209
|
-
*/
|
|
210
|
-
async _listenOnAvailableHopRelays (peersToIgnore = []) {
|
|
211
|
-
// TODO: The peer redial issue on disconnect should be handled by connection gating
|
|
212
|
-
// Check if already listening on enough relays
|
|
213
|
-
if (this._listenRelays.size >= this.maxListeners) {
|
|
214
|
-
return
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const knownHopsToDial = []
|
|
218
|
-
const peers = await all(this._peerStore.getPeers())
|
|
219
|
-
|
|
220
|
-
// Check if we have known hop peers to use and attempt to listen on the already connected
|
|
221
|
-
for await (const { id, metadata } of peers) {
|
|
222
|
-
const idStr = id.toB58String()
|
|
223
|
-
|
|
224
|
-
// Continue to next if listening on this or peer to ignore
|
|
225
|
-
if (this._listenRelays.has(idStr)) {
|
|
226
|
-
continue
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (peersToIgnore.includes(idStr)) {
|
|
230
|
-
continue
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const supportsHop = metadata.get(HOP_METADATA_KEY)
|
|
234
|
-
|
|
235
|
-
// Continue to next if it does not support Hop
|
|
236
|
-
if (!supportsHop || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
|
|
237
|
-
continue
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const connection = this._connectionManager.get(id)
|
|
241
|
-
|
|
242
|
-
// If not connected, store for possible later use.
|
|
243
|
-
if (!connection) {
|
|
244
|
-
knownHopsToDial.push(id)
|
|
245
|
-
continue
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
await this._addListenRelay(connection, idStr)
|
|
249
|
-
|
|
250
|
-
// Check if already listening on enough relays
|
|
251
|
-
if (this._listenRelays.size >= this.maxListeners) {
|
|
252
|
-
return
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Try to listen on known peers that are not connected
|
|
257
|
-
for (const peerId of knownHopsToDial) {
|
|
258
|
-
await this._tryToListenOnRelay(peerId)
|
|
259
|
-
|
|
260
|
-
// Check if already listening on enough relays
|
|
261
|
-
if (this._listenRelays.size >= this.maxListeners) {
|
|
262
|
-
return
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// Try to find relays to hop on the network
|
|
267
|
-
try {
|
|
268
|
-
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
|
269
|
-
for await (const provider of this._libp2p.contentRouting.findProviders(cid)) {
|
|
270
|
-
if (!provider.multiaddrs.length) {
|
|
271
|
-
continue
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const peerId = provider.id
|
|
275
|
-
await this._peerStore.addressBook.add(peerId, provider.multiaddrs)
|
|
276
|
-
|
|
277
|
-
await this._tryToListenOnRelay(peerId)
|
|
278
|
-
|
|
279
|
-
// Check if already listening on enough relays
|
|
280
|
-
if (this._listenRelays.size >= this.maxListeners) {
|
|
281
|
-
return
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
} catch (/** @type {any} */ err) {
|
|
285
|
-
this._onError(err)
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* @param {PeerId} peerId
|
|
291
|
-
*/
|
|
292
|
-
async _tryToListenOnRelay (peerId) {
|
|
293
|
-
try {
|
|
294
|
-
const connection = await this._libp2p.dial(peerId)
|
|
295
|
-
await this._addListenRelay(connection, peerId.toB58String())
|
|
296
|
-
} catch (/** @type {any} */ err) {
|
|
297
|
-
this._onError(err, `could not connect and listen on known hop relay ${peerId.toB58String()}`)
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
module.exports = AutoRelay
|