@constl/utils-tests 0.2.3 → 1.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 (59) hide show
  1. package/.aegir.js +20 -13
  2. package/README.md +12 -1
  3. package/dist/src/@types.js.map +1 -0
  4. package/dist/{attente.js → src/attente.js} +2 -2
  5. package/dist/src/attente.js.map +1 -0
  6. package/dist/src/dossiers.d.ts +4 -0
  7. package/dist/{dossiers.js → src/dossiers.js} +1 -14
  8. package/dist/src/dossiers.js.map +1 -0
  9. package/dist/src/index.d.ts +5 -0
  10. package/dist/src/index.js +7 -0
  11. package/dist/src/index.js.map +1 -0
  12. package/dist/src/orbite.d.ts +22 -0
  13. package/dist/src/orbite.js +112 -0
  14. package/dist/src/orbite.js.map +1 -0
  15. package/dist/src/sfip.d.ts +8 -0
  16. package/dist/src/sfip.js +64 -0
  17. package/dist/src/sfip.js.map +1 -0
  18. package/dist/src/version.d.ts +1 -0
  19. package/dist/src/version.js +3 -0
  20. package/dist/src/version.js.map +1 -0
  21. package/dist/tsconfig.build.tsbuildinfo +1 -1
  22. package/package.json +46 -39
  23. package/renovate.json +1 -3
  24. package/src/@types/{shims-orbit-db-core.d.ts → shims-orbitdb-core.d.ts} +12 -6
  25. package/src/@types/shims-orbitdb-quickstart.d.ts +15 -0
  26. package/src/attente.ts +2 -2
  27. package/src/dossiers.ts +1 -21
  28. package/src/index.ts +4 -107
  29. package/src/orbite.ts +165 -0
  30. package/src/sfip.ts +83 -14
  31. package/src/version.ts +1 -1
  32. package/tsconfig.json +4 -4
  33. package//303/240Faire/constellation.spec.ts +28 -0
  34. package//303/240Faire/constellation.ts +80 -0
  35. package/dist/@types.js.map +0 -1
  36. package/dist/attente.js.map +0 -1
  37. package/dist/client.d.ts +0 -17
  38. package/dist/client.js +0 -74
  39. package/dist/client.js.map +0 -1
  40. package/dist/dossiers.d.ts +0 -8
  41. package/dist/dossiers.js.map +0 -1
  42. package/dist/index.d.ts +0 -13
  43. package/dist/index.js +0 -81
  44. package/dist/index.js.map +0 -1
  45. package/dist/orbitDbTestUtils.d.ts +0 -5
  46. package/dist/orbitDbTestUtils.js +0 -46
  47. package/dist/orbitDbTestUtils.js.map +0 -1
  48. package/dist/sfip.d.ts +0 -7
  49. package/dist/sfip.js +0 -17
  50. package/dist/sfip.js.map +0 -1
  51. package/dist/version.d.ts +0 -1
  52. package/dist/version.js +0 -3
  53. package/dist/version.js.map +0 -1
  54. package/scripts/node-globals.js +0 -3
  55. package/src/client.ts +0 -111
  56. package/src/orbitDbTestUtils.ts +0 -55
  57. /package/dist/{@types.d.ts → src/@types.d.ts} +0 -0
  58. /package/dist/{@types.js → src/@types.js} +0 -0
  59. /package/dist/{attente.d.ts → src/attente.d.ts} +0 -0
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
- "version": "0.2.3",
2
+ "version": "1.0.0",
3
3
  "license": "AGPL-3.0-or-later",
4
4
  "name": "@constl/utils-tests",
5
5
  "author": "Julien Jean Malard-Adam",
6
6
  "type": "module",
7
- "types": "./dist/index.d.ts",
8
- "exports": "./dist/index.js",
7
+ "types": "./dist/src/index.d.ts",
8
+ "exports": "./dist/src/index.js",
9
9
  "engines": {
10
10
  "node": ">=20"
11
11
  },
@@ -20,69 +20,76 @@
20
20
  "postbuild": "./dist/**/*"
21
21
  },
