@libp2p/daemon-server 0.0.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/dht.d.ts +1 -1
- package/dist/src/dht.d.ts.map +1 -1
- package/dist/src/dht.js +19 -18
- package/dist/src/dht.js.map +1 -1
- package/dist/src/index.d.ts +10 -20
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +126 -120
- package/dist/src/index.js.map +1 -1
- package/dist/src/pubsub.d.ts +1 -1
- package/dist/src/pubsub.d.ts.map +1 -1
- package/dist/src/pubsub.js +43 -22
- package/dist/src/pubsub.js.map +1 -1
- package/dist/src/responses.d.ts.map +1 -1
- package/dist/src/responses.js.map +1 -1
- package/package.json +13 -25
- package/src/dht.ts +20 -20
- package/src/index.ts +160 -149
- package/src/pubsub.ts +45 -27
- package/src/responses.ts +1 -1
- package/dist/src/client.d.ts +0 -26
- package/dist/src/client.d.ts.map +0 -1
- package/dist/src/client.js +0 -43
- package/dist/src/client.js.map +0 -1
- package/dist/src/stream-handler.d.ts +0 -28
- package/dist/src/stream-handler.d.ts.map +0 -1
- package/dist/src/stream-handler.js +0 -47
- package/dist/src/stream-handler.js.map +0 -1
- package/dist/src/util/index.d.ts +0 -13
- package/dist/src/util/index.d.ts.map +0 -1
- package/dist/src/util/index.js +0 -26
- package/dist/src/util/index.js.map +0 -1
- package/src/client.ts +0 -56
- package/src/stream-handler.ts +0 -65
- package/src/util/index.ts +0 -30
package/dist/src/dht.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface DHTOperationsInit {
|
|
|
5
5
|
dht: DualDHT;
|
|
6
6
|
}
|
|
7
7
|
export declare class DHTOperations {
|
|
8
|
-
private dht;
|
|
8
|
+
private readonly dht;
|
|
9
9
|
constructor(init: DHTOperationsInit);
|
|
10
10
|
provide(cid: CID): AsyncGenerator<Uint8Array, void, unknown>;
|
|
11
11
|
getClosestPeers(key: Uint8Array): AsyncGenerator<Uint8Array, void, undefined>;
|
package/dist/src/dht.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dht.d.ts","sourceRoot":"","sources":["../../src/dht.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AACrD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"dht.d.ts","sourceRoot":"","sources":["../../src/dht.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AACrD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAM3C,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,OAAO,CAAA;CACb;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;gBAEhB,IAAI,EAAE,iBAAiB;IAM5B,OAAO,CAAE,GAAG,EAAE,GAAG;IAUjB,eAAe,CAAE,GAAG,EAAE,UAAU;IAqBhC,YAAY,CAAE,MAAM,EAAE,MAAM;IAI5B,QAAQ,CAAE,GAAG,EAAE,UAAU;IAkBzB,QAAQ,CAAE,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU;IAW5C,QAAQ,CAAE,MAAM,EAAE,MAAM;IAuBxB,aAAa,CAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM;CAsC/C"}
|
package/dist/src/dht.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
/* eslint max-depth: ["error", 6] */
|
|
2
|
-
import { DHTResponse
|
|
2
|
+
import { DHTResponse } from '@libp2p/daemon-protocol';
|
|
3
3
|
import { ErrorResponse, OkResponse } from './responses.js';
|
|
4
4
|
import drain from 'it-drain';
|
|
5
|
+
import { logger } from '@libp2p/logger';
|
|
6
|
+
const log = logger('libp2p:daemon-server:dht');
|
|
5
7
|
export class DHTOperations {
|
|
6
8
|
constructor(init) {
|
|
7
9
|
const { dht } = init;
|
|
8
10
|
this.dht = dht;
|
|
9
11
|
}
|
|
10
12
|
async *provide(cid) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
try {
|
|
14
|
+
await drain(this.dht.provide(cid));
|
|
15
|
+
yield OkResponse();
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
log.error(err);
|
|
19
|
+
yield ErrorResponse(err);
|
|
20
|
+
}
|
|
13
21
|
}
|
|
14
22
|
async *getClosestPeers(key) {
|
|
15
23
|
yield OkResponse({
|
|
@@ -31,20 +39,10 @@ export class DHTOperations {
|
|
|
31
39
|
}
|
|
32
40
|
async *getPublicKey(peerId) {
|
|
33
41
|
yield ErrorResponse(new Error('FIX ME: not implemented'));
|
|
34
|
-
/*
|
|
35
|
-
const pubKey = await this.dht.getPublicKey(peerId)
|
|
36
|
-
|
|
37
|
-
yield OkResponse({
|
|
38
|
-
dht: {
|
|
39
|
-
type: DHTResponse.Type.VALUE,
|
|
40
|
-
value: pubKey.bytes
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
*/
|
|
44
42
|
}
|
|
45
43
|
async *getValue(key) {
|
|
46
44
|
try {
|
|
47
|
-
for await (const event of this.dht.
|
|
45
|
+
for await (const event of this.dht.get(key)) {
|
|
48
46
|
if (event.name === 'VALUE') {
|
|
49
47
|
yield OkResponse({
|
|
50
48
|
dht: {
|
|
@@ -56,7 +54,8 @@ export class DHTOperations {
|
|
|
56
54
|
}
|
|
57
55
|
}
|
|
58
56
|
catch (err) {
|
|
59
|
-
|
|
57
|
+
log.error(err);
|
|
58
|
+
yield ErrorResponse(err);
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
async *putValue(key, value) {
|
|
@@ -65,7 +64,8 @@ export class DHTOperations {
|
|
|
65
64
|
yield OkResponse();
|
|
66
65
|
}
|
|
67
66
|
catch (err) {
|
|
68
|
-
|
|
67
|
+
log.error(err);
|
|
68
|
+
yield ErrorResponse(err);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
async *findPeer(peerId) {
|
|
@@ -86,6 +86,7 @@ export class DHTOperations {
|
|
|
86
86
|
throw new Error('Peer not found');
|
|
87
87
|
}
|
|
88
88
|
catch (err) {
|
|
89
|
+
log.error(err);
|
|
89
90
|
yield ErrorResponse(err);
|
|
90
91
|
}
|
|
91
92
|
}
|
|
@@ -106,12 +107,12 @@ export class DHTOperations {
|
|
|
106
107
|
type: DHTResponse.Type.VALUE,
|
|
107
108
|
peer: {
|
|
108
109
|
id: provider.id.toBytes(),
|
|
109
|
-
addrs: (provider.multiaddrs
|
|
110
|
+
addrs: (provider.multiaddrs ?? []).map(m => m.bytes)
|
|
110
111
|
}
|
|
111
112
|
}).finish();
|
|
112
113
|
}
|
|
113
114
|
if (maxNumProviders === found) {
|
|
114
|
-
|
|
115
|
+
break;
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
118
|
}
|
package/dist/src/dht.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dht.js","sourceRoot":"","sources":["../../src/dht.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,EACL,WAAW,
|
|
1
|
+
{"version":3,"file":"dht.js","sourceRoot":"","sources":["../../src/dht.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,EACL,WAAW,EACZ,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAI1D,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAEvC,MAAM,GAAG,GAAG,MAAM,CAAC,0BAA0B,CAAC,CAAA;AAM9C,MAAM,OA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ACN;iBACF;aACF;SACF;QAAC,OAAO,GAAQ,EAAE;YACjB,MAAM,aAAa,CAAC,GAAG,CAAC,CAAA;SACzB;QAED,MAAM,WAAW,CAAC,MAAM,CAAC;YACvB,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,GAAG;SAC3B,CAAC,CAAC,MAAM,EAAE,CAAA;IACb,CAAC;CACF"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -31,16 +31,16 @@ export interface DaemonInit {
|
|
|
31
31
|
export interface Libp2pServer {
|
|
32
32
|
start: () => Promise<void>;
|
|
33
33
|
stop: () => Promise<void>;
|
|
34
|
-
|
|
34
|
+
getMultiaddr: () => Multiaddr;
|
|
35
35
|
}
|
|
36
36
|
export declare class Server implements Libp2pServer {
|
|
37
|
-
private multiaddr;
|
|
38
|
-
private libp2p;
|
|
39
|
-
private tcp;
|
|
40
|
-
private listener;
|
|
41
|
-
private streamHandlers;
|
|
42
|
-
private dhtOperations?;
|
|
43
|
-
private pubsubOperations?;
|
|
37
|
+
private readonly multiaddr;
|
|
38
|
+
private readonly libp2p;
|
|
39
|
+
private readonly tcp;
|
|
40
|
+
private readonly listener;
|
|
41
|
+
private readonly streamHandlers;
|
|
42
|
+
private readonly dhtOperations?;
|
|
43
|
+
private readonly pubsubOperations?;
|
|
44
44
|
constructor(init: DaemonInit);
|
|
45
45
|
/**
|
|
46
46
|
* Connects the daemons libp2p node to the peer provided
|
|
@@ -54,16 +54,10 @@ export declare class Server implements Libp2pServer {
|
|
|
54
54
|
* Sends inbound requests for the given protocol
|
|
55
55
|
* to the unix socket path provided. If an existing handler
|
|
56
56
|
* is registered at the path, it will be overridden.
|
|
57
|
-
*
|
|
58
|
-
* @param {StreamHandlerRequest} request
|
|
59
|
-
* @returns {Promise<void>}
|
|
60
57
|
*/
|
|
61
58
|
registerStreamHandler(request: IRequest): Promise<void>;
|
|
62
59
|
/**
|
|
63
60
|
* Listens for process exit to handle cleanup
|
|
64
|
-
*
|
|
65
|
-
* @private
|
|
66
|
-
* @returns {void}
|
|
67
61
|
*/
|
|
68
62
|
_listen(): void;
|
|
69
63
|
_onExit(): void;
|
|
@@ -71,13 +65,9 @@ export declare class Server implements Libp2pServer {
|
|
|
71
65
|
* Starts the daemon
|
|
72
66
|
*/
|
|
73
67
|
start(): Promise<void>;
|
|
74
|
-
|
|
68
|
+
getMultiaddr(): Multiaddr;
|
|
75
69
|
/**
|
|
76
70
|
* Stops the daemon
|
|
77
|
-
*
|
|
78
|
-
* @param {object} options
|
|
79
|
-
* @param {boolean} options.exit - If the daemon process should exit
|
|
80
|
-
* @returns {Promise<void>}
|
|
81
71
|
*/
|
|
82
72
|
stop(options?: {
|
|
83
73
|
exit: boolean;
|
|
@@ -99,5 +89,5 @@ export declare class Server implements Libp2pServer {
|
|
|
99
89
|
/**
|
|
100
90
|
* Creates a daemon from the provided Daemon Options
|
|
101
91
|
*/
|
|
102
|
-
export declare const createServer: (multiaddr: Multiaddr, libp2pNode: Libp2p) =>
|
|
92
|
+
export declare const createServer: (multiaddr: Multiaddr, libp2pNode: Libp2p) => Libp2pServer;
|
|
103
93
|
//# 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":"AAGA,OAAO,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAa,MAAM,yBAAyB,CAAA;AAM9D,OAAO,EAML,QAAQ,EACR,WAAW,EACX,UAAU,EACV,WAAW,EACX,iBAAiB,EAClB,MAAM,yBAAyB,CAAA;AAEhC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,KAAK,EAAE,YAAY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TCP } from '@libp2p/tcp';
|
|
3
|
-
import { Multiaddr } from '@multiformats/multiaddr';
|
|
3
|
+
import { Multiaddr, protocols } from '@multiformats/multiaddr';
|
|
4
4
|
import { CID } from 'multiformats/cid';
|
|
5
5
|
import * as lp from 'it-length-prefixed';
|
|
6
6
|
import { pipe } from 'it-pipe';
|
|
7
|
-
import { StreamHandler } from '
|
|
8
|
-
import {
|
|
9
|
-
import { passThroughUpgrader } from './util/index.js';
|
|
7
|
+
import { StreamHandler } from '@libp2p/daemon-protocol/stream-handler';
|
|
8
|
+
import { passThroughUpgrader } from '@libp2p/daemon-protocol/upgrader';
|
|
10
9
|
import { Request, DHTRequest, PeerstoreRequest, PSRequest, StreamInfo } from '@libp2p/daemon-protocol';
|
|
11
10
|
import { ErrorResponse, OkResponse } from './responses.js';
|
|
12
11
|
import { DHTOperations } from './dht.js';
|
|
13
|
-
import { peerIdFromBytes
|
|
12
|
+
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
14
13
|
import { PubSubOperations } from './pubsub.js';
|
|
15
14
|
import { logger } from '@libp2p/logger';
|
|
16
15
|
const LIMIT = 1 << 22; // 4MB
|
|
17
|
-
const log = logger('libp2p:daemon');
|
|
16
|
+
const log = logger('libp2p:daemon-server');
|
|
18
17
|
export class Server {
|
|
19
18
|
constructor(init) {
|
|
20
19
|
const { multiaddr, libp2pNode } = init;
|
|
@@ -45,7 +44,7 @@ export class Server {
|
|
|
45
44
|
const addrs = request.connect.addrs.map((a) => new Multiaddr(a));
|
|
46
45
|
const peerId = peerIdFromBytes(peer);
|
|
47
46
|
await this.libp2p.peerStore.addressBook.set(peerId, addrs);
|
|
48
|
-
return this.libp2p.dial(peerId);
|
|
47
|
+
return await this.libp2p.dial(peerId);
|
|
49
48
|
}
|
|
50
49
|
/**
|
|
51
50
|
* Opens a stream on one of the given protocols to the given peer
|
|
@@ -55,7 +54,7 @@ export class Server {
|
|
|
55
54
|
throw new Error('Invalid request');
|
|
56
55
|
}
|
|
57
56
|
const { peer, proto } = request.streamOpen;
|
|
58
|
-
const peerId =
|
|
57
|
+
const peerId = peerIdFromBytes(peer);
|
|
59
58
|
const connection = await this.libp2p.dial(peerId);
|
|
60
59
|
const { stream, protocol } = await connection.newStream(proto);
|
|
61
60
|
return {
|
|
@@ -71,9 +70,6 @@ export class Server {
|
|
|
71
70
|
* Sends inbound requests for the given protocol
|
|
72
71
|
* to the unix socket path provided. If an existing handler
|
|
73
72
|
* is registered at the path, it will be overridden.
|
|
74
|
-
*
|
|
75
|
-
* @param {StreamHandlerRequest} request
|
|
76
|
-
* @returns {Promise<void>}
|
|
77
73
|
*/
|
|
78
74
|
async registerStreamHandler(request) {
|
|
79
75
|
if (request.streamHandler == null || request.streamHandler.proto == null) {
|
|
@@ -83,13 +79,13 @@ export class Server {
|
|
|
83
79
|
const addr = new Multiaddr(request.streamHandler.addr);
|
|
84
80
|
const addrString = addr.toString();
|
|
85
81
|
// If we have a handler, end it
|
|
86
|
-
if (this.streamHandlers[addrString]) {
|
|
87
|
-
this.streamHandlers[addrString].close();
|
|
88
|
-
delete this.streamHandlers[addrString];
|
|
82
|
+
if (this.streamHandlers[addrString] != null) {
|
|
83
|
+
await this.streamHandlers[addrString].close();
|
|
84
|
+
delete this.streamHandlers[addrString]; // eslint-disable-line @typescript-eslint/no-dynamic-delete
|
|
89
85
|
}
|
|
90
|
-
protocols.
|
|
86
|
+
await Promise.all(protocols.map(async (proto) => {
|
|
91
87
|
// Connect the client socket with the libp2p connection
|
|
92
|
-
this.libp2p.handle(proto, ({ connection, stream, protocol }) => {
|
|
88
|
+
await this.libp2p.handle(proto, ({ connection, stream, protocol }) => {
|
|
93
89
|
const message = StreamInfo.encode({
|
|
94
90
|
peer: connection.remotePeer.toBytes(),
|
|
95
91
|
addr: connection.remoteAddr.bytes,
|
|
@@ -98,18 +94,17 @@ export class Server {
|
|
|
98
94
|
const encodedMessage = lp.encode.single(message);
|
|
99
95
|
// Tell the client about the new connection
|
|
100
96
|
// And then begin piping the client and peer connection
|
|
101
|
-
pipe([encodedMessage, stream.source], clientConnection, stream.sink)
|
|
97
|
+
void pipe([encodedMessage, stream.source], clientConnection, stream.sink).catch(err => {
|
|
98
|
+
log.error(err);
|
|
99
|
+
});
|
|
102
100
|
});
|
|
103
|
-
});
|
|
101
|
+
}));
|
|
104
102
|
const clientConnection = await this.tcp.dial(addr, {
|
|
105
103
|
upgrader: passThroughUpgrader
|
|
106
104
|
});
|
|
107
105
|
}
|
|
108
106
|
/**
|
|
109
107
|
* Listens for process exit to handle cleanup
|
|
110
|
-
*
|
|
111
|
-
* @private
|
|
112
|
-
* @returns {void}
|
|
113
108
|
*/
|
|
114
109
|
_listen() {
|
|
115
110
|
// listen for graceful termination
|
|
@@ -118,7 +113,9 @@ export class Server {
|
|
|
118
113
|
process.on('SIGHUP', this._onExit);
|
|
119
114
|
}
|
|
120
115
|
_onExit() {
|
|
121
|
-
this.stop({ exit: true })
|
|
116
|
+
void this.stop({ exit: true }).catch(err => {
|
|
117
|
+
log.error(err);
|
|
118
|
+
});
|
|
122
119
|
}
|
|
123
120
|
/**
|
|
124
121
|
* Starts the daemon
|
|
@@ -128,15 +125,15 @@ export class Server {
|
|
|
128
125
|
await this.libp2p.start();
|
|
129
126
|
await this.listener.listen(this.multiaddr);
|
|
130
127
|
}
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
getMultiaddr() {
|
|
129
|
+
const addrs = this.listener.getAddrs();
|
|
130
|
+
if (addrs.length > 0) {
|
|
131
|
+
return addrs[0];
|
|
132
|
+
}
|
|
133
|
+
throw new Error('Not started');
|
|
133
134
|
}
|
|
134
135
|
/**
|
|
135
136
|
* Stops the daemon
|
|
136
|
-
*
|
|
137
|
-
* @param {object} options
|
|
138
|
-
* @param {boolean} options.exit - If the daemon process should exit
|
|
139
|
-
* @returns {Promise<void>}
|
|
140
137
|
*/
|
|
141
138
|
async stop(options = { exit: false }) {
|
|
142
139
|
await this.libp2p.stop();
|
|
@@ -155,9 +152,9 @@ export class Server {
|
|
|
155
152
|
if (request.id == null) {
|
|
156
153
|
throw new Error('Invalid request');
|
|
157
154
|
}
|
|
158
|
-
const peerId = peerIdFromBytes(request.id);
|
|
159
|
-
const peer = await this.libp2p.peerStore.get(peerId);
|
|
160
|
-
const protos = peer.protocols;
|
|
155
|
+
const peerId = peerIdFromBytes(request.id); // eslint-disable-line no-case-declarations
|
|
156
|
+
const peer = await this.libp2p.peerStore.get(peerId); // eslint-disable-line no-case-declarations
|
|
157
|
+
const protos = peer.protocols; // eslint-disable-line no-case-declarations
|
|
161
158
|
yield OkResponse({ peerStore: { protos } });
|
|
162
159
|
return;
|
|
163
160
|
case PeerstoreRequest.Type.GET_PEER_INFO:
|
|
@@ -167,6 +164,7 @@ export class Server {
|
|
|
167
164
|
}
|
|
168
165
|
}
|
|
169
166
|
catch (err) {
|
|
167
|
+
log.error(err);
|
|
170
168
|
yield ErrorResponse(err);
|
|
171
169
|
}
|
|
172
170
|
}
|
|
@@ -175,7 +173,7 @@ export class Server {
|
|
|
175
173
|
*/
|
|
176
174
|
async *handlePubsubRequest(request) {
|
|
177
175
|
try {
|
|
178
|
-
if (this.libp2p.pubsub == null ||
|
|
176
|
+
if (this.libp2p.pubsub == null || (this.pubsubOperations == null)) {
|
|
179
177
|
throw new Error('PubSub not configured');
|
|
180
178
|
}
|
|
181
179
|
switch (request.type) {
|
|
@@ -199,6 +197,7 @@ export class Server {
|
|
|
199
197
|
}
|
|
200
198
|
}
|
|
201
199
|
catch (err) {
|
|
200
|
+
log.error(err);
|
|
202
201
|
yield ErrorResponse(err);
|
|
203
202
|
}
|
|
204
203
|
}
|
|
@@ -207,7 +206,7 @@ export class Server {
|
|
|
207
206
|
*/
|
|
208
207
|
async *handleDHTRequest(request) {
|
|
209
208
|
try {
|
|
210
|
-
if (this.libp2p.dht == null ||
|
|
209
|
+
if (this.libp2p.dht == null || (this.dhtOperations == null)) {
|
|
211
210
|
throw new Error('DHT not configured');
|
|
212
211
|
}
|
|
213
212
|
switch (request.type) {
|
|
@@ -258,6 +257,7 @@ export class Server {
|
|
|
258
257
|
}
|
|
259
258
|
}
|
|
260
259
|
catch (err) {
|
|
260
|
+
log.error(err);
|
|
261
261
|
yield ErrorResponse(err);
|
|
262
262
|
}
|
|
263
263
|
}
|
|
@@ -265,114 +265,120 @@ export class Server {
|
|
|
265
265
|
* Handles requests for the given connection
|
|
266
266
|
*/
|
|
267
267
|
async handleConnection(connection) {
|
|
268
|
-
const daemon = this;
|
|
268
|
+
const daemon = this; // eslint-disable-line @typescript-eslint/no-this-alias
|
|
269
269
|
// @ts-expect-error connection may actually be a maconn?
|
|
270
270
|
const streamHandler = new StreamHandler({ stream: connection, maxLength: LIMIT });
|
|
271
271
|
await pipe(streamHandler.decoder, source => (async function* () {
|
|
272
272
|
let request;
|
|
273
|
-
for await (
|
|
273
|
+
for await (const buf of source) {
|
|
274
274
|
try {
|
|
275
275
|
request = Request.decode(buf);
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
catch (err) {
|
|
288
|
-
yield ErrorResponse(err);
|
|
276
|
+
switch (request.type) {
|
|
277
|
+
// Connect to another peer
|
|
278
|
+
case Request.Type.CONNECT: {
|
|
279
|
+
try {
|
|
280
|
+
await daemon.connect(request);
|
|
281
|
+
}
|
|
282
|
+
catch (err) {
|
|
283
|
+
yield ErrorResponse(err);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
yield OkResponse();
|
|
289
287
|
break;
|
|
290
288
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
id: daemon.libp2p.peerId.toBytes(),
|
|
299
|
-
addrs: daemon.libp2p.getMultiaddrs().map(m => m.bytes)
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
break;
|
|
303
|
-
}
|
|
304
|
-
// Get a list of our current peers
|
|
305
|
-
case Request.Type.LIST_PEERS: {
|
|
306
|
-
const peers = [];
|
|
307
|
-
for (const peerId of daemon.libp2p.getPeers()) {
|
|
308
|
-
const conn = daemon.libp2p.getConnections(peerId)[0];
|
|
309
|
-
peers.push({
|
|
310
|
-
id: peerId.toBytes(),
|
|
311
|
-
addrs: [conn.remoteAddr.bytes]
|
|
289
|
+
// Get the daemon peer id and addresses
|
|
290
|
+
case Request.Type.IDENTIFY: {
|
|
291
|
+
yield OkResponse({
|
|
292
|
+
identify: {
|
|
293
|
+
id: daemon.libp2p.peerId.toBytes(),
|
|
294
|
+
addrs: daemon.libp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code)).map(m => m.bytes)
|
|
295
|
+
}
|
|
312
296
|
});
|
|
297
|
+
break;
|
|
313
298
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
299
|
+
// Get a list of our current peers
|
|
300
|
+
case Request.Type.LIST_PEERS: {
|
|
301
|
+
const peers = [];
|
|
302
|
+
const seen = new Set();
|
|
303
|
+
for (const connection of daemon.libp2p.getConnections()) {
|
|
304
|
+
const peerId = connection.remotePeer.toString();
|
|
305
|
+
if (seen.has(peerId)) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
seen.add(peerId);
|
|
309
|
+
peers.push({
|
|
310
|
+
id: connection.remotePeer.toBytes(),
|
|
311
|
+
addrs: [connection.remoteAddr.bytes]
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
yield OkResponse({ peers });
|
|
324
315
|
break;
|
|
325
316
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
317
|
+
case Request.Type.STREAM_OPEN: {
|
|
318
|
+
let response;
|
|
319
|
+
try {
|
|
320
|
+
response = await daemon.openStream(request);
|
|
321
|
+
}
|
|
322
|
+
catch (err) {
|
|
323
|
+
yield ErrorResponse(err);
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
// write the response
|
|
327
|
+
yield OkResponse({
|
|
328
|
+
streamInfo: response.streamInfo
|
|
329
|
+
});
|
|
330
|
+
const stream = streamHandler.rest();
|
|
331
|
+
// then pipe the connection to the client
|
|
332
|
+
await pipe(stream, response.connection, stream);
|
|
333
|
+
// Exit the iterator, no more requests can come through
|
|
334
|
+
return;
|
|
339
335
|
}
|
|
340
|
-
|
|
341
|
-
|
|
336
|
+
case Request.Type.STREAM_HANDLER: {
|
|
337
|
+
try {
|
|
338
|
+
await daemon.registerStreamHandler(request);
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
yield ErrorResponse(err);
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
// write the response
|
|
345
|
+
yield OkResponse();
|
|
342
346
|
break;
|
|
343
347
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'));
|
|
348
|
+
case Request.Type.PEERSTORE: {
|
|
349
|
+
if (request.peerStore == null) {
|
|
350
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'));
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
yield* daemon.handlePeerStoreRequest(request.peerStore);
|
|
351
354
|
break;
|
|
352
355
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
yield
|
|
356
|
+
case Request.Type.PUBSUB: {
|
|
357
|
+
if (request.pubsub == null) {
|
|
358
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'));
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
yield* daemon.handlePubsubRequest(request.pubsub);
|
|
359
362
|
break;
|
|
360
363
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
yield
|
|
364
|
+
case Request.Type.DHT: {
|
|
365
|
+
if (request.dht == null) {
|
|
366
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'));
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
yield* daemon.handleDHTRequest(request.dht);
|
|
367
370
|
break;
|
|
368
371
|
}
|
|
369
|
-
|
|
370
|
-
|
|
372
|
+
// Not yet supported or doesn't exist
|
|
373
|
+
default:
|
|
374
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST_TYPE'));
|
|
375
|
+
break;
|
|
371
376
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
377
|
+
}
|
|
378
|
+
catch (err) {
|
|
379
|
+
log.error(err);
|
|
380
|
+
yield ErrorResponse(err);
|
|
381
|
+
continue;
|
|
376
382
|
}
|
|
377
383
|
}
|
|
378
384
|
})(), async function (source) {
|
|
@@ -385,7 +391,7 @@ export class Server {
|
|
|
385
391
|
/**
|
|
386
392
|
* Creates a daemon from the provided Daemon Options
|
|
387
393
|
*/
|
|
388
|
-
export const createServer =
|
|
394
|
+
export const createServer = (multiaddr, libp2pNode) => {
|
|
389
395
|
const daemon = new Server({
|
|
390
396
|
multiaddr,
|
|
391
397
|
libp2pNode
|