@constl/utils-tests 1.7.0 → 2.0.0

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 (81) hide show
  1. package/.aegir.js +2 -2
  2. package/dist/attente.d.ts +8 -30
  3. package/dist/attente.js +101 -141
  4. package/dist/attente.js.map +1 -1
  5. package/dist/config.d.ts +4 -1
  6. package/dist/config.js +4 -4
  7. package/dist/config.js.map +1 -1
  8. package/dist/constellation.d.ts +14 -10
  9. package/dist/constellation.js +24 -43
  10. package/dist/constellation.js.map +1 -1
  11. package/dist/dossiers.d.ts +1 -1
  12. package/dist/dossiers.js +19 -5
  13. package/dist/dossiers.js.map +1 -1
  14. package/dist/h/303/251lia.d.ts +11 -0
  15. package/dist/h/303/251lia.js +38 -0
  16. package/dist/h/303/251lia.js.map +1 -0
  17. package/dist/index.d.ts +8 -8
  18. package/dist/index.js +8 -8
  19. package/dist/index.js.map +1 -1
  20. package/dist/libp2p/config.d.ts +25 -0
  21. package/dist/{libp2p.js → libp2p/config.js} +47 -56
  22. package/dist/libp2p/config.js.map +1 -0
  23. package/dist/libp2p/index.d.ts +3 -0
  24. package/dist/libp2p/index.js +4 -0
  25. package/dist/libp2p/index.js.map +1 -0
  26. package/dist/libp2p/libp2p.d.ts +9 -0
  27. package/dist/libp2p/libp2p.js +21 -0
  28. package/dist/libp2p/libp2p.js.map +1 -0
  29. package/dist/libp2p/utils.d.ts +10 -0
  30. package/dist/libp2p/utils.js +41 -0
  31. package/dist/libp2p/utils.js.map +1 -0
  32. package/dist/orbite.d.ts +7 -23
  33. package/dist/orbite.js +31 -154
  34. package/dist/orbite.js.map +1 -1
  35. package/dist/{scripts/relai.js → relai/bin.js} +12 -16
  36. package/dist/relai/bin.js.map +1 -0
  37. package/dist/relai/consts.d.ts +3 -0
  38. package/dist/relai/consts.js +4 -0
  39. package/dist/relai/consts.js.map +1 -0
  40. package/dist/relai/index.d.ts +3 -0
  41. package/dist/relai/index.js +4 -0
  42. package/dist/relai/index.js.map +1 -0
  43. package/dist/relai/relai.d.ts +5 -0
  44. package/dist/relai/relai.js +29 -0
  45. package/dist/relai/relai.js.map +1 -0
  46. package/dist/relai/utils.d.ts +4 -0
  47. package/dist/relai/utils.js +6 -0
  48. package/dist/relai/utils.js.map +1 -0
  49. package/dist/tsconfig.build.tsbuildinfo +1 -1
  50. package/dist/utils.d.ts +4 -0
  51. package/dist/utils.js +5 -0
  52. package/dist/utils.js.map +1 -0
  53. package/dist/version.d.ts +1 -1
  54. package/dist/version.js +1 -1
  55. package/package.json +1 -1
  56. package/src/attente.ts +129 -156
  57. package/src/config.ts +10 -4
  58. package/src/constellation.ts +39 -60
  59. package/src/dossiers.ts +18 -6
  60. package/src/h/303/251lia.ts +61 -0
  61. package/src/index.ts +24 -8
  62. package/src/{libp2p.ts → libp2p/config.ts} +66 -71
  63. package/src/libp2p/index.ts +9 -0
  64. package/src/libp2p/libp2p.ts +41 -0
  65. package/src/libp2p/utils.ts +59 -0
  66. package/src/orbite.ts +43 -228
  67. package/src/{scripts/relai.ts → relai/bin.ts} +11 -16
  68. package/src/relai/consts.ts +5 -0
  69. package/src/relai/index.ts +3 -0
  70. package/src/relai/relai.ts +40 -0
  71. package/src/relai/utils.ts +14 -0
  72. package/src/utils.ts +5 -0
  73. package/src/version.ts +1 -1
  74. package/dist/libp2p.d.ts +0 -26
  75. package/dist/libp2p.js.map +0 -1
  76. package/dist/scripts/relai.js.map +0 -1
  77. package/dist/sfip.d.ts +0 -13
  78. package/dist/sfip.js +0 -89
  79. package/dist/sfip.js.map +0 -1
  80. package/src/sfip.ts +0 -127
  81. /package/dist/{scripts/relai.d.ts → relai/bin.d.ts} +0 -0
