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
@@ -1 +0,0 @@
1
- {"version":3,"file":"hop.js","sourceRoot":"","sources":["../../../../src/circuit/circuit/hop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,YAAY,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,KAAK,IAAI,MAAM,E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CAAE,OAAsB;IAClD,MAAM,EACJ,UAAU,EACV,MAAM,EACP,GAAG,OAAO,CAAA;IAEX,mCAAmC;IACnC,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,WAAW,EAAE;QACrD,MAAM;KACP,CAAC,CAAA;IAEF,uBAAuB;IACvB,MAAM,aAAa,GAAG,IAAI,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC,CAAA;IACnD,aAAa,CAAC,KAAK,CAAC;QAClB,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,OAAO;KAC7B,CAAC,CAAA;IAEF,MAAM,QAAQ,GAAG,MAAM,aAAa,CAAC,IAAI,EAAE,CAAA;IAC3C,MAAM,aAAa,CAAC,KAAK,EAAE,CAAA;IAE3B,IAAI,QAAQ,IAAI,IAAI,IAAI,QAAQ,CAAC,IAAI,KAAK,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE;QAClE,OAAO,KAAK,CAAA;KACb;IAED,OAAO,IAAI,CAAA;AACb,CAAC;AAQD;;GAEG;AACH,MAAM,UAAU,YAAY,CAAE,OAA4B;IACxD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,OAAO,EACR,GAAG,OAAO,CAAA;IACX,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAA;IACnC,GAAG,CAAC,8BAA8B,EAAE,MAAM,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;IAClE,aAAa,CAAC,GAAG,CAAC;QAChB,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,MAAM;QAC3B,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,oBAAoB;KAChF,CAAC,CAAA;AACJ,CAAC"}
@@ -1,24 +0,0 @@
1
- import { CircuitRelay as CircuitPB } from '../pb/index.js';
2
- import { StreamHandler } from './stream-handler.js';
3
- import type { Connection } from '@libp2p/interface-connection';
4
- import type { Duplex } from 'it-stream-types';
5
- import type { AbortOptions } from '@libp2p/interfaces';
6
- import type { Uint8ArrayList } from 'uint8arraylist';
7
- export interface HandleStopOptions {
8
- connection: Connection;
9
- request: CircuitPB;
10
- streamHandler: StreamHandler;
11
- }
12
- /**
13
- * Handles incoming STOP requests
14
- */
15
- export declare function handleStop(options: HandleStopOptions): Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> | undefined;
16
- export interface StopOptions extends AbortOptions {
17
- connection: Connection;
18
- request: CircuitPB;
19
- }
20
- /**
21
- * Creates a STOP request
22
- */
23
- export declare function stop(options: StopOptions): Promise<Duplex<Uint8ArrayList, Uint8Array | Uint8ArrayList, Promise<void>> | undefined>;
24
- //# sourceMappingURL=stop.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stop.d.ts","sourceRoot":"","sources":["../../../../src/circuit/circuit/stop.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAEnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAIpD,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;IAClB,aAAa,EAAE,aAAa,CAAA;CAC7B;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAE,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAAC,cAAc,EAAE,cAAc,GAAG,UAAU,CAAC,GAAG,SAAS,CAuBvH;AAED,MAAM,WAAW,WAAY,SAAQ,YAAY;IAC/C,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB;AAED;;GAEG;AACH,wBAAsB,IAAI,CAAE,OAAO,EAAE,WAAW,2FA4B/C"}
@@ -1,51 +0,0 @@
1
- import { logger } from '@libp2p/logger';
2
- import { CircuitRelay as CircuitPB } from '../pb/index.js';
3
- import { RELAY_CODEC } from '../multicodec.js';
4
- import { StreamHandler } from './stream-handler.js';
5
- import { validateAddrs } from './utils.js';
6
- const log = logger('libp2p:circuit:stop');
7
- /**
8
- * Handles incoming STOP requests
9
- */
10
- export function handleStop(options) {
11
- const { connection, request, streamHandler } = options;
12
- // Validate the STOP request has the required input
13
- try {
14
- validateAddrs(request, streamHandler);
15
- }
16
- catch (err) {
17
- log.error('invalid stop request via peer %p %o', connection.remotePeer, err);
18
- return;
19
- }
20
- // The request is valid
21
- log('stop request is valid');
22
- streamHandler.write({
23
- type: CircuitPB.Type.STATUS,
24
- code: CircuitPB.Status.SUCCESS
25
- });
26
- return streamHandler.rest();
27
- }
28
- /**
29
- * Creates a STOP request
30
- */
31
- export async function stop(options) {
32
- const { connection, request, signal } = options;
33
- const stream = await connection.newStream(RELAY_CODEC, {
34
- signal
35
- });
36
- log('starting stop request to %p', connection.remotePeer);
37
- const streamHandler = new StreamHandler({ stream });
38
- streamHandler.write(request);
39
- const response = await streamHandler.read();
40
- if (response == null) {
41
- streamHandler.close();
42
- return;
43
- }
44
- if (response.code === CircuitPB.Status.SUCCESS) {
45
- log('stop request to %p was successful', connection.remotePeer);
46
- return streamHandler.rest();
47
- }
48
- log('stop request failed with code %d', response.code);
49
- streamHandler.close();
50
- }
51
- //# sourceMappingURL=stop.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stop.js","sourceRoot":"","sources":["../../../../src/circuit/circuit/stop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,YAAY,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,EA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AAI,EAAE,CAAA;IAE3C,IAAI,QAAQ,IAAI,IAAI,EAAE;QACpB,aAAa,CAAC,KAAK,EAAE,CAAA;QACrB,OAAM;KACP;IAED,IAAI,QAAQ,CAAC,IAAI,KAAK,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE;QAC9C,GAAG,CAAC,mCAAmC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;QAC/D,OAAO,aAAa,CAAC,IAAI,EAAE,CAAA;KAC5B;IAED,GAAG,CAAC,kCAAkC,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAA;IACtD,aAAa,CAAC,KAAK,EAAE,CAAA;AACvB,CAAC"}
@@ -1,40 +0,0 @@
1
- import { CircuitRelay } from '../pb/index.js';
2
- import type { Stream } from '@libp2p/interface-connection';
3
- import type { Uint8ArrayList } from 'uint8arraylist';
4
- export interface StreamHandlerOptions {
5
- /**
6
- * A duplex iterable
7
- */
8
- stream: Stream;
9
- /**
10
- * max bytes length of message
11
- */
12
- maxLength?: number;
13
- }
14
- export declare class StreamHandler {
15
- private readonly stream;
16
- private readonly shake;
17
- private readonly decoder;
18
- constructor(options: StreamHandlerOptions);
19
- /**
20
- * Read and decode message
21
- */
22
- read(): Promise<CircuitRelay | undefined>;
23
- /**
24
- * Encode and write array of buffers
25
- */
26
- write(msg: CircuitRelay): void;
27
- /**
28
- * Return the handshake rest stream and invalidate handler
29
- */
30
- rest(): import("it-stream-types").Duplex<Uint8ArrayList, Uint8Array | Uint8ArrayList, Promise<void>>;
31
- /**
32
- * @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
33
- */
34
- end(msg: CircuitRelay): void;
35
- /**
36
- * Close the stream
37
- */
38
- close(): void;
39
- }
40
- //# sourceMappingURL=stream-handler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stream-handler.d.ts","sourceRoot":"","sources":["../../../../src/circuit/circuit/stream-handler.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAA;AAE1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAIpD,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAwC;IAC9D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwB;gBAEnC,OAAO,EAAE,oBAAoB;IAQ1C;;OAEG;IACG,IAAI;IAeV;;OAEG;IACH,KAAK,CAAE,GAAG,EAAE,YAAY;IAKxB;;OAEG;IACH,IAAI;IAKJ;;OAEG;IACH,GAAG,CAAE,GAAG,EAAE,YAAY;IAKtB;;OAEG;IACH,KAAK;CAMN"}
@@ -1,59 +0,0 @@
1
- import { logger } from '@libp2p/logger';
2
- import * as lp from 'it-length-prefixed';
3
- import { handshake } from 'it-handshake';
4
- import { CircuitRelay } from '../pb/index.js';
5
- const log = logger('libp2p:circuit:stream-handler');
6
- export class StreamHandler {
7
- constructor(options) {
8
- const { stream, maxLength = 4096 } = options;
9
- this.stream = stream;
10
- this.shake = handshake(this.stream);
11
- this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength });
12
- }
13
- /**
14
- * Read and decode message
15
- */
16
- async read() {
17
- // @ts-expect-error FIXME is a source, needs to be a generator
18
- const msg = await this.decoder.next();
19
- if (msg.value != null) {
20
- const value = CircuitRelay.decode(msg.value);
21
- log('read message type', value.type);
22
- return value;
23
- }
24
- log('read received no value, closing stream');
25
- // End the stream, we didn't get data
26
- this.close();
27
- }
28
- /**
29
- * Encode and write array of buffers
30
- */
31
- write(msg) {
32
- log('write message type %s', msg.type);
33
- this.shake.write(lp.encode.single(CircuitRelay.encode(msg)));
34
- }
35
- /**
36
- * Return the handshake rest stream and invalidate handler
37
- */
38
- rest() {
39
- this.shake.rest();
40
- return this.shake.stream;
41
- }
42
- /**
43
- * @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
44
- */
45
- end(msg) {
46
- this.write(msg);
47
- this.close();
48
- }
49
- /**
50
- * Close the stream
51
- */
52
- close() {
53
- log('closing the stream');
54
- void this.rest().sink([]).catch(err => {
55
- log.error(err);
56
- });
57
- }
58
- }
59
- //# sourceMappingURL=stream-handler.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stream-handler.js","sourceRoot":"","sources":["../../../../src/circuit/circuit/stream-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAa,SAAS,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAK7C,MAAM,GAAG,GAAG,MAAM,CAAC,+BAA+B,CAAC,CAAA;AAcnD,MAAM,OAAO,aAAa;IAKxB,YAAa,OAA6B;QACxC,MAAM,EAAE,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,OAAO,CAAA;QAE5C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACnC,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,aAAa,EAAE,SAAS,EAAE,CAAC,CAAA;IACtF,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,8DAA8D;QAC9D,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;QAErC,IAAI,GAAG,CAAC,KAAK,IAAI,IAAI,EAAE;YACrB,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;YAC5C,GAAG,CAAC,mBAAmB,EAAE,KAAK,CAAC,IAAI,CAAC,CAAA;YACpC,OAAO,KAAK,CAAA;SACb;QAED,GAAG,CAAC,wCAAwC,CAAC,CAAA;QAC7C,qCAAqC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAA;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAE,GAAiB;QACtB,GAAG,CAAC,uBAAuB,EAAE,GAAG,CAAC,IAAI,CAAC,CAAA;QACtC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;IAC9D,CAAC;IAED;;OAEG;IACH,IAAI;QACF,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAA;IAC1B,CAAC;IAED;;OAEG;IACH,GAAG,CAAE,GAAiB;QACpB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACf,IAAI,CAAC,KAAK,EAAE,CAAA;IACd,CAAC;IAED;;OAEG;IACH,KAAK;QACH,GAAG,CAAC,oBAAoB,CAAC,CAAA;QACzB,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YACpC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAChB,CAAC,CAAC,CAAA;IACJ,CAAC;CACF"}
@@ -1,7 +0,0 @@
1
- import { CircuitRelay } from '../pb/index.js';
2
- import type { StreamHandler } from './stream-handler.js';
3
- /**
4
- * Validate incomming HOP/STOP message
5
- */
6
- export declare function validateAddrs(msg: CircuitRelay, streamHandler: StreamHandler): void;
7
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/circuit/circuit/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAYxD;;GAEG;AACH,wBAAgB,aAAa,CAAE,GAAG,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,QA0B7E"}
@@ -1,43 +0,0 @@
1
- import { multiaddr } from '@multiformats/multiaddr';
2
- import { CircuitRelay } from '../pb/index.js';
3
- /**
4
- * Write a response
5
- */
6
- function writeResponse(streamHandler, status) {
7
- streamHandler.write({
8
- type: CircuitRelay.Type.STATUS,
9
- code: status
10
- });
11
- }
12
- /**
13
- * Validate incomming HOP/STOP message
14
- */
15
- export function validateAddrs(msg, streamHandler) {
16
- try {
17
- if (msg.dstPeer?.addrs != null) {
18
- msg.dstPeer.addrs.forEach((addr) => {
19
- return multiaddr(addr);
20
- });
21
- }
22
- }
23
- catch (err) {
24
- writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
25
- ? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
26
- : CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID);
27
- throw err;
28
- }
29
- try {
30
- if (msg.srcPeer?.addrs != null) {
31
- msg.srcPeer.addrs.forEach((addr) => {
32
- return multiaddr(addr);
33
- });
34
- }
35
- }
36
- catch (err) {
37
- writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
38
- ? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
39
- : CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID);
40
- throw err;
41
- }
42
- }
43
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../src/circuit/circuit/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAA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,CAAA;QACnD,MAAM,GAAG,CAAA;KACV;AACH,CAAC"}
@@ -1,128 +0,0 @@
1
- EDIT: This document is outdated and here only for historical purposes
2
-
3
- NOTE: This document is structured in an `if-then/else[if]-then` manner, each line is a precondition for following lines with a higher number of indentation
4
-
5
- Example:
6
-
7
- - if there are apples
8
- - eat them
9
- - if not, check for pears
10
- - then eat them
11
- - if not, check for cherries
12
- - then eat them
13
-
14
- Or,
15
-
16
- - if there are apples
17
- - eat them
18
- - if not
19
- - check for pears
20
- - then eat them
21
- - if not
22
- - check for cherries
23
- - then eat them
24
-
25
- In order to minimize nesting, the first example is preferred
26
-
27
- # Relay flow
28
-
29
- ## Relay transport (dialer/listener)
30
-
31
- - ### Dial over a relay
32
- - See if there is a relay that's already connected to the destination peer, if not
33
- - Ask all the peer's known relays to dial the destination peer until an active relay (one that can dial on behalf of other peers), or a relay that may have recently acquired a connection to the destination peer is successful.
34
- - If successful
35
- - Write the `/ipfs/relay/circuit/1.0.0` header to the relay, followed by the destination address
36
- - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmDest`.
37
- - If no relays could connect, fail the same way a regular transport would
38
- - Once the connection has been established, the swarm should treat it as a regular connection,
39
- - i.e. muxing, encrypt, etc should all be performed on the relayed connection
40
-
41
- - ### Listen for relayed connections
42
- - Peer mounts the `/ipfs/relay/circuit/1.0.0` proto and listens for relayed connections
43
- - A connection arrives
44
- - read the address of the source peer from the incoming connection stream
45
- - if valid, create a PeerInfo object for that peer and add the incoming address to its multiaddresses list
46
- - pass the connection to `protocolMuxer(swarm.protocols, conn)` to have it go through the regular muxing/encryption flow
47
-
48
- - ### Relay discovery and static relay addresses in swarm config
49
-
50
- - #### Relay address in swarm config
51
- - A peer has relay addresses in its swarm config section
52
- - On node startup, connect to the relays in swarm config
53
- - if successful add address to swarms PeerInfo's multiaddresses
54
- - `identify` should take care of announcing that the peer is reachable over the listed relays
55
-
56
- - #### Passive relay discovery
57
- - A peer that can dial over `/ipfs/relay/circuit/1.0.0` listens for the `peer-mux-established` swarm event, every time a new muxed connection arrives, it checks if the incoming peer is a relay. (How would this work? Some way of discovering if its a relay is required.)
58
- - *Useful in cases when the peer/node doesn't know of any relays on startup and also, to learn of as many additional relays in the network as possible*
59
- - *Useful during startup, when connecting to bootstrap nodes. It allows us to implicitly learn if its a relay without having to explicitly add `/p2p-circuit` addresses to the bootstrap list*
60
- - *Also useful if the relay communicates its capabilities upon connecting to it, as to avoid additional unnecessary requests/queries. I.e. if it supports weather its able to forward connections and weather it supports the `ls` or other commands.*
61
- - *Should it be possible to disable passive relay discovery?*
62
- - This could be useful when the peer wants to be reachable **only** over the listed relays
63
- - If the incoming peer is a relay, send an `ls` and record its peers
64
-
65
- ## Relay Nodes
66
-
67
- - ### Passive relay node
68
- - *A passive relay does not explicitly dial into any requested peer, only those that it's swarm already has connections to.*
69
- - When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
70
- - check its swarm's peerbook(?) see if its a known peer, if it is
71
- - use the swarms existing connection and
72
- - send the multistream header and the source peer address to the dest peer
73
- - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
74
- - circuit the source and dest connections
75
- - if couldn't dial, or the connection/stream to the dest peer closed prematurelly
76
- - close the src stream
77
-
78
-
79
- - ### Active relay node
80
- - *An active relay node can dial other peers even if its swarm doesnt know about those peers*
81
- - When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
82
- - use the swarm to dial to the dest node
83
- - send the multistream header and the source peer address to the dest peer
84
- - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
85
- - circuit the source and dest connections
86
- - if couldn't dial, or the connection/stream to the dest peer closed prematurely
87
- - close the src stream
88
-
89
- - ### `ls` command
90
- - *A relay node can allow the peers known to it's swarm to be listed*
91
- - *this should be possible to enable/disable from the config*
92
- - when a relay gets the `ls` request
93
- - if enabled, get its swarm's peerbook's known peers and return their ids and multiaddrs
94
- - e.g `[{id: /ipfs/QmPeerId, addrs: ['ma1', 'ma2', 'ma3']}, ...]`
95
- - if disabled, respond with `na`
96
-
97
-
98
- ## Relay Implementation notes
99
-
100
- - ### Relay transport
101
- - Currently I've implemented the dialer and listener parts of the relay as a transport, meaning that it *tries* to implement the `interface-transport` interface as closely as possible. This seems to work pretty well and it's makes the dialer/listener parts really easy to plug in into the swarm. I think this is the cleanest solution.
102
-
103
- - ### `circuit-relay`
104
- - This is implemented as a separate piece (not a transport), and it can be enabled/disabled with a config. The transport listener however, will do the initial parsing of the incoming header and figure out weather it's a connection that's needs to be handled by the circuit-relay, or its a connection that is being relayed from a circuit-relay.
105
-
106
- ## Relay swarm integration
107
-
108
- - The relay transport is mounted explicitly by calling the `swarm.connection.relay(config.relay)` from libp2p
109
- - Swarm will register the dialer and listener using the swarm `transport.add` and `transport.listen` methods
110
-
111
- - ### Listener
112
- - the listener registers itself as a multistream handler on the `/ipfs/relay/circuit/1.0.0` proto
113
- - if `circuit-relay` is enabled, the listener will delegate connections to it if appropriate
114
- - when the listener receives a connection, it will read the multiaddr and determine if its a connection that needs to be relayed, or its a connection that is being relayed
115
-
116
- - ### Dialer
117
- - When the swarm attempts to dial to a peer, it will filter the protocols that the peer can be reached on
118
- - *The relay will be used in two cases*
119
- - If the peer has an explicit relay address that it can be reached on
120
- - no other transport is available
121
- - The relay will attempt to dial the peer over that relay
122
- - If no explicit relay address is provided
123
- - no other transport is available
124
- - A generic circuit address will be added to the peers multiaddr list
125
- - i.e. `/p2p-circuit/ipfs/QmDest`
126
- - If another transport is available, then use that instead of the relay
127
-
128
-
@@ -1,111 +0,0 @@
1
- # js-libp2p-circuit
2
-
3
- > Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/connection) interface for dial/listen.
4
-
5
- **Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-circuit.
6
-
7
- `libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node. You can read more about this in its [spec](https://github.com/libp2p/specs/tree/master/relay).
8
-
9
- ## Table of Contents
10
-
11
- - [js-libp2p-circuit](#js-libp2p-circuit)
12
- - [Table of Contents](#table-of-contents)
13
- - [Why?](#why)
14
- - [libp2p-circuit and IPFS](#libp2p-circuit-and-ipfs)
15
- - [Usage](#usage)
16
- - [API](#api)
17
- - [Implementation rational](#implementation-rational)
18
-
19
- ### Why?
20
-
21
- `circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
22
-
23
- The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
24
- - routing traffic between private nets and circumventing NAT layers
25
- - route mangling for better privacy (matreshka/shallot dialing).
26
-
27
- It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes.
28
-
29
- ### libp2p-circuit and IPFS
30
-
31
- Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
32
-
33
- ## Usage
34
-
35
- Libp2p circuit configuration can be seen at [Setup with Relay](../../doc/CONFIGURATION.md#setup-with-relay).
36
-
37
- Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows:
38
-
39
- ```js
40
- import { multiaddr } from '@multiformats/multiaddr'
41
- import Libp2p from 'libp2p'
42
- import { tcp } from '@libp2p/tcp'
43
- import { mplex } from '@libp2p/mplex'
44
- import { noise } from '@chainsafe/libp2p-noise'
45
-
46
- const relayAddr = ...
47
-
48
- const node = await createLibp2p({
49
- addresses: {
50
- listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
51
- },
52
- transports: [
53
- tcp()
54
- ],
55
- streamMuxers: [
56
- mplex()
57
- ],
58
- connectionEncryption: [
59
- noise()
60
- ]
61
- },
62
- config: {
63
- relay: { // Circuit Relay options (this config is part of libp2p core configurations)
64
- enabled: true // Allows you to dial and accept relayed connections. Does not make you a relay.
65
- }
66
- }
67
- })
68
- ```
69
-
70
- ## API
71
-
72
- [![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
73
-
74
- `libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e:
75
-
76
- `/p2p-circuit/ip4/127.0.0.1/tcp/4001/p2p/QmHash`
77
-
78
- Both for dialing and listening.
79
-
80
- ### Implementation rational
81
-
82
- This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening.
83
-
84
- ```
85
- libp2p libp2p-circuit (transport)
86
- +-------------------------------------------------+ +--------------------------+
87
- | +---------------------------------+ | | |
88
- | | | | | +------------------+ |
89
- | | | | circuit-relay listens for the HOP | | | |
90
- | | libp2p <------------------------------------------------| circuit-relay | |
91
- | | | | message to handle incomming relay | | | |
92
- | | | | requests from other nodes | +------------------+ |
93
- | +---------------------------------+ | | |
94
- | ^ ^ ^ ^ ^ ^ | | +------------------+ |
95
- | | | | | | | | | | +-------------+ | |
96
- | | | | | | | | dialer uses libp2p to dial | | | | | |
97
- | | | | +----------------------------------------------------------------------> dialer | | |
98
- | | | transports | | to a circuit-relay node using the | | | | | |
99
- | | | | | | | HOP message | | +-------------+ | |
100
- | | | | | | | | | | |
101
- | v v | v v | | | | |
102
- |+------------------|----------------------------+| | | +-------------+ | |
103
- || | | | | || | | | | | |
104
- ||libp2p-tcp |libp2p-ws | .... |libp2p-circuit || listener handles STOP messages from| | | listener | | |
105
- || | +--------------------------------------------------------------------------> | | |
106
- || | | |plugs in just || circuit-relay nodes | | +-------------+ | |
107
- || | | |as any other || | | | |
108
- || | | |transport || | +------------------+ |
109
- |+-----------------------------------------------+| | |
110
- +-------------------------------------------------+ +--------------------------+
111
- ```