@constl/utils-tests 0.2.3 → 1.0.1
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/.aegir.js +20 -13
- package/README.md +12 -1
- package/dist/src/@types.js.map +1 -0
- package/dist/{attente.js → src/attente.js} +2 -2
- package/dist/src/attente.js.map +1 -0
- package/dist/src/constellation.d.ts +12 -0
- package/dist/src/constellation.js +51 -0
- package/dist/src/constellation.js.map +1 -0
- package/dist/src/dossiers.d.ts +4 -0
- package/dist/{dossiers.js → src/dossiers.js} +1 -14
- package/dist/src/dossiers.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +7 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/orbite.d.ts +22 -0
- package/dist/src/orbite.js +112 -0
- package/dist/src/orbite.js.map +1 -0
- package/dist/src/sfip.d.ts +8 -0
- package/dist/src/sfip.js +64 -0
- package/dist/src/sfip.js.map +1 -0
- package/dist/src/version.d.ts +1 -0
- package/dist/src/version.js +3 -0
- package/dist/src/version.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +47 -39
- package/renovate.json +1 -3
- package/src/@types/{shims-orbit-db-core.d.ts → shims-orbitdb-core.d.ts} +12 -6
- package/src/@types/shims-orbitdb-quickstart.d.ts +15 -0
- package/src/attente.ts +2 -2
- package/src/constellation.ts +80 -0
- package/src/dossiers.ts +1 -21
- package/src/index.ts +4 -107
- package/src/orbite.ts +165 -0
- package/src/sfip.ts +83 -14
- package/src/version.ts +1 -1
- package/tsconfig.json +4 -4
- package//303/240Faire/constellation.spec.ts +28 -0
- package/dist/@types.js.map +0 -1
- package/dist/attente.js.map +0 -1
- package/dist/client.d.ts +0 -17
- package/dist/client.js +0 -74
- package/dist/client.js.map +0 -1
- package/dist/dossiers.d.ts +0 -8
- package/dist/dossiers.js.map +0 -1
- package/dist/index.d.ts +0 -13
- package/dist/index.js +0 -81
- package/dist/index.js.map +0 -1
- package/dist/orbitDbTestUtils.d.ts +0 -5
- package/dist/orbitDbTestUtils.js +0 -46
- package/dist/orbitDbTestUtils.js.map +0 -1
- package/dist/sfip.d.ts +0 -7
- package/dist/sfip.js +0 -17
- package/dist/sfip.js.map +0 -1
- package/dist/version.d.ts +0 -1
- package/dist/version.js +0 -3
- package/dist/version.js.map +0 -1
- package/scripts/node-globals.js +0 -3
- package/src/client.ts +0 -111
- package/src/orbitDbTestUtils.ts +0 -55
- /package/dist/{@types.d.ts → src/@types.d.ts} +0 -0
- /package/dist/{@types.js → src/@types.js} +0 -0
- /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
|
+
"version": "1.0.1",
|
|
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,77 @@
|
|
|
20
20
|
"postbuild": "./dist/**/*"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"
|
|
23
|
+
"@chainsafe/libp2p-noise": "^15.0.0",
|
|
24
|
+
"@chainsafe/libp2p-yamux": "^6.0.1",
|
|
25
|
+
"@constl/ipa": "^0.9.25",
|
|
26
|
+
"@libp2p/circuit-relay-v2": "^1.0.13",
|
|
27
|
+
"@libp2p/crypto": "^4.0.1",
|
|
28
|
+
"@libp2p/identify": "^1.0.12",
|
|
29
|
+
"@libp2p/peer-id-factory": "^4.0.5",
|
|
30
|
+
"@libp2p/websockets": "^8.0.13",
|
|
31
|
+
"@multiformats/multiaddr": "^12.1.14",
|
|
32
|
+
"@multiformats/multiaddr-matcher": "^1.1.2",
|
|
33
|
+
"@orbitdb/feed-db": "^1.0.0",
|
|
34
|
+
"@types/crypto-js": "^4.2.2",
|
|
35
|
+
"@types/file-saver": "^2.0.7",
|
|
36
|
+
"@types/mocha": "^10.0.6",
|
|
37
|
+
"@types/node": "^20.11.10",
|
|
38
|
+
"@types/uuid": "^9.0.8",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
|
40
|
+
"@typescript-eslint/parser": "^6.20.0",
|
|
41
|
+
"aegir": "^42.2.2",
|
|
31
42
|
"browserify-fs": "^1.0.0",
|
|
32
43
|
"buffer": "^6.0.3",
|
|
33
|
-
"core-js": "^3.
|
|
44
|
+
"core-js": "^3.35.1",
|
|
34
45
|
"crypto-browserify": "^3.12.0",
|
|
35
|
-
"eslint": "^8.
|
|
46
|
+
"eslint": "^8.56.0",
|
|
36
47
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
37
|
-
"eslint-config-prettier": "^9.
|
|
48
|
+
"eslint-config-prettier": "^9.1.0",
|
|
38
49
|
"eslint-config-standard": "^17.1.0",
|
|
39
|
-
"eslint-plugin-import": "^2.29.
|
|
50
|
+
"eslint-plugin-import": "^2.29.1",
|
|
40
51
|
"eslint-plugin-node": "^11.1.0",
|
|
41
52
|
"eslint-plugin-promise": "^6.1.1",
|
|
53
|
+
"file-saver": "^2.0.5",
|
|
54
|
+
"keys": "link:@libp2p/crypto/keys",
|
|
42
55
|
"mocha": "^10.2.0",
|
|
43
56
|
"os-browserify": "^0.3.0",
|
|
44
|
-
"playwright": "^1.
|
|
45
|
-
"prettier": "^3.
|
|
57
|
+
"playwright": "^1.41.1",
|
|
58
|
+
"prettier": "^3.2.4",
|
|
46
59
|
"process": "^0.11.10",
|
|
47
60
|
"source-map-support": "^0.5.21",
|
|
48
|
-
"
|
|
61
|
+
"stream-browserify": "^3.0.0",
|
|
62
|
+
"ts-patch": "^3.1.2",
|
|
49
63
|
"tsconfig-paths": "^4.2.0",
|
|
50
64
|
"tscpaths": "^0.0.9",
|
|
51
|
-
"typescript": "5.
|
|
65
|
+
"typescript": "5.3.3",
|
|
52
66
|
"typescript-transform-paths": "^3.4.6"
|
|
53
67
|
},
|
|
54
68
|
"dependencies": {
|
|
55
|
-
"@
|
|
69
|
+
"@helia/block-brokers": "^2.0.0",
|
|
70
|
+
"@orbitdb/quickstart": "^1.0.0",
|
|
71
|
+
"blockstore-core": "^4.3.10",
|
|
72
|
+
"blockstore-level": "^1.1.7",
|
|
56
73
|
"chokidar": "^3.5.3",
|
|
57
|
-
"
|
|
58
|
-
"
|
|
74
|
+
"events": "^3.3.0",
|
|
75
|
+
"genversion": "^3.2.0",
|
|
76
|
+
"helia": "^3.0.1",
|
|
77
|
+
"libp2p": "^1.2.1",
|
|
59
78
|
"rimraf": "^5.0.5",
|
|
60
|
-
"uint8arrays": "^
|
|
79
|
+
"uint8arrays": "^5.0.1",
|
|
61
80
|
"uuid": "^9.0.1",
|
|
62
81
|
"wherearewe": "^2.0.1"
|
|
63
82
|
},
|
|
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
83
|
"scripts": {
|
|
77
84
|
"nettoyer": "rimraf dist",
|
|
78
|
-
"compiler": "pnpm versioner && pnpm nettoyer && pnpm tspc -p tsconfig.build.json",
|
|
85
|
+
"compiler": "pnpm versioner && pnpm formatter && pnpm nettoyer && pnpm tspc -p tsconfig.build.json",
|
|
79
86
|
"test:node": "pnpm compiler && pnpm aegir test -t node --cov -- --exit",
|
|
80
|
-
"test:navig": "pnpm compiler && pnpm aegir test -t browser --cov",
|
|
87
|
+
"test:navig": "pnpm compiler && pnpm webrtc && pnpm aegir test -t browser --cov",
|
|
81
88
|
"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",
|
|
89
|
+
"test:électron-rendu": "pnpm compiler && pnpm webrtc && pnpm aegir test -t electron-renderer --cov",
|
|
83
90
|
"test": "pnpm test:node && pnpm test:navig",
|
|
84
91
|
"formatter": "pnpm prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\" && pnpm eslint --ext .js,.ts src",
|
|
85
92
|
"publier": "pnpm versioner && pnpm compiler && (git commit -a -m 'version' || true) && pnpm publish",
|
|
86
|
-
"versioner": "pnpm genversion --es6 --semi src/version.ts"
|
|
93
|
+
"versioner": "pnpm genversion --es6 --semi src/version.ts",
|
|
94
|
+
"webrtc": "node ./test/utils/relai.js &"
|
|
87
95
|
}
|
|
88
96
|
}
|
package/renovate.json
CHANGED
|
@@ -151,10 +151,14 @@ declare module "@orbitdb/core" {
|
|
|
151
151
|
};
|
|
152
152
|
|
|
153
153
|
// Temporaires
|
|
154
|
-
export type Store =
|
|
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<
|
|
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
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type réseau,
|
|
3
|
+
type client,
|
|
4
|
+
type ClientConstellation,
|
|
5
|
+
} from "@constl/ipa";
|
|
6
|
+
import { isBrowser } from "wherearewe";
|
|
7
|
+
|
|
8
|
+
import { créerOrbiteTest } from "@/orbite.js";
|
|
9
|
+
import { AttendreRésultat } from "@/attente.js";
|
|
10
|
+
|
|
11
|
+
export const créerConstellationsTest = async <T = ClientConstellation>({
|
|
12
|
+
n = 1,
|
|
13
|
+
fGénérerClient,
|
|
14
|
+
}: {
|
|
15
|
+
n: number;
|
|
16
|
+
fGénérerClient: ({ opts }: { opts: client.optsConstellation }) => T;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
clients: ReturnType<typeof fGénérerClient>[];
|
|
19
|
+
fOublier: () => Promise<void>;
|
|
20
|
+
}> => {
|
|
21
|
+
const clients: ReturnType<typeof fGénérerClient>[] = [];
|
|
22
|
+
const fsOublier: (() => Promise<void>)[] = [];
|
|
23
|
+
|
|
24
|
+
// Nécessaire pour Playwright
|
|
25
|
+
if (isBrowser) window.localStorage.clear();
|
|
26
|
+
|
|
27
|
+
const { orbites, fOublier: fOublierOrbites } = await créerOrbiteTest({ n });
|
|
28
|
+
fsOublier.push(fOublierOrbites);
|
|
29
|
+
|
|
30
|
+
for (const i in [...Array(n).keys()]) {
|
|
31
|
+
const client = fGénérerClient({
|
|
32
|
+
opts: { orbite: orbites[i] },
|
|
33
|
+
});
|
|
34
|
+
clients.push(client);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const fOublier = async () => {
|
|
38
|
+
if (isBrowser) return; // Mystère et boule de gomme !!
|
|
39
|
+
|
|
40
|
+
await Promise.all(
|
|
41
|
+
clients.map((client) => (client as ClientConstellation).fermer()),
|
|
42
|
+
);
|
|
43
|
+
await Promise.all(fsOublier.map((f) => f()));
|
|
44
|
+
};
|
|
45
|
+
return { fOublier, clients };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const constellationConnectéeÀ = async (
|
|
49
|
+
constellation: client.ClientConstellation,
|
|
50
|
+
connectéeÀ: client.ClientConstellation,
|
|
51
|
+
): Promise<void> => {
|
|
52
|
+
const dispositifsConnectés = new AttendreRésultat<
|
|
53
|
+
réseau.statutDispositif[]
|
|
54
|
+
>();
|
|
55
|
+
const idCompte2 = await connectéeÀ.obtIdCompte();
|
|
56
|
+
|
|
57
|
+
const fOublier = await constellation.réseau!.suivreConnexionsDispositifs({
|
|
58
|
+
f: (dispositifs) => dispositifsConnectés.mettreÀJour(dispositifs),
|
|
59
|
+
});
|
|
60
|
+
await dispositifsConnectés.attendreQue((dispositifs) => {
|
|
61
|
+
return !!dispositifs.find((d) => d.infoDispositif.idCompte === idCompte2);
|
|
62
|
+
});
|
|
63
|
+
await fOublier();
|
|
64
|
+
return;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const constellationsConnectées = async (
|
|
68
|
+
...clients: client.ClientConstellation[]
|
|
69
|
+
): Promise<void> => {
|
|
70
|
+
if (clients.length < 2) return;
|
|
71
|
+
const promesses: Promise<void>[] = [];
|
|
72
|
+
for (let i = 1; i < clients.length; i++) {
|
|
73
|
+
for (let j = 0; j < i; j++) {
|
|
74
|
+
promesses.push(constellationConnectéeÀ(clients[i], clients[j]));
|
|
75
|
+
promesses.push(constellationConnectéeÀ(clients[j], clients[i]));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
await Promise.all(promesses);
|
|
79
|
+
return;
|
|
80
|
+
};
|
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
|
|
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
|
|
2
|
+
export * as constellation from "@/constellation.js";
|
|
12
3
|
export * as dossiers from "@/dossiers.js";
|
|
13
|
-
export
|
|
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
|
-
|
|
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
|
+
};
|