peerbit 4.0.7 → 4.0.8-3a75d6e
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/dist/benchmark/start-stop.d.ts +2 -0
- package/dist/benchmark/start-stop.d.ts.map +1 -0
- package/dist/benchmark/start-stop.js +20 -0
- package/dist/benchmark/start-stop.js.map +1 -0
- package/{lib/esm → dist/src}/bootstrap.d.ts +1 -0
- package/dist/src/bootstrap.d.ts.map +1 -0
- package/{lib/esm → dist/src}/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +3 -0
- package/{lib/esm → dist/src}/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +27 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/{lib/esm → dist/src}/libp2p.js +23 -22
- package/dist/src/libp2p.js.map +1 -0
- package/{lib/esm → dist/src}/peer.d.ts +12 -8
- package/dist/src/peer.d.ts.map +1 -0
- package/{lib/esm → dist/src}/peer.js +38 -23
- package/dist/src/peer.js.map +1 -0
- package/{lib/esm/transports-browser.d.ts → dist/src/transports.browser.d.ts} +2 -1
- package/dist/src/transports.browser.d.ts.map +1 -0
- package/{lib/esm/transports-browser.js → dist/src/transports.browser.js} +5 -4
- package/dist/src/transports.browser.js.map +1 -0
- package/{lib/esm → dist/src}/transports.d.ts +1 -0
- package/dist/src/transports.d.ts.map +1 -0
- package/dist/src/transports.js +21 -0
- package/dist/src/transports.js.map +1 -0
- package/package.json +108 -77
- package/src/bootstrap.ts +2 -2
- package/src/index.ts +1 -1
- package/src/libp2p.ts +39 -29
- package/src/peer.ts +72 -48
- package/src/{transports-browser.ts → transports.browser.ts} +5 -4
- package/src/transports.ts +15 -7
- package/LICENSE +0 -202
- package/lib/esm/index.js +0 -3
- package/lib/esm/libp2p.d.ts +0 -22
- package/lib/esm/libp2p.js.map +0 -1
- package/lib/esm/peer.js.map +0 -1
- package/lib/esm/transports-browser.js.map +0 -1
- package/lib/esm/transports.js +0 -16
- package/lib/esm/transports.js.map +0 -1
- /package/{lib/esm → dist/src}/bootstrap.js +0 -0
- /package/{lib/esm → dist/src}/bootstrap.js.map +0 -0
package/package.json
CHANGED
|
@@ -1,79 +1,110 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
2
|
+
"name": "peerbit",
|
|
3
|
+
"version": "4.0.8-3a75d6e",
|
|
4
|
+
"description": "Peerbit client",
|
|
5
|
+
"author": "dao.xyz",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"types": "./dist/src/index.d.ts",
|
|
10
|
+
"typesVersions": {
|
|
11
|
+
"*": {
|
|
12
|
+
"*": [
|
|
13
|
+
"*",
|
|
14
|
+
"dist/*",
|
|
15
|
+
"dist/src/*",
|
|
16
|
+
"dist/src/*/index"
|
|
17
|
+
],
|
|
18
|
+
"src/*": [
|
|
19
|
+
"*",
|
|
20
|
+
"dist/*",
|
|
21
|
+
"dist/src/*",
|
|
22
|
+
"dist/src/*/index"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"dist",
|
|
29
|
+
"!dist/e2e",
|
|
30
|
+
"!dist/test",
|
|
31
|
+
"!**/*.tsbuildinfo"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/src/index.d.ts",
|
|
36
|
+
"import": "./dist/src/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"eslintConfig": {
|
|
40
|
+
"extends": "peerbit",
|
|
41
|
+
"parserOptions": {
|
|
42
|
+
"project": true,
|
|
43
|
+
"sourceType": "module"
|
|
44
|
+
},
|
|
45
|
+
"ignorePatterns": [
|
|
46
|
+
"!.aegir.js",
|
|
47
|
+
"test/ts-use",
|
|
48
|
+
"*.d.ts"
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "https://github.com/dao-xyz/peerbit"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=18"
|
|
60
|
+
},
|
|
61
|
+
"browser": {
|
|
62
|
+
"./dist/src/transports.js": "./dist/src/transports.browser.js"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@dao-xyz/borsh": "^5.2.3",
|
|
66
|
+
"@peerbit/any-store": "2.1.0-3a75d6e",
|
|
67
|
+
"@peerbit/crypto": "2.3.0-3a75d6e",
|
|
68
|
+
"@peerbit/logger": "1.0.3-3a75d6e",
|
|
69
|
+
"@peerbit/program": "5.0.0-3a75d6e",
|
|
70
|
+
"@peerbit/pubsub": "3.0.6-3a75d6e",
|
|
71
|
+
"@peerbit/blocks": "2.1.0-3a75d6e",
|
|
72
|
+
"@peerbit/indexer-simple": "1.0.0-3a75d6e",
|
|
73
|
+
"@peerbit/indexer-sqlite3": "1.0.0-3a75d6e",
|
|
74
|
+
"datastore-level": "^10.1.5",
|
|
75
|
+
"@dao-xyz/libp2p-noise": "^14.1.2",
|
|
76
|
+
"@chainsafe/libp2p-yamux": "^6.0.2",
|
|
77
|
+
"@libp2p/webrtc": "^4.1.1",
|
|
78
|
+
"@libp2p/websockets": "^8.1.2",
|
|
79
|
+
"@libp2p/identify": "^2.1.2",
|
|
80
|
+
"@libp2p/circuit-relay-v2": "^1.1.2",
|
|
81
|
+
"@libp2p/tcp": "^9.1.1",
|
|
82
|
+
"level": "^8.0.0",
|
|
83
|
+
"memory-level": "^1.0.0",
|
|
84
|
+
"path-browserify": "^1.0.1"
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"p-each-series": "^3.0.0",
|
|
88
|
+
"p-map-series": "^3.0.0",
|
|
89
|
+
"@types/path-browserify": "^1.0.2"
|
|
90
|
+
},
|
|
91
|
+
"scripts": {
|
|
92
|
+
"clean": "aegir clean",
|
|
93
|
+
"build": "aegir build --no-bundle",
|
|
94
|
+
"test": "aegir test -t node",
|
|
95
|
+
"lint": "aegir lint"
|
|
96
|
+
},
|
|
97
|
+
"localMaintainers": [
|
|
98
|
+
"dao.xyz"
|
|
99
|
+
],
|
|
100
|
+
"keywords": [
|
|
101
|
+
"crdt",
|
|
102
|
+
"database",
|
|
103
|
+
"distributed",
|
|
104
|
+
"decentralised",
|
|
105
|
+
"distributed",
|
|
106
|
+
"ipfs",
|
|
107
|
+
"p2p",
|
|
108
|
+
"peer-to-peer"
|
|
109
|
+
]
|
|
79
110
|
}
|
package/src/bootstrap.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export const resolveBootstrapAddresses = async (
|
|
2
|
-
v: string = "4"
|
|
2
|
+
v: string = "4",
|
|
3
3
|
): Promise<string[]> => {
|
|
4
4
|
// Bootstrap addresses for network
|
|
5
5
|
return (
|
|
6
6
|
await (
|
|
7
7
|
await fetch(
|
|
8
|
-
`https://raw.githubusercontent.com/dao-xyz/peerbit-bootstrap/master/bootstrap${v ? "-" + v : ""}.env
|
|
8
|
+
`https://raw.githubusercontent.com/dao-xyz/peerbit-bootstrap/master/bootstrap${v ? "-" + v : ""}.env`,
|
|
9
9
|
)
|
|
10
10
|
).text()
|
|
11
11
|
)
|
package/src/index.ts
CHANGED
package/src/libp2p.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DirectSub } from "@peerbit/pubsub";
|
|
3
|
-
import { DirectBlock } from "@peerbit/blocks";
|
|
1
|
+
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
4
2
|
import { noise } from "@dao-xyz/libp2p-noise";
|
|
5
|
-
import {
|
|
3
|
+
import type { CircuitRelayService } from "@libp2p/circuit-relay-v2";
|
|
6
4
|
import { identify } from "@libp2p/identify";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
5
|
+
import { DirectBlock } from "@peerbit/blocks";
|
|
6
|
+
import { DefaultKeychain, type Keychain } from "@peerbit/keychain";
|
|
7
|
+
import { DirectSub } from "@peerbit/pubsub";
|
|
8
|
+
import {
|
|
9
|
+
type Libp2p,
|
|
10
|
+
type Libp2pOptions,
|
|
11
|
+
type ServiceFactoryMap,
|
|
12
|
+
createLibp2p,
|
|
13
|
+
} from "libp2p";
|
|
14
|
+
import { listen, relay, transports } from "./transports.js";
|
|
10
15
|
|
|
11
16
|
export type Libp2pExtendServices = {
|
|
12
17
|
pubsub: DirectSub;
|
|
@@ -18,7 +23,11 @@ export type Libp2pExtended = Libp2p<
|
|
|
18
23
|
>;
|
|
19
24
|
|
|
20
25
|
export type Libp2pCreateOptions = Libp2pOptions<
|
|
21
|
-
Libp2pExtendServices & { relay: CircuitRelayService; identify: any }
|
|
26
|
+
Partial<Libp2pExtendServices & { relay: CircuitRelayService; identify: any }>
|
|
27
|
+
>;
|
|
28
|
+
|
|
29
|
+
export type PartialLibp2pCreateOptions = Libp2pOptions<
|
|
30
|
+
Partial<Libp2pExtendServices & { relay: CircuitRelayService; identify: any }>
|
|
22
31
|
>;
|
|
23
32
|
|
|
24
33
|
export type Libp2pCreateOptionsWithServices = Libp2pCreateOptions & {
|
|
@@ -26,52 +35,53 @@ export type Libp2pCreateOptionsWithServices = Libp2pCreateOptions & {
|
|
|
26
35
|
};
|
|
27
36
|
|
|
28
37
|
export const createLibp2pExtended = (
|
|
29
|
-
opts:
|
|
38
|
+
opts: PartialLibp2pCreateOptions = {
|
|
30
39
|
services: {
|
|
31
|
-
blocks: (c) => new DirectBlock(c),
|
|
32
|
-
pubsub: (c) => new DirectSub(c),
|
|
33
|
-
keychain: (
|
|
34
|
-
}
|
|
35
|
-
}
|
|
40
|
+
blocks: (c: any) => new DirectBlock(c),
|
|
41
|
+
pubsub: (c: any) => new DirectSub(c),
|
|
42
|
+
keychain: () => new DefaultKeychain(),
|
|
43
|
+
},
|
|
44
|
+
},
|
|
36
45
|
): Promise<Libp2pExtended> => {
|
|
37
|
-
|
|
38
|
-
relay: relay(),
|
|
39
|
-
identify: identify()
|
|
40
|
-
};
|
|
46
|
+
let extraServices: any = {};
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (
|
|
45
|
-
|
|
48
|
+
if (!opts.services?.["relay"]) {
|
|
49
|
+
const relayComponent = relay();
|
|
50
|
+
if (relayComponent) {
|
|
51
|
+
// will be null in browser
|
|
52
|
+
extraServices["relay"] = relayComponent;
|
|
46
53
|
}
|
|
47
|
-
}
|
|
54
|
+
}
|
|
55
|
+
if (!opts.services?.["identify"]) {
|
|
56
|
+
extraServices["identify"] = identify();
|
|
57
|
+
}
|
|
48
58
|
|
|
49
59
|
return createLibp2p({
|
|
50
60
|
...opts,
|
|
51
61
|
connectionManager: {
|
|
52
62
|
minConnections: 0,
|
|
53
|
-
...opts.connectionManager
|
|
63
|
+
...opts.connectionManager,
|
|
54
64
|
},
|
|
55
65
|
addresses: {
|
|
56
66
|
listen: listen(),
|
|
57
|
-
...opts.addresses
|
|
67
|
+
...opts.addresses,
|
|
58
68
|
},
|
|
59
69
|
transports: opts.transports || transports(),
|
|
60
70
|
connectionEncryption: opts.connectionEncryption || [noise()],
|
|
61
71
|
streamMuxers: opts.streamMuxers || [yamux()],
|
|
62
72
|
services: {
|
|
63
|
-
...relayIdentify,
|
|
64
73
|
pubsub:
|
|
65
74
|
opts.services?.pubsub ||
|
|
66
75
|
((c) =>
|
|
67
76
|
new DirectSub(c, {
|
|
68
|
-
canRelayMessage: true
|
|
77
|
+
canRelayMessage: true,
|
|
69
78
|
// auto dial true
|
|
70
79
|
// auto prune true
|
|
71
80
|
})),
|
|
72
81
|
blocks: opts.services?.blocks || ((c) => new DirectBlock(c)),
|
|
73
82
|
keychain: opts.services?.keychain || ((c) => new DefaultKeychain()),
|
|
74
|
-
...opts.services
|
|
75
|
-
|
|
83
|
+
...opts.services,
|
|
84
|
+
...extraServices,
|
|
85
|
+
},
|
|
76
86
|
});
|
|
77
87
|
};
|
package/src/peer.ts
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import "@libp2p/peer-id";
|
|
2
|
+
import {
|
|
3
|
+
type Multiaddr,
|
|
4
|
+
isMultiaddr,
|
|
5
|
+
multiaddr,
|
|
6
|
+
} from "@multiformats/multiaddr";
|
|
7
|
+
import { type AnyStore, createStore } from "@peerbit/any-store";
|
|
8
|
+
import { DirectBlock } from "@peerbit/blocks";
|
|
4
9
|
import { Ed25519Keypair, Ed25519PublicKey } from "@peerbit/crypto";
|
|
10
|
+
import type { Indices } from "@peerbit/indexer-interface";
|
|
11
|
+
import { create as createSQLiteIndexer } from "@peerbit/indexer-sqlite3";
|
|
12
|
+
import { DefaultKeychain } from "@peerbit/keychain";
|
|
13
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
5
14
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
15
|
+
type Address,
|
|
16
|
+
type ExtractArgs,
|
|
17
|
+
type OpenOptions,
|
|
18
|
+
type Program,
|
|
19
|
+
type ProgramClient,
|
|
20
|
+
ProgramHandler,
|
|
10
21
|
} from "@peerbit/program";
|
|
11
22
|
import { DirectSub } from "@peerbit/pubsub";
|
|
23
|
+
import { waitFor } from "@peerbit/time";
|
|
24
|
+
import { LevelDatastore } from "datastore-level";
|
|
25
|
+
import type { Libp2p } from "libp2p";
|
|
12
26
|
import sodium from "libsodium-wrappers";
|
|
13
27
|
import path from "path-browserify";
|
|
14
|
-
import {
|
|
15
|
-
import "@libp2p/peer-id";
|
|
16
|
-
|
|
28
|
+
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
17
29
|
import {
|
|
30
|
+
type Libp2pCreateOptions as ClientCreateOptions,
|
|
31
|
+
type Libp2pExtended,
|
|
32
|
+
type PartialLibp2pCreateOptions,
|
|
18
33
|
createLibp2pExtended,
|
|
19
|
-
Libp2pExtended,
|
|
20
|
-
Libp2pCreateOptions as ClientCreateOptions
|
|
21
34
|
} from "./libp2p.js";
|
|
22
|
-
import { DirectBlock } from "@peerbit/blocks";
|
|
23
|
-
import { LevelDatastore } from "datastore-level";
|
|
24
|
-
import { logger as loggerFn } from "@peerbit/logger";
|
|
25
|
-
import { OpenOptions } from "@peerbit/program";
|
|
26
|
-
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
27
|
-
import { createStore } from "@peerbit/any-store";
|
|
28
|
-
import { DefaultKeychain } from "@peerbit/keychain";
|
|
29
|
-
import { ExtractArgs } from "@peerbit/program";
|
|
30
35
|
|
|
31
36
|
export const logger = loggerFn({ module: "client" });
|
|
32
37
|
|
|
@@ -36,12 +41,14 @@ export type OptionalCreateOptions = {
|
|
|
36
41
|
export type CreateOptions = {
|
|
37
42
|
directory?: string;
|
|
38
43
|
storage: AnyStore;
|
|
44
|
+
indexer: Indices;
|
|
39
45
|
identity: Ed25519Keypair;
|
|
40
46
|
} & OptionalCreateOptions;
|
|
41
|
-
type Libp2pOptions = { libp2p?: Libp2pExtended |
|
|
47
|
+
type Libp2pOptions = { libp2p?: Libp2pExtended | PartialLibp2pCreateOptions };
|
|
42
48
|
type SimpleLibp2pOptions = { relay?: boolean };
|
|
43
49
|
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
44
50
|
directory?: string;
|
|
51
|
+
indexer?: (directory?: string) => Promise<Indices> | Indices;
|
|
45
52
|
} & OptionalCreateOptions;
|
|
46
53
|
|
|
47
54
|
const isLibp2pInstance = (libp2p: Libp2pExtended | ClientCreateOptions) =>
|
|
@@ -49,7 +56,7 @@ const isLibp2pInstance = (libp2p: Libp2pExtended | ClientCreateOptions) =>
|
|
|
49
56
|
|
|
50
57
|
const createCache = async (
|
|
51
58
|
directory: string | undefined,
|
|
52
|
-
options?: { reset?: boolean }
|
|
59
|
+
options?: { reset?: boolean },
|
|
53
60
|
) => {
|
|
54
61
|
const cache = createStore(directory);
|
|
55
62
|
|
|
@@ -69,6 +76,7 @@ export class Peerbit implements ProgramClient {
|
|
|
69
76
|
directory?: string;
|
|
70
77
|
|
|
71
78
|
private _storage: AnyStore;
|
|
79
|
+
private _indexer: Indices;
|
|
72
80
|
private _libp2pExternal?: boolean = false;
|
|
73
81
|
|
|
74
82
|
// Libp2p peerid in Identity form
|
|
@@ -83,7 +91,7 @@ export class Peerbit implements ProgramClient {
|
|
|
83
91
|
if (this.libp2p.peerId.type !== "Ed25519") {
|
|
84
92
|
throw new Error(
|
|
85
93
|
"Unsupported id type, expecting Ed25519 but got " +
|
|
86
|
-
this.libp2p.peerId.type
|
|
94
|
+
this.libp2p.peerId.type,
|
|
87
95
|
);
|
|
88
96
|
}
|
|
89
97
|
|
|
@@ -95,6 +103,7 @@ export class Peerbit implements ProgramClient {
|
|
|
95
103
|
this.directory = options.directory;
|
|
96
104
|
this._storage = options.storage;
|
|
97
105
|
this._libp2pExternal = options.libp2pExternal;
|
|
106
|
+
this._indexer = options.indexer;
|
|
98
107
|
}
|
|
99
108
|
|
|
100
109
|
static async create(options: CreateInstanceOptions = {}): Promise<Peerbit> {
|
|
@@ -102,27 +111,32 @@ export class Peerbit implements ProgramClient {
|
|
|
102
111
|
|
|
103
112
|
let libp2pExtended: Libp2pExtended | undefined = (options as Libp2pOptions)
|
|
104
113
|
.libp2p as Libp2pExtended;
|
|
114
|
+
|
|
105
115
|
const asRelay = (options as SimpleLibp2pOptions).relay;
|
|
106
116
|
|
|
107
117
|
const directory = options.directory;
|
|
108
118
|
const hasDir = directory != null;
|
|
109
119
|
|
|
110
120
|
const storage = await createCache(
|
|
111
|
-
directory != null ? path.join(directory, "/cache") : undefined
|
|
121
|
+
directory != null ? path.join(directory, "/cache") : undefined,
|
|
112
122
|
);
|
|
113
123
|
|
|
124
|
+
const indexerFn = options.indexer || createSQLiteIndexer;
|
|
125
|
+
const indexer =
|
|
126
|
+
directory != null
|
|
127
|
+
? await indexerFn(path.join(directory, "/index"))
|
|
128
|
+
: await indexerFn();
|
|
129
|
+
|
|
114
130
|
const blocksDirectory = hasDir
|
|
115
|
-
? path.join(
|
|
131
|
+
? path.join(directory, "/blocks").toString()
|
|
116
132
|
: undefined;
|
|
117
133
|
|
|
118
134
|
const keychainDirectory = hasDir
|
|
119
|
-
? path.join(
|
|
135
|
+
? path.join(directory, "/keychain").toString()
|
|
120
136
|
: undefined;
|
|
121
137
|
|
|
122
138
|
const datastore = hasDir
|
|
123
|
-
? new LevelDatastore(
|
|
124
|
-
path.join(options["directory"], "/libp2p").toString()
|
|
125
|
-
)
|
|
139
|
+
? new LevelDatastore(path.join(directory, "/libp2p").toString())
|
|
126
140
|
: undefined;
|
|
127
141
|
|
|
128
142
|
if (datastore) {
|
|
@@ -133,8 +147,11 @@ export class Peerbit implements ProgramClient {
|
|
|
133
147
|
if (!libp2pExternal) {
|
|
134
148
|
const extendedOptions: ClientCreateOptions | undefined =
|
|
135
149
|
libp2pExtended as any as ClientCreateOptions;
|
|
150
|
+
const store = createStore(keychainDirectory);
|
|
151
|
+
await store.open();
|
|
152
|
+
|
|
136
153
|
const keychain = new DefaultKeychain({
|
|
137
|
-
store
|
|
154
|
+
store,
|
|
138
155
|
});
|
|
139
156
|
const peerId =
|
|
140
157
|
extendedOptions?.peerId ||
|
|
@@ -145,16 +162,16 @@ export class Peerbit implements ProgramClient {
|
|
|
145
162
|
...extendedOptions,
|
|
146
163
|
peerId,
|
|
147
164
|
services: {
|
|
148
|
-
keychain: (c) => keychain,
|
|
149
|
-
blocks: (c) =>
|
|
165
|
+
keychain: (c: any) => keychain,
|
|
166
|
+
blocks: (c: any) =>
|
|
150
167
|
new DirectBlock(c, {
|
|
151
168
|
canRelayMessage: asRelay,
|
|
152
|
-
directory: blocksDirectory
|
|
169
|
+
directory: blocksDirectory,
|
|
153
170
|
}),
|
|
154
|
-
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
155
|
-
...extendedOptions?.services
|
|
156
|
-
},
|
|
157
|
-
datastore
|
|
171
|
+
pubsub: (c: any) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
172
|
+
...extendedOptions?.services,
|
|
173
|
+
} as any, // TODO types are funky
|
|
174
|
+
datastore,
|
|
158
175
|
});
|
|
159
176
|
}
|
|
160
177
|
if (datastore) {
|
|
@@ -175,7 +192,7 @@ export class Peerbit implements ProgramClient {
|
|
|
175
192
|
if (libp2pExtended.peerId.type !== "Ed25519") {
|
|
176
193
|
throw new Error(
|
|
177
194
|
"Unsupported id type, expecting Ed25519 but got " +
|
|
178
|
-
libp2pExtended.peerId.type
|
|
195
|
+
libp2pExtended.peerId.type,
|
|
179
196
|
);
|
|
180
197
|
}
|
|
181
198
|
|
|
@@ -183,10 +200,10 @@ export class Peerbit implements ProgramClient {
|
|
|
183
200
|
try {
|
|
184
201
|
await libp2pExtended.services.keychain.import({
|
|
185
202
|
keypair: identity,
|
|
186
|
-
id: SELF_IDENTITY_KEY_ID
|
|
203
|
+
id: SELF_IDENTITY_KEY_ID,
|
|
187
204
|
});
|
|
188
205
|
} catch (error: any) {
|
|
189
|
-
if (error.code
|
|
206
|
+
if (error.code === "ERR_KEY_ALREADY_EXISTS") {
|
|
190
207
|
// Do nothing
|
|
191
208
|
} else {
|
|
192
209
|
throw error;
|
|
@@ -195,9 +212,10 @@ export class Peerbit implements ProgramClient {
|
|
|
195
212
|
|
|
196
213
|
const peer = new Peerbit(libp2pExtended, {
|
|
197
214
|
directory,
|
|
198
|
-
storage
|
|
215
|
+
storage,
|
|
199
216
|
libp2pExternal,
|
|
200
|
-
identity
|
|
217
|
+
identity,
|
|
218
|
+
indexer,
|
|
201
219
|
});
|
|
202
220
|
return peer;
|
|
203
221
|
}
|
|
@@ -228,10 +246,10 @@ export class Peerbit implements ProgramClient {
|
|
|
228
246
|
* Dial a peer with an Ed25519 peerId
|
|
229
247
|
*/
|
|
230
248
|
async dial(
|
|
231
|
-
address: string | Multiaddr | Multiaddr[] | ProgramClient
|
|
249
|
+
address: string | Multiaddr | Multiaddr[] | ProgramClient,
|
|
232
250
|
): Promise<boolean> {
|
|
233
251
|
const maddress =
|
|
234
|
-
typeof address
|
|
252
|
+
typeof address === "string"
|
|
235
253
|
? multiaddr(address)
|
|
236
254
|
: isMultiaddr(address) || Array.isArray(address)
|
|
237
255
|
? address
|
|
@@ -244,13 +262,14 @@ export class Peerbit implements ProgramClient {
|
|
|
244
262
|
(await waitFor(
|
|
245
263
|
() =>
|
|
246
264
|
this.libp2p.services.pubsub.peers.has(publicKey.hashcode()) &&
|
|
247
|
-
this.libp2p.services.blocks.peers.has(publicKey.hashcode())
|
|
265
|
+
this.libp2p.services.blocks.peers.has(publicKey.hashcode()),
|
|
248
266
|
)) || false
|
|
249
267
|
);
|
|
250
268
|
}
|
|
251
269
|
|
|
252
270
|
async start() {
|
|
253
271
|
await this._storage.open();
|
|
272
|
+
await this.indexer.start();
|
|
254
273
|
|
|
255
274
|
if (this.libp2p.status === "stopped" || this.libp2p.status === "stopping") {
|
|
256
275
|
this._libp2pExternal = false; // this means we will also close libp2p client on close
|
|
@@ -260,6 +279,7 @@ export class Peerbit implements ProgramClient {
|
|
|
260
279
|
async stop() {
|
|
261
280
|
await this._handler?.stop();
|
|
262
281
|
await this._storage.close();
|
|
282
|
+
await this.indexer.stop();
|
|
263
283
|
|
|
264
284
|
// Close libp2p (after above)
|
|
265
285
|
if (!this._libp2pExternal) {
|
|
@@ -274,7 +294,7 @@ export class Peerbit implements ProgramClient {
|
|
|
274
294
|
throw new Error("Failed to find any addresses to dial");
|
|
275
295
|
}
|
|
276
296
|
const settled = await Promise.allSettled(
|
|
277
|
-
addresses.map((x) => this.dial(x))
|
|
297
|
+
addresses.map((x) => this.dial(x)),
|
|
278
298
|
);
|
|
279
299
|
let once = false;
|
|
280
300
|
for (const [i, result] of settled.entries()) {
|
|
@@ -285,7 +305,7 @@ export class Peerbit implements ProgramClient {
|
|
|
285
305
|
"Failed to dial bootstrap address(s): " +
|
|
286
306
|
JSON.stringify(addresses[i]) +
|
|
287
307
|
". Reason: " +
|
|
288
|
-
result.reason
|
|
308
|
+
result.reason,
|
|
289
309
|
);
|
|
290
310
|
}
|
|
291
311
|
}
|
|
@@ -304,7 +324,7 @@ export class Peerbit implements ProgramClient {
|
|
|
304
324
|
|
|
305
325
|
async open<S extends Program<ExtractArgs<S>>>(
|
|
306
326
|
storeOrAddress: S | Address | string,
|
|
307
|
-
options: OpenOptions<S> = {}
|
|
327
|
+
options: OpenOptions<S> = {},
|
|
308
328
|
): Promise<S> {
|
|
309
329
|
return (
|
|
310
330
|
this._handler || (this._handler = new ProgramHandler({ client: this }))
|
|
@@ -314,4 +334,8 @@ export class Peerbit implements ProgramClient {
|
|
|
314
334
|
get storage() {
|
|
315
335
|
return this._storage;
|
|
316
336
|
}
|
|
337
|
+
|
|
338
|
+
get indexer() {
|
|
339
|
+
return this._indexer;
|
|
340
|
+
}
|
|
317
341
|
}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { webSockets } from "@libp2p/websockets";
|
|
2
1
|
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
3
2
|
import { webRTC } from "@libp2p/webrtc";
|
|
3
|
+
import { webSockets } from "@libp2p/websockets";
|
|
4
4
|
import { all } from "@libp2p/websockets/filters";
|
|
5
5
|
|
|
6
6
|
export const transports = () => [
|
|
7
7
|
webSockets({ filter: all }),
|
|
8
8
|
circuitRelayTransport({
|
|
9
|
-
discoverRelays: 1
|
|
9
|
+
discoverRelays: 1,
|
|
10
|
+
reservationCompletionTimeout: 5000,
|
|
10
11
|
}),
|
|
11
|
-
webRTC({})
|
|
12
|
+
webRTC({}),
|
|
12
13
|
];
|
|
13
14
|
|
|
14
|
-
export const relay = () => undefined;
|
|
15
|
+
export const relay = () => undefined as any;
|
|
15
16
|
|
|
16
17
|
export const listen: () => string[] | undefined = () => ["/webrtc"];
|
package/src/transports.ts
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
circuitRelayServer,
|
|
3
|
+
circuitRelayTransport,
|
|
4
|
+
} from "@libp2p/circuit-relay-v2";
|
|
3
5
|
import { tcp } from "@libp2p/tcp";
|
|
4
|
-
import {
|
|
6
|
+
import { webSockets } from "@libp2p/websockets";
|
|
5
7
|
import { all } from "@libp2p/websockets/filters";
|
|
8
|
+
|
|
6
9
|
export const transports = () => [
|
|
7
10
|
webSockets({ filter: all }),
|
|
8
|
-
circuitRelayTransport(
|
|
9
|
-
|
|
11
|
+
circuitRelayTransport({
|
|
12
|
+
reservationCompletionTimeout: 5000,
|
|
13
|
+
}),
|
|
14
|
+
tcp(),
|
|
10
15
|
];
|
|
11
16
|
export const relay = () =>
|
|
12
|
-
|
|
17
|
+
// applyDefaultLimit: false because of https://github.com/libp2p/js-libp2p/issues/2622
|
|
18
|
+
circuitRelayServer({
|
|
19
|
+
reservations: { applyDefaultLimit: false, maxReservations: 1000 },
|
|
20
|
+
});
|
|
13
21
|
|
|
14
22
|
export const listen: () => string[] | undefined = () => [
|
|
15
23
|
"/ip4/127.0.0.1/tcp/0",
|
|
16
|
-
"/ip4/127.0.0.1/tcp/0/ws"
|
|
24
|
+
"/ip4/127.0.0.1/tcp/0/ws",
|
|
17
25
|
];
|