22
22
  "devDependencies": {
23
- "@constl/ipa": "^0.9.5",
24
- "@types/crypto-js": "^4.1.3",
25
- "@types/mocha": "^10.0.3",
26
- "@types/node": "^20.8.9",
27
- "@types/uuid": "^9.0.6",
28
- "@typescript-eslint/eslint-plugin": "^6.0.0",
29
- "@typescript-eslint/parser": "^6.9.0",
30
- "aegir": "^41.0.0",
23
+ "@chainsafe/libp2p-noise": "^15.0.0",
24
+ "@chainsafe/libp2p-yamux": "^6.0.1",
25
+ "@libp2p/circuit-relay-v2": "^1.0.13",
26
+ "@libp2p/crypto": "^4.0.1",
27
+ "@libp2p/identify": "^1.0.12",
28
+ "@libp2p/peer-id-factory": "^4.0.5",
29
+ "@libp2p/websockets": "^8.0.13",
30
+ "@multiformats/multiaddr": "^12.1.14",
31
+ "@multiformats/multiaddr-matcher": "^1.1.2",
32
+ "@orbitdb/feed-db": "^1.0.0",
33
+ "@types/crypto-js": "^4.2.2",
34
+ "@types/file-saver": "^2.0.7",
35
+ "@types/mocha": "^10.0.6",
36
+ "@types/node": "^20.11.10",
37
+ "@types/uuid": "^9.0.8",
38
+ "@typescript-eslint/eslint-plugin": "^6.20.0",
39
+ "@typescript-eslint/parser": "^6.20.0",
40
+ "aegir": "^42.2.2",
31
41
  "browserify-fs": "^1.0.0",
32
42
  "buffer": "^6.0.3",
33
- "core-js": "^3.33.1",
43
+ "core-js": "^3.35.1",
34
44
  "crypto-browserify": "^3.12.0",
35
- "eslint": "^8.52.0",
45
+ "eslint": "^8.56.0",
36
46
  "eslint-config-airbnb-base": "^15.0.0",
37
- "eslint-config-prettier": "^9.0.0",
47
+ "eslint-config-prettier": "^9.1.0",
38
48
  "eslint-config-standard": "^17.1.0",
39
- "eslint-plugin-import": "^2.29.0",
49
+ "eslint-plugin-import": "^2.29.1",
40
50
  "eslint-plugin-node": "^11.1.0",
41
51
  "eslint-plugin-promise": "^6.1.1",
52
+ "file-saver": "^2.0.5",
53
+ "keys": "link:@libp2p/crypto/keys",
42
54
  "mocha": "^10.2.0",
43
55
  "os-browserify": "^0.3.0",
44
- "playwright": "^1.39.0",
45
- "prettier": "^3.0.3",
56
+ "playwright": "^1.41.1",
57
+ "prettier": "^3.2.4",
46
58
  "process": "^0.11.10",
47
59
  "source-map-support": "^0.5.21",
48
- "ts-patch": "^3.0.2",
60
+ "stream-browserify": "^3.0.0",
61
+ "ts-patch": "^3.1.2",
49
62
  "tsconfig-paths": "^4.2.0",
50
63
  "tscpaths": "^0.0.9",
51
- "typescript": "5.2.2",
64
+ "typescript": "5.3.3",
52
65
  "typescript-transform-paths": "^3.4.6"
53
66
  },
54
67
  "dependencies": {
55
- "@orbitdb/core": "^1.0.0",
68
+ "@helia/block-brokers": "^2.0.0",
69
+ "@orbitdb/quickstart": "^1.0.0",
70
+ "blockstore-core": "^4.3.10",
71
+ "blockstore-level": "^1.1.7",
56
72
  "chokidar": "^3.5.3",
57
- "genversion": "^3.1.1",
58
- "ipfs-core": "^0.18.1",
73
+ "events": "^3.3.0",
74
+ "genversion": "^3.2.0",
75
+ "helia": "^3.0.1",
76
+ "libp2p": "^1.2.1",
59
77
  "rimraf": "^5.0.5",
60
- "uint8arrays": "^4.0.6",
78
+ "uint8arrays": "^5.0.1",
61
79
  "uuid": "^9.0.1",
62
80
  "wherearewe": "^2.0.1"
63
81
  },
