@peerbit/libp2p-test-utils 2.1.21 → 2.2.0-000e3f1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/session.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { noise } from "@chainsafe/libp2p-noise";
2
2
  import { yamux } from "@chainsafe/libp2p-yamux";
3
- import { type CircuitRelayService } from "@libp2p/circuit-relay-v2";
3
+ import type { CircuitRelayService } from "@libp2p/circuit-relay-v2";
4
4
  import { identify } from "@libp2p/identify";
5
5
  import type { Multiaddr } from "@multiformats/multiaddr";
6
6
  import { waitFor } from "@peerbit/time";
7
7
  import { setMaxListeners } from "events";
8
- import { type Libp2p, type Libp2pOptions, createLibp2p } from "libp2p";
8
+ import { createLibp2p } from "libp2p";
9
+ import type { Libp2p, Libp2pOptions } from "libp2p";
9
10
  import { listen, relay, transports } from "./transports.js";
10
11
 
11
12
  type DefaultServices = { relay: CircuitRelayService; identify: any };
@@ -20,10 +21,10 @@ export class TestSession<T> {
20
21
 
21
22
  async connect(
22
23
  groups?: {
23
- getMultiaddrs: () => Multiaddr[];
24
- dial: (addres: Multiaddr[]) => Promise<any>;
24
+ getMultiaddrs(): Multiaddr[];
25
+ dial(addres: Multiaddr[]): Promise<any>;
25
26
  }[][],
26
- ) {
27
+ ): Promise<TestSession<T>> {
27
28
  // Connect the nodes
28
29
  const connectPromises: Promise<any>[] = [];
29
30
  if (!groups) {
@@ -49,10 +50,10 @@ export class TestSession<T> {
49
50
 
50
51
  async connectLine(
51
52
  groups?: {
52
- getMultiaddrs: () => Multiaddr[];
53
- dial: (addres: Multiaddr[]) => Promise<any>;
53
+ getMultiaddrs(): Multiaddr[];
54
+ dial(addres: Multiaddr[]): Promise<any>;
54
55
  }[][],
55
- ) {
56
+ ): Promise<TestSession<T>> {
56
57
  const connectPromises: Promise<any>[] = [];
57
58
  if (!groups) {
58
59
  groups = [this.peers];
@@ -76,7 +77,7 @@ export class TestSession<T> {
76
77
  static async connected<T extends Record<string, unknown>>(
77
78
  n: number,
78
79
  options?: Libp2pOptions<T> | Libp2pOptions<T>[],
79
- ) {
80
+ ): Promise<TestSession<T>> {
80
81
  const libs = (await TestSession.disconnected<T>(n, options)).peers;
81
82
  return new TestSession<T>(libs).connect();
82
83
  }
@@ -84,16 +85,16 @@ export class TestSession<T> {
84
85
  static async disconnected<T extends Record<string, unknown>>(
85
86
  n: number,
86
87
  options?: Libp2pOptions<T> | Libp2pOptions<T>[],
87
- ) {
88
+ ): Promise<TestSession<T>> {
88
89
  // Allow more than 11 listneers
89
90
  setMaxListeners(Infinity);
90
91
 
91
92
  // create nodes
92
- const promises: Promise<Libp2p<T>>[] = [];
93
+ const promises: Promise<Libp2pWithServices<T>>[] = [];
93
94
  for (let i = 0; i < n; i++) {
94
- const result = async () => {
95
- const definedOptions: Libp2pOptions<T> | undefined =
96
- (options as any)?.[i] || options;
95
+ const result = async (): Promise<Libp2pWithServices<T>> => {
96
+ const definedOptions: Libp2pOptions<T & DefaultServices> | undefined =
97
+ (options as any)?.[i] || (options as any);
97
98
 
98
99
  const services: any = {
99
100
  identify: identify(),
@@ -105,9 +106,10 @@ export class TestSession<T> {
105
106
  delete services.relay;
106
107
  }
107
108
 
108
- const node = await createLibp2p<T>({
109
+ const node = await createLibp2p<T & DefaultServices>({
109
110
  addresses: {
110
- listen: listen(),
111
+ ...(definedOptions?.addresses ?? {}),
112
+ listen: definedOptions?.addresses?.listen ?? listen(),
111
113
  },
112
114
  connectionManager: definedOptions?.connectionManager,
113
115
  privateKey: definedOptions?.privateKey,
package/src/transports.ts CHANGED
@@ -3,11 +3,11 @@ import {
3
3
  circuitRelayTransport,
4
4
  } from "@libp2p/circuit-relay-v2";
5
5
  import type { Transport } from "@libp2p/interface";
6
+ import { tcp } from "@libp2p/tcp";
6
7
  import { webRTC } from "@libp2p/webrtc";
7
8
  import { webSockets } from "@libp2p/websockets";
8
9
 
9
10
  export const transports = (): Array<(components: any) => Transport> => [
10
- // todo: add types
11
11
  circuitRelayTransport({
12
12
  reservationCompletionTimeout: 5000,
13
13
  }),
@@ -15,12 +15,18 @@ export const transports = (): Array<(components: any) => Transport> => [
15
15
  webSockets(),
16
16
  ];
17
17
 
18
+ export const transportsFast = (): Array<(components: any) => Transport> => [
19
+ tcp(),
20
+ ];
21
+
18
22
  // applyDefaultLimit: false because of https://github.com/libp2p/js-libp2p/issues/2622
19
- export const relay = () =>
23
+ export const relay = (): ReturnType<typeof circuitRelayServer> =>
20
24
  circuitRelayServer({ reservations: { applyDefaultLimit: false } });
21
25
 
22
- export const listen = () => [
26
+ export const listen = (): string[] => [
23
27
  "/ip4/127.0.0.1/tcp/0",
24
28
  "/ip4/127.0.0.1/tcp/0/ws",
25
29
  "/p2p-circuit",
26
30
  ];
31
+
32
+ export const listenFast = (): string[] => ["/ip4/127.0.0.1/tcp/0"];