@constl/utils-tests 1.6.15 → 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 (84) 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 -42
  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 -21
  33. package/dist/orbite.js +31 -156
  34. package/dist/orbite.js.map +1 -1
  35. package/dist/{scripts/relai.js → relai/bin.js} +12 -17
  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/dist/version.js.map +1 -1
  56. package/package.json +43 -43
  57. package/renovate.json +10 -4
  58. package/src/attente.ts +129 -156
  59. package/src/config.ts +10 -4
  60. package/src/constellation.ts +38 -58
  61. package/src/dossiers.ts +18 -6
  62. package/src/h/303/251lia.ts +61 -0
  63. package/src/index.ts +24 -8
  64. package/src/libp2p/config.ts +115 -0
  65. package/src/libp2p/index.ts +9 -0
  66. package/src/libp2p/libp2p.ts +41 -0
  67. package/src/libp2p/utils.ts +59 -0
  68. package/src/orbite.ts +43 -223
  69. package/src/{scripts/relai.ts → relai/bin.ts} +11 -17
  70. package/src/relai/consts.ts +5 -0
  71. package/src/relai/index.ts +3 -0
  72. package/src/relai/relai.ts +40 -0
  73. package/src/relai/utils.ts +14 -0
  74. package/src/utils.ts +5 -0
  75. package/src/version.ts +1 -1
  76. package/dist/libp2p.d.ts +0 -26
  77. package/dist/libp2p.js.map +0 -1
  78. package/dist/scripts/relai.js.map +0 -1
  79. package/dist/sfip.d.ts +0 -13
  80. package/dist/sfip.js +0 -89
  81. package/dist/sfip.js.map +0 -1
  82. package/src/libp2p.ts +0 -112
  83. package/src/sfip.ts +0 -127
  84. /package/dist/{scripts/relai.d.ts → relai/bin.d.ts} +0 -0
package/src/dossiers.ts CHANGED
@@ -3,7 +3,7 @@ import { v4 as uuidv4 } from "uuid";
3
3
 