64
- "resolutions": {
65
- "iso-constants": "git+https://github.com/inverted-capital/iso-constants.git#patch-1",
66
- "ipfs-pubsub-1on1": "git+https://github.com/ipfs-shipyard/ipfs-pubsub-1on1.git"
67
- },
68
- "overrides": {
69
- "iso-constants": {
70
- ".": "git+https://github.com/inverted-capital/iso-constants.git#patch-1"
71
- },
72
- "ipfs-pubsub-1on1": {
73
- ".": "git+https://github.com/ipfs-shipyard/ipfs-pubsub-1on1.git"
74
- }
75
- },
76
82
  "scripts": {
77
83
  "nettoyer": "rimraf dist",
78
- "compiler": "pnpm versioner && pnpm nettoyer && pnpm tspc -p tsconfig.build.json",
84
+ "compiler": "pnpm versioner && pnpm formatter && pnpm nettoyer && pnpm tspc -p tsconfig.build.json",
79
85
  "test:node": "pnpm compiler && pnpm aegir test -t node --cov -- --exit",
80
- "test:navig": "pnpm compiler && pnpm aegir test -t browser --cov",
86
+ "test:navig": "pnpm compiler && pnpm webrtc && pnpm aegir test -t browser --cov",
81
87
  "test:électron-principal": "pnpm compiler && pnpm aegir test -t electron-main --cov",
82
- "test:électron-rendu": "pnpm compiler && pnpm aegir test -t electron-renderer --cov",
88
+ "test:électron-rendu": "pnpm compiler && pnpm webrtc && pnpm aegir test -t electron-renderer --cov",
83
89
  "test": "pnpm test:node && pnpm test:navig",
84
90
  "formatter": "pnpm prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\" && pnpm eslint --ext .js,.ts src",
85
91
  "publier": "pnpm versioner && pnpm compiler && (git commit -a -m 'version' || true) && pnpm publish",
86
- "versioner": "pnpm genversion --es6 --semi src/version.ts"
92
+ "versioner": "pnpm genversion --es6 --semi src/version.ts",
93
+ "webrtc": "node ./test/utils/relai.js &"
87
94
  }
88
95
  }