@@ -24,89 +24,32 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
24
  SOFTWARE.
25
25
  */
26
26
 
27
- import { Identify, identify } from "@libp2p/identify";
28
- import { webSockets } from "@libp2p/websockets";
29
- import { webRTC } from "@libp2p/webrtc";
30
- import { all } from "@libp2p/websockets/filters";
31
- import { noise } from "@chainsafe/libp2p-noise";
32
- import { yamux } from "@chainsafe/libp2p-yamux";
33
27
  import {
34
28
  gossipsub,
35
- GossipSub,
36
29
  GossipSubComponents,
30
+ GossipSub,
37
31
  } from "@chainsafe/libp2p-gossipsub";
32
+ import { noise } from "@chainsafe/libp2p-noise";
33
+ import { yamux } from "@chainsafe/libp2p-yamux";
38
34
  import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
39
- import type { PrivateKey } from "@libp2p/interface";
40
- import type { Libp2pOptions } from "libp2p";
35
+ import { Identify, identify } from "@libp2p/identify";
36
+ import { webRTC } from "@libp2p/webrtc";
37
+ import { webSockets } from "@libp2p/websockets";
41
38
 
42
- /**
43
- * A basic Libp2p configuration for Node.js nodes.
44
- */
45
- export const DefaultLibp2pOptions: Libp2pOptions<ServicesLibp2pConstlTest> = {
46
- addresses: {
47
- listen: ["/ip4/0.0.0.0/tcp/0/ws", "/p2p-circuit"],
48
- },
49
- transports: [
50
- webSockets({
51
- filter: all,
52
- }),
53
- webRTC(),
54
- circuitRelayTransport(),
55
- ],
56
- connectionEncrypters: [noise()],
57
- streamMuxers: [yamux()],
58
- connectionGater: {
59
- denyDialMultiaddr: () => false,
60
- },
61
- services: {
62
- identify: identify(),
63
- pubsub: gossipsub({ allowPublishToZeroTopicPeers: true }) as (
64
- components: GossipSubComponents,
65
- ) => GossipSub, // Erreur de type dans @chainsafe/pubsub
66
- obtClefPrivée: (components: ComposantesServiceClefPrivée) =>
67
- new ServiceClefPrivée(components),
68
- },
69
- };
39
+ import { Libp2pOptions } from "libp2p";
70
40
 
71
- /**
72
- * A basic Libp2p configuration for browser nodes.
73
- */
74
- export const DefaultLibp2pBrowserOptions: Libp2pOptions<ServicesLibp2pConstlTest> =
75
- {
76
- addresses: {
77
- listen: ["/webrtc", "/p2p-circuit"],
78
- },
79
- transports: [
80
- webSockets({
81
- filter: all,
82
- }),
83
- webRTC(),
84
- circuitRelayTransport(),
85
- ],
86
- connectionEncrypters: [noise()],
87
- streamMuxers: [yamux()],
88
- connectionGater: {
89
- denyDialMultiaddr: () => false,
90
- },
91
- services: {
92
- identify: identify(),
93
- pubsub: gossipsub({ allowPublishToZeroTopicPeers: true }) as (
94
- components: GossipSubComponents,
95
- ) => GossipSub, // Erreur de type dans @chainsafe/pubsub,
96
- obtClefPrivée: (components: ComposantesServiceClefPrivée) =>
97
- new ServiceClefPrivée(components),
98
- },
99
- };
100
-
101
- interface ComposantesServiceClefPrivée {
102
- privateKey: PrivateKey;
103
- }
41
+ import { PrivateKey } from "@libp2p/interface";
104
42
 
