@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/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 './stream-handler.js'
9
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
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, peerIdFromString } from '@libp2p/peer-id'
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
- getMultiaddrs: () => Multiaddr[]
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 = peerIdFromString(uint8ArrayToString(peer, 'base58btc'))
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
- protocols.forEach((proto) => {
167
- // Connect the client socket with the libp2p connection
168
- 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
-
179
- pipe(
180
- [encodedMessage, stream.source],
181
- clientConnection,
182
- stream.sink
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
- getMultiaddrs (): Multiaddr[] {
219
- return this.listener.getAddrs()
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 || !this.pubsubOperations) {
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 || !this.dhtOperations) {
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 (let buf of source) {
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
- switch (request.type) {
387
- // Connect to another peer
388
- case Request.Type.CONNECT: {
389
- try {
390
- await daemon.connect(request)
391
- } catch (err: any) {
392
- yield ErrorResponse(err)
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
- yield OkResponse()
396
- break
397
- }
398
- // Get the daemon peer id and addresses
399
- case Request.Type.IDENTIFY: {
400
- yield OkResponse({
401
- identify: {
402
- id: daemon.libp2p.peerId.toBytes(),
403
- addrs: daemon.libp2p.getMultiaddrs().map(m => m.bytes)
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
- for (const peerId of daemon.libp2p.getPeers()) {
413
- const conn = daemon.libp2p.getConnections(peerId)[0]
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
- peers.push({
416
- id: peerId.toBytes(),
417
- addrs: [conn.remoteAddr.bytes]
439
+ // write the response
440
+ yield OkResponse({
441
+ streamInfo: response.streamInfo
418
442
  })
419
- }
420
443
 
421
- yield OkResponse({ peers })
422
- break
423
- }
424
- case Request.Type.STREAM_OPEN: {
425
- let response
426
- try {
427
- response = await daemon.openStream(request)
428
- } catch (err: any) {
429
- yield ErrorResponse(err.message)
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
- // write the response
434
- yield OkResponse({
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
- // write the response
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
- yield * daemon.handlePeerStoreRequest(request.peerStore)
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
- yield * daemon.handlePubsubRequest(request.pubsub)
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
- yield * daemon.handleDHTRequest(request.dht)
485
- break
493
+ // Not yet supported or doesn't exist
494
+ default:
495
+ yield ErrorResponse(new Error('ERR_INVALID_REQUEST_TYPE'))
496
+ break
486
497
  }
487
- // Not yet supported or doesn't exist
488
- default:
489
- yield ErrorResponse(new Error('ERR_INVALID_REQUEST_TYPE'))
490
- break
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 = async (multiaddr: Multiaddr, libp2pNode: Libp2p): Promise<Libp2pServer> => {
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
- yield OkResponse({
26
- pubsub: {
27
- topics: this.pubsub.getTopics()
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
- const onMessage = pushable<Uint8Array>()
34
-
35
- await this.pubsub.addEventListener(topic, (evt) => {
36
- const msg = evt.detail
37
-
38
- onMessage.push(PSMessage.encode({
39
- from: msg.from.toBytes(),
40
- data: msg.data,
41
- seqno: msg.sequenceNumber == null ? undefined : uint8ArrayFromString(msg.sequenceNumber.toString(16).padStart(16, '0'), 'base16'),
42
- topicIDs: [msg.topic],
43
- signature: msg.signature,
44
- key: msg.key
45
- }).finish())
46
- })
47
-
48
- yield OkResponse()
49
- yield * onMessage
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
- this.pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
54
- yield OkResponse()
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
- export function OkResponse (data?: Partial<IResponse>): Uint8Array {
6
+ export function OkResponse (data?: Partial<IResponse>): Uint8Array {
7
7
  return Response.encode({
8
8
  type: Response.Type.OK,
9
9
  ...data
@@ -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
@@ -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"}
@@ -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
@@ -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"}