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/upgrader.ts
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import errCode from 'err-code'
|
|
3
|
+
import { Dialer, Listener } from '@libp2p/multistream-select'
|
|
4
|
+
import { pipe } from 'it-pipe'
|
|
5
|
+
// @ts-expect-error mutable-proxy does not export types
|
|
6
|
+
import mutableProxy from 'mutable-proxy'
|
|
7
|
+
import { codes } from './errors.js'
|
|
8
|
+
import { createConnection } from '@libp2p/connection'
|
|
9
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
10
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
11
|
+
import type { Connection, ProtocolStream, Stream } from '@libp2p/interfaces/connection'
|
|
12
|
+
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
|
|
13
|
+
import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
|
|
14
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
15
|
+
import type { MultiaddrConnection, Upgrader, UpgraderEvents } from '@libp2p/interfaces/transport'
|
|
16
|
+
import type { Duplex } from 'it-stream-types'
|
|
17
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
18
|
+
|
|
19
|
+
const log = logger('libp2p:upgrader')
|
|
20
|
+
|
|
21
|
+
interface CreateConectionOptions {
|
|
22
|
+
cryptoProtocol: string
|
|
23
|
+
direction: 'inbound' | 'outbound'
|
|
24
|
+
maConn: MultiaddrConnection
|
|
25
|
+
upgradedConn: Duplex<Uint8Array>
|
|
26
|
+
remotePeer: PeerId
|
|
27
|
+
muxerFactory?: StreamMuxerFactory
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface OnStreamOptions {
|
|
31
|
+
connection: Connection
|
|
32
|
+
stream: Stream
|
|
33
|
+
protocol: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CryptoResult extends SecuredConnection {
|
|
37
|
+
protocol: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface UpgraderInit {
|
|
41
|
+
connectionEncryption: ConnectionEncrypter[]
|
|
42
|
+
muxers: StreamMuxerFactory[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upgrader {
|
|
46
|
+
private readonly components: Components
|
|
47
|
+
private readonly connectionEncryption: Map<string, ConnectionEncrypter>
|
|
48
|
+
private readonly muxers: Map<string, StreamMuxerFactory>
|
|
49
|
+
|
|
50
|
+
constructor (components: Components, init: UpgraderInit) {
|
|
51
|
+
super()
|
|
52
|
+
|
|
53
|
+
this.components = components
|
|
54
|
+
this.connectionEncryption = new Map()
|
|
55
|
+
|
|
56
|
+
init.connectionEncryption.forEach(encrypter => {
|
|
57
|
+
this.connectionEncryption.set(encrypter.protocol, encrypter)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
this.muxers = new Map()
|
|
61
|
+
|
|
62
|
+
init.muxers.forEach(muxer => {
|
|
63
|
+
this.muxers.set(muxer.protocol, muxer)
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Upgrades an inbound connection
|
|
69
|
+
*/
|
|
70
|
+
async upgradeInbound (maConn: MultiaddrConnection): Promise<Connection> {
|
|
71
|
+
let encryptedConn
|
|
72
|
+
let remotePeer
|
|
73
|
+
let upgradedConn: Duplex<Uint8Array>
|
|
74
|
+
let muxerFactory: StreamMuxerFactory | undefined
|
|
75
|
+
let cryptoProtocol
|
|
76
|
+
let setPeer
|
|
77
|
+
let proxyPeer
|
|
78
|
+
const metrics = this.components.getMetrics()
|
|
79
|
+
|
|
80
|
+
if (await this.components.getConnectionGater().denyInboundConnection(maConn)) {
|
|
81
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (metrics != null) {
|
|
85
|
+
({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
|
|
86
|
+
const idString = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
|
|
87
|
+
setPeer({ toString: () => idString })
|
|
88
|
+
maConn = metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
log('starting the inbound connection upgrade')
|
|
92
|
+
|
|
93
|
+
// Protect
|
|
94
|
+
let protectedConn = maConn
|
|
95
|
+
const protector = this.components.getConnectionProtector()
|
|
96
|
+
|
|
97
|
+
if (protector != null) {
|
|
98
|
+
log('protecting the inbound connection')
|
|
99
|
+
protectedConn = await protector.protect(maConn)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
// Encrypt the connection
|
|
104
|
+
({
|
|
105
|
+
conn: encryptedConn,
|
|
106
|
+
remotePeer,
|
|
107
|
+
protocol: cryptoProtocol
|
|
108
|
+
} = await this._encryptInbound(protectedConn))
|
|
109
|
+
|
|
110
|
+
if (await this.components.getConnectionGater().denyInboundEncryptedConnection(remotePeer, {
|
|
111
|
+
...protectedConn,
|
|
112
|
+
...encryptedConn
|
|
113
|
+
})) {
|
|
114
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Multiplex the connection
|
|
118
|
+
if (this.muxers.size > 0) {
|
|
119
|
+
const multiplexed = await this._multiplexInbound({
|
|
120
|
+
...protectedConn,
|
|
121
|
+
...encryptedConn
|
|
122
|
+
}, this.muxers)
|
|
123
|
+
muxerFactory = multiplexed.muxerFactory
|
|
124
|
+
upgradedConn = multiplexed.stream
|
|
125
|
+
} else {
|
|
126
|
+
upgradedConn = encryptedConn
|
|
127
|
+
}
|
|
128
|
+
} catch (err: any) {
|
|
129
|
+
log.error('Failed to upgrade inbound connection', err)
|
|
130
|
+
await maConn.close(err)
|
|
131
|
+
throw err
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (await this.components.getConnectionGater().denyInboundUpgradedConnection(remotePeer, {
|
|
135
|
+
...protectedConn,
|
|
136
|
+
...encryptedConn
|
|
137
|
+
})) {
|
|
138
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (metrics != null) {
|
|
142
|
+
metrics.updatePlaceholder(proxyPeer, remotePeer)
|
|
143
|
+
setPeer(remotePeer)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
log('Successfully upgraded inbound connection')
|
|
147
|
+
|
|
148
|
+
return this._createConnection({
|
|
149
|
+
cryptoProtocol,
|
|
150
|
+
direction: 'inbound',
|
|
151
|
+
maConn,
|
|
152
|
+
upgradedConn,
|
|
153
|
+
muxerFactory,
|
|
154
|
+
remotePeer
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Upgrades an outbound connection
|
|
160
|
+
*/
|
|
161
|
+
async upgradeOutbound (maConn: MultiaddrConnection): Promise<Connection> {
|
|
162
|
+
const idStr = maConn.remoteAddr.getPeerId()
|
|
163
|
+
if (idStr == null) {
|
|
164
|
+
throw errCode(new Error('outbound connection must have a peer id'), codes.ERR_INVALID_MULTIADDR)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const remotePeerId = peerIdFromString(idStr)
|
|
168
|
+
|
|
169
|
+
if (await this.components.getConnectionGater().denyOutboundConnection(remotePeerId, maConn)) {
|
|
170
|
+
throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let encryptedConn
|
|
174
|
+
let remotePeer
|
|
175
|
+
let upgradedConn
|
|
176
|
+
let cryptoProtocol
|
|
177
|
+
let muxerFactory
|
|
178
|
+
let setPeer
|
|
179
|
+
let proxyPeer
|
|
180
|
+
const metrics = this.components.getMetrics()
|
|
181
|
+
|
|
182
|
+
if (metrics != null) {
|
|
183
|
+
({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
|
|
184
|
+
const idString = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
|
|
185
|
+
setPeer({ toB58String: () => idString })
|
|
186
|
+
maConn = metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
log('Starting the outbound connection upgrade')
|
|
190
|
+
|
|
191
|
+
// Protect
|
|
192
|
+
let protectedConn = maConn
|
|
193
|
+
const protector = this.components.getConnectionProtector()
|
|
194
|
+
|
|
195
|
+
if (protector != null) {
|
|
196
|
+
protectedConn = await protector.protect(maConn)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
// Encrypt the connection
|
|
201
|
+
({
|
|
202
|
+
conn: encryptedConn,
|
|
203
|
+
remotePeer,
|
|
204
|
+
protocol: cryptoProtocol
|
|
205
|
+
} = await this._encryptOutbound(protectedConn, remotePeerId))
|
|
206
|
+
|
|
207
|
+
if (await this.components.getConnectionGater().denyOutboundEncryptedConnection(remotePeer, {
|
|
208
|
+
...protectedConn,
|
|
209
|
+
...encryptedConn
|
|
210
|
+
})) {
|
|
211
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Multiplex the connection
|
|
215
|
+
if (this.muxers.size > 0) {
|
|
216
|
+
const multiplexed = await this._multiplexOutbound({
|
|
217
|
+
...protectedConn,
|
|
218
|
+
...encryptedConn
|
|
219
|
+
}, this.muxers)
|
|
220
|
+
muxerFactory = multiplexed.muxerFactory
|
|
221
|
+
upgradedConn = multiplexed.stream
|
|
222
|
+
} else {
|
|
223
|
+
upgradedConn = encryptedConn
|
|
224
|
+
}
|
|
225
|
+
} catch (err: any) {
|
|
226
|
+
log.error('Failed to upgrade outbound connection', err)
|
|
227
|
+
await maConn.close(err)
|
|
228
|
+
throw err
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (await this.components.getConnectionGater().denyOutboundUpgradedConnection(remotePeer, {
|
|
232
|
+
...protectedConn,
|
|
233
|
+
...encryptedConn
|
|
234
|
+
})) {
|
|
235
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (metrics != null) {
|
|
239
|
+
metrics.updatePlaceholder(proxyPeer, remotePeer)
|
|
240
|
+
setPeer(remotePeer)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
log('Successfully upgraded outbound connection')
|
|
244
|
+
|
|
245
|
+
return this._createConnection({
|
|
246
|
+
cryptoProtocol,
|
|
247
|
+
direction: 'outbound',
|
|
248
|
+
maConn,
|
|
249
|
+
upgradedConn,
|
|
250
|
+
muxerFactory,
|
|
251
|
+
remotePeer
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* A convenience method for generating a new `Connection`
|
|
257
|
+
*/
|
|
258
|
+
_createConnection (opts: CreateConectionOptions): Connection {
|
|
259
|
+
const {
|
|
260
|
+
cryptoProtocol,
|
|
261
|
+
direction,
|
|
262
|
+
maConn,
|
|
263
|
+
upgradedConn,
|
|
264
|
+
remotePeer,
|
|
265
|
+
muxerFactory
|
|
266
|
+
} = opts
|
|
267
|
+
|
|
268
|
+
let muxer: StreamMuxer | undefined
|
|
269
|
+
let newStream: ((multicodecs: string[]) => Promise<ProtocolStream>) | undefined
|
|
270
|
+
let connection: Connection // eslint-disable-line prefer-const
|
|
271
|
+
|
|
272
|
+
if (muxerFactory != null) {
|
|
273
|
+
// Create the muxer
|
|
274
|
+
muxer = muxerFactory.createStreamMuxer(this.components, {
|
|
275
|
+
// Run anytime a remote stream is created
|
|
276
|
+
onIncomingStream: muxedStream => {
|
|
277
|
+
if (connection == null) {
|
|
278
|
+
return
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
void Promise.resolve()
|
|
282
|
+
.then(async () => {
|
|
283
|
+
const mss = new Listener(muxedStream)
|
|
284
|
+
const protocols = this.components.getRegistrar().getProtocols()
|
|
285
|
+
const { stream, protocol } = await mss.handle(protocols)
|
|
286
|
+
log('%s: incoming stream opened on %s', direction, protocol)
|
|
287
|
+
|
|
288
|
+
const metrics = this.components.getMetrics()
|
|
289
|
+
|
|
290
|
+
if (metrics != null) {
|
|
291
|
+
metrics.trackStream({ stream, remotePeer, protocol })
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (connection == null) {
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
connection.addStream(muxedStream, { protocol })
|
|
299
|
+
this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
|
|
300
|
+
})
|
|
301
|
+
.catch(err => {
|
|
302
|
+
log.error(err)
|
|
303
|
+
})
|
|
304
|
+
},
|
|
305
|
+
// Run anytime a stream closes
|
|
306
|
+
onStreamEnd: muxedStream => {
|
|
307
|
+
connection?.removeStream(muxedStream.id)
|
|
308
|
+
}
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
newStream = async (protocols: string[]): Promise<ProtocolStream> => {
|
|
312
|
+
if (muxer == null) {
|
|
313
|
+
throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
log('%s: starting new stream on %s', direction, protocols)
|
|
317
|
+
const muxedStream = muxer.newStream()
|
|
318
|
+
const mss = new Dialer(muxedStream)
|
|
319
|
+
const metrics = this.components.getMetrics()
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
let { stream, protocol } = await mss.select(protocols)
|
|
323
|
+
|
|
324
|
+
if (metrics != null) {
|
|
325
|
+
stream = metrics.trackStream({ stream, remotePeer, protocol })
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return { stream: { ...muxedStream, ...stream }, protocol }
|
|
329
|
+
} catch (err: any) {
|
|
330
|
+
log.error('could not create new stream', err)
|
|
331
|
+
throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Pipe all data through the muxer
|
|
336
|
+
pipe(upgradedConn, muxer, upgradedConn).catch(log.error)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const _timeline = maConn.timeline
|
|
340
|
+
maConn.timeline = new Proxy(_timeline, {
|
|
341
|
+
set: (...args) => {
|
|
342
|
+
if (connection != null && args[1] === 'close' && args[2] != null && _timeline.close == null) {
|
|
343
|
+
// Wait for close to finish before notifying of the closure
|
|
344
|
+
(async () => {
|
|
345
|
+
try {
|
|
346
|
+
if (connection.stat.status === 'OPEN') {
|
|
347
|
+
await connection.close()
|
|
348
|
+
}
|
|
349
|
+
} catch (err: any) {
|
|
350
|
+
log.error(err)
|
|
351
|
+
} finally {
|
|
352
|
+
this.dispatchEvent(new CustomEvent<Connection>('connectionEnd', {
|
|
353
|
+
detail: connection
|
|
354
|
+
}))
|
|
355
|
+
}
|
|
356
|
+
})().catch(err => {
|
|
357
|
+
log.error(err)
|
|
358
|
+
})
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return Reflect.set(...args)
|
|
362
|
+
}
|
|
363
|
+
})
|
|
364
|
+
maConn.timeline.upgraded = Date.now()
|
|
365
|
+
|
|
366
|
+
const errConnectionNotMultiplexed = () => {
|
|
367
|
+
throw errCode(new Error('connection is not multiplexed'), codes.ERR_CONNECTION_NOT_MULTIPLEXED)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Create the connection
|
|
371
|
+
connection = createConnection({
|
|
372
|
+
remoteAddr: maConn.remoteAddr,
|
|
373
|
+
remotePeer: remotePeer,
|
|
374
|
+
stat: {
|
|
375
|
+
status: 'OPEN',
|
|
376
|
+
direction,
|
|
377
|
+
timeline: maConn.timeline,
|
|
378
|
+
multiplexer: muxer?.protocol,
|
|
379
|
+
encryption: cryptoProtocol
|
|
380
|
+
},
|
|
381
|
+
newStream: newStream ?? errConnectionNotMultiplexed,
|
|
382
|
+
getStreams: () => muxer != null ? muxer.streams : errConnectionNotMultiplexed(),
|
|
383
|
+
close: async () => {
|
|
384
|
+
await maConn.close()
|
|
385
|
+
// Ensure remaining streams are aborted
|
|
386
|
+
if (muxer != null) {
|
|
387
|
+
muxer.streams.map(stream => stream.abort())
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
this.dispatchEvent(new CustomEvent<Connection>('connection', {
|
|
393
|
+
detail: connection
|
|
394
|
+
}))
|
|
395
|
+
|
|
396
|
+
return connection
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Routes incoming streams to the correct handler
|
|
401
|
+
*/
|
|
402
|
+
_onStream (opts: OnStreamOptions): void {
|
|
403
|
+
const { connection, stream, protocol } = opts
|
|
404
|
+
const handler = this.components.getRegistrar().getHandler(protocol)
|
|
405
|
+
handler({ connection, stream, protocol })
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Attempts to encrypt the incoming `connection` with the provided `cryptos`
|
|
410
|
+
*/
|
|
411
|
+
async _encryptInbound (connection: Duplex<Uint8Array>): Promise<CryptoResult> {
|
|
412
|
+
const mss = new Listener(connection)
|
|
413
|
+
const protocols = Array.from(this.connectionEncryption.keys())
|
|
414
|
+
log('handling inbound crypto protocol selection', protocols)
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
const { stream, protocol } = await mss.handle(protocols)
|
|
418
|
+
const encrypter = this.connectionEncryption.get(protocol)
|
|
419
|
+
|
|
420
|
+
if (encrypter == null) {
|
|
421
|
+
throw new Error(`no crypto module found for ${protocol}`)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
log('encrypting inbound connection...')
|
|
425
|
+
|
|
426
|
+
return {
|
|
427
|
+
...await encrypter.secureInbound(this.components.getPeerId(), stream),
|
|
428
|
+
protocol
|
|
429
|
+
}
|
|
430
|
+
} catch (err: any) {
|
|
431
|
+
throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Attempts to encrypt the given `connection` with the provided connection encrypters.
|
|
437
|
+
* The first `ConnectionEncrypter` module to succeed will be used
|
|
438
|
+
*/
|
|
439
|
+
async _encryptOutbound (connection: MultiaddrConnection, remotePeerId: PeerId): Promise<CryptoResult> {
|
|
440
|
+
const mss = new Dialer(connection)
|
|
441
|
+
const protocols = Array.from(this.connectionEncryption.keys())
|
|
442
|
+
log('selecting outbound crypto protocol', protocols)
|
|
443
|
+
|
|
444
|
+
try {
|
|
445
|
+
const { stream, protocol } = await mss.select(protocols)
|
|
446
|
+
const encrypter = this.connectionEncryption.get(protocol)
|
|
447
|
+
|
|
448
|
+
if (encrypter == null) {
|
|
449
|
+
throw new Error(`no crypto module found for ${protocol}`)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
log('encrypting outbound connection to %p', remotePeerId)
|
|
453
|
+
|
|
454
|
+
return {
|
|
455
|
+
...await encrypter.secureOutbound(this.components.getPeerId(), stream, remotePeerId),
|
|
456
|
+
protocol
|
|
457
|
+
}
|
|
458
|
+
} catch (err: any) {
|
|
459
|
+
throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Selects one of the given muxers via multistream-select. That
|
|
465
|
+
* muxer will be used for all future streams on the connection.
|
|
466
|
+
*/
|
|
467
|
+
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
|
|
468
|
+
const dialer = new Dialer(connection)
|
|
469
|
+
const protocols = Array.from(muxers.keys())
|
|
470
|
+
log('outbound selecting muxer %s', protocols)
|
|
471
|
+
try {
|
|
472
|
+
const { stream, protocol } = await dialer.select(protocols)
|
|
473
|
+
log('%s selected as muxer protocol', protocol)
|
|
474
|
+
const muxerFactory = muxers.get(protocol)
|
|
475
|
+
return { stream, muxerFactory }
|
|
476
|
+
} catch (err: any) {
|
|
477
|
+
log.error('error multiplexing outbound stream', err)
|
|
478
|
+
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Registers support for one of the given muxers via multistream-select. The
|
|
484
|
+
* selected muxer will be used for all future streams on the connection.
|
|
485
|
+
*/
|
|
486
|
+
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
|
|
487
|
+
const listener = new Listener(connection)
|
|
488
|
+
const protocols = Array.from(muxers.keys())
|
|
489
|
+
log('inbound handling muxers %s', protocols)
|
|
490
|
+
try {
|
|
491
|
+
const { stream, protocol } = await listener.handle(protocols)
|
|
492
|
+
const muxerFactory = muxers.get(protocol)
|
|
493
|
+
return { stream, muxerFactory }
|
|
494
|
+
} catch (err: any) {
|
|
495
|
+
log.error('error multiplexing inbound stream', err)
|
|
496
|
+
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
package/src/version.ts
ADDED