4
4
  export const dossierTempo = async (): Promise<{
5
5
  dossier: string;
6
- fEffacer: () => void;
6
+ effacer: () => void;
7
7
  }> => {
8
8
  if (isNode || isElectronMain) {
9
9
  const fs = await import("fs");
@@ -11,14 +11,26 @@ export const dossierTempo = async (): Promise<{
11
11
  const os = await import("os");
12
12
  const sync = (await import("rimraf")).sync;
13
13
 
14
- const dossier = fs.mkdtempSync(path.join(os.tmpdir(), "constl-ipa-"));
15
- const fEffacer = () => sync(dossier);
16
- return { dossier, fEffacer };
14
+ const dossier = fs.mkdtempSync(path.join(os.tmpdir(), "constl-"));
15
+ const effacer = () => {
16
+ try {
17
+ sync(dossier);
18
+ } catch (e) {
19
+ // Sur Windows, parfois les fichiers de Hélia sont encore en utilisation
20
+ if (process.platform === "win32") {
21
+ console.log("On ignore ça sur Windows\n", e);
22
+ return;
23
+ } else {
24
+ throw e;
25
+ }
26
+ }
27
+ };
28
+ return { dossier, effacer };
17
29
  } else {
18
- const dossier = uuidv4() + "/constl-ipa";
30
+ const dossier = uuidv4() + "/constl-";
19
31
  return {
20
32
  dossier,
21
- fEffacer: () => {
33
+ effacer: () => {
22
34
  // Rien à faire, je crois
23
35
  },
24
36
  };
@@ -0,0 +1,61 @@
1
+ import { Helia, createHelia } from "helia";
2
+ import { ServicesLibp2pTest, créerLibp2psTest } from "./libp2p/index.js";
3
+ import { Libp2p } from "@libp2p/interface";
4
+ import { bitswap } from "@helia/block-brokers";
5
+ import { MemoryBlockstore } from "blockstore-core";
6
+ import { join } from "path";
7
+ import { sousDossier } from "./utils.js";
8
+ import { LevelBlockstore } from "blockstore-level";
9
+ import { obtenirAdresseRelai } from "./relai/index.js";
10
+
11
+ export const créerHéliasTest = async ({
12
+ n,
13
+ dossier,
14
+ adresseRelai,
15
+ }: {
16
+ n: number;
17
+ dossier?: string;
18
+ adresseRelai?: string;
19
+ }): Promise<{
20
+ hélias: Helia<Libp2p<ServicesLibp2pTest>>[];
21
+ fermer: () => Promise<void>;
22
+ }> => {
23
+ adresseRelai = adresseRelai ?? obtenirAdresseRelai();
24
+
25
+ const { libp2ps, fermer: fermerLibsp2p } = await créerLibp2psTest({
26
+ n,
27
+ adresseRelai,
28
+ });
29
+
30
+ let i = 0;
31
+ const hélias = await Promise.all(
32
+ libp2ps.map(async (libp2p) => {
33
+ const blockstore = dossier
34
+ ? new LevelBlockstore(
35
+ join(sousDossier({ dossier, i }), "hélia", "blocks"),
36
+ )
37
+ : new MemoryBlockstore();
38
+ i++;
39
+
40
+ const optionsHélia = {
41
+ blockstore,
42
+ libp2p,
43
+ blockBrokers: [bitswap()],
44
+ };
45
+ return await createHelia(optionsHélia);
46
+ }),
47
+ );
48
+
49
+ const fermer = async () => {
50
+ await Promise.all(
51
+ hélias.map(async (h) => {
52
+ await h.stop();
53
+ // @ts-expect-error Je ne sais pas pourquoi
54
+ await h.blockstore.unwrap()?.unwrap()?.child?.db?.close();
55
+ }),
56
+ );
57
+ await fermerLibsp2p();
58
+ };
59
+
60
+ return { hélias, fermer };
61
+ };
package/src/index.ts CHANGED
@@ -1,9 +1,25 @@
1
- export * as attente from "@/attente.js";
2
- export * as constellation from "@/constellation.js";
3
- export * as dossiers from "@/dossiers.js";
4
- export * as orbite from "@/orbite.js";
5
- export * as sfip from "@/sfip.js";
6
- export * as config from "@/config.js";
7
- export * as libp2p from "@/libp2p.js";
1
+ export {
2
+ ServicesLibp2pTest,
3
+ ComposantesServiceClefPrivée,
4
+ ServiceClefPrivée,
5
+ OptionsDéfautLibp2pNode,
6
+ OptionsDéfautLibp2pNavigateur,
7
+ créerLibp2psTest,
8
+ toutesConnectées,
9
+ connecterPairs,
10
+ } from "./libp2p/index.js";
8
11
 
9
- export { version } from "@/version.js";
12
+ export {
13
+ ID_DÉFAUT_RELAI,
14
+ PORT_DÉFAUT_RELAI,
15
+ obtenirAdresseRelai,
16
+ lancerRelai,
17
+ obtenirFichierRelai,
18
+ } from "./relai/index.js";
19
+
20
+ export { attendreFichierExiste, attendreFichierModifié } from "./attente.js";
21
+ export { obtConfigEsbuild, générerConfigÆgir } from "./config.js";
22
+ export { créerConstellationsTest } from "./constellation.js";
23
+ export { dossierTempo } from "./dossiers.js";
24
+ export { créerHéliasTest } from "./hélia.js";
25
+ export { créerOrbitesTest } from "./orbite.js";
@@ -0,0 +1,115 @@
1
+ /*
2
+ Certaines parties de ce fichier proviennent de @orbitdb/quickstart
3
+
4
+ MIT License
5
+
6
+ Copyright (c) 2024 OrbitDB Community
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+ */
26
+
27
+ import {
28
+ gossipsub,
29
+ GossipSubComponents,
30
+ GossipSub,
31
+ } from "@chainsafe/libp2p-gossipsub";
32
+ import { noise } from "@chainsafe/libp2p-noise";
33
+ import { yamux } from "@chainsafe/libp2p-yamux";
34
+ import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
35
+ import { Identify, identify } from "@libp2p/identify";
36
+ import { webRTC } from "@libp2p/webrtc";
37
+ import { webSockets } from "@libp2p/websockets";
38
+
39
+ import { Libp2pOptions } from "libp2p";
40
+
41
+ import { PrivateKey } from "@libp2p/interface";
42
+
43
+ export type ServicesLibp2pTest = {
44
+ identify: Identify;
45
+ pubsub: GossipSub;
46
+ obtClefPrivée: ServiceClefPrivée;
47
+ };
48
+
49
+ export interface ComposantesServiceClefPrivée {
50
+ privateKey: PrivateKey;
51
+ }
52
+
53
+ export class ServiceClefPrivée {
54
+ privateKey: PrivateKey;
55
+
56
+ constructor(components: ComposantesServiceClefPrivée) {
57
+ this.privateKey = components.privateKey;
58
+ }
59
+
60
+ obtenirClef(): PrivateKey {
61
+ return this.privateKey;
62
+ }
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,236 +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 { HeliaLibp2p } from "helia";
29
- import type {
30
- Identity,
31
- OrbitDB,
32
- Database,
33
- IdentitiesType,
34
- } from "@orbitdb/core";
35
- import type { TypedKeyValue, TypedFeed, TypedSet } from "@constl/bohr-db";
36
- import { once } from "events";
37
-
38
- import { dossierTempo } from "@/dossiers.js";
39
- import { connecterPairs } from "@/sfip.js";
40
- import { isNode, isElectronMain } from "wherearewe";
41
-
42
- import { createHelia } from "helia";
43
- import { Libp2p, createLibp2p } from "libp2p";
44
- import { createOrbitDB } from "@orbitdb/core";
45
- import { LevelBlockstore } from "blockstore-level";
46
- import { bitswap } from "@helia/block-brokers";
47
- import {
48
- DefaultLibp2pOptions,
49
- DefaultLibp2pBrowserOptions,
50
- type ServicesLibp2pConstlTest,
51
- } from "./libp2p.js";
52
- import type { ServiceMap } from "@libp2p/interface";
53
-
54
- const isBrowser = () => typeof window !== "undefined";
55
-
56
- const startOrbitDB = async ({
57
- id,
58
- identity,
59
- identities,
60
- directory,
61
- }: {
62
- id?: string;
63
- identity?: Identity;
64
- identities?: IdentitiesType;
65
- directory?: string;
66
- } = {}) => {
67
- const options = isBrowser()
68
- ? DefaultLibp2pBrowserOptions
69
- : DefaultLibp2pOptions;
70
- const libp2p = await createLibp2p({ ...options });
71
- directory = directory || ".";
72
- const blockstore = new LevelBlockstore(`${directory}/ipfs/blocs`);
73
- const ipfs = await createHelia({
74
- libp2p,
75
- blockstore,
76
- blockBrokers: [bitswap()],
77
- });
78
- const orbitdb = await createOrbitDB({
79
- ipfs,
80
- id,
81
- identity,
82
- identities,
83
- directory,
84
- });
85
- return orbitdb;
86
- };
87
-
88
- const stopOrbitDB = async (orbitdb: OrbitDB<ServicesLibp2pConstlTest>) => {
89
- await orbitdb.stop();
90
- await orbitdb.ipfs.stop();
91
- // @ts-expect-error Je ne sais pas pourquoi
92
- await orbitdb.ipfs.blockstore.unwrap().unwrap().child.db.close();
93
- };
94
-
95
- export const créerOrbiteTest = async ({
96
- 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,
97
13
  }: {
98
14
  n: number;
15
+ dossier?: string;
16
+ adresseRelai?: string;
99
17
  }): Promise<{
100
- orbites: OrbitDB<ServicesLibp2pConstlTest>[];
101
- fOublier: () => Promise<void>;
18
+ orbites: OrbitDB<ServicesLibp2pTest>[];
19
+ fermer: () => Promise<void>;
102
20
  }> => {
103
- const sfips: HeliaLibp2p<Libp2p<ServicesLibp2pConstlTest>>[] = [];
104
- const orbites: OrbitDB<ServicesLibp2pConstlTest>[] = [];
105
-
106
- const {
107
- dossier: racineDossierOrbite,
108
- fEffacer: fEffacerRacineDossierOrbite,
109
- } = await dossierTempo();
21
+ adresseRelai = adresseRelai ?? obtenirAdresseRelai();
110
22
 
111
- const _générer = async (i: number): Promise<void> => {
112
- const racineDossier = `${racineDossierOrbite}/${i}`;
113
- const orbite = await startOrbitDB({
114
- directory: `${racineDossier}/orbite`,
115
- });
116
- for (const ip of sfips) {
117
- await connecterPairs(orbite.ipfs, ip);
118
- }
119
- sfips.push(orbite.ipfs);
120
-
121
- orbites.push(orbite);
122
- };
123
-
124
- await Promise.all([...Array(n).keys()].map((i) => _générer(i)));
125
-
126
- const fOublier = async () => {
127
- for (const o of orbites) await stopOrbitDB(o);
128
-
129
- try {
130
- fEffacerRacineDossierOrbite();
131
- } catch (e) {
132
- // Sur Windows, parfois les fichiers de Hélia sont encore en utilisation
133
- if ((isNode || isElectronMain) && process.platform === "win32") {
134
- console.log("On ignore ça sur Windows\n", e);
135
- return;
136
- } else {
137
- throw e;
138
- }
139
- }
140
- };
141
- return { orbites, fOublier };
142
- };
143
-
144
- export const attendreSync = async (
145
- bd: Awaited<ReturnType<typeof Database>>,
146
- ): Promise<void> => {
147
- await once(bd.events, "update");
148
- };
149
-
150
- export type ContrôleurConstellation = {
151
- type: string;
152
- address: string;
153
- adresseBdAccès: string;
154
- write: string;
155
- estAutorisé: (id: string) => Promise<boolean>;
156
- grant: (rôle: "MODÉRATEUR" | "MEMBRE", id: string) => Promise<void>;
157
- revoke: (_rôle: "MODÉRATEUR" | "MEMBRE", _id: string) => Promise<void>;
158
- bd: Awaited<ReturnType<typeof Database>>;
159
- };
23
+ let effacerDossier: () => void = () => {};
24
+ if (!dossier) {
25
+ ({ dossier, effacer: effacerDossier } = await dossierTempo());
26
+ }
160
27
 
161
- const attendreInvité = (
162
- accès: ContrôleurConstellation,
163
- idInvité: string,
164
- ): Promise<void> =>
165
- new Promise<void>((résoudre) => {
166
- const testAutorisé = async () => {
167
- const autorisé = await accès.estAutorisé(idInvité);
168
- if (autorisé) {
169
- résoudre();
170
- } else {
171
- setTimeout(testAutorisé, 100);
172
- }
173
- };
174
- testAutorisé();
28
+ const orbites: OrbitDB<ServicesLibp2pTest>[] = [];
29
+ const { hélias, fermer: fermerHélias } = await créerHéliasTest({
30
+ n,
31
+ dossier,
32
+ adresseRelai,
175
33
  });
176
34
 
177
- export const peutÉcrire = async <T extends ServiceMap = ServiceMap>(
178
- bd:
179
- | TypedKeyValue<{ [clef: string]: number }>
180
- | TypedFeed<string>
181
- | TypedSet<string>,
182
- attendre?: OrbitDB<T>,
183
- ): Promise<boolean> => {
184
- if (attendre) {
185
- await attendreInvité(
186
- bd.access as unknown as ContrôleurConstellation,
187
- 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
+ }),
188
42
  );
43
+ i++;
189
44
  }
190
45
 
191
- try {
192
- if (bd.type === "keyvalue") {
193
- const bdKV = bd as TypedKeyValue<{ [clef: string]: number }>;
194
-
195
- // Important d'avoir une clef unique pour éviter l'interférence entre les tests
196
- const CLEF = "test" + Math.random().toString();
197
- const VAL = 123;
198
-
199
- await bdKV.set(CLEF, VAL);
200
- const val = await bdKV.get(CLEF);
201
-
202
- await bdKV.del(CLEF);
203
- return val === VAL;
204
- } else if (bd.type === "feed") {
205
- const VAL = "test";
206
-
207
- await (bd as TypedFeed<string>).add(VAL);
208
- const éléments = await (bd as TypedFeed<string>).all();
209
-
210
- const autorisé = éléments.length === 1 && éléments[0].value === VAL;
211
- if (éléments.length === 1) {
212
- await (bd as TypedFeed<string>).remove(éléments[0].hash);
213
- }
214
- return autorisé;
215
- } else if (bd.type === "set") {
216
- const VAL = "test";
217
-
218
- await bd.add(VAL);
219
- const éléments = await bd.all();
46
+ const fermer = async () => {
47
+ await Promise.all(orbites.map((o) => o.stop()));
48
+ await fermerHélias();
49
+ effacerDossier();
50
+ };
220
51
 
221
- const autorisé = éléments.length === 1 && éléments[0].value === VAL;
222
- if (éléments.length === 1) {
223
- await bd.del(éléments[0].hash);
224
- }
225
- return autorisé;
226
- } else {
227
- // @ts-expect-error bd.type n'a plus d'options
228
- throw new Error(`Type de BD ${bd.type} non supporté par ce test.`);
229
- }
230
- } catch (e) {
231
- if (e.toString().includes("is not allowed to write to the log")) {
232
- return false;
233
- }
234
- throw e;
235
- }
52
+ return {
53
+ orbites,
54
+ fermer,
55
+ };
236
56
  };