@libp2p/daemon-server 0.0.1 → 0.0.2
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 -10
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +125 -109
- 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 +11 -17
- package/src/dht.ts +20 -20
- package/src/index.ts +159 -138
- 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)
|
|
@@ -158,31 +157,34 @@ export class Server implements Libp2pServer {
|
|
|
158
157
|
const addrString = addr.toString()
|
|
159
158
|
|
|
160
159
|
// If we have a handler, end it
|
|
161
|
-
if (this.streamHandlers[addrString]) {
|
|
160
|
+
if (this.streamHandlers[addrString] != null) {
|
|
162
161
|
this.streamHandlers[addrString].close()
|
|
163
|
-
delete this.streamHandlers[addrString]
|
|
162
|
+
delete this.streamHandlers[addrString] // eslint-disable-line @typescript-eslint/no-dynamic-delete
|
|
164
163
|
}
|
|
165
164
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
165
|
+
await Promise.all(
|
|
166
|
+
protocols.map(async (proto) => {
|
|
167
|
+
// Connect the client socket with the libp2p connection
|
|
168
|
+
await this.libp2p.handle(proto, ({ connection, stream, protocol }) => {
|
|
169
|
+
const message = StreamInfo.encode({
|
|
170
|
+
peer: connection.remotePeer.toBytes(),
|
|
171
|
+
addr: connection.remoteAddr.bytes,
|
|
172
|
+
proto: protocol
|
|
173
|
+
}).finish()
|
|
174
|
+
const encodedMessage = lp.encode.single(message)
|
|
175
|
+
|
|
176
|
+
// Tell the client about the new connection
|
|
177
|
+
// And then begin piping the client and peer connection
|
|
178
|
+
void pipe(
|
|
179
|
+
[encodedMessage, stream.source],
|
|
180
|
+
clientConnection,
|
|
181
|
+
stream.sink
|
|
182
|
+
).catch(err => {
|
|
183
|
+
log.error(err)
|
|
184
|
+
})
|
|
185
|
+
})
|
|
184
186
|
})
|
|
185
|
-
|
|
187
|
+
)
|
|
186
188
|
|
|
187
189
|
const clientConnection = await this.tcp.dial(addr, {
|
|
188
190
|
upgrader: passThroughUpgrader
|
|
@@ -203,7 +205,9 @@ export class Server implements Libp2pServer {
|
|
|
203
205
|
}
|
|
204
206
|
|
|
205
207
|
_onExit () {
|
|
206
|
-
this.stop({ exit: true })
|
|
208
|
+
void this.stop({ exit: true }).catch(err => {
|
|
209
|
+
log.error(err)
|
|
210
|
+
})
|
|
207
211
|
}
|
|
208
212
|
|
|
209
213
|
/**
|
|
@@ -215,8 +219,14 @@ export class Server implements Libp2pServer {
|
|
|
215
219
|
await this.listener.listen(this.multiaddr)
|
|
216
220
|
}
|
|
217
221
|
|
|
218
|
-
|
|
219
|
-
|
|
222
|
+
getMultiaddr (): Multiaddr {
|
|
223
|
+
const addrs = this.listener.getAddrs()
|
|
224
|
+
|
|
225
|
+
if (addrs.length > 0) {
|
|
226
|
+
return addrs[0]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
throw new Error('Not started')
|
|
220
230
|
}
|
|
221
231
|
|
|
222
232
|
/**
|
|
@@ -245,9 +255,9 @@ export class Server implements Libp2pServer {
|
|
|
245
255
|
throw new Error('Invalid request')
|
|
246
256
|
}
|
|
247
257
|
|
|
248
|
-
const peerId = peerIdFromBytes(request.id)
|
|
249
|
-
const peer = await this.libp2p.peerStore.get(peerId)
|
|
250
|
-
const protos = peer.protocols
|
|
258
|
+
const peerId = peerIdFromBytes(request.id) // eslint-disable-line no-case-declarations
|
|
259
|
+
const peer = await this.libp2p.peerStore.get(peerId) // eslint-disable-line no-case-declarations
|
|
260
|
+
const protos = peer.protocols // eslint-disable-line no-case-declarations
|
|
251
261
|
yield OkResponse({ peerStore: { protos } })
|
|
252
262
|
return
|
|
253
263
|
case PeerstoreRequest.Type.GET_PEER_INFO:
|
|
@@ -256,6 +266,7 @@ export class Server implements Libp2pServer {
|
|
|
256
266
|
throw new Error('ERR_INVALID_REQUEST_TYPE')
|
|
257
267
|
}
|
|
258
268
|
} catch (err: any) {
|
|
269
|
+
log.error(err)
|
|
259
270
|
yield ErrorResponse(err)
|
|
260
271
|
}
|
|
261
272
|
}
|
|
@@ -265,7 +276,7 @@ export class Server implements Libp2pServer {
|
|
|
265
276
|
*/
|
|
266
277
|
async * handlePubsubRequest (request: IPSRequest) {
|
|
267
278
|
try {
|
|
268
|
-
if (this.libp2p.pubsub == null ||
|
|
279
|
+
if (this.libp2p.pubsub == null || (this.pubsubOperations == null)) {
|
|
269
280
|
throw new Error('PubSub not configured')
|
|
270
281
|
}
|
|
271
282
|
|
|
@@ -291,6 +302,7 @@ export class Server implements Libp2pServer {
|
|
|
291
302
|
throw new Error('ERR_INVALID_REQUEST_TYPE')
|
|
292
303
|
}
|
|
293
304
|
} catch (err: any) {
|
|
305
|
+
log.error(err)
|
|
294
306
|
yield ErrorResponse(err)
|
|
295
307
|
}
|
|
296
308
|
}
|
|
@@ -300,7 +312,7 @@ export class Server implements Libp2pServer {
|
|
|
300
312
|
*/
|
|
301
313
|
async * handleDHTRequest (request: IDHTRequest) {
|
|
302
314
|
try {
|
|
303
|
-
if (this.libp2p.dht == null ||
|
|
315
|
+
if (this.libp2p.dht == null || (this.dhtOperations == null)) {
|
|
304
316
|
throw new Error('DHT not configured')
|
|
305
317
|
}
|
|
306
318
|
|
|
@@ -358,6 +370,7 @@ export class Server implements Libp2pServer {
|
|
|
358
370
|
throw new Error('ERR_INVALID_REQUEST_TYPE')
|
|
359
371
|
}
|
|
360
372
|
} catch (err: any) {
|
|
373
|
+
log.error(err)
|
|
361
374
|
yield ErrorResponse(err)
|
|
362
375
|
}
|
|
363
376
|
}
|
|
@@ -366,7 +379,7 @@ export class Server implements Libp2pServer {
|
|
|
366
379
|
* Handles requests for the given connection
|
|
367
380
|
*/
|
|
368
381
|
async handleConnection (connection: Connection) {
|
|
369
|
-
const daemon = this
|
|
382
|
+
const daemon = this // eslint-disable-line @typescript-eslint/no-this-alias
|
|
370
383
|
// @ts-expect-error connection may actually be a maconn?
|
|
371
384
|
const streamHandler = new StreamHandler({ stream: connection, maxLength: LIMIT })
|
|
372
385
|
|
|
@@ -375,119 +388,127 @@ export class Server implements Libp2pServer {
|
|
|
375
388
|
source => (async function * () {
|
|
376
389
|
let request: Request
|
|
377
390
|
|
|
378
|
-
for await (
|
|
391
|
+
for await (const buf of source) {
|
|
379
392
|
try {
|
|
380
393
|
request = Request.decode(buf)
|
|
381
|
-
} catch (err) {
|
|
382
|
-
yield ErrorResponse(new Error('ERR_INVALID_MESSAGE'))
|
|
383
|
-
continue
|
|
384
|
-
}
|
|
385
394
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
395
|
+
switch (request.type) {
|
|
396
|
+
// Connect to another peer
|
|
397
|
+
case Request.Type.CONNECT: {
|
|
398
|
+
try {
|
|
399
|
+
await daemon.connect(request)
|
|
400
|
+
} catch (err: any) {
|
|
401
|
+
yield ErrorResponse(err)
|
|
402
|
+
break
|
|
403
|
+
}
|
|
404
|
+
yield OkResponse()
|
|
393
405
|
break
|
|
394
406
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
407
|
+
// Get the daemon peer id and addresses
|
|
408
|
+
case Request.Type.IDENTIFY: {
|
|
409
|
+
yield OkResponse({
|
|
410
|
+
identify: {
|
|
411
|
+
id: daemon.libp2p.peerId.toBytes(),
|
|
412
|
+
addrs: daemon.libp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code)).map(m => m.bytes)
|
|
413
|
+
}
|
|
414
|
+
})
|
|
415
|
+
break
|
|
416
|
+
}
|
|
417
|
+
// Get a list of our current peers
|
|
418
|
+
case Request.Type.LIST_PEERS: {
|
|
419
|
+
const peers = []
|
|
420
|
+
const seen = new Set<string>()
|
|
421
|
+
|
|
422
|
+
for (const connection of daemon.libp2p.getConnections()) {
|
|
423
|
+
const peerId = connection.remotePeer.toString()
|
|
424
|
+
|
|
425
|
+
if (seen.has(peerId)) {
|
|
426
|
+
continue
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
seen.add(peerId)
|
|
430
|
+
|
|
431
|
+
peers.push({
|
|
432
|
+
id: connection.remotePeer.toBytes(),
|
|
433
|
+
addrs: [connection.remoteAddr.bytes]
|
|
434
|
+
})
|
|
404
435
|
}
|
|
405
|
-
})
|
|
406
|
-
break
|
|
407
|
-
}
|
|
408
|
-
// Get a list of our current peers
|
|
409
|
-
case Request.Type.LIST_PEERS: {
|
|
410
|
-
const peers = []
|
|
411
436
|
|
|
412
|
-
|
|
413
|
-
|
|
437
|
+
yield OkResponse({ peers })
|
|
438
|
+
break
|
|
439
|
+
}
|
|
440
|
+
case Request.Type.STREAM_OPEN: {
|
|
441
|
+
let response
|
|
442
|
+
try {
|
|
443
|
+
response = await daemon.openStream(request)
|
|
444
|
+
} catch (err: any) {
|
|
445
|
+
yield ErrorResponse(err)
|
|
446
|
+
break
|
|
447
|
+
}
|
|
414
448
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
449
|
+
// write the response
|
|
450
|
+
yield OkResponse({
|
|
451
|
+
streamInfo: response.streamInfo
|
|
418
452
|
})
|
|
419
|
-
}
|
|
420
453
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
break
|
|
454
|
+
const stream = streamHandler.rest()
|
|
455
|
+
// then pipe the connection to the client
|
|
456
|
+
await pipe(
|
|
457
|
+
stream,
|
|
458
|
+
response.connection,
|
|
459
|
+
stream
|
|
460
|
+
)
|
|
461
|
+
// Exit the iterator, no more requests can come through
|
|
462
|
+
return
|
|
431
463
|
}
|
|
464
|
+
case Request.Type.STREAM_HANDLER: {
|
|
465
|
+
try {
|
|
466
|
+
await daemon.registerStreamHandler(request)
|
|
467
|
+
} catch (err: any) {
|
|
468
|
+
yield ErrorResponse(err)
|
|
469
|
+
break
|
|
470
|
+
}
|
|
432
471
|
|
|
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)
|
|
472
|
+
// write the response
|
|
473
|
+
yield OkResponse()
|
|
453
474
|
break
|
|
454
475
|
}
|
|
476
|
+
case Request.Type.PEERSTORE: {
|
|
477
|
+
if (request.peerStore == null) {
|
|
478
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
479
|
+
break
|
|
480
|
+
}
|
|
455
481
|
|
|
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'))
|
|
482
|
+
yield * daemon.handlePeerStoreRequest(request.peerStore)
|
|
463
483
|
break
|
|
464
484
|
}
|
|
485
|
+
case Request.Type.PUBSUB: {
|
|
486
|
+
if (request.pubsub == null) {
|
|
487
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
488
|
+
break
|
|
489
|
+
}
|
|
465
490
|
|
|
466
|
-
|
|
467
|
-
break
|
|
468
|
-
}
|
|
469
|
-
case Request.Type.PUBSUB: {
|
|
470
|
-
if (request.pubsub == null) {
|
|
471
|
-
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
491
|
+
yield * daemon.handlePubsubRequest(request.pubsub)
|
|
472
492
|
break
|
|
473
493
|
}
|
|
494
|
+
case Request.Type.DHT: {
|
|
495
|
+
if (request.dht == null) {
|
|
496
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
497
|
+
break
|
|
498
|
+
}
|
|
474
499
|
|
|
475
|
-
|
|
476
|
-
break
|
|
477
|
-
}
|
|
478
|
-
case Request.Type.DHT: {
|
|
479
|
-
if (request.dht == null) {
|
|
480
|
-
yield ErrorResponse(new Error('ERR_INVALID_REQUEST'))
|
|
500
|
+
yield * daemon.handleDHTRequest(request.dht)
|
|
481
501
|
break
|
|
482
502
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
503
|
+
// Not yet supported or doesn't exist
|
|
504
|
+
default:
|
|
505
|
+
yield ErrorResponse(new Error('ERR_INVALID_REQUEST_TYPE'))
|
|
506
|
+
break
|
|
486
507
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
508
|
+
} catch (err: any) {
|
|
509
|
+
log.error(err)
|
|
510
|
+
yield ErrorResponse(err)
|
|
511
|
+
continue
|
|
491
512
|
}
|
|
492
513
|
}
|
|
493
514
|
})(),
|
|
@@ -503,7 +524,7 @@ export class Server implements Libp2pServer {
|
|
|
503
524
|
/**
|
|
504
525
|
* Creates a daemon from the provided Daemon Options
|
|
505
526
|
*/
|
|
506
|
-
export const createServer =
|
|
527
|
+
export const createServer = (multiaddr: Multiaddr, libp2pNode: Libp2p): Libp2pServer => {
|
|
507
528
|
const daemon = new Server({
|
|
508
529
|
multiaddr,
|
|
509
530
|
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"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { Duplex, Source } from 'it-stream-types';
|
|
2
|
-
export interface StreamHandlerOptions {
|
|
3
|
-
stream: Duplex<Uint8Array>;
|
|
4
|
-
maxLength?: number;
|
|
5
|
-
}
|
|
6
|
-
export declare class StreamHandler {
|
|
7
|
-
private stream;
|
|
8
|
-
private shake;
|
|
9
|
-
decoder: Source<Uint8Array>;
|
|
10
|
-
/**
|
|
11
|
-
* Create a stream handler for connection
|
|
12
|
-
*/
|
|
13
|
-
constructor(opts: StreamHandlerOptions);
|
|
14
|
-
/**
|
|
15
|
-
* Read and decode message
|
|
16
|
-
*/
|
|
17
|
-
read(): Promise<any>;
|
|
18
|
-
write(msg: Uint8Array): void;
|
|
19
|
-
/**
|
|
20
|
-
* Return the handshake rest stream and invalidate handler
|
|
21
|
-
*/
|
|
22
|
-
rest(): Duplex<Uint8Array, Uint8Array, Promise<void>>;
|
|
23
|
-
/**
|
|
24
|
-
* Close the stream
|
|
25
|
-
*/
|
|
26
|
-
close(): void;
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=stream-handler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream-handler.d.ts","sourceRoot":"","sources":["../../src/stream-handler.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAKrD,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAoB;IAClC,OAAO,CAAC,KAAK,CAAW;IACjB,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAClC;;OAEG;gBACU,IAAI,EAAE,oBAAoB;IAQvC;;OAEG;IACG,IAAI;IAWV,KAAK,CAAE,GAAG,EAAE,UAAU;IAOtB;;OAEG;IACH,IAAI;IAKJ;;OAEG;IACH,KAAK;CAIN"}
|