@libp2p/circuit-relay-v2 2.1.3 → 2.1.4-5d199f9b6

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 (53) hide show
  1. package/dist/index.min.js +2 -2
  2. package/dist/src/constants.d.ts +4 -6
  3. package/dist/src/constants.d.ts.map +1 -1
  4. package/dist/src/constants.js +5 -6
  5. package/dist/src/constants.js.map +1 -1
  6. package/dist/src/index.d.ts +18 -3
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/index.js.map +1 -1
  9. package/dist/src/pb/index.d.ts +12 -1
  10. package/dist/src/pb/index.d.ts.map +1 -1
  11. package/dist/src/pb/index.js +78 -2
  12. package/dist/src/pb/index.js.map +1 -1
  13. package/dist/src/server/index.d.ts.map +1 -1
  14. package/dist/src/server/index.js +71 -60
  15. package/dist/src/server/index.js.map +1 -1
  16. package/dist/src/server/reservation-store.d.ts +5 -9
  17. package/dist/src/server/reservation-store.d.ts.map +1 -1
  18. package/dist/src/server/reservation-store.js +32 -33
  19. package/dist/src/server/reservation-store.js.map +1 -1
  20. package/dist/src/server/reservation-voucher.d.ts +1 -1
  21. package/dist/src/transport/discovery.js +1 -1
  22. package/dist/src/transport/discovery.js.map +1 -1
  23. package/dist/src/transport/index.d.ts +2 -2
  24. package/dist/src/transport/index.d.ts.map +1 -1
  25. package/dist/src/transport/listener.d.ts.map +1 -1
  26. package/dist/src/transport/listener.js +23 -22
  27. package/dist/src/transport/listener.js.map +1 -1
  28. package/dist/src/transport/reservation-store.d.ts +4 -3
  29. package/dist/src/transport/reservation-store.d.ts.map +1 -1
  30. package/dist/src/transport/reservation-store.js +111 -47
  31. package/dist/src/transport/reservation-store.js.map +1 -1
  32. package/dist/src/transport/transport.d.ts.map +1 -1
  33. package/dist/src/transport/transport.js +11 -8
  34. package/dist/src/transport/transport.js.map +1 -1
  35. package/dist/src/utils.d.ts +7 -1
  36. package/dist/src/utils.d.ts.map +1 -1
  37. package/dist/src/utils.js +19 -8
  38. package/dist/src/utils.js.map +1 -1
  39. package/package.json +12 -12
  40. package/src/constants.ts +7 -7
  41. package/src/index.ts +21 -3
  42. package/src/pb/index.proto +20 -1
  43. package/src/pb/index.ts +99 -3
  44. package/src/server/index.ts +75 -64
  45. package/src/server/reservation-store.ts +38 -42
  46. package/src/server/reservation-voucher.ts +2 -2
  47. package/src/transport/discovery.ts +1 -1
  48. package/src/transport/index.ts +2 -2
  49. package/src/transport/listener.ts +28 -25
  50. package/src/transport/reservation-store.ts +147 -57
  51. package/src/transport/transport.ts +12 -8
  52. package/src/utils.ts +21 -8
  53. package/dist/typedoc-urls.json +0 -23
@@ -1,8 +1,9 @@
1
1
  import { CID } from 'multiformats/cid';
2
+ import type { RelayReservation } from './index.js';
2
3
  import type { Limit } from './pb/index.js';
3
4
  import type { ConnectionLimits, LoggerOptions, Stream } from '@libp2p/interface';
4
5
  import type { Uint8ArrayList } from 'uint8arraylist';
5
- export declare function createLimitedRelay(src: Stream, dst: Stream, abortSignal: AbortSignal, limit: Limit | undefined, options: LoggerOptions): void;
6
+ export declare function createLimitedRelay(src: Stream, dst: Stream, abortSignal: AbortSignal, reservation: RelayReservation, options: LoggerOptions): void;
6
7
  /**
7
8
  * Convert a namespace string into a cid
8
9
  */
@@ -18,4 +19,9 @@ export declare class LimitTracker {
18
19
  onData(buf: Uint8ArrayList | Uint8Array): void;
19
20
  getLimits(): ConnectionLimits | undefined;
20
21
  }