105
- export type ServicesLibp2pConstlTest = {
43
+ export type ServicesLibp2pTest = {
106
44
  identify: Identify;
107
45
  pubsub: GossipSub;
108
46
  obtClefPrivée: ServiceClefPrivée;
109
47
  };
48
+
49
+ export interface ComposantesServiceClefPrivée {
50
+ privateKey: PrivateKey;
51
+ }
52
+
110
53
  export class ServiceClefPrivée {
111
54
  privateKey: PrivateKey;
112
55
 
@@ -118,3 +61,55 @@ export class ServiceClefPrivée {
118
61
  return this.privateKey;
119
62
  }
120
63
  }
64
+
65
+ /**
66
+ * Configuration Libp2p pour Node.js.
67
+ */
68
+ export const OptionsDéfautLibp2pNode: () => Libp2pOptions<ServicesLibp2pTest> =
69
+ () => {
70
+ return {
71
+ addresses: {
72
+ listen: ["/ip4/0.0.0.0/tcp/0/ws", "/p2p-circuit"],
73
+ },
74
+ transports: [webSockets(), webRTC(), circuitRelayTransport()],
75
+ connectionEncrypters: [noise()],
76
+ streamMuxers: [yamux()],
77
+ connectionGater: {
78
+ denyDialMultiaddr: () => false,
79
+ },
80
+ services: {
81
+ identify: identify(),
82
+ pubsub: gossipsub({ allowPublishToZeroTopicPeers: true }) as (
83
+ components: GossipSubComponents,
84
+ ) => GossipSub, // Erreur de type dans @chainsafe/pubsub
85
+ obtClefPrivée: (components: ComposantesServiceClefPrivée) =>
86
+ new ServiceClefPrivée(components),
87
+ },
88
+ };
89
+ };
90
+
91
+ /**
92
+ * Configuration Libp2p pour les navigateurs.
93
+ */
94
+ export const OptionsDéfautLibp2pNavigateur: () => Libp2pOptions<ServicesLibp2pTest> =
95
+ () => {
96
+ return {
97
+ addresses: {
98
+ listen: ["/webrtc", "/p2p-circuit"],
99
+ },
100
+ transports: [webSockets(), webRTC(), circuitRelayTransport()],
101
+ connectionEncrypters: [noise()],
102
+ streamMuxers: [yamux()],
103
+ connectionGater: {
104
+ denyDialMultiaddr: () => false,
105
+ },
106
+ services: {
107
+ identify: identify(),
108
+ pubsub: gossipsub({ allowPublishToZeroTopicPeers: true }) as (
109
+ components: GossipSubComponents,
110
+ ) => GossipSub, // Erreur de type dans @chainsafe/pubsub,
111
+ obtClefPrivée: (components: ComposantesServiceClefPrivée) =>
112
+ new ServiceClefPrivée(components),
113
+ },
114
+ };
115
+ };
@@ -0,0 +1,9 @@
1
+ export {
2
+ ServicesLibp2pTest,
3
+ ComposantesServiceClefPrivée,
4
+ ServiceClefPrivée,
5
+ OptionsDéfautLibp2pNode,
6
+ OptionsDéfautLibp2pNavigateur,
7
+ } from "./config.js";
8
+ export { créerLibp2psTest } from "./libp2p.js";
9
+ export { connecterPairs, toutesConnectées } from "./utils.js";
@@ -0,0 +1,41 @@
1
+ import { Libp2p } from "@libp2p/interface";
2
+ import { createLibp2p } from "libp2p";
3
+ import {
4
+ OptionsDéfautLibp2pNavigateur,
5
+ OptionsDéfautLibp2pNode,
6
+ ServicesLibp2pTest,
7
+ } from "./config.js";
8
+ import { isBrowser } from "wherearewe";
9
+ import { connecterPairs } from "./utils.js";
10
+ import { obtenirAdresseRelai } from "../relai/index.js";
11
+
12
+ export const créerLibp2psTest = async ({
13
+ n,
14
+ adresseRelai,
15
+ }: {
16
+ n: number;
17
+ adresseRelai?: string;
18
+ }): Promise<{
19
+ libp2ps: Libp2p<ServicesLibp2pTest>[];
20
+ fermer: () => Promise<void>;
21
+ }> => {
22
+ adresseRelai = adresseRelai ?? obtenirAdresseRelai();
23
+
24
+ const libp2ps: Libp2p<ServicesLibp2pTest>[] = [];
25
+ for (const _ of Array(n).keys()) {
26
+ const libp2p = await createLibp2p(
27
+ isBrowser ? OptionsDéfautLibp2pNavigateur() : OptionsDéfautLibp2pNode(),
28
+ );
29
+
30
+ for (const l of libp2ps) {
31
+ await connecterPairs(libp2p, l, { adresseRelai });
32
+ }
33
+ libp2ps.push(libp2p);
34
+ }
35
+
36
+ const fermer = async () => {
37
+ await Promise.all(libp2ps.map((l) => l.stop()));
38
+ };
39
+
40
+ return { libp2ps, fermer };
41
+ };
@@ -0,0 +1,59 @@
1
+ import { Libp2p } from "@libp2p/interface";
2
+ import { WebRTC } from "@multiformats/multiaddr-matcher";
3
+ import { multiaddr, Multiaddr } from "@multiformats/multiaddr";
4
+ import { isBrowser, isElectronRenderer } from "wherearewe";
5
+ import { ServicesLibp2pTest } from "./config.js";
6
+
7
+ const filtreParDéfaut = () => true;
8
+
9
+ type OptionsConnectionPairs = {
10
+ adresseRelai: string;
11
+ filtre?: (ma: Multiaddr) => boolean;
12
+ };
13
+ export const connecterPairs = async <
14
+ T extends Libp2p = Libp2p<ServicesLibp2pTest>,
15
+ >(
16
+ libp2p1: T,
17
+ libp2p2: T,
18
+ options: OptionsConnectionPairs,
19
+ ) => {
20
+ const filtre = options.filtre ?? filtreParDéfaut;
21
+
22
+ if (isBrowser || isElectronRenderer) {
23
+ await libp2p1.dial(multiaddr(options.adresseRelai));
24
+
25
+ const adresse1 = await new Promise<Multiaddr>((resolve) => {
26
+ const testConnecté = () => {
27
+ const adresse = libp2p1
28
+ .getMultiaddrs()
29
+ .filter((ma) => WebRTC.matches(ma) && filtre(ma))
30
+ .pop();
31
+ if (adresse != null) {
32
+ clearInterval(interval);
33
+ resolve(adresse);
34
+ }
35
+ };
36
+ const interval = setInterval(testConnecté, 100);
37
+ testConnecté();
38
+ });
39
+ await libp2p2.dial(adresse1);
40
+ } else {
41
+ await libp2p2.peerStore.save(libp2p1.peerId, {
42
+ multiaddrs: libp2p1.getMultiaddrs().filter(filtre),
43
+ });
44
+ await libp2p2.dial(libp2p1.peerId);
45
+ }
46
+ };
47
+
48
+ export const toutesConnectées = async (
49
+ libp2ps: Libp2p<ServicesLibp2pTest>[],
50
+ options: OptionsConnectionPairs,
51
+ ) => {
52
+ const connectées: Libp2p<ServicesLibp2pTest>[] = [];
53
+ for (const lib of libp2ps) {
54
+ for (const autre of connectées) {
55
+ await connecterPairs(lib, autre, options);
56
+ }
57
+ connectées.push(lib);
58
+ }
59
+ };
package/src/orbite.ts CHANGED
@@ -1,241 +1,56 @@
1
- /*
2
- Certaines parties de ce fichier proviennent d'OrbitDB et de @orbitdb/quickstart
3
-
4
- MIT License
5
-
6
- Copyright (c) 2019 Haja Networks Oy
7
- Copyright (c) 2024 OrbitDB Community
8
-
9
- Permission is hereby granted, free of charge, to any person obtaining a copy
10
- of this software and associated documentation files (the "Software"), to deal
11
- in the Software without restriction, including without limitation the rights
12
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
- copies of the Software, and to permit persons to whom the Software is
14
- furnished to do so, subject to the following conditions:
15
-
16
- The above copyright notice and this permission notice shall be included in all
17
- copies or substantial portions of the Software.
18
-
19
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
- SOFTWARE.
26
- */
27
-
28
- import type { Helia } from "helia";
29
- import type {
30
- Identity,
31
- OrbitDB,
32
- Database,
33
- IdentitiesType,
34
- } from "@orbitdb/core";
35
- import {
36
- type TypedKeyValue,
37
- type TypedFeed,
38
- type TypedSet,
39
- type TypedOrderedKeyValue,
40
- } from "@constl/bohr-db";
41
- import { once } from "events";
42
-
43
- import { dossierTempo } from "@/dossiers.js";
44
- import { connecterPairs } from "@/sfip.js";
45
- import { isNode, isElectronMain } from "wherearewe";
46
-
47
- import { createHelia } from "helia";
48
- import { Libp2p, createLibp2p } from "libp2p";
49
- import { createOrbitDB } from "@orbitdb/core";
50
- import { LevelBlockstore } from "blockstore-level";
51
- import { bitswap } from "@helia/block-brokers";
52
- import {
53
- DefaultLibp2pOptions,
54
- DefaultLibp2pBrowserOptions,
55
- type ServicesLibp2pConstlTest,
56
- } from "./libp2p.js";
57
- import type { ServiceMap } from "@libp2p/interface";
58
-
59
- const isBrowser = () => typeof window !== "undefined";
60
-
61
- const startOrbitDB = async ({
62
- id,
63
- identity,
64
- identities,
65
- directory,
66
- }: {
67
- id?: string;
68
- identity?: Identity;
69
- identities?: IdentitiesType;
70
- directory?: string;
71
- } = {}) => {
72
- const options = isBrowser()
73
- ? DefaultLibp2pBrowserOptions
74
- : DefaultLibp2pOptions;
75
- const libp2p = await createLibp2p({ ...options });
76
- directory = directory || ".";
77
- const blockstore = new LevelBlockstore(`${directory}/ipfs/blocs`);
78
- const ipfs = await createHelia({
79
- libp2p,
80
- blockstore,
81
- blockBrokers: [bitswap()],
82
- });
83
- const orbitdb = await createOrbitDB({
84
- ipfs,
85
- id,
86
- identity,
87
- identities,
88
- directory,
89
- });
90
- return orbitdb;
91
- };
92
-
93
- const stopOrbitDB = async (orbitdb: OrbitDB<ServicesLibp2pConstlTest>) => {
94
- await orbitdb.stop();
95
- await orbitdb.ipfs.stop();
96
- // @ts-expect-error Je ne sais pas pourquoi
97
- await orbitdb.ipfs.blockstore.unwrap().unwrap().child.db.close();
98
- };
99
-
100
- export const créerOrbiteTest = async ({
101
- n = 1,
1
+ import { OrbitDB, createOrbitDB } from "@orbitdb/core";
2
+ import { dossierTempo } from "./dossiers.js";
3
+ import { ServicesLibp2pTest } from "./libp2p/index.js";
4
+ import { créerHéliasTest } from "./hélia.js";
5
+ import { join } from "path";
6
+ import { sousDossier } from "./utils.js";
7
+ import { obtenirAdresseRelai } from "./relai/utils.js";
8
+
9
+ export const créerOrbitesTest = async ({
10
+ n,
11
+ dossier,
12
+ adresseRelai,
102
13
  }: {
103
14
  n: number;
15
+ dossier?: string;
16
+ adresseRelai?: string;
104
17
  }): Promise<{
105
- orbites: OrbitDB<ServicesLibp2pConstlTest>[];
106
- fOublier: () => Promise<void>;
18
+ orbites: OrbitDB<ServicesLibp2pTest>[];
19
+ fermer: () => Promise<void>;
107
20
  }> => {
108
- const sfips: Helia<Libp2p<ServicesLibp2pConstlTest>>[] = [];
109
- const orbites: OrbitDB<ServicesLibp2pConstlTest>[] = [];
110
-
111
- const {
112
- dossier: racineDossierOrbite,
113
- fEffacer: fEffacerRacineDossierOrbite,
114
- } = await dossierTempo();
115
-
116
- const _générer = async (i: number): Promise<void> => {
117
- const racineDossier = `${racineDossierOrbite}/${i}`;
118
- const orbite = await startOrbitDB({
119
- directory: `${racineDossier}/orbite`,
120
- });
121
- for (const ip of sfips) {
122
- await connecterPairs(orbite.ipfs, ip);
123
- }
124
- sfips.push(orbite.ipfs);
125
-
126
- orbites.push(orbite);
127
- };
128
-
129
- await Promise.all([...Array(n).keys()].map((i) => _générer(i)));
130
-
131
- const fOublier = async () => {
132
- for (const o of orbites) await stopOrbitDB(o);
133
-
134
- try {
135
- fEffacerRacineDossierOrbite();
136
- } catch (e) {
137
- // Sur Windows, parfois les fichiers de Hélia sont encore en utilisation
138
- if ((isNode || isElectronMain) && process.platform === "win32") {
139
- console.log("On ignore ça sur Windows\n", e);
140
- return;
141
- } else {
142
- throw e;
143
- }
144
- }
145
- };
146
- return { orbites, fOublier };
147
- };
148
-
149
- export const attendreSync = async (
150
- bd: Awaited<ReturnType<typeof Database>>,
151
- ): Promise<void> => {
152
- await once(bd.events, "update");
153
- };
21
+ adresseRelai = adresseRelai ?? obtenirAdresseRelai();
154
22
 
155
- export type ContrôleurConstellation = {
156
- type: string;
157
- address: string;
158
- adresseBdAccès: string;
159
- write: string;
160
- estAutorisé: (id: string) => Promise<boolean>;
161
- grant: (rôle: "MODÉRATEUR" | "MEMBRE", id: string) => Promise<void>;
162
- revoke: (_rôle: "MODÉRATEUR" | "MEMBRE", _id: string) => Promise<void>;
163
- bd: Awaited<ReturnType<typeof Database>>;
164
- };
23
+ let effacerDossier: () => void = () => {};
24
+ if (!dossier) {
25
+ ({ dossier, effacer: effacerDossier } = await dossierTempo());
26
+ }
165
27
 
166
- const attendreInvité = (
167
- accès: ContrôleurConstellation,
168
- idInvité: string,
169
- ): Promise<void> =>
170
- new Promise<void>((résoudre) => {
171
- const testAutorisé = async () => {
172
- const autorisé = await accès.estAutorisé(idInvité);
173
- if (autorisé) {
174
- résoudre();
175
- } else {
176
- setTimeout(testAutorisé, 100);
177
- }
178
- };
179
- testAutorisé();
28
+ const orbites: OrbitDB<ServicesLibp2pTest>[] = [];
29
+ const { hélias, fermer: fermerHélias } = await créerHéliasTest({
30
+ n,
31
+ dossier,
32
+ adresseRelai,
180
33
  });
181
34
 
182
- export const peutÉcrire = async <T extends ServiceMap = ServiceMap>(
183
- bd:
184
- | TypedKeyValue<{ [clef: string]: number }>
185
- | TypedOrderedKeyValue<{ [clef: string]: number }>
186
- | TypedSet<string>
187
- | TypedFeed<string>,
188
- attendre?: OrbitDB<T>,
189
- ): Promise<boolean> => {
190
- if (attendre) {
191
- await attendreInvité(
192
- bd.access as unknown as ContrôleurConstellation,
193
- attendre.identity.id,
35
+ let i = 0;
36
+ for (const hélia of hélias) {
37
+ orbites.push(
38
+ await createOrbitDB({
39
+ ipfs: hélia,
40
+ directory: join(sousDossier({ dossier, i }), "orbite"),
41
+ }),
194
42
  );
43
+ i++;
195
44
  }
196
45
 
197
- try {
198
- if (bd.type === "keyvalue" || bd.type === "ordered-keyvalue") {
199
- const bdKV = bd as TypedKeyValue<{ [clef: string]: number }>;
200
-
201
- // Important d'avoir une clef unique pour éviter l'interférence entre les tests
202
- const CLEF = "test" + Math.random().toString();
203
- const VAL = 123;
204
-
205
- await bdKV.set(CLEF, VAL);
206
- const val = await bdKV.get(CLEF);
207
-
208
- await bdKV.del(CLEF);
209
- return val === VAL;
210
- } else if (bd.type === "feed") {
211
- const VAL = "test";
212
-
213
- await (bd as TypedFeed<string>).add(VAL);
214
- const éléments = await (bd as TypedFeed<string>).all();
215
-
216
- const autorisé = éléments.length === 1 && éléments[0].value === VAL;
217
- if (éléments.length === 1) {
218
- await (bd as TypedFeed<string>).remove(éléments[0].hash);
219
- }
220
- return autorisé;
221
- } else if (bd.type === "set") {
222
- const VAL = "test";
223
-
224
- await bd.add(VAL);
225
- const éléments = await bd.all();
226
-
227
- const autorisé = éléments.size === 1 && éléments.has(VAL);
228
- await bd.del(VAL);
46
+ const fermer = async () => {
47
+ await Promise.all(orbites.map((o) => o.stop()));
48
+ await fermerHélias();
49
+ effacerDossier();
50
+ };
229
51
 
230
- return autorisé;
231
- } else {
232
- // @ts-expect-error bd.type n'a plus d'options
233
- throw new Error(`Type de BD ${bd.type} non supporté par ce test.`);
234
- }
235
- } catch (e) {
236
- if (e.toString().includes("is not allowed to write to the log")) {
237
- return false;
238
- }
239
- throw e;
240
- }
52
+ return {
53
+ orbites,
54
+ fermer,
55
+ };
241
56
  };
@@ -29,31 +29,26 @@ import { createLibp2p } from "libp2p";
29
29
  import { noise } from "@chainsafe/libp2p-noise";
30
30
  import { circuitRelayServer } from "@libp2p/circuit-relay-v2";
31
31
  import { webSockets } from "@libp2p/websockets";
32
- import * as filters from "@libp2p/websockets/filters";
33
32
  import { identify } from "@libp2p/identify";
34
33
  import { keys } from "@libp2p/crypto";
35
34
  import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
36
35
  import { FaultTolerance } from "@libp2p/interface";
36
+ import { CLEF_PRIVÉE_RELAI, PORT_DÉFAUT_RELAI } from "./consts.js";
37
37
 
38
- // output of: console.log(server.peerId.privateKey.toString('hex'))
39
- const privateKeyString =
40
- "08011240821cb6bc3d4547fcccb513e82e4d718089f8a166b23ffcd4a436754b6b0774cf07447d1693cd10ce11ef950d7517bad6e9472b41a927cd17fc3fb23f8c70cd99";
41
- // L'identité de pair qui correspond à la clef privée ci-dessus
42
- // const relayId = '12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE'
38
+ // produit par: console.log(server.peerId.privateKey.toString('hex'))
39
+ const privateKeyString = process.env.CLEF_PRIVÉE_RELAI || CLEF_PRIVÉE_RELAI;
43
40
 
44
- const encoded = uint8ArrayFromString(privateKeyString, "hex");
45
- const privateKey = keys.privateKeyFromProtobuf(encoded);
41
+ const chiffrée = uint8ArrayFromString(privateKeyString, "hex");
42
+ const clefPrivée = keys.privateKeyFromProtobuf(chiffrée);
46
43
 
47
44
  const relai = await createLibp2p({
48
- privateKey,
45
+ privateKey: clefPrivée,
49
46
  addresses: {
50
- listen: ["/ip4/0.0.0.0/tcp/54321/ws"],
47
+ listen: [
48
+ `/ip4/0.0.0.0/tcp/${process.env.PORT_DÉFAUT_RELAI || PORT_DÉFAUT_RELAI}/ws`,
49
+ ],
51
50
  },
52
- transports: [
53
- webSockets({
54
- filter: filters.all,
55
- }),
56
- ],
51
+ transports: [webSockets()],
57
52
  transportManager: {
58
53
  faultTolerance: FaultTolerance.NO_FATAL,
59
54
  },
@@ -88,4 +83,4 @@ console.log(
88
83
  "p2p addr: ",
89
84
  relai.getMultiaddrs().map((ma) => ma.toString()),
90
85
  );
91
- // generates a deterministic address: /ip4/127.0.0.1/tcp/54321/ws/p2p/12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE
86
+ // génère une adresse prédéterminée : /ip4/127.0.0.1/tcp/54321/ws/p2p/12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE
@@ -0,0 +1,5 @@
1
+ export const PORT_DÉFAUT_RELAI = 54321;
2
+ export const CLEF_PRIVÉE_RELAI =
3
+ "08011240821cb6bc3d4547fcccb513e82e4d718089f8a166b23ffcd4a436754b6b0774cf07447d1693cd10ce11ef950d7517bad6e9472b41a927cd17fc3fb23f8c70cd99";
4
+ export const ID_DÉFAUT_RELAI =
5
+ "12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE";
@@ -0,0 +1,3 @@
1
+ export { ID_DÉFAUT_RELAI, PORT_DÉFAUT_RELAI } from "./consts.js";
2
+ export { obtenirAdresseRelai } from "./utils.js";
3
+ export { lancerRelai, obtenirFichierRelai } from "./relai.js";
@@ -0,0 +1,40 @@
1
+ export const obtenirFichierRelai = async (): Promise<string> => {
2
+ const path = await import("path");
3
+ const url = await import("url");
4
+ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
5
+
6
+ const fichierRelai = path.join(__dirname, "./bin.js");
7
+ return fichierRelai;
8
+ };
9
+
10
+ export const lancerRelai = async ({
11
+ portDéfautRelai,
12
+ clefPrivéeRelai,
13
+ }: {
14
+ portDéfautRelai?: string;
15
+ clefPrivéeRelai?: string;
16
+ } = {}) => {
17
+ const { execa } = await import("execa");
18
+
19
+ const fichierRelai = await obtenirFichierRelai();
20
+
21
+ const relai = execa({
22
+ env: {
23
+ ...process.env,
24
+ PORT_DÉFAUT_RELAI: portDéfautRelai,
25
+ CLEF_PRIVÉE_RELAI: clefPrivéeRelai,
26
+ },
27
+ })`node ${fichierRelai} &`;
28
+
29
+ relai.catch((e) => {
30
+ // Ignorer l'erreur causée par la termination du processus par notre code
31
+ if (e.signal !== "SIGTERM") throw new Error(e);
32
+ });
33
+
34
+ relai.stdout.on("data", (x) => {
35
+ const texte = new TextDecoder().decode(x);
36
+ console.log(texte);
37
+ });
38
+
39
+ return () => relai.kill();
40
+ };
@@ -0,0 +1,14 @@
1
+ import { ID_DÉFAUT_RELAI, PORT_DÉFAUT_RELAI } from "./consts.js";
2
+
3
+ export const obtenirAdresseRelai = (
4
+ {
5
+ portRelai,
6
+ idRelai,
7
+ }: {
8
+ portRelai: number;
9
+ idRelai: string;
10
+ } = {
11
+ portRelai: PORT_DÉFAUT_RELAI,
12
+ idRelai: ID_DÉFAUT_RELAI,
13
+ },
14
+ ) => `/ip4/127.0.0.1/tcp/${portRelai}/ws/p2p/${idRelai}`;
package/src/utils.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { join } from "path";
2
+
3
+ export const sousDossier = ({ dossier, i }: { dossier: string; i: number }) => {
4
+ return join(dossier, i.toString());
5
+ };
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = "1.7.0";
2
+ export const version = "2.0.0";