@libp2p/circuit-relay-v2 3.2.24 → 4.0.0-049bfa0fa

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.
Files changed (52) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/constants.d.ts +1 -6
  4. package/dist/src/constants.d.ts.map +1 -1
  5. package/dist/src/constants.js +1 -6
  6. package/dist/src/constants.js.map +1 -1
  7. package/dist/src/index.d.ts +162 -7
  8. package/dist/src/index.d.ts.map +1 -1
  9. package/dist/src/index.js +12 -2
  10. package/dist/src/index.js.map +1 -1
  11. package/dist/src/server/index.d.ts +41 -45
  12. package/dist/src/server/index.d.ts.map +1 -1
  13. package/dist/src/server/index.js +32 -51
  14. package/dist/src/server/index.js.map +1 -1
  15. package/dist/src/server/reservation-store.d.ts +2 -36
  16. package/dist/src/server/reservation-store.d.ts.map +1 -1
  17. package/dist/src/server/reservation-store.js.map +1 -1
  18. package/dist/src/transport/discovery.d.ts +2 -17
  19. package/dist/src/transport/discovery.d.ts.map +1 -1
  20. package/dist/src/transport/discovery.js +2 -2
  21. package/dist/src/transport/discovery.js.map +1 -1
  22. package/dist/src/transport/index.d.ts +34 -42
  23. package/dist/src/transport/index.d.ts.map +1 -1
  24. package/dist/src/transport/index.js +291 -5
  25. package/dist/src/transport/index.js.map +1 -1
  26. package/dist/src/transport/reservation-store.d.ts +3 -37
  27. package/dist/src/transport/reservation-store.d.ts.map +1 -1
  28. package/dist/src/transport/reservation-store.js +4 -6
  29. package/dist/src/transport/reservation-store.js.map +1 -1
  30. package/dist/src/transport/stream-to-conn.d.ts +19 -0
  31. package/dist/src/transport/stream-to-conn.d.ts.map +1 -0
  32. package/dist/src/transport/stream-to-conn.js +60 -0
  33. package/dist/src/transport/stream-to-conn.js.map +1 -0
  34. package/dist/src/utils.d.ts.map +1 -1
  35. package/dist/src/utils.js +36 -59
  36. package/dist/src/utils.js.map +1 -1
  37. package/package.json +19 -24
  38. package/src/constants.ts +1 -7
  39. package/src/index.ts +198 -8
  40. package/src/server/index.ts +37 -105
  41. package/src/server/reservation-store.ts +2 -42
  42. package/src/transport/discovery.ts +4 -22
  43. package/src/transport/index.ts +338 -46
  44. package/src/transport/reservation-store.ts +8 -46
  45. package/src/transport/stream-to-conn.ts +91 -0
  46. package/src/utils.ts +37 -66
  47. package/dist/src/transport/transport.d.ts +0 -54
  48. package/dist/src/transport/transport.d.ts.map +0 -1
  49. package/dist/src/transport/transport.js +0 -314
  50. package/dist/src/transport/transport.js.map +0 -1
  51. package/dist/typedoc-urls.json +0 -23
  52. package/src/transport/transport.ts +0 -380
@@ -1,7 +1,3 @@
1
- /**
2
- * Multicodec code
3
- */
4
- export declare const CIRCUIT_PROTO_CODE = 290;
5
1
  /**
6
2
  * The maximum number of relay reservations the relay server will accept
7
3
  */
@@ -17,14 +13,13 @@ export declare const DEFAULT_RESERVATION_CONCURRENCY = 1;
17
13
  /**
18
14
  * How long to wait for a reservation attempt to finish
19
15
  */
20
- export declare const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 2000;
16
+ export declare const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 5000;
21
17
  /**
22
18
  * How long to let the reservation attempt queue to grow
23
19
  */
24
20
  export declare const DEFAULT_MAX_RESERVATION_QUEUE_LENGTH = 100;
25
21
  export declare const RELAY_SOURCE_TAG = "circuit-relay-source";
26
22
  export declare const KEEP_ALIVE_TAG = "keep-alive-circuit-relay";
27
- export declare const KEEP_ALIVE_SOURCE_TAG = "keep-alive-circuit-relay-source";
28
23
  export declare const DEFAULT_DURATION_LIMIT: number;
