@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.
@@ -1,39 +1,60 @@
1
1
  /* eslint max-depth: ["error", 6] */
2
2
  import { PSMessage } from '@libp2p/daemon-protocol';
3
- import { OkResponse } from './responses.js';
3
+ import { ErrorResponse, OkResponse } from './responses.js';
4
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
5
5
  import { pushable } from 'it-pushable';
6
+ import { CustomEvent } from '@libp2p/interfaces';
7
+ import { logger } from '@libp2p/logger';
8
+ const log = logger('libp2p:daemon-server:pubsub');
6
9
  export class PubSubOperations {
7
10
  constructor(init) {
8
11
  const { pubsub } = init;
9
12
  this.pubsub = pubsub;
10
13
  }
11
14
  async *getTopics() {
12
- yield OkResponse({
13
- pubsub: {
14
- topics: this.pubsub.getTopics()
15
- }
16
- });
15
+ try {
16
+ yield OkResponse({
17
+ pubsub: {
18
+ topics: this.pubsub.getTopics()
19
+ }
20
+ });
21
+ }
22
+ catch (err) {
23
+ log.error(err);
24
+ yield ErrorResponse(err);
25
+ }
17
26
  }
18
27
  async *subscribe(topic) {
19
- const onMessage = pushable();
20
- await this.pubsub.addEventListener(topic, (evt) => {
21
- const msg = evt.detail;
22
- onMessage.push(PSMessage.encode({
23
- from: msg.from.toBytes(),
24
- data: msg.data,
25
- seqno: msg.sequenceNumber == null ? undefined : uint8ArrayFromString(msg.sequenceNumber.toString(16).padStart(16, '0'), 'base16'),
26
- topicIDs: [msg.topic],
27
- signature: msg.signature,
28
- key: msg.key
29
- }).finish());
30
- });
31
- yield OkResponse();
32
- yield* onMessage;
28
+ try {
29
+ const onMessage = pushable();
30
+ await this.pubsub.addEventListener(topic, (evt) => {
31
+ const msg = evt.detail;
32
+ onMessage.push(PSMessage.encode({
33
+ from: msg.from.toBytes(),
34
+ data: msg.data,
35
+ seqno: msg.sequenceNumber == null ? undefined : uint8ArrayFromString(msg.sequenceNumber.toString(16).padStart(16, '0'), 'base16'),
36
+ topicIDs: [msg.topic],
37
+ signature: msg.signature,
38
+ key: msg.key
39
+ }).finish());
40
+ });
41
+ yield OkResponse();
42
+ yield* onMessage;
43
+ }
44
+ catch (err) {
45
+ log.error(err);
46
+ yield ErrorResponse(err);
47
+ }
33
48
  }
34
49
  async *publish(topic, data) {
35
- this.pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }));
36
- yield OkResponse();
50
+ try {
51
+ this.pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }));
52
+ yield OkResponse();
53
+ }
54
+ catch (err) {
55
+ log.error(err);
56
+ yield ErrorResponse(err);
57
+ }
37
58
  }
38
59
  }
39
60
  //# sourceMappingURL=pubsub.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"pubsub.js","sourceRoot":"","sources":["../../src/pubsub.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,EACL,SAAS,EACV,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAE3C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAMtC,MAAM,OAAO,gBAAgB;IAG3B,YAAa,IAA0B;QACrC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;QAEvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;IAED,KAAK,CAAC,CAAE,SAAS;QACf,MAAM,UAAU,CAAC;YACf,MAAM,EAAE;gBACN,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;aAChC;SACF,CAAC,CAAA;IACJ,CAAC;IAED,KAAK,CAAC,CAAE,SAAS,CAAE,KAAa;QAC9B,MAAM,SAAS,GAAG,QAAQ,EAAc,CAAA;QAExC,MAAM,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,KAAK,E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Q,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC;oBACrB,SAAS,EAAE,GAAG,CAAC,SAAS;oBACxB,GAAG,EAAE,GAAG,CAAC,GAAG;iBACb,CAAC,CAAC,MAAM,EAAE,CAAC,CAAA;YACd,CAAC,CAAC,CAAA;YAEF,MAAM,UAAU,EAAE,CAAA;YAClB,KAAM,CAAC,CAAC,SAAS,CAAA;SAClB;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACd,MAAM,aAAa,CAAC,GAAG,CAAC,CAAA;SACzB;IACH,CAAC;IAED,KAAK,CAAC,CAAE,OAAO,CAAE,KAAa,EAAE,IAAgB;QAC9C,IAAI;YACF,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;YACnE,MAAM,UAAU,EAAE,CAAA;SACnB;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACd,MAAM,aAAa,CAAC,GAAG,CAAC,CAAA;SACzB;IACH,CAAC;CACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"responses.d.ts","sourceRoot":"","sources":["../../src/responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAY,MAAM,yBAAyB,CAAA;AAE7D;;GAEG;AACF,wBAAgB,UAAU,CAAE,IAAI,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,UAAU,CAKlE;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAE,GAAG,EAAE,KAAK,GAAG,UAAU,CAOrD"}
