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/dialer/index.js
DELETED
|
@@ -1,376 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const all = require('it-all')
|
|
5
|
-
const filter = require('it-filter')
|
|
6
|
-
const { pipe } = require('it-pipe')
|
|
7
|
-
const log = Object.assign(debug('libp2p:dialer'), {
|
|
8
|
-
error: debug('libp2p:dialer:err')
|
|
9
|
-
})
|
|
10
|
-
const errCode = require('err-code')
|
|
11
|
-
const { Multiaddr } = require('multiaddr')
|
|
12
|
-
const { TimeoutController } = require('timeout-abort-controller')
|
|
13
|
-
const { AbortError } = require('abortable-iterator')
|
|
14
|
-
const { anySignal } = require('any-signal')
|
|
15
|
-
// @ts-expect-error setMaxListeners is missing from the types
|
|
16
|
-
const { setMaxListeners } = require('events')
|
|
17
|
-
const DialRequest = require('./dial-request')
|
|
18
|
-
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
|
19
|
-
const getPeer = require('../get-peer')
|
|
20
|
-
const trackedMap = require('../metrics/tracked-map')
|
|
21
|
-
const { codes } = require('../errors')
|
|
22
|
-
const {
|
|
23
|
-
DIAL_TIMEOUT,
|
|
24
|
-
MAX_PARALLEL_DIALS,
|
|
25
|
-
MAX_PER_PEER_DIALS,
|
|
26
|
-
MAX_ADDRS_TO_DIAL
|
|
27
|
-
} = require('../constants')
|
|
28
|
-
|
|
29
|
-
const METRICS_COMPONENT = 'dialler'
|
|
30
|
-
const METRICS_PENDING_DIALS = 'pending-dials'
|
|
31
|
-
const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
35
|
-
* @typedef {import('peer-id')} PeerId
|
|
36
|
-
* @typedef {import('../peer-store/types').PeerStore} PeerStore
|
|
37
|
-
* @typedef {import('../peer-store/types').Address} Address
|
|
38
|
-
* @typedef {import('../transport-manager')} TransportManager
|
|
39
|
-
* @typedef {import('../types').ConnectionGater} ConnectionGater
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @typedef {Object} DialerProperties
|
|
44
|
-
* @property {PeerStore} peerStore
|
|
45
|
-
* @property {TransportManager} transportManager
|
|
46
|
-
* @property {ConnectionGater} connectionGater
|
|
47
|
-
*
|
|
48
|
-
* @typedef {(addr:Multiaddr) => Promise<string[]>} Resolver
|
|
49
|
-
*
|
|
50
|
-
* @typedef {Object} DialerOptions
|
|
51
|
-
* @property {(addresses: Address[]) => Address[]} [options.addressSorter = publicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
|
|
52
|
-
* @property {number} [maxParallelDials = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
|
|
53
|
-
* @property {number} [maxAddrsToDial = MAX_ADDRS_TO_DIAL] - Number of max addresses to dial for a given peer.
|
|
54
|
-
* @property {number} [maxDialsPerPeer = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
|
|
55
|
-
* @property {number} [dialTimeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
|
|
56
|
-
* @property {Record<string, Resolver>} [resolvers = {}] - multiaddr resolvers to use when dialing
|
|
57
|
-
* @property {import('../metrics')} [metrics]
|
|
58
|
-
*
|
|
59
|
-
* @typedef DialTarget
|
|
60
|
-
* @property {string} id
|
|
61
|
-
* @property {Multiaddr[]} addrs
|
|
62
|
-
*
|
|
63
|
-
* @typedef PendingDial
|
|
64
|
-
* @property {import('./dial-request')} dialRequest
|
|
65
|
-
* @property {import('timeout-abort-controller').TimeoutController} controller
|
|
66
|
-
* @property {Promise<Connection>} promise
|
|
67
|
-
* @property {function():void} destroy
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
class Dialer {
|
|
71
|
-
/**
|
|
72
|
-
* @class
|
|
73
|
-
* @param {DialerProperties & DialerOptions} options
|
|
74
|
-
*/
|
|
75
|
-
constructor ({
|
|
76
|
-
transportManager,
|
|
77
|
-
peerStore,
|
|
78
|
-
connectionGater,
|
|
79
|
-
addressSorter = publicAddressesFirst,
|
|
80
|
-
maxParallelDials = MAX_PARALLEL_DIALS,
|
|
81
|
-
maxAddrsToDial = MAX_ADDRS_TO_DIAL,
|
|
82
|
-
dialTimeout = DIAL_TIMEOUT,
|
|
83
|
-
maxDialsPerPeer = MAX_PER_PEER_DIALS,
|
|
84
|
-
resolvers = {},
|
|
85
|
-
metrics
|
|
86
|
-
}) {
|
|
87
|
-
this.connectionGater = connectionGater
|
|
88
|
-
this.transportManager = transportManager
|
|
89
|
-
this.peerStore = peerStore
|
|
90
|
-
this.addressSorter = addressSorter
|
|
91
|
-
this.maxParallelDials = maxParallelDials
|
|
92
|
-
this.maxAddrsToDial = maxAddrsToDial
|
|
93
|
-
this.timeout = dialTimeout
|
|
94
|
-
this.maxDialsPerPeer = maxDialsPerPeer
|
|
95
|
-
this.tokens = [...new Array(maxParallelDials)].map((_, index) => index)
|
|
96
|
-
|
|
97
|
-
/** @type {Map<string, PendingDial>} */
|
|
98
|
-
this._pendingDials = trackedMap({
|
|
99
|
-
component: METRICS_COMPONENT,
|
|
100
|
-
metric: METRICS_PENDING_DIALS,
|
|
101
|
-
metrics
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
/** @type {Map<string, { resolve: (value: any) => void, reject: (err: Error) => void}>} */
|
|
105
|
-
this._pendingDialTargets = trackedMap({
|
|
106
|
-
component: METRICS_COMPONENT,
|
|
107
|
-
metric: METRICS_PENDING_DIAL_TARGETS,
|
|
108
|
-
metrics
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
for (const [key, value] of Object.entries(resolvers)) {
|
|
112
|
-
Multiaddr.resolvers.set(key, value)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Clears any pending dials
|
|
118
|
-
*/
|
|
119
|
-
destroy () {
|
|
120
|
-
for (const dial of this._pendingDials.values()) {
|
|
121
|
-
try {
|
|
122
|
-
dial.controller.abort()
|
|
123
|
-
} catch (/** @type {any} */ err) {
|
|
124
|
-
log.error(err)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
this._pendingDials.clear()
|
|
128
|
-
|
|
129
|
-
for (const pendingTarget of this._pendingDialTargets.values()) {
|
|
130
|
-
pendingTarget.reject(new AbortError('Dialer was destroyed'))
|
|
131
|
-
}
|
|
132
|
-
this._pendingDialTargets.clear()
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Connects to a given `peer` by dialing all of its known addresses.
|
|
137
|
-
* The dial to the first address that is successfully able to upgrade a connection
|
|
138
|
-
* will be used.
|
|
139
|
-
*
|
|
140
|
-
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
|
141
|
-
* @param {object} [options]
|
|
142
|
-
* @param {AbortSignal} [options.signal] - An AbortController signal
|
|
143
|
-
* @returns {Promise<Connection>}
|
|
144
|
-
*/
|
|
145
|
-
async connectToPeer (peer, options = {}) {
|
|
146
|
-
const { id } = getPeer(peer)
|
|
147
|
-
|
|
148
|
-
if (await this.connectionGater.denyDialPeer(id)) {
|
|
149
|
-
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const dialTarget = await this._createCancellableDialTarget(peer)
|
|
153
|
-
|
|
154
|
-
if (!dialTarget.addrs.length) {
|
|
155
|
-
throw errCode(new Error('The dial request has no valid addresses'), codes.ERR_NO_VALID_ADDRESSES)
|
|
156
|
-
}
|
|
157
|
-
const pendingDial = this._pendingDials.get(dialTarget.id) || this._createPendingDial(dialTarget, options)
|
|
158
|
-
|
|
159
|
-
try {
|
|
160
|
-
const connection = await pendingDial.promise
|
|
161
|
-
log('dial succeeded to %s', dialTarget.id)
|
|
162
|
-
return connection
|
|
163
|
-
} catch (/** @type {any} */ err) {
|
|
164
|
-
// Error is a timeout
|
|
165
|
-
if (pendingDial.controller.signal.aborted) {
|
|
166
|
-
err.code = codes.ERR_TIMEOUT
|
|
167
|
-
}
|
|
168
|
-
log.error(err)
|
|
169
|
-
throw err
|
|
170
|
-
} finally {
|
|
171
|
-
pendingDial.destroy()
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Connects to a given `peer` by dialing all of its known addresses.
|
|
177
|
-
* The dial to the first address that is successfully able to upgrade a connection
|
|
178
|
-
* will be used.
|
|
179
|
-
*
|
|
180
|
-
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
|
181
|
-
* @returns {Promise<DialTarget>}
|
|
182
|
-
*/
|
|
183
|
-
async _createCancellableDialTarget (peer) {
|
|
184
|
-
// Make dial target promise cancellable
|
|
185
|
-
const id = `${(parseInt(String(Math.random() * 1e9), 10)).toString() + Date.now()}`
|
|
186
|
-
const cancellablePromise = new Promise((resolve, reject) => {
|
|
187
|
-
this._pendingDialTargets.set(id, { resolve, reject })
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
try {
|
|
191
|
-
const dialTarget = await Promise.race([
|
|
192
|
-
this._createDialTarget(peer),
|
|
193
|
-
cancellablePromise
|
|
194
|
-
])
|
|
195
|
-
|
|
196
|
-
return dialTarget
|
|
197
|
-
} finally {
|
|
198
|
-
this._pendingDialTargets.delete(id)
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Creates a DialTarget. The DialTarget is used to create and track
|
|
204
|
-
* the DialRequest to a given peer.
|
|
205
|
-
* If a multiaddr is received it should be the first address attempted.
|
|
206
|
-
* Multiaddrs not supported by the available transports will be filtered out.
|
|
207
|
-
*
|
|
208
|
-
* @private
|
|
209
|
-
* @param {PeerId|Multiaddr|string} peer - A PeerId or Multiaddr
|
|
210
|
-
* @returns {Promise<DialTarget>}
|
|
211
|
-
*/
|
|
212
|
-
async _createDialTarget (peer) {
|
|
213
|
-
const { id, multiaddrs } = getPeer(peer)
|
|
214
|
-
|
|
215
|
-
if (multiaddrs) {
|
|
216
|
-
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
let knownAddrs = await pipe(
|
|
220
|
-
await this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter),
|
|
221
|
-
(source) => filter(source, async (multiaddr) => {
|
|
222
|
-
return !(await this.connectionGater.denyDialMultiaddr(id, multiaddr))
|
|
223
|
-
}),
|
|
224
|
-
(source) => all(source)
|
|
225
|
-
)
|
|
226
|
-
|
|
227
|
-
// If received a multiaddr to dial, it should be the first to use
|
|
228
|
-
// But, if we know other multiaddrs for the peer, we should try them too.
|
|
229
|
-
if (Multiaddr.isMultiaddr(peer)) {
|
|
230
|
-
knownAddrs = knownAddrs.filter((addr) => !peer.equals(addr))
|
|
231
|
-
knownAddrs.unshift(peer)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/** @type {Multiaddr[]} */
|
|
235
|
-
const addrs = []
|
|
236
|
-
for (const a of knownAddrs) {
|
|
237
|
-
const resolvedAddrs = await this._resolve(a)
|
|
238
|
-
resolvedAddrs.forEach(ra => addrs.push(ra))
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Multiaddrs not supported by the available transports will be filtered out.
|
|
242
|
-
const supportedAddrs = addrs.filter(a => this.transportManager.transportForMultiaddr(a))
|
|
243
|
-
|
|
244
|
-
if (supportedAddrs.length > this.maxAddrsToDial) {
|
|
245
|
-
await this.peerStore.delete(id)
|
|
246
|
-
throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return {
|
|
250
|
-
id: id.toB58String(),
|
|
251
|
-
addrs: supportedAddrs
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Creates a PendingDial that wraps the underlying DialRequest
|
|
257
|
-
*
|
|
258
|
-
* @private
|
|
259
|
-
* @param {DialTarget} dialTarget
|
|
260
|
-
* @param {object} [options]
|
|
261
|
-
* @param {AbortSignal} [options.signal] - An AbortController signal
|
|
262
|
-
* @returns {PendingDial}
|
|
263
|
-
*/
|
|
264
|
-
_createPendingDial (dialTarget, options = {}) {
|
|
265
|
-
/**
|
|
266
|
-
* @param {Multiaddr} addr
|
|
267
|
-
* @param {{ signal: { aborted: any; }; }} options
|
|
268
|
-
*/
|
|
269
|
-
const dialAction = (addr, options) => {
|
|
270
|
-
if (options.signal.aborted) throw errCode(new Error('already aborted'), codes.ERR_ALREADY_ABORTED)
|
|
271
|
-
return this.transportManager.dial(addr, options)
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const dialRequest = new DialRequest({
|
|
275
|
-
addrs: dialTarget.addrs,
|
|
276
|
-
dialAction,
|
|
277
|
-
dialer: this
|
|
278
|
-
})
|
|
279
|
-
|
|
280
|
-
// Combine the timeout signal and options.signal, if provided
|
|
281
|
-
const timeoutController = new TimeoutController(this.timeout)
|
|
282
|
-
|
|
283
|
-
const signals = [timeoutController.signal]
|
|
284
|
-
options.signal && signals.push(options.signal)
|
|
285
|
-
const signal = anySignal(signals)
|
|
286
|
-
|
|
287
|
-
// this signal will potentially be used while dialing lots of
|
|
288
|
-
// peers so prevent MaxListenersExceededWarning appearing in the console
|
|
289
|
-
try {
|
|
290
|
-
// fails on node < 15.4
|
|
291
|
-
setMaxListeners && setMaxListeners(Infinity, signal)
|
|
292
|
-
} catch {}
|
|
293
|
-
|
|
294
|
-
const pendingDial = {
|
|
295
|
-
dialRequest,
|
|
296
|
-
controller: timeoutController,
|
|
297
|
-
promise: dialRequest.run({ ...options, signal }),
|
|
298
|
-
destroy: () => {
|
|
299
|
-
timeoutController.clear()
|
|
300
|
-
this._pendingDials.delete(dialTarget.id)
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
this._pendingDials.set(dialTarget.id, pendingDial)
|
|
304
|
-
|
|
305
|
-
return pendingDial
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* @param {number} num
|
|
310
|
-
*/
|
|
311
|
-
getTokens (num) {
|
|
312
|
-
const total = Math.min(num, this.maxDialsPerPeer, this.tokens.length)
|
|
313
|
-
const tokens = this.tokens.splice(0, total)
|
|
314
|
-
log('%d tokens request, returning %d, %d remaining', num, total, this.tokens.length)
|
|
315
|
-
return tokens
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* @param {number} token
|
|
320
|
-
*/
|
|
321
|
-
releaseToken (token) {
|
|
322
|
-
// Guard against duplicate releases
|
|
323
|
-
if (this.tokens.indexOf(token) > -1) return
|
|
324
|
-
log('token %d released', token)
|
|
325
|
-
this.tokens.push(token)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Resolve multiaddr recursively.
|
|
330
|
-
*
|
|
331
|
-
* @param {Multiaddr} ma
|
|
332
|
-
* @returns {Promise<Multiaddr[]>}
|
|
333
|
-
*/
|
|
334
|
-
async _resolve (ma) {
|
|
335
|
-
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
|
|
336
|
-
// Now only supporting resolve for dnsaddr
|
|
337
|
-
const resolvableProto = ma.protoNames().includes('dnsaddr')
|
|
338
|
-
|
|
339
|
-
// Multiaddr is not resolvable? End recursion!
|
|
340
|
-
if (!resolvableProto) {
|
|
341
|
-
return [ma]
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
const resolvedMultiaddrs = await this._resolveRecord(ma)
|
|
345
|
-
const recursiveMultiaddrs = await Promise.all(resolvedMultiaddrs.map((nm) => {
|
|
346
|
-
return this._resolve(nm)
|
|
347
|
-
}))
|
|
348
|
-
|
|
349
|
-
const addrs = recursiveMultiaddrs.flat()
|
|
350
|
-
return addrs.reduce((array, newM) => {
|
|
351
|
-
if (!array.find(m => m.equals(newM))) {
|
|
352
|
-
array.push(newM)
|
|
353
|
-
}
|
|
354
|
-
return array
|
|
355
|
-
}, /** @type {Multiaddr[]} */([]))
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Resolve a given multiaddr. If this fails, an empty array will be returned
|
|
360
|
-
*
|
|
361
|
-
* @param {Multiaddr} ma
|
|
362
|
-
* @returns {Promise<Multiaddr[]>}
|
|
363
|
-
*/
|
|
364
|
-
async _resolveRecord (ma) {
|
|
365
|
-
try {
|
|
366
|
-
ma = new Multiaddr(ma.toString()) // Use current multiaddr module
|
|
367
|
-
const multiaddrs = await ma.resolve()
|
|
368
|
-
return multiaddrs
|
|
369
|
-
} catch (_) {
|
|
370
|
-
log.error(`multiaddr ${ma} could not be resolved`)
|
|
371
|
-
return []
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
module.exports = Dialer
|
package/src/errors.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.messages = {
|
|
4
|
-
NOT_STARTED_YET: 'The libp2p node is not started yet',
|
|
5
|
-
DHT_DISABLED: 'DHT is not available',
|
|
6
|
-
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required',
|
|
7
|
-
NOT_FOUND: 'Not found'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
exports.codes = {
|
|
11
|
-
DHT_DISABLED: 'ERR_DHT_DISABLED',
|
|
12
|
-
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
|
|
13
|
-
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
|
|
14
|
-
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
|
|
15
|
-
ERR_PEER_DIAL_INTERCEPTED: 'ERR_PEER_DIAL_INTERCEPTED',
|
|
16
|
-
ERR_CONNECTION_INTERCEPTED: 'ERR_CONNECTION_INTERCEPTED',
|
|
17
|
-
ERR_INVALID_PROTOCOLS_FOR_STREAM: 'ERR_INVALID_PROTOCOLS_FOR_STREAM',
|
|
18
|
-
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
|
|
19
|
-
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
|
|
20
|
-
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
|
21
|
-
ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED',
|
|
22
|
-
ERR_TOO_MANY_ADDRESSES: 'ERR_TOO_MANY_ADDRESSES',
|
|
23
|
-
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
|
|
24
|
-
ERR_RELAYED_DIAL: 'ERR_RELAYED_DIAL',
|
|
25
|
-
ERR_DIALED_SELF: 'ERR_DIALED_SELF',
|
|
26
|
-
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
|
|
27
|
-
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
|
28
|
-
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
|
29
|
-
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
|
|
30
|
-
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
|
|
31
|
-
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
|
|
32
|
-
ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
|
|
33
|
-
ERR_INVALID_PEER: 'ERR_INVALID_PEER',
|
|
34
|
-
ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE',
|
|
35
|
-
ERR_NOT_FOUND: 'ERR_NOT_FOUND',
|
|
36
|
-
ERR_TIMEOUT: 'ERR_TIMEOUT',
|
|
37
|
-
ERR_TRANSPORT_UNAVAILABLE: 'ERR_TRANSPORT_UNAVAILABLE',
|
|
38
|
-
ERR_TRANSPORT_DIAL_FAILED: 'ERR_TRANSPORT_DIAL_FAILED',
|
|
39
|
-
ERR_UNSUPPORTED_PROTOCOL: 'ERR_UNSUPPORTED_PROTOCOL',
|
|
40
|
-
ERR_INVALID_MULTIADDR: 'ERR_INVALID_MULTIADDR',
|
|
41
|
-
ERR_SIGNATURE_NOT_VALID: 'ERR_SIGNATURE_NOT_VALID',
|
|
42
|
-
ERR_FIND_SELF: 'ERR_FIND_SELF',
|
|
43
|
-
ERR_NO_ROUTERS_AVAILABLE: 'ERR_NO_ROUTERS_AVAILABLE',
|
|
44
|
-
ERR_CONNECTION_NOT_MULTIPLEXED: 'ERR_CONNECTION_NOT_MULTIPLEXED',
|
|
45
|
-
ERR_NO_DIAL_TOKENS: 'ERR_NO_DIAL_TOKENS',
|
|
46
|
-
ERR_KEYCHAIN_REQUIRED: 'ERR_KEYCHAIN_REQUIRED',
|
|
47
|
-
ERR_INVALID_CMS: 'ERR_INVALID_CMS',
|
|
48
|
-
ERR_MISSING_KEYS: 'ERR_MISSING_KEYS',
|
|
49
|
-
ERR_NO_KEY: 'ERR_NO_KEY',
|
|
50
|
-
ERR_INVALID_KEY_NAME: 'ERR_INVALID_KEY_NAME',
|
|
51
|
-
ERR_INVALID_KEY_TYPE: 'ERR_INVALID_KEY_TYPE',
|
|
52
|
-
ERR_KEY_ALREADY_EXISTS: 'ERR_KEY_ALREADY_EXISTS',
|
|
53
|
-
ERR_INVALID_KEY_SIZE: 'ERR_INVALID_KEY_SIZE',
|
|
54
|
-
ERR_KEY_NOT_FOUND: 'ERR_KEY_NOT_FOUND',
|
|
55
|
-
ERR_OLD_KEY_NAME_INVALID: 'ERR_OLD_KEY_NAME_INVALID',
|
|
56
|
-
ERR_NEW_KEY_NAME_INVALID: 'ERR_NEW_KEY_NAME_INVALID',
|
|
57
|
-
ERR_PASSWORD_REQUIRED: 'ERR_PASSWORD_REQUIRED',
|
|
58
|
-
ERR_PEM_REQUIRED: 'ERR_PEM_REQUIRED',
|
|
59
|
-
ERR_CANNOT_READ_KEY: 'ERR_CANNOT_READ_KEY',
|
|
60
|
-
ERR_MISSING_PRIVATE_KEY: 'ERR_MISSING_PRIVATE_KEY',
|
|
61
|
-
ERR_INVALID_OLD_PASS_TYPE: 'ERR_INVALID_OLD_PASS_TYPE',
|
|
62
|
-
ERR_INVALID_NEW_PASS_TYPE: 'ERR_INVALID_NEW_PASS_TYPE',
|
|
63
|
-
ERR_INVALID_PASS_LENGTH: 'ERR_INVALID_PASS_LENGTH',
|
|
64
|
-
ERR_NOT_IMPLEMENTED: 'ERR_NOT_IMPLEMENTED',
|
|
65
|
-
ERR_WRONG_PING_ACK: 'ERR_WRONG_PING_ACK'
|
|
66
|
-
}
|
package/src/fetch/constants.js
DELETED
package/src/fetch/index.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const log = Object.assign(debug('libp2p:fetch'), {
|
|
5
|
-
error: debug('libp2p:fetch:err')
|
|
6
|
-
})
|
|
7
|
-
const errCode = require('err-code')
|
|
8
|
-
const { codes } = require('../errors')
|
|
9
|
-
const lp = require('it-length-prefixed')
|
|
10
|
-
const { FetchRequest, FetchResponse } = require('./proto')
|
|
11
|
-
// @ts-ignore it-handshake does not export types
|
|
12
|
-
const handshake = require('it-handshake')
|
|
13
|
-
const { PROTOCOL } = require('./constants')
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @typedef {import('../')} Libp2p
|
|
17
|
-
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
18
|
-
* @typedef {import('peer-id')} PeerId
|
|
19
|
-
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
|
20
|
-
* @typedef {(key: string) => Promise<Uint8Array | null>} LookupFunction
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A simple libp2p protocol for requesting a value corresponding to a key from a peer.
|
|
25
|
-
* Developers can register one or more lookup function for retrieving the value corresponding to
|
|
26
|
-
* a given key. Each lookup function must act on a distinct part of the overall key space, defined
|
|
27
|
-
* by a fixed prefix that all keys that should be routed to that lookup function will start with.
|
|
28
|
-
*/
|
|
29
|
-
class FetchProtocol {
|
|
30
|
-
/**
|
|
31
|
-
* @param {Libp2p} libp2p
|
|
32
|
-
*/
|
|
33
|
-
constructor (libp2p) {
|
|
34
|
-
this._lookupFunctions = new Map() // Maps key prefix to value lookup function
|
|
35
|
-
this._libp2p = libp2p
|
|
36
|
-
this.handleMessage = this.handleMessage.bind(this)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
41
|
-
*
|
|
42
|
-
* @param {PeerId|Multiaddr} peer
|
|
43
|
-
* @param {string} key
|
|
44
|
-
* @returns {Promise<Uint8Array | null>}
|
|
45
|
-
*/
|
|
46
|
-
async fetch (peer, key) {
|
|
47
|
-
// @ts-ignore multiaddr might not have toB58String
|
|
48
|
-
log('dialing %s to %s', this._protocol, peer.toB58String ? peer.toB58String() : peer)
|
|
49
|
-
|
|
50
|
-
const connection = await this._libp2p.dial(peer)
|
|
51
|
-
const { stream } = await connection.newStream(FetchProtocol.PROTOCOL)
|
|
52
|
-
const shake = handshake(stream)
|
|
53
|
-
|
|
54
|
-
// send message
|
|
55
|
-
const request = new FetchRequest({ identifier: key })
|
|
56
|
-
shake.write(lp.encode.single(FetchRequest.encode(request).finish()))
|
|
57
|
-
|
|
58
|
-
// read response
|
|
59
|
-
const response = FetchResponse.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
|
|
60
|
-
switch (response.status) {
|
|
61
|
-
case (FetchResponse.StatusCode.OK): {
|
|
62
|
-
return response.data
|
|
63
|
-
}
|
|
64
|
-
case (FetchResponse.StatusCode.NOT_FOUND): {
|
|
65
|
-
return null
|
|
66
|
-
}
|
|
67
|
-
case (FetchResponse.StatusCode.ERROR): {
|
|
68
|
-
const errmsg = (new TextDecoder()).decode(response.data)
|
|
69
|
-
throw errCode(new Error('Error in fetch protocol response: ' + errmsg), codes.ERR_INVALID_PARAMETERS)
|
|
70
|
-
}
|
|
71
|
-
default: {
|
|
72
|
-
throw errCode(new Error('Unknown response status'), codes.ERR_INVALID_MESSAGE)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Invoked when a fetch request is received. Reads the request message off the given stream and
|
|
79
|
-
* responds based on looking up the key in the request via the lookup callback that corresponds
|
|
80
|
-
* to the key's prefix.
|
|
81
|
-
*
|
|
82
|
-
* @param {object} options
|
|
83
|
-
* @param {MuxedStream} options.stream
|
|
84
|
-
* @param {string} options.protocol
|
|
85
|
-
*/
|
|
86
|
-
async handleMessage (options) {
|
|
87
|
-
const { stream } = options
|
|
88
|
-
const shake = handshake(stream)
|
|
89
|
-
const request = FetchRequest.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
|
|
90
|
-
|
|
91
|
-
let response
|
|
92
|
-
const lookup = this._getLookupFunction(request.identifier)
|
|
93
|
-
if (lookup) {
|
|
94
|
-
const data = await lookup(request.identifier)
|
|
95
|
-
if (data) {
|
|
96
|
-
response = new FetchResponse({ status: FetchResponse.StatusCode.OK, data })
|
|
97
|
-
} else {
|
|
98
|
-
response = new FetchResponse({ status: FetchResponse.StatusCode.NOT_FOUND })
|
|
99
|
-
}
|
|
100
|
-
} else {
|
|
101
|
-
const errmsg = (new TextEncoder()).encode('No lookup function registered for key: ' + request.identifier)
|
|
102
|
-
response = new FetchResponse({ status: FetchResponse.StatusCode.ERROR, data: errmsg })
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
shake.write(lp.encode.single(FetchResponse.encode(response).finish()))
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Given a key, finds the appropriate function for looking up its corresponding value, based on
|
|
110
|
-
* the key's prefix.
|
|
111
|
-
*
|
|
112
|
-
* @param {string} key
|
|
113
|
-
*/
|
|
114
|
-
_getLookupFunction (key) {
|
|
115
|
-
for (const prefix of this._lookupFunctions.keys()) {
|
|
116
|
-
if (key.startsWith(prefix)) {
|
|
117
|
-
return this._lookupFunctions.get(prefix)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return null
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Registers a new lookup callback that can map keys to values, for a given set of keys that
|
|
125
|
-
* share the same prefix.
|
|
126
|
-
*
|
|
127
|
-
* @param {string} prefix
|
|
128
|
-
* @param {LookupFunction} lookup
|
|
129
|
-
*/
|
|
130
|
-
registerLookupFunction (prefix, lookup) {
|
|
131
|
-
if (this._lookupFunctions.has(prefix)) {
|
|
132
|
-
throw errCode(new Error("Fetch protocol handler for key prefix '" + prefix + "' already registered"), codes.ERR_KEY_ALREADY_EXISTS)
|
|
133
|
-
}
|
|
134
|
-
this._lookupFunctions.set(prefix, lookup)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Registers a new lookup callback that can map keys to values, for a given set of keys that
|
|
139
|
-
* share the same prefix.
|
|
140
|
-
*
|
|
141
|
-
* @param {string} prefix
|
|
142
|
-
* @param {LookupFunction} [lookup]
|
|
143
|
-
*/
|
|
144
|
-
unregisterLookupFunction (prefix, lookup) {
|
|
145
|
-
if (lookup != null) {
|
|
146
|
-
const existingLookup = this._lookupFunctions.get(prefix)
|
|
147
|
-
|
|
148
|
-
if (existingLookup !== lookup) {
|
|
149
|
-
return
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this._lookupFunctions.delete(prefix)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
FetchProtocol.PROTOCOL = PROTOCOL
|
|
158
|
-
|
|
159
|
-
exports = module.exports = FetchProtocol
|