22
+ /**
23
+ * A custom matcher that makes the `/p2p/peer-id` part of circuit relay
24
+ * addresses optional
25
+ */
26
+ export declare const CircuitListen: import("@multiformats/multiaddr-matcher").MultiaddrMatcher;
21
27
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAGtC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM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AW,EAAE,gBAAgB,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAwEnJ;AAED;;GAEG;AACH,wBAAsB,cAAc,CAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAKrE;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAE,iBAAiB,EAAE,MAAM,GAAG,MAAM,CAM5E;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAQ;IACjC,OAAO,CAAC,KAAK,CAAC,CAAQ;gBAET,MAAM,CAAC,EAAE,KAAK;IAc3B,MAAM,CAAE,GAAG,EAAE,cAAc,GAAG,UAAU,GAAG,IAAI;IAY/C,SAAS,IAAK,gBAAgB,GAAG,SAAS;CA6B3C;AAED;;;GAGG;AACH,eAAO,MAAM,aAAa,4DAAwE,CAAA"}
package/dist/src/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { P2P } from '@multiformats/multiaddr-matcher';
2
+ import { fmt, peerId, literal, optional, and } from '@multiformats/multiaddr-matcher/utils';
1
3
  import { anySignal } from 'any-signal';
2
4
  import { CID } from 'multiformats/cid';
3
5
  import { sha256 } from 'multiformats/hashes/sha2';
@@ -24,27 +26,30 @@ async function* countStreamBytes(source, limit, options) {
24
26
  yield buf;
25
27
  }
26
28
  }