1
+ {"version":3,"file":"responses.d.ts","sourceRoot":"","sources":["../../src/responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAY,MAAM,yBAAyB,CAAA;AAE7D;;GAEG;AACH,wBAAgB,UAAU,CAAE,IAAI,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,UAAU,CAKjE;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAE,GAAG,EAAE,KAAK,GAAG,UAAU,CAOrD"}
@@ -1 +1 @@
1
- {"version":3,"file":"responses.js","sourceRoot":"","sources":["../../src/responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAE7D;;GAEG;AACF,MAAM,UAAU,UAAU,CAAE,IAAyB;IACpD,OAAO,QAAQ,CAAC,MAAM,CAAC;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE;QACtB,GAAG,IAAI;KACR,CAAC,CAAC,MAAM,EAAE,CAAA;AACb,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAE,GAAU;IACvC,OAAO,QAAQ,CAAC,MAAM,CAAC;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,KAAK;QACzB,KAAK,EAAE;YACL,GAAG,EAAE,GAAG,CAAC,OAAO;SACjB;KACF,CAAC,CAAC,MAAM,EAAE,CAAA;AACb,CAAC"}
1
+ {"version":3,"file":"responses.js","sourceRoot":"","sources":["../../src/responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAE7D;;GAEG;AACH,MAAM,UAAU,UAAU,CAAE,IAAyB;IACnD,OAAO,QAAQ,CAAC,MAAM,CAAC;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE;QACtB,GAAG,IAAI;KACR,CAAC,CAAC,MAAM,EAAE,CAAA;AACb,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAE,GAAU;IACvC,OAAO,QAAQ,CAAC,MAAM,CAAC;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,KAAK;QACzB,KAAK,EAAE;YACL,GAAG,EAAE,GAAG,CAAC,OAAO;SACjB;KACF,CAAC,CAAC,MAAM,EAAE,CAAA;AACb,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@libp2p/daemon-server",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "API server for libp2p-daemon instances",
5
5
  "license": "Apache-2.0 OR MIT",
6
- "homepage": "https://github.com/libp2p/js-libp2p-daemon/tree/master/packages/libp2p-daemon#readme",
6
+ "homepage": "https://github.com/libp2p/js-libp2p-daemon/tree/master/packages/libp2p-daemon-server#readme",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/libp2p/js-libp2p-daemon.git"
@@ -18,9 +18,6 @@
18
18
  "node": ">=16.0.0",
19
19
  "npm": ">=7.0.0"
20
20
  },
21
- "bin": {
22
- "jsp2pd": "src/cli/bin.js"
23
- },
24
21
  "type": "module",
25
22
  "types": "./dist/src/index.d.ts",
26
23
  "files": [
@@ -130,30 +127,27 @@
130
127
  "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
131
128
  "build": "tsc",
132
129
  "pretest": "npm run build",
133
- "test": "aegir test -t node -f test/*.js test/**/*.js",
134
- "test:node": "aegir test -t node -f test/*.js test/**/*.js",
130
+ "test": "aegir test -t node -f dist/test/*.js dist/test/**/*.js",
131
+ "test:node": "aegir test -t node -f dist/test/*.js dist/test/**/*.js",
135
132
  "release": "semantic-release"
136
133
  },
137
134
  "dependencies": {
138
- "@chainsafe/libp2p-noise": "^6.0.0",
139
- "@libp2p/daemon-protocol": "^0.0.0",
135
+ "@libp2p/daemon-protocol": "^0.0.1",
140
136
  "@libp2p/interfaces": "^1.3.17",
137
+ "@libp2p/logger": "^1.1.2",
138
+ "@libp2p/tcp": "^1.0.6",
139
+ "@libp2p/peer-id": "^1.1.8",
141
140
  "@multiformats/multiaddr": "^10.1.8",
142
141
  "it-drain": "^1.0.5",
143
- "it-handshake": "^3.0.1",
144
142
  "it-length-prefixed": "^7.0.1",
145
143
  "it-pipe": "^2.0.3",
146
144
  "it-pushable": "^2.0.1",
147
- "multiformats": "^9.4.2",
145
+ "multiformats": "^9.6.4",
148
146
  "uint8arrays": "^3.0.0"
149
147
  },
150
148
  "devDependencies": {
151
149
  "aegir": "^36.0.0",
152
- "delay": "^5.0.0",
153
- "it-buffer": "^0.1.3",
154
- "it-pair": "^2.0.2",
155
- "mocha": "^9.1.1",
156
- "p-defer": "^4.0.0",
157
- "sinon": "^13.0.1"
150
+ "sinon": "^13.0.1",
151
+ "ts-sinon": "^2.0.2"
158
152
  }
159
153
  }
package/src/dht.ts CHANGED
@@ -1,20 +1,23 @@
1
1
  /* eslint max-depth: ["error", 6] */
2
2
 
3
3
  import {
4
- DHTResponse,
4
+ DHTResponse
5
5
  } from '@libp2p/daemon-protocol'
6
6
  import { ErrorResponse, OkResponse } from './responses.js'
7
7
  import type { PeerId } from '@libp2p/interfaces/peer-id'
8
8
  import type { DualDHT } from '@libp2p/interfaces/dht'
9
9
  import type { CID } from 'multiformats/cid'
10
10
  import drain from 'it-drain'
11
+ import { logger } from '@libp2p/logger'
12
+
13
+ const log = logger('libp2p:daemon-server:dht')
11
14
 
12
15
  export interface DHTOperationsInit {
13
16
  dht: DualDHT
14
17
  }
15
18
 
16
19
  export class DHTOperations {
17
- private dht: DualDHT
20
+ private readonly dht: DualDHT
18
21
 
19
22
  constructor (init: DHTOperationsInit) {
20
23
  const { dht } = init
@@ -23,8 +26,13 @@ export class DHTOperations {
23
26
  }
24
27
 
25
28
  async * provide (cid: CID) {
26
- await this.dht.provide(cid)
27
- yield OkResponse()
29
+ try {
30
+ await drain(this.dht.provide(cid))
31
+ yield OkResponse()
32
+ } catch (err: any) {
33
+ log.error(err)
34
+ yield ErrorResponse(err)
35
+ }
28
36
  }
29
37
 
30
38
  async * getClosestPeers (key: Uint8Array) {
@@ -50,22 +58,11 @@ export class DHTOperations {
50
58
 
51
59
  async * getPublicKey (peerId: PeerId) {
52
60
  yield ErrorResponse(new Error('FIX ME: not implemented'))
53
-
54
- /*
55
- const pubKey = await this.dht.getPublicKey(peerId)
56
-
57
- yield OkResponse({
58
- dht: {
59
- type: DHTResponse.Type.VALUE,
60
- value: pubKey.bytes
61
- }
62
- })
63
- */
64
61
  }
65
62
 
66
63
  async * getValue (key: Uint8Array) {
67
64
  try {
68
- for await (const event of this.dht.getClosestPeers(key)) {
65
+ for await (const event of this.dht.get(key)) {
69
66
  if (event.name === 'VALUE') {
70
67
  yield OkResponse({
71
68
  dht: {
@@ -76,7 +73,8 @@ export class DHTOperations {
76
73
  }
77
74
  }
78
75
  } catch (err: any) {
79
- yield ErrorResponse(err.message)
76
+ log.error(err)
77
+ yield ErrorResponse(err)
80
78
  }
81
79
  }
82
80
 
@@ -86,7 +84,8 @@ export class DHTOperations {
86
84
 
87
85
  yield OkResponse()
88
86
  } catch (err: any) {
89
- yield ErrorResponse(err.message)
87
+ log.error(err)
88
+ yield ErrorResponse(err)
90
89
  }
91
90
  }
92
91
 
@@ -108,6 +107,7 @@ export class DHTOperations {
108
107
 
109
108
  throw new Error('Peer not found')
110
109
  } catch (err: any) {
110
+ log.error(err)
111
111
  yield ErrorResponse(err)
112
112
  }
113
113
  }
@@ -132,13 +132,13 @@ export class DHTOperations {
132
132
  type: DHTResponse.Type.VALUE,
133
133
  peer: {
134
134
  id: provider.id.toBytes(),
135
- addrs: (provider.multiaddrs || []).map(m => m.bytes)
135
+ addrs: (provider.multiaddrs ?? []).map(m => m.bytes)
136
136
  }
137
137
  }).finish()
138
138
  }
139
139
 
140
140
  if (maxNumProviders === found) {
141
- return
141
+ break
142
142
  }
143
143
  }
144
144
  }