29
24
  export declare const DEFAULT_DATA_LIMIT: bigint;
30
25
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC;;GAEG;AACH,eAAO,MAAM,kCA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kB,QAAkB,CAAA;AAEjD;;GAEG;AACH,eAAO,MAAM,kBAAkB,oCAAoC,CAAA;AAEnE;;GAEG;AACH,eAAO,MAAM,mBAAmB,qCAAqC,CAAA;AAErE;;GAEG;AACH,eAAO,MAAM,mBAAmB,QAAc,CAAA;AAE9C;;GAEG;AACH,eAAO,MAAM,yBAAyB,QAAc,CAAA;AAEpD,eAAO,MAAM,eAAe,MAAM,CAAA;AAElC,eAAO,MAAM,6BAA6B,OAAO,CAAA;AACjD,eAAO,MAAM,mCAAmC,QAAQ,CAAA"}
@@ -1,10 +1,6 @@
1
1
  import { KEEP_ALIVE } from '@libp2p/interface';
2
2
  const second = 1000;
3
3
  const minute = 60 * second;
4
- /**
5
- * Multicodec code
6
- */
7
- export const CIRCUIT_PROTO_CODE = 290;
8
4
  /**
9
5
  * The maximum number of relay reservations the relay server will accept
10
6
  */
@@ -20,14 +16,13 @@ export const DEFAULT_RESERVATION_CONCURRENCY = 1;
20
16
  /**
21
17
  * How long to wait for a reservation attempt to finish
22
18
  */
23
- export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 2000;
19
+ export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 5_000;
24
20
  /**
25
21
  * How long to let the reservation attempt queue to grow
26
22
  */
27
23
  export const DEFAULT_MAX_RESERVATION_QUEUE_LENGTH = 100;
28
24
  export const RELAY_SOURCE_TAG = 'circuit-relay-source';
29
25
  export const KEEP_ALIVE_TAG = `${KEEP_ALIVE}-circuit-relay`;
30
- export const KEEP_ALIVE_SOURCE_TAG = `${KEEP_ALIVE}-circuit-relay-source`;
31
26
  // circuit v2 connection limits
32
27
  // https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/resources.go#L61-L66
33
28
  // 2 min is the default connection duration
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE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;AAEpD,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAA;AAElC,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAA;AACjD,MAAM,CAAC,MAAM,mCAAmC,GAAG,KAAK,CAAA"}
@@ -40,6 +40,8 @@
40
40
  */
41
41
  import { TypedEventEmitter } from 'main-event';
42
42
  import type { Limit } from './pb/index.js';
43
+ import type { ComponentLogger, ConnectionGater, Libp2pEvents, Metrics, PeerId, PeerStore, PrivateKey, TopologyFilter, Transport, TypedEventTarget, Upgrader } from '@libp2p/interface';
44
+ import type { AddressManager, ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal';
43
45
  import type { PeerMap } from '@libp2p/peer-collections';
44
46
  import type { Multiaddr } from '@multiformats/multiaddr';
45
47
  import type { RetimeableAbortSignal } from 'retimeable-signal';
@@ -71,12 +73,165 @@ export interface CircuitRelayServiceEvents {
71
73
  export interface CircuitRelayService extends TypedEventEmitter<CircuitRelayServiceEvents> {
72
74
  reservations: PeerMap<RelayReservation>;
73
75
  }
74
- export { circuitRelayServer } from './server/index.js';
75
- export type { CircuitRelayServerInit, CircuitRelayServerComponents } from './server/index.js';
76
- export type { ReservationStoreInit as ServerReservationStoreInit } from './server/reservation-store.js';
77
- export { circuitRelayTransport } from './transport/index.js';
78
- export type { RelayDiscoveryComponents } from './transport/discovery.js';
79
- export type { ReservationStoreInit as TransportReservationStoreInit } from './transport/reservation-store.js';
80
- export type { CircuitRelayTransportInit, CircuitRelayTransportComponents } from './transport/index.js';
81
76
  export { RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from './constants.js';
77
+ export interface ServerReservationStoreInit {
78
+ /**
79
+ * maximum number of reservations allowed
80
+ *
81
+ * @default 15
82
+ */
83
+ maxReservations?: number;
84
+ /**
85
+ * interval after which stale reservations are cleared
86
+ *
87
+ * @default 300000
88
+ */
89
+ reservationClearInterval?: number;
90
+ /**
91
+ * apply default relay limits to a new reservation
92
+ *
93
+ * @default true
94
+ */
95
+ applyDefaultLimit?: boolean;
96
+ /**
97
+ * reservation ttl
98
+ *
99
+ * @default 7200000
100
+ */
101
+ reservationTtl?: number;
102
+ /**
103
+ * The maximum time a relayed connection can be open for
104
+ */
105
+ defaultDurationLimit?: number;
106
+ /**
107
+ * The maximum amount of data allowed to be transferred over a relayed connection
108
+ */
109
+ defaultDataLimit?: bigint;
110
+ }
111
+ export interface CircuitRelayServerComponents {
112
+ registrar: Registrar;
113
+ peerStore: PeerStore;
114
+ addressManager: AddressManager;
115
+ peerId: PeerId;
116
+ privateKey: PrivateKey;
117
+ connectionManager: ConnectionManager;
118
+ connectionGater: ConnectionGater;
119
+ logger: ComponentLogger;
120
+ metrics?: Metrics;
121
+ }
122
+ export interface CircuitRelayServerInit {
123
+ /**
124
+ * Incoming hop requests must complete within this time in ms otherwise
125
+ * the stream will be reset
126
+ *
127
+ * @default 30000
128
+ */
129
+ hopTimeout?: number;
130
+ /**
131
+ * Configuration of reservations
132
+ */
133
+ reservations?: ServerReservationStoreInit;
134
+ /**
135
+ * The maximum number of simultaneous HOP inbound streams that can be open at once
136
+ */
137
+ maxInboundHopStreams?: number;
138
+ /**
139
+ * The maximum number of simultaneous HOP outbound streams that can be open at once
140
+ */
141
+ maxOutboundHopStreams?: number;
142
+ /**
143
+ * The maximum number of simultaneous STOP outbound streams that can be open at
144
+ * once.
145
+ *
146
+ * @default 300
147
+ */
148
+ maxOutboundStopStreams?: number;
149
+ }
150
+ export declare function circuitRelayServer(init?: CircuitRelayServerInit): (components: CircuitRelayServerComponents) => CircuitRelayService;
151
+ export interface RelayDiscoveryEvents {
152
+ 'relay:discover': CustomEvent<PeerId>;
153
+ }
154
+ export interface TransportReservationStoreComponents {
155
+ peerId: PeerId;
156
+ connectionManager: ConnectionManager;
157
+ peerStore: PeerStore;
158
+ events: TypedEventTarget<Libp2pEvents>;
159
+ logger: ComponentLogger;
160
+ metrics?: Metrics;
161
+ }
162
+ export interface TransportReservationStoreInit {
163
+ /**
164
+ * Multiple relays may be discovered simultaneously - to prevent listening
165
+ * on too many relays, this value controls how many to attempt to reserve a
166
+ * slot on at once. If set to more than one, we may end up listening on
167
+ * more relays than the `maxReservations` value, but on networks with poor
168
+ * connectivity the user may wish to attempt to reserve on multiple relays
169
+ * simultaneously.
170
+ *
171
+ * @default 1
172
+ */
173
+ reservationConcurrency?: number;
174
+ /**
175
+ * Limit the number of potential relays we will dial
176
+ *
177
+ * @default 100
178
+ */
179
+ maxReservationQueueLength?: number;
180
+ /**
181
+ * When creating a reservation it must complete within this number of ms
182
+ *
183
+ * @default 5000
184
+ */
185
+ reservationCompletionTimeout?: number;
186
+ }
187
+ export interface RelayDiscoveryComponents {
188
+ peerStore: PeerStore;
189
+ connectionManager: ConnectionManager;
190
+ transportManager: TransportManager;
191
+ registrar: Registrar;
192
+ logger: ComponentLogger;
193
+ randomWalk: RandomWalk;
194
+ events: TypedEventTarget<Libp2pEvents>;
195
+ }
196
+ export interface RelayDiscoveryInit {
197
+ filter?: TopologyFilter;
198
+ }
199
+ export interface CircuitRelayTransportComponents extends RelayDiscoveryComponents {
200
+ peerId: PeerId;
201
+ upgrader: Upgrader;
202
+ addressManager: AddressManager;
203
+ connectionGater: ConnectionGater;
204
+ }
205
+ /**
206
+ * RelayConfig configures the circuit v2 relay transport.
207
+ */
208
+ export interface CircuitRelayTransportInit extends TransportReservationStoreInit {
209
+ /**
210
+ * An optional filter used to prevent duplicate attempts to reserve relay
211
+ * slots on the same peer
212
+ */
213
+ discoveryFilter?: TopologyFilter;
214
+ /**
215
+ * The maximum number of simultaneous STOP inbound streams that can be open at
216
+ * once - each inbound relayed connection uses a STOP stream
217
+ *
218
+ * @default 300
219
+ */
220
+ maxInboundStopStreams?: number;
221
+ /**
222
+ * The maximum number of simultaneous STOP outbound streams that can be open
223
+ * at once. If this transport is used along with the relay server these
224
+ * settings should be set to the same value
225
+ *
226
+ * @default 300
227
+ */
228
+ maxOutboundStopStreams?: number;
229
+ /**
230
+ * When creating a reservation it must complete within this number of ms
231
+ *
232
+ * @default 10_000
233
+ */
234
+ reservationCompletionTimeout?: number;
235
+ }
236
+ export declare function circuitRelayTransport(init?: CircuitRelayTransportInit): (components: CircuitRelayTransportComponents) => Transport;
82
237
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAE9D,YAAY,EAAE,KAAK,EAAE,CAAA;AAErB,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,MAAM,EAAE,IAAI,CAAA;IAEZ;;OAEG;IACH,IAAI,EAAE,SAAS,CAAA;IAEf;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAA;IAEb;;OAEG;IACH,MAAM,EAAE,qBAAqB,CAAA;CAC9B;AAED,MAAM,WAAW,yBAAyB;IACxC,mBAAmB,EAAE,WAAW,CAAC,gBAAgB,CAAC,CAAA;IAClD,sBAAsB,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IAC5C,oBAAoB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;CACzC;AAED,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB,CAAC,yBAAyB,CAAC;IACvF,YAAY,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAA;CACxC;AAED,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,YAAY,EAAE,sBAAsB,EAAE,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E,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,QAAQ,CAAA;IAClB,cAAc,EAAE,cAAc,CAAA;IAC9B,eAAe,EAAE,eAAe,CAAA;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,6BAA6B;IAC9E;;;OAGG;IACH,eAAe,CAAC,EAAE,cAAc,CAAA;IAEhC;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAE9B;;;;;;OAMG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAA;IAE/B;;;;OAIG;IACH,4BAA4B,CAAC,EAAE,MAAM,CAAA;CACtC;AAED,wBAAgB,qBAAqB,CAAE,IAAI,GAAE,yBAA8B,GAAG,CAAC,UAAU,EAAE,+BAA+B,KAAK,SAAS,CAIvI"}
package/dist/src/index.js CHANGED
@@ -39,7 +39,17 @@
39
39
  * ```
40
40
  */
41
41
  import { TypedEventEmitter } from 'main-event';
42
- export { circuitRelayServer } from './server/index.js';
43
- export { circuitRelayTransport } from './transport/index.js';
42
+ import { CircuitRelayServer } from './server/index.js';
43
+ import { CircuitRelayTransport } from "./transport/index.js";
44
44
  export { RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from './constants.js';
45
+ export function circuitRelayServer(init = {}) {
46
+ return (components) => {
47
+ return new CircuitRelayServer(components, init);
48
+ };
49
+ }
50
+ export function circuitRelayTransport(init = {}) {
51
+ return (components) => {
52
+ return new CircuitRelayTransport(components, init);
53
+ };
54
+ }
45
55
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAyC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAK5D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AA2C5D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA;AAuFvB,MAAM,UAAU,kBAAkB,CAAE,OAA+B,EAAE;IACnE,OAAO,CAAC,UAAU,EAAE,EAAE;QACpB,OAAO,IAAI,kBAAkB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACjD,CAAC,CAAA;AACH,CAAC;AAmGD,MAAM,UAAU,qBAAqB,CAAE,OAAkC,EAAE;IACzE,OAAO,CAAC,UAAU,EAAE,EAAE;QACpB,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC,CAAA;AACH,CAAC"}
@@ -1,37 +1,10 @@
1
+ import { TypedEventEmitter } from 'main-event';
1
2
  import { HopMessage, StopMessage } from '../pb/index.js';
2
- import type { ReservationStoreInit } from './reservation-store.js';
3
- import type { CircuitRelayService, RelayReservation } from '../index.js';
4
- import type { ComponentLogger, Connection, Stream, ConnectionGater, PeerId, PeerStore, PrivateKey, Metrics } from '@libp2p/interface';
5
- import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal';
6
- import type { ProtobufStream } from 'it-protobuf-stream';
7
- export interface CircuitRelayServerInit {
8
- /**
9
- * Incoming hop requests must complete within this time in ms otherwise
10
- * the stream will be reset
11
- *
12
- * @default 30000
13
- */
14
- hopTimeout?: number;
15
- /**
16
- * Configuration of reservations
17
- */
18
- reservations?: ReservationStoreInit;
19
- /**
20
- * The maximum number of simultaneous HOP inbound streams that can be open at once
21
- */
22
- maxInboundHopStreams?: number;
23
- /**
24
- * The maximum number of simultaneous HOP outbound streams that can be open at once
25
- */
26
- maxOutboundHopStreams?: number;
27
- /**
28
- * The maximum number of simultaneous STOP outbound streams that can be open at
29
- * once.
30
- *
31
- * @default 300
32
- */
33
- maxOutboundStopStreams?: number;
34
- }
3
+ import type { CircuitRelayServerComponents, CircuitRelayServerInit, CircuitRelayService, RelayReservation } from '../index.js';
4
+ import type { Reservation } from '../pb/index.js';
5
+ import type { Connection, Stream, PeerId, Startable, AbortOptions } from '@libp2p/interface';
6
+ import type { PeerMap } from '@libp2p/peer-collections';
7
+ import type { ProtobufStream } from '@libp2p/utils';
35
8
  export interface HopProtocolOptions {
36
9
  connection: Connection;
37
10
  request: HopMessage;
@@ -41,21 +14,44 @@ export interface StopOptions {
41
14
  connection: Connection;
42
15
  request: StopMessage;
43
16
  }
44
- export interface CircuitRelayServerComponents {
45
- registrar: Registrar;
46
- peerStore: PeerStore;
47
- addressManager: AddressManager;
48
- peerId: PeerId;
49
- privateKey: PrivateKey;
50
- connectionManager: ConnectionManager;
51
- connectionGater: ConnectionGater;
52
- logger: ComponentLogger;
53
- metrics?: Metrics;
54
- }
55
17
  export interface RelayServerEvents {
56
18
  'relay:reservation': CustomEvent<RelayReservation>;
57
19
  'relay:advert:success': CustomEvent<unknown>;
58
20
  'relay:advert:error': CustomEvent<Error>;
59
21
  }
60
- export declare function circuitRelayServer(init?: CircuitRelayServerInit): (components: CircuitRelayServerComponents) => CircuitRelayService;
22
+ export declare class CircuitRelayServer extends TypedEventEmitter<RelayServerEvents> implements Startable, CircuitRelayService {
23
+ private readonly components;
24
+ private readonly reservationStore;
25
+ private started;
26
+ private readonly hopTimeout;
27
+ private readonly shutdownController;
28
+ private readonly maxInboundHopStreams?;
29
+ private readonly maxOutboundHopStreams?;
30
+ private readonly maxOutboundStopStreams;
31
+ private readonly log;
32
+ /**
33
+ * Creates an instance of Relay
34
+ */
35
+ constructor(components: CircuitRelayServerComponents, init?: CircuitRelayServerInit);
36
+ readonly [Symbol.toStringTag] = "@libp2p/circuit-relay-v2-server";
37
+ isStarted(): boolean;
38
+ /**
39
+ * Start Relay service
40
+ */
41
+ start(): Promise<void>;
42
+ /**
43
+ * Stop Relay service
44
+ */
45
+ stop(): Promise<void>;
46
+ onHop(stream: Stream, connection: Connection): Promise<void>;
47
+ handleHopProtocol({ stream, request, connection }: HopProtocolOptions, options: AbortOptions): Promise<void>;
48
+ handleReserve({ stream, connection }: HopProtocolOptions, options: AbortOptions): Promise<void>;
49
+ makeReservation(remotePeer: PeerId, expire: bigint): Promise<Reservation>;
50
+ handleConnect({ stream, request, connection }: HopProtocolOptions, options: AbortOptions): Promise<void>;
51
+ /**
52
+ * Send a STOP request to the target peer that the dialing peer wants to contact
53
+ */
54
+ stopHop({ connection, request }: StopOptions, options: AbortOptions): Promise<Stream | undefined>;
55
+ get reservations(): PeerMap<RelayReservation>;
56
+ }
61
57
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,UAAU,EAAU,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAIhE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAExE,OAAO,KAAK,EAAE,eAAe,EAAU,UAAU,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,SAAS,EAAa,UAAU,EAAE,OAAO,EAAoC,MAAM,mBAAmB,CAAA;AAC1L,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAG9F,OAAO,KAAK,EAAE,cAAc,E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1
  import { publicKeyToProtobuf } from '@libp2p/crypto/keys';
2
2
  import { peerIdFromMultihash } from '@libp2p/peer-id';
3
3
  import { RecordEnvelope } from '@libp2p/peer-record';
4
+ import { pbStream } from '@libp2p/utils';
4
5
  import { multiaddr } from '@multiformats/multiaddr';
5
- import { pbStream } from 'it-protobuf-stream';
6
+ import { Circuit } from '@multiformats/multiaddr-matcher';
6
7
  import { TypedEventEmitter, setMaxListeners } from 'main-event';
7
8
  import * as Digest from 'multiformats/hashes/digest';
8
- import { CIRCUIT_PROTO_CODE, DEFAULT_HOP_TIMEOUT, KEEP_ALIVE_SOURCE_TAG, MAX_CONNECTIONS, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
9
+ import { DEFAULT_HOP_TIMEOUT, MAX_CONNECTIONS, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
9
10
  import { HopMessage, Status, StopMessage } from '../pb/index.js';
10
11
  import { createLimitedRelay } from '../utils.js';
11
12
  import { ReservationStore } from './reservation-store.js';
12
13
  import { ReservationVoucherRecord } from './reservation-voucher.js';
13
- const isRelayAddr = (ma) => ma.protoCodes().includes(CIRCUIT_PROTO_CODE);
14
14
  const defaults = {
15
15
  maxOutboundStopStreams: MAX_CONNECTIONS
16
16
  };
17
- class CircuitRelayServer extends TypedEventEmitter {
18
- registrar;
19
- peerStore;
20
- addressManager;
21
- peerId;
22
- privateKey;
23
- connectionManager;
24
- connectionGater;
17
+ export class CircuitRelayServer extends TypedEventEmitter {
18
+ components;
25
19
  reservationStore;
26
20
  started;
27
21
  hopTimeout;
@@ -36,13 +30,7 @@ class CircuitRelayServer extends TypedEventEmitter {
36
30
  constructor(components, init = {}) {
37
31
  super();
38
32
  this.log = components.logger.forComponent('libp2p:circuit-relay:server');
39
- this.registrar = components.registrar;
40
- this.peerStore = components.peerStore;
41
- this.addressManager = components.addressManager;
42
- this.peerId = components.peerId;
43
- this.privateKey = components.privateKey;
44
- this.connectionManager = components.connectionManager;
45
- this.connectionGater = components.connectionGater;
33
+ this.components = components;
46
34
  this.started = false;
47
35
  this.hopTimeout = init?.hopTimeout ?? DEFAULT_HOP_TIMEOUT;
48
36
  this.maxInboundHopStreams = init.maxInboundHopStreams;
@@ -51,6 +39,7 @@ class CircuitRelayServer extends TypedEventEmitter {
51
39
  this.reservationStore = new ReservationStore(components, init.reservations);
52
40
  this.shutdownController = new AbortController();
53
41
  setMaxListeners(Infinity, this.shutdownController.signal);
42
+ this.onHop = this.onHop.bind(this);
54
43
  }
55
44
  [Symbol.toStringTag] = '@libp2p/circuit-relay-v2-server';
56
45
  isStarted() {
@@ -63,11 +52,7 @@ class CircuitRelayServer extends TypedEventEmitter {
63
52
  if (this.started) {
64
53
  return;
65
54
  }
66
- await this.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
67
- void this.onHop(data).catch(err => {
68
- this.log.error(err);
69
- });
70
- }, {
55
+ await this.components.registrar.handle(RELAY_V2_HOP_CODEC, this.onHop, {
71
56
  maxInboundStreams: this.maxInboundHopStreams,
72
57
  maxOutboundStreams: this.maxOutboundHopStreams,
73
58
  runOnLimitedConnection: true
@@ -80,13 +65,15 @@ class CircuitRelayServer extends TypedEventEmitter {
80
65
  async stop() {
81
66
  this.reservationStore.clear();
82
67
  this.shutdownController.abort();
83
- await this.registrar.unhandle(RELAY_V2_HOP_CODEC);
68
+ await this.components.registrar.unhandle(RELAY_V2_HOP_CODEC);
84
69
  this.started = false;
85
70
  }
86
- async onHop({ connection, stream }) {
71
+ async onHop(stream, connection) {
87
72
  this.log('received circuit v2 hop protocol stream from %p', connection.remotePeer);
73
+ const signal = AbortSignal.timeout(this.hopTimeout);
74
+ setMaxListeners(Infinity, signal);
88
75
  const options = {
89
- signal: AbortSignal.timeout(this.hopTimeout)
76
+ signal
90
77
  };
91
78
  const pbstr = pbStream(stream);
92
79
  try {
@@ -128,12 +115,12 @@ class CircuitRelayServer extends TypedEventEmitter {
128
115
  async handleReserve({ stream, connection }, options) {
129
116
  const hopstr = stream.pb(HopMessage);
130
117
  this.log('hop reserve request from %p', connection.remotePeer);
131
- if (isRelayAddr(connection.remoteAddr)) {
118
+ if (Circuit.exactMatch(connection.remoteAddr)) {
132
119
  this.log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
133
120
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
134
121
  return;
135
122
  }
136
- if ((await this.connectionGater.denyInboundRelayReservation?.(connection.remotePeer)) === true) {
123
+ if ((await this.components.connectionGater.denyInboundRelayReservation?.(connection.remotePeer)) === true) {
137
124
  this.log.error('reservation for %p denied by connection gater', connection.remotePeer);
138
125
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
139
126
  return;
@@ -148,12 +135,11 @@ class CircuitRelayServer extends TypedEventEmitter {
148
135
  // result.expire is non-null if `ReservationStore.reserve` returns with status == OK
149
136
  if (result.expire != null) {
150
137
  const ttl = (result.expire * 1000) - Date.now();
151
- await this.peerStore.merge(connection.remotePeer, {
138
+ await this.components.peerStore.merge(connection.remotePeer, {
152
139
  tags: {
153
- [RELAY_SOURCE_TAG]: { value: 1, ttl },
154
- [KEEP_ALIVE_SOURCE_TAG]: { value: 1, ttl }
140
+ [RELAY_SOURCE_TAG]: { value: 1, ttl }
155
141
  }
156
- });
142
+ }, options);
157
143
  }
158
144
  await hopstr.write({
159
145
  type: HopMessage.Type.STATUS,
@@ -162,17 +148,18 @@ class CircuitRelayServer extends TypedEventEmitter {
162
148
  limit: this.reservationStore.get(connection.remotePeer)?.limit
163
149
  }, options);
164
150
  this.log('sent confirmation response to %s', connection.remotePeer);
151
+ // close writable end of stream
152
+ await hopstr.unwrap().unwrap().close(options);
165
153
  }
166
154
  catch (err) {
167
155
  this.log.error('failed to send confirmation response to %p - %e', connection.remotePeer, err);
168
156
  this.reservationStore.removeReservation(connection.remotePeer);
169
157
  try {
170
- await this.peerStore.merge(connection.remotePeer, {
158
+ await this.components.peerStore.merge(connection.remotePeer, {
171
159
  tags: {
172
- [RELAY_SOURCE_TAG]: undefined,
173
- [KEEP_ALIVE_SOURCE_TAG]: undefined
160
+ [RELAY_SOURCE_TAG]: undefined
174
161
  }
175
- });
162
+ }, options);
176
163
  }
177
164
  catch (err) {
178
165
  this.log.error('failed to untag relay source peer %p - %e', connection.remotePeer, err);
@@ -181,7 +168,7 @@ class CircuitRelayServer extends TypedEventEmitter {
181
168
  }
182
169
  async makeReservation(remotePeer, expire) {
183
170
  const addrs = [];
184
- for (const relayAddr of this.addressManager.getAddresses()) {
171
+ for (const relayAddr of this.components.addressManager.getAddresses()) {
185
172
  if (relayAddr.toString().includes('/p2p-circuit')) {
186
173
  continue;
187
174
  }
@@ -189,9 +176,9 @@ class CircuitRelayServer extends TypedEventEmitter {
189
176
  }
190
177
  const envelope = await RecordEnvelope.seal(new ReservationVoucherRecord({
191
178
  peer: remotePeer,
192
- relay: this.peerId,
179
+ relay: this.components.peerId,
193
180
  expiration: expire
194
- }), this.privateKey);
181
+ }), this.components.privateKey);
195
182
  return {
196
183
  addrs,
197
184
  expire,
@@ -200,7 +187,7 @@ class CircuitRelayServer extends TypedEventEmitter {
200
187
  payloadType: envelope.payloadType,
201
188
  payload: {
202
189
  peer: remotePeer.toMultihash().bytes,
203
- relay: this.peerId.toMultihash().bytes,
190
+ relay: this.components.peerId.toMultihash().bytes,
204
191
  expiration: expire
205
192
  },
206
193
  signature: envelope.signature
@@ -209,7 +196,7 @@ class CircuitRelayServer extends TypedEventEmitter {
209
196
  }
210
197
  async handleConnect({ stream, request, connection }, options) {
211
198
  const hopstr = stream.pb(HopMessage);
212
- if (isRelayAddr(connection.remoteAddr)) {
199
+ if (Circuit.matches(connection.remoteAddr)) {
213
200
  this.log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
214
201
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
215
202
  return;
@@ -235,12 +222,12 @@ class CircuitRelayServer extends TypedEventEmitter {
235
222
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }, options);
236
223
  return;
237
224
  }
238
- if ((await this.connectionGater.denyOutboundRelayedConnection?.(connection.remotePeer, dstPeer)) === true) {
225
+ if ((await this.components.connectionGater.denyOutboundRelayedConnection?.(connection.remotePeer, dstPeer)) === true) {
239
226
  this.log.error('hop connect for %p to %p denied by connection gater', connection.remotePeer, dstPeer);
240
227
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
241
228
  return;
242
229
  }
243
- const connections = this.connectionManager.getConnections(dstPeer);
230
+ const connections = this.components.connectionManager.getConnections(dstPeer);
244
231
  if (connections.length === 0) {
245
232
  this.log('hop connect denied for destination peer %p not having a connection for %p as there is no destination connection', dstPeer, connection.remotePeer);
246
233
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }, options);
@@ -268,10 +255,9 @@ class CircuitRelayServer extends TypedEventEmitter {
268
255
  status: Status.OK,
269
256
  limit: reservation?.limit
270
257
  }, options);
271
- const sourceStream = stream.unwrap();
272
258
  this.log('connection from %p to %p established - merging streams', connection.remotePeer, dstPeer);
273
259
  // Short circuit the two streams to create the relayed connection
274
- createLimitedRelay(sourceStream, destinationStream, this.shutdownController.signal, reservation, {
260
+ createLimitedRelay(stream.unwrap(), destinationStream, this.shutdownController.signal, reservation, {
275
261
  log: this.log
276
262
  });
277
263
  }
@@ -280,7 +266,7 @@ class CircuitRelayServer extends TypedEventEmitter {
280
266
  */
281
267
  async stopHop({ connection, request }, options) {
282
268
  this.log('starting circuit relay v2 stop request to %s', connection.remotePeer);
283
- const stream = await connection.newStream([RELAY_V2_STOP_CODEC], {
269
+ const stream = await connection.newStream(RELAY_V2_STOP_CODEC, {
284
270
  maxOutboundStreams: this.maxOutboundStopStreams,
285
271
  runOnLimitedConnection: true,
286
272
  ...options
@@ -311,9 +297,4 @@ class CircuitRelayServer extends TypedEventEmitter {
311
297
  return this.reservationStore.reservations;
312
298
  }
313
299
  }
314
- export function circuitRelayServer(init = {}) {
315
- return (components) => {
316
- return new CircuitRelayServer(components, init);
317
- };
318
- }
319
300
  //# sourceMappingURL=index.js.map