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.
Files changed (136) hide show
  1. package/dist/index.min.js +29 -29
  2. package/dist/src/circuit/client.d.ts +81 -0
  3. package/dist/src/circuit/client.d.ts.map +1 -0
  4. package/dist/src/circuit/client.js +285 -0
  5. package/dist/src/circuit/client.js.map +1 -0
  6. package/dist/src/circuit/constants.d.ts +7 -8
  7. package/dist/src/circuit/constants.d.ts.map +1 -1
  8. package/dist/src/circuit/constants.js +11 -8
  9. package/dist/src/circuit/constants.js.map +1 -1
  10. package/dist/src/circuit/hop.d.ts +23 -0
  11. package/dist/src/circuit/hop.d.ts.map +1 -0
  12. package/dist/src/circuit/hop.js +167 -0
  13. package/dist/src/circuit/hop.js.map +1 -0
  14. package/dist/src/circuit/index.d.ts +47 -47
  15. package/dist/src/circuit/index.d.ts.map +1 -1
  16. package/dist/src/circuit/index.js +1 -68
  17. package/dist/src/circuit/index.js.map +1 -1
  18. package/dist/src/circuit/interfaces.d.ts +27 -0
  19. package/dist/src/circuit/interfaces.d.ts.map +1 -0
  20. package/dist/src/circuit/interfaces.js +2 -0
  21. package/dist/src/circuit/interfaces.js.map +1 -0
  22. package/dist/src/circuit/multicodec.d.ts +2 -1
  23. package/dist/src/circuit/multicodec.d.ts.map +1 -1
  24. package/dist/src/circuit/multicodec.js +2 -1
  25. package/dist/src/circuit/multicodec.js.map +1 -1
  26. package/dist/src/circuit/pb/index.d.ts +83 -43
  27. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  28. package/dist/src/circuit/pb/index.js +363 -127
  29. package/dist/src/circuit/pb/index.js.map +1 -1
  30. package/dist/src/circuit/relay.d.ts +34 -0
  31. package/dist/src/circuit/relay.d.ts.map +1 -0
  32. package/dist/src/circuit/relay.js +61 -0
  33. package/dist/src/circuit/relay.js.map +1 -0
  34. package/dist/src/circuit/reservation-store.d.ts +42 -0
  35. package/dist/src/circuit/reservation-store.d.ts.map +1 -0
  36. package/dist/src/circuit/reservation-store.js +59 -0
  37. package/dist/src/circuit/reservation-store.js.map +1 -0
  38. package/dist/src/circuit/reservation-voucher.d.ts +18 -0
  39. package/dist/src/circuit/reservation-voucher.d.ts.map +1 -0
  40. package/dist/src/circuit/reservation-voucher.js +33 -0
  41. package/dist/src/circuit/reservation-voucher.js.map +1 -0
  42. package/dist/src/circuit/stop.d.ts +19 -0
  43. package/dist/src/circuit/stop.d.ts.map +1 -0
  44. package/dist/src/circuit/stop.js +67 -0
  45. package/dist/src/circuit/stop.js.map +1 -0
  46. package/dist/src/circuit/transport.d.ts +24 -9
  47. package/dist/src/circuit/transport.d.ts.map +1 -1
  48. package/dist/src/circuit/transport.js +133 -118
  49. package/dist/src/circuit/transport.js.map +1 -1
  50. package/dist/src/circuit/utils.d.ts +5 -0
  51. package/dist/src/circuit/utils.d.ts.map +1 -1
  52. package/dist/src/circuit/utils.js +114 -0
  53. package/dist/src/circuit/utils.js.map +1 -1
  54. package/dist/src/config.d.ts.map +1 -1
  55. package/dist/src/config.js +2 -3
  56. package/dist/src/config.js.map +1 -1
  57. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  58. package/dist/src/connection-manager/dialer/dial-request.js +7 -0
  59. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
  60. package/dist/src/errors.d.ts +2 -1
  61. package/dist/src/errors.d.ts.map +1 -1
  62. package/dist/src/errors.js +1 -0
  63. package/dist/src/errors.js.map +1 -1
  64. package/dist/src/fetch/pb/proto.d.ts +3 -3
  65. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  66. package/dist/src/fetch/pb/proto.js +4 -3
  67. package/dist/src/fetch/pb/proto.js.map +1 -1
  68. package/dist/src/identify/pb/message.d.ts +2 -2
  69. package/dist/src/identify/pb/message.d.ts.map +1 -1
  70. package/dist/src/identify/pb/message.js +1 -0
  71. package/dist/src/identify/pb/message.js.map +1 -1
  72. package/dist/src/insecure/pb/proto.d.ts +3 -3
  73. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  74. package/dist/src/insecure/pb/proto.js +4 -5
  75. package/dist/src/insecure/pb/proto.js.map +1 -1
  76. package/dist/src/libp2p.d.ts +2 -0
  77. package/dist/src/libp2p.d.ts.map +1 -1
  78. package/dist/src/libp2p.js +10 -3
  79. package/dist/src/libp2p.js.map +1 -1
  80. package/dist/src/transport-manager.d.ts.map +1 -1
  81. package/dist/src/transport-manager.js +2 -3
  82. package/dist/src/transport-manager.js.map +1 -1
  83. package/dist/src/upgrader.d.ts.map +1 -1
  84. package/dist/src/upgrader.js.map +1 -1
  85. package/package.json +12 -11
  86. package/src/circuit/client.ts +381 -0
  87. package/src/circuit/constants.ts +14 -8
  88. package/src/circuit/hop.ts +210 -0
  89. package/src/circuit/index.ts +49 -111
  90. package/src/circuit/interfaces.ts +28 -0
  91. package/src/circuit/multicodec.ts +2 -1
  92. package/src/circuit/pb/index.proto +57 -32
  93. package/src/circuit/pb/index.ts +448 -138
  94. package/src/circuit/relay.ts +87 -0
  95. package/src/circuit/reservation-store.ts +106 -0
  96. package/src/circuit/reservation-voucher.ts +51 -0
  97. package/src/circuit/stop.ts +92 -0
  98. package/src/circuit/transport.ts +166 -131
  99. package/src/circuit/utils.ts +145 -0
  100. package/src/config.ts +2 -3
  101. package/src/connection-manager/dialer/dial-request.ts +7 -0
  102. package/src/errors.ts +2 -1
  103. package/src/fetch/pb/proto.ts +8 -7
  104. package/src/identify/pb/message.ts +3 -2
  105. package/src/insecure/pb/proto.ts +8 -9
  106. package/src/libp2p.ts +12 -3
  107. package/src/transport-manager.ts +2 -3
  108. package/src/upgrader.ts +4 -4
  109. package/src/version.ts +1 -1
  110. package/dist/src/circuit/auto-relay.d.ts +0 -46
  111. package/dist/src/circuit/auto-relay.d.ts.map +0 -1
  112. package/dist/src/circuit/auto-relay.js +0 -220
  113. package/dist/src/circuit/auto-relay.js.map +0 -1
  114. package/dist/src/circuit/circuit/hop.d.ts +0 -42
  115. package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
  116. package/dist/src/circuit/circuit/hop.js +0 -142
  117. package/dist/src/circuit/circuit/hop.js.map +0 -1
  118. package/dist/src/circuit/circuit/stop.d.ts +0 -24
  119. package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
  120. package/dist/src/circuit/circuit/stop.js +0 -51
  121. package/dist/src/circuit/circuit/stop.js.map +0 -1
  122. package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
  123. package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
  124. package/dist/src/circuit/circuit/stream-handler.js +0 -59
  125. package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
  126. package/dist/src/circuit/circuit/utils.d.ts +0 -7
  127. package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
  128. package/dist/src/circuit/circuit/utils.js +0 -43
  129. package/dist/src/circuit/circuit/utils.js.map +0 -1
  130. package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
  131. package/src/circuit/README.md +0 -111
  132. package/src/circuit/auto-relay.ts +0 -292
  133. package/src/circuit/circuit/hop.ts +0 -220
  134. package/src/circuit/circuit/stop.ts +0 -83
  135. package/src/circuit/circuit/stream-handler.ts +0 -87
  136. package/src/circuit/circuit/utils.ts +0 -44
