@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/src/index.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/* eslint max-depth: ["error", 6] */
|
|
2
2
|
|
|
3
3
|
import { TCP } from '@libp2p/tcp'
|
|
4
|
-
import { Multiaddr } from '@multiformats/multiaddr'
|
|
4
|
+
import { Multiaddr, protocols } from '@multiformats/multiaddr'
|
|
5
5
|
import { CID } from 'multiformats/cid'
|
|
6
6
|
import * as lp from 'it-length-prefixed'
|
|
7
7
|
import { pipe } from 'it-pipe'
|
|
8
|
-
import { StreamHandler } from '
|
|
9
|
-
import {
|
|
10
|
-
import { passThroughUpgrader } from './util/index.js'
|
|
8
|
+
import { StreamHandler } from '@libp2p/daemon-protocol/stream-handler'
|
|
9
|
+
import { passThroughUpgrader } from '@libp2p/daemon-protocol/upgrader'
|
|
11
10
|
import {
|
|
12
11
|
Request,
|
|
13
12
|
DHTRequest,
|
|
@@ -30,15 +29,15 @@ import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
|
30
29
|
import type { PeerStore } from '@libp2p/interfaces/peer-store'
|
|
31
30
|
import { ErrorResponse, OkResponse } from './responses.js'
|
|
32
31
|
import { DHTOperations } from './dht.js'
|
|
33
|
-
import { peerIdFromBytes
|
|
32
|
+
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
34
33
|
import { PubSubOperations } from './pubsub.js'
|
|
35
34
|
import { logger } from '@libp2p/logger'
|
|
36
35
|
|
|
37
36
|
const LIMIT = 1 << 22 // 4MB
|
|
38
|
-
const log = logger('libp2p:daemon')
|
|
37
|
+
const log = logger('libp2p:daemon-server')
|
|
39
38
|
|
|
40
39
|
export interface OpenStream {
|
|
41
|
-
streamInfo: IStreamInfo
|
|
40
|
+
streamInfo: IStreamInfo
|
|
42
41
|
connection: Stream
|
|
43
42
|
}
|
|
44
43
|
|
|
@@ -58,24 +57,24 @@ export interface Libp2p {
|
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
export interface DaemonInit {
|
|
61
|
-
multiaddr: Multiaddr
|
|
60
|
+
multiaddr: Multiaddr
|
|
62
61
|
libp2pNode: any
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
export interface Libp2pServer {
|
|
66
65
|
start: () => Promise<void>
|
|
67
66
|
stop: () => Promise<void>
|
|
68
|
-
|
|
67
|
+
getMultiaddr: () => Multiaddr
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
export class Server implements Libp2pServer {
|
|
72
|
-
private multiaddr: Multiaddr
|
|
73
|
-
private libp2p: Libp2p
|
|
74
|
-
private tcp: TCP
|
|
75
|
-
private listener: Listener
|
|
76
|
-
private streamHandlers: Record<string, StreamHandler>
|
|
77
|
-
private dhtOperations?: DHTOperations
|
|
78
|
-
private pubsubOperations?: PubSubOperations
|
|
71
|
+
private readonly multiaddr: Multiaddr
|
|
72
|
+
private readonly libp2p: Libp2p
|
|
73
|
+
private readonly tcp: TCP
|
|
74
|
+
private readonly listener: Listener
|
|
75
|
+
private readonly streamHandlers: Record<string, StreamHandler>
|
|
76
|
+
private readonly dhtOperations?: DHTOperations
|
|
77
|
+
private readonly pubsubOperations?: PubSubOperations
|
|
79
78
|
|
|
80
79
|
constructor (init: DaemonInit) {
|
|
81
80
|
const { multiaddr, libp2pNode } = init
|
|
@@ -112,7 +111,7 @@ export class Server implements Libp2pServer {
|
|
|
112
111
|
const peerId = peerIdFromBytes(peer)
|
|
113
112
|
|
|
114
113
|
await this.libp2p.peerStore.addressBook.set(peerId, addrs)
|
|
115
|
-
return this.libp2p.dial(peerId)
|
|
114
|
+
return await this.libp2p.dial(peerId)
|
|
116
115
|
}
|
|
117
116
|
|
|
118
117
|
/**
|
|
@@ -125,7 +124,7 @@ export class Server implements Libp2pServer {
|
|
|
125
124
|
|
|
126
125
|
const { peer, proto } = request.streamOpen
|
|
127
126
|
|
|
128
|
-
const peerId =
|
|
127
|
+
const peerId = peerIdFromBytes(peer)
|
|
129
128
|
|
|
130
129
|
const connection = await this.libp2p.dial(peerId)
|
|
131
130
|
const { stream, protocol } = await connection.newStream(proto)
|
|
@@ -144,9 +143,6 @@ export class Server implements Libp2pServer {
|
|
|
144
143
|
* Sends inbound requests for the given protocol
|
|
145
144
|
* to the unix socket path provided. If an existing handler
|
|
146
145
|
* is registered at the path, it will be overridden.
|
|
147
|
-
*
|
|
148
|
-
* @param {StreamHandlerRequest} request
|
|
149
|
-
* @returns {Promise<void>}
|
|
150
146
|
*/
|
|
151
147
|
async registerStreamHandler (request: IRequest): Promise<void> {
|
|
152
148
|
if (request.streamHandler == null || request.streamHandler.proto == null) {
|
|
@@ -158,31 +154,34 @@ export class Server implements Libp2pServer {
|
|
|
158
154
|
const addrString = addr.toString()
|
|
159
155
|
|
|
160
156
|
// If we have a handler, end it
|
|
161
|
-
if (this.streamHandlers[addrString]) {
|
|
162
|
-
this.streamHandlers[addrString].close()
|
|
163
|
-
delete this.streamHandlers[addrString]
|
|
157
|
+
if (this.streamHandlers[addrString] != null) {
|
|
158
|
+
await this.streamHandlers[addrString].close()
|
|
159
|
+
delete this.streamHandlers[addrString] // eslint-disable-line @typescript-eslint/no-dynamic-delete
|
|
164
160
|
}
|
|
165
161
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
162
|
+
await Promise.all(
|
|
163
|
+
protocols.map(async (proto) => {
|
|
164
|
+
// Connect the client socket with the libp2p connection
|
|
165
|
+
await this.libp2p.handle(proto, ({ connection, stream, protocol }) => {
|
|
166
|
+
const message = StreamInfo.encode({
|
|
167
|
+
peer: connection.remotePeer.toBytes(),
|
|
168
|
+
addr: connection.remoteAddr.bytes,
|
|
169
|
+
proto: protocol
|
|
170
|
+
}).finish()
|
|
171
|
+
const encodedMessage = lp.encode.single(message)
|
|
172
|
+
|
|
173
|
+
// Tell the client about the new connection
|
|
174
|
+
// And then begin piping the client and peer connection
|
|
175
|
+
void pipe(
|
|
176
|
+
[encodedMessage, stream.source],
|
|
177
|
+
clientConnection,
|
|
178
|
+
stream.sink
|
|
179
|
+
).catch(err => {
|
|
180
|
+
log.error(err)
|
|
181
|
+
})
|
|
182
|
+
})
|
|
184
183
|
})
|
|
185
|
-
|
|
184
|
+
)
|
|
186
185
|
|
|
187
186
|
const clientConnection = await this.tcp.dial(addr, {
|
|
188
187
|
upgrader: passThroughUpgrader
|
|
@@ -191,9 +190,6 @@ export class Server implements Libp2pServer {
|
|
|
191
190
|
|
|
192
191
|
/**
|
|
193
192
|
* Listens for process exit to handle cleanup
|
|
194
|
-
*
|
|
195
|
-
* @private
|
|
196
|
-
* @returns {void}
|
|
197
193
|
*/
|
|
198
194
|
_listen () {
|
|
199
195
|
// listen for graceful termination
|
|
@@ -203,7 +199,9 @@ export class Server implements Libp2pServer {
|
|
|
203
199
|
}
|
|
204
200
|
|
|
205
201
|
_onExit () {
|
|
206
|
-
this.stop({ exit: true })
|
|
202
|
+
void this.stop({ exit: true }).catch(err => {
|
|
203
|
+
log.error(err)
|
|
204
|
+
})
|
|
207
205
|
}
|
|
208
206
|
|
|
209
207
|
/**
|
|
@@ -215,16 +213,18 @@ export class Server implements Libp2pServer {
|
|
|
215
213
|
await this.listener.listen(this.multiaddr)
|
|
216
214
|
}
|
|
217
215
|
|
|
218
|
-
|
|
219
|
-
|
|
216
|
+
getMultiaddr (): Multiaddr {
|
|
217
|
+
const addrs = this.listener.getAddrs()
|
|
218
|
+
|
|
219
|
+
if (addrs.length > 0) {
|
|
220
|
+
return addrs[0]
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
throw new Error('Not started')
|
|
220
224
|
}
|
|
221
225
|
|
|
222
226
|
/**
|
|
223
227
|
* Stops the daemon
|
|
224
|
-
*
|
|
225
|
-
* @param {object} options
|
|
226
|
-
* @param {boolean} options.exit - If the daemon process should exit
|
|
227
|
-
* @returns {Promise<void>}
|
|
228
228
|
*/
|
|
229
229
|
async stop (options = { exit: false }) {
|
|
230
230
|
await this.libp2p.stop()
|
|
@@ -245,9 +245,9 @@ export class Server implements Libp2pServer {
|
|
|
245
245
|
throw new Error('Invalid request')
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
const peerId = peerIdFromBytes(request.id)
|
|
249
|
-
const peer = await this.libp2p.peerStore.get(peerId)
|
|
250
|
-
const protos = peer.protocols
|
|
248
|
+
const peerId = peerIdFromBytes(request.id) // eslint-disable-line no-case-declarations
|
|
249
|
+
const peer = await this.libp2p.peerStore.get(peerId) // eslint-disable-line no-case-declarations
|
|
250
|
+
const protos = peer.protocols // eslint-disable-line no-case-declarations
|
|
251
251
|
yield OkResponse({ peerStore: { protos } })
|
|
252
252
|
return
|
|
253
253
|
case PeerstoreRequest.Type.GET_PEER_INFO:
|
|
@@ -256,6 +256,7 @@ export class Server implements Libp2pServer {
|
|
|
256
256
|
throw new Error('ERR_INVALID_REQUEST_TYPE')
|
|
257
257
|
}
|
|
258
258
|
} catch (err: any) {
|
|
259
|
+
log.error(err)
|
|
259
260
|
yield ErrorResponse(err)
|
|
260
261
|
}
|
|
261
262
|
}
|
|
@@ -265,7 +266,7 @@ export class Server implements Libp2pServer {
|
|
|
265
266
|
*/
|
|
266
267
|
async * handlePubsubRequest (request: IPSRequest) {
|
|
267
268
|
try {
|
|
268
|
-
if (this.libp2p.pubsub == null ||
|
|
269
|
+
if (this.libp2p.pubsub == null || (this.pubsubOperations == null)) {
|
|
269
270
|
throw new Error('PubSub not configured')
|
|
270
271
|
}
|
|
271
272
|
|
|
@@ -291,6 +292,7 @@ export class Server implements Libp2pServer {
|
|
|
291
292
|
throw new Error('ERR_INVALID_REQUEST_TYPE')
|
|
292
293
|
}
|
|
293
294
|
} catch (err: any) {
|
|
295
|
+
log.error(err)
|
|
294
296
|
yield ErrorResponse(err)
|
|
295
297
|
}
|
|
296
298
|
}
|
|
@@ -300,7 +302,7 @@ export class Server implements Libp2pServer {
|
|
|
300
302
|
*/
|
|
301
303
|
async * handleDHTRequest (request: IDHTRequest) {
|
|
302
304
|
try {
|
|
303
|
-
if (this.libp2p.dht == null ||
|
|
305
|
+
if (this.libp2p.dht == null || (this.dhtOperations == null)) {
|
|
304
306
|
throw new Error('DHT not configured')
|
|
305
307
|
}
|
|
306
308
|
|
|
@@ -358,6 +360,7 @@ export class Server implements Libp2pServer {
|
|
|
358
360
|
throw new Error('ERR_INVALID_REQUEST_TYPE')
|
|
359
361
|
}
|
|
360
362
|
} catch (err: any) {
|
|
363
|
+
log.error(err)
|
|
361
364
|
yield ErrorResponse(err)
|
|
362
365
|
}
|
|
363
366
|
}
|
|
@@ -366,7 +369,7 @@ export class Server implements Libp2pServer {
|
|
|
366
369
|
* Handles requests for the given connection
|
|
367
370
|
*/
|
|
368
371
|
async handleConnection (connection: Connection) {
|
|
369
|
-
const daemon = this
|
|
372
|
+
const daemon = this // eslint-disable-line @typescript-eslint/no-this-alias
|
|
370
373
|
// @ts-expect-error connection may actually be a maconn?
|
|
371
374
|
const streamHandler = new StreamHandler({ stream: connection, maxLength: LIMIT })
|
|
372
375
|
|
|
@@ -375,119 +378,127 @@ export class Server implements Libp2pServer {
|
|
|
375
378
|
source => (async function * () {
|
|
376
379
|
let request: Request
|
|
377
380
|
|
|
378
|
-
for await (
|
|
381
|
+
for await (const buf of source) {
|
|
379
382
|
try {
|
|
380
383
|
request = Request.decode(buf)
|
|
381
|
-
} catch (err) {
|
|
382
|
-
yield ErrorResponse(new Error('ERR_INVALID_MESSAGE'))
|
|
383
|
-
continue
|
|
384
|
-
}
|
|
385
384
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
385
|
+
switch (request.type) {
|
|
386
|
+
// Connect to another peer
|
|
387
|
+
case Request.Type.CONNECT: {
|
|
388
|
+
try {
|
|
389
|
+
await daemon.connect(request)
|
|
390
|
+
} catch (err: any) {
|
|
391
|
+
yield ErrorResponse(err)
|
|
392
|
+
break
|
|
393
|
+
}
|
|
394
|
+
yield OkResponse()
|
|
393
395
|
break
|
|
394
396
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
397
|
+
// Get the daemon peer id and addresses
|
|
398
|
+
case Request.Type.IDENTIFY: {
|
|
399
|
+
yield OkResponse({
|
|
400
|
+
identify: {
|
|
401
|
+
id: daemon.libp2p.peerId.toBytes(),
|
|
402
|
+
addrs: daemon.libp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code)).map(m => m.bytes)
|
|
403
|
+
}
|
|
404
|
+
})
|
|
405
|
+
break
|
|
406
|
+
}
|
|
407
|
+
// Get a list of our current peers
|
|
408
|
+
case Request.Type.LIST_PEERS: {
|
|
409
|
+
const peers = []
|
|
410
|
+
const seen = new Set<string>()
|
|
411
|
+
|
|
412
|
+
for (const connection of daemon.libp2p.getConnections()) {
|
|
413
|
+
const peerId = connection.remotePeer.toString()
|
|
414
|
+
|
|
415
|
+
if (seen.has(peerId)) {
|
|
416
|
+
continue
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
seen.add(peerId)
|
|
420
|
+
|
|
421
|
+
peers.push({
|
|
422
|
+
id: connection.remotePeer.toBytes(),
|
|
423
|
+
addrs: [connection.remoteAddr.bytes]
|
|
424
|
+
})
|
|
404
425
|
}
|
|
405
|
-
})
|
|
406
|
-
break
|
|
407
|
-
}
|
|
408
|
-
// Get a list of our current peers
|
|
409
|
-
case Request.Type.LIST_PEERS: {
|
|
410
|
-
const peers = []
|
|
411
426
|
|
|
412
|
-
|
|
413
|
-
|
|
427
|
+
yield OkResponse({ peers })
|
|
428
|
+
break
|
|
429
|
+
}
|
|
430
|
+
case Request.Type.STREAM_OPEN: {
|
|
431
|
+
let response
|
|
432
|
+
try {
|
|
433
|
+
response = await daemon.openStream(request)
|
|
434
|
+
} catch (err: any) {
|
|
435
|
+
yield ErrorResponse(err)
|
|
436
|
+
break
|
|
437
|
+
}
|
|
414
438
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
439
|
+
// write the response
|
|
440
|
+
yield OkResponse({
|
|
441
|
+
streamInfo: response.streamInfo
|
|
418
442
|
})
|
|
419
|
-
}
|
|
420
443
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
break
|
|
444
|
+
const stream = streamHandler.rest()
|
|
445
|
+
// then pipe the connection to the client
|
|
446
|
+
await pipe(
|
|
447
|
+
stream,
|
|
448
|
+
response.connection,
|
|
449
|
+
stream
|
|
450
|
+
)
|
|
451
|
+
// Exit the iterator, no more requests can come through
|
|
452
|
+
return
|
|
431
453
|
}
|
|
454
|
+
case Request.Type.STREAM_HANDLER: {
|
|
455
|
+
try {
|
|
456
|
+
await daemon.registerStreamHandler(request)
|
|
457
|
+
} catch (err: any) {
|
|
458
|
+
yield ErrorResponse(err)
|
|
459
|
+
break
|
|
460
|
+
}
|
|
432
461
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
streamInfo: response.streamInfo
|
|
436
|
-
})
|
|
437
|
-
|
|
438
|
-
const stream = streamHandler.rest()
|
|
439
|
-
// then pipe the connection to the client
|
|
440
|
-
await pipe(
|
|
441
|
-
stream,
|
|
442
|
-
response.connection,
|
|
443
|
-
stream
|
|
444
|
-
)
|
|
445
|
-
// Exit the iterator, no more requests can come through
|
|
446
|
-
return
|
|
447
|
-
}
|
|
448
|
-
case Request.Type.STREAM_HANDLER: {
|
|
449
|
-
try {
|
|
450
|
-
await daemon.registerStreamHandler(request)
|
|
451
|
-
} catch (err: any) {
|
|
452
|
-
yield ErrorResponse(err)
|
|
462
|
+
// write the response
|
|
463
|
+
yield OkResponse()
|
|
453
464
|
break
|
|
454
465
|
}
|
|
466
|
+
case Request.Type.PEERSTORE: {
|
|
467
|
+
if (request.peerStore == null) {
|
|
468
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
469
|
+
break
|
|
470
|
+
}
|
|
455
471
|
|
|
456
|
-
|
|
457
|
-
yield OkResponse()
|
|
458
|
-
break
|
|
459
|
-
}
|
|
460
|
-
case Request.Type.PEERSTORE: {
|
|
461
|
-
if (request.peerStore == null) {
|
|
462
|
-
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
472
|
+
yield * daemon.handlePeerStoreRequest(request.peerStore)
|
|
463
473
|
break
|
|
464
474
|
}
|
|
475
|
+
case Request.Type.PUBSUB: {
|
|
476
|
+
if (request.pubsub == null) {
|
|
477
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
478
|
+
break
|
|
479
|
+
}
|
|
465
480
|
|
|
466
|
-
|
|
467
|
-
break
|
|
468
|
-
}
|
|
469
|
-
case Request.Type.PUBSUB: {
|
|
470
|
-
if (request.pubsub == null) {
|
|
471
|
-
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
481
|
+
yield * daemon.handlePubsubRequest(request.pubsub)
|
|
472
482
|
break
|
|
473
483
|
}
|
|
484
|
+
case Request.Type.DHT: {
|
|
485
|
+
if (request.dht == null) {
|
|
486
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
487
|
+
break
|
|
488
|
+
}
|
|
474
489
|
|
|
475
|
-
|
|
476
|
-
break
|
|
477
|
-
}
|
|
478
|
-
case Request.Type.DHT: {
|
|
479
|
-
if (request.dht == null) {
|
|
480
|
-
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
490
|
+
yield * daemon.handleDHTRequest(request.dht)
|
|
481
491
|
break
|
|
482
492
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
493
|
+
// Not yet supported or doesn't exist
|
|
494
|
+
default:
|
|
495
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST_TYPE'))
|
|
496
|
+
break
|
|
486
497
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
498
|
+
} catch (err: any) {
|
|
499
|
+
log.error(err)
|
|
500
|
+
yield ErrorResponse(err)
|
|
501
|
+
continue
|
|
491
502
|
}
|
|
492
503
|
}
|
|
493
504
|
})(),
|
|
@@ -503,7 +514,7 @@ export class Server implements Libp2pServer {
|
|
|
503
514
|
/**
|
|
504
515
|
* Creates a daemon from the provided Daemon Options
|
|
505
516
|
*/
|
|
506
|
-
export const createServer =
|
|
517
|
+
export const createServer = (multiaddr: Multiaddr, libp2pNode: Libp2p): Libp2pServer => {
|
|
507
518
|
const daemon = new Server({
|
|
508
519
|
multiaddr,
|
|
509
520
|
libp2pNode
|
package/src/pubsub.ts
CHANGED
|
@@ -3,17 +3,21 @@
|
|
|
3
3
|
import {
|
|
4
4
|
PSMessage
|
|
5
5
|
} from '@libp2p/daemon-protocol'
|
|
6
|
-
import { OkResponse } from './responses.js'
|
|
6
|
+
import { ErrorResponse, OkResponse } from './responses.js'
|
|
7
7
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
8
8
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
9
9
|
import { pushable } from 'it-pushable'
|
|
10
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
11
|
+
import { logger } from '@libp2p/logger'
|
|
12
|
+
|
|
13
|
+
const log = logger('libp2p:daemon-server:pubsub')
|
|
10
14
|
|
|
11
15
|
export interface PubSubOperationsInit {
|
|
12
16
|
pubsub: PubSub
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
export class PubSubOperations {
|
|
16
|
-
private pubsub: PubSub
|
|
20
|
+
private readonly pubsub: PubSub
|
|
17
21
|
|
|
18
22
|
constructor (init: PubSubOperationsInit) {
|
|
19
23
|
const { pubsub } = init
|
|
@@ -22,36 +26,50 @@ export class PubSubOperations {
|
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
async * getTopics () {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
try {
|
|
30
|
+
yield OkResponse({
|
|
31
|
+
pubsub: {
|
|
32
|
+
topics: this.pubsub.getTopics()
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
} catch (err: any) {
|
|
36
|
+
log.error(err)
|
|
37
|
+
yield ErrorResponse(err)
|
|
38
|
+
}
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
async * subscribe (topic: string) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
try {
|
|
43
|
+
const onMessage = pushable<Uint8Array>()
|
|
44
|
+
|
|
45
|
+
await this.pubsub.addEventListener(topic, (evt) => {
|
|
46
|
+
const msg = evt.detail
|
|
47
|
+
|
|
48
|
+
onMessage.push(PSMessage.encode({
|
|
49
|
+
from: msg.from.toBytes(),
|
|
50
|
+
data: msg.data,
|
|
51
|
+
seqno: msg.sequenceNumber == null ? undefined : uint8ArrayFromString(msg.sequenceNumber.toString(16).padStart(16, '0'), 'base16'),
|
|
52
|
+
topicIDs: [msg.topic],
|
|
53
|
+
signature: msg.signature,
|
|
54
|
+
key: msg.key
|
|
55
|
+
}).finish())
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
yield OkResponse()
|
|
59
|
+
yield * onMessage
|
|
60
|
+
} catch (err: any) {
|
|
61
|
+
log.error(err)
|
|
62
|
+
yield ErrorResponse(err)
|
|
63
|
+
}
|
|
50
64
|
}
|
|
51
65
|
|
|
52
66
|
async * publish (topic: string, data: Uint8Array) {
|
|
53
|
-
|
|
54
|
-
|
|
67
|
+
try {
|
|
68
|
+
this.pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
69
|
+
yield OkResponse()
|
|
70
|
+
} catch (err: any) {
|
|
71
|
+
log.error(err)
|
|
72
|
+
yield ErrorResponse(err)
|
|
73
|
+
}
|
|
55
74
|
}
|
|
56
|
-
|
|
57
75
|
}
|
package/src/responses.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { IResponse, Response } from '@libp2p/daemon-protocol'
|
|
|
3
3
|
/**
|
|
4
4
|
* Creates and encodes an OK response
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
export function OkResponse (data?: Partial<IResponse>): Uint8Array {
|
|
7
7
|
return Response.encode({
|
|
8
8
|
type: Response.Type.OK,
|
|
9
9
|
...data
|
package/dist/src/client.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
2
|
-
import type { Connection } from '@libp2p/interfaces/connection';
|
|
3
|
-
import type { ConnectionHandler } from '@libp2p/interfaces/transport';
|
|
4
|
-
export declare class Client {
|
|
5
|
-
private multiaddr;
|
|
6
|
-
private tcp;
|
|
7
|
-
private listener?;
|
|
8
|
-
constructor(addr: Multiaddr);
|
|
9
|
-
/**
|
|
10
|
-
* Connects to a daemon at the unix socket path the client
|
|
11
|
-
* was created with
|
|
12
|
-
*/
|
|
13
|
-
connect(): Promise<Connection>;
|
|
14
|
-
/**
|
|
15
|
-
* Starts a server listening at `socketPath`. New connections
|
|
16
|
-
* will be sent to the `connectionHandler`.
|
|
17
|
-
*/
|
|
18
|
-
start(addr: Multiaddr, handler: ConnectionHandler): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* Closes the socket
|
|
21
|
-
*
|
|
22
|
-
* @returns {Promise}
|
|
23
|
-
*/
|
|
24
|
-
close(): Promise<void>;
|
|
25
|
-
}
|
|
26
|
-
//# sourceMappingURL=client.d.ts.map
|
package/dist/src/client.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAY,MAAM,8BAA8B,CAAA;AAE/E,qBAAa,MAAM;IACjB,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,GAAG,CAAK;IAChB,OAAO,CAAC,QAAQ,CAAC,CAAU;gBAEd,IAAI,EAAE,SAAS;IAK5B;;;OAGG;IACH,OAAO,IAAK,OAAO,CAAC,UAAU,CAAC;IAM/B;;;OAGG;IACG,KAAK,CAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAaxE;;;;OAIG;IACG,KAAK;CAOZ"}
|
package/dist/src/client.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { TCP } from '@libp2p/tcp';
|
|
2
|
-
import { passThroughUpgrader } from './util/index.js';
|
|
3
|
-
export class Client {
|
|
4
|
-
constructor(addr) {
|
|
5
|
-
this.multiaddr = addr;
|
|
6
|
-
this.tcp = new TCP();
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Connects to a daemon at the unix socket path the client
|
|
10
|
-
* was created with
|
|
11
|
-
*/
|
|
12
|
-
connect() {
|
|
13
|
-
return this.tcp.dial(this.multiaddr, {
|
|
14
|
-
upgrader: passThroughUpgrader
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Starts a server listening at `socketPath`. New connections
|
|
19
|
-
* will be sent to the `connectionHandler`.
|
|
20
|
-
*/
|
|
21
|
-
async start(addr, handler) {
|
|
22
|
-
if (this.listener != null) {
|
|
23
|
-
await this.close();
|
|
24
|
-
}
|
|
25
|
-
this.listener = this.tcp.createListener({
|
|
26
|
-
handler,
|
|
27
|
-
upgrader: passThroughUpgrader
|
|
28
|
-
});
|
|
29
|
-
await this.listener.listen(addr);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Closes the socket
|
|
33
|
-
*
|
|
34
|
-
* @returns {Promise}
|
|
35
|
-
*/
|
|
36
|
-
async close() {
|
|
37
|
-
if (this.listener != null) {
|
|
38
|
-
await this.listener.close();
|
|
39
|
-
}
|
|
40
|
-
this.listener = undefined;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
//# sourceMappingURL=client.js.map
|
package/dist/src/client.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAKrD,MAAM,OAAO,MAAM;IAKjB,YAAa,IAAe;QAC1B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;QACrB,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,EAAE,CAAA;IACtB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnC,QAAQ,EAAE,mBAAmB;SAC9B,CAAC,CAAA;IACJ,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK,CAAE,IAAe,EAAE,OAA0B;QACtD,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,EAAE;YACzB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;SACnB;QAED,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC;YACtC,OAAO;YACP,QAAQ,EAAE,mBAAmB;SAC9B,CAAC,CAAA;QAEF,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK;QACT,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,EAAE;YACzB,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAA;SAC5B;QAED,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAA;IAC3B,CAAC;CACF"}
|