27
- export function createLimitedRelay(src, dst, abortSignal, limit, options) {
29
+ export function createLimitedRelay(src, dst, abortSignal, reservation, options) {
28
30
  function abortStreams(err) {
29
31
  src.abort(err);
30
32
  dst.abort(err);
31
33
  }
32
- const signals = [abortSignal];
33
- if (limit?.duration != null) {
34
- signals.push(AbortSignal.timeout(limit.duration));
34
+ // combine shutdown signal and reservation expiry signal
35
+ const signals = [abortSignal, reservation.signal];
36
+ if (reservation.limit?.duration != null) {
37
+ options.log('limiting relayed connection duration to %dms', reservation.limit.duration);
38
+ signals.push(AbortSignal.timeout(reservation.limit.duration));
35
39
  }
36
40
  const signal = anySignal(signals);
37
41
  let srcDstFinished = false;
38
42
  let dstSrcFinished = false;
39
43
  let dataLimit;
40
- if (limit?.data != null) {
44
+ if (reservation.limit?.data != null) {
41
45
  dataLimit = {
42
- remaining: limit.data
46
+ remaining: reservation.limit.data
43
47
  };
44
48
  }
45
49
  queueMicrotask(() => {
46
50
  const onAbort = () => {
47
- dst.abort(new DurationLimitError(`duration limit of ${limit?.duration} ms exceeded`));
51
+ options.log('relayed connection reached time limit');
52
+ dst.abort(new DurationLimitError(`duration limit of ${reservation.limit?.duration} ms exceeded`));
48
53
  };
49
54
  signal.addEventListener('abort', onAbort, { once: true });
50
55
  void dst.sink(dataLimit == null ? src.source : countStreamBytes(src.source, dataLimit, options))
@@ -62,7 +67,8 @@ export function createLimitedRelay(src, dst, abortSignal, limit, options) {
62
67
  });
63
68
  queueMicrotask(() => {
64
69
  const onAbort = () => {
65
- src.abort(new DurationLimitError(`duration limit of ${limit?.duration} ms exceeded`));
70
+ options.log('relayed connection reached time limit');
71
+ src.abort(new DurationLimitError(`duration limit of ${reservation.limit?.duration} ms exceeded`));
66
72
  };
67
73
  signal.addEventListener('abort', onAbort, { once: true });
68
74
  void src.sink(dataLimit == null ? dst.source : countStreamBytes(dst.source, dataLimit, options))
@@ -142,4 +148,9 @@ export class LimitTracker {
142
148
  return output;
143
149
  }
144
150
  }
151
+ /**
152
+ * A custom matcher that makes the `/p2p/peer-id` part of circuit relay
153
+ * addresses optional
154
+ */
155
+ export const CircuitListen = fmt(and(P2P.matchers[0], literal('p2p-circuit'), optional(peerId())));
145
156
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,EAAE,kBAAkB,EAAE,kBAAk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CAAC;IAED,SAAS;QACP,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;YAC/C,OAAM;QACR,CAAC;QAED,MAAM,MAAM,GAAG,EAAE,CAAA;QAEjB,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,IAAI,GAAG,IAAI,CAAA;YAEjB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,OAAO,EAAE;gBACrC,GAAG;oBACD,OAAO,IAAI,CAAC,KAAK,CAAA;gBACnB,CAAC;aACF,CAAC,CAAA;QACJ,CAAC;QAED,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI,EAAE,CAAC;YACzB,MAAM,IAAI,GAAG,IAAI,CAAA;YAEjB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,SAAS,EAAE;gBACvC,GAAG;oBACD,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,CAAA;gBAC9D,CAAC;aACF,CAAC,CAAA;QACJ,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/circuit-relay-v2",
3
- "version": "2.1.3",
3
+ "version": "2.1.4-5d199f9b6",
4
4
  "description": "Implementation of Circuit Relay v2",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-circuit-relay-v2#readme",
@@ -52,29 +52,29 @@
52
52
  "doc-check": "aegir doc-check"
53
53
  },
54
54
  "dependencies": {
55
- "@libp2p/interface": "^2.1.2",
56
- "@libp2p/interface-internal": "^2.0.7",
57
- "@libp2p/peer-collections": "^6.0.7",
58
- "@libp2p/peer-id": "^5.0.4",
59
- "@libp2p/peer-record": "^8.0.7",
60
- "@libp2p/utils": "^6.1.0",
61
- "@multiformats/mafmt": "^12.1.6",
55
+ "@libp2p/crypto": "5.0.5-5d199f9b6",
56
+ "@libp2p/interface": "2.1.3-5d199f9b6",
57
+ "@libp2p/interface-internal": "2.0.8-5d199f9b6",
58
+ "@libp2p/peer-collections": "6.0.8-5d199f9b6",
59
+ "@libp2p/peer-id": "5.0.5-5d199f9b6",
60
+ "@libp2p/peer-record": "8.0.8-5d199f9b6",
61
+ "@libp2p/utils": "6.1.1-5d199f9b6",
62
62
  "@multiformats/multiaddr": "^12.2.3",
63
+ "@multiformats/multiaddr-matcher": "^1.3.0",
63
64
  "any-signal": "^4.1.1",
64
65
  "it-protobuf-stream": "^1.1.3",
65
66
  "it-stream-types": "^2.0.1",
66
67
  "multiformats": "^13.1.0",
67
- "p-defer": "^4.0.1",
68
68
  "progress-events": "^1.0.0",
69
69
  "protons-runtime": "^5.4.0",
70
70
  "race-signal": "^1.0.2",
71
+ "retimeable-signal": "^0.0.0",
71
72
  "uint8arraylist": "^2.4.8",
72
73
  "uint8arrays": "^5.1.0"
73
74
  },
74
75
  "devDependencies": {
75
- "@libp2p/crypto": "^5.0.4",
76
- "@libp2p/interface-compliance-tests": "^6.1.5",
77
- "@libp2p/logger": "^5.1.0",
76
+ "@libp2p/interface-compliance-tests": "6.1.6-5d199f9b6",
77
+ "@libp2p/logger": "5.1.1-5d199f9b6",
78
78
  "aegir": "^44.0.1",
79
79
  "delay": "^6.0.0",
80
80
  "it-drain": "^3.0.7",
package/src/constants.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { KEEP_ALIVE } from '@libp2p/interface'
2
+
1
3
  const second = 1000
2
4
  const minute = 60 * second
3
5
 
@@ -11,11 +13,6 @@ export const CIRCUIT_PROTO_CODE = 290
11
13
  */
12
14
  export const DEFAULT_MAX_RESERVATION_STORE_SIZE = 15
13
15
 
14
- /**
15
- * How often to check for reservation expiry
16
- */
17
- export const DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL = 300 * second
18
-
19
16
  /**
20
17
  * How often to check for reservation expiry
21
18
  */
@@ -27,9 +24,9 @@ export const DEFAULT_MAX_RESERVATION_TTL = 2 * 60 * minute
27
24
  export const DEFAULT_RESERVATION_CONCURRENCY = 1
28
25
 
29
26
  /**
30
- * How long to wait for a reservation attempt to finsih
27
+ * How long to wait for a reservation attempt to finish
31
28
  */
32
- export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 1000
29
+ export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 2000
33
30
 
34
31
  /**
35
32
  * How long to let the reservation attempt queue to grow
@@ -40,6 +37,9 @@ export const RELAY_SOURCE_TAG = 'circuit-relay-source'
40
37
 
41
38
  export const RELAY_TAG = 'circuit-relay-relay'
42
39
 
40
+ export const KEEP_ALIVE_TAG = `${KEEP_ALIVE}-circuit-relay`
41
+ export const KEEP_ALIVE_SOURCE_TAG = `${KEEP_ALIVE}-circuit-relay-source`
42
+
43
43
  // circuit v2 connection limits
44
44
  // https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/resources.go#L61-L66
45
45
 
package/src/index.ts CHANGED
@@ -43,13 +43,31 @@ import type { Limit } from './pb/index.js'
43
43
  import type { TypedEventEmitter } from '@libp2p/interface'
44
44
  import type { PeerMap } from '@libp2p/peer-collections'
45
45
  import type { Multiaddr } from '@multiformats/multiaddr'
46
+ import type { RetimeableAbortSignal } from 'retimeable-signal'
46
47
 
47
48
  export type { Limit }
48
49
 
49
50
  export interface RelayReservation {
50
- expire: Date
51
+ /**
52
+ * When this reservation expires
53
+ */
54
+ expiry: Date
55
+
56
+ /**
57
+ * The address of the relay client
58
+ */
51
59
  addr: Multiaddr
60
+
61
+ /**
62
+ * How much data can be transferred over each relayed connection and for how
63
+ * long before the underlying stream is reset
64
+ */
52
65
  limit?: Limit
66
+
67
+ /**
68
+ * This signal will fire it's "abort" event when the reservation expires
69
+ */
70
+ signal: RetimeableAbortSignal
53
71
  }
54
72
 
55
73
  export interface CircuitRelayServiceEvents {
@@ -64,10 +82,10 @@ export interface CircuitRelayService extends TypedEventEmitter<CircuitRelayServi
64
82
 
65
83
  export { circuitRelayServer } from './server/index.js'
66
84
  export type { CircuitRelayServerInit, CircuitRelayServerComponents } from './server/index.js'
67
- export type { ReservationStoreInit } from './server/reservation-store.js'
85
+ export type { ReservationStoreInit as ServerReservationStoreInit } from './server/reservation-store.js'
68
86
  export { circuitRelayTransport } from './transport/index.js'
69
87
  export type { RelayDiscoveryComponents } from './transport/discovery.js'
70
- export type { RelayStoreInit } from './transport/reservation-store.js'
88
+ export type { ReservationStoreInit as TransportReservationStoreInit } from './transport/reservation-store.js'
71
89
  export type { CircuitRelayTransportInit, CircuitRelayTransportComponents } from './transport/index.js'
72
90
 
73
91
  export {
@@ -40,7 +40,7 @@ message Peer {
40
40
  message Reservation {
41
41
  uint64 expire = 1; // Unix expiration time (UTC)
42
42
  repeated bytes addrs = 2; // relay addrs for reserving peer
43
- optional bytes voucher = 3; // reservation voucher
43
+ optional Envelope voucher = 3; // reservation voucher
44
44
  }
45
45
 
46
46
  message Limit {
@@ -65,3 +65,22 @@ message ReservationVoucher {
65
65
  bytes peer = 2;
66
66
  uint64 expiration = 3;
67
67
  }
68
+
69
+ // https://github.com/libp2p/specs/blob/master/RFC/0002-signed-envelopes.md
70
+ message Envelope {
71
+ // public_key is the public key of the keypair the enclosed payload was
72
+ // signed with.
73
+ bytes public_key = 1;
74
+
75
+ // payload_type encodes the type of payload, so that it can be deserialized
76
+ // deterministically.
77
+ bytes payload_type = 2;
78
+
79
+ // payload is the actual payload carried inside this envelope.
80
+ ReservationVoucher payload = 3;
81
+
82
+ // signature is the signature produced by the private key corresponding to
83
+ // the enclosed public key, over the payload, prefixing a domain string for
84
+ // additional security.
85
+ bytes signature = 5;
86
+ }
package/src/pb/index.ts CHANGED
@@ -318,7 +318,7 @@ export namespace Peer {
318
318
  export interface Reservation {
319
319
  expire: bigint
320
320
  addrs: Uint8Array[]
321
- voucher?: Uint8Array
321
+ voucher?: Envelope
322
322
  }
323
323
 
324
324
  export namespace Reservation {
@@ -345,7 +345,7 @@ export namespace Reservation {
345
345
 
346
346
  if (obj.voucher != null) {
347
347
  w.uint32(26)
348
- w.bytes(obj.voucher)
348
+ Envelope.codec().encode(obj.voucher, w)
349
349
  }
350
350
 
351
351
  if (opts.lengthDelimited !== false) {
@@ -376,7 +376,9 @@ export namespace Reservation {
376
376
  break
377
377
  }
378
378
  case 3: {
379
- obj.voucher = reader.bytes()
379
+ obj.voucher = Envelope.codec().decode(reader, reader.uint32(), {
380
+ limits: opts.limits?.voucher
381
+ })
380
382
  break
381
383
  }
382
384
  default: {
@@ -580,3 +582,97 @@ export namespace ReservationVoucher {
580
582
  return decodeMessage(buf, ReservationVoucher.codec(), opts)
581
583
  }
582
584
  }
585
+
586
+ export interface Envelope {
587
+ publicKey: Uint8Array
588
+ payloadType: Uint8Array
589
+ payload?: ReservationVoucher
590
+ signature: Uint8Array
591
+ }
592
+
593
+ export namespace Envelope {
594
+ let _codec: Codec<Envelope>
595
+
596
+ export const codec = (): Codec<Envelope> => {
597
+ if (_codec == null) {
598
+ _codec = message<Envelope>((obj, w, opts = {}) => {
599
+ if (opts.lengthDelimited !== false) {
600
+ w.fork()
601
+ }
602
+
603
+ if ((obj.publicKey != null && obj.publicKey.byteLength > 0)) {
604
+ w.uint32(10)
605
+ w.bytes(obj.publicKey)
606
+ }
607
+
608
+ if ((obj.payloadType != null && obj.payloadType.byteLength > 0)) {
609
+ w.uint32(18)
610
+ w.bytes(obj.payloadType)
611
+ }
612
+
613
+ if (obj.payload != null) {
614
+ w.uint32(26)
615
+ ReservationVoucher.codec().encode(obj.payload, w)
616
+ }
617
+
618
+ if ((obj.signature != null && obj.signature.byteLength > 0)) {
619
+ w.uint32(42)
620
+ w.bytes(obj.signature)
621
+ }
622
+
623
+ if (opts.lengthDelimited !== false) {
624
+ w.ldelim()
625
+ }
626
+ }, (reader, length, opts = {}) => {
627
+ const obj: any = {
628
+ publicKey: uint8ArrayAlloc(0),
629
+ payloadType: uint8ArrayAlloc(0),
630
+ signature: uint8ArrayAlloc(0)
631
+ }
632
+
633
+ const end = length == null ? reader.len : reader.pos + length
634
+
635
+ while (reader.pos < end) {
636
+ const tag = reader.uint32()
637
+
638
+ switch (tag >>> 3) {
639
+ case 1: {
640
+ obj.publicKey = reader.bytes()
641
+ break
642
+ }
643
+ case 2: {
644
+ obj.payloadType = reader.bytes()
645
+ break
646
+ }
647
+ case 3: {
648
+ obj.payload = ReservationVoucher.codec().decode(reader, reader.uint32(), {
649
+ limits: opts.limits?.payload
650
+ })
651
+ break
652
+ }
653
+ case 5: {
654
+ obj.signature = reader.bytes()
655
+ break
656
+ }
657
+ default: {
658
+ reader.skipType(tag & 7)
659
+ break
660
+ }
661
+ }
662
+ }
663
+
664
+ return obj
665
+ })
666
+ }
667
+
668
+ return _codec
669
+ }
670
+
671
+ export const encode = (obj: Partial<Envelope>): Uint8Array => {
672
+ return encodeMessage(obj, Envelope.codec())
673
+ }
674
+
675
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<Envelope>): Envelope => {
676
+ return decodeMessage(buf, Envelope.codec(), opts)
677
+ }
678
+ }