@@ -0,0 +1,87 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import { codes } from '../errors.js'
3
+ import {
4
+ setDelayedInterval,
5
+ clearDelayedInterval
6
+ // @ts-expect-error set-delayed-interval does not export types
7
+ } from 'set-delayed-interval'
8
+ import { namespaceToCid } from './utils.js'
9
+ import {
10
+ RELAY_RENDEZVOUS_NS
11
+ } from './constants.js'
12
+ import type { AddressSorter } from '@libp2p/interface-peer-store'
13
+ import type { Startable } from '@libp2p/interfaces/startable'
14
+ import type { Components } from '../components.js'
15
+ import type { HopConfig, RelayAdvertiseConfig } from './index.js'
16
+
17
+ const log = logger('libp2p:circuit:relay')
18
+
19
+ export interface RelayInit {
20
+ addressSorter?: AddressSorter
21
+ maxListeners?: number
22
+ hop: HopConfig
23
+ advertise: RelayAdvertiseConfig
24
+ }
25
+
26
+ export class Relay implements Startable {
27
+ private readonly components: Components
28
+ private readonly init: RelayInit
29
+ private timeout?: any
30
+ private started: boolean
31
+
32
+ /**
33
+ * Creates an instance of Relay
34
+ */
35
+ constructor (components: Components, init: RelayInit) {
36
+ this.components = components
37
+ this.started = false
38
+ this.init = init
39
+ this._advertiseService = this._advertiseService.bind(this)
40
+ }
41
+
42
+ isStarted () {
43
+ return this.started
44
+ }
45
+
46
+ /**
47
+ * Start Relay service
48
+ */
49
+ async start () {
50
+ // Advertise service if HOP enabled and advertising enabled
51
+ if (this.init.hop.enabled === true && this.init.advertise.enabled === true) {
52
+ this.timeout = setDelayedInterval(
53
+ this._advertiseService, this.init.advertise.ttl, this.init.advertise.bootDelay
54
+ )
55
+ }
56
+
57
+ this.started = true
58
+ }
59
+
60
+ /**
61
+ * Stop Relay service
62
+ */
63
+ async stop () {
64
+ try {
65
+ clearDelayedInterval(this.timeout)
66
+ } catch (err) { }
67
+
68
+ this.started = false
69
+ }
70
+
71
+ /**
72
+ * Advertise hop relay service in the network.
73
+ */
74
+ async _advertiseService () {
75
+ try {
76
+ const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
77
+ await this.components.contentRouting.provide(cid)
78
+ } catch (err: any) {
79
+ if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) {
80
+ log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err)
81
+ await this.stop()
82
+ } else {
83
+ log.error('could not advertise service: ', err)
84
+ }
85
+ }
86
+ }
87
+ }
@@ -0,0 +1,106 @@
1
+ import { Limit, Status } from './pb/index.js'
2
+ import type { ReservationStore as IReservationStore, ReservationStatus, Reservation } from './interfaces.js'
3
+ import type { Multiaddr } from '@multiformats/multiaddr'
4
+ import type { PeerId } from '@libp2p/interface-peer-id'
5
+ import type { Startable } from '@libp2p/interfaces/startable'
6
+ import { PeerMap } from '@libp2p/peer-collections'
7
+ import type { RecursivePartial } from '@libp2p/interfaces'
8
+ import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT } from './constants.js'
9
+
10
+ export interface ReservationStoreInit {
11
+ /*
12
+ * maximum number of reservations allowed, default: 15
13
+ */
14
+ maxReservations: number
15
+ /*
16
+ * interval after which stale reservations are cleared, default: 300s
17
+ */
18
+ reservationClearInterval: number
19
+ /*
20
+ * apply default relay limits to a new reservation, default: true
21
+ */
22
+ applyDefaultLimit: boolean
23
+ /**
24
+ * reservation ttl, default: 2 hours
25
+ */
26
+ reservationTtl: number
27
+ /**
28
+ * The maximum time a relayed connection can be open for
29
+ */
30
+ defaultDurationLimit?: number
31
+ /**
32
+ * The maximum amount of data allowed to be transferred over a relayed connection
33
+ */
34
+ defaultDataLimit?: bigint
35
+ }
36
+
37
+ export type ReservationStoreOptions = RecursivePartial<ReservationStoreInit>
38
+
39
+ export class ReservationStore implements IReservationStore, Startable {
40
+ private readonly reservations = new PeerMap<Reservation>()
41
+ private _started = false;
42
+ private interval: any
43
+ private readonly init: ReservationStoreInit
44
+
45
+ constructor (options?: ReservationStoreOptions) {
46
+ this.init = {
47
+ maxReservations: options?.maxReservations ?? 15,
48
+ reservationClearInterval: options?.reservationClearInterval ?? 300 * 1000,
49
+ applyDefaultLimit: options?.applyDefaultLimit !== false,
50
+ reservationTtl: options?.reservationTtl ?? 2 * 60 * 60 * 1000,
51
+ defaultDurationLimit: options?.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT,
52
+ defaultDataLimit: options?.defaultDataLimit ?? DEFAULT_DATA_LIMIT
53
+ }
54
+ }
55
+
56
+ isStarted () {
57
+ return this._started
58
+ }
59
+
60
+ start () {
61
+ if (this._started) {
62
+ return
63
+ }
64
+ this._started = true
65
+ this.interval = setInterval(
66
+ () => {
67
+ const now = (new Date()).getTime()
68
+ this.reservations.forEach((r, k) => {
69
+ if (r.expire.getTime() < now) {
70
+ this.reservations.delete(k)
71
+ }
72
+ })
73
+ },
74
+ this.init.reservationClearInterval
75
+ )
76
+ }
77
+
78
+ stop () {
79
+ clearInterval(this.interval)
80
+ }
81
+
82
+ reserve (peer: PeerId, addr: Multiaddr, limit?: Limit): { status: ReservationStatus, expire?: number } {
83
+ if (this.reservations.size >= this.init.maxReservations && !this.reservations.has(peer)) {
84
+ return { status: Status.RESERVATION_REFUSED }
85
+ }
86
+ const expire = new Date(Date.now() + this.init.reservationTtl)
87
+ let checkedLimit: Limit | undefined
88
+ if (this.init.applyDefaultLimit) {
89
+ checkedLimit = limit ?? { data: this.init.defaultDataLimit, duration: this.init.defaultDurationLimit }
90
+ }
91
+ this.reservations.set(peer, { addr, expire, limit: checkedLimit })
92
+ return { status: Status.OK, expire: expire.getTime() }
93
+ }
94
+
95
+ removeReservation (peer: PeerId): void {
96
+ this.reservations.delete(peer)
97
+ }
98
+
99
+ hasReservation (dst: PeerId): boolean {
100
+ return this.reservations.has(dst)
101
+ }
102
+
103
+ get (peer: PeerId): Reservation | undefined {
104
+ return this.reservations.get(peer)
105
+ }
106
+ }
@@ -0,0 +1,51 @@
1
+ import type { PeerId } from '@libp2p/interface-peer-id'
2
+ import type { Record } from '@libp2p/interface-record'
3
+ import { ReservationVoucher } from './pb/index.js'
4
+
5
+ export interface ReservationVoucherOptions {
6
+ relay: PeerId
7
+ peer: PeerId
8
+ expiration: number
9
+ }
10
+
11
+ export class ReservationVoucherRecord implements Record {
12
+ public readonly domain = 'libp2p-relay-rsvp'
13
+ public readonly codec = new Uint8Array([0x03, 0x02])
14
+
15
+ private readonly relay: PeerId
16
+ private readonly peer: PeerId
17
+ private readonly expiration: number
18
+
19
+ constructor ({ relay, peer, expiration }: ReservationVoucherOptions) {
20
+ this.relay = relay
21
+ this.peer = peer
22
+ this.expiration = expiration
23
+ }
24
+
25
+ marshal () {
26
+ return ReservationVoucher.encode({
27
+ relay: this.relay.toBytes(),
28
+ peer: this.peer.toBytes(),
29
+ expiration: BigInt(this.expiration)
30
+ })
31
+ }
32
+
33
+ equals (other: Record) {
34
+ if (!(other instanceof ReservationVoucherRecord)) {
35
+ return false
36
+ }
37
+ if (!this.peer.equals(other.peer)) {
38
+ return false
39
+ }
40
+
41
+ if (!this.relay.equals(other.relay)) {
42
+ return false
43
+ }
44
+
45
+ if (this.expiration !== other.expiration) {
46
+ return false
47
+ }
48
+
49
+ return true
50
+ }
51
+ }
@@ -0,0 +1,92 @@
1
+
2
+ import { Status, StopMessage } from './pb/index.js'
3
+ import type { Connection, Stream } from '@libp2p/interface-connection'
4
+
5
+ import { logger } from '@libp2p/logger'
6
+ import { RELAY_V2_STOP_CODEC } from './multicodec.js'
7
+ import { multiaddr } from '@multiformats/multiaddr'
8
+ import { pbStream, ProtobufStream } from 'it-pb-stream'
9
+
10
+ const log = logger('libp2p:circuit:v2:stop')
11
+
12
+ export interface HandleStopOptions {
13
+ connection: Connection
14
+ request: StopMessage
15
+ pbstr: ProtobufStream<Stream>
16
+ }
17
+
18
+ const isValidStop = (request: StopMessage): boolean => {
19
+ if (request.peer == null) {
20
+ return false
21
+ }
22
+ try {
23
+ request.peer.addrs.forEach(multiaddr)
24
+ } catch (_err) {
25
+ return false
26
+ }
27
+ return true
28
+ }
29
+ export async function handleStop ({
30
+ connection,
31
+ request,
32
+ pbstr
33
+ }: HandleStopOptions) {
34
+ const stopstr = pbstr.pb(StopMessage)
35
+ log('new circuit relay v2 stop stream from %s', connection.remotePeer)
36
+ // Validate the STOP request has the required input
37
+ if (request.type !== StopMessage.Type.CONNECT) {
38
+ log.error('invalid stop connect request via peer %s', connection.remotePeer)
39
+ stopstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE })
40
+ return
41
+ }
42
+ if (!isValidStop(request)) {
43
+ log.error('invalid stop connect request via peer %s', connection.remotePeer)
44
+ stopstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE })
45
+ return
46
+ }
47
+
48
+ // TODO: go-libp2p marks connection transient if there is limit field present in request.
49
+ // Cannot find any reference to transient connections in js-libp2p
50
+
51
+ stopstr.write({ type: StopMessage.Type.STATUS, status: Status.OK })
52
+ return pbstr.unwrap()
53
+ }
54
+
55
+ export interface StopOptions {
56
+ connection: Connection
57
+ request: StopMessage
58
+ }
59
+
60
+ /**
61
+ * Creates a STOP request
62
+ *
63
+ */
64
+ export async function stop ({
65
+ connection,
66
+ request
67
+ }: StopOptions): Promise<Stream | undefined> {
68
+ const stream = await connection.newStream([RELAY_V2_STOP_CODEC])
69
+ log('starting circuit relay v2 stop request to %s', connection.remotePeer)
70
+ const pbstr = pbStream(stream)
71
+ const stopstr = pbstr.pb(StopMessage)
72
+ stopstr.write(request)
73
+ let response
74
+ try {
75
+ response = await stopstr.read()
76
+ } catch (err) {
77
+ log.error('error parsing stop message response from %s', connection.remotePeer)
78
+ }
79
+
80
+ if (response == null) {
81
+ log.error('could not read response from %s', connection.remotePeer)
82
+ stream.close()
83
+ return
84
+ }
85
+ if (response.status === Status.OK) {
86
+ log('stop request to %s was successful', connection.remotePeer)
87
+ return pbstr.unwrap()
88
+ }
89
+
90
+ log('stop request failed with code %d', response.status)
91
+ stream.close()
92
+ }