package/renovate.json CHANGED
@@ -1,8 +1,6 @@
1
1
  {
2
2
  "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3
- "extends": [
4
- "config:base"
5
- ],
3
+ "extends": ["config:base"],
6
4
  "packageRules": [
7
5
  {
8
6
  "matchDepTypes": ["devDependencies"],
@@ -151,10 +151,14 @@ declare module "@orbitdb/core" {
151
151
  };
152
152
 
153
153
  // Temporaires
154
- export type Store = FeedStore | SetStore | KeyValueStore | OrderedKeyValueStore;
155
-
154
+ export type Store =
155
+ | FeedStore
156
+ | SetStore
157
+ | KeyValueStore
158
+ | OrderedKeyValueStore;
159
+
156
160
  export type FeedStore = Awaited<ReturnType<typeof Database>> & {
157
- remove: (hash: string) => Promise<string>
161
+ remove: (hash: string) => Promise<string>;
158
162
  };
159
163
  export type FeedStoreTypé<T extends élémentsBd> = Omit<
160
164
  FeedStore,
@@ -189,11 +193,14 @@ declare module "@orbitdb/core" {
189
193
  export type OrderedKeyValue = ReturnType<typeof Database>;
190
194
  export type OrderedKeyValueStore = Awaited<OrderedKeyValue>;
191
195
  export type OrderedKeyValueStoreTypé<T extends { [clef: string]: unknown }> =
192
- Omit<OrderedKeyValueStore, "put" | "set" | "del" | "move" | "get" | "all"> & {
196
+ Omit<
197
+ OrderedKeyValueStore,
198
+ "put" | "set" | "del" | "move" | "get" | "all"
199
+ > & {
193
200
  put: <K extends keyof T>(
194
201
  key: K,
195
202
  value: T[K],
196
- position?: number
203
+ position?: number,
197
204
  ) => Promise<string>;
198
205
  set: OrderedKeyValueStoreTypé<T>["put"];
199
206
  del: <K extends keyof T>(key: K) => Promise<string>;
@@ -232,5 +239,4 @@ declare module "@orbitdb/core" {
232
239
  get<K extends keyof T>(key: K): Promise<T[K] | undefined>;
233
240
  all(): Promise<T>;
234
241
  };
235
-
236
242
  }
@@ -0,0 +1,15 @@
1
+ declare module "@orbitdb/quickstart" {
2
+ import { Identities, Identity, OrbitDB } from "@orbitdb/core";
3
+ import { Libp2pOptions as Libp2pOptionsType } from "libp2p";
4
+
5
+ export function startOrbitDB(args: {
6
+ id?: string;
7
+ identity?: Identity;
8
+ identities?: Identities;
9
+ directory?: string;
10
+ }): Promise<OrbitDB>;
11
+ export function stopOrbitDB(orbitdb: OrbitDB): Promise<void>;
12
+
13
+ export const DefaultLibp2pOptions: Libp2pOptionsType;
14
+ export const DefaultLibp2pBrowserOptions: Libp2pOptionsType;
15
+ }
package/src/attente.ts CHANGED
@@ -21,12 +21,12 @@ export class AttendreRésultat<T> {
21
21
  }
22
22
 
23
23
  async attendreQue(f: (x: T) => boolean | Promise<boolean>): Promise<T> {
24
- if (this.val !== undefined && await f(this.val)) return this.val;
24
+ if (this.val !== undefined && (await f(this.val))) return this.val;
25
25
  const id = uuidv4();
26
26
 
27
27
  return new Promise((résoudre) => {
28
28
  const fLorsqueChangé = async () => {
29
- if (this.val !== undefined && await f(this.val)) {
29
+ if (this.val !== undefined && (await f(this.val))) {
30
30
  this.oublier(id);
31
31
  résoudre(this.val);
32
32
  }
package/src/dossiers.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { isNode, isElectronMain } from "wherearewe";
2
2
  import { v4 as uuidv4 } from "uuid";
3
3
 
4
- export const dossierTempoTests = async (): Promise<{
4
+ export const dossierTempo = async (): Promise<{
5
5
  dossier: string;
6
6
  fEffacer: () => void;
7
7
  }> => {
@@ -24,23 +24,3 @@ export const dossierTempoTests = async (): Promise<{
24
24
  };
25
25
  }
26
26
  };
27
-
28
- export const obtDirTempoPourTest = async ({
29
- base,
30
- nom,
31
- }: {
32
- base: string;
33
- nom?: string;
34
- }): Promise<string> => {
35
- if (isNode || isElectronMain) {
36
- const fs = await import("fs");
37
- const path = await import("path");
38
-
39
- const dossier = path.resolve(base, (nom || "") + uuidv4());
40
- fs.mkdirSync(dossier, { recursive: true });
41
- return dossier;
42
- } else {
43
- const dossier = base + "/" + (nom || "") + uuidv4();
44
- return dossier;
45
- }
46
- };
package/src/index.ts CHANGED
@@ -1,110 +1,7 @@
1
- import { once } from "events";
2
-
3
- import { FeedStoreTypé, KeyValueStoreTypé, OrbitDB, type Store } from "@orbitdb/core";
4
-
5
- import type { client, réseau, accès } from "@constl/ipa";
6
-
7
- import { AttendreRésultat } from "@/attente.js";
8
-
9
- export * as sfip from "@/sfip.js";
10
1
  export * as attente from "@/attente.js";
11
- export * as client from "@/client.js";
2
+ // export * as client from "@/constellation.js";
12
3
  export * as dossiers from "@/dossiers.js";
13
- export { version } from "@/version.js";
14
-
15
- type ContrôleurConstellation = Awaited<
16
- ReturnType<ReturnType<typeof accès.cntrlConstellation.default>>
17
- >;
18
-
19
- const attendreInvité = (accès: ContrôleurConstellation, idInvité: string): Promise<void> =>
20
- new Promise<void>((resolve) => {
21
- const testAutorisé = async () => {
22
- const autorisé = await accès.estAutorisé(idInvité);
23
- if (autorisé) {
24
- clearInterval(interval);
25
- resolve();
26
- }
27
- };
28
- const interval = setInterval(testAutorisé, 100);
29
- testAutorisé();
30
- });
31
-
32
- export const clientConnectéÀ = async (
33
- client1: client.ClientConstellation,
34
- client2: client.ClientConstellation,
35
- ): Promise<void> => {
36
- const dispositifsConnectés = new AttendreRésultat<
37
- réseau.statutDispositif[]
38
- >();
39
- const idCompte2 = await client2.obtIdCompte();
40
-
41
- const fOublier = await client1.réseau!.suivreConnexionsDispositifs({
42
- f: (dispositifs) => dispositifsConnectés.mettreÀJour(dispositifs),
43
- });
44
- await dispositifsConnectés.attendreQue((dispositifs) => {
45
- return !!dispositifs.find((d) => d.infoDispositif.idCompte === idCompte2);
46
- });
47
- await fOublier();
48
- return;
49
- };
50
-
51
- export const clientsConnectés = async (
52
- ...clients: client.ClientConstellation[]
53
- ): Promise<void> => {
54
- if (clients.length < 2) return;
55
- const promesses: Promise<void>[] = [];
56
- for (let i = 1; i < clients.length; i++) {
57
- for (let j = 0; j < i; j++) {
58
- promesses.push(clientConnectéÀ(clients[i], clients[j]));
59
- promesses.push(clientConnectéÀ(clients[j], clients[i]));
60
- }
61
- }
62
- await Promise.all(promesses);
63
- return;
64
- };
65
-
66
- export const attendreSync = async (bd: Store): Promise<void> => {
67
- const accès = bd.access as unknown as ContrôleurConstellation;
68
- await once(accès.bd!.events, "peer.exchanged");
69
- };
70
-
71
- export const peutÉcrire = async (
72
- bd: KeyValueStoreTypé<{ [clef: string]: number }> | FeedStoreTypé<string>,
73
- attendre?: OrbitDB,
74
- ): Promise<boolean> => {
75
- if (attendre) {
76
- await attendreInvité(bd.access as unknown as ContrôleurConstellation, attendre.identity.id);
77
- }
78
-
79
- try {
80
- if (bd.type === "keyvalue") {
81
- const bdKV = bd as KeyValueStoreTypé<{ [clef: string]: number }>
82
-
83
- // Important d'avoir une clef unique pour éviter l'interférence entre les tests
84
- const CLEF = "test" + Math.random().toString();
85
- const VAL = 123;
86
-
87
- await bdKV.set(CLEF, VAL);
88
- const val = await bdKV.get(CLEF);
89
-
90
- await bdKV.del(CLEF);
91
- return val === VAL;
92
- } else if (bd.type === "feed") {
93
- const VAL = "test";
94
-
95
- await (bd as FeedStoreTypé<string>).add(VAL);
96
- const éléments = await (bd as FeedStoreTypé<string>).all();
4
+ export * as orbite from "@/orbite.js";
5
+ export * as sfip from "@/sfip.js";
97
6
 
98
- const autorisé =
99
- éléments.length === 1 && éléments[0].value === VAL;
100
- if (éléments.length === 1) {
101
- await (bd as FeedStoreTypé<string>).remove(éléments[0].hash);
102
- }
103
- return autorisé;
104
- } else {
105
- throw new Error(`Type de BD ${bd.type} non supporté par ce test.`);
106
- }
107
- } catch {
108
- return false;
109
- }
110
- };
7
+ export { version } from "@/version.js";
package/src/orbite.ts ADDED
@@ -0,0 +1,165 @@
1
+ /*
2
+ Certaines parties de ce fichier proviennent d'OrbitDB
3
+
4
+ MIT License
5
+
6
+ Copyright (c) 2019 Haja Networks Oy
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 type { Helia } from "helia";
28
+ import type {
29
+ FeedStoreTypé,
30
+ KeyValueStoreTypé,
31
+ OrbitDB,
32
+ Store,
33
+ } from "@orbitdb/core";
34
+
35
+ import { once } from "events";
36
+
37
+ import { dossierTempo } from "@/dossiers.js";
38
+ import { connecterPairs } from "@/sfip.js";
39
+
40
+ const OrbitDBQuickstart = await import("@orbitdb/quickstart");
41
+ const { startOrbitDB, stopOrbitDB } = OrbitDBQuickstart;
42
+
43
+ export const créerOrbiteTest = async ({
44
+ n = 1,
45
+ }: {
46
+ n: number;
47
+ }): Promise<{ orbites: OrbitDB[]; fOublier: () => Promise<void> }> => {
48
+ const sfips: Helia[] = [];
49
+ const orbites: OrbitDB[] = [];
50
+
51
+ const {
52
+ dossier: racineDossierOrbite,
53
+ fEffacer: fEffacerRacineDossierOrbite,
54
+ } = await dossierTempo();
55
+
56
+ fEffacerRacineDossierOrbite();
57
+
58
+ const _générer = async (i: number): Promise<void> => {
59
+ const racineDossier = `${racineDossierOrbite}/${i}`;
60
+ const orbite = await startOrbitDB({
61
+ directory: `${racineDossier}/orbite`,
62
+ });
63
+ for (const ip of sfips) {
64
+ await connecterPairs(orbite.ipfs, ip);
65
+ }
66
+ sfips.push(orbite.ipfs);
67
+
68
+ orbites.push(orbite);
69
+ };
70
+
71
+ await Promise.all([...Array(n).keys()].map((i) => _générer(i)));
72
+
73
+ const fOublier = async () => {
74
+ await Promise.all(orbites.map(stopOrbitDB));
75
+
76
+ fEffacerRacineDossierOrbite();
77
+ };
78
+ return { orbites, fOublier };
79
+ };
80
+
81
+ export const attendreSync = async (bd: Store): Promise<void> => {
82
+ const accèsBd = bd.access as unknown as ContrôleurConstellation;
83
+ await once(accèsBd.bd.events, "peer.exchanged");
84
+ };
85
+
86
+ export type ContrôleurConstellation = {
87
+ type: string;
88
+ address: string;
89
+ adresseBdAccès: string;
90
+ write: string;
91
+ estAutorisé: (id: string) => Promise<boolean>;
92
+ grant: (rôle: "MODÉRATEUR" | "MEMBRE", id: string) => Promise<void>;
93
+ revoke: (_rôle: "MODÉRATEUR" | "MEMBRE", _id: string) => Promise<void>;
94
+ bd: Store;
95
+ };
96
+
97
+ const attendreInvité = (
98
+ accès: ContrôleurConstellation,
99
+ idInvité: string,
100
+ ): Promise<void> =>
101
+ new Promise<void>((resolve) => {
102
+ const testAutorisé = async () => {
103
+ const autorisé = await accès.estAutorisé(idInvité);
104
+ if (autorisé) {
105
+ clearInterval(interval);
106
+ resolve();
107
+ }
108
+ };
109
+ const interval = setInterval(testAutorisé, 100);
110
+ testAutorisé();
111
+ });
112
+
113
+ export const peutÉcrire = async (
114
+ bd: KeyValueStoreTypé<{ [clef: string]: number }> | FeedStoreTypé<string>,
115
+ attendre?: OrbitDB,
116
+ ): Promise<boolean> => {
117
+ if (attendre) {
118
+ await attendreInvité(
119
+ bd.access as unknown as ContrôleurConstellation,
120
+ attendre.identity.id,
121
+ );
122
+ }
123
+
124
+ try {
125
+ if (bd.type === "keyvalue") {
126
+ const bdKV = bd as KeyValueStoreTypé<{ [clef: string]: number }>;
127
+
128
+ // Important d'avoir une clef unique pour éviter l'interférence entre les tests
129
+ const CLEF = "test" + Math.random().toString();
130
+ const VAL = 123;
131
+
132
+ await bdKV.set(CLEF, VAL);
133
+ const val = await bdKV.get(CLEF);
134
+
135
+ await bdKV.del(CLEF);
136
+ return val === VAL;
137
+ } else if (bd.type === "feed") {
138
+ const VAL = "test";
139
+
140
+ await (bd as FeedStoreTypé<string>).add(VAL);
141
+ const éléments = await (bd as FeedStoreTypé<string>).all();
142
+
143
+ const autorisé = éléments.length === 1 && éléments[0].value === VAL;
144
+ if (éléments.length === 1) {
145
+ await (bd as FeedStoreTypé<string>).remove(éléments[0].hash);
146
+ }
147
+ return autorisé;
148
+ } else if (bd.type === "set") {
149
+ const VAL = "test";
150
+
151
+ await (bd as FeedStoreTypé<string>).add(VAL);
152
+ const éléments = await (bd as FeedStoreTypé<string>).all();
153
+
154
+ const autorisé = éléments.length === 1 && éléments[0].value === VAL;
155
+ if (éléments.length === 1) {
156
+ await (bd as FeedStoreTypé<string>).remove(éléments[0].hash);
157
+ }
158
+ return autorisé;
159
+ } else {
160
+ throw new Error(`Type de BD ${bd.type} non supporté par ce test.`);
161
+ }
162
+ } catch {
163
+ return false;
164
+ }
165
+ };
package/src/sfip.ts CHANGED
@@ -1,20 +1,89 @@
1
- import { create } from "ipfs-core";
2
- import type { IPFS } from "ipfs-core";
1
+ import { createHelia, type Helia } from "helia";
2
+ import { bitswap } from "@helia/block-brokers";
3
+ import { createLibp2p } from "libp2p";
4
+ import { MemoryBlockstore } from "blockstore-core";
5
+ import { LevelBlockstore } from "blockstore-level";
6
+ import { multiaddr } from "@multiformats/multiaddr";
7
+ import { WebRTC } from "@multiformats/multiaddr-matcher";
8
+ import type { Multiaddr } from "@multiformats/multiaddr";
3
9
 
4
- export const config = {
5
- patience: 10 * 1000,
6
- patienceInit: 60 * 1000,
10
+ import { isBrowser, isElectronRenderer } from "wherearewe";
11
+
12
+ import {
13
+ DefaultLibp2pBrowserOptions,
14
+ DefaultLibp2pOptions,
15
+ } from "@orbitdb/quickstart";
16
+
17
+ export const créerHéliaTest = async ({
18
+ dossier,
19
+ }: {
20
+ dossier?: string;
21
+ } = {}): Promise<Helia> => {
22
+ const options =
23
+ isBrowser || isElectronRenderer
24
+ ? DefaultLibp2pBrowserOptions
25
+ : DefaultLibp2pOptions;
26
+
27
+ const libp2p = await createLibp2p({ ...options });
28
+
29
+ const blockstore = dossier
30
+ ? new LevelBlockstore(`${dossier}/blocks`)
31
+ : new MemoryBlockstore();
32
+
33
+ const heliaOptions = {
34
+ blockstore,
35
+ libp2p,
36
+ blockBrokers: [bitswap()],
37
+ };
38
+
39
+ return createHelia({ ...heliaOptions });
7
40
  };
8
41
 
9
- export const initierSFIP = async (dossier = ""): Promise<IPFS> => {
10
- return create({
11
- repo: dossier,
12
- init: {
13
- profiles: ["test"],
14
- },
15
- });
42
+ const filtreParDéfaut = () => true;
43
+
44
+ export const connecterPairs = async (
45
+ sfip1: Helia,
46
+ sfip2: Helia,
47
+ options = {
48
+ filtre: filtreParDéfaut,
49
+ },
50
+ ) => {
51
+ if (isBrowser || isElectronRenderer) {
52
+ const relayId = "12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE";
53
+
54
+ await sfip1.libp2p.dial(
55
+ multiaddr(`/ip4/127.0.0.1/tcp/12345/ws/p2p/${relayId}`),
56
+ );
57
+
58
+ const adresse1 = await new Promise<Multiaddr>((resolve) => {
59
+ const testConnecté = () => {
60
+ const adresse = sfip1.libp2p
61
+ .getMultiaddrs()
62
+ .filter((ma) => WebRTC.matches(ma))
63
+ .pop();
64
+ if (adresse != null) {
65
+ clearInterval(interval);
66
+ resolve(adresse);
67
+ }
68
+ };
69
+ const interval = setInterval(testConnecté, 100);
70
+ testConnecté();
71
+ });
72
+ await sfip2.libp2p.dial(adresse1);
73
+ } else {
74
+ await sfip2.libp2p.peerStore.save(sfip1.libp2p.peerId, {
75
+ multiaddrs: sfip1.libp2p.getMultiaddrs().filter(options.filtre),
76
+ });
77
+ await sfip2.libp2p.dial(sfip1.libp2p.peerId);
78
+ }
16
79
  };
17
80
 
18
- export const arrêterSFIP = async (sfip: IPFS) => {
19
- await sfip.stop();
81
+ export const toutesConnectées = async (sfips: Helia[]) => {
82
+ const connectés: Helia[] = [];
83
+ for (const sfip of sfips) {
84
+ for (const autre of connectés) {
85
+ await connecterPairs(sfip, autre);
86
+ }
87
+ connectés.push(sfip);
88
+ }
20
89
  };
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '0.2.3';
2
+ export const version = "1.0.0";
package/tsconfig.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "noUnusedLocals": false,
16
16
  "noUnusedParameters": false,
17
17
  "paths": {
18
- "@/*": ["src/*"]
18
+ "@/*": ["src/*"],
19
19
  },
20
20
  "outDir": "./dist",
21
21
  "sourceMap": true,
@@ -31,8 +31,8 @@
31
31
 
32
32
  "plugins": [
33
33
  { "transform": "typescript-transform-paths" },
34
- { "transform": "typescript-transform-paths", "afterDeclarations": true }
35
- ]
34
+ { "transform": "typescript-transform-paths", "afterDeclarations": true },
35
+ ],
36
36
  },
37
- "include": ["src", "@types", "test"]
37
+ "include": ["src", "@types", "test"],
38
38
  }