libp2p 0.42.2-45fc415b → 0.42.2-593deefe
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/dist/index.min.js +29 -29
- package/dist/src/circuit/client.d.ts +81 -0
- package/dist/src/circuit/client.d.ts.map +1 -0
- package/dist/src/circuit/client.js +285 -0
- package/dist/src/circuit/client.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +7 -8
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +11 -8
- package/dist/src/circuit/constants.js.map +1 -1
- package/dist/src/circuit/hop.d.ts +23 -0
- package/dist/src/circuit/hop.d.ts.map +1 -0
- package/dist/src/circuit/hop.js +167 -0
- package/dist/src/circuit/hop.js.map +1 -0
- package/dist/src/circuit/index.d.ts +47 -47
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -68
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/interfaces.d.ts +27 -0
- package/dist/src/circuit/interfaces.d.ts.map +1 -0
- package/dist/src/circuit/interfaces.js +2 -0
- package/dist/src/circuit/interfaces.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +2 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -1
- package/dist/src/circuit/multicodec.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +83 -43
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +363 -127
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/relay.d.ts +34 -0
- package/dist/src/circuit/relay.d.ts.map +1 -0
- package/dist/src/circuit/relay.js +61 -0
- package/dist/src/circuit/relay.js.map +1 -0
- package/dist/src/circuit/reservation-store.d.ts +42 -0
- package/dist/src/circuit/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/reservation-store.js +59 -0
- package/dist/src/circuit/reservation-store.js.map +1 -0
- package/dist/src/circuit/reservation-voucher.d.ts +18 -0
- package/dist/src/circuit/reservation-voucher.d.ts.map +1 -0
- package/dist/src/circuit/reservation-voucher.js +33 -0
- package/dist/src/circuit/reservation-voucher.js.map +1 -0
- package/dist/src/circuit/stop.d.ts +19 -0
- package/dist/src/circuit/stop.d.ts.map +1 -0
- package/dist/src/circuit/stop.js +67 -0
- package/dist/src/circuit/stop.js.map +1 -0
- package/dist/src/circuit/transport.d.ts +24 -9
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +133 -118
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/circuit/utils.d.ts +5 -0
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +114 -0
- package/dist/src/circuit/utils.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +2 -3
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js +7 -0
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +3 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +4 -3
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -2
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +1 -0
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +3 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +4 -5
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/libp2p.d.ts +2 -0
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +10 -3
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +2 -3
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +12 -11
- package/src/circuit/client.ts +381 -0
- package/src/circuit/constants.ts +14 -8
- package/src/circuit/hop.ts +210 -0
- package/src/circuit/index.ts +49 -111
- package/src/circuit/interfaces.ts +28 -0
- package/src/circuit/multicodec.ts +2 -1
- package/src/circuit/pb/index.proto +57 -32
- package/src/circuit/pb/index.ts +448 -138
- package/src/circuit/relay.ts +87 -0
- package/src/circuit/reservation-store.ts +106 -0
- package/src/circuit/reservation-voucher.ts +51 -0
- package/src/circuit/stop.ts +92 -0
- package/src/circuit/transport.ts +166 -131
- package/src/circuit/utils.ts +145 -0
- package/src/config.ts +2 -3
- package/src/connection-manager/dialer/dial-request.ts +7 -0
- package/src/errors.ts +2 -1
- package/src/fetch/pb/proto.ts +8 -7
- package/src/identify/pb/message.ts +3 -2
- package/src/insecure/pb/proto.ts +8 -9
- package/src/libp2p.ts +12 -3
- package/src/transport-manager.ts +2 -3
- package/src/upgrader.ts +4 -4
- package/src/version.ts +1 -1
- package/dist/src/circuit/auto-relay.d.ts +0 -46
- package/dist/src/circuit/auto-relay.d.ts.map +0 -1
- package/dist/src/circuit/auto-relay.js +0 -220
- package/dist/src/circuit/auto-relay.js.map +0 -1
- package/dist/src/circuit/circuit/hop.d.ts +0 -42
- package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/hop.js +0 -142
- package/dist/src/circuit/circuit/hop.js.map +0 -1
- package/dist/src/circuit/circuit/stop.d.ts +0 -24
- package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stop.js +0 -51
- package/dist/src/circuit/circuit/stop.js.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.js +0 -59
- package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
- package/dist/src/circuit/circuit/utils.d.ts +0 -7
- package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
- package/dist/src/circuit/circuit/utils.js +0 -43
- package/dist/src/circuit/circuit/utils.js.map +0 -1
- package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
- package/src/circuit/README.md +0 -111
- package/src/circuit/auto-relay.ts +0 -292
- package/src/circuit/circuit/hop.ts +0 -220
- package/src/circuit/circuit/stop.ts +0 -83
- package/src/circuit/circuit/stream-handler.ts +0 -87
- package/src/circuit/circuit/utils.ts +0 -44
package/src/insecure/pb/proto.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
|
|
6
7
|
import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
|
|
7
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export interface Exchange {
|
|
11
12
|
id?: Uint8Array
|
|
@@ -29,9 +30,7 @@ export namespace Exchange {
|
|
|
29
30
|
|
|
30
31
|
if (obj.pubkey != null) {
|
|
31
32
|
w.uint32(18)
|
|
32
|
-
PublicKey.codec().encode(obj.pubkey, w
|
|
33
|
-
writeDefaults: false
|
|
34
|
-
})
|
|
33
|
+
PublicKey.codec().encode(obj.pubkey, w)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
if (opts.lengthDelimited !== false) {
|
|
@@ -65,7 +64,7 @@ export namespace Exchange {
|
|
|
65
64
|
return _codec
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
export const encode = (obj: Exchange): Uint8Array => {
|
|
67
|
+
export const encode = (obj: Partial<Exchange>): Uint8Array => {
|
|
69
68
|
return encodeMessage(obj, Exchange.codec())
|
|
70
69
|
}
|
|
71
70
|
|
|
@@ -89,7 +88,7 @@ enum __KeyTypeValues {
|
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
export namespace KeyType {
|
|
92
|
-
export const codec = () => {
|
|
91
|
+
export const codec = (): Codec<KeyType> => {
|
|
93
92
|
return enumeration<KeyType>(__KeyTypeValues)
|
|
94
93
|
}
|
|
95
94
|
}
|
|
@@ -108,12 +107,12 @@ export namespace PublicKey {
|
|
|
108
107
|
w.fork()
|
|
109
108
|
}
|
|
110
109
|
|
|
111
|
-
if (
|
|
110
|
+
if (obj.Type != null && __KeyTypeValues[obj.Type] !== 0) {
|
|
112
111
|
w.uint32(8)
|
|
113
112
|
KeyType.codec().encode(obj.Type, w)
|
|
114
113
|
}
|
|
115
114
|
|
|
116
|
-
if (
|
|
115
|
+
if ((obj.Data != null && obj.Data.byteLength > 0)) {
|
|
117
116
|
w.uint32(18)
|
|
118
117
|
w.bytes(obj.Data)
|
|
119
118
|
}
|
|
@@ -152,7 +151,7 @@ export namespace PublicKey {
|
|
|
152
151
|
return _codec
|
|
153
152
|
}
|
|
154
153
|
|
|
155
|
-
export const encode = (obj: PublicKey): Uint8Array => {
|
|
154
|
+
export const encode = (obj: Partial<PublicKey>): Uint8Array => {
|
|
156
155
|
return encodeMessage(obj, PublicKey.codec())
|
|
157
156
|
}
|
|
158
157
|
|
package/src/libp2p.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RelayReservationManager } from './circuit/client.js'
|
|
1
2
|
import { logger } from '@libp2p/logger'
|
|
2
3
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
3
4
|
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
|
|
@@ -60,6 +61,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
60
61
|
public dht: DualDHT
|
|
61
62
|
public pubsub: PubSub
|
|
62
63
|
public identifyService: IdentifyService
|
|
64
|
+
public circuitService?: RelayReservationManager
|
|
63
65
|
public fetchService: FetchService
|
|
64
66
|
public pingService: PingService
|
|
65
67
|
public components: Components
|
|
@@ -181,6 +183,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
181
183
|
})
|
|
182
184
|
this.configureComponent(this.identifyService)
|
|
183
185
|
|
|
186
|
+
if (init.relay.reservationManager.enabled === true) {
|
|
187
|
+
this.circuitService = new RelayReservationManager(this.components, {
|
|
188
|
+
addressSorter: init.connectionManager.addressSorter,
|
|
189
|
+
...init.relay.reservationManager
|
|
190
|
+
})
|
|
191
|
+
this.services.push(this.circuitService)
|
|
192
|
+
}
|
|
193
|
+
|
|
184
194
|
// dht provided components (peerRouting, contentRouting, dht)
|
|
185
195
|
if (init.dht != null) {
|
|
186
196
|
this.dht = this.components.dht = init.dht(this.components)
|
|
@@ -230,7 +240,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
230
240
|
this.components.transportManager.add(this.configureComponent(new Circuit(this.components, init.relay)))
|
|
231
241
|
|
|
232
242
|
this.configureComponent(new Relay(this.components, {
|
|
233
|
-
addressSorter: init.connectionManager.addressSorter,
|
|
234
243
|
...init.relay
|
|
235
244
|
}))
|
|
236
245
|
}
|
|
@@ -291,7 +300,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
291
300
|
|
|
292
301
|
// start any startables
|
|
293
302
|
await Promise.all(
|
|
294
|
-
this.services.map(service => service.start())
|
|
303
|
+
this.services.map(async service => await service.start())
|
|
295
304
|
)
|
|
296
305
|
|
|
297
306
|
await Promise.all(
|
|
@@ -331,7 +340,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
331
340
|
)
|
|
332
341
|
|
|
333
342
|
await Promise.all(
|
|
334
|
-
this.services.map(service => service.stop())
|
|
343
|
+
this.services.map(async service => await service.stop())
|
|
335
344
|
)
|
|
336
345
|
|
|
337
346
|
await Promise.all(
|
package/src/transport-manager.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import pSettle from 'p-settle'
|
|
3
2
|
import { codes } from './errors.js'
|
|
4
3
|
import errCode from 'err-code'
|
|
5
4
|
import { FaultTolerance } from '@libp2p/interface-transport'
|
|
@@ -218,12 +217,12 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|
|
218
217
|
continue
|
|
219
218
|
}
|
|
220
219
|
|
|
221
|
-
const results = await
|
|
220
|
+
const results = await Promise.allSettled(tasks)
|
|
222
221
|
// If we are listening on at least 1 address, succeed.
|
|
223
222
|
// TODO: we should look at adding a retry (`p-retry`) here to better support
|
|
224
223
|
// listening on remote addresses as they may be offline. We could then potentially
|
|
225
224
|
// just wait for any (`p-any`) listener to succeed on each transport before returning
|
|
226
|
-
const isListening = results.find(r => r.
|
|
225
|
+
const isListening = results.find(r => r.status === 'fulfilled')
|
|
227
226
|
if ((isListening == null) && this.faultTolerance !== FaultTolerance.NO_FATAL) {
|
|
228
227
|
throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
|
|
229
228
|
}
|
package/src/upgrader.ts
CHANGED
|
@@ -149,7 +149,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
149
149
|
try {
|
|
150
150
|
// fails on node < 15.4
|
|
151
151
|
setMaxListeners?.(Infinity, timeoutController.signal)
|
|
152
|
-
} catch {}
|
|
152
|
+
} catch { }
|
|
153
153
|
|
|
154
154
|
try {
|
|
155
155
|
const abortableStream = abortableDuplex(maConn, timeoutController.signal)
|
|
@@ -439,7 +439,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
439
439
|
try {
|
|
440
440
|
// fails on node < 15.4
|
|
441
441
|
setMaxListeners?.(Infinity, controller.signal)
|
|
442
|
-
} catch {}
|
|
442
|
+
} catch { }
|
|
443
443
|
}
|
|
444
444
|
|
|
445
445
|
const { stream, protocol } = await mss.select(muxedStream, protocols, options)
|
|
@@ -626,7 +626,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
626
626
|
* Selects one of the given muxers via multistream-select. That
|
|
627
627
|
* muxer will be used for all future streams on the connection.
|
|
628
628
|
*/
|
|
629
|
-
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
|
|
629
|
+
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory }> {
|
|
630
630
|
const protocols = Array.from(muxers.keys())
|
|
631
631
|
log('outbound selecting muxer %s', protocols)
|
|
632
632
|
try {
|
|
@@ -646,7 +646,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
646
646
|
* Registers support for one of the given muxers via multistream-select. The
|
|
647
647
|
* selected muxer will be used for all future streams on the connection.
|
|
648
648
|
*/
|
|
649
|
-
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
|
|
649
|
+
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory }> {
|
|
650
650
|
const protocols = Array.from(muxers.keys())
|
|
651
651
|
log('inbound handling muxers %s', protocols)
|
|
652
652
|
try {
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '0.42.2-
|
|
1
|
+
export const version = '0.42.2-593deefe'
|
|
2
2
|
export const name = 'libp2p'
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
2
|
-
import type { AddressSorter, PeerProtocolsChangeData } from '@libp2p/interface-peer-store';
|
|
3
|
-
import type { Connection } from '@libp2p/interface-connection';
|
|
4
|
-
import type { RelayComponents } from './index.js';
|
|
5
|
-
export interface AutoRelayInit {
|
|
6
|
-
addressSorter?: AddressSorter;
|
|
7
|
-
maxListeners?: number;
|
|
8
|
-
onError?: (error: Error, msg?: string) => void;
|
|
9
|
-
}
|
|
10
|
-
export declare class AutoRelay {
|
|
11
|
-
private readonly components;
|
|
12
|
-
private readonly addressSorter;
|
|
13
|
-
private readonly maxListeners;
|
|
14
|
-
private readonly listenRelays;
|
|
15
|
-
private readonly onError;
|
|
16
|
-
constructor(components: RelayComponents, init: AutoRelayInit);
|
|
17
|
-
/**
|
|
18
|
-
* Check if a peer supports the relay protocol.
|
|
19
|
-
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
|
20
|
-
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
|
21
|
-
* inside the threshold.
|
|
22
|
-
*/
|
|
23
|
-
_onProtocolChange(evt: CustomEvent<PeerProtocolsChangeData>): Promise<void>;
|
|
24
|
-
/**
|
|
25
|
-
* Peer disconnects
|
|
26
|
-
*/
|
|
27
|
-
_onPeerDisconnected(evt: CustomEvent<Connection>): void;
|
|
28
|
-
/**
|
|
29
|
-
* Attempt to listen on the given relay connection
|
|
30
|
-
*/
|
|
31
|
-
_addListenRelay(connection: Connection, id: string): Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
* Remove listen relay
|
|
34
|
-
*/
|
|
35
|
-
_removeListenRelay(id: string): Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Try to listen on available hop relay connections.
|
|
38
|
-
* The following order will happen while we do not have enough relays.
|
|
39
|
-
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
|
40
|
-
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
|
41
|
-
* 3. Search the network.
|
|
42
|
-
*/
|
|
43
|
-
_listenOnAvailableHopRelays(peersToIgnore?: string[]): Promise<void>;
|
|
44
|
-
_tryToListenOnRelay(peerId: PeerId): Promise<void>;
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=auto-relay.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"auto-relay.d.ts","sourceRoot":"","sources":["../../../src/circuit/auto-relay.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAA;AAC1F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAK9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAMjD,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CAC/C;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiB;IAC5C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAQ;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAa;IAC1C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;gBAEjD,UAAU,EAAE,eAAe,EAAE,IAAI,EAAE,aAAa;IAkB7D;;;;;OAKG;IACG,iBAAiB,CAAE,GAAG,EAAE,WAAW,CAAC,uBAAuB,CAAC;IAkDlE;;OAEG;IACH,mBAAmB,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IAejD;;OAEG;IACG,eAAe,CAAE,UAAU,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA+CzE;;OAEG;IACG,kBAAkB,CAAE,EAAE,EAAE,MAAM;IAOpC;;;;;;OAMG;IACG,2BAA2B,CAAE,aAAa,GAAE,MAAM,EAAO;IAqFzD,mBAAmB,CAAE,MAAM,EAAE,MAAM;CAS1C"}
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger';
|
|
2
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
3
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
4
|
-
import { RELAY_CODEC } from './multicodec.js';
|
|
5
|
-
import { canHop } from './circuit/hop.js';
|
|
6
|
-
import { namespaceToCid } from './utils.js';
|
|
7
|
-
import { CIRCUIT_PROTO_CODE, HOP_METADATA_KEY, HOP_METADATA_VALUE, RELAY_RENDEZVOUS_NS } from './constants.js';
|
|
8
|
-
import sort from 'it-sort';
|
|
9
|
-
import all from 'it-all';
|
|
10
|
-
import { pipe } from 'it-pipe';
|
|
11
|
-
import { publicAddressesFirst } from '@libp2p/utils/address-sort';
|
|
12
|
-
const log = logger('libp2p:auto-relay');
|
|
13
|
-
const noop = () => { };
|
|
14
|
-
export class AutoRelay {
|
|
15
|
-
constructor(components, init) {
|
|
16
|
-
this.components = components;
|
|
17
|
-
this.addressSorter = init.addressSorter ?? publicAddressesFirst;
|
|
18
|
-
this.maxListeners = init.maxListeners ?? 1;
|
|
19
|
-
this.listenRelays = new Set();
|
|
20
|
-
this.onError = init.onError ?? noop;
|
|
21
|
-
this._onProtocolChange = this._onProtocolChange.bind(this);
|
|
22
|
-
this._onPeerDisconnected = this._onPeerDisconnected.bind(this);
|
|
23
|
-
this.components.peerStore.addEventListener('change:protocols', (evt) => {
|
|
24
|
-
void this._onProtocolChange(evt).catch(err => {
|
|
25
|
-
log.error(err);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Check if a peer supports the relay protocol.
|
|
32
|
-
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
|
33
|
-
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
|
34
|
-
* inside the threshold.
|
|
35
|
-
*/
|
|
36
|
-
async _onProtocolChange(evt) {
|
|
37
|
-
const { peerId, protocols } = evt.detail;
|
|
38
|
-
const id = peerId.toString();
|
|
39
|
-
// Check if it has the protocol
|
|
40
|
-
const hasProtocol = protocols.find(protocol => protocol === RELAY_CODEC);
|
|
41
|
-
// If no protocol, check if we were keeping the peer before as a listenRelay
|
|
42
|
-
if (hasProtocol == null) {
|
|
43
|
-
if (this.listenRelays.has(id)) {
|
|
44
|
-
await this._removeListenRelay(id);
|
|
45
|
-
}
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (this.listenRelays.has(id)) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
|
52
|
-
try {
|
|
53
|
-
const connections = this.components.connectionManager.getConnections(peerId);
|
|
54
|
-
if (connections.length === 0) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const connection = connections[0];
|
|
58
|
-
// Do not hop on a relayed connection
|
|
59
|
-
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
|
60
|
-
log(`relayed connection to ${id} will not be used to hop on`);
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
const supportsHop = await canHop({ connection });
|
|
64
|
-
if (supportsHop) {
|
|
65
|
-
await this.components.peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE));
|
|
66
|
-
await this._addListenRelay(connection, id);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
this.onError(err);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Peer disconnects
|
|
75
|
-
*/
|
|
76
|
-
_onPeerDisconnected(evt) {
|
|
77
|
-
const connection = evt.detail;
|
|
78
|
-
const peerId = connection.remotePeer;
|
|
79
|
-
const id = peerId.toString();
|
|
80
|
-
// Not listening on this relay
|
|
81
|
-
if (!this.listenRelays.has(id)) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
this._removeListenRelay(id).catch(err => {
|
|
85
|
-
log.error(err);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Attempt to listen on the given relay connection
|
|
90
|
-
*/
|
|
91
|
-
async _addListenRelay(connection, id) {
|
|
92
|
-
try {
|
|
93
|
-
// Check if already listening on enough relays
|
|
94
|
-
if (this.listenRelays.size >= this.maxListeners) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
// Get peer known addresses and sort them with public addresses first
|
|
98
|
-
const remoteAddrs = await pipe(await this.components.peerStore.addressBook.get(connection.remotePeer), (source) => sort(source, this.addressSorter), async (source) => await all(source));
|
|
99
|
-
// Attempt to listen on relay
|
|
100
|
-
const result = await Promise.all(remoteAddrs.map(async (addr) => {
|
|
101
|
-
try {
|
|
102
|
-
let multiaddr = addr.multiaddr;
|
|
103
|
-
if (multiaddr.getPeerId() == null) {
|
|
104
|
-
multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`);
|
|
105
|
-
}
|
|
106
|
-
multiaddr = multiaddr.encapsulate('/p2p-circuit');
|
|
107
|
-
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
|
108
|
-
await this.components.transportManager.listen([multiaddr]);
|
|
109
|
-
return true;
|
|
110
|
-
}
|
|
111
|
-
catch (err) {
|
|
112
|
-
log.error('error listening on circuit address', err);
|
|
113
|
-
this.onError(err);
|
|
114
|
-
}
|
|
115
|
-
return false;
|
|
116
|
-
}));
|
|
117
|
-
if (result.includes(true)) {
|
|
118
|
-
this.listenRelays.add(id);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
catch (err) {
|
|
122
|
-
this.onError(err);
|
|
123
|
-
this.listenRelays.delete(id);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Remove listen relay
|
|
128
|
-
*/
|
|
129
|
-
async _removeListenRelay(id) {
|
|
130
|
-
if (this.listenRelays.delete(id)) {
|
|
131
|
-
// TODO: this should be responsibility of the connMgr
|
|
132
|
-
await this._listenOnAvailableHopRelays([id]);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Try to listen on available hop relay connections.
|
|
137
|
-
* The following order will happen while we do not have enough relays.
|
|
138
|
-
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
|
139
|
-
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
|
140
|
-
* 3. Search the network.
|
|
141
|
-
*/
|
|
142
|
-
async _listenOnAvailableHopRelays(peersToIgnore = []) {
|
|
143
|
-
// TODO: The peer redial issue on disconnect should be handled by connection gating
|
|
144
|
-
// Check if already listening on enough relays
|
|
145
|
-
if (this.listenRelays.size >= this.maxListeners) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
const knownHopsToDial = [];
|
|
149
|
-
const peers = await this.components.peerStore.all();
|
|
150
|
-
// Check if we have known hop peers to use and attempt to listen on the already connected
|
|
151
|
-
for (const { id, metadata } of peers) {
|
|
152
|
-
const idStr = id.toString();
|
|
153
|
-
// Continue to next if listening on this or peer to ignore
|
|
154
|
-
if (this.listenRelays.has(idStr)) {
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
if (peersToIgnore.includes(idStr)) {
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
const supportsHop = metadata.get(HOP_METADATA_KEY);
|
|
161
|
-
// Continue to next if it does not support Hop
|
|
162
|
-
if ((supportsHop == null) || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
const connections = this.components.connectionManager.getConnections(id);
|
|
166
|
-
// If not connected, store for possible later use.
|
|
167
|
-
if (connections.length === 0) {
|
|
168
|
-
knownHopsToDial.push(id);
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
await this._addListenRelay(connections[0], idStr);
|
|
172
|
-
// Check if already listening on enough relays
|
|
173
|
-
if (this.listenRelays.size >= this.maxListeners) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
// Try to listen on known peers that are not connected
|
|
178
|
-
for (const peerId of knownHopsToDial) {
|
|
179
|
-
await this._tryToListenOnRelay(peerId);
|
|
180
|
-
// Check if already listening on enough relays
|
|
181
|
-
if (this.listenRelays.size >= this.maxListeners) {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
// Try to find relays to hop on the network
|
|
186
|
-
try {
|
|
187
|
-
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS);
|
|
188
|
-
for await (const provider of this.components.contentRouting.findProviders(cid)) {
|
|
189
|
-
if (provider.multiaddrs.length === 0) {
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
const peerId = provider.id;
|
|
193
|
-
if (peerId.equals(this.components.peerId)) {
|
|
194
|
-
// Skip the provider if it's us as dialing will fail
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs);
|
|
198
|
-
await this._tryToListenOnRelay(peerId);
|
|
199
|
-
// Check if already listening on enough relays
|
|
200
|
-
if (this.listenRelays.size >= this.maxListeners) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
catch (err) {
|
|
206
|
-
this.onError(err);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
async _tryToListenOnRelay(peerId) {
|
|
210
|
-
try {
|
|
211
|
-
const connection = await this.components.connectionManager.openConnection(peerId);
|
|
212
|
-
await this._addListenRelay(connection, peerId.toString());
|
|
213
|
-
}
|
|
214
|
-
catch (err) {
|
|
215
|
-
log.error('Could not use %p as relay', peerId, err);
|
|
216
|
-
this.onError(err, `could not connect and listen on known hop relay ${peerId.toString()}`);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
//# sourceMappingURL=auto-relay.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"auto-relay.js","sourceRoot":"","sources":["../../../src/circuit/auto-relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA;AAIvB,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;AAGjE,MAAM,GAAG,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAA;AAEvC,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;AAQrB,MAAM,OAAO,SAAS;IA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
|
|
3
|
-
import type { Connection } from '@libp2p/interface-connection';
|
|
4
|
-
import type { Duplex } from 'it-stream-types';
|
|
5
|
-
import type { Circuit } from '../transport.js';
|
|
6
|
-
import type { ConnectionManager } from '@libp2p/interface-connection-manager';
|
|
7
|
-
import type { AbortOptions } from '@libp2p/interfaces';
|
|
8
|
-
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
9
|
-
export interface HopRequest {
|
|
10
|
-
connection: Connection;
|
|
11
|
-
request: CircuitPB;
|
|
12
|
-
streamHandler: StreamHandler;
|
|
13
|
-
circuit: Circuit;
|
|
14
|
-
connectionManager: ConnectionManager;
|
|
15
|
-
}
|
|
16
|
-
export declare function handleHop(hopRequest: HopRequest): Promise<void>;
|
|
17
|
-
export interface HopConfig extends AbortOptions {
|
|
18
|
-
connection: Connection;
|
|
19
|
-
request: CircuitPB;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Performs a HOP request to a relay peer, to request a connection to another
|
|
23
|
-
* peer. A new, virtual, connection will be created between the two via the relay.
|
|
24
|
-
*/
|
|
25
|
-
export declare function hop(options: HopConfig): Promise<Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>>;
|
|
26
|
-
export interface CanHopOptions extends AbortOptions {
|
|
27
|
-
connection: Connection;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Performs a CAN_HOP request to a relay peer, in order to understand its capabilities
|
|
31
|
-
*/
|
|
32
|
-
export declare function canHop(options: CanHopOptions): Promise<boolean>;
|
|
33
|
-
export interface HandleCanHopOptions {
|
|
34
|
-
connection: Connection;
|
|
35
|
-
streamHandler: StreamHandler;
|
|
36
|
-
circuit: Circuit;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Creates an unencoded CAN_HOP response based on the Circuits configuration
|
|
40
|
-
*/
|
|
41
|
-
export declare function handleCanHop(options: HandleCanHopOptions): void;
|
|
42
|
-
//# sourceMappingURL=hop.d.ts.map
|
|
@@ -1 +0,0 @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UAAU,EAAE,UAAU,CAAA;IACtB,aAAa,EAAE,aAAa,CAAA;IAC5B,OAAO,EAAE,OAAO,CAAA;CACjB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAE,OAAO,EAAE,mBAAmB,QAYzD"}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger';
|
|
2
|
-
import errCode from 'err-code';
|
|
3
|
-
import { validateAddrs } from './utils.js';
|
|
4
|
-
import { StreamHandler } from './stream-handler.js';
|
|
5
|
-
import { CircuitRelay as CircuitPB } from '../pb/index.js';
|
|
6
|
-
import { pipe } from 'it-pipe';
|
|
7
|
-
import { codes as Errors } from '../../errors.js';
|
|
8
|
-
import { stop } from './stop.js';
|
|
9
|
-
import { RELAY_CODEC } from '../multicodec.js';
|
|
10
|
-
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
11
|
-
const log = logger('libp2p:circuit:hop');
|
|
12
|
-
export async function handleHop(hopRequest) {
|
|
13
|
-
const { connection, request, streamHandler, circuit, connectionManager } = hopRequest;
|
|
14
|
-
// Ensure hop is enabled
|
|
15
|
-
if (!circuit.hopEnabled()) {
|
|
16
|
-
log('HOP request received but we are not acting as a relay');
|
|
17
|
-
return streamHandler.end({
|
|
18
|
-
type: CircuitPB.Type.STATUS,
|
|
19
|
-
code: CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
// Validate the HOP request has the required input
|
|
23
|
-
try {
|
|
24
|
-
validateAddrs(request, streamHandler);
|
|
25
|
-
}
|
|
26
|
-
catch (err) {
|
|
27
|
-
log.error('invalid hop request via peer %p %o', connection.remotePeer, err);
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (request.dstPeer == null) {
|
|
31
|
-
log('HOP request received but we do not receive a dstPeer');
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
// Get the connection to the destination (stop) peer
|
|
35
|
-
const destinationPeer = peerIdFromBytes(request.dstPeer.id);
|
|
36
|
-
const destinationConnections = connectionManager.getConnections(destinationPeer);
|
|
37
|
-
if (destinationConnections.length === 0 && !circuit.hopActive()) {
|
|
38
|
-
log('HOP request received but we are not connected to the destination peer');
|
|
39
|
-
return streamHandler.end({
|
|
40
|
-
type: CircuitPB.Type.STATUS,
|
|
41
|
-
code: CircuitPB.Status.HOP_NO_CONN_TO_DST
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
// TODO: Handle being an active relay
|
|
45
|
-
if (destinationConnections.length === 0) {
|
|
46
|
-
log('did not have connection to remote peer');
|
|
47
|
-
return streamHandler.end({
|
|
48
|
-
type: CircuitPB.Type.STATUS,
|
|
49
|
-
code: CircuitPB.Status.HOP_NO_CONN_TO_DST
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
// Handle the incoming HOP request by performing a STOP request
|
|
53
|
-
const stopRequest = {
|
|
54
|
-
type: CircuitPB.Type.STOP,
|
|
55
|
-
dstPeer: request.dstPeer,
|
|
56
|
-
srcPeer: request.srcPeer
|
|
57
|
-
};
|
|
58
|
-
let destinationStream;
|
|
59
|
-
try {
|
|
60
|
-
log('performing STOP request');
|
|
61
|
-
const result = await stop({
|
|
62
|
-
connection: destinationConnections[0],
|
|
63
|
-
request: stopRequest
|
|
64
|
-
});
|
|
65
|
-
if (result == null) {
|
|
66
|
-
throw new Error('Could not stop');
|
|
67
|
-
}
|
|
68
|
-
destinationStream = result;
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
log.error(err);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
log('hop request from %p is valid', connection.remotePeer);
|
|
75
|
-
streamHandler.write({
|
|
76
|
-
type: CircuitPB.Type.STATUS,
|
|
77
|
-
code: CircuitPB.Status.SUCCESS
|
|
78
|
-
});
|
|
79
|
-
const sourceStream = streamHandler.rest();
|
|
80
|
-
log('creating related connections');
|
|
81
|
-
// Short circuit the two streams to create the relayed connection
|
|
82
|
-
return await pipe(sourceStream, destinationStream, sourceStream);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Performs a HOP request to a relay peer, to request a connection to another
|
|
86
|
-
* peer. A new, virtual, connection will be created between the two via the relay.
|
|
87
|
-
*/
|
|
88
|
-
export async function hop(options) {
|
|
89
|
-
const { connection, request, signal } = options;
|
|
90
|
-
// Create a new stream to the relay
|
|
91
|
-
const stream = await connection.newStream(RELAY_CODEC, {
|
|
92
|
-
signal
|
|
93
|
-
});
|
|
94
|
-
// Send the HOP request
|
|
95
|
-
const streamHandler = new StreamHandler({ stream });
|
|
96
|
-
streamHandler.write(request);
|
|
97
|
-
const response = await streamHandler.read();
|
|
98
|
-
if (response == null) {
|
|
99
|
-
throw errCode(new Error('HOP request had no response'), Errors.ERR_HOP_REQUEST_FAILED);
|
|
100
|
-
}
|
|
101
|
-
if (response.code === CircuitPB.Status.SUCCESS) {
|
|
102
|
-
log('hop request was successful');
|
|
103
|
-
return streamHandler.rest();
|
|
104
|
-
}
|
|
105
|
-
log('hop request failed with code %d, closing stream', response.code);
|
|
106
|
-
streamHandler.close();
|
|
107
|
-
throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Performs a CAN_HOP request to a relay peer, in order to understand its capabilities
|
|
111
|
-
*/
|
|
112
|
-
export async function canHop(options) {
|
|
113
|
-
const { connection, signal } = options;
|
|
114
|
-
// Create a new stream to the relay
|
|
115
|
-
const stream = await connection.newStream(RELAY_CODEC, {
|
|
116
|
-
signal
|
|
117
|
-
});
|
|
118
|
-
// Send the HOP request
|
|
119
|
-
const streamHandler = new StreamHandler({ stream });
|
|
120
|
-
streamHandler.write({
|
|
121
|
-
type: CircuitPB.Type.CAN_HOP
|
|
122
|
-
});
|
|
123
|
-
const response = await streamHandler.read();
|
|
124
|
-
await streamHandler.close();
|
|
125
|
-
if (response == null || response.code !== CircuitPB.Status.SUCCESS) {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
return true;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Creates an unencoded CAN_HOP response based on the Circuits configuration
|
|
132
|
-
*/
|
|
133
|
-
export function handleCanHop(options) {
|
|
134
|
-
const { connection, streamHandler, circuit } = options;
|
|
135
|
-
const canHop = circuit.hopEnabled();
|
|
136
|
-
log('can hop (%s) request from %p', canHop, connection.remotePeer);
|
|
137
|
-
streamHandler.end({
|
|
138
|
-
type: CircuitPB.Type.STATUS,
|
|
139
|
-
code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
//# sourceMappingURL=hop.js.map
|