@libp2p/daemon-client 8.0.6 → 9.0.1
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/index.min.js +27 -4
- package/dist/src/dht.d.ts +1 -1
- package/dist/src/dht.d.ts.map +1 -1
- package/dist/src/dht.js +32 -30
- package/dist/src/dht.js.map +1 -1
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +32 -22
- 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 +17 -15
- package/dist/src/pubsub.js.map +1 -1
- package/dist/typedoc-urls.json +2 -0
- package/package.json +11 -10
- package/src/dht.ts +32 -31
- package/src/index.ts +33 -22
- package/src/pubsub.ts +17 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/daemon-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "libp2p-daemon client implementation",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-daemon/tree/main/packages/libp2p-daemon-client#readme",
|
|
@@ -46,24 +46,25 @@
|
|
|
46
46
|
"build": "aegir build",
|
|
47
47
|
"pretest": "npm run build",
|
|
48
48
|
"test": "aegir test -t node",
|
|
49
|
-
"test:node": "aegir test -t node"
|
|
49
|
+
"test:node": "aegir test -t node",
|
|
50
|
+
"release": "aegir release"
|
|
50
51
|
},
|
|
51
52
|
"dependencies": {
|
|
52
53
|
"@libp2p/daemon-protocol": "^6.0.0",
|
|
53
|
-
"@libp2p/interface": "^
|
|
54
|
-
"@libp2p/logger": "^
|
|
55
|
-
"@libp2p/peer-id": "^
|
|
56
|
-
"@libp2p/tcp": "^
|
|
54
|
+
"@libp2p/interface": "^2.0.0",
|
|
55
|
+
"@libp2p/logger": "^5.0.0",
|
|
56
|
+
"@libp2p/peer-id": "^5.0.0",
|
|
57
|
+
"@libp2p/tcp": "^10.0.0",
|
|
57
58
|
"@multiformats/multiaddr": "^12.1.14",
|
|
58
59
|
"it-protobuf-stream": "^1.1.2",
|
|
59
60
|
"multiformats": "^13.0.1"
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
|
-
"@chainsafe/libp2p-gossipsub": "^
|
|
63
|
+
"@chainsafe/libp2p-gossipsub": "^14.0.0",
|
|
63
64
|
"@libp2p/daemon-server": "^7.0.0",
|
|
64
|
-
"@libp2p/interface-compliance-tests": "^
|
|
65
|
-
"@libp2p/kad-dht": "^
|
|
66
|
-
"aegir": "^
|
|
65
|
+
"@libp2p/interface-compliance-tests": "^6.0.0",
|
|
66
|
+
"@libp2p/kad-dht": "^13.0.0",
|
|
67
|
+
"aegir": "^44.1.0",
|
|
67
68
|
"it-all": "^3.0.4",
|
|
68
69
|
"it-pipe": "^3.0.1",
|
|
69
70
|
"sinon": "^18.0.0",
|
package/src/dht.ts
CHANGED
|
@@ -4,13 +4,14 @@ import {
|
|
|
4
4
|
DHTRequest,
|
|
5
5
|
DHTResponse
|
|
6
6
|
} from '@libp2p/daemon-protocol'
|
|
7
|
-
import {
|
|
7
|
+
import { InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface'
|
|
8
8
|
import { isPeerId, type PeerId, type PeerInfo } from '@libp2p/interface'
|
|
9
9
|
import { logger } from '@libp2p/logger'
|
|
10
|
-
import {
|
|
10
|
+
import { peerIdFromMultihash } from '@libp2p/peer-id'
|
|
11
11
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
12
12
|
import { CID } from 'multiformats/cid'
|
|
13
|
-
import
|
|
13
|
+
import * as Digest from 'multiformats/hashes/digest'
|
|
14
|
+
import { OperationFailedError, type DaemonClient } from './index.js'
|
|
14
15
|
|
|
15
16
|
const log = logger('libp2p:daemon-client:dht')
|
|
16
17
|
|
|
@@ -26,11 +27,11 @@ export class DHT {
|
|
|
26
27
|
*/
|
|
27
28
|
async put (key: Uint8Array, value: Uint8Array): Promise<void> {
|
|
28
29
|
if (!(key instanceof Uint8Array)) {
|
|
29
|
-
throw new
|
|
30
|
+
throw new InvalidParametersError('invalid key received')
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
if (!(value instanceof Uint8Array)) {
|
|
33
|
-
throw new
|
|
34
|
+
throw new InvalidParametersError('value received is not a Uint8Array')
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
const sh = await this.client.send({
|
|
@@ -49,7 +50,7 @@ export class DHT {
|
|
|
49
50
|
await sh.unwrap().close()
|
|
50
51
|
|
|
51
52
|
if (response.type !== Response.Type.OK) {
|
|
52
|
-
throw new
|
|
53
|
+
throw new ProtocolError(response.error?.msg ?? 'DHT put failed')
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
|
|
@@ -58,7 +59,7 @@ export class DHT {
|
|
|
58
59
|
*/
|
|
59
60
|
async get (key: Uint8Array): Promise<Uint8Array> {
|
|
60
61
|
if (!(key instanceof Uint8Array)) {
|
|
61
|
-
throw new
|
|
62
|
+
throw new InvalidParametersError('invalid key received')
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
const sh = await this.client.send({
|
|
@@ -74,11 +75,11 @@ export class DHT {
|
|
|
74
75
|
await sh.unwrap().close()
|
|
75
76
|
|
|
76
77
|
if (response.type !== Response.Type.OK) {
|
|
77
|
-
throw new
|
|
78
|
+
throw new OperationFailedError(response.error?.msg ?? 'DHT get failed')
|
|
78
79
|
}
|
|
79
80
|
|
|
80
|
-
if (response.dht
|
|
81
|
-
throw new
|
|
81
|
+
if (response.dht?.value == null) {
|
|
82
|
+
throw new OperationFailedError('Invalid DHT get response')
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
return response.dht.value
|
|
@@ -89,14 +90,14 @@ export class DHT {
|
|
|
89
90
|
*/
|
|
90
91
|
async findPeer (peerId: PeerId): Promise<PeerInfo> {
|
|
91
92
|
if (!isPeerId(peerId)) {
|
|
92
|
-
throw new
|
|
93
|
+
throw new InvalidParametersError('invalid peer id received')
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
const sh = await this.client.send({
|
|
96
97
|
type: Request.Type.DHT,
|
|
97
98
|
dht: {
|
|
98
99
|
type: DHTRequest.Type.FIND_PEER,
|
|
99
|
-
peer: peerId.
|
|
100
|
+
peer: peerId.toMultihash().bytes
|
|
100
101
|
}
|
|
101
102
|
})
|
|
102
103
|
|
|
@@ -105,15 +106,15 @@ export class DHT {
|
|
|
105
106
|
await sh.unwrap().close()
|
|
106
107
|
|
|
107
108
|
if (response.type !== Response.Type.OK) {
|
|
108
|
-
throw new
|
|
109
|
+
throw new OperationFailedError(response.error?.msg ?? 'DHT find peer failed')
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
if (response.dht
|
|
112
|
-
throw new
|
|
112
|
+
if (response.dht?.peer?.addrs == null) {
|
|
113
|
+
throw new OperationFailedError('Invalid response')
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
return {
|
|
116
|
-
id:
|
|
117
|
+
id: peerIdFromMultihash(Digest.decode(response.dht.peer.id)),
|
|
117
118
|
multiaddrs: response.dht.peer.addrs.map((a) => multiaddr(a))
|
|
118
119
|
}
|
|
119
120
|
}
|
|
@@ -123,7 +124,7 @@ export class DHT {
|
|
|
123
124
|
*/
|
|
124
125
|
async provide (cid: CID): Promise<void> {
|
|
125
126
|
if (cid == null || CID.asCID(cid) == null) {
|
|
126
|
-
throw new
|
|
127
|
+
throw new InvalidParametersError('invalid cid received')
|
|
127
128
|
}
|
|
128
129
|
|
|
129
130
|
const sh = await this.client.send({
|
|
@@ -139,7 +140,7 @@ export class DHT {
|
|
|
139
140
|
await sh.unwrap().close()
|
|
140
141
|
|
|
141
142
|
if (response.type !== Response.Type.OK) {
|
|
142
|
-
throw new
|
|
143
|
+
throw new OperationFailedError(response.error?.msg ?? 'DHT provide failed')
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
|
|
@@ -148,7 +149,7 @@ export class DHT {
|
|
|
148
149
|
*/
|
|
149
150
|
async * findProviders (cid: CID, count: number = 1): AsyncIterable<PeerInfo> {
|
|
150
151
|
if (cid == null || CID.asCID(cid) == null) {
|
|
151
|
-
throw new
|
|
152
|
+
throw new InvalidParametersError('invalid cid received')
|
|
152
153
|
}
|
|
153
154
|
|
|
154
155
|
const sh = await this.client.send({
|
|
@@ -165,7 +166,7 @@ export class DHT {
|
|
|
165
166
|
|
|
166
167
|
if (response.type !== Response.Type.OK) {
|
|
167
168
|
await sh.unwrap().close()
|
|
168
|
-
throw new
|
|
169
|
+
throw new OperationFailedError(response.error?.msg ?? 'DHT find providers failed')
|
|
169
170
|
}
|
|
170
171
|
|
|
171
172
|
while (true) {
|
|
@@ -178,15 +179,15 @@ export class DHT {
|
|
|
178
179
|
}
|
|
179
180
|
|
|
180
181
|
// Stream values
|
|
181
|
-
if (dhtResponse.type === DHTResponse.Type.VALUE && dhtResponse.peer
|
|
182
|
+
if (dhtResponse.type === DHTResponse.Type.VALUE && dhtResponse.peer?.addrs != null) {
|
|
182
183
|
yield {
|
|
183
|
-
id:
|
|
184
|
+
id: peerIdFromMultihash(Digest.decode(dhtResponse.peer.id)),
|
|
184
185
|
multiaddrs: dhtResponse.peer.addrs.map((a) => multiaddr(a))
|
|
185
186
|
}
|
|
186
187
|
} else {
|
|
187
188
|
// Unexpected message received
|
|
188
189
|
await sh.unwrap().close()
|
|
189
|
-
throw new
|
|
190
|
+
throw new ProtocolError('unexpected message received')
|
|
190
191
|
}
|
|
191
192
|
}
|
|
192
193
|
}
|
|
@@ -196,7 +197,7 @@ export class DHT {
|
|
|
196
197
|
*/
|
|
197
198
|
async * getClosestPeers (key: Uint8Array): AsyncIterable<PeerInfo> {
|
|
198
199
|
if (!(key instanceof Uint8Array)) {
|
|
199
|
-
throw new
|
|
200
|
+
throw new InvalidParametersError('invalid key received')
|
|
200
201
|
}
|
|
201
202
|
|
|
202
203
|
const sh = await this.client.send({
|
|
@@ -212,7 +213,7 @@ export class DHT {
|
|
|
212
213
|
|
|
213
214
|
if (response.type !== Response.Type.OK) {
|
|
214
215
|
await sh.unwrap().close()
|
|
215
|
-
throw new
|
|
216
|
+
throw new OperationFailedError(response.error?.msg ?? 'DHT find providers failed')
|
|
216
217
|
}
|
|
217
218
|
|
|
218
219
|
while (true) {
|
|
@@ -226,7 +227,7 @@ export class DHT {
|
|
|
226
227
|
|
|
227
228
|
// Stream values
|
|
228
229
|
if (dhtResponse.type === DHTResponse.Type.VALUE && dhtResponse.value != null) {
|
|
229
|
-
const peerId =
|
|
230
|
+
const peerId = peerIdFromMultihash(Digest.decode(dhtResponse.value))
|
|
230
231
|
|
|
231
232
|
yield {
|
|
232
233
|
id: peerId,
|
|
@@ -235,7 +236,7 @@ export class DHT {
|
|
|
235
236
|
} else {
|
|
236
237
|
// Unexpected message received
|
|
237
238
|
await sh.unwrap().close()
|
|
238
|
-
throw new
|
|
239
|
+
throw new InvalidMessageError('unexpected message received')
|
|
239
240
|
}
|
|
240
241
|
}
|
|
241
242
|
}
|
|
@@ -245,14 +246,14 @@ export class DHT {
|
|
|
245
246
|
*/
|
|
246
247
|
async getPublicKey (peerId: PeerId): Promise<Uint8Array | undefined> {
|
|
247
248
|
if (!isPeerId(peerId)) {
|
|
248
|
-
throw new
|
|
249
|
+
throw new InvalidParametersError('invalid peer id received')
|
|
249
250
|
}
|
|
250
251
|
|
|
251
252
|
const sh = await this.client.send({
|
|
252
253
|
type: Request.Type.DHT,
|
|
253
254
|
dht: {
|
|
254
255
|
type: DHTRequest.Type.GET_PUBLIC_KEY,
|
|
255
|
-
peer: peerId.
|
|
256
|
+
peer: peerId.toMultihash().bytes
|
|
256
257
|
}
|
|
257
258
|
})
|
|
258
259
|
|
|
@@ -261,11 +262,11 @@ export class DHT {
|
|
|
261
262
|
await sh.unwrap().close()
|
|
262
263
|
|
|
263
264
|
if (response.type !== Response.Type.OK) {
|
|
264
|
-
throw new
|
|
265
|
+
throw new OperationFailedError(response.error?.msg ?? 'DHT get public key failed')
|
|
265
266
|
}
|
|
266
267
|
|
|
267
268
|
if (response.dht == null) {
|
|
268
|
-
throw new
|
|
269
|
+
throw new InvalidMessageError('Invalid response')
|
|
269
270
|
}
|
|
270
271
|
|
|
271
272
|
return response.dht.value
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { type PSMessage, Request, Response, StreamInfo } from '@libp2p/daemon-protocol'
|
|
2
2
|
import { StreamHandler } from '@libp2p/daemon-protocol/stream-handler'
|
|
3
3
|
import { passThroughUpgrader } from '@libp2p/daemon-protocol/upgrader'
|
|
4
|
-
import {
|
|
4
|
+
import { InvalidParametersError, isPeerId } from '@libp2p/interface'
|
|
5
5
|
import { defaultLogger, logger } from '@libp2p/logger'
|
|
6
|
-
import {
|
|
6
|
+
import { peerIdFromMultihash } from '@libp2p/peer-id'
|
|
7
7
|
import { tcp } from '@libp2p/tcp'
|
|
8
8
|
import { multiaddr, isMultiaddr } from '@multiformats/multiaddr'
|
|
9
9
|
import { pbStream, type ProtobufStream } from 'it-protobuf-stream'
|
|
10
|
+
import * as Digest from 'multiformats/hashes/digest'
|
|
10
11
|
import { DHT } from './dht.js'
|
|
11
12
|
import { Pubsub } from './pubsub.js'
|
|
12
13
|
import type { Stream, PeerId, MultiaddrConnection, PeerInfo, Transport } from '@libp2p/interface'
|
|
@@ -15,6 +16,13 @@ import type { CID } from 'multiformats/cid'
|
|
|
15
16
|
|
|
16
17
|
const log = logger('libp2p:daemon-client')
|
|
17
18
|
|
|
19
|
+
export class OperationFailedError extends Error {
|
|
20
|
+
constructor (message = 'Operation failed') {
|
|
21
|
+
super(message)
|
|
22
|
+
this.name = 'OperationFailedError'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
18
26
|
class Client implements DaemonClient {
|
|
19
27
|
private readonly multiaddr: Multiaddr
|
|
20
28
|
public dht: DHT
|
|
@@ -66,23 +74,23 @@ class Client implements DaemonClient {
|
|
|
66
74
|
*/
|
|
67
75
|
async connect (peerId: PeerId, addrs: Multiaddr[]): Promise<void> {
|
|
68
76
|
if (!isPeerId(peerId)) {
|
|
69
|
-
throw new
|
|
77
|
+
throw new InvalidParametersError('invalid peer id received')
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
if (!Array.isArray(addrs)) {
|
|
73
|
-
throw new
|
|
81
|
+
throw new InvalidParametersError('addrs received are not in an array')
|
|
74
82
|
}
|
|
75
83
|
|
|
76
84
|
addrs.forEach((addr) => {
|
|
77
85
|
if (!isMultiaddr(addr)) {
|
|
78
|
-
throw new
|
|
86
|
+
throw new InvalidParametersError('received an address that is not a multiaddr')
|
|
79
87
|
}
|
|
80
88
|
})
|
|
81
89
|
|
|
82
90
|
const sh = await this.send({
|
|
83
91
|
type: Request.Type.CONNECT,
|
|
84
92
|
connect: {
|
|
85
|
-
peer: peerId.
|
|
93
|
+
peer: peerId.toMultihash().bytes,
|
|
86
94
|
addrs: addrs.map((a) => a.bytes)
|
|
87
95
|
}
|
|
88
96
|
})
|
|
@@ -91,7 +99,7 @@ class Client implements DaemonClient {
|
|
|
91
99
|
|
|
92
100
|
if (response.type !== Response.Type.OK) {
|
|
93
101
|
const errResponse = response.error ?? { msg: 'unspecified' }
|
|
94
|
-
throw new
|
|
102
|
+
throw new OperationFailedError(errResponse.msg ?? 'unspecified')
|
|
95
103
|
}
|
|
96
104
|
|
|
97
105
|
await sh.unwrap().close()
|
|
@@ -114,14 +122,14 @@ class Client implements DaemonClient {
|
|
|
114
122
|
const response = await sh.read(Response)
|
|
115
123
|
|
|
116
124
|
if (response.type !== Response.Type.OK) {
|
|
117
|
-
throw new
|
|
125
|
+
throw new OperationFailedError(response.error?.msg ?? 'Identify failed')
|
|
118
126
|
}
|
|
119
127
|
|
|
120
|
-
if (response.identify
|
|
121
|
-
throw new
|
|
128
|
+
if (response.identify?.addrs == null) {
|
|
129
|
+
throw new OperationFailedError('Invalid response')
|
|
122
130
|
}
|
|
123
131
|
|
|
124
|
-
const peerId =
|
|
132
|
+
const peerId = peerIdFromMultihash(Digest.decode(response.identify?.id))
|
|
125
133
|
const addrs = response.identify.addrs.map((a) => multiaddr(a))
|
|
126
134
|
|
|
127
135
|
await sh.unwrap().close()
|
|
@@ -140,12 +148,12 @@ class Client implements DaemonClient {
|
|
|
140
148
|
const response = await sh.read(Response)
|
|
141
149
|
|
|
142
150
|
if (response.type !== Response.Type.OK) {
|
|
143
|
-
throw new
|
|
151
|
+
throw new OperationFailedError(response.error?.msg ?? 'List peers failed')
|
|
144
152
|
}
|
|
145
153
|
|
|
146
154
|
await sh.unwrap().close()
|
|
147
155
|
|
|
148
|
-
return response.peers.map((peer) =>
|
|
156
|
+
return response.peers.map((peer) => peerIdFromMultihash(Digest.decode(peer.id)))
|
|
149
157
|
}
|
|
150
158
|
|
|
151
159
|
/**
|
|
@@ -153,17 +161,17 @@ class Client implements DaemonClient {
|
|
|
153
161
|
*/
|
|
154
162
|
async openStream (peerId: PeerId, protocol: string): Promise<MultiaddrConnection> {
|
|
155
163
|
if (!isPeerId(peerId)) {
|
|
156
|
-
throw new
|
|
164
|
+
throw new InvalidParametersError('invalid peer id received')
|
|
157
165
|
}
|
|
158
166
|
|
|
159
167
|
if (typeof protocol !== 'string') {
|
|
160
|
-
throw new
|
|
168
|
+
throw new InvalidParametersError('invalid protocol received')
|
|
161
169
|
}
|
|
162
170
|
|
|
163
171
|
const sh = await this.send({
|
|
164
172
|
type: Request.Type.STREAM_OPEN,
|
|
165
173
|
streamOpen: {
|
|
166
|
-
peer: peerId.
|
|
174
|
+
peer: peerId.toMultihash().bytes,
|
|
167
175
|
proto: [protocol]
|
|
168
176
|
}
|
|
169
177
|
})
|
|
@@ -172,7 +180,7 @@ class Client implements DaemonClient {
|
|
|
172
180
|
|
|
173
181
|
if (response.type !== Response.Type.OK) {
|
|
174
182
|
await sh.unwrap().close()
|
|
175
|
-
throw new
|
|
183
|
+
throw new OperationFailedError(response.error?.msg ?? 'Open stream failed')
|
|
176
184
|
}
|
|
177
185
|
|
|
178
186
|
return sh.unwrap()
|
|
@@ -183,7 +191,7 @@ class Client implements DaemonClient {
|
|
|
183
191
|
*/
|
|
184
192
|
async registerStreamHandler (protocol: string, handler: StreamHandlerFunction): Promise<void> {
|
|
185
193
|
if (typeof protocol !== 'string') {
|
|
186
|
-
throw new
|
|
194
|
+
throw new InvalidParametersError('invalid protocol received')
|
|
187
195
|
}
|
|
188
196
|
|
|
189
197
|
// open a tcp port, pipe any data from it to the handler function
|
|
@@ -199,18 +207,21 @@ class Client implements DaemonClient {
|
|
|
199
207
|
const message = await sh.read()
|
|
200
208
|
|
|
201
209
|
if (message == null) {
|
|
202
|
-
throw new
|
|
210
|
+
throw new OperationFailedError('Could not read open stream response')
|
|
203
211
|
}
|
|
204
212
|
|
|
205
213
|
const response = StreamInfo.decode(message)
|
|
206
214
|
|
|
207
215
|
if (response.proto !== protocol) {
|
|
208
|
-
throw new
|
|
216
|
+
throw new OperationFailedError('Incorrect protocol')
|
|
209
217
|
}
|
|
210
218
|
|
|
211
219
|
// @ts-expect-error because we are using a passthrough upgrader, this is a MultiaddrConnection
|
|
212
220
|
await handler(sh.rest())
|
|
213
221
|
})
|
|
222
|
+
.catch(err => {
|
|
223
|
+
connection.abort(err)
|
|
224
|
+
})
|
|
214
225
|
.finally(() => {
|
|
215
226
|
connection.close()
|
|
216
227
|
.catch(err => {
|
|
@@ -227,7 +238,7 @@ class Client implements DaemonClient {
|
|
|
227
238
|
const address = listener.getAddrs()[0]
|
|
228
239
|
|
|
229
240
|
if (address == null) {
|
|
230
|
-
throw new
|
|
241
|
+
throw new OperationFailedError('Could not listen on port')
|
|
231
242
|
}
|
|
232
243
|
|
|
233
244
|
const sh = await this.send({
|
|
@@ -243,7 +254,7 @@ class Client implements DaemonClient {
|
|
|
243
254
|
await sh.unwrap().close()
|
|
244
255
|
|
|
245
256
|
if (response.type !== Response.Type.OK) {
|
|
246
|
-
throw new
|
|
257
|
+
throw new OperationFailedError(response.error?.msg ?? 'Register stream handler failed')
|
|
247
258
|
}
|
|
248
259
|
}
|
|
249
260
|
}
|
package/src/pubsub.ts
CHANGED
|
@@ -4,9 +4,10 @@ import {
|
|
|
4
4
|
PSRequest,
|
|
5
5
|
PSMessage
|
|
6
6
|
} from '@libp2p/daemon-protocol'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
7
|
+
import { InvalidParametersError } from '@libp2p/interface'
|
|
8
|
+
import { peerIdFromMultihash } from '@libp2p/peer-id'
|
|
9
|
+
import * as Digest from 'multiformats/hashes/digest'
|
|
10
|
+
import { OperationFailedError, type DaemonClient, type Subscription } from './index.js'
|
|
10
11
|
import type { PeerId } from '@libp2p/interface'
|
|
11
12
|
|
|
12
13
|
export class Pubsub {
|
|
@@ -34,11 +35,11 @@ export class Pubsub {
|
|
|
34
35
|
await sh.unwrap().close()
|
|
35
36
|
|
|
36
37
|
if (response.type !== Response.Type.OK) {
|
|
37
|
-
throw new
|
|
38
|
+
throw new OperationFailedError(response.error?.msg ?? 'Pubsub get topics failed')
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
if (response.pubsub
|
|
41
|
-
throw new
|
|
41
|
+
if (response.pubsub?.topics == null) {
|
|
42
|
+
throw new OperationFailedError('Invalid response')
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
return response.pubsub.topics
|
|
@@ -49,11 +50,11 @@ export class Pubsub {
|
|
|
49
50
|
*/
|
|
50
51
|
async publish (topic: string, data: Uint8Array): Promise<void> {
|
|
51
52
|
if (typeof topic !== 'string') {
|
|
52
|
-
throw new
|
|
53
|
+
throw new InvalidParametersError('invalid topic received')
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
if (!(data instanceof Uint8Array)) {
|
|
56
|
-
throw new
|
|
57
|
+
throw new InvalidParametersError('data received is not a Uint8Array')
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
const sh = await this.client.send({
|
|
@@ -70,7 +71,7 @@ export class Pubsub {
|
|
|
70
71
|
await sh.unwrap().close()
|
|
71
72
|
|
|
72
73
|
if (response.type !== Response.Type.OK) {
|
|
73
|
-
throw new
|
|
74
|
+
throw new OperationFailedError(response.error?.msg ?? 'Pubsub publish failed')
|
|
74
75
|
}
|
|
75
76
|
}
|
|
76
77
|
|
|
@@ -79,7 +80,7 @@ export class Pubsub {
|
|
|
79
80
|
*/
|
|
80
81
|
async subscribe (topic: string): Promise<Subscription> {
|
|
81
82
|
if (typeof topic !== 'string') {
|
|
82
|
-
throw new
|
|
83
|
+
throw new InvalidParametersError('invalid topic received')
|
|
83
84
|
}
|
|
84
85
|
|
|
85
86
|
const sh = await this.client.send({
|
|
@@ -93,7 +94,7 @@ export class Pubsub {
|
|
|
93
94
|
const response = await sh.read(Response)
|
|
94
95
|
|
|
95
96
|
if (response.type !== Response.Type.OK) {
|
|
96
|
-
throw new
|
|
97
|
+
throw new OperationFailedError(response.error?.msg ?? 'Pubsub publish failed')
|
|
97
98
|
}
|
|
98
99
|
|
|
99
100
|
let subscribed = true
|
|
@@ -115,7 +116,7 @@ export class Pubsub {
|
|
|
115
116
|
|
|
116
117
|
async getSubscribers (topic: string): Promise<PeerId[]> {
|
|
117
118
|
if (typeof topic !== 'string') {
|
|
118
|
-
throw new
|
|
119
|
+
throw new InvalidParametersError('invalid topic received')
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
const sh = await this.client.send({
|
|
@@ -131,13 +132,13 @@ export class Pubsub {
|
|
|
131
132
|
await sh.unwrap().close()
|
|
132
133
|
|
|
133
134
|
if (response.type !== Response.Type.OK) {
|
|
134
|
-
throw new
|
|
135
|
+
throw new OperationFailedError(response.error?.msg ?? 'Pubsub get subscribers failed')
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
if (response.pubsub
|
|
138
|
-
throw new
|
|
138
|
+
if (response.pubsub?.topics == null) {
|
|
139
|
+
throw new OperationFailedError('Invalid response')
|
|
139
140
|
}
|
|
140
141
|
|
|
141
|
-
return response.pubsub.peerIDs.map(buf =>
|
|
142
|
+
return response.pubsub.peerIDs.map(buf => peerIdFromMultihash(Digest.decode(buf)))
|
|
142
143
|
}
|
|
143
144
|
}
|