peerbit 4.0.7-aa577a5 → 4.0.7-cccc078
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.js +2 -2
- package/dist/benchmark/start-stop.js.map +1 -1
- package/dist/src/bootstrap.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +10 -6
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +23 -22
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer.d.ts +10 -7
- package/dist/src/peer.d.ts.map +1 -1
- package/dist/src/peer.js +35 -22
- package/dist/src/peer.js.map +1 -1
- package/dist/src/transports.browser.d.ts.map +1 -1
- package/dist/src/transports.browser.js +4 -3
- package/dist/src/transports.browser.js.map +1 -1
- package/dist/src/transports.d.ts.map +1 -1
- package/dist/src/transports.js +12 -7
- package/dist/src/transports.js.map +1 -1
- package/package.json +18 -15
- package/src/bootstrap.ts +2 -2
- package/src/index.ts +1 -1
- package/src/libp2p.ts +38 -28
- package/src/peer.ts +70 -45
- package/src/transports.browser.ts +4 -3
- package/src/transports.ts +15 -7
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import B from "benchmark";
|
|
2
2
|
import { Peerbit } from "../src/peer.js";
|
|
3
|
-
// Run with "node --loader ts-node/esm ./
|
|
3
|
+
// Run with "node --loader ts-node/esm ./benchmark/start-stop.ts"
|
|
4
4
|
// start and stop x 91.15 ops/sec ±0.95% (87 runs sampled)
|
|
5
5
|
const suite = new B.Suite();
|
|
6
6
|
suite
|
|
@@ -11,7 +11,7 @@ suite
|
|
|
11
11
|
await node.stop();
|
|
12
12
|
deferred.resolve();
|
|
13
13
|
},
|
|
14
|
-
defer: true
|
|
14
|
+
defer: true,
|
|
15
15
|
})
|
|
16
16
|
.on("cycle", (event) => {
|
|
17
17
|
console.log(String(event.target));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"start-stop.js","sourceRoot":"","sources":["../../benchmark/start-stop.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,WAAW,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"start-stop.js","sourceRoot":"","sources":["../../benchmark/start-stop.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,WAAW,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,iEAAiE;AAEjE,0DAA0D;AAC1D,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;AAC5B,KAAK;KACH,GAAG,CAAC,gBAAgB,EAAE;IACtB,EAAE,EAAE,KAAK,EAAE,QAAa,EAAE,EAAE;QAC3B,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC;QACpC,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACnB,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,QAAQ,CAAC,OAAO,EAAE,CAAC;IACpB,CAAC;IACD,KAAK,EAAE,IAAI;CACX,CAAC;KACD,EAAE,CAAC,OAAO,EAAE,CAAC,KAAU,EAAE,EAAE;IAC3B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;KACD,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../../src/bootstrap.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,yBAAyB,OAClC,MAAM,KACP,
|
|
1
|
+
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../../src/bootstrap.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,yBAAyB,OAClC,MAAM,KACP,OAAO,CAAC,MAAM,EAAE,CAWlB,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./peer.js";
|
|
2
|
-
export { createLibp2pExtended, type Libp2pExtendServices, type Libp2pExtended, type Libp2pCreateOptions, type Libp2pCreateOptionsWithServices } from "./libp2p.js";
|
|
2
|
+
export { createLibp2pExtended, type Libp2pExtendServices, type Libp2pExtended, type Libp2pCreateOptions, type Libp2pCreateOptionsWithServices, } from "./libp2p.js";
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EACN,oBAAoB,EACpB,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,+BAA+B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EACN,oBAAoB,EACpB,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,+BAA+B,GACpC,MAAM,aAAa,CAAC"}
|
package/dist/src/index.js
CHANGED
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EACN,oBAAoB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EACN,oBAAoB,GAKpB,MAAM,aAAa,CAAC"}
|
package/dist/src/libp2p.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type Libp2p, type Libp2pOptions, type ServiceFactoryMap } from "libp2p";
|
|
2
|
-
import { DirectSub } from "@peerbit/pubsub";
|
|
3
|
-
import { DirectBlock } from "@peerbit/blocks";
|
|
4
1
|
import type { CircuitRelayService } from "@libp2p/circuit-relay-v2";
|
|
2
|
+
import { DirectBlock } from "@peerbit/blocks";
|
|
5
3
|
import { type Keychain } from "@peerbit/keychain";
|
|
4
|
+
import { DirectSub } from "@peerbit/pubsub";
|
|
5
|
+
import { type Libp2p, type Libp2pOptions, type ServiceFactoryMap } from "libp2p";
|
|
6
6
|
export type Libp2pExtendServices = {
|
|
7
7
|
pubsub: DirectSub;
|
|
8
8
|
blocks: DirectBlock;
|
|
@@ -12,12 +12,16 @@ export type Libp2pExtended = Libp2p<{
|
|
|
12
12
|
relay: CircuitRelayService;
|
|
13
13
|
identify: any;
|
|
14
14
|
} & Libp2pExtendServices>;
|
|
15
|
-
export type Libp2pCreateOptions = Libp2pOptions<Libp2pExtendServices & {
|
|
15
|
+
export type Libp2pCreateOptions = Libp2pOptions<Partial<Libp2pExtendServices & {
|
|
16
|
+
relay: CircuitRelayService;
|
|
17
|
+
identify: any;
|
|
18
|
+
}>>;
|
|
19
|
+
export type PartialLibp2pCreateOptions = Libp2pOptions<Partial<Libp2pExtendServices & {
|
|
16
20
|
relay: CircuitRelayService;
|
|
17
21
|
identify: any;
|
|
18
|
-
}
|
|
22
|
+
}>>;
|
|
19
23
|
export type Libp2pCreateOptionsWithServices = Libp2pCreateOptions & {
|
|
20
24
|
services: ServiceFactoryMap<Libp2pExtendServices>;
|
|
21
25
|
};
|
|
22
|
-
export declare const createLibp2pExtended: (opts?:
|
|
26
|
+
export declare const createLibp2pExtended: (opts?: PartialLibp2pCreateOptions) => Promise<Libp2pExtended>;
|
|
23
27
|
//# sourceMappingURL=libp2p.d.ts.map
|
package/dist/src/libp2p.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAmB,KAAK,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EACN,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,iBAAiB,EAEtB,MAAM,QAAQ,CAAC;AAGhB,MAAM,MAAM,oBAAoB,GAAG;IAClC,MAAM,EAAE,SAAS,CAAC;IAClB,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,QAAQ,CAAC;CACnB,CAAC;AACF,MAAM,MAAM,cAAc,GAAG,MAAM,CAClC;IAAE,KAAK,EAAE,mBAAmB,CAAC;IAAC,QAAQ,EAAE,GAAG,CAAA;CAAE,GAAG,oBAAoB,CACpE,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG,aAAa,CAC9C,OAAO,CAAC,oBAAoB,GAAG;IAAE,KAAK,EAAE,mBAAmB,CAAC;IAAC,QAAQ,EAAE,GAAG,CAAA;CAAE,CAAC,CAC7E,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACrD,OAAO,CAAC,oBAAoB,GAAG;IAAE,KAAK,EAAE,mBAAmB,CAAC;IAAC,QAAQ,EAAE,GAAG,CAAA;CAAE,CAAC,CAC7E,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG,mBAAmB,GAAG;IACnE,QAAQ,EAAE,iBAAiB,CAAC,oBAAoB,CAAC,CAAC;CAClD,CAAC;AAEF,eAAO,MAAM,oBAAoB,UAC1B,0BAA0B,KAO9B,OAAO,CAAC,cAAc,CA0CxB,CAAC"}
|
package/dist/src/libp2p.js
CHANGED
|
@@ -1,53 +1,54 @@
|
|
|
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 { transports, relay, listen } from "./transports.js";
|
|
6
3
|
import { identify } from "@libp2p/identify";
|
|
7
|
-
import {
|
|
4
|
+
import { DirectBlock } from "@peerbit/blocks";
|
|
8
5
|
import { DefaultKeychain } from "@peerbit/keychain";
|
|
6
|
+
import { DirectSub } from "@peerbit/pubsub";
|
|
7
|
+
import { createLibp2p, } from "libp2p";
|
|
8
|
+
import { listen, relay, transports } from "./transports.js";
|
|
9
9
|
export const createLibp2pExtended = (opts = {
|
|
10
10
|
services: {
|
|
11
11
|
blocks: (c) => new DirectBlock(c),
|
|
12
12
|
pubsub: (c) => new DirectSub(c),
|
|
13
|
-
keychain: (
|
|
14
|
-
}
|
|
13
|
+
keychain: () => new DefaultKeychain(),
|
|
14
|
+
},
|
|
15
15
|
}) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (relayIdentify[key] === undefined) {
|
|
23
|
-
delete relayIdentify[key];
|
|
16
|
+
let extraServices = {};
|
|
17
|
+
if (!opts.services?.["relay"]) {
|
|
18
|
+
const relayComponent = relay();
|
|
19
|
+
if (relayComponent) {
|
|
20
|
+
// will be null in browser
|
|
21
|
+
extraServices["relay"] = relayComponent;
|
|
24
22
|
}
|
|
25
|
-
}
|
|
23
|
+
}
|
|
24
|
+
if (!opts.services?.["identify"]) {
|
|
25
|
+
extraServices["identify"] = identify();
|
|
26
|
+
}
|
|
26
27
|
return createLibp2p({
|
|
27
28
|
...opts,
|
|
28
29
|
connectionManager: {
|
|
29
30
|
minConnections: 0,
|
|
30
|
-
...opts.connectionManager
|
|
31
|
+
...opts.connectionManager,
|
|
31
32
|
},
|
|
32
33
|
addresses: {
|
|
33
34
|
listen: listen(),
|
|
34
|
-
...opts.addresses
|
|
35
|
+
...opts.addresses,
|
|
35
36
|
},
|
|
36
37
|
transports: opts.transports || transports(),
|
|
37
38
|
connectionEncryption: opts.connectionEncryption || [noise()],
|
|
38
39
|
streamMuxers: opts.streamMuxers || [yamux()],
|
|
39
40
|
services: {
|
|
40
|
-
...relayIdentify,
|
|
41
41
|
pubsub: opts.services?.pubsub ||
|
|
42
42
|
((c) => new DirectSub(c, {
|
|
43
|
-
canRelayMessage: true
|
|
43
|
+
canRelayMessage: true,
|
|
44
44
|
// auto dial true
|
|
45
45
|
// auto prune true
|
|
46
46
|
})),
|
|
47
47
|
blocks: opts.services?.blocks || ((c) => new DirectBlock(c)),
|
|
48
48
|
keychain: opts.services?.keychain || ((c) => new DefaultKeychain()),
|
|
49
|
-
...opts.services
|
|
50
|
-
|
|
49
|
+
...opts.services,
|
|
50
|
+
...extraServices,
|
|
51
|
+
},
|
|
51
52
|
});
|
|
52
53
|
};
|
|
53
54
|
//# sourceMappingURL=libp2p.js.map
|
package/dist/src/libp2p.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,eAAe,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AC,KAAK,EAAE,CAAC;QAC5D,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,CAAC,KAAK,EAAE,CAAC;QAC5C,QAAQ,EAAE;YACT,MAAM,EACL,IAAI,CAAC,QAAQ,EAAE,MAAM;gBACrB,CAAC,CAAC,CAAC,EAAE,EAAE,CACN,IAAI,SAAS,CAAC,CAAC,EAAE;oBAChB,eAAe,EAAE,IAAI;oBACrB,iBAAiB;oBACjB,kBAAkB;iBAClB,CAAC,CAAC;YACL,MAAM,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;YAC5D,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,QAAQ,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,eAAe,EAAE,CAAC;YACnE,GAAG,IAAI,CAAC,QAAQ;YAChB,GAAG,aAAa;SAChB;KACD,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/src/peer.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type Multiaddr } from "@multiformats/multiaddr";
|
|
2
|
-
import { Ed25519Keypair } from "@peerbit/crypto";
|
|
3
|
-
import { Program, type Address, type ProgramClient, ProgramHandler } from "@peerbit/program";
|
|
4
1
|
import "@libp2p/peer-id";
|
|
5
|
-
import { type
|
|
6
|
-
import { type OpenOptions } from "@peerbit/program";
|
|
2
|
+
import { type Multiaddr } from "@multiformats/multiaddr";
|
|
7
3
|
import { type AnyStore } from "@peerbit/any-store";
|
|
8
|
-
import {
|
|
4
|
+
import { Ed25519Keypair } from "@peerbit/crypto";
|
|
5
|
+
import type { Indices } from "@peerbit/indexer-interface";
|
|
6
|
+
import { type Address, type ExtractArgs, type OpenOptions, type Program, type ProgramClient, ProgramHandler } from "@peerbit/program";
|
|
7
|
+
import { type Libp2pExtended, type PartialLibp2pCreateOptions } from "./libp2p.js";
|
|
9
8
|
export declare const logger: import("pino").Logger<never>;
|
|
10
9
|
export type OptionalCreateOptions = {
|
|
11
10
|
libp2pExternal?: boolean;
|
|
@@ -13,21 +12,24 @@ export type OptionalCreateOptions = {
|
|
|
13
12
|
export type CreateOptions = {
|
|
14
13
|
directory?: string;
|
|
15
14
|
storage: AnyStore;
|
|
15
|
+
indexer: Indices;
|
|
16
16
|
identity: Ed25519Keypair;
|
|
17
17
|
} & OptionalCreateOptions;
|
|
18
18
|
type Libp2pOptions = {
|
|
19
|
-
libp2p?: Libp2pExtended |
|
|
19
|
+
libp2p?: Libp2pExtended | PartialLibp2pCreateOptions;
|
|
20
20
|
};
|
|
21
21
|
type SimpleLibp2pOptions = {
|
|
22
22
|
relay?: boolean;
|
|
23
23
|
};
|
|
24
24
|
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
25
25
|
directory?: string;
|
|
26
|
+
indexer?: (directory?: string) => Promise<Indices> | Indices;
|
|
26
27
|
} & OptionalCreateOptions;
|
|
27
28
|
export declare class Peerbit implements ProgramClient {
|
|
28
29
|
_libp2p: Libp2pExtended;
|
|
29
30
|
directory?: string;
|
|
30
31
|
private _storage;
|
|
32
|
+
private _indexer;
|
|
31
33
|
private _libp2pExternal?;
|
|
32
34
|
private _identity;
|
|
33
35
|
private _handler;
|
|
@@ -58,6 +60,7 @@ export declare class Peerbit implements ProgramClient {
|
|
|
58
60
|
*/
|
|
59
61
|
open<S extends Program<ExtractArgs<S>>>(storeOrAddress: S | Address | string, options?: OpenOptions<S>): Promise<S>;
|
|
60
62
|
get storage(): AnyStore;
|
|
63
|
+
get indexer(): Indices;
|
|
61
64
|
}
|
|
62
65
|
export {};
|
|
63
66
|
//# sourceMappingURL=peer.d.ts.map
|
package/dist/src/peer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer.d.ts","sourceRoot":"","sources":["../../src/peer.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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BAAqB,CAAC;AAqB1B,qBAAa,OAAQ,YAAW,aAAa;IAC5C,OAAO,EAAE,cAAc,CAAC;IAExB,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,eAAe,CAAC,CAAkB;IAG1C,OAAO,CAAC,SAAS,CAAiB;IAClC,OAAO,CAAC,QAAQ,CAAiB;gBAErB,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,aAAa;WAuB7C,MAAM,CAAC,OAAO,GAAE,qBAA0B,GAAG,OAAO,CAAC,OAAO,CAAC;IAiH1E,IAAI,MAAM,IAAI,cAAc,CAE3B;IAED,IAAI,QAAQ,IAAI,cAAc,CAE7B;IAED,IAAI,MAAM,uCAET;IAED,IAAI,QAAQ;;;mDAEX;IAED,IAAI,OAAO,IAAI,cAAc,CAE5B;IAED,aAAa,IAAI,SAAS,EAAE;IAG5B;;OAEG;IACG,IAAI,CACT,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,GAAG,aAAa,GACvD,OAAO,CAAC,OAAO,CAAC;IAoBb,KAAK;IASL,IAAI;IAYJ,SAAS;IA0Bf;;;;;;OAMG;IAEG,IAAI,CAAC,CAAC,SAAS,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAC3C,cAAc,EAAE,CAAC,GAAG,OAAO,GAAG,MAAM,EACpC,OAAO,GAAE,WAAW,CAAC,CAAC,CAAM,GAC1B,OAAO,CAAC,CAAC,CAAC;IAMb,IAAI,OAAO,aAEV;IAED,IAAI,OAAO,YAEV;CACD"}
|
package/dist/src/peer.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "@libp2p/peer-id";
|
|
2
|
+
import { isMultiaddr, multiaddr, } from "@multiformats/multiaddr";
|
|
3
|
+
import { createStore } from "@peerbit/any-store";
|
|
4
|
+
import { DirectBlock } from "@peerbit/blocks";
|
|
2
5
|
import { Ed25519Keypair, Ed25519PublicKey } from "@peerbit/crypto";
|
|
3
|
-
import {
|
|
6
|
+
import { create as createSQLiteIndexer } from "@peerbit/indexer-sqlite3";
|
|
7
|
+
import { DefaultKeychain } from "@peerbit/keychain";
|
|
8
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
9
|
+
import { ProgramHandler, } from "@peerbit/program";
|
|
4
10
|
import { DirectSub } from "@peerbit/pubsub";
|
|
5
|
-
import sodium from "libsodium-wrappers";
|
|
6
|
-
import path from "path-browserify";
|
|
7
11
|
import { waitFor } from "@peerbit/time";
|
|
8
|
-
import "@libp2p/peer-id";
|
|
9
|
-
import { createLibp2pExtended } from "./libp2p.js";
|
|
10
|
-
import { DirectBlock } from "@peerbit/blocks";
|
|
11
12
|
import { LevelDatastore } from "datastore-level";
|
|
12
|
-
import
|
|
13
|
-
import
|
|
13
|
+
import sodium from "libsodium-wrappers";
|
|
14
|
+
import path from "path-browserify";
|
|
14
15
|
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
15
|
-
import {
|
|
16
|
-
import { DefaultKeychain } from "@peerbit/keychain";
|
|
17
|
-
import {} from "@peerbit/program";
|
|
16
|
+
import { createLibp2pExtended, } from "./libp2p.js";
|
|
18
17
|
export const logger = loggerFn({ module: "client" });
|
|
19
18
|
const isLibp2pInstance = (libp2p) => !!libp2p.getMultiaddrs;
|
|
20
19
|
const createCache = async (directory, options) => {
|
|
@@ -32,6 +31,7 @@ export class Peerbit {
|
|
|
32
31
|
_libp2p;
|
|
33
32
|
directory;
|
|
34
33
|
_storage;
|
|
34
|
+
_indexer;
|
|
35
35
|
_libp2pExternal = false;
|
|
36
36
|
// Libp2p peerid in Identity form
|
|
37
37
|
_identity;
|
|
@@ -52,6 +52,7 @@ export class Peerbit {
|
|
|
52
52
|
this.directory = options.directory;
|
|
53
53
|
this._storage = options.storage;
|
|
54
54
|
this._libp2pExternal = options.libp2pExternal;
|
|
55
|
+
this._indexer = options.indexer;
|
|
55
56
|
}
|
|
56
57
|
static async create(options = {}) {
|
|
57
58
|
await sodium.ready; // Some of the modules depends on sodium to be readyy
|
|
@@ -61,6 +62,10 @@ export class Peerbit {
|
|
|
61
62
|
const directory = options.directory;
|
|
62
63
|
const hasDir = directory != null;
|
|
63
64
|
const storage = await createCache(directory != null ? path.join(directory, "/cache") : undefined);
|
|
65
|
+
const indexerFn = options.indexer || createSQLiteIndexer;
|
|
66
|
+
const indexer = directory != null
|
|
67
|
+
? await indexerFn(path.join(directory, "/index"))
|
|
68
|
+
: await indexerFn();
|
|
64
69
|
const blocksDirectory = hasDir
|
|
65
70
|
? path.join(directory, "/blocks").toString()
|
|
66
71
|
: undefined;
|
|
@@ -76,8 +81,10 @@ export class Peerbit {
|
|
|
76
81
|
const libp2pExternal = libp2pExtended && isLibp2pInstance(libp2pExtended);
|
|
77
82
|
if (!libp2pExternal) {
|
|
78
83
|
const extendedOptions = libp2pExtended;
|
|
84
|
+
const store = createStore(keychainDirectory);
|
|
85
|
+
await store.open();
|
|
79
86
|
const keychain = new DefaultKeychain({
|
|
80
|
-
store
|
|
87
|
+
store,
|
|
81
88
|
});
|
|
82
89
|
const peerId = extendedOptions?.peerId ||
|
|
83
90
|
(await (await keychain.exportById(SELF_IDENTITY_KEY_ID, Ed25519Keypair))?.toPeerId());
|
|
@@ -88,12 +95,12 @@ export class Peerbit {
|
|
|
88
95
|
keychain: (c) => keychain,
|
|
89
96
|
blocks: (c) => new DirectBlock(c, {
|
|
90
97
|
canRelayMessage: asRelay,
|
|
91
|
-
directory: blocksDirectory
|
|
98
|
+
directory: blocksDirectory,
|
|
92
99
|
}),
|
|
93
100
|
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
94
|
-
...extendedOptions?.services
|
|
95
|
-
},
|
|
96
|
-
datastore
|
|
101
|
+
...extendedOptions?.services,
|
|
102
|
+
}, // TODO types are funky
|
|
103
|
+
datastore,
|
|
97
104
|
});
|
|
98
105
|
}
|
|
99
106
|
if (datastore) {
|
|
@@ -115,11 +122,11 @@ export class Peerbit {
|
|
|
115
122
|
try {
|
|
116
123
|
await libp2pExtended.services.keychain.import({
|
|
117
124
|
keypair: identity,
|
|
118
|
-
id: SELF_IDENTITY_KEY_ID
|
|
125
|
+
id: SELF_IDENTITY_KEY_ID,
|
|
119
126
|
});
|
|
120
127
|
}
|
|
121
128
|
catch (error) {
|
|
122
|
-
if (error.code
|
|
129
|
+
if (error.code === "ERR_KEY_ALREADY_EXISTS") {
|
|
123
130
|
// Do nothing
|
|
124
131
|
}
|
|
125
132
|
else {
|
|
@@ -128,9 +135,10 @@ export class Peerbit {
|
|
|
128
135
|
}
|
|
129
136
|
const peer = new Peerbit(libp2pExtended, {
|
|
130
137
|
directory,
|
|
131
|
-
storage
|
|
138
|
+
storage,
|
|
132
139
|
libp2pExternal,
|
|
133
|
-
identity
|
|
140
|
+
identity,
|
|
141
|
+
indexer,
|
|
134
142
|
});
|
|
135
143
|
return peer;
|
|
136
144
|
}
|
|
@@ -156,7 +164,7 @@ export class Peerbit {
|
|
|
156
164
|
* Dial a peer with an Ed25519 peerId
|
|
157
165
|
*/
|
|
158
166
|
async dial(address) {
|
|
159
|
-
const maddress = typeof address
|
|
167
|
+
const maddress = typeof address === "string"
|
|
160
168
|
? multiaddr(address)
|
|
161
169
|
: isMultiaddr(address) || Array.isArray(address)
|
|
162
170
|
? address
|
|
@@ -169,6 +177,7 @@ export class Peerbit {
|
|
|
169
177
|
}
|
|
170
178
|
async start() {
|
|
171
179
|
await this._storage.open();
|
|
180
|
+
await this.indexer.start();
|
|
172
181
|
if (this.libp2p.status === "stopped" || this.libp2p.status === "stopping") {
|
|
173
182
|
this._libp2pExternal = false; // this means we will also close libp2p client on close
|
|
174
183
|
return this.libp2p.start();
|
|
@@ -177,6 +186,7 @@ export class Peerbit {
|
|
|
177
186
|
async stop() {
|
|
178
187
|
await this._handler?.stop();
|
|
179
188
|
await this._storage.close();
|
|
189
|
+
await this.indexer.stop();
|
|
180
190
|
// Close libp2p (after above)
|
|
181
191
|
if (!this._libp2pExternal) {
|
|
182
192
|
// only close it if we created it
|
|
@@ -218,5 +228,8 @@ export class Peerbit {
|
|
|
218
228
|
get storage() {
|
|
219
229
|
return this._storage;
|
|
220
230
|
}
|
|
231
|
+
get indexer() {
|
|
232
|
+
return this._indexer;
|
|
233
|
+
}
|
|
221
234
|
}
|
|
222
235
|
//# sourceMappingURL=peer.js.map
|
package/dist/src/peer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer.js","sourceRoot":"","sources":["../../src/peer.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"peer.js","sourceRoot":"","sources":["../../src/peer.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AACzB,OAAO,EAEN,WAAW,EACX,SAAS,GACT,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAiB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnE,OAAO,EAAE,MAAM,IAAI,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAMN,cAAc,GACd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,iBAAiB,CAAC;AACnC,OAAO,EAAE,yBAAyB,EAAE,MAAM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B,KAAK,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;YAC7C,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;gBACnC,IAAI,GAAG,IAAI,CAAC;YACb,CAAC;iBAAM,CAAC;gBACP,MAAM,CAAC,IAAI,CACV,uCAAuC;oBACtC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;oBAC5B,YAAY;oBACZ,MAAM,CAAC,MAAM,CACd,CAAC;YACH,CAAC;QACF,CAAC;QACD,IAAI,CAAC,IAAI,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IAEH,KAAK,CAAC,IAAI,CACT,cAAoC,EACpC,UAA0B,EAAE;QAE5B,OAAO,CACN,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,QAAQ,GAAG,IAAI,cAAc,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CACvE,CAAC,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IACjC,CAAC;IAED,IAAI,OAAO;QACV,OAAO,IAAI,CAAC,QAAQ,CAAC;IACtB,CAAC;IAED,IAAI,OAAO;QACV,OAAO,IAAI,CAAC,QAAQ,CAAC;IACtB,CAAC;CACD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports.browser.d.ts","sourceRoot":"","sources":["../../src/transports.browser.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"transports.browser.d.ts","sourceRoot":"","sources":["../../src/transports.browser.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,UAAU,8SAOtB,CAAC;AAEF,eAAO,MAAM,KAAK,QAAsB,GAAG,CAAC;AAE5C,eAAO,MAAM,MAAM,EAAE,MAAM,MAAM,EAAE,GAAG,SAA6B,CAAC"}
|
|
@@ -1,13 +1,14 @@
|
|
|
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
|
export const transports = () => [
|
|
6
6
|
webSockets({ filter: all }),
|
|
7
7
|
circuitRelayTransport({
|
|
8
|
-
discoverRelays: 1
|
|
8
|
+
discoverRelays: 1,
|
|
9
|
+
reservationCompletionTimeout: 5000,
|
|
9
10
|
}),
|
|
10
|
-
webRTC({})
|
|
11
|
+
webRTC({}),
|
|
11
12
|
];
|
|
12
13
|
export const relay = () => undefined;
|
|
13
14
|
export const listen = () => ["/webrtc"];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports.browser.js","sourceRoot":"","sources":["../../src/transports.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"transports.browser.js","sourceRoot":"","sources":["../../src/transports.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AAEjD,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3B,qBAAqB,CAAC;QACrB,cAAc,EAAE,CAAC;QACjB,4BAA4B,EAAE,IAAI;KAClC,CAAC;IACF,MAAM,CAAC,EAAE,CAAC;CACV,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,SAAgB,CAAC;AAE5C,MAAM,CAAC,MAAM,MAAM,GAA+B,GAAG,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports.d.ts","sourceRoot":"","sources":["../../src/transports.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transports.d.ts","sourceRoot":"","sources":["../../src/transports.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,UAAU,yPAMtB,CAAC;AACF,eAAO,MAAM,KAAK,+JAIf,CAAC;AAEJ,eAAO,MAAM,MAAM,EAAE,MAAM,MAAM,EAAE,GAAG,SAGrC,CAAC"}
|
package/dist/src/transports.js
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
1
|
+
import { circuitRelayServer, circuitRelayTransport, } from "@libp2p/circuit-relay-v2";
|
|
3
2
|
import { tcp } from "@libp2p/tcp";
|
|
4
|
-
import {
|
|
3
|
+
import { webSockets } from "@libp2p/websockets";
|
|
5
4
|
import { all } from "@libp2p/websockets/filters";
|
|
6
5
|
export const transports = () => [
|
|
7
6
|
webSockets({ filter: all }),
|
|
8
|
-
circuitRelayTransport(
|
|
9
|
-
|
|
7
|
+
circuitRelayTransport({
|
|
8
|
+
reservationCompletionTimeout: 5000,
|
|
9
|
+
}),
|
|
10
|
+
tcp(),
|
|
10
11
|
];
|
|
11
|
-
export const relay = () =>
|
|
12
|
+
export const relay = () =>
|
|
13
|
+
// applyDefaultLimit: false because of https://github.com/libp2p/js-libp2p/issues/2622
|
|
14
|
+
circuitRelayServer({
|
|
15
|
+
reservations: { applyDefaultLimit: false, maxReservations: 1000 },
|
|
16
|
+
});
|
|
12
17
|
export const listen = () => [
|
|
13
18
|
"/ip4/127.0.0.1/tcp/0",
|
|
14
|
-
"/ip4/127.0.0.1/tcp/0/ws"
|
|
19
|
+
"/ip4/127.0.0.1/tcp/0/ws",
|
|
15
20
|
];
|
|
16
21
|
//# sourceMappingURL=transports.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports.js","sourceRoot":"","sources":["../../src/transports.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"transports.js","sourceRoot":"","sources":["../../src/transports.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,kBAAkB,EAClB,qBAAqB,GACrB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AAEjD,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3B,qBAAqB,CAAC;QACrB,4BAA4B,EAAE,IAAI;KAClC,CAAC;IACF,GAAG,EAAE;CACL,CAAC;AACF,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,EAAE;AACzB,sFAAsF;AACtF,kBAAkB,CAAC;IAClB,YAAY,EAAE,EAAE,iBAAiB,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE;CACjE,CAAC,CAAC;AAEJ,MAAM,CAAC,MAAM,MAAM,GAA+B,GAAG,EAAE,CAAC;IACvD,sBAAsB;IACtB,yBAAyB;CACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peerbit",
|
|
3
|
-
"version": "4.0.7-
|
|
3
|
+
"version": "4.0.7-cccc078",
|
|
4
4
|
"description": "Peerbit client",
|
|
5
5
|
"author": "dao.xyz",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"eslintConfig": {
|
|
40
|
-
"extends": "
|
|
40
|
+
"extends": "peerbit",
|
|
41
41
|
"parserOptions": {
|
|
42
42
|
"project": true,
|
|
43
43
|
"sourceType": "module"
|
|
@@ -62,21 +62,23 @@
|
|
|
62
62
|
"./dist/src/transports.js": "./dist/src/transports.browser.js"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@dao-xyz/borsh": "^5.2.
|
|
66
|
-
"@peerbit/any-store": "2.0.2-
|
|
67
|
-
"@peerbit/crypto": "2.2.0-
|
|
68
|
-
"@peerbit/logger": "1.0.2-
|
|
69
|
-
"@peerbit/program": "4.0.7-
|
|
70
|
-
"@peerbit/pubsub": "3.0.5-
|
|
71
|
-
"@peerbit/blocks": "2.0.32-
|
|
65
|
+
"@dao-xyz/borsh": "^5.2.3",
|
|
66
|
+
"@peerbit/any-store": "2.0.2-cccc078",
|
|
67
|
+
"@peerbit/crypto": "2.2.0-cccc078",
|
|
68
|
+
"@peerbit/logger": "1.0.2-cccc078",
|
|
69
|
+
"@peerbit/program": "4.0.7-cccc078",
|
|
70
|
+
"@peerbit/pubsub": "3.0.5-cccc078",
|
|
71
|
+
"@peerbit/blocks": "2.0.32-cccc078",
|
|
72
|
+
"@peerbit/indexer-simple": "0.0.1-cccc078",
|
|
73
|
+
"@peerbit/indexer-sqlite3": "0.0.1-cccc078",
|
|
72
74
|
"datastore-level": "^10.1.5",
|
|
73
75
|
"@dao-xyz/libp2p-noise": "^14.1.2",
|
|
74
76
|
"@chainsafe/libp2p-yamux": "^6.0.2",
|
|
75
|
-
"@libp2p/webrtc": "^4.
|
|
76
|
-
"@libp2p/websockets": "^8.
|
|
77
|
-
"@libp2p/identify": "^1.
|
|
78
|
-
"@libp2p/circuit-relay-v2": "^1.
|
|
79
|
-
"@libp2p/tcp": "^9.
|
|
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",
|
|
80
82
|
"level": "^8.0.0",
|
|
81
83
|
"memory-level": "^1.0.0",
|
|
82
84
|
"path-browserify": "^1.0.1"
|
|
@@ -89,7 +91,8 @@
|
|
|
89
91
|
"scripts": {
|
|
90
92
|
"clean": "aegir clean",
|
|
91
93
|
"build": "aegir build --no-bundle",
|
|
92
|
-
"test": "aegir test -t node"
|
|
94
|
+
"test": "aegir test -t node",
|
|
95
|
+
"lint": "aegir lint"
|
|
93
96
|
},
|
|
94
97
|
"localMaintainers": [
|
|
95
98
|
"dao.xyz"
|
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 { transports, relay, listen } from "./transports.js";
|
|
6
|
-
import { identify } from "@libp2p/identify";
|
|
7
3
|
import type { CircuitRelayService } from "@libp2p/circuit-relay-v2";
|
|
8
|
-
import {
|
|
4
|
+
import { identify } from "@libp2p/identify";
|
|
5
|
+
import { DirectBlock } from "@peerbit/blocks";
|
|
9
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,31 +1,37 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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";
|
|
3
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";
|
|
4
14
|
import {
|
|
5
|
-
Program,
|
|
6
15
|
type Address,
|
|
16
|
+
type ExtractArgs,
|
|
17
|
+
type OpenOptions,
|
|
18
|
+
type Program,
|
|
7
19
|
type ProgramClient,
|
|
8
|
-
ProgramHandler
|
|
20
|
+
ProgramHandler,
|
|
9
21
|
} from "@peerbit/program";
|
|
10
22
|
import { DirectSub } from "@peerbit/pubsub";
|
|
23
|
+
import { waitFor } from "@peerbit/time";
|
|
24
|
+
import { LevelDatastore } from "datastore-level";
|
|
25
|
+
import type { Libp2p } from "libp2p";
|
|
11
26
|
import sodium from "libsodium-wrappers";
|
|
12
27
|
import path from "path-browserify";
|
|
13
|
-
import {
|
|
14
|
-
import "@libp2p/peer-id";
|
|
15
|
-
|
|
28
|
+
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
16
29
|
import {
|
|
17
|
-
|
|
30
|
+
type Libp2pCreateOptions as ClientCreateOptions,
|
|
18
31
|
type Libp2pExtended,
|
|
19
|
-
type
|
|
32
|
+
type PartialLibp2pCreateOptions,
|
|
33
|
+
createLibp2pExtended,
|
|
20
34
|
} from "./libp2p.js";
|
|
21
|
-
import { DirectBlock } from "@peerbit/blocks";
|
|
22
|
-
import { LevelDatastore } from "datastore-level";
|
|
23
|
-
import { logger as loggerFn } from "@peerbit/logger";
|
|
24
|
-
import { type OpenOptions } from "@peerbit/program";
|
|
25
|
-
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
26
|
-
import { type AnyStore, createStore } from "@peerbit/any-store";
|
|
27
|
-
import { DefaultKeychain } from "@peerbit/keychain";
|
|
28
|
-
import { type ExtractArgs } from "@peerbit/program";
|
|
29
35
|
|
|
30
36
|
export const logger = loggerFn({ module: "client" });
|
|
31
37
|
|
|
@@ -35,12 +41,14 @@ export type OptionalCreateOptions = {
|
|
|
35
41
|
export type CreateOptions = {
|
|
36
42
|
directory?: string;
|
|
37
43
|
storage: AnyStore;
|
|
44
|
+
indexer: Indices;
|
|
38
45
|
identity: Ed25519Keypair;
|
|
39
46
|
} & OptionalCreateOptions;
|
|
40
|
-
type Libp2pOptions = { libp2p?: Libp2pExtended |
|
|
47
|
+
type Libp2pOptions = { libp2p?: Libp2pExtended | PartialLibp2pCreateOptions };
|
|
41
48
|
type SimpleLibp2pOptions = { relay?: boolean };
|
|
42
49
|
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
43
50
|
directory?: string;
|
|
51
|
+
indexer?: (directory?: string) => Promise<Indices> | Indices;
|
|
44
52
|
} & OptionalCreateOptions;
|
|
45
53
|
|
|
46
54
|
const isLibp2pInstance = (libp2p: Libp2pExtended | ClientCreateOptions) =>
|
|
@@ -48,7 +56,7 @@ const isLibp2pInstance = (libp2p: Libp2pExtended | ClientCreateOptions) =>
|
|
|
48
56
|
|
|
49
57
|
const createCache = async (
|
|
50
58
|
directory: string | undefined,
|
|
51
|
-
options?: { reset?: boolean }
|
|
59
|
+
options?: { reset?: boolean },
|
|
52
60
|
) => {
|
|
53
61
|
const cache = createStore(directory);
|
|
54
62
|
|
|
@@ -68,6 +76,7 @@ export class Peerbit implements ProgramClient {
|
|
|
68
76
|
directory?: string;
|
|
69
77
|
|
|
70
78
|
private _storage: AnyStore;
|
|
79
|
+
private _indexer: Indices;
|
|
71
80
|
private _libp2pExternal?: boolean = false;
|
|
72
81
|
|
|
73
82
|
// Libp2p peerid in Identity form
|
|
@@ -82,7 +91,7 @@ export class Peerbit implements ProgramClient {
|
|
|
82
91
|
if (this.libp2p.peerId.type !== "Ed25519") {
|
|
83
92
|
throw new Error(
|
|
84
93
|
"Unsupported id type, expecting Ed25519 but got " +
|
|
85
|
-
|
|
94
|
+
this.libp2p.peerId.type,
|
|
86
95
|
);
|
|
87
96
|
}
|
|
88
97
|
|
|
@@ -94,6 +103,7 @@ export class Peerbit implements ProgramClient {
|
|
|
94
103
|
this.directory = options.directory;
|
|
95
104
|
this._storage = options.storage;
|
|
96
105
|
this._libp2pExternal = options.libp2pExternal;
|
|
106
|
+
this._indexer = options.indexer;
|
|
97
107
|
}
|
|
98
108
|
|
|
99
109
|
static async create(options: CreateInstanceOptions = {}): Promise<Peerbit> {
|
|
@@ -101,15 +111,22 @@ export class Peerbit implements ProgramClient {
|
|
|
101
111
|
|
|
102
112
|
let libp2pExtended: Libp2pExtended | undefined = (options as Libp2pOptions)
|
|
103
113
|
.libp2p as Libp2pExtended;
|
|
114
|
+
|
|
104
115
|
const asRelay = (options as SimpleLibp2pOptions).relay;
|
|
105
116
|
|
|
106
117
|
const directory = options.directory;
|
|
107
118
|
const hasDir = directory != null;
|
|
108
119
|
|
|
109
120
|
const storage = await createCache(
|
|
110
|
-
directory != null ? path.join(directory, "/cache") : undefined
|
|
121
|
+
directory != null ? path.join(directory, "/cache") : undefined,
|
|
111
122
|
);
|
|
112
123
|
|
|
124
|
+
const indexerFn = options.indexer || createSQLiteIndexer;
|
|
125
|
+
const indexer =
|
|
126
|
+
directory != null
|
|
127
|
+
? await indexerFn(path.join(directory, "/index"))
|
|
128
|
+
: await indexerFn();
|
|
129
|
+
|
|
113
130
|
const blocksDirectory = hasDir
|
|
114
131
|
? path.join(directory, "/blocks").toString()
|
|
115
132
|
: undefined;
|
|
@@ -119,9 +136,7 @@ export class Peerbit implements ProgramClient {
|
|
|
119
136
|
: undefined;
|
|
120
137
|
|
|
121
138
|
const datastore = hasDir
|
|
122
|
-
? new LevelDatastore(
|
|
123
|
-
path.join(directory, "/libp2p").toString()
|
|
124
|
-
)
|
|
139
|
+
? new LevelDatastore(path.join(directory, "/libp2p").toString())
|
|
125
140
|
: undefined;
|
|
126
141
|
|
|
127
142
|
if (datastore) {
|
|
@@ -132,8 +147,11 @@ export class Peerbit implements ProgramClient {
|
|
|
132
147
|
if (!libp2pExternal) {
|
|
133
148
|
const extendedOptions: ClientCreateOptions | undefined =
|
|
134
149
|
libp2pExtended as any as ClientCreateOptions;
|
|
150
|
+
const store = createStore(keychainDirectory);
|
|
151
|
+
await store.open();
|
|
152
|
+
|
|
135
153
|
const keychain = new DefaultKeychain({
|
|
136
|
-
store
|
|
154
|
+
store,
|
|
137
155
|
});
|
|
138
156
|
const peerId =
|
|
139
157
|
extendedOptions?.peerId ||
|
|
@@ -144,16 +162,16 @@ export class Peerbit implements ProgramClient {
|
|
|
144
162
|
...extendedOptions,
|
|
145
163
|
peerId,
|
|
146
164
|
services: {
|
|
147
|
-
keychain: (c) => keychain,
|
|
148
|
-
blocks: (c) =>
|
|
165
|
+
keychain: (c: any) => keychain,
|
|
166
|
+
blocks: (c: any) =>
|
|
149
167
|
new DirectBlock(c, {
|
|
150
168
|
canRelayMessage: asRelay,
|
|
151
|
-
directory: blocksDirectory
|
|
169
|
+
directory: blocksDirectory,
|
|
152
170
|
}),
|
|
153
|
-
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
154
|
-
...extendedOptions?.services
|
|
155
|
-
},
|
|
156
|
-
datastore
|
|
171
|
+
pubsub: (c: any) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
172
|
+
...extendedOptions?.services,
|
|
173
|
+
} as any, // TODO types are funky
|
|
174
|
+
datastore,
|
|
157
175
|
});
|
|
158
176
|
}
|
|
159
177
|
if (datastore) {
|
|
@@ -174,7 +192,7 @@ export class Peerbit implements ProgramClient {
|
|
|
174
192
|
if (libp2pExtended.peerId.type !== "Ed25519") {
|
|
175
193
|
throw new Error(
|
|
176
194
|
"Unsupported id type, expecting Ed25519 but got " +
|
|
177
|
-
|
|
195
|
+
libp2pExtended.peerId.type,
|
|
178
196
|
);
|
|
179
197
|
}
|
|
180
198
|
|
|
@@ -182,10 +200,10 @@ export class Peerbit implements ProgramClient {
|
|
|
182
200
|
try {
|
|
183
201
|
await libp2pExtended.services.keychain.import({
|
|
184
202
|
keypair: identity,
|
|
185
|
-
id: SELF_IDENTITY_KEY_ID
|
|
203
|
+
id: SELF_IDENTITY_KEY_ID,
|
|
186
204
|
});
|
|
187
205
|
} catch (error: any) {
|
|
188
|
-
if (error.code
|
|
206
|
+
if (error.code === "ERR_KEY_ALREADY_EXISTS") {
|
|
189
207
|
// Do nothing
|
|
190
208
|
} else {
|
|
191
209
|
throw error;
|
|
@@ -194,9 +212,10 @@ export class Peerbit implements ProgramClient {
|
|
|
194
212
|
|
|
195
213
|
const peer = new Peerbit(libp2pExtended, {
|
|
196
214
|
directory,
|
|
197
|
-
storage
|
|
215
|
+
storage,
|
|
198
216
|
libp2pExternal,
|
|
199
|
-
identity
|
|
217
|
+
identity,
|
|
218
|
+
indexer,
|
|
200
219
|
});
|
|
201
220
|
return peer;
|
|
202
221
|
}
|
|
@@ -227,10 +246,10 @@ export class Peerbit implements ProgramClient {
|
|
|
227
246
|
* Dial a peer with an Ed25519 peerId
|
|
228
247
|
*/
|
|
229
248
|
async dial(
|
|
230
|
-
address: string | Multiaddr | Multiaddr[] | ProgramClient
|
|
249
|
+
address: string | Multiaddr | Multiaddr[] | ProgramClient,
|
|
231
250
|
): Promise<boolean> {
|
|
232
251
|
const maddress =
|
|
233
|
-
typeof address
|
|
252
|
+
typeof address === "string"
|
|
234
253
|
? multiaddr(address)
|
|
235
254
|
: isMultiaddr(address) || Array.isArray(address)
|
|
236
255
|
? address
|
|
@@ -243,13 +262,14 @@ export class Peerbit implements ProgramClient {
|
|
|
243
262
|
(await waitFor(
|
|
244
263
|
() =>
|
|
245
264
|
this.libp2p.services.pubsub.peers.has(publicKey.hashcode()) &&
|
|
246
|
-
this.libp2p.services.blocks.peers.has(publicKey.hashcode())
|
|
265
|
+
this.libp2p.services.blocks.peers.has(publicKey.hashcode()),
|
|
247
266
|
)) || false
|
|
248
267
|
);
|
|
249
268
|
}
|
|
250
269
|
|
|
251
270
|
async start() {
|
|
252
271
|
await this._storage.open();
|
|
272
|
+
await this.indexer.start();
|
|
253
273
|
|
|
254
274
|
if (this.libp2p.status === "stopped" || this.libp2p.status === "stopping") {
|
|
255
275
|
this._libp2pExternal = false; // this means we will also close libp2p client on close
|
|
@@ -259,6 +279,7 @@ export class Peerbit implements ProgramClient {
|
|
|
259
279
|
async stop() {
|
|
260
280
|
await this._handler?.stop();
|
|
261
281
|
await this._storage.close();
|
|
282
|
+
await this.indexer.stop();
|
|
262
283
|
|
|
263
284
|
// Close libp2p (after above)
|
|
264
285
|
if (!this._libp2pExternal) {
|
|
@@ -273,7 +294,7 @@ export class Peerbit implements ProgramClient {
|
|
|
273
294
|
throw new Error("Failed to find any addresses to dial");
|
|
274
295
|
}
|
|
275
296
|
const settled = await Promise.allSettled(
|
|
276
|
-
addresses.map((x) => this.dial(x))
|
|
297
|
+
addresses.map((x) => this.dial(x)),
|
|
277
298
|
);
|
|
278
299
|
let once = false;
|
|
279
300
|
for (const [i, result] of settled.entries()) {
|
|
@@ -282,9 +303,9 @@ export class Peerbit implements ProgramClient {
|
|
|
282
303
|
} else {
|
|
283
304
|
logger.warn(
|
|
284
305
|
"Failed to dial bootstrap address(s): " +
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
306
|
+
JSON.stringify(addresses[i]) +
|
|
307
|
+
". Reason: " +
|
|
308
|
+
result.reason,
|
|
288
309
|
);
|
|
289
310
|
}
|
|
290
311
|
}
|
|
@@ -303,7 +324,7 @@ export class Peerbit implements ProgramClient {
|
|
|
303
324
|
|
|
304
325
|
async open<S extends Program<ExtractArgs<S>>>(
|
|
305
326
|
storeOrAddress: S | Address | string,
|
|
306
|
-
options: OpenOptions<S> = {}
|
|
327
|
+
options: OpenOptions<S> = {},
|
|
307
328
|
): Promise<S> {
|
|
308
329
|
return (
|
|
309
330
|
this._handler || (this._handler = new ProgramHandler({ client: this }))
|
|
@@ -313,4 +334,8 @@ export class Peerbit implements ProgramClient {
|
|
|
313
334
|
get storage() {
|
|
314
335
|
return this._storage;
|
|
315
336
|
}
|
|
337
|
+
|
|
338
|
+
get indexer() {
|
|
339
|
+
return this._indexer;
|
|
340
|
+
}
|
|
316
341
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
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
15
|
export const relay = () => undefined as any;
|
